Back to index

lightning-sunbird  0.9+nobinonly
nsTString.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  *   Rick Gessner <rickg@netscape.com> (original author)
00024  *   Scott Collins <scc@mozilla.org>
00025  *   Darin Fisher <darin@meer.net>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 
00050 class nsTString_CharT : public nsTSubstring_CharT
00051   {
00052     public:
00053 
00054       typedef nsTString_CharT    self_type;
00055 
00056     public:
00057 
00062       nsTString_CharT()
00063         : substring_type() {}
00064 
00065       explicit
00066       nsTString_CharT( char_type c )
00067         : substring_type()
00068         {
00069           Assign(c);
00070         }
00071 
00072       explicit
00073       nsTString_CharT( const char_type* data, size_type length = size_type(-1) )
00074         : substring_type()
00075         {
00076           Assign(data, length);
00077         }
00078 
00079       nsTString_CharT( const self_type& str )
00080         : substring_type()
00081         {
00082           Assign(str);
00083         }
00084 
00085       nsTString_CharT( const substring_tuple_type& tuple )
00086         : substring_type()
00087         {
00088           Assign(tuple);
00089         }
00090 
00091       explicit
00092 #ifdef MOZ_V1_STRING_ABI
00093       nsTString_CharT( const abstract_string_type& readable )
00094 #else
00095       nsTString_CharT( const substring_type& readable )
00096 #endif
00097         : substring_type()
00098         {
00099           Assign(readable);
00100         }
00101 
00102 
00103         // |operator=| does not inherit, so we must define our own
00104       self_type& operator=( char_type c )                                                       { Assign(c);        return *this; }
00105       self_type& operator=( const char_type* data )                                             { Assign(data);     return *this; }
00106       self_type& operator=( const self_type& str )                                              { Assign(str);      return *this; }
00107       self_type& operator=( const substring_type& str )                                         { Assign(str);      return *this; }
00108       self_type& operator=( const substring_tuple_type& tuple )                                 { Assign(tuple);    return *this; }
00109 #ifdef MOZ_V1_STRING_ABI
00110       self_type& operator=( const abstract_string_type& readable )                              { Assign(readable); return *this; }
00111 #endif
00112 
00113 
00118       const char_type* get() const
00119         {
00120           return mData;
00121         }
00122 
00123 
00131       char_type CharAt( index_type i ) const
00132         {
00133           NS_ASSERTION(i <= mLength, "index exceeds allowable range");
00134           return mData[i];
00135         }
00136 
00137       char_type operator[]( index_type i ) const
00138         {
00139           return CharAt(i);
00140         }
00141 
00142 
00143 #if MOZ_STRING_WITH_OBSOLETE_API
00144 
00145 
00157       NS_COM PRInt32 Find( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
00158       NS_COM PRInt32 Find( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
00159 
00160 #ifdef CharT_is_PRUnichar
00161       NS_COM PRInt32 Find( const nsAFlatString& aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
00162       NS_COM PRInt32 Find( const PRUnichar* aString, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
00163 #endif
00164 
00165         
00178       NS_COM PRInt32 RFind( const nsCString& aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
00179       NS_COM PRInt32 RFind( const char* aCString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
00180 
00181 #ifdef CharT_is_PRUnichar
00182       NS_COM PRInt32 RFind( const nsAFlatString& aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
00183       NS_COM PRInt32 RFind( const PRUnichar* aString, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
00184 #endif
00185 
00186 
00197       // PRInt32 FindChar( PRUnichar aChar, PRInt32 aOffset=0, PRInt32 aCount=-1 ) const;
00198       NS_COM PRInt32 RFindChar( PRUnichar aChar, PRInt32 aOffset=-1, PRInt32 aCount=-1 ) const;
00199 
00200 
00211       NS_COM PRInt32 FindCharInSet( const char* aString, PRInt32 aOffset=0 ) const;
00212       PRInt32 FindCharInSet( const self_type& aString, PRInt32 aOffset=0 ) const
00213         {
00214           return FindCharInSet(aString.get(), aOffset);
00215         }
00216 
00217 #ifdef CharT_is_PRUnichar
00218       NS_COM PRInt32 FindCharInSet( const PRUnichar* aString, PRInt32 aOffset=0 ) const;
00219 #endif
00220 
00221 
00232       NS_COM PRInt32 RFindCharInSet( const char_type* aString, PRInt32 aOffset=-1 ) const;
00233       PRInt32 RFindCharInSet( const self_type& aString, PRInt32 aOffset=-1 ) const
00234         {
00235           return RFindCharInSet(aString.get(), aOffset);
00236         }
00237 
00238 
00248 #ifdef CharT_is_char
00249       NS_COM PRInt32 Compare( const char* aString, PRBool aIgnoreCase=PR_FALSE, PRInt32 aCount=-1 ) const;
00250 #endif
00251 
00252 
00261 #ifdef CharT_is_char
00262       PRBool EqualsIgnoreCase( const char* aString, PRInt32 aCount=-1 ) const {
00263         return Compare(aString, PR_TRUE, aCount) == 0;
00264       }
00265 #else
00266       NS_COM PRBool EqualsIgnoreCase( const char* aString, PRInt32 aCount=-1 ) const;
00267 
00268 
00279       NS_COM char* ToCString( char* aBuf, PRUint32 aBufLength, PRUint32 aOffset=0 ) const;
00280 
00281 #endif // !CharT_is_PRUnichar
00282 
00289       NS_COM float ToFloat( PRInt32* aErrorCode ) const;
00290 
00291 
00298       NS_COM PRInt32 ToInteger( PRInt32* aErrorCode, PRUint32 aRadix=kRadix10 ) const;
00299       
00300 
00318       NS_COM size_type Mid( self_type& aResult, PRUint32 aStartPos, PRUint32 aCount ) const;
00319 
00320       size_type Left( self_type& aResult, size_type aCount ) const
00321         {
00322           return Mid(aResult, 0, aCount);
00323         }
00324 
00325       size_type Right( self_type& aResult, size_type aCount ) const
00326         {
00327           aCount = NS_MIN(mLength, aCount);
00328           return Mid(aResult, mLength - aCount, aCount);
00329         }
00330 
00331 
00340       NS_COM PRBool SetCharAt( PRUnichar aChar, PRUint32 aIndex );
00341 
00342 
00349       NS_COM void StripChars( const char* aSet );
00350 
00351 
00355       NS_COM void StripWhitespace();
00356 
00357 
00362       NS_COM void ReplaceChar( char_type aOldChar, char_type aNewChar );
00363       NS_COM void ReplaceChar( const char* aSet, char_type aNewChar );
00364       NS_COM void ReplaceSubstring( const self_type& aTarget, const self_type& aNewValue);
00365       NS_COM void ReplaceSubstring( const char_type* aTarget, const char_type* aNewValue);
00366 
00367 
00378       NS_COM void Trim( const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE, PRBool aIgnoreQuotes=PR_FALSE );
00379 
00388       NS_COM void CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE );
00389 
00390 
00395       NS_COM void AssignWithConversion( const nsTAString_IncompatibleCharT& aString );
00396       NS_COM void AssignWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 );
00397 
00398       NS_COM void AppendWithConversion( const nsTAString_IncompatibleCharT& aString );
00399       NS_COM void AppendWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 );
00400 
00404       NS_COM void AppendInt( PRInt32 aInteger, PRInt32 aRadix=kRadix10 ); //radix=8,10 or 16
00405 
00409       inline void AppendInt( PRUint32 aInteger, PRInt32 aRadix = kRadix10 )
00410         {
00411           AppendInt(PRInt32(aInteger), aRadix);
00412         }
00413 
00419       NS_COM void AppendInt( PRInt64 aInteger, PRInt32 aRadix=kRadix10 );
00420 
00425       NS_COM void AppendFloat( double aFloat );
00426 
00427 #endif // !MOZ_STRING_WITH_OBSOLETE_API
00428 
00429 
00430     protected:
00431 
00432       explicit
00433       nsTString_CharT( PRUint32 flags )
00434         : substring_type(flags) {}
00435 
00436         // allow subclasses to initialize fields directly
00437       nsTString_CharT( char_type* data, size_type length, PRUint32 flags )
00438         : substring_type(data, length, flags) {}
00439   };
00440 
00441 
00442 class nsTFixedString_CharT : public nsTString_CharT
00443   {
00444     public:
00445 
00446       typedef nsTFixedString_CharT    self_type;
00447       typedef nsTFixedString_CharT    fixed_string_type;
00448 
00449     public:
00450 
00461       nsTFixedString_CharT( char_type* data, size_type storageSize )
00462         : string_type(data, char_traits::length(data), F_TERMINATED | F_FIXED | F_CLASS_FIXED)
00463         , mFixedCapacity(storageSize - 1)
00464         , mFixedBuf(data)
00465         {}
00466 
00467       nsTFixedString_CharT( char_type* data, size_type storageSize, size_type length )
00468         : string_type(data, length, F_TERMINATED | F_FIXED | F_CLASS_FIXED)
00469         , mFixedCapacity(storageSize - 1)
00470         , mFixedBuf(data)
00471         {
00472           // null-terminate
00473           mFixedBuf[length] = char_type(0);
00474         }
00475 
00476         // |operator=| does not inherit, so we must define our own
00477       self_type& operator=( char_type c )                                                       { Assign(c);        return *this; }
00478       self_type& operator=( const char_type* data )                                             { Assign(data);     return *this; }
00479       self_type& operator=( const substring_type& str )                                         { Assign(str);      return *this; }
00480       self_type& operator=( const substring_tuple_type& tuple )                                 { Assign(tuple);    return *this; }
00481 #ifdef MOZ_V1_STRING_ABI
00482       self_type& operator=( const abstract_string_type& readable )                              { Assign(readable); return *this; }
00483 #endif
00484 
00485     protected:
00486 
00487       friend class nsTSubstring_CharT;
00488 
00489       size_type  mFixedCapacity;
00490       char_type *mFixedBuf;
00491   };
00492 
00493 
00500 class nsTAutoString_CharT : public nsTFixedString_CharT
00501   {
00502     public:
00503 
00504       typedef nsTAutoString_CharT    self_type;
00505 
00506     public:
00507 
00512       nsTAutoString_CharT()
00513         : fixed_string_type(mStorage, kDefaultStorageSize, 0)
00514         {}
00515 
00516       explicit
00517       nsTAutoString_CharT( char_type c )
00518         : fixed_string_type(mStorage, kDefaultStorageSize, 0)
00519         {
00520           Assign(c);
00521         }
00522 
00523       explicit
00524       nsTAutoString_CharT( const char_type* data, size_type length = size_type(-1) )
00525         : fixed_string_type(mStorage, kDefaultStorageSize, 0)
00526         {
00527           Assign(data, length);
00528         }
00529 
00530       nsTAutoString_CharT( const self_type& str )
00531         : fixed_string_type(mStorage, kDefaultStorageSize, 0)
00532         {
00533           Assign(str);
00534         }
00535 
00536       explicit
00537       nsTAutoString_CharT( const substring_type& str )
00538         : fixed_string_type(mStorage, kDefaultStorageSize, 0)
00539         {
00540           Assign(str);
00541         }
00542 
00543       nsTAutoString_CharT( const substring_tuple_type& tuple )
00544         : fixed_string_type(mStorage, kDefaultStorageSize, 0)
00545         {
00546           Assign(tuple);
00547         }
00548 
00549 #ifdef MOZ_V1_STRING_ABI
00550       explicit
00551       nsTAutoString_CharT( const abstract_string_type& readable )
00552         : fixed_string_type(mStorage, kDefaultStorageSize, 0)
00553         {
00554           Assign(readable);
00555         }
00556 #endif
00557 
00558         // |operator=| does not inherit, so we must define our own
00559       self_type& operator=( char_type c )                                                       { Assign(c);        return *this; }
00560       self_type& operator=( const char_type* data )                                             { Assign(data);     return *this; }
00561       self_type& operator=( const self_type& str )                                              { Assign(str);      return *this; }
00562       self_type& operator=( const substring_type& str )                                         { Assign(str);      return *this; }
00563       self_type& operator=( const substring_tuple_type& tuple )                                 { Assign(tuple);    return *this; }
00564 #ifdef MOZ_V1_STRING_ABI
00565       self_type& operator=( const abstract_string_type& readable )                              { Assign(readable); return *this; }
00566 #endif
00567 
00568       enum { kDefaultStorageSize = 64 };
00569 
00570     private:
00571 
00572       char_type mStorage[kDefaultStorageSize];
00573   };
00574 
00575 
00583 class nsTXPIDLString_CharT : public nsTString_CharT
00584   {
00585     public:
00586 
00587       typedef nsTXPIDLString_CharT    self_type;
00588 
00589     public:
00590 
00591       nsTXPIDLString_CharT()
00592         : string_type(NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer), 0, F_TERMINATED | F_VOIDED) {}
00593 
00594         // copy-constructor required to avoid default
00595       nsTXPIDLString_CharT( const self_type& str )
00596         : string_type(NS_CONST_CAST(char_type*, char_traits::sEmptyBuffer), 0, F_TERMINATED | F_VOIDED)
00597         {
00598           Assign(str);
00599         }
00600 
00601         // return nsnull if we are voided
00602       const char_type* get() const
00603         {
00604           return (mFlags & F_VOIDED) ? nsnull : mData;
00605         }
00606 
00607         // this case operator is the reason why this class cannot just be a
00608         // typedef for nsTString
00609       operator const char_type*() const
00610         {
00611           return get();
00612         }
00613 
00614         // need this to diambiguous operator[int]
00615       char_type operator[]( PRInt32 i ) const
00616         {
00617           return CharAt(index_type(i));
00618         }
00619 
00620         // |operator=| does not inherit, so we must define our own
00621       self_type& operator=( char_type c )                                                       { Assign(c);        return *this; }
00622       self_type& operator=( const char_type* data )                                             { Assign(data);     return *this; }
00623       self_type& operator=( const self_type& str )                                              { Assign(str);      return *this; }
00624       self_type& operator=( const substring_type& str )                                         { Assign(str);      return *this; }
00625       self_type& operator=( const substring_tuple_type& tuple )                                 { Assign(tuple);    return *this; }
00626 #ifdef MOZ_V1_STRING_ABI
00627       self_type& operator=( const abstract_string_type& readable )                              { Assign(readable); return *this; }
00628 #endif
00629   };
00630 
00631 
00644 class nsTGetterCopies_CharT
00645   {
00646     public:
00647       typedef CharT char_type;
00648 
00649       nsTGetterCopies_CharT(nsTXPIDLString_CharT& str)
00650         : mString(str), mData(nsnull) {}
00651 
00652       ~nsTGetterCopies_CharT()
00653         {
00654           mString.Adopt(mData); // OK if mData is null
00655         }
00656 
00657       operator char_type**()
00658         {
00659           return &mData;
00660         }
00661 
00662     private:
00663       nsTXPIDLString_CharT& mString;
00664       char_type*            mData;
00665   };
00666 
00667 inline
00668 nsTGetterCopies_CharT
00669 getter_Copies( nsTXPIDLString_CharT& aString )
00670   {
00671     return nsTGetterCopies_CharT(aString);
00672   }
00673 
00674 
00683 class nsTAdoptingString_CharT : public nsTXPIDLString_CharT
00684   {
00685     public:
00686 
00687       typedef nsTAdoptingString_CharT    self_type;
00688 
00689     public:
00690 
00691       explicit nsTAdoptingString_CharT() {}
00692       explicit nsTAdoptingString_CharT(char_type* str, size_type length = size_type(-1))
00693         {
00694           Adopt(str, length);
00695         }
00696 
00697         // copy-constructor required to adopt on copy. Note that this
00698         // will violate the constness of |str| in the operator=()
00699         // call. |str| will be truncated as a side-effect of this
00700         // constructor.
00701       nsTAdoptingString_CharT( const self_type& str )
00702         {
00703           *this = str;
00704         }
00705 
00706         // |operator=| does not inherit, so we must define our own
00707       self_type& operator=( const substring_type& str )                                         { Assign(str);      return *this; }
00708       self_type& operator=( const substring_tuple_type& tuple )                                 { Assign(tuple);    return *this; }
00709 #ifdef MOZ_V1_STRING_ABI
00710       self_type& operator=( const abstract_string_type& readable )                              { Assign(readable); return *this; }
00711 #endif
00712 
00713         // Adopt(), if possible, when assigning to a self_type&. Note
00714         // that this violates the constness of str, str is always
00715         // truncated when this operator is called.
00716       NS_COM self_type& operator=( const self_type& str );
00717 
00718     private:
00719         // NOT TO BE IMPLEMENTED.
00720       self_type& operator=( const char_type* data );
00721       self_type& operator=( char_type* data );
00722   };
00723