Back to index

lightning-sunbird  0.9+nobinonly
nsTAString.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=2 sw=2 sts=2 et cindent: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla.
00017  *
00018  * The Initial Developer of the Original Code is IBM Corporation.
00019  * Portions created by IBM Corporation are Copyright (C) 2003
00020  * IBM Corporation. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Scott Collins <scc@mozilla.org> (original author)
00024  *   Darin Fisher <darin@meer.net>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef MOZILLA_INTERNAL_API
00041 #error Cannot use internal string classes without MOZILLA_INTERNAL_API defined. Use the frozen header nsStringAPI.h instead.
00042 #endif
00043 
00047 class NS_COM nsTStringComparator_CharT
00048   {
00049     public:
00050       typedef CharT char_type;
00051 
00052       nsTStringComparator_CharT() {}
00053 
00054       virtual int operator()( const char_type*, const char_type*, PRUint32 length ) const = 0;
00055       virtual int operator()( char_type, char_type ) const = 0;
00056   };
00057 
00058 
00062 class NS_COM nsTDefaultStringComparator_CharT
00063     : public nsTStringComparator_CharT
00064   {
00065     public:
00066       typedef CharT char_type;
00067 
00068       nsTDefaultStringComparator_CharT() {}
00069 
00070       virtual int operator()( const char_type*, const char_type*, PRUint32 length ) const;
00071       virtual int operator()( char_type, char_type ) const;
00072   };
00073 
00074 
00106 class nsTAString_CharT
00107   {
00108     public:
00109 
00110       typedef CharT                                  char_type;
00111       typedef nsCharTraits<char_type>                char_traits;
00112 
00113       typedef char_traits::incompatible_char_type    incompatible_char_type;
00114 
00115       typedef nsTAString_CharT                       self_type;
00116       typedef nsTAString_CharT                       abstract_string_type;
00117       typedef nsTObsoleteAString_CharT               obsolete_string_type;
00118       typedef nsTSubstring_CharT                     substring_type;
00119       typedef nsTSubstringTuple_CharT                substring_tuple_type;
00120 
00121       typedef nsReadingIterator<char_type>           const_iterator;
00122       typedef nsWritingIterator<char_type>           iterator;
00123 
00124       typedef nsTStringComparator_CharT              comparator_type;
00125 
00126       typedef PRUint32                               size_type;
00127       typedef PRUint32                               index_type;
00128 
00129 #ifdef MOZ_V1_STRING_ABI
00130     public:
00131 
00132         // this acts like a virtual destructor
00133       NS_COM NS_FASTCALL ~nsTAString_CharT();
00134 
00135 
00143       inline const_iterator& BeginReading( const_iterator& iter ) const
00144         {
00145           size_type len = GetReadableBuffer(&iter.mStart);
00146           iter.mEnd = iter.mStart + len;
00147           iter.mPosition = iter.mStart;
00148           return iter;
00149         }
00150 
00151       inline const_iterator& EndReading( const_iterator& iter ) const
00152         {
00153           size_type len = GetReadableBuffer(&iter.mStart);
00154           iter.mEnd = iter.mStart + len;
00155           iter.mPosition = iter.mEnd;
00156           return iter;
00157         }
00158 
00159       inline const char_type* BeginReading() const
00160         {
00161           const char_type *b;
00162           GetReadableBuffer(&b);
00163           return b;
00164         }
00165 
00166       inline const char_type* EndReading() const
00167         {
00168           const char_type *b;
00169           size_type len = GetReadableBuffer(&b);
00170           return b + len;
00171         }
00172 
00173 
00181       inline iterator& BeginWriting( iterator& iter )
00182         {
00183           size_type len = GetWritableBuffer(&iter.mStart);
00184           iter.mEnd = iter.mStart + len;
00185           iter.mPosition = iter.mStart;
00186           return iter;
00187         }
00188 
00189       inline iterator& EndWriting( iterator& iter )
00190         {
00191           size_type len = GetWritableBuffer(&iter.mStart);
00192           iter.mEnd = iter.mStart + len;
00193           iter.mPosition = iter.mEnd;
00194           return iter;
00195         }
00196 
00197       inline char_type* BeginWriting()
00198         {
00199           char_type *b;
00200           GetWritableBuffer(&b);
00201           return b;
00202         }
00203 
00204       inline char_type* EndWriting()
00205         {
00206           char_type *b;
00207           size_type len = GetWritableBuffer(&b);
00208           return b + len;
00209         }
00210 
00216       NS_COM size_type NS_FASTCALL Length() const;
00217       PRBool IsEmpty() const { return Length() == 0; }
00218 
00219 
00226       NS_COM PRBool NS_FASTCALL Equals( const self_type& ) const;
00227       NS_COM PRBool NS_FASTCALL Equals( const self_type&, const comparator_type& ) const;
00228       NS_COM PRBool NS_FASTCALL Equals( const char_type* ) const;
00229       NS_COM PRBool NS_FASTCALL Equals( const char_type*, const comparator_type& ) const;
00230 
00236       NS_COM PRBool NS_FASTCALL EqualsASCII( const char* data, size_type len ) const;
00242       NS_COM PRBool NS_FASTCALL EqualsASCII( const char* data ) const;
00243 
00244     // EqualsLiteral must ONLY be applied to an actual literal string.
00245     // Do not attempt to use it with a regular char* pointer, or with a char
00246     // array variable.
00247     // The template trick to acquire the array length at compile time without
00248     // using a macro is due to Corey Kosak, with much thanks.
00249 #ifdef NS_DISABLE_LITERAL_TEMPLATE
00250       inline PRBool EqualsLiteral( const char* str ) const
00251         {
00252           return EqualsASCII(str);
00253         }
00254 #else
00255       template<int N>
00256       inline PRBool EqualsLiteral( const char (&str)[N] ) const
00257         {
00258           return EqualsASCII(str, N-1);
00259         }
00260       template<int N>
00261       inline PRBool EqualsLiteral( char (&str)[N] ) const
00262         {
00263           const char* s = str;
00264           return EqualsASCII(s, N-1);
00265         }
00266 #endif
00267 
00268     // The LowerCaseEquals methods compare the lower case version of
00269     // this string to some ASCII/Literal string. The ASCII string is
00270     // *not* lowercased for you. If you compare to an ASCII or literal
00271     // string that contains an uppercase character, it is guaranteed to
00272     // return false. We will throw assertions too.
00273       NS_COM PRBool NS_FASTCALL LowerCaseEqualsASCII( const char* data, size_type len ) const;
00274       NS_COM PRBool NS_FASTCALL LowerCaseEqualsASCII( const char* data ) const;
00275 
00276     // LowerCaseEqualsLiteral must ONLY be applied to an actual
00277     // literal string.  Do not attempt to use it with a regular char*
00278     // pointer, or with a char array variable. Use
00279     // LowerCaseEqualsASCII for them.
00280 #ifdef NS_DISABLE_LITERAL_TEMPLATE
00281       inline PRBool LowerCaseEqualsLiteral( const char* str ) const
00282         {
00283           return LowerCaseEqualsASCII(str);
00284         }
00285 #else
00286       template<int N>
00287       inline PRBool LowerCaseEqualsLiteral( const char (&str)[N] ) const
00288         {
00289           return LowerCaseEqualsASCII(str, N-1);
00290         }
00291       template<int N>
00292       inline PRBool LowerCaseEqualsLiteral( char (&str)[N] ) const
00293         {
00294           const char* s = str;
00295           return LowerCaseEqualsASCII(s, N-1);
00296         }
00297 #endif
00298 
00307       NS_COM PRBool NS_FASTCALL IsVoid() const;
00308       NS_COM void NS_FASTCALL SetIsVoid( PRBool );
00309 
00310 
00318       NS_COM PRBool NS_FASTCALL IsTerminated() const;
00319 
00320 
00324       NS_COM char_type NS_FASTCALL First() const;
00325       NS_COM char_type NS_FASTCALL Last() const;
00326 
00327 
00331       NS_COM size_type NS_FASTCALL CountChar( char_type ) const;
00332 
00333 
00339       NS_COM PRInt32 NS_FASTCALL FindChar( char_type, index_type offset = 0 ) const;
00340 
00341 
00348       NS_COM void NS_FASTCALL SetCapacity( size_type );
00349 
00350 
00371       NS_COM void NS_FASTCALL SetLength( size_type );
00372 
00373 
00377       void Truncate( size_type aNewLength=0 )
00378         {
00379           NS_ASSERTION(aNewLength <= Length(), "Truncate cannot make string longer");
00380           SetLength(aNewLength);
00381         }
00382 
00383 
00391       NS_COM void NS_FASTCALL Assign( const self_type& readable );
00392       NS_COM void NS_FASTCALL Assign( const substring_tuple_type& tuple );
00393       NS_COM void NS_FASTCALL Assign( const char_type* data );
00394       NS_COM void NS_FASTCALL Assign( const char_type* data, size_type length );
00395       NS_COM void NS_FASTCALL Assign( char_type c );
00396 
00397       NS_COM void NS_FASTCALL AssignASCII( const char* data, size_type length );
00398       NS_COM void NS_FASTCALL AssignASCII( const char* data );
00399 
00400     // AssignLiteral must ONLY be applied to an actual literal string.
00401     // Do not attempt to use it with a regular char* pointer, or with a char
00402     // array variable. Use AssignASCII for those.
00403 #ifdef NS_DISABLE_LITERAL_TEMPLATE
00404       void AssignLiteral( const char* str )
00405                   { AssignASCII(str); }
00406 #else
00407       template<int N>
00408       void AssignLiteral( const char (&str)[N] )
00409                   { AssignASCII(str, N-1); }
00410       template<int N>
00411       void AssignLiteral( char (&str)[N] )
00412                   { AssignASCII(str, N-1); }
00413 #endif
00414 
00415         // copy-assignment operator.  I must define my own if I don't want the compiler to make me one
00416       self_type& operator=( const self_type& readable )                                             { Assign(readable); return *this; }
00417       self_type& operator=( const substring_tuple_type& tuple )                                     { Assign(tuple); return *this; }
00418       self_type& operator=( const char_type* data )                                                 { Assign(data); return *this; }
00419       self_type& operator=( char_type c )                                                           { Assign(c); return *this; }
00420 
00421 
00422 
00427       NS_COM void NS_FASTCALL Append( const self_type& readable );
00428       NS_COM void NS_FASTCALL Append( const substring_tuple_type& tuple );
00429       NS_COM void NS_FASTCALL Append( const char_type* data );
00430       NS_COM void NS_FASTCALL Append( const char_type* data, size_type length );
00431       NS_COM void NS_FASTCALL Append( char_type c );
00432 
00433       NS_COM void NS_FASTCALL AppendASCII( const char* data, size_type length );
00434       NS_COM void NS_FASTCALL AppendASCII( const char* data );
00435 
00436     // AppendLiteral must ONLY be applied to an actual literal string.
00437     // Do not attempt to use it with a regular char* pointer, or with a char
00438     // array variable. Use AppendASCII for those.
00439 #ifdef NS_DISABLE_LITERAL_TEMPLATE
00440       void AppendLiteral( const char* str )
00441                   { AppendASCII(str); }
00442 #else
00443       template<int N>
00444       void AppendLiteral( const char (&str)[N] )
00445                   { AppendASCII(str, N-1); }
00446       template<int N>
00447       void AppendLiteral( char (&str)[N] )
00448                   { AppendASCII(str, N-1); }
00449 #endif
00450 
00451       self_type& operator+=( const self_type& readable )                                            { Append(readable); return *this; }
00452       self_type& operator+=( const substring_tuple_type& tuple )                                    { Append(tuple); return *this; }
00453       self_type& operator+=( const char_type* data )                                                { Append(data); return *this; }
00454       self_type& operator+=( char_type c )                                                          { Append(c); return *this; }
00455 
00456 
00462       NS_COM void NS_FASTCALL Insert( const self_type& readable, index_type pos );
00463       NS_COM void NS_FASTCALL Insert( const substring_tuple_type& tuple, index_type pos );
00464       NS_COM void NS_FASTCALL Insert( const char_type* data, index_type pos );
00465       NS_COM void NS_FASTCALL Insert( const char_type* data, index_type pos, size_type length );
00466       NS_COM void NS_FASTCALL Insert( char_type c, index_type pos );
00467 
00468 
00473       NS_COM void NS_FASTCALL Cut( index_type cutStart, size_type cutLength );
00474 
00475       
00480       NS_COM void NS_FASTCALL Replace( index_type cutStart, size_type cutLength, const self_type& readable );
00481       NS_COM void NS_FASTCALL Replace( index_type cutStart, size_type cutLength, const substring_tuple_type& readable );
00482 
00483       
00488       nsTAString_CharT(const substring_tuple_type& tuple)
00489         : mVTable(obsolete_string_type::sCanonicalVTable)
00490         , mData(nsnull)
00491         , mLength(0)
00492         , mFlags(0)
00493         {
00494           Assign(tuple);
00495         }
00496 
00497     protected:
00498 
00499       friend class nsTSubstringTuple_CharT;
00500 
00501       // GCC 3.2 erroneously needs these (even though they are subclasses!)
00502       friend class nsTSubstring_CharT;
00503       friend class nsTDependentSubstring_CharT;
00504       friend class nsTPromiseFlatString_CharT;
00505 
00510       const void* mVTable;
00511 
00518       char_type*  mData;
00519       size_type   mLength;
00520       PRUint32    mFlags;
00521 
00525       nsTAString_CharT(char_type* data, size_type length, PRUint32 flags)
00526         : mVTable(obsolete_string_type::sCanonicalVTable)
00527         , mData(data)
00528         , mLength(length)
00529         , mFlags(flags)
00530         {}
00531 
00537       explicit
00538       nsTAString_CharT(PRUint32 flags)
00539         : mVTable(obsolete_string_type::sCanonicalVTable)
00540         , mFlags(flags)
00541         {}
00542 
00547       NS_COM size_type NS_FASTCALL GetReadableBuffer( const char_type **data ) const;
00548       NS_COM size_type NS_FASTCALL GetWritableBuffer(       char_type **data );
00549 
00554       PRBool NS_FASTCALL IsDependentOn(const char_type *start, const char_type *end) const;
00555 
00559       const substring_type NS_FASTCALL ToSubstring() const;
00560 
00565       const obsolete_string_type* AsObsoleteString() const
00566         {
00567           return NS_REINTERPRET_CAST(const obsolete_string_type*, this);
00568         }
00569 
00570       obsolete_string_type* AsObsoleteString()
00571         {
00572           return NS_REINTERPRET_CAST(obsolete_string_type*, this);
00573         }
00574 
00575       const substring_type* AsSubstring() const
00576         {
00577           return NS_REINTERPRET_CAST(const substring_type*, this);
00578         }
00579 
00580       substring_type* AsSubstring()
00581         {
00582           return NS_REINTERPRET_CAST(substring_type*, this);
00583         }
00584 
00585     private:
00586 
00587         // GCC 2.95.3, EGCS-2.91.66, Sun Workshop/Forte, and IBM VisualAge C++
00588         // require a public copy-constructor in order to support automatic
00589         // construction of a nsTAString from a nsTSubstringTuple.  I believe
00590         // enabling the default copy-constructor is harmless, but I do not want
00591         // it to be enabled by default because that might tempt people into
00592         // using it (where it would be invalid).
00593 #if !defined(__SUNPRO_CC) && \
00594    !(defined(_AIX) && defined(__IBMCPP__)) && \
00595    (!defined(__GNUC__) || __GNUC__ > 2 || __GNUC_MINOR__ > 95)
00596 
00597         // NOT TO BE IMPLEMENTED
00598       nsTAString_CharT( const self_type& );
00599 
00600 #endif
00601 
00602         // NOT TO BE IMPLEMENTED
00603       void operator=     ( incompatible_char_type );
00604       void Assign        ( incompatible_char_type );
00605       void operator+=    ( incompatible_char_type );
00606       void Append        ( incompatible_char_type );
00607       void Insert        ( incompatible_char_type, index_type );
00608 #endif
00609   };