diff --git a/mozilla/string/obsolete/nsStr.cpp b/mozilla/string/obsolete/nsStr.cpp index 0a0b2ca937a..52b1cc2466f 100644 --- a/mozilla/string/obsolete/nsStr.cpp +++ b/mozilla/string/obsolete/nsStr.cpp @@ -31,11 +31,13 @@ #include "nsStr.h" #include "bufferRoutines.h" #include "stdio.h" //only used for printf -#include "nsDeque.h" #include "nsCRT.h" +#include "nsDeque.h" static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; +static const char* kCallFindChar = "For better performance, call FindChar() for targets whose length==1."; +static const char* kCallRFindChar = "For better performance, call RFindChar() for targets whose length==1."; //---------------------------------------------------------------------------------------- // The following is a memory agent who knows how to recycled (pool) freed memory... @@ -44,6 +46,8 @@ static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; /************************************************************** Define the char* (pooled) deallocator class... **************************************************************/ + +/* class nsBufferDeallocator: public nsDequeFunctor{ public: virtual void* operator()(void* anObject) { @@ -53,12 +57,6 @@ public: } }; -/** - * - * @update gess10/30/98 - * @param - * @return - */ class nsPoolingMemoryAgent : public nsMemoryAgent{ public: nsPoolingMemoryAgent() { @@ -128,6 +126,8 @@ public: nsDeque* mPools[16]; }; +*/ + static char* gCommonEmptyBuffer=0; /** * @@ -151,7 +151,8 @@ char* GetSharedEmptyBuffer() { } /** - * + * This method initializes all the members of the nsStr structure + * * @update gess10/30/98 * @param * @return @@ -166,7 +167,7 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { } /** - * + * This method initializes all the members of the nsStr structure * @update gess10/30/98 * @param * @return @@ -193,7 +194,7 @@ nsIMemoryAgent* GetDefaultAgent(void){ } /** - * + * This member destroys the memory buffer owned by an nsStr object (if it actually owns it) * @update gess10/30/98 * @param * @return @@ -386,9 +387,10 @@ void nsStr::Truncate(nsStr& aDest,PRUint32 aDestOffset,nsIMemoryAgent* anAgent){ /** - * + * This method forces the given string to upper or lowercase * @update gess1/7/99 - * @param + * @param aDest is the string you're going to change + * @param aToUpper: if TRUE, then we go uppercase, otherwise we go lowercase * @return */ void nsStr::ChangeCase(nsStr& aDest,PRBool aToUpper) { @@ -397,19 +399,6 @@ void nsStr::ChangeCase(nsStr& aDest,PRBool aToUpper) { NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } -/** - * - * @update gess1/7/99 - * @param - * @return - */ -void nsStr::StripChars(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,const char* aCharSet){ - PRUint32 aNewLen=gStripChars[aDest.mCharSize](aDest.mStr,aDestOffset,aCount,aCharSet); - aDest.mLength=aNewLen; - NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); -} - - /** * * @update gess1/7/99 @@ -417,8 +406,43 @@ void nsStr::StripChars(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,const ch * @return */ void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){ - PRUint32 aNewLen=gTrimChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet,aEliminateLeading,aEliminateTrailing); - aDest.mLength=aNewLen; + + if((aDest.mLength>0) && aSet){ + PRInt32 theIndex=-1; + PRInt32 theMax=aDest.mLength; + PRInt32 theSetLen=nsCRT::strlen(aSet); + + if(aEliminateLeading) { + while(++theIndex<=theMax) { + PRUnichar theChar=GetCharAt(aDest,theIndex); + PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE); + if(kNotFound==thePos) + break; + } + if(00) { + PRUnichar theChar=GetCharAt(aDest,theIndex); //read at end now... + PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE); + if(kNotFound0) && (aTarget.mLength>0) && (anOffset0) && (aTarget.mLength>0)){ - int32 theTargetMax=aTarget.mLength; - while(++index<=theMax) { - int32 theSubIndex=-1; + PRInt32 result=kNotFound; + + if((0=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){ + PRInt32 theTargetMax=aTarget.mLength; + while(index<=theMax) { + PRInt32 theSubIndex=-1; PRBool matches=PR_TRUE; while((++theSubIndex0) && (aTarget.mLength>0)){ + if((0=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){ + + nsStr theCopy; + nsStr::Initialize(theCopy,eOneByte); + nsStr::Assign(theCopy,aTarget,0,aTarget.mLength,0); + if(aIgnoreCase){ + nsStr::ChangeCase(theCopy,PR_FALSE); //force to lowercase } - } //while - nsStr::Destroy(theCopy,0); + + PRInt32 theTargetMax=theCopy.mLength; + while(index>=0) { + PRInt32 theSubIndex=-1; + PRBool matches=PR_FALSE; + if(index+theCopy.mLength<=aDest.mLength) { + matches=PR_TRUE; + while((++theSubIndex=0) { - PRUnichar theChar=GetCharAt(aDest,offset); - thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); - if(kNotFound!=thePos) - return offset; - } //while + PRInt32 index=(0<=anOffset) ? anOffset : aDest.mLength; + PRInt32 thePos; + + //note that the search is inverted here. We're scanning aDest, one char at a time + //but doing the search against the given set. That's why we use 0 as the offset below. + if(0=0) { + PRUnichar theChar=GetCharAt(aDest,index); + thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); + if(kNotFound!=thePos) + return index; + } //while + } return kNotFound; } /** - * - * @update gess11/12/98 - * @param + * Compare source and dest strings, up to an (optional max) number of chars + * @param aDest is the first str to compare + * @param aSource is the second str to compare + * @param aCount -- if (-1), then we use length of longer string; if (0aSource=1 */ PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - int minlen=(aSource.mLength1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + +CSharedStrBuffer::CSharedStrBuffer(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eTwoByte; + mStackBased=aStackBased; + mLength=mCapacity=0; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + + +//---------------------------------------------------------------------------------------- diff --git a/mozilla/string/obsolete/nsStr.h b/mozilla/string/obsolete/nsStr.h index 07b753a25b0..8757ce92f85 100644 --- a/mozilla/string/obsolete/nsStr.h +++ b/mozilla/string/obsolete/nsStr.h @@ -48,6 +48,10 @@ enum eCharSize {eOneByte=0,eTwoByte=1}; #define kDefaultCharSize eTwoByte +#define kRadix10 (10) +#define kRadix16 (16) +#define kAutoDetect (100) +#define kRadixUnknown (kAutoDetect+1) const PRInt32 kNotFound = -1; @@ -55,8 +59,23 @@ class nsIMemoryAgent; //---------------------------------------------------------------------------------------- +class NS_COM CSharedStrBuffer { +public: + CSharedStrBuffer(char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CSharedStrBuffer(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); -struct nsStr { + char* mBuffer; + eCharSize mCharSize; + PRUint32 mCapacity; + PRInt32 mLength; + PRBool mStackBased; + +}; + +//---------------------------------------------------------------------------------------- + + +struct NS_COM nsStr { /** * This method initializes an nsStr for use @@ -169,16 +188,6 @@ struct nsStr { */ static void ChangeCase(nsStr& aDest,PRBool aToUpper); - /** - * This method removes chars (given in aSet) from the given buffer - * - * @update gess 01/04/99 - * @param aString is the buffer to be manipulated - * @param aDestOffset is starting pos in buffer for manipulation - * @param aCount is the number of chars to compare - * @param aSet tells us which chars to remove from given buffer - */ - static void StripChars(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,const char* aCharSet); /** * This method trims chars (given in aSet) from the edges of given buffer @@ -201,7 +210,7 @@ struct nsStr { * @param aEliminateLeading tells us whether to strip chars from the start of the buffer * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer */ - static void CompressSet(nsStr& aDest,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing); + static void CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing); /** * This method compares the data bewteen two nsStr's @@ -225,13 +234,13 @@ struct nsStr { * @param anOffset tells us where in the dest string to start searching * @return the index of the source (substr) in dest, or -1 (kNotFound) if not found. */ - static PRInt32 FindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 FindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset); + static PRInt32 FindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 FindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset); - static PRInt32 RFindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset); + static PRInt32 RFindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset); PRUint32 mLength; diff --git a/mozilla/string/obsolete/nsString.cpp b/mozilla/string/obsolete/nsString.cpp index d0d86da25a5..af2c0971ae8 100644 --- a/mozilla/string/obsolete/nsString.cpp +++ b/mozilla/string/obsolete/nsString.cpp @@ -1,3 +1,4 @@ + /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public License @@ -14,38 +15,28 @@ * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. - */ + */ #include #include -#include +#include #include #include "nsString.h" -#include "nsCRT.h" #include "nsDebug.h" -#include "prprf.h" +#include "nsCRT.h" +#include "nsDeque.h" + +#ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" -#include "nsStr.h" - -#include "nsUnicharUtilCIID.h" -#include "nsIServiceManager.h" -#include "nsICaseConversion.h" - -const PRInt32 kGrowthDelta = 8; -PRUnichar gBadChar = 0; -const char* kOutOfBoundsError = "Error: out of bounds"; -const char* kNullPointerError = "Error: unexpected null ptr"; -const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; - -PRUnichar kCommonEmptyBuffer[100]; //shared by all strings; NEVER WRITE HERE!!! - -#ifdef RICKG_DEBUG -PRBool nsString1::mSelfTested = PR_FALSE; #endif +static const char* kNullPointerError = "Error: unexpected null ptr"; +static const char* kWhitespace="\b\t\r\n "; -#define NOT_USED 0xfffd + + +#define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { NOT_USED, @@ -99,67 +90,26 @@ public: } } }; -static CTableConstructor gTableConstructor; -//---- XPCOM code to connect with UnicharUtil -class HandleCaseConversionShutdown2 : public nsIShutdownListener { -public : - NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); - HandleCaseConversionShutdown2(void) { NS_INIT_REFCNT(); } - virtual ~HandleCaseConversionShutdown2(void) {} - NS_DECL_ISUPPORTS -}; -static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); -static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); - -static nsICaseConversion * gCaseConv = NULL; - -static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); -NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown2, kIShutdownListenerIID); - -nsresult -HandleCaseConversionShutdown2::OnShutdown(const nsCID& cid, nsISupports* service) -{ - if (cid.Equals(kUnicharUtilCID)) { - NS_ASSERTION(service == gCaseConv, "wrong service!"); - gCaseConv->Release(); - gCaseConv = NULL; +static void CSubsume(nsStr& aDest,nsStr& aSource){ + if(aSource.mStr && aSource.mLength) { + if(aSource.mOwnsBuffer){ + nsStr::Destroy(aDest,0); + aDest.mStr=aSource.mStr; + aDest.mLength=aSource.mLength; + aDest.mCharSize=aSource.mCharSize; + aDest.mCapacity=aSource.mCapacity; + aDest.mOwnsBuffer=aSource.mOwnsBuffer; + aSource.mOwnsBuffer=PR_FALSE; + aSource.mStr=0; } - return NS_OK; -} - -static HandleCaseConversionShutdown2* gListener = NULL; - -static void StartUpCaseConversion() -{ - nsresult err; - - if ( NULL == gListener ) - { - gListener = new HandleCaseConversionShutdown2(); - gListener->AddRef(); + else{ + nsStr::Assign(aDest,aSource,0,aSource.mLength,0); } - err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, - (nsISupports**) &gCaseConv, gListener); + } + else nsStr::Truncate(aDest,0,0); } -static void CheckCaseConversion() -{ - if(NULL == gCaseConv ) - StartUpCaseConversion(); - - // NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); - -} - -/*********************************************************************** - IMPLEMENTATION NOTES: - - Man I hate writing string classes. - You'd think after about a qintrillion lines of code have been written, - that no poor soul would ever have to do this again. Sigh. - ***********************************************************************/ - /** @@ -168,102 +118,83 @@ static void CheckCaseConversion() * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString1::nsString1() { - - NS_ASSERTION(kCommonEmptyBuffer[0]==0,kFoolMsg); - mLength = mCapacity = 0; - mStr = kCommonEmptyBuffer; - -#ifdef RICKG_DEBUG - if(!mSelfTested) { - mSelfTested=PR_TRUE; - SelfTest(); - } -#endif +nsCString::nsCString(nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); } - - /** - * This constructor accepts an isolatin string - * @update gess7/30/98 + * This constructor accepts an ascii string + * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString1::nsString1(const char* aCString) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - if(aCString) { - PRInt32 len=strlen(aCString); - EnsureCapacityFor(len); - this->SetString(aCString,len); - } +nsCString::nsCString(const char* aCString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + Assign(aCString,aLength); } +/** + * This constructor accepts an ascii string + * @update gess 1/4/99 + * @param aCString is a ptr to a 1-byte cstr + */ +nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + Assign(aString,aLength); +} /** * This is our copy constructor - * @update gess7/30/98 - * @param reference to another nsString1 + * @update gess 1/4/99 + * @param reference to another nsCString */ -nsString1::nsString1(const nsString1 &aString) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - if(aString.mLength) { - EnsureCapacityFor(aString.mLength); - this->SetString(aString.mStr,aString.mLength); - } +nsCString::nsCString(const nsStr &aString,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } - /** - * Constructor from a unicode string - * @update gess7/30/98 - * @param anicodestr pts to a unicode string + * This is our copy constructor + * @update gess 1/4/99 + * @param reference to another nsCString */ -nsString1::nsString1(const PRUnichar* aUnicodeStr) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - - PRInt32 len=(aUnicodeStr) ? nsCRT::strlen(aUnicodeStr) : 0; - if(len>0) { - EnsureCapacityFor(len); - this->SetString(aUnicodeStr,len); - } - +nsCString::nsCString(const nsCString& aString) :mAgent(aString.mAgent) { + nsStr::Initialize(*this,aString.mCharSize); + nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } +/** + * construct off a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString + */ +nsCString::nsCString(nsSubsumeCStr& aSubsumeStr) :mAgent(0) { + CSubsume(*this,aSubsumeStr); +} /** * Destructor - * @update gess7/30/98 + * Make sure we call nsStr::Destroy. */ -nsString1::~nsString1() -{ - if(mStr && (mStr!=kCommonEmptyBuffer)) - delete [] mStr; - mStr=0; - mCapacity=mLength=0; +nsCString::~nsCString() { + nsStr::Destroy(*this,mAgent); } +void nsCString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity << mCharSize); +#endif +} /** * This method truncates this string to given length. * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param anIndex -- new length of string * @return nada */ -void nsString1::Truncate(PRInt32 anIndex) { - if((anIndex>-1) && (anIndexAdd(sizeof(*this)); - aHandler->Add(mCapacity * sizeof(chartype)); +void nsCString::Truncate(PRInt32 anIndex) { + nsStr::Truncate(*this,anIndex,mAgent); } /** @@ -273,10 +204,10 @@ void nsString1::SizeOf(nsISizeOfHandler* aHandler) const * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString1::IsOrdered(void) const { +PRBool nsCString::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { - PRInt32 theIndex; + PRUint32 theIndex; for(theIndex=1;theIndexmStr[theIndex]) { result=PR_FALSE; @@ -287,445 +218,396 @@ PRBool nsString1::IsOrdered(void) const { return result; } -/** - * This method gets called when the internal buffer needs - * to grow to a given size. - * @update gess 3/30/98 - * @param aNewLength -- new capacity of string - * @return void - */ -void nsString1::EnsureCapacityFor(PRInt32 aNewLength) -{ - PRInt32 newCapacity; - - if (mCapacity > 64) { - // When the string starts getting large, double the capacity as we - // grow. - newCapacity = mCapacity * 2; - if (newCapacity < aNewLength) { - newCapacity = mCapacity + aNewLength; - } - } else { - // When the string is small, keep it's capacity a multiple of - // kGrowthDelta - PRInt32 size =aNewLength+kGrowthDelta; - newCapacity=size-(size % kGrowthDelta); - } - - if(mCapacity 0) { - nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype) + sizeof(chartype)); - } - if(mStr && (mStr!=kCommonEmptyBuffer)) - delete [] mStr; - mStr = temp; - mStr[mLength]=0; - } -} - /** - * Call this method if you want string to report a shorter length. - * @update gess7/30/98 + * Call this method if you want to force the string to a certain capacity + * @update gess 1/4/99 * @param aLength -- contains new length for mStr * @return */ -void nsString1::SetLength(PRInt32 aLength) { - if(aLength>mLength) { - EnsureCapacityFor(aLength); - nsCRT::zero(mStr + mLength, (aLength - mLength) * sizeof(chartype)); +void nsCString::SetCapacity(PRUint32 aLength) { + if(aLength>mCapacity) { + GrowCapacity(*this,aLength,mAgent); } - if((aLength>=0) && (aLengthToLower(mStr, mStr, mLength); - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* cp = mStr; - chartype* end = cp + mLength; - while (cp < end) { - chartype ch = *cp; - if ((ch >= 'A') && (ch <= 'Z')) { - *cp = 'a' + (ch - 'A'); - } - cp++; - } - -} - -/** - * Converts all chars in internal string to upper - * @update gess 7/27/98 - */ -void nsString1::ToUpperCase() -{ - // I18N code begin - CheckCaseConversion(); - if(gCaseConv) { - nsresult err = gCaseConv->ToUpper(mStr, mStr, mLength); - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* cp = mStr; - chartype* end = cp + mLength; - while (cp < end) { - chartype ch = *cp; - if ((ch >= 'a') && (ch <= 'z')) { - *cp = 'A' + (ch - 'a'); - } - cp++; - } - -} +/********************************************************************** + Lexomorphic transforms... + *********************************************************************/ /** * Converts all chars in given string to UCS2 */ -void nsString1::ToUCS2(PRInt32 aStartOffset){ - if(aStartOffsetToLower(mStr, aOut.mStr, mLength); - (*(aOut.mStr+mLength)) = 0; - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - - chartype* to = aOut.mStr; - chartype* from = mStr; - chartype* end = from + mLength; - while (from < end) { - chartype ch = *from++; - if ((ch >= 'A') && (ch <= 'Z')) { - ch = 'a' + (ch - 'A'); - } - *to++ = ch; - } - *to = 0; +void nsCString::ToLowerCase(nsCString& aString) const { + aString=*this; + nsStr::ChangeCase(aString,PR_FALSE); } /** * Converts chars in this to lowercase, and * stores them in a given output string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aOut is a string to contain result */ -void nsString1::ToUpperCase(nsString1& aOut) const -{ - aOut.EnsureCapacityFor(mLength); - aOut.mLength = mLength; - - // I18N code begin - CheckCaseConversion(); - if(gCaseConv) { - nsresult err = gCaseConv->ToUpper(mStr, aOut.mStr, mLength); - (*(aOut.mStr+mLength)) = 0; - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* to = aOut.mStr; - chartype* from = mStr; - chartype* end = from + mLength; - while (from < end) { - chartype ch = *from++; - if ((ch >= 'a') && (ch <= 'z')) { - ch = 'A' + (ch - 'a'); - } - *to++ = ch; - } - *to = 0; +void nsCString::ToUpperCase(nsCString& aString) const { + aString=*this; + nsStr::ChangeCase(aString,PR_TRUE); } +/** + * This method is used to remove all occurances of the + * characters found in aSet from this string. + * + * @update gess 01/04/99 + * @param aSet -- characters to be cut from this + * @return *this + */ +nsCString& nsCString::StripChar(PRUnichar aChar){ + + PRInt32 theIndex=FindChar(aChar,PR_FALSE,0); + while(kNotFound= 0) { - *to++ = *from++; - } - *to++ = 0; - return rv; +PRUnichar* nsCString::ToNewUnicode() const { + nsString temp(mStr); + temp.SetCapacity(8); + PRUnichar* result=temp.mUStr; + temp.mStr=0; + temp.mOwnsBuffer=PR_FALSE; + return result; } /** - * Copies contents of this onto given string. - * @update gess 7/27/98 - * @param aString to hold copy of this - * @return nada. - */ -void nsString1::Copy(nsString1& aString) const -{ - aString.mLength = 0; - aString.Append(mStr, mLength); -} - -/** - * - * @update gess 7/27/98 + * Copies contents of this string into he given buffer + * @update gess 01/04/99 * @param * @return */ -char* nsString1::ToCString(char* aBuf, PRInt32 aBufLength) const -{ - aBufLength--; // leave room for the \0 - PRInt32 len = (mLength > aBufLength) ? aBufLength : mLength; - char* to = aBuf; - chartype* from = mStr; - while (--len >= 0) { - *to++ = char(*from++); +char* nsCString::ToCString(char* aBuf, PRUint32 aBufLength,PRUint32 anOffset) const{ + if(aBuf) { + CSharedStrBuffer theSB(aBuf,PR_FALSE,aBufLength,0); + nsCAutoString temp(theSB); + temp.Assign(*this); + temp.mStr=0; } - *to++ = '\0'; return aBuf; } /** * Perform string to float conversion. - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aErrorCode will contain error if one occurs * @return float rep of string value */ -float nsString1::ToFloat(PRInt32* aErrorCode) const -{ - char buf[40]; +float nsCString::ToFloat(PRInt32* aErrorCode) const { + char buf[100]; if (mLength > PRInt32(sizeof(buf)-1)) { *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; return 0.0f; @@ -741,67 +623,56 @@ float nsString1::ToFloat(PRInt32* aErrorCode) const /** * Perform numeric string to int conversion with given radix. + * NOTE: 1. This method mandates that the string is well formed. + * 2. This method will return an error if the string you give + contains chars outside the range for the specified radix. + * @update gess 10/01/98 * @param aErrorCode will contain error if one occurs * @param aRadix tells us what base to expect the string in. - * @return int rep of string value; aErrorCode gets set too: NS_OK, NS_ERROR_ILLEGAL_VALUE + * @return int rep of string value */ -PRInt32 nsString1::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { - PRInt32 result = 0; - PRInt32 decPt=Find(PRUnichar('.'),0); - PRUnichar* cp = (-1==decPt) ? mStr + mLength-1 : mStr+decPt-1; - char digit=0; - PRUnichar theChar; -// PRInt32 theShift=0; - PRInt32 theMult=1; +PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { - *aErrorCode = (0= mStr) { - theChar = *cp; - if((theChar>='0') && (theChar<='9')){ - break; - } - else if((theChar>='a') && (theChar<='f')) { - break; - } - else if((theChar>='A') && (theChar<='F')) { - break; - } - cp--; - } + *anErrorCode = NS_OK; //now iterate the numeric chars and build our result - while(cp>=mStr) { - theChar=*cp--; + char theChar=0; + char theDigit=0; + while(--cp>=aString.mStr){ + char theChar=*cp; if((theChar>='0') && (theChar<='9')){ - digit=theChar-'0'; - } - else if((theChar>='a') && (theChar<='f')) { - digit=(theChar-'a')+10; + theDigit=theChar-'0'; } else if((theChar>='A') && (theChar<='F')) { - digit=(theChar-'A')+10; + if(10==aRadix){ + *anErrorCode=NS_ERROR_ILLEGAL_VALUE; + result=0; + break; + } + theDigit=(theChar-'A')+10; } else if('-'==theChar) { result=-result; break; } - else if(('+'==theChar) || (' '==theChar) || ('#'==theChar)) { //stop in a good state if you see this... + else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this... break; } - else if((('x'==theChar) || ('X'==theChar)) && (16==aRadix)) { - //stop in a good state. - break; - } - else{ - *aErrorCode=NS_ERROR_ILLEGAL_VALUE; + else { + //we've encountered a char that's not a legal number or sign + *anErrorCode=NS_ERROR_ILLEGAL_VALUE; result=0; break; } - result+=digit*theMult; + + result+=theDigit*theMult; theMult*=aRadix; } @@ -809,275 +680,296 @@ PRInt32 nsString1::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { } /** - * assign given PRUnichar* to this string - * @update gess 7/27/98 - * @param PRUnichar: buffer to be assigned to this - * @return this + * Call this method to extract the rightmost numeric value from the given + * 1-byte input string, and simultaneously determine the radix. + * NOTE: This method mandates that the string is well formed. + * Leading and trailing gunk should be removed, and the case upper. + * @update gess 10/01/98 + * @param anInputString contains orig string + * @param anOutString contains numeric portion copy of input string + * @param aRadix (an out parm) tells the caller what base we think the string is in. + * @return non-zero error code if this string is non-numeric */ -nsString1& nsString1::SetString(const PRUnichar* aStr,PRInt32 aLength) { - if((0 == aLength) || (nsnull == aStr)) { - mLength=0; - if (nsnull != mStr) { - mStr[0]=0; +PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { + + aString.ToUpperCase(); + + PRInt32 decPt=nsStr::FindChar(aString,'.',PR_TRUE,0); + char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1; + + aRadix=kRadixUnknown; //assume for starters... + + // Skip trailing non-numeric... + while (cp >= aString.mStr) { + if((*cp>='0') && (*cp<='9')){ + if(kRadixUnknown==aRadix) + aRadix=kRadix10; + break; } - - } else { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aStr) : aLength; - if(mCapacity<=len) - EnsureCapacityFor(len); - nsCRT::memcpy(mStr,aStr,len*sizeof(chartype)); - mLength=len; - mStr[mLength]=0; + else if((*cp>='A') && (*cp<='F')) { + aRadix=16; + break; + } + cp--; } + aString.Truncate(cp-aString.mStr+1); - return *this; -} + //ok, now scan through chars until you find the start of this number... + //we delimit the number by the presence of: +,-,#,X - -/** - * assign given char* to this string - * @update gess 7/27/98 - * @param aCString: buffer to be assigned to this - * @return this - */ -nsString1& nsString1::SetString(const char* aCString,PRInt32 aLength) { - if(aCString!=0) { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aCString) : aLength; - if(mCapacity<=len) - EnsureCapacityFor(len); - const unsigned char* from = (const unsigned char*) aCString; - const unsigned char* end = from + len; - PRUnichar* dst = mStr; - while (from < end) { - *dst++ = PRUnichar(*from++); + // Skip trailing non-numeric... + cp++; + while (--cp >= aString.mStr) { + if((*cp>='0') && (*cp<='9')){ + continue; + } + else if((*cp>='A') && (*cp<='F')) { + continue; + } + else if((*cp=='-') || (*cp=='+')){ + break; + } + else { + if(('#'==(*cp)) || ('X'==(*cp))) + aRadix=kRadix16; + cp++; //move back by one + break; } - mLength=len; - mStr[mLength]=0; - } - else { - mLength=0; //This little bit of code handles the case - mStr[0]=0; //where some blockhead hands us a null string } + if(cp>aString.mStr) + aString.Cut(0,cp-aString.mStr); + PRInt32 result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; - return *this; + return result; } + /** - * assign given char* to this string - * @update gess 7/27/98 - * @param aCString: buffer to be assigned to this - * @return this + * This method tries to autodetect that radix given a string + * @update gess 10/01/98 + * @return 10,16,or 0 (meaning I don't know) */ -nsString1& nsString1::SetString(const nsString1& aString) { - - if(mCapacity<=aString.mLength) - EnsureCapacityFor(aString.mLength); - PRUnichar* from = (PRUnichar*) aString.mStr; - PRUnichar* end = from + aString.mLength; - PRUnichar* dst = (PRUnichar*) mStr; - while(fromSetString(aStr); +PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { + + //copy chars to local buffer -- step down from 2 bytes to 1 if necessary... + nsCAutoString theString(*this); + PRUint32 theRadix=aRadix; + PRInt32 result=0; + + *anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead + if(NS_OK==*anErrorCode){ + if(kAutoDetect==aRadix) + aRadix=theRadix; + if((kRadix10==aRadix) || (kRadix16==aRadix)) + result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one. + else *anErrorCode=NS_ERROR_ILLEGAL_VALUE; + } + + return result; } +/********************************************************************** + String manipulation methods... + *********************************************************************/ + + /** * assign given string to this one - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aString: string to be added to this * @return this */ -nsString1& nsString1::operator=(const nsString1& aString) { - return this->SetString(aString); +nsCString& nsCString::Assign(const nsStr& aString,PRInt32 aCount) { + if(this!=&aString){ + nsStr::Truncate(*this,0,0); + if(-1==aCount) aCount=aString.mLength; + nsStr::Assign(*this,aString,0,aCount,mAgent); + } + return *this; } - - + /** * assign given char* to this string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aCString: buffer to be assigned to this * @return this */ -nsString1& nsString1::operator=(const char* aCString) { - return SetString(aCString); +nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) { + nsStr::Truncate(*this,0,0); + if(aCString){ + Append(aCString,aCount); + } + return *this; } - /** - * assign given char to this string - * @update gess 7/27/98 - * @param aChar: char to be assignd to this + * assign given unichar* to this string + * @update gess 01/04/99 + * @param aString: buffer to be assigned to this * @return this */ -nsString1& nsString1::operator=(char aChar) { - return this->operator=(PRUnichar(aChar)); +nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) { + nsStr::Truncate(*this,0,0); + if(aString){ + Append(aString,aCount); + } + return *this; } /** * assign given char to this string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aChar: char to be assignd to this * @return this */ -nsString1& nsString1::operator=(PRUnichar aChar) { - if(mCapacity<1) - EnsureCapacityFor(kGrowthDelta); - mStr[0]=aChar; - mLength=1; - mStr[mLength]=0; - return *this; -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const nsString1& aString,PRInt32 aLength) { - return Append(aString.mStr,aString.mLength); -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const char* aCString,PRInt32 aLength) { - - if(aCString!=0) { - PRInt32 len=(aLength<0) ? strlen(aCString) : aLength; - if(mLength+len >= mCapacity) { - EnsureCapacityFor(mLength+len); - } - const unsigned char* from = (const unsigned char*) aCString; - const unsigned char* end = from + len; - PRUnichar* to = mStr + mLength; - while (from < end) { - *to++ = PRUnichar(*from++); - } - mLength+=len; - mStr[mLength]=0; - } - return *this; -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(char aChar) { - return Append(PRUnichar(aChar)); -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const PRUnichar* aString,PRInt32 aLength) { - - if(aString!=0) { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aString) : aLength; - if(mLength+len >= mCapacity) { - EnsureCapacityFor(mLength+len); - } - if(len>0) - nsCRT::memcpy(&mStr[mLength],aString,len*sizeof(chartype)); - mLength+=len; - mStr[mLength]=0; - } - return *this; -} - - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(PRUnichar aChar) { - if(mLength < mCapacity) { - mStr[mLength++]=aChar; // the new string len < capacity, so just copy - mStr[mLength]=0; - } - else { // The new string exceeds our capacity - EnsureCapacityFor(mLength+1); - mStr[mLength++]=aChar; - mStr[mLength]=0; - } - return *this; -} - - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const nsString1 &aString) { - return this->Append(aString.mStr,aString.mLength); -} - - -/** - * append given buffer to this string - * @update gess 7/27/98 - * @param aCString: buffer to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const char* aCString) { - return Append(aCString); -} - - -/** - * append given buffer to this string - * @update gess 7/27/98 - * @param aBuffer: buffer to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const PRUnichar* aBuffer) { - return Append(aBuffer); -} - - -/** - * append given char to this string - * @update gess 7/27/98 - * @param aChar: char to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(PRUnichar aChar) { +nsCString& nsCString::Assign(char aChar) { + nsStr::Truncate(*this,0,0); return Append(aChar); } /** - * - * @update gess 7/27/98 + * assign given char to this string + * @update gess 01/04/99 + * @param aChar: char to be assignd to this + * @return this + */ +nsCString& nsCString::Assign(PRUnichar aChar) { + nsStr::Truncate(*this,0,0); + return Append(aChar); +} + +/** + * WARNING! THIS IS A VERY SPECIAL METHOD. + * This method "steals" the contents of aSource and hands it to aDest. + * Ordinarily a copy is made, but not in this version. + * @update gess10/30/98 * @param * @return */ -nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { +#ifdef AIX +nsCString& nsCString::operator=(const nsSubsumeCStr& aSubsumeString) { + nsSubsumeCStr temp(aSubsumeString); // a temp is needed for the AIX compiler + CSubsume(*this,temp); +#else +nsCString& nsCString::operator=(nsSubsumeCStr& aSubsumeString) { + CSubsume(*this,aSubsumeString); +#endif // AIX + return *this; +} + + +/** + * append given string to this string + * @update gess 01/04/99 + * @param aString : string to be appended to this + * @return this + */ +nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { + if(-1==aCount) aCount=aString.mLength; + if(0=0) && (aChar<128)) { + return Append((char)aChar); + } + return *this; +} + +/** + * + * @update gess 01/04/99 + * @param + * @return + */ +nsCString& nsCString::Append(PRInt32 aInteger,PRInt32 aRadix) { char* fmt = "%d"; if (8 == aRadix) { fmt = "%o"; @@ -1085,27 +977,28 @@ nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { fmt = "%x"; } char buf[40]; - PR_snprintf(buf, sizeof(buf), fmt, aInteger); - Append(buf); - return *this; + // *** XX UNCOMMENT THIS LINE + //PR_snprintf(buf, sizeof(buf), fmt, aInteger); + sprintf(buf,fmt,aInteger); + return Append(buf); } /** * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param * @return */ -nsString1& nsString1::Append(float aFloat){ +nsCString& nsCString::Append(float aFloat){ char buf[40]; - PR_snprintf(buf, sizeof(buf), "%g", aFloat); - Append(buf); - return *this; + // *** XX UNCOMMENT THIS LINE + //PR_snprintf(buf, sizeof(buf), "%g", aFloat); + sprintf(buf,"%g",aFloat); + return Append(buf); } - /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -1116,8 +1009,9 @@ nsString1& nsString1::Append(float aFloat){ * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { - return Mid(aCopy,0,aCount); +PRUint32 nsCString::Left(nsCString& aDest,PRInt32 aCount) const{ + nsStr::Assign(aDest,*this,0,aCount,mAgent); + return aDest.mLength; } /* @@ -1126,27 +1020,14 @@ PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { * * * @update gess 4/1/98 - * @param aCopy -- Receiving string + * @param aDest -- Receiving string * @param aCount -- number of chars to copy * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 nsString1::Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const { - aCopy.Truncate(); - if(anOffsetaCopy.mLength) ? aCopy.mLength : aCount; //don't try to copy more than you are given - if (aCount < 0) aCount = aCopy.mLength; - if(0<=anOffset) { - if(aCount>0) { - - //1st optimization: If you're inserting at end, then simply append! - if(anOffset>=mLength){ - Append(aCopy,aCopy.mLength); - return aCopy.mLength; - } - - if(mLength+aCount >= mCapacity) { - EnsureCapacityFor(mLength+aCount); - } - - PRUnichar* last = mStr + mLength; - PRUnichar* first = mStr + anOffset-1; - PRUnichar* next = mStr + mLength + aCount; - - //Copy rightmost chars, up to offset+aCount... - while(first=mCapacity) { - EnsureCapacityFor(mLength+1); +nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ + if(aCString){ + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mStr=(char*)aCString; + temp.mLength=nsCRT::strlen(aCString); + if(aCount<0) + aCount=temp.mLength; + if(temp.mLength && (0= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is a valid space character - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -PRBool nsString1::IsSpace(PRUnichar aChar) { - // XXX i18n - if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is valid digit - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */PRBool nsString1::IsDigit(PRUnichar aChar) { - // XXX i18n - return PRBool((aChar >= '0') && (aChar <= '9')); -} - - -/** - * This method trims characters found in aTrimSet from - * either end of the underlying string. - * - * @update gess 3/31/98 - * @param aTrimSet -- contains chars to be trimmed from - * both ends - * @return this - */ -nsString1& nsString1::Trim(const char* aTrimSet, - PRBool aEliminateLeading, - PRBool aEliminateTrailing) -{ - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength-1; - PRUnichar* to = mStr; - - //begin by find the first char not in aTrimSet - if(aEliminateLeading) { - while (from < end) { - PRUnichar ch = *from; - if(!strchr(aTrimSet,char(ch))) { - break; - } - from++; - } - } - - //Now, find last char not in aTrimSet - if(aEliminateTrailing) { - while(end> from) { - PRUnichar ch = *end; - if(!strchr(aTrimSet,char(ch))) { - break; - } - end--; - } - } - - //now rewrite your string without unwanted - //leading or trailing characters. - if (from != to) { - while (from <= end) { - *to++ = *from++; - } - } - else { - to = ++end; - } - - *to = '\0'; - mLength = to - mStr; - return *this; -} - -/** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. - * - * @update gess 3/31/98 - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this - */ -nsString1& nsString1::CompressWhitespace( PRBool aEliminateLeading, - PRBool aEliminateTrailing) -{ - Trim(" \r\n\t",aEliminateLeading,aEliminateTrailing); - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength; - PRUnichar* to = from; - - //this code converts /n, /t, /r into normal space ' '; - //it also eliminates runs of whitespace... - while (from < end) { - PRUnichar ch = *from++; - if (IsSpace(ch)) { - *to++ = ' '; - while (from < end) { - ch = *from++; - if (!IsSpace(ch)) { - *to++ = ch; - break; - } - } - } else { - *to++ = ch; - } - } - - *to = '\0'; - mLength = to - mStr; - return *this; -} - -/** - * This method strips whitespace throughout the string - * - * @update gess 7/27/98 - * @return this - */ -nsString1& nsString1::StripWhitespace() -{ - Trim(" \r\n\t"); - return StripChars("\r\t\n"); -} - -/** - * This method is used to replace all occurances of the - * given source char with the given dest char - * - * @param - * @return *this - */ -nsString1& nsString1::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength; - - while (from < end) { - PRUnichar ch = *from; - if(ch==aSourceChar) { - *from = aDestChar; - } - from++; - } -return *this; -} - +/********************************************************************** + Searching methods... + *********************************************************************/ + /** * Search for given character within this string. * This method does so by using a binary search, * so your string HAD BETTER BE ORDERED! * - * @update gess 3/25/98 * @param aChar is the unicode char to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { +PRInt32 nsCString::BinarySearch(PRUnichar aChar) const{ PRInt32 low=0; PRInt32 high=mLength-1; while (low <= high) { int middle = (low + high) >> 1; - if (mStr[middle]==aChar) + PRUnichar theChar=GetCharAt(*this,middle); + if (theChar==aChar) return middle; - if (mStr[middle]>aChar) + if (theChar>aChar) high = middle - 1; else low = middle + 1; @@ -1514,64 +1182,60 @@ PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { * @param aCStringBuf - charstr to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString1::Find(const char* aCStringBuf) const{ - NS_ASSERTION(0!=aCStringBuf,kNullPointerError); +PRInt32 nsCString::Find(const char* aCString,PRBool aIgnoreCase,PRInt32 anOffset) const{ PRInt32 result=kNotFound; - if(aCStringBuf) { - PRInt32 len=strlen(aCStringBuf); - if((00;i--){ - char* pos=strchr(anAsciiSet,char(mStr[i])); - if(pos) - return i; - } - } - return kNotFound; -} - -/** - * - * - * @update gess 3/25/98 - * @param - * @return - */ -PRInt32 nsString1::RFindCharInSet(nsString1& aSet,PRInt32 anOffset) const{ - if(aSet.Length()) { - for(PRInt32 i=mLength-1;i>0;i--){ - PRInt32 pos=aSet.Find(mStr[i]); - if(kNotFound!=pos) - return i; - } - } - return kNotFound; -} - -/** - * - * - * @update gess 3/25/98 - * @param - * @return - */ -PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ +PRInt32 nsCString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset) const{ NS_ASSERTION(0!=aString,kNullPointerError); + PRInt32 result=kNotFound; if(aString) { - PRInt32 len=nsCRT::strlen(aString); - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],aString,len); - else result=nsCRT::strncmp(&mStr[offset],aString,len); - if(0==result) - return offset; //in this case, 0 means they match - } - } + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mLength=nsCRT::strlen(aString); + temp.mStr=(char*)aString; + result=nsStr::RFindSubstr(*this,temp,aIgnoreCase,anOffset); } - return kNotFound; + return result; +} + +/** + * Search for a given char, starting at given offset + * + * @update gess 3/25/98 + * @param + * @return offset of found char, or -1 (kNotFound) + */ +PRInt32 nsCString::RFind(PRUnichar aChar,PRUint32 anOffset,PRBool aIgnoreCase) const{ + PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset); + return result; +} + + + +/** + * Search for a given char, starting at given offset + * + * @update gess 3/25/98 + * @param + * @return offset of found char, or -1 (kNotFound) + */ +PRInt32 nsCString::RFindChar(PRUnichar aChar,PRBool aIgnoreCase,PRInt32 anOffset) const{ + PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset); + return result; } /** @@ -1697,21 +1371,20 @@ PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ * * @update gess 3/25/98 * @param - * @return + * @return */ -PRInt32 nsString1::RFind(const nsString1& aString,PRBool aIgnoreCase) const{ - PRInt32 len=aString.mLength; - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],aString.mStr,len); - else result=nsCRT::strncmp(&mStr[offset],aString.mStr,len); - if(0==result) - return offset; //in this case, 0 means they match - } +PRInt32 nsCString::RFindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{ + NS_ASSERTION(0!=aCStringSet,kNullPointerError); + + PRInt32 result=kNotFound; + if(aCStringSet) { + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mLength=nsCRT::strlen(aCStringSet); + temp.mStr=(char*)aCStringSet; + result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset); } - return kNotFound; + return result; } /** @@ -1719,316 +1392,313 @@ PRInt32 nsString1::RFind(const nsString1& aString,PRBool aIgnoreCase) const{ * * @update gess 3/25/98 * @param - * @return + * @return */ -PRInt32 nsString1::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=anAsciiSet,kNullPointerError); +PRInt32 nsCString::RFindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{ + NS_ASSERTION(0!=aStringSet,kNullPointerError); - if(anAsciiSet) { - PRInt32 len=strlen(anAsciiSet); - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],anAsciiSet,len); - else result=nsCRT::strncmp(&mStr[offset],anAsciiSet,len); - if(0==result) - return offset; //in this case, 0 means they match - } - } + PRInt32 result=kNotFound; + if(aStringSet) { + nsStr temp; + nsStr::Initialize(temp,eTwoByte); + temp.mLength=nsCRT::strlen(aStringSet); + temp.mUStr=(PRUnichar*)aStringSet; + result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset); } - return kNotFound; + return result; } /** - * Scans this string backwards for first occurance of - * the given char. + * * * @update gess 3/25/98 * @param - * @return offset of char in string, or -1 (kNotFound) + * @return */ -PRInt32 nsString1::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ - chartype uc=nsCRT::ToUpper(aChar); - for(PRInt32 offset=mLength-1;offset>=0;offset--) - if(aIgnoreCase) { - if(nsCRT::ToUpper(mStr[offset])==uc) - return offset; - } - else if(mStr[offset]==aChar) - return offset; //in this case, 0 means they match - return kNotFound; - +PRInt32 nsCString::RFindCharInSet(const nsStr& aSet,PRInt32 anOffset) const{ + PRInt32 result=nsStr::RFindCharInSet(*this,aSet,PR_FALSE,anOffset); + return result; } + /************************************************************** COMPARISON METHODS... **************************************************************/ + /** * Compares given cstring to this string. - * @update gess 7/27/98 - * @param aCString pts to a cstring + * @update gess 01/04/99 + * @param aCString points to a cstring * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length * @return -1,0,1 */ -PRInt32 nsString1::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { +PRInt32 nsCString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount) const { NS_ASSERTION(0!=aCString,kNullPointerError); - if(-1!=aLength) { - - //if you're given a length, use it to determine the max # of bytes to compare. - //In some cases, this can speed up the string comparison. - - int maxlen=(aLength(const nsString1 &S) const {return PRBool(Compare(S)>0);} -PRBool nsString1::operator>(const char *s) const {return PRBool(Compare(s)>0);} -PRBool nsString1::operator>(const PRUnichar *s) const {return PRBool(Compare(s)>0);} -PRBool nsString1::operator<=(const nsString1 &S) const {return PRBool(Compare(S)<=0);} -PRBool nsString1::operator<=(const char *s) const {return PRBool(Compare(s)<=0);} -PRBool nsString1::operator<=(const PRUnichar *s) const {return PRBool(Compare(s)<=0);} -PRBool nsString1::operator>=(const nsString1 &S) const {return PRBool(Compare(S)>=0);} -PRBool nsString1::operator>=(const char *s) const {return PRBool(Compare(s)>=0);} -PRBool nsString1::operator>=(const PRUnichar *s) const {return PRBool(Compare(s)>=0);} +/** + * Compare given nsStr with this cstring. + * + * @param aString is an nsStr instance to be compared + * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length + * @return -1,0,1 + */ +PRInt32 nsCString::Compare(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + return nsStr::Compare(*this,aString,aCount,aIgnoreCase); +} + + +PRBool nsCString::operator==(const nsStr& S) const {return Equals(S);} +PRBool nsCString::operator==(const char* s) const {return Equals(s);} +PRBool nsCString::operator==(const PRUnichar* s) const {return Equals(s);} + +PRBool nsCString::operator!=(const nsStr& S) const {return PRBool(Compare(S)!=0);} +PRBool nsCString::operator!=(const char* s) const {return PRBool(Compare(s)!=0);} +PRBool nsCString::operator!=(const PRUnichar* s) const {return PRBool(Compare(s)!=0);} + +PRBool nsCString::operator<(const nsStr& S) const {return PRBool(Compare(S)<0);} +PRBool nsCString::operator<(const char* s) const {return PRBool(Compare(s)<0);} +PRBool nsCString::operator<(const PRUnichar* s) const {return PRBool(Compare(s)<0);} + +PRBool nsCString::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} +PRBool nsCString::operator>(const char* s) const {return PRBool(Compare(s)>0);} +PRBool nsCString::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} + +PRBool nsCString::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} +PRBool nsCString::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} +PRBool nsCString::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} + +PRBool nsCString::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} +PRBool nsCString::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} +PRBool nsCString::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} + + +PRBool nsCString::EqualsIgnoreCase(const nsStr& aString) const { + return Equals(aString,PR_TRUE); +} + +PRBool nsCString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); +} + +PRBool nsCString::EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); +} /** * Compare this to given string; note that we compare full strings here. * - * @update gess 7/27/98 - * @param aString is the other nsString1 to be compared to + * @update gess 01/04/99 + * @param aString is the other nsCString to be compared to + * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString1::Equals(const nsString1& aString) const { - if(aString.mLength==mLength) { - PRInt32 result=nsCRT::strcmp(mStr,aString.mStr); - return PRBool(0==result); - } - return PR_FALSE; +PRBool nsCString::Equals(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); + return result; } - /** * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 7/27/98 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. + * + * @param aString is the CString to be compared + * @param aCount tells us how many chars you want to compare starting with start of string + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString1::Equals(const char* aCString,PRInt32 aLength) const{ - NS_ASSERTION(0!=aCString,kNullPointerError); - - if((aLength>0) && (aLength!=mLength)) - return PR_FALSE; - - PRInt32 result=nsCRT::strcmp(mStr,aCString); - return PRBool(0==result); +PRBool nsCString::Equals(const char* aCString,PRBool aIgnoreCase,PRInt32 aCount) const{ + PRInt32 theAnswer=Compare(aCString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); + return result; } - -/** - * Compare this to given atom - * @update gess 7/27/98 - * @param aAtom -- atom to compare to this - * @return TRUE if equal - */ -PRBool nsString1::Equals(const nsIAtom* aAtom) const -{ - NS_ASSERTION(0!=aAtom,kNullPointerError); - PRInt32 result=nsCRT::strcmp(mStr,aAtom->GetUnicode()); - return PRBool(0==result); -} - - -/** - * Compare given strings - * @update gess 7/27/98 - * @param s1 -- first string to be compared - * @param s2 -- second string to be compared - * @return TRUE if equal - */ -PRBool nsString1::Equals(const PRUnichar* s1, const PRUnichar* s2) const { - NS_ASSERTION(0!=s1,kNullPointerError); - NS_ASSERTION(0!=s2,kNullPointerError); - PRBool result=PR_FALSE; - if((s1) && (s2)){ - PRInt32 cmp=nsCRT::strcmp(s1,s2); - result=PRBool(0==cmp); - } +PRBool nsCString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); return result; } /** - * Compares all chars in both strings w/o regard to case - * @update gess 7/27/98 - * @param - * @return + * Determine if given char in valid alpha range + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if in alpha range */ -PRBool nsString1::EqualsIgnoreCase(const nsString1& aString) const{ - if(aString.mLength==mLength) { - PRInt32 result=nsCRT::strcasecmp(mStr,aString.mStr); - return PRBool(0==result); +PRBool nsCString::IsAlpha(PRUnichar aChar) { + // XXX i18n + if (((aChar >= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { + return PR_TRUE; } return PR_FALSE; } +/** + * Determine if given char is a valid space character + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if is valid space char + */ +PRBool nsCString::IsSpace(PRUnichar aChar) { + // XXX i18n + if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { + return PR_TRUE; + } + return PR_FALSE; +} + +/** + * Determine if given char is valid digit + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if char is a valid digit + */ +PRBool nsCString::IsDigit(PRUnichar aChar) { + // XXX i18n + return PRBool((aChar >= '0') && (aChar <= '9')); +} + + +/************************************************************** + Define the string deallocator class... + **************************************************************/ +#ifndef RICKG_TESTBED +class nsCStringDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + static nsMemoryAgent theAgent; + nsCString* aString= (nsCString*)anObject; + if(aString){ + aString->mAgent=&theAgent; + delete aString; + } + return 0; + } +}; +#endif + +/**************************************************************************** + * This class, appropriately enough, creates and recycles nsCString objects.. + ****************************************************************************/ + + + +#ifndef RICKG_TESTBED +class nsCStringRecycler { +public: + nsCStringRecycler() : mDeque(0) { + } + + ~nsCStringRecycler() { + nsCStringDeallocator theDeallocator; + mDeque.ForEach(theDeallocator); //now delete the strings + } + + void Recycle(nsCString* aString) { + mDeque.Push(aString); + } + + nsCString* CreateString(void){ + nsCString* result=(nsCString*)mDeque.Pop(); + if(!result) + result=new nsCString(); + return result; + } + nsDeque mDeque; +}; +static nsCStringRecycler& GetRecycler(void); + /** * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param * @return */ -PRBool nsString1::EqualsIgnoreCase(const nsIAtom *aAtom) const{ - NS_ASSERTION(0!=aAtom,kNullPointerError); - PRBool result=PR_FALSE; - if(aAtom){ - PRInt32 cmp=nsCRT::strcasecmp(mStr,aAtom->GetUnicode()); - result=PRBool(0==cmp); - } - return result; +nsCStringRecycler& GetRecycler(void){ + static nsCStringRecycler gCRecycler; + return gCRecycler; } - +#endif /** - * Compares given unicode string to this w/o regard to case - * @update gess 7/27/98 - * @param s1 is the unicode string to be compared with this - * @param aLength is the length of s1, not # of bytes to compare - * @return true if full length of both strings are equal (modulo case) - */ -PRBool nsString1::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { - NS_ASSERTION(0!=s1,kNullPointerError); - NS_ASSERTION(0!=s2,kNullPointerError); - PRBool result=PR_FALSE; - if((s1) && (s2)){ - PRInt32 cmp=nsCRT::strcasecmp(s1,s2); - result=PRBool(0==cmp); - } - return result; -} - - -/** - * Compare this to given string w/o regard to case; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an optimization. - * - * @update gess 7/27/98 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString1::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { - NS_ASSERTION(0!=aCString,kNullPointerError); - - if((aLength>0) && (aLength!=mLength)) - return PR_FALSE; - - PRInt32 cmp=nsCRT::strcasecmp(mStr,aCString); - return PRBool(0==cmp); -} - - -/** - * - * @update gess 7/27/98 + * Call this mehod when you're done + * @update gess 01/04/99 * @param * @return */ -void nsString1::DebugDump(ostream& aStream) const { - for(int i=0;i= PRInt32(sizeof(buf))) { + cp = aString.ToNewCString(); + } else { + aString.ToCString(cp, len + 1); + } + if(len>0) + ::fwrite(cp, 1, len, out); + if (cp != buf) { + delete[] cp; + } + return (int) len; +} + + +/*********************************************************************** + IMPLEMENTATION NOTES: AUTOSTRING... + ***********************************************************************/ + + +/** + * Special case constructor, that allows the consumer to provide + * an underlying buffer for performance reasons. + * @param aBuffer points to your buffer + * @param aBufSize defines the size of your buffer + * @param aCurrentLength tells us the current length of the buffer + */ +nsCAutoString::nsCAutoString() : nsCString(){ + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); } /** - * - * @update gess 7/27/98 - * @param - * @return + * construct from external buffer and given string + * @param anExtBuffer describes an external buffer + * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString1::nsAutoString1(const char* aCString) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(aCString); +nsCAutoString::nsCAutoString(const nsCAutoString& aString) : nsCString() { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); + Append(aString); +} + + +/** + * Copy construct from ascii c-string + * @param aCString is a ptr to a 1-byte cstr + */ +nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); + Append(aCString,aLength); } /** - * - * @update gess 7/27/98 - * @param - * @return + * Copy construct from ascii c-string + * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString1::nsAutoString1(const nsString1& other) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(other.GetUnicode(),other.Length()); +nsCAutoString::nsCAutoString(CSharedStrBuffer& aBuffer) : nsCString() { + mAgent=0; + if(!aBuffer.mBuffer) { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + } + else { + nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); + } + AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. } /** - * - * @update gess 7/27/98 - * @param - * @return + * Copy construct from uni-string + * @param aString is a ptr to a unistr */ -nsAutoString1::nsAutoString1(PRUnichar aChar) - : nsString1() -{ - INIT_AUTO_STRING(); +nsCAutoString::nsCAutoString(const PRUnichar* aString,PRInt32 aLength) : nsCString() { + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); + Append(aString,aLength); +} + +/** + * Copy construct from an nsCString + * @param + */ +nsCAutoString::nsCAutoString(const nsStr& aString) : nsCString() { + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); + Append(aString); +} + + + +/** + * Copy construct from an nsCString + * @param + */ +nsCAutoString::nsCAutoString(PRUnichar aChar) : nsCString(){ + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); Append(aChar); } /** - * - * @update gess 7/27/98 - * @param - * @return + * construct from a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString */ -nsAutoString1::nsAutoString1(const nsAutoString1& other) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(other.GetUnicode(),other.mLength); +#ifdef AIX +nsCAutoString::nsCAutoString(const nsSubsumeCStr& aSubsumeStr) :nsCString() { + mAgent=0; + nsSubsumeCStr temp(aSubsumeStr); // a temp is needed for the AIX compiler + CSubsume(*this,temp); +#else +nsCAutoString::nsCAutoString( nsSubsumeCStr& aSubsumeStr) :nsCString() { + mAgent=0; + CSubsume(*this,aSubsumeStr); +#endif // AIX } /** - * - * @update gess 7/27/98 - * @param - * @return + * deconstruct the autstring + * @param */ -nsAutoString1::nsAutoString1(const PRUnichar* unicode, PRInt32 uslen) - : nsString1() -{ - INIT_AUTO_STRING(); - Append(unicode, uslen ? uslen : nsCRT::strlen(unicode)); +nsCAutoString::~nsCAutoString(){ } -/** - * - * @update gess 7/27/98 - * @param - * @return - */ -nsAutoString1::~nsAutoString1() -{ - if (mStr == mBuf) { - // Force to null so that baseclass dtor doesn't do damage - mStr = nsnull; - } -} - -/** - * nsAutoString1's buffer growing routine uses a different algorithm - * than nsString1 because the lifetime of the auto string is assumed - * to be shorter. Therefore, we double the size of the buffer each - * time we grow so that (hopefully) we quickly get to the right - * size. - */ -void nsAutoString1::EnsureCapacityFor(PRInt32 aNewLength) { - if (aNewLength > mCapacity) { - PRInt32 size = mCapacity * 2; - if (size < aNewLength) { - size = mCapacity + aNewLength; - } - mCapacity=size; - chartype* temp = new chartype[mCapacity+1]; - if (mLength > 0) { - nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype) + sizeof(chartype)); - } - if ((mStr != mBuf) && (0 != mStr)) { - delete [] mStr; - } - mStr = temp; - } -} - -void -nsAutoString1::SizeOf(nsISizeOfHandler* aHandler) const -{ +void nsCAutoString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); - if (mStr != mBuf) { - aHandler->Add(mCapacity * sizeof(chartype)); - } -} - - -/** - * - * - * @update gess 3/31/98 - * @param - * @return - */ -void nsAutoString1::SelfTest(){ - nsAutoString1 xas("Hello there"); - xas.Append("this string exceeds the max size"); - xas.DebugDump(cout); -} - -/** - * - * @update gess8/8/98 - * @param - * @return - */ -ostream& operator<<(ostream& os,nsAutoString1& aString){ - const PRUnichar* uc=aString.GetUnicode(); - int len=aString.Length(); - for(int i=0;i= PRInt32(sizeof(buf))) { - cp = aString.ToNewCString(); - } else { - aString.ToCString(cp, aLength + 1); - } - if(aLength >0) - ::fwrite(cp, 1, aLength, out); - if (cp != buf) { - delete [] cp; - } - return (int) aLength; -} - - - -/** - * - * @update gess 7/27/98 - * @param - * @return - */ -void nsString1::SelfTest(void) { - -#ifdef RICKG_DEBUG - mSelfTested=PR_TRUE; - - { - nsAutoString1 a("foobar"); - nsAutoString1 b("foo"); - nsAutoString1 c(".5111"); - nsAutoString1 d(" 5"); - PRInt32 result=a.Compare(b); - PRInt32 result2=result; - result=c.ToInteger(&result2); - result=d.ToInteger(&result2); - result2=result; - } - - static const char* kConstructorError = kConstructorError; - static const char* kComparisonError = "Comparision error!"; - static const char* kEqualsError = "Equals error!"; - - - nsAutoString1 as("Hello there"); - as.SelfTest(); - - static const char* temp="hello"; - - //first, let's test the constructors... - nsString1 empty; - empty=""; - empty="xxx"; - empty=""; - - nsString1 a(temp); - nsString1* a_=new nsString1(a); //test copy constructor - nsString1 b("world!"); - - //verify destructor... - delete a_; - a_=0; - - //Let's verify the Length() method... - NS_ASSERTION(5==a.Length(),"Error: constructor probably bad!"); - - //********************************************** - //Let's check out the ACCESSORS... - //********************************************** - - const chartype* p1=a.GetUnicode(); - for(int i=0;itemp3.Length(),kConstructorError); //should be char longer - - nsString1* es1=temp2.ToNewString(); //this should make us a new string - char* es2=temp2.ToNewCString(); - for(i=0;itemp8.Compare(temp9),kComparisonError); - NS_ASSERTION(0temp8,kComparisonError); - NS_ASSERTION(temp9>aaaa,kComparisonError); - - NS_ASSERTION(temp8<=temp8,kComparisonError); - NS_ASSERTION(temp8<=temp9,kComparisonError); - NS_ASSERTION(temp8<=bbbb,kComparisonError); - - NS_ASSERTION(temp9>=temp9,kComparisonError); - NS_ASSERTION(temp9>=temp8,kComparisonError); - NS_ASSERTION(temp9>=aaaa,kComparisonError); - - NS_ASSERTION(temp8.Equals(temp8),kEqualsError); - NS_ASSERTION(temp8.Equals(aaaa),kEqualsError); - - nsString1 temp10(temp8); - temp10.ToUpperCase(); - NS_ASSERTION(temp8.EqualsIgnoreCase(temp10),kEqualsError); - NS_ASSERTION(temp8.EqualsIgnoreCase("AAAA"),kEqualsError); - - - //********************************************** - //Now let's test a few string MANIPULATORS... - //********************************************** - - nsAutoString1 ab("ab"); - nsString1 abcde("cde"); - abcde.Insert(ab,0,2); - nsAutoString1 xxx("xxx"); - abcde.Insert(xxx,2,3); - - temp2.ToUpperCase(); - for(i=0;iAdd(mCapacity << mCharSize); #endif } + +nsSubsumeCStr::nsSubsumeCStr(nsStr& aString) : nsCString() { + CSubsume(*this,aString); +} + +nsSubsumeCStr::nsSubsumeCStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() { + mUStr=aString; + mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; +} + +nsSubsumeCStr::nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() { + mStr=aString; + mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; +} + + diff --git a/mozilla/string/obsolete/nsString.h b/mozilla/string/obsolete/nsString.h index f3a9f8c9030..521d49015db 100644 --- a/mozilla/string/obsolete/nsString.h +++ b/mozilla/string/obsolete/nsString.h @@ -20,32 +20,33 @@ /*********************************************************************** MODULE NOTES: - A. There are two philosophies to building string classes: - 1. Hide the underlying buffer & offer API's allow indirect iteration - 2. Reveal underlying buffer, risk corruption, but gain performance - - We chose the second option for performance reasons. - - B Our internal buffer always holds capacity+1 bytes. + This version of the nsString class offers many improvements over the + original version: + 1. Wide and narrow chars + 2. Allocators + 3. Much smarter autostrings + 4. Subsumable strings + 5. Memory pools and recycling ***********************************************************************/ -#ifndef _nsString1 -#define _nsString1 - +#ifndef _nsCString_ +#define _nsCString_ +#include "nsString2.h" #include "prtypes.h" #include "nscore.h" -#include "nsIAtom.h" #include #include +#include "nsCRT.h" #include "nsStr.h" +#include "nsIAtom.h" -#include "nsString2.h" //get new string class -class nsISizeOfHandler; +class NS_COM nsSubsumeCStr; + +class NS_COM nsCString : public nsStr { -class NS_COM nsString1 { public: /** @@ -54,46 +55,74 @@ class NS_COM nsString1 { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString1(); +nsCString(nsIMemoryAgent* anAgent=0); + /** * This constructor accepts an isolatin string - * @param an ascii is a ptr to a 1-byte cstr + * @param aCString is a ptr to a 1-byte cstr */ -nsString1(const char* aCString); +nsCString(const char* aCString,PRInt32 aLength=-1,nsIMemoryAgent* anAgent=0); + +/** + * This constructor accepts a unichar string + * @param aCString is a ptr to a 2-byte cstr + */ +nsCString(const PRUnichar* aString,PRInt32 aLength=1,nsIMemoryAgent* anAgent=0); + +/** + * This is a copy constructor that accepts an nsStr + * @param reference to another nsCString + */ +nsCString(const nsStr&,nsIMemoryAgent* anAgent=0); /** * This is our copy constructor - * @param reference to another nsString1 + * @param reference to another nsCString */ -nsString1(const nsString1&); +nsCString(const nsCString& aString); /** - * Constructor from a unicode string - * @param anicodestr pts to a unicode string + * This constructor takes a subsumestr + * @param reference to subsumestr */ -nsString1(const PRUnichar* aUnicode); +nsCString(nsSubsumeCStr& aSubsumeStr); /** - * Virtual Destructor + * Destructor + * */ -virtual ~nsString1(); - +virtual ~nsCString(); /** * Retrieve the length of this string * @return string length */ -PRInt32 Length() const { return mLength; } +inline PRInt32 Length() const { return (PRInt32)mLength; } +/** + * Retrieve the size of this string + * @return string length + */ +virtual void SizeOf(nsISizeOfHandler* aHandler) const; + + +/** + * Call this method if you want to force a different string capacity + * @update gess7/30/98 + * @param aLength -- contains new length for mStr + * @return + */ +void SetLength(PRUint32 aLength) { + Truncate(aLength); +} /** * Sets the new length of the string. * @param aLength is new string length. * @return nada */ -void SetLength(PRInt32 aLength); - +void SetCapacity(PRUint32 aLength); /** * This method truncates this string to given length. * @@ -103,20 +132,6 @@ void SetLength(PRInt32 aLength); void Truncate(PRInt32 anIndex=0); -/** - * This method gets called when the internal buffer needs - * to grow to a given size. - * @param aNewLength -- new capacity of string - * @return void - */ -virtual void EnsureCapacityFor(PRInt32 aNewLength); - -/** - * - * @param - */ -virtual void SizeOf(nsISizeOfHandler* aHandler) const; - /** * Determine whether or not the characters in this * string are in sorted order. @@ -125,6 +140,17 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const; */ PRBool IsOrdered(void) const; +/** + * Determine whether or not the characters in this + * string are in store as 1 or 2 byte (unicode) strings. + * + * @return TRUE if ordered. + */ +PRBool IsUnicode(void) const { + PRBool result=PRBool(mCharSize==eTwoByte); + return result; +} + /** * Determine whether or not this string has a length of 0 * @@ -138,54 +164,20 @@ PRBool IsEmpty(void) const { Accessor methods... *********************************************************************/ -/** - * Retrieve pointer to internal string value - * @return PRUnichar* to internal string - */ -const PRUnichar* GetUnicode(void) const; +const char* GetBuffer(void) const; -/** - * - * @param - * @return - */ -operator const PRUnichar*() const; -/** - * Retrieve unicode char at given index - * @param offset into string - * @return PRUnichar* to internal string - */ -//PRUnichar operator()(PRInt32 anIndex) const; + /** + * Get nth character. + */ +PRUnichar operator[](PRUint32 anIndex) const; +PRUnichar CharAt(PRUint32 anIndex) const; +PRUnichar First(void) const; +PRUnichar Last(void) const; -/** - * Retrieve reference to unicode char at given index - * @param offset into string - * @return PRUnichar& from internal string - */ -PRUnichar& operator[](PRInt32 anIndex) const; - -/** - * Retrieve reference to unicode char at given index - * @param offset into string - * @return PRUnichar& from internal string - */ -PRUnichar& CharAt(PRInt32 anIndex) const; - -/** - * Retrieve reference to first unicode char in string - * @return PRUnichar from internal string - */ -PRUnichar& First() const; - -/** - * Retrieve reference to last unicode char in string - * @return PRUnichar from internal string - */ -PRUnichar& Last() const; - -PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); +PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); +operator nsStr&() {return *this;} /********************************************************************** String creation methods... @@ -196,95 +188,165 @@ PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString1 operator+(const nsString1& aString); +nsSubsumeCStr operator+(const nsStr& aString); /** * create a new string by adding this to the given buffer. * @param aCString is a ptr to cstring to be added to this * @return newly created string */ -nsString1 operator+(const char* aCString); +nsSubsumeCStr operator+(const char* aCString); + +/** + * create a new string by adding this to the given wide buffer. + * @param aString is a ptr to UC-string to be added to this + * @return newly created string + */ +nsSubsumeCStr operator+(const PRUnichar* aString); /** * create a new string by adding this to the given char. * @param aChar is a char to be added to this * @return newly created string */ -nsString1 operator+(char aChar); - -/** - * create a new string by adding this to the given buffer. - * @param aStr unichar buffer to be added to this - * @return newly created string - */ -nsString1 operator+(const PRUnichar* aBuffer); +nsSubsumeCStr operator+(char aChar); /** * create a new string by adding this to the given char. * @param aChar is a unichar to be added to this * @return newly created string */ -nsString1 operator+(PRUnichar aChar); +nsSubsumeCStr operator+(PRUnichar aChar); -/** - * Converts all chars in internal string to lower - */ -void ToLowerCase(); - -/** - * Converts all chars in given string to lower - */ -void ToLowerCase(nsString1& aString) const; - -/** - * Converts all chars in given string to upper - */ -void ToUpperCase(); +/********************************************************************** + Lexomorphic transforms... + *********************************************************************/ /** * Converts all chars in given string to UCS2 * which ensure that the lower 256 chars are correct. */ -void ToUCS2(PRInt32 aStartOffset); +void ToUCS2(PRUint32 aStartOffset); /** - * Converts all chars in internal string to upper + * Converts chars in this to lowercase + * @update gess 7/27/98 */ -void ToUpperCase(nsString1& aString) const; +void ToLowerCase(); + /** - * Creates a duplicate clone (ptr) of this string. - * @return ptr to clone of this string + * Converts chars in this to lowercase, and + * stores them in aOut + * @update gess 7/27/98 + * @param aOut is a string to contain result */ -nsString1* ToNewString() const; +void ToLowerCase(nsCString& aString) const; /** - * Creates an ascii clone of this string - * NOTE: This string is allocated with new; YOU MUST deallocate with delete[]! - * @return ptr to new c-String string + * Converts chars in this to uppercase + * @update gess 7/27/98 + */ +void ToUpperCase(); + +/** + * Converts chars in this to lowercase, and + * stores them in a given output string + * @update gess 7/27/98 + * @param aOut is a string to contain result + */ +void ToUpperCase(nsCString& aString) const; + + +/** + * This method is used to remove all occurances of the + * characters found in aSet from this string. + * + * @param aSet -- characters to be cut from this + * @return *this + */ +nsCString& StripChars(const char* aSet); +nsCString& StripChar(PRUnichar aChar); + +/** + * This method strips whitespace throughout the string + * + * @return this + */ +nsCString& StripWhitespace(); + +/** + * swaps occurence of 1 string for another + * + * @return this + */ +nsCString& ReplaceChar(PRUnichar aOldChar,PRUnichar aNewChar); +nsCString& ReplaceChar(const char* aSet,PRUnichar aNewChar); + +PRInt32 CountChar(PRUnichar aChar); + +/** + * This method trims characters found in aTrimSet from + * either end of the underlying string. + * + * @param aTrimSet -- contains chars to be trimmed from + * both ends + * @return this + */ +nsCString& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/** + * This method strips whitespace from string. + * You can control whether whitespace is yanked from + * start and end of string as well. + * + * @param aEliminateLeading controls stripping of leading ws + * @param aEliminateTrailing controls stripping of trailing ws + * @return this + */ +nsCString& CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/** + * This method strips whitespace from string. + * You can control whether whitespace is yanked from + * start and end of string as well. + * + * @param aEliminateLeading controls stripping of leading ws + * @param aEliminateTrailing controls stripping of trailing ws + * @return this + */ +nsCString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/********************************************************************** + string conversion methods... + *********************************************************************/ + +/** + * This method constructs a new nsCString on the stack that is a copy + * of this string. + * + */ +nsCString* ToNewString() const; + +/** + * Creates an ISOLatin1 clone of this string + * @return ptr to new isolatin1 string */ char* ToNewCString() const; +/** + * Creates a unicode clone of this string + * @return ptr to new unicode string + */ +PRUnichar* ToNewUnicode() const; + /** * Copies data from internal buffer onto given char* buffer * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer */ -char* ToCString(char* aBuf,PRInt32 aBufLength) const; - -/** - * Copies contents of this onto given string. - * @param aString to hold copy of this - * @return nada. - */ -void Copy(nsString1& aString) const; - -/** - * Creates an unichar clone of this string - * @return ptr to new unichar string - */ -PRUnichar* ToNewUnicode() const; +char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const; /** * Perform string to float conversion. @@ -293,147 +355,99 @@ PRUnichar* ToNewUnicode() const; */ float ToFloat(PRInt32* aErrorCode) const; +/** + * Try to derive the radix from the value contained in this string + * @return kRadix10, kRadix16 or kAutoDetect (meaning unknown) + */ +PRUint32 DetermineRadix(void); + /** * Perform string to int conversion. * @param aErrorCode will contain error if one occurs * @return int rep of string value */ -PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; +PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const; + /********************************************************************** String manipulation methods... *********************************************************************/ /** - * assign given PRUnichar* to this string + * Functionally equivalent to assign or operator= + * + */ +nsCString& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsCString& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsCString& SetString(const nsStr& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} + +/** + * assign given string to this string * @param aStr: buffer to be assigned to this * @param alength is the length of the given str (or -1) if you want me to determine its length * @return this */ -nsString1& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); -nsString1& SetString(const char* aCString,PRInt32 aLength=-1); -nsString1& SetString(const nsString1& aString); +nsCString& Assign(const nsStr& aString,PRInt32 aCount=-1); +nsCString& Assign(const char* aString,PRInt32 aCount=-1); +nsCString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); +nsCString& Assign(char aChar); +nsCString& Assign(PRUnichar aChar); /** - * assign given string to this one + * here come a bunch of assignment operators... * @param aString: string to be added to this * @return this */ -nsString1& operator=(const nsString1& aString); +nsCString& operator=(const nsStr& aString) {return Assign(aString);} +nsCString& operator=(const nsCString& aString) {return Assign(aString);} +nsCString& operator=(char aChar) {return Assign(aChar);} +nsCString& operator=(PRUnichar aChar) {return Assign(aChar);} +nsCString& operator=(const char* aCString) {return Assign(aCString);} +nsCString& operator=(const PRUnichar* aString) {return Assign(aString);} +#ifdef AIX +nsCString& operator=(const nsSubsumeCStr& aSubsumeString); // AIX requires a const here +#else +nsCString& operator=(nsSubsumeCStr& aSubsumeString); +#endif /** - * assign given char* to this string - * @param aCString: buffer to be assigned to this - * @return this - */ -nsString1& operator=(const char* aCString); - -/** - * assign given char to this string - * @param aChar: char to be assignd to this - * @return this - */ -nsString1& operator=(char aChar); - -/** - * assign given unichar* to this string - * @param aBuffer: unichar buffer to be assigned to this - * @return this - */ -nsString1& operator=(const PRUnichar* aBuffer); - -/** - * assign given char to this string - * @param aChar: char to be assignd to this - * @return this - */ -nsString1& operator=(PRUnichar aChar); - -/** - * append given string to this string + * Here's a bunch of append mehtods for varying types... * @param aString : string to be appended to this * @return this */ -nsString1& operator+=(const nsString1& aString); +nsCString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} +nsCString& operator+=(const char* aCString) {return Append(aCString);} +//nsCString& operator+=(char aChar){return Append(aChar);} +nsCString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} +nsCString& operator+=(PRUnichar aChar){return Append(aChar);} -/** - * append given buffer to this string - * @param aCString: buffer to be appended to this - * @return this +/* + * Appends n characters from given string to this, + * This version computes the length of your given string + * + * @param aString is the source to be appended to this + * @return number of chars copied */ -nsString1& operator+=(const char* aCString); +nsCString& Append(const nsStr& aString) {return Append(aString,aString.mLength);} +nsCString& Append(const nsCString& aString) {return Append(aString,aString.mLength);} + -/** - * append given buffer to this string - * @param aBuffer: buffer to be appended to this - * @return this +/* + * Appends n characters from given string to this, + * + * @param aString is the source to be appended to this + * @param aCount -- number of chars to copy + * @return number of chars copied */ -nsString1& operator+=(const PRUnichar* aBuffer); - -/** - * append given char to this string - * @param aChar: char to be appended to this - * @return this - */ -nsString1& operator+=(PRUnichar aChar); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const nsString1& aString,PRInt32 aLength=-1); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const char* aCString,PRInt32 aLength=-1); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @return this - */ -nsString1& Append(char aChar); - -/** - * append given unichar buffer to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1); - -/** - * append given unichar character to this string - * @param aChar is the char to be appended to this - * @return this - */ -nsString1& Append(PRUnichar aChar); - -/** - * Append an integer onto this string - * @param aInteger is the int to be appended - * @param aRadix specifies 8,10,16 - * @return this - */ -nsString1& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16 - -/** - * Append a float value onto this string - * @param aFloat is the float to be appended - * @return this - */ -nsString1& Append(float aFloat); - +nsCString& Append(const nsStr& aString,PRInt32 aCount); +nsCString& Append(const char* aString,PRInt32 aCount=-1); +nsCString& Append(const PRUnichar* aString,PRInt32 aCount=-1); +nsCString& Append(char aChar); +nsCString& Append(PRUnichar aChar); +nsCString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 +nsCString& Append(float aFloat); + /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -443,7 +457,7 @@ nsString1& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; +PRUint32 Left(nsCString& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -455,7 +469,7 @@ PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; +PRUint32 Mid(nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -466,7 +480,7 @@ PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; +PRUint32 Right(nsCString& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -477,17 +491,29 @@ PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -PRInt32 Insert(const nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** - * Insert a single unicode char into this string at + * Insert a given string into this string at * a specified offset. * - * @param aChar char to be inserted into this string + * @param aString* to be inserted into this string * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); +nsCString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); + +/** + * Insert a single char into this string at + * a specified offset. + * + * @param character to be inserted into this string + * @param anOffset is insert pos in str + * @return the number of chars inserted into this string + */ +//nsCString& Insert(char aChar,PRUint32 anOffset); +nsCString& Insert(PRUnichar aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -497,56 +523,180 @@ PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString1& Cut(PRInt32 anOffset,PRInt32 aCount); +nsCString& Cut(PRUint32 anOffset,PRInt32 aCount); + + +/********************************************************************** + Searching methods... + *********************************************************************/ + +/** + * Search for given character within this string. + * This method does so by using a binary search, + * so your string HAD BETTER BE ORDERED! + * + * @param aChar is the unicode char to be found + * @return offset in string, or -1 (kNotFound) + */ +PRInt32 BinarySearch(PRUnichar aChar) const; /** - * This method is used to remove all occurances of the - * characters found in aSet from this string. + * Search for given substring within this string * - * @param aSet -- characters to be cut from this - * @return *this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @return offset in string, or -1 (kNotFound) */ -nsString1& StripChars(const char* aSet); +PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** - * This method is used to replace all occurances of the - * given source char with the given dest char + * Search for given char within this string * - * @param - * @return *this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) */ -nsString1& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); +PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** - * This method strips whitespace throughout the string - * - * @return this + * This method searches this string for the first character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this */ -nsString1& StripWhitespace(); +PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + /** - * This method trims characters found in aTrimSet from - * either end of the underlying string. - * - * @param aTrimSet -- contains chars to be trimmed from - * both ends - * @return this + * This methods scans the string backwards, looking for the given string + * @param aString is substring to be sought in this + * @param aIgnoreCase tells us whether or not to do caseless compare + * @return offset in string, or -1 (kNotFound) */ -nsString1& Trim(const char* aSet, - PRBool aEliminateLeading=PR_TRUE, - PRBool aEliminateTrailing=PR_TRUE); +PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + /** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. + * Search for given char within this string * - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) */ -nsString1& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, - PRBool aEliminateTrailing=PR_TRUE); +PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + +/** + * This method searches this string for the last character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this + */ +PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + + + +/********************************************************************** + Comparison methods... + *********************************************************************/ + +/** + * Compares a given string type to this string. + * @update gess 7/27/98 + * @param S is the string to be compared + * @param aIgnoreCase tells us how to treat case + * @return -1,0,1 + */ +virtual PRInt32 Compare(const nsStr &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; + +/** + * These methods compare a given string type to this one + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator==(const nsStr &aString) const; +PRBool operator==(const char *aString) const; +PRBool operator==(const PRUnichar* aString) const; + +/** + * These methods perform a !compare of a given string type to this + * @param aString is the string to be compared to this + * @return TRUE + */ +PRBool operator!=(const nsStr &aString) const; +PRBool operator!=(const char* aString) const; +PRBool operator!=(const PRUnichar* aString) const; + +/** + * These methods test if a given string is < than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator<(const nsStr &aString) const; +PRBool operator<(const char* aString) const; +PRBool operator<(const PRUnichar* aString) const; + +/** + * These methods test if a given string is > than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator>(const nsStr &S) const; +PRBool operator>(const char* aString) const; +PRBool operator>(const PRUnichar* aString) const; + +/** + * These methods test if a given string is <= than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator<=(const nsStr &S) const; +PRBool operator<=(const char* aString) const; +PRBool operator<=(const PRUnichar* aString) const; + +/** + * These methods test if a given string is >= than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator>=(const nsStr &S) const; +PRBool operator>=(const char* aString) const; +PRBool operator>=(const PRUnichar* aString) const; + +/** + * Compare this to given string; note that we compare full strings here. + * The optional length argument just lets us know how long the given string is. + * If you provide a length, it is compared to length of this string as an + * optimization. + * + * @param aString -- the string to compare to this + * @param aCount -- number of chars in given string you want to compare + * @return TRUE if equal + */ +PRBool Equals(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; + +PRBool EqualsIgnoreCase(const nsStr& aString) const; +PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; +PRBool EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aCount=-1) const; + /** * Determine if given char is a valid space character @@ -572,248 +722,83 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -/********************************************************************** - Searching methods... - *********************************************************************/ - -/** - * Search for given character within this string. - * This method does so by using a binary search, - * so your string HAD BETTER BE ORDERED! - * - * @param aChar is the unicode char to be found - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 BinarySearch(PRUnichar aChar) const; +static void Recycle(nsCString* aString); +static nsCString* CreateString(void); -/** - * Search for given substring within this string - * - * @param aString is substring to be sought in this - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 Find(const char* aString) const; -PRInt32 Find(const PRUnichar* aString) const; -PRInt32 Find(const nsString1& aString) const; +virtual void DebugDump(ostream& aStream) const; -/** - * Search for given char within this string - * - * @param aChar - char to be found - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const; + nsIMemoryAgent* mAgent; -/** - * This method searches this string for the first character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 FindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; - -/** - * This method searches this string for the last character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 RFindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; - - -/** - * This methods scans the string backwards, looking for the given string - * @param aString is substring to be sought in this - * @param aIgnoreCase tells us whether or not to do caseless compare - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString1& aString,PRBool aIgnoreCase=PR_FALSE) const; - -/** - * This methods scans the string backwards, looking for the given char - * @param char is the char to be sought in this - * @param aIgnoreCase tells us whether or not to do caseless compare - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const; - -/********************************************************************** - Comparison methods... - *********************************************************************/ - -/** - * Compares a given string type to this string. - * @update gess 7/27/98 - * @param S is the string to be compared - * @param aIgnoreCase tells us how to treat case - * @return -1,0,1 - */ -virtual PRInt32 Compare(const nsString1 &aString,PRBool aIgnoreCase=PR_FALSE) const; -virtual PRInt32 Compare(const char *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; -virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; - -/** - * These methods compare a given string type to this one - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator==(const nsString1 &aString) const; -PRBool operator==(const char *aString) const; -PRBool operator==(const PRUnichar* aString) const; -PRBool operator==(PRUnichar* aString) const; - -/** - * These methods perform a !compare of a given string type to this - * @param aString is the string to be compared to this - * @return TRUE - */ -PRBool operator!=(const nsString1 &aString) const; -PRBool operator!=(const char *aString) const; -PRBool operator!=(const PRUnichar* aString) const; - -/** - * These methods test if a given string is < than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator<(const nsString1 &aString) const; -PRBool operator<(const char *aString) const; -PRBool operator<(const PRUnichar* aString) const; - -/** - * These methods test if a given string is > than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator>(const nsString1 &S) const; -PRBool operator>(const char *aCString) const; -PRBool operator>(const PRUnichar* aString) const; - -/** - * These methods test if a given string is <= than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator<=(const nsString1 &S) const; -PRBool operator<=(const char *aCString) const; -PRBool operator<=(const PRUnichar* aString) const; - -/** - * These methods test if a given string is >= than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator>=(const nsString1 &S) const; -PRBool operator>=(const char* aCString) const; -PRBool operator>=(const PRUnichar* aString) const; - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. - * @return TRUE if equal - */ -PRBool Equals(const nsString1& aString) const; -PRBool Equals(const char* aString,PRInt32 aLength=-1) const; -PRBool Equals(const nsIAtom *aAtom) const; - - -/** - * Compares to unichar string ptrs to each other - * @param s1 is a ptr to a unichar buffer - * @param s2 is a ptr to a unichar buffer - * @return TRUE if they match - */ -PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const; - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. - * @return TRUE if equal - */ -PRBool EqualsIgnoreCase(const nsString1& aString) const; -PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; -PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; - -/** - * Compares to unichar string ptrs to each other without respect to case - * @param s1 is a ptr to a unichar buffer - * @param s2 is a ptr to a unichar buffer - * @return TRUE if they match - */ -PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; - - -static void SelfTest(); -virtual void DebugDump(ostream& aStream) const; - - protected: - -typedef PRUnichar chartype; - - chartype* mStr; - PRInt32 mLength; - PRInt32 mCapacity; -#ifdef RICKG_DEBUG - static PRBool mSelfTested; -#endif }; -ostream& operator<<(ostream& os,nsString1& aString); -extern NS_COM int fputs(const nsString1& aString, FILE* out); +extern NS_COM int fputs(const nsCString& aString, FILE* out); +ostream& operator<<(ostream& aStream,const nsCString& aString); -//---------------------------------------------------------------------- -/** - * A version of nsString1 which is designed to be used as an automatic - * variable. It attempts to operate out of a fixed size internal - * buffer until too much data is added; then a dynamic buffer is - * allocated and grown as necessary. - */ -// XXX template this with a parameter for the size of the buffer? -class NS_COM nsAutoString1 : public nsString1 { +/************************************************************** + Here comes the AutoString class which uses internal memory + (typically found on the stack) for its default buffer. + If the buffer needs to grow, it gets reallocated on the heap. + **************************************************************/ + +class NS_COM nsCAutoString : public nsCString { +public: + + nsCAutoString(); + nsCAutoString(const char* aString,PRInt32 aLength=-1); + nsCAutoString(CSharedStrBuffer& aBuffer); + nsCAutoString(const PRUnichar* aString,PRInt32 aLength=-1); + nsCAutoString(const nsStr& aString); + nsCAutoString(const nsCAutoString& aString); + +#ifdef AIX + nsCAutoString(const nsSubsumeCStr& aSubsumeStr); // AIX requires a const +#else + nsCAutoString(nsSubsumeCStr& aSubsumeStr); +#endif // AIX + nsCAutoString(PRUnichar aChar); + virtual ~nsCAutoString(); + + nsCAutoString& operator=(const nsStr& aString) {nsCString::Assign(aString); return *this;} + nsCAutoString& operator=(const char* aCString) {nsCString::operator=(aCString); return *this;} + nsCAutoString& operator=(char aChar) {nsCString::operator=(aChar); return *this;} + nsCAutoString& operator=(const PRUnichar* aBuffer) {nsCString::operator=(aBuffer); return *this;} + nsCAutoString& operator=(PRUnichar aChar) {nsCString::operator=(aChar); return *this;} + + operator char*() {return mStr;} + operator const char*() {return (const char*)mStr;} + + /** + * Retrieve the size of this string + * @return string length + */ + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + + char mBuffer[32]; +}; + +/*************************************************************** + The subsumestr class is very unusual. + It differs from a normal string in that it doesn't use normal + copy semantics when another string is assign to this. + Instead, it "steals" the contents of the source string. + + This is very handy for returning nsString classes as part of + an operator+(...) for example, in that it cuts down the number + of copy operations that must occur. + + You should probably not use this class unless you really know + what you're doing. + ***************************************************************/ +class NS_COM nsSubsumeCStr : public nsCString { public: - nsAutoString1(); - nsAutoString1(const nsString1& other); - nsAutoString1(const nsAutoString1& other); - nsAutoString1(PRUnichar aChar); - nsAutoString1(const char* aCString); - nsAutoString1(const PRUnichar* us, PRInt32 uslen = -1); - virtual ~nsAutoString1(); - - nsAutoString1& operator=(const nsString1& aString) {nsString1::operator=(aString); return *this;} - nsAutoString1& operator=(const nsAutoString1& aString) {nsString1::operator=(aString); return *this;} - nsAutoString1& operator=(const char* aCString) {nsString1::operator=(aCString); return *this;} - nsAutoString1& operator=(char aChar) {nsString1::operator=(aChar); return *this;} - nsAutoString1& operator=(const PRUnichar* aBuffer) {nsString1::operator=(aBuffer); return *this;} - nsAutoString1& operator=(PRUnichar aChar) {nsString1::operator=(aChar); return *this;} - - virtual void SizeOf(nsISizeOfHandler* aHandler) const; - - static void SelfTest(); - -protected: - virtual void EnsureCapacityFor(PRInt32 aNewLength); - - chartype mBuf[32]; + nsSubsumeCStr(nsStr& aString); + nsSubsumeCStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); + nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; -ostream& operator<<(ostream& os,nsAutoString1& aString); + #endif + diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index 917657481fb..0cf0074773c 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -17,26 +17,23 @@ * Reserved. */ -#include #include #include -#include -#include "nsString2.h" +#include "nsString.h" #include "nsDebug.h" -#include "nsCRT.h" -#include "nsIAtom.h" #include "nsDeque.h" + #ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" #endif + static const char* kNullPointerError = "Error: unexpected null ptr"; static const char* kWhitespace="\b\t\r\n "; - #define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { @@ -92,17 +89,11 @@ public: } }; -/*********************************************************************** - IMPLEMENTATION NOTES: - Man I hate writing string classes. - You'd think after about a qintrillion lines of code have been written, - that no poor soul would ever have to do this again. Sigh. - ***********************************************************************/ - -void Subsume(nsStr& aDest,nsStr& aSource){ +static void Subsume(nsStr& aDest,nsStr& aSource){ if(aSource.mStr && aSource.mLength) { if(aSource.mOwnsBuffer){ + nsStr::Destroy(aDest,0); aDest.mStr=aSource.mStr; aDest.mLength=aSource.mLength; aDest.mCharSize=aSource.mCharSize; @@ -118,17 +109,6 @@ void Subsume(nsStr& aDest,nsStr& aSource){ else nsStr::Truncate(aDest,0,0); } -//#define RICKG_DEBUG -#undef RICKG_DEBUG -#ifdef RICKG_DEBUG -/******************************************************** - This class's only purpose in life is to test nsString2. - ********************************************************/ -class CStringTester { -public: - CStringTester(); -}; -#endif /** * Default constructor. Note that we actually allocate a small buffer @@ -136,10 +116,7 @@ public: * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { -#ifdef RICKG_DEBUG - static CStringTester gStringTester; -#endif +nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); } @@ -148,9 +125,10 @@ nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAge * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString2::nsString2(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aCString); + } /** @@ -158,7 +136,7 @@ nsString2::nsString2(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* an * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString2::nsString2(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aString); } @@ -166,9 +144,9 @@ nsString2::nsString2(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent /** * This is our copy constructor * @update gess 1/4/99 - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } @@ -176,9 +154,9 @@ nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* an /** * This is our copy constructor * @update gess 1/4/99 - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { +nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { nsStr::Initialize(*this,aString.mCharSize); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } @@ -188,7 +166,7 @@ nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { * @update gess 1/4/99 * @param reference to a subsumeString */ -nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { +nsString::nsString(nsSubsumeStr& aSubsumeStr) :mAgent(0) { Subsume(*this,aSubsumeStr); } @@ -196,13 +174,15 @@ nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { * Destructor * Make sure we call nsStr::Destroy. */ -nsString2::~nsString2() { +nsString::~nsString() { nsStr::Destroy(*this,mAgent); } -void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { +void nsString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); aHandler->Add(mCapacity << mCharSize); +#endif } /** @@ -212,7 +192,7 @@ void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { * @param anIndex -- new length of string * @return nada */ -void nsString2::Truncate(PRInt32 anIndex) { +void nsString::Truncate(PRInt32 anIndex) { nsStr::Truncate(*this,anIndex,mAgent); } @@ -223,7 +203,7 @@ void nsString2::Truncate(PRInt32 anIndex) { * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString2::IsOrdered(void) const { +PRBool nsString::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { PRUint32 theIndex; @@ -248,11 +228,10 @@ PRBool nsString2::IsOrdered(void) const { * @param aLength -- contains new length for mStr * @return */ -void nsString2::SetCapacity(PRUint32 aLength) { - if(aLength>mLength) { +void nsString::SetCapacity(PRUint32 aLength) { + if(aLength>mCapacity) { GrowCapacity(*this,aLength,mAgent); } - mLength=aLength; AddNullTerminator(*this); } @@ -263,50 +242,54 @@ void nsString2::SetCapacity(PRUint32 aLength) { //static char gChar=0; -/** +/** * * @update gess1/4/99 * @return ptr to internal buffer (if 1-byte), otherwise NULL */ -const char* nsString2::GetBuffer(void) const { +const char* nsString::GetBuffer(void) const { const char* result=(eOneByte==mCharSize) ? mStr : 0; return result; } + /** - * + * This method returns the internal unicode buffer. + * Now that we've factored the string class, this should never + * be able to return a 1 byte string. + * * @update gess1/4/99 - * @return ptr to internal buffer (if 2-byte), otherwise NULL + * @return ptr to internal (2-byte) buffer; */ -const PRUnichar* nsString2::GetUnicode(void) const { - const PRUnichar* result=(eTwoByte==mCharSize) ? mUStr : 0; +const PRUnichar* nsString::GetUnicode(void) const { + const PRUnichar* result=(eOneByte==mCharSize) ? 0 : mUStr; return result; } /** * Get nth character. */ -PRUnichar nsString2::operator[](PRUint32 anIndex) const { +PRUnichar nsString::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::CharAt(PRUint32 anIndex) const { +PRUnichar nsString::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::First(void) const{ +PRUnichar nsString::First(void) const{ return GetCharAt(*this,0); } -PRUnichar nsString2::Last(void) const{ +PRUnichar nsString::Last(void) const{ return GetCharAt(*this,mLength-1); } -PRBool nsString2::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ +PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex PRInt32(sizeof(buf)-1)) { *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; @@ -655,7 +701,7 @@ float nsString2::ToFloat(PRInt32* aErrorCode) const { * @param aRadix tells us what base to expect the string in. * @return int rep of string value */ -PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { +PRInt32 _ToInteger(nsString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { //copy chars to local buffer -- step down from 2 bytes to 1 if necessary... PRInt32 result=0; @@ -698,7 +744,6 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { result+=theDigit*theMult; theMult*=aRadix; } - return result; } @@ -713,11 +758,11 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * @param aRadix (an out parm) tells the caller what base we think the string is in. * @return non-zero error code if this string is non-numeric */ -PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { +PRInt32 GetNumericSubstring(nsString& aString,PRUint32& aRadix) { aString.ToUpperCase(); - PRInt32 decPt=aString.FindChar(aString,'.',PR_TRUE,0); + PRInt32 decPt=nsStr::FindChar(aString,'.',PR_TRUE,0); char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1; aRadix=kRadixUnknown; //assume for starters... @@ -762,7 +807,6 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { if(cp>aString.mStr) aString.Cut(0,cp-aString.mStr); PRInt32 result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; - return result; } @@ -772,10 +816,10 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { * @update gess 10/01/98 * @return 10,16,or 0 (meaning I don't know) */ -PRUint32 nsString2::DetermineRadix(void) { +PRUint32 nsString::DetermineRadix(void) { PRUint32 result=kRadixUnknown; if(0(const nsString2& S) const {return PRBool(Compare(S)>0);} -PRBool nsString2::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} -PRBool nsString2::operator>(const char* s) const {return PRBool(Compare(s)>0);} -PRBool nsString2::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} +PRBool nsString::operator>(const nsString& S) const {return PRBool(Compare(S)>0);} +PRBool nsString::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} +PRBool nsString::operator>(const char* s) const {return PRBool(Compare(s)>0);} +PRBool nsString::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} -PRBool nsString2::operator<=(const nsString2& S) const {return PRBool(Compare(S)<=0);} -PRBool nsString2::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} -PRBool nsString2::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} -PRBool nsString2::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} +PRBool nsString::operator<=(const nsString& S) const {return PRBool(Compare(S)<=0);} +PRBool nsString::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} +PRBool nsString::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} +PRBool nsString::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} -PRBool nsString2::operator>=(const nsString2& S) const {return PRBool(Compare(S)>=0);} -PRBool nsString2::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} -PRBool nsString2::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} -PRBool nsString2::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} +PRBool nsString::operator>=(const nsString& S) const {return PRBool(Compare(S)>=0);} +PRBool nsString::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} +PRBool nsString::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} +PRBool nsString::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} -PRBool nsString2::EqualsIgnoreCase(const nsString2& aString) const { +PRBool nsString::EqualsIgnoreCase(const nsString& aString) const { return Equals(aString,PR_TRUE); } -PRBool nsString2::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { - return Equals(aString,aLength,PR_TRUE); +PRBool nsString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); } -PRBool nsString2::EqualsIgnoreCase(const nsIAtom *aAtom) const { +PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const { return Equals(aAtom,PR_TRUE); } -PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { return Equals(s1,s2,PR_TRUE); } @@ -1562,66 +1659,61 @@ PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) con * Compare this to given string; note that we compare full strings here. * * @update gess 01/04/99 - * @param aString is the other nsString2 to be compared to + * @param aString is the other nsString to be compared to + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString2::Equals(const nsString2& aString,PRBool aIgnoreCase) const { - PRInt32 result=nsStr::Compare(*this,aString,MinInt(mLength,aString.mLength),aIgnoreCase); - return PRBool(0==result); +PRBool nsString::Equals(const nsString& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); + return result; + } /** * Compare this to given string; note that we compare full strings here. * * @update gess 01/04/99 - * @param aString is the other nsString2 to be compared to + * @param aString is the other nsString to be compared to + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString2::Equals(const nsStr& aString,PRBool aIgnoreCase) const { - PRInt32 result=nsStr::Compare(*this,aString,MinInt(mLength,aString.mLength),aIgnoreCase); - return PRBool(0==result); -} - -PRBool nsString2::Equals(const char* aString,PRBool aIgnoreCase) const { - if(aString) { - return Equals(aString,nsCRT::strlen(aString),aIgnoreCase); - } - return 0; -} - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 01/04/99 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString2::Equals(const char* aCString,PRUint32 aCount,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=aCString,kNullPointerError); - PRBool result=PR_FALSE; - if(aCString) { - PRInt32 theAnswer=Compare(aCString,aIgnoreCase,aCount); - result=PRBool(0==theAnswer); - } +PRBool nsString::Equals(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); return result; } -PRBool nsString2::Equals(const PRUnichar* aString,PRBool aIgnoreCase) const { - NS_ASSERTION(0!=aString,kNullPointerError); - PRBool result=PR_FALSE; - if(aString) { - result=Equals(aString,nsCRT::strlen(aString),aIgnoreCase); - } +/** + * Compare this to given c-string; note that we compare full strings here. + * + * @param aString is the CString to be compared + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length + * @return TRUE if equal + */ +PRBool nsString::Equals(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); return result; } +/** + * Compare this to given unicode string; note that we compare full strings here. + * + * @param aString is the U-String to be compared + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length + * @return TRUE if equal + */ +PRBool nsString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); + return result; +} /** - * Compare this to given string; note that we compare full strings here. + * Compare this to given atom; note that we compare full strings here. * The optional length argument just lets us know how long the given string is. * If you provide a length, it is compared to length of this string as an * optimization. @@ -1631,45 +1723,29 @@ PRBool nsString2::Equals(const PRUnichar* aString,PRBool aIgnoreCase) const { * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString2::Equals(const PRUnichar* aString,PRUint32 aCount,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=aString,kNullPointerError); - PRBool result=PR_FALSE; - if(aString){ - PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); - result=PRBool(0==theAnswer); - } - return result; -} - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 01/04/99 - * @param aString -- unistring to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString2::Equals(const nsIAtom* aAtom,PRBool aIgnoreCase) const{ +PRBool nsString::Equals(const nsIAtom* aAtom,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=aAtom,kNullPointerError); PRBool result=PR_FALSE; if(aAtom){ - PRInt32 cmp=nsCRT::strcasecmp(mUStr,aAtom->GetUnicode()); + PRInt32 cmp=0; + if (aIgnoreCase) + cmp=nsCRT::strcasecmp(mUStr,aAtom->GetUnicode()); + else + cmp=nsCRT::strcmp(mUStr,aAtom->GetUnicode()); result=PRBool(0==cmp); } - return result; + + return result; } /** * Compare given strings * @update gess 7/27/98 - * @param s1 -- first string to be compared - * @param s2 -- second string to be compared + * @param s1 -- first unichar string to be compared + * @param s2 -- second unichar string to be compared * @return TRUE if equal */ -PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase) const { +PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1677,7 +1753,8 @@ PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnore PRInt32 cmp=(aIgnoreCase) ? nsCRT::strcasecmp(s1,s2) : nsCRT::strcmp(s1,s2); result=PRBool(0==cmp); } - return result; + + return result; } /** @@ -1687,7 +1764,7 @@ PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnore * @param aChar is character to be tested * @return TRUE if in alpha range */ -PRBool nsString2::IsAlpha(PRUnichar aChar) { +PRBool nsString::IsAlpha(PRUnichar aChar) { // XXX i18n if (((aChar >= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { return PR_TRUE; @@ -1702,7 +1779,7 @@ PRBool nsString2::IsAlpha(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if is valid space char */ -PRBool nsString2::IsSpace(PRUnichar aChar) { +PRBool nsString::IsSpace(PRUnichar aChar) { // XXX i18n if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { return PR_TRUE; @@ -1717,12 +1794,12 @@ PRBool nsString2::IsSpace(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if char is a valid digit */ -PRBool nsString2::IsDigit(PRUnichar aChar) { +PRBool nsString::IsDigit(PRUnichar aChar) { // XXX i18n return PRBool((aChar >= '0') && (aChar <= '9')); } - +#ifndef RICKG_TESTBED /************************************************************** Define the string deallocator class... **************************************************************/ @@ -1730,7 +1807,7 @@ class nsStringDeallocator: public nsDequeFunctor{ public: virtual void* operator()(void* anObject) { static nsMemoryAgent theAgent; - nsString2* aString= (nsString2*)anObject; + nsString* aString= (nsString*)anObject; if(aString){ aString->mAgent=&theAgent; delete aString; @@ -1740,10 +1817,9 @@ public: }; /**************************************************************************** - * This class, appropriately enough, creates and recycles nsString2 objects.. + * This class, appropriately enough, creates and recycles nsString objects.. ****************************************************************************/ - class nsStringRecycler { public: nsStringRecycler() : mDeque(0) { @@ -1754,22 +1830,19 @@ public: mDeque.ForEach(theDeallocator); //now delete the strings } - void Recycle(nsString2* aString) { + void Recycle(nsString* aString) { mDeque.Push(aString); } - nsString2* CreateString(eCharSize aCharSize){ - nsString2* result=(nsString2*)mDeque.Pop(); + nsString* CreateString(eCharSize aCharSize){ + nsString* result=(nsString*)mDeque.Pop(); if(!result) - result=new nsString2(aCharSize); + result=new nsString(aCharSize); return result; } nsDeque mDeque; }; - -void Subsume(nsStr& aDest,nsStr& aSource); -nsStringRecycler& GetRecycler(void); - +static nsStringRecycler& GetRecycler(void); /** * @@ -1781,6 +1854,8 @@ nsStringRecycler& GetRecycler(void){ static nsStringRecycler gRecycler; return gRecycler; } +#endif + /** * Call this mehod when you're done @@ -1788,8 +1863,11 @@ nsStringRecycler& GetRecycler(void){ * @param * @return */ -nsString2* nsString2::CreateString(eCharSize aCharSize){ - nsString2* result=GetRecycler().CreateString(aCharSize); +nsString* nsString::CreateString(eCharSize aCharSize){ + nsString* result=0; +#ifndef RICKG_TESTBED + GetRecycler().CreateString(aCharSize); +#endif return result; } @@ -1799,8 +1877,12 @@ nsString2* nsString2::CreateString(eCharSize aCharSize){ * @param * @return */ -void nsString2::Recycle(nsString2* aString){ +void nsString::Recycle(nsString* aString){ +#ifndef RICKG_TESTBED GetRecycler().Recycle(aString); +#else + delete aString; +#endif } @@ -1810,7 +1892,7 @@ void nsString2::Recycle(nsString2* aString){ * @param * @return */ -void nsString2::DebugDump(ostream& aStream) const { +void nsString::DebugDump(ostream& aStream) const { for(PRUint32 i=0;i>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); -} -/** - * construct from external buffer and given string - * @param anExtBuffer describes an external buffer - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2(aStr.mCharSize) { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,aStr.mCharSize,PR_FALSE); - mAgent=0; - AddNullTerminator(*this); - Assign(aCString); } /** * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const char* aCString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); - Assign(aCString); -} + Append(aCString,aLength); + -/** - * Copy construct from ascii c-string - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(char* aCString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - mAgent=0; - if(assumeOwnership && aCString) { - aCapacity = (-1==aCapacity) ? strlen(aCString) : aCapacity-1; - nsStr::Initialize(*this,aCString,aCapacity,aCapacity,eOneByte,PR_TRUE); - AddNullTerminator(*this); - } - else { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aCString); - } } /** * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const PRUnichar* aString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); + Append(aString,aLength); + } /** * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString2::nsAutoString2(PRUnichar* aString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { +nsAutoString::nsAutoString(CSharedStrBuffer& aBuffer) : nsString(eTwoByte) { mAgent=0; - if(assumeOwnership && aString) { - aCapacity = (-1==aCapacity) ? nsCRT::strlen(aString) : aCapacity-1; - nsStr::Initialize(*this,(char*)aString,aCapacity,aCapacity,eTwoByte,PR_TRUE); - AddNullTerminator(*this); + if(!aBuffer.mBuffer) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); } else { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aString); + nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } + AddNullTerminator(*this); } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(const nsStr& aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const nsStr& aString,eCharSize aCharSize) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); + Append(aString); + } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const nsAutoString& aString,eCharSize aCharSize) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsString2& aString,eCharSize aCharSize) : nsString2(aCharSize){ - mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aString); + Append(aString); + } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ +nsAutoString::nsAutoString(PRUnichar aChar,eCharSize aCharSize) : nsString(aCharSize){ mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aChar); + Append(aChar); + } /** @@ -2012,12 +2057,12 @@ nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aC * @param reference to a subsumeString */ #ifdef AIX -nsAutoString2::nsAutoString2(const nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { +nsAutoString::nsAutoString(const nsSubsumeStr& aSubsumeStr) :nsString(aSubsumeStr.mCharSize) { mAgent=0; nsSubsumeStr temp(aSubsumeStr); // a temp is needed for the AIX compiler Subsume(*this,temp); #else -nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { +nsAutoString::nsAutoString( nsSubsumeStr& aSubsumeStr) :nsString(aSubsumeStr.mCharSize) { mAgent=0; Subsume(*this,aSubsumeStr); #endif // AIX @@ -2027,384 +2072,29 @@ nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr. * deconstruct the autstring * @param */ -nsAutoString2::~nsAutoString2(){ -// bool b=true; -// mStr=0; +nsAutoString::~nsAutoString(){ } -void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { +void nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); aHandler->Add(mCapacity << mCharSize); +#endif } -nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2(aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString(aString.mCharSize) { Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2(aString.mCharSize) { - Subsume(*this,aString); -} - -nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eTwoByte) { +nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString(eTwoByte) { mUStr=aString; mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } -nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { +nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString(eOneByte) { mStr=aString; mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } -/*********************************************************************** - IMPLEMENTATION of nsCAutoString, which is a vanilla string class that - only ever stores 1 byte character strings. Typically you'll use this - class to hold a pointer to a char*, which comes from an nsString. - ***********************************************************************/ -nsCAutoString::nsCAutoString(const nsString2& aString) : nsAutoString(aString,eOneByte){ -} - -nsCAutoString::operator const char*() const { - return (const char*)mStr; -} - - - -#ifdef RICKG_DEBUG -/*********************************************************************** - IMPLEMENTATION of CStringTester... - ***********************************************************************/ -CStringTester::CStringTester() { - - static const char* kConstructorError = kConstructorError; - static const char* kComparisonError = "Comparision error!"; - static const char* kEqualsError = "Equals error!"; - - eCharSize theSize=eTwoByte; - - //begin by testing the constructors... - { - { - nsString2 theString0("foo",theSize); //watch it construct and destruct - } - - //Let's test our autoCStrings... - { - nsString2 theString("hello"); - nsCAutoString theCStr(theString); - } - - - { - //this test makes sure that autostrings who assume ownership of a buffer, - //don't also try to copy that buffer onto itself... (was a bug) - char* theStr="hello rick"; - nsAutoString2(theStr,5,eOneByte,PR_FALSE); - - } - - { - nsString2 theString("hello"); - nsString2 temp1=theString+" there!"; - nsString2 temp2=theString+'!'; - nsSubsumeStr temp3=theString+'?'; - nsString2 temp4(temp3); - temp1=temp3; - nsAutoString2 temp5("hello"); - nsSubsumeStr temp6(temp5); - } - - nsString2 theString1(theSize); - nsString2 theString("hello",theSize); - nsString2 theString3(theString,theSize); - nsStr& si=theString; - nsString2 theString4(si,theSize); - PRUint32 theLen=theString3.Length(); - - //and hey, why not do a few lexo-morphic tests... - theString.ToUpperCase(); - theString.ToLowerCase(); - - //while we're here, let's try truncation and setting the length. - theString3.Truncate(3); - theLen=theString3.Length(); - - - theString.SetCapacity(3); - const char* theBuffer=theString.GetBuffer(); - const char* theOther=theBuffer; - - { - nsString2 temp(" hello there rick ",theSize); - temp.CompressWhitespace(); - } - - nsString2 theString5(" hello there rick ",theSize); - theString5.StripChars("reo"); - theString5.Trim(" ",PR_TRUE,PR_FALSE); - theString5.StripWhitespace(); - - nsString2* theString6=theString5.ToNewString(); - nsString2::Recycle(theString6); - - char* str=theString5.ToNewCString(); - delete [] str; - - char buffer[100]; - theString5.ToCString(buffer,sizeof(buffer)-1); - theOther=theString5.GetBuffer(); - } - - //try a few numeric conversion routines... - { - nsString2 str1("10000",theSize); - nsString2 str2("hello"); - nsString2 str3; - PRInt32 err; - PRInt32 theInt=str1.ToInteger(&err); - theInt=str2.ToInteger(&err); - theInt=str3.ToInteger(&err); - - str1="100.100"; - float theFloat=str1.ToFloat(&err); - } - //Now test the character accessor methods... - { - nsString2 theString("hello",theSize); - PRUint32 len=theString.Length(); - PRUnichar ch; - for(PRUint32 i=0;itemp8.Compare(bbbb),kComparisonError); - NS_ASSERTION(0>temp8.Compare(temp9),kComparisonError); - NS_ASSERTION(0=temp8)),kComparisonError); - - NS_ASSERTION(((temp9>temp8) && (temp8<=temp9)),kComparisonError); - NS_ASSERTION(temp9>aaaa,kComparisonError); - - NS_ASSERTION(temp8<=temp8,kComparisonError); - NS_ASSERTION(temp8<=temp9,kComparisonError); - NS_ASSERTION(temp8<=bbbb,kComparisonError); - - NS_ASSERTION(((temp9>=temp8) && (temp8=temp8,kComparisonError); - NS_ASSERTION(temp9>=aaaa,kComparisonError); - - NS_ASSERTION(temp8.Equals(temp8),kEqualsError); - NS_ASSERTION(temp8.Equals(aaaa),kEqualsError); - - nsString2 temp10(temp8); - temp10.ToUpperCase(); - NS_ASSERTION(temp8.Equals(temp10,PR_TRUE),kEqualsError); - NS_ASSERTION(temp8.Equals("AAAA",PR_TRUE),kEqualsError); - - - //********************************************** - //Now let's test a few string MANIPULATORS... - //********************************************** - - { - nsAutoString2 ab("ab",theSize); - nsString2 abcde("cde",theSize); - nsString2 cut("abcdef",theSize); - cut.Cut(7,10); //this is out of bounds, so ignore... - cut.DebugDump(cout); - cut.Cut(5,2); //cut last chars - cut.DebugDump(cout); - cut.Cut(1,1); //cut first char - cut.DebugDump(cout); - cut.Cut(2,1); //cut one from the middle - cut.DebugDump(cout); - cut="Hello there Rick"; - cut.DebugDump(cout); - - cut="'\"abcdef\"'"; - cut.Trim("'"); - cut.DebugDump(cout); - cut.Trim("\"",PR_TRUE,PR_FALSE); - cut.DebugDump(cout); - cut.Trim("\"",PR_FALSE,PR_TRUE); - cut.DebugDump(cout); - - cut="abc def\n\n ghi"; - cut.StripWhitespace(); - cut.DebugDump(cout); - cut="abc def\n\n ghi"; - cut.CompressWhitespace(); - cut.DebugDump(cout); - } - - - //********************************************** - //Now let's test the SEARCHING operations... - //********************************************** - - { - nsString2 find1("abcdefghijk",theSize); - nsString2 find2("ijk",theSize); - - PRInt32 pos=find1.Find("efg"); - NS_ASSERTION(pos==4,"Error: Find routine"); - - pos=find1.Find("EFG",PR_TRUE); - NS_ASSERTION(pos==4,"Error: Find routine"); - - pos=find1.Find('d'); - NS_ASSERTION(pos==3,"Error: Find char routine"); - - pos=find1.Find(find2); - NS_ASSERTION(pos==8,"Error: Find char routine"); - - pos=find1.FindCharInSet("12k"); - NS_ASSERTION(pos==10,"Error: FindFirstInChar routine"); - - pos=find1.RFindCharInSet("12k"); - NS_ASSERTION(pos==10,"Error: FindLastInChar routine"); - - pos=find1.RFind("efg"); - NS_ASSERTION(pos==4,"Error: RFind routine"); - - pos=find1.RFind("xxx"); - NS_ASSERTION(pos==-1,"Error: RFind routine"); //this should fail - - pos=find1.RFind(""); - NS_ASSERTION(pos==-1,"Error: RFind routine"); //this too should fail. - - pos=find1.RFind('a'); - NS_ASSERTION(pos==0,"Error: RFind routine"); - - pos=find1.BinarySearch('a'); - pos=find1.BinarySearch('b'); - pos=find1.BinarySearch('c'); - pos=find1.BinarySearch('d'); - pos=find1.BinarySearch('e'); - pos=find1.BinarySearch('f'); - pos=find1.BinarySearch('g'); - pos=find1.BinarySearch('h'); - pos=find1.BinarySearch('i'); - pos=find1.BinarySearch('z'); - } - - { - //now let's try a memory allocation test... - nsString2 temp; - for(int i=0;i<100;i++){ - temp+="hello "; - } - int x=5; - } -}; - -#endif //rickg_debug diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index 79d0658aae7..29490974262 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -30,19 +30,17 @@ ***********************************************************************/ -#ifndef _nsString2 -#define _nsString2 +#ifndef _nsString_ +#define _nsString_ #include "prtypes.h" #include "nscore.h" #include #include #include "nsCRT.h" - -#include "nsStr.h" -#include -#include +#include "nsString.h" #include "nsIAtom.h" +#include "nsStr.h" class nsISizeOfHandler; @@ -50,14 +48,9 @@ class nsISizeOfHandler; #define nsString2 nsString #define nsAutoString2 nsAutoString -#define kRadix10 (10) -#define kRadix16 (16) -#define kAutoDetect (100) -#define kRadixUnknown (kAutoDetect+1) - class NS_COM nsSubsumeStr; -class NS_COM nsString2 : public nsStr { +class NS_COM nsString : public nsStr { public: @@ -67,44 +60,44 @@ class NS_COM nsString2 : public nsStr { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString2(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This constructor accepts an isolatin string * @param aCString is a ptr to a 1-byte cstr */ -nsString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This constructor accepts a unichar string * @param aCString is a ptr to a 2-byte cstr */ -nsString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This is a copy constructor that accepts an nsStr - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This is our copy constructor - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2(const nsString2& aString); +nsString(const nsString& aString); /** * This constructor takes a subsumestr * @param reference to subsumestr */ -nsString2(nsSubsumeStr& aSubsumeStr); +nsString(nsSubsumeStr& aSubsumeStr); /** * Destructor * */ -virtual ~nsString2(); +virtual ~nsString(); /** * Retrieve the length of this string @@ -120,13 +113,13 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const; /** - * Call this method if you want to force a different string capacity + * Call this method if you want to force a different string length * @update gess7/30/98 * @param aLength -- contains new length for mStr * @return */ void SetLength(PRUint32 aLength) { - SetCapacity(aLength); + Truncate(aLength); } /** @@ -173,7 +166,7 @@ PRBool IsEmpty(void) const { } /********************************************************************** - Accessor methods... + Getters/Setters... *********************************************************************/ const char* GetBuffer(void) const; @@ -188,11 +181,14 @@ PRUnichar CharAt(PRUint32 anIndex) const; PRUnichar First(void) const; PRUnichar Last(void) const; + /** + * Set nth character. + */ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); /********************************************************************** - String creation methods... + String concatenation methods... *********************************************************************/ /** @@ -201,13 +197,7 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); * @return new string */ nsSubsumeStr operator+(const nsStr& aString); - -/** - * Create a new string by appending given string to this - * @param aString -- 2nd string to be appended - * @return new string - */ -nsSubsumeStr operator+(const nsString2& aString); +nsSubsumeStr operator+(const nsString& aString); /** * create a new string by adding this to the given buffer. @@ -260,7 +250,7 @@ void ToLowerCase(); * @update gess 7/27/98 * @param aOut is a string to contain result */ -void ToLowerCase(nsString2& aString) const; +void ToLowerCase(nsString& aString) const; /** * Converts chars in this to uppercase @@ -274,7 +264,7 @@ void ToUpperCase(); * @update gess 7/27/98 * @param aOut is a string to contain result */ -void ToUpperCase(nsString2& aString) const; +void ToUpperCase(nsString& aString) const; /** @@ -284,21 +274,25 @@ void ToUpperCase(nsString2& aString) const; * @param aSet -- characters to be cut from this * @return *this */ -nsString2& StripChars(const char* aSet); +nsString& StripChars(const char* aSet); +nsString& StripChar(PRUnichar aChar); /** * This method strips whitespace throughout the string * * @return this */ -nsString2& StripWhitespace(); +nsString& StripWhitespace(); /** * swaps occurence of 1 string for another * * @return this */ -nsString2& ReplaceChar(PRUnichar aSourceChar,PRUnichar aDestChar); +nsString& ReplaceChar(PRUnichar anOldChar,PRUnichar aNewChar); +nsString& ReplaceChar(const char* aSet,PRUnichar aNewChar); + +PRInt32 CountChar(PRUnichar aChar); /** * This method trims characters found in aTrimSet from @@ -308,7 +302,7 @@ nsString2& ReplaceChar(PRUnichar aSourceChar,PRUnichar aDestChar); * both ends * @return this */ -nsString2& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /** * This method strips whitespace from string. @@ -319,7 +313,7 @@ nsString2& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aElimin * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString2& CompressSet(const char* aSet, char aChar, PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /** * This method strips whitespace from string. @@ -330,18 +324,18 @@ nsString2& CompressSet(const char* aSet, char aChar, PRBool aEliminateLeading=PR * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString2& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /********************************************************************** string conversion methods... *********************************************************************/ /** - * This method constructs a new nsString2 on the stack that is a copy + * This method constructs a new nsString on the stack that is a copy * of this string. * */ -nsString2* ToNewString() const; +nsString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string @@ -392,9 +386,9 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const; * Functionally equivalent to assign or operator= * */ -nsString2& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} -nsString2& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} -nsString2& SetString(const nsString2& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const nsString& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} /** * assign given string to this string @@ -403,28 +397,27 @@ nsString2& SetString(const nsString2& aString,PRInt32 aLength=-1) {return Assign if you want me to determine its length * @return this */ -nsString2& Assign(const nsString2& aString,PRInt32 aCount=-1); -nsString2& Assign(const nsStr& aString,PRInt32 aCount=-1); -nsString2& Assign(const char* aString,PRInt32 aCount=-1); -nsString2& Assign(const PRUnichar* aString,PRInt32 aCount=-1); -nsString2& Assign(char aChar); -nsString2& Assign(PRUnichar aChar); +nsString& Assign(const nsStr& aString,PRInt32 aCount=-1); +nsString& Assign(const char* aString,PRInt32 aCount=-1); +nsString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); +nsString& Assign(char aChar); +nsString& Assign(PRUnichar aChar); /** * here come a bunch of assignment operators... * @param aString: string to be added to this * @return this */ -nsString2& operator=(const nsString2& aString) {return Assign(aString);} -nsString2& operator=(const nsStr& aString) {return Assign(aString);} -nsString2& operator=(char aChar) {return Assign(aChar);} -nsString2& operator=(PRUnichar aChar) {return Assign(aChar);} -nsString2& operator=(const char* aCString) {return Assign(aCString);} -nsString2& operator=(const PRUnichar* aString) {return Assign(aString);} +nsString& operator=(const nsString& aString) {return Assign(aString);} +nsString& operator=(const nsStr& aString) {return Assign(aString);} +nsString& operator=(char aChar) {return Assign(aChar);} +nsString& operator=(PRUnichar aChar) {return Assign(aChar);} +nsString& operator=(const char* aCString) {return Assign(aCString);} +nsString& operator=(const PRUnichar* aString) {return Assign(aString);} #ifdef AIX -nsString2& operator=(const nsSubsumeStr& aSubsumeString); // AIX requires a const here +nsString& operator=(const nsSubsumeStr& aSubsumeString); // AIX requires a const here #else -nsString2& operator=(nsSubsumeStr& aSubsumeString); +nsString& operator=(nsSubsumeStr& aSubsumeString); #endif /** @@ -432,12 +425,12 @@ nsString2& operator=(nsSubsumeStr& aSubsumeString); * @param aString : string to be appended to this * @return this */ -nsString2& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} -nsString2& operator+=(const nsString2& aString){return Append(aString,aString.mLength);} -nsString2& operator+=(const char* aCString) {return Append(aCString);} -//nsString2& operator+=(char aChar){return Append(aChar);} -nsString2& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} -nsString2& operator+=(PRUnichar aChar){return Append(aChar);} +nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} +nsString& operator+=(const nsString& aString){return Append(aString,aString.mLength);} +nsString& operator+=(const char* aCString) {return Append(aCString);} +//nsString& operator+=(char aChar){return Append(aChar);} +nsString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} +nsString& operator+=(PRUnichar aChar){return Append(aChar);} /* * Appends n characters from given string to this, @@ -446,8 +439,8 @@ nsString2& operator+=(PRUnichar aChar){return Append(aChar);} * @param aString is the source to be appended to this * @return number of chars copied */ -nsString2& Append(const nsStr& aString) {return Append(aString,aString.mLength);} -nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLength);} +nsString& Append(const nsStr& aString) {return Append(aString,aString.mLength);} +nsString& Append(const nsString& aString) {return Append(aString,aString.mLength);} /* @@ -457,14 +450,14 @@ nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLeng * @param aCount -- number of chars to copy * @return number of chars copied */ -nsString2& Append(const nsStr& aString,PRInt32 aCount); -nsString2& Append(const nsString2& aString,PRInt32 aCount); -nsString2& Append(const char* aString,PRInt32 aCount=-1); -nsString2& Append(const PRUnichar* aString,PRInt32 aCount=-1); -nsString2& Append(char aChar); -nsString2& Append(PRUnichar aChar); -nsString2& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 -nsString2& Append(float aFloat); +nsString& Append(const nsStr& aString,PRInt32 aCount); +nsString& Append(const nsString& aString,PRInt32 aCount); +nsString& Append(const char* aString,PRInt32 aCount=-1); +nsString& Append(const PRUnichar* aString,PRInt32 aCount=-1); +nsString& Append(char aChar); +nsString& Append(PRUnichar aChar); +nsString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 +nsString& Append(float aFloat); /* * Copies n characters from this string to given string, @@ -475,7 +468,7 @@ nsString2& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRUint32 Left(nsString2& aCopy,PRInt32 aCount) const; +PRUint32 Left(nsString& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -487,7 +480,7 @@ PRUint32 Left(nsString2& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRUint32 Mid(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount) const; +PRUint32 Mid(nsString& aCopy,PRUint32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -498,7 +491,7 @@ PRUint32 Mid(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRUint32 Right(nsString2& aCopy,PRInt32 aCount) const; +PRUint32 Right(nsString& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -509,7 +502,7 @@ PRUint32 Right(nsString2& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -519,8 +512,8 @@ nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -nsString2& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); -nsString2& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a single char into this string at @@ -530,8 +523,8 @@ nsString2& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -//nsString2& Insert(char aChar,PRUint32 anOffset); -nsString2& Insert(PRUnichar aChar,PRUint32 anOffset); +//nsString& Insert(char aChar,PRUint32 anOffset); +nsString& Insert(PRUnichar aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -541,7 +534,7 @@ nsString2& Insert(PRUnichar aChar,PRUint32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString2& Cut(PRUint32 anOffset,PRInt32 aCount); +nsString& Cut(PRUint32 anOffset,PRInt32 aCount); /********************************************************************** @@ -564,11 +557,22 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * @param aString is substring to be sought in this * @return offset in string, or -1 (kNotFound) */ -PRInt32 Find(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 Find(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + + +/** + * Search for given char within this string + * + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) + */ PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** * This method searches this string for the first character @@ -577,20 +581,9 @@ PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) cons * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this */ -PRInt32 FindCharInSet(const char* aString,PRUint32 anOffset=0) const; -PRInt32 FindCharInSet(const PRUnichar* aString,PRUint32 anOffset=0) const; -PRInt32 FindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; - -/** - * This method searches this string for the last character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 RFindCharInSet(const char* aString,PRUint32 anOffset=0) const; -PRInt32 RFindCharInSet(const PRUnichar* aString,PRUint32 anOffset=0) const; -PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; +PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; /** @@ -599,11 +592,34 @@ PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; * @param aIgnoreCase tells us whether or not to do caseless compare * @return offset in string, or -1 (kNotFound) */ -PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + + +/** + * Search for given char within this string + * + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) + */ PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + +/** + * This method searches this string for the last character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this + */ +PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + /********************************************************************** Comparison methods... @@ -616,7 +632,7 @@ PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) con * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -virtual PRInt32 Compare(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const nsStr &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; @@ -626,7 +642,7 @@ virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRI * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator==(const nsString2 &aString) const; +PRBool operator==(const nsString &aString) const; PRBool operator==(const nsStr &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; @@ -636,7 +652,7 @@ PRBool operator==(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE */ -PRBool operator!=(const nsString2 &aString) const; +PRBool operator!=(const nsString &aString) const; PRBool operator!=(const nsStr &aString) const; PRBool operator!=(const char* aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -646,7 +662,7 @@ PRBool operator!=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<(const nsString2 &aString) const; +PRBool operator<(const nsString &aString) const; PRBool operator<(const nsStr &aString) const; PRBool operator<(const char* aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -656,7 +672,7 @@ PRBool operator<(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>(const nsString2 &aString) const; +PRBool operator>(const nsString &aString) const; PRBool operator>(const nsStr &S) const; PRBool operator>(const char* aString) const; PRBool operator>(const PRUnichar* aString) const; @@ -666,7 +682,7 @@ PRBool operator>(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<=(const nsString2 &aString) const; +PRBool operator<=(const nsString &aString) const; PRBool operator<=(const nsStr &S) const; PRBool operator<=(const char* aString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -676,7 +692,7 @@ PRBool operator<=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>=(const nsString2 &aString) const; +PRBool operator>=(const nsString &aString) const; PRBool operator>=(const nsStr &S) const; PRBool operator>=(const char* aString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -688,20 +704,18 @@ PRBool operator>=(const PRUnichar* aString) const; * optimization. * * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. + * @param aCount -- number of chars to be compared. * @return TRUE if equal */ -PRBool Equals(const nsString2 &aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const char* aString,PRUint32 aCount,PRBool aIgnoreCase) const; -PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const PRUnichar* aString,PRUint32 aCount,PRBool aIgnoreCase) const; +PRBool Equals(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; PRBool Equals(const nsIAtom* anAtom,PRBool aIgnoreCase) const; PRBool Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase=PR_FALSE) const; -PRBool EqualsIgnoreCase(const nsString2& aString) const; -PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; +PRBool EqualsIgnoreCase(const nsString& aString) const; +PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; @@ -730,8 +744,8 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -static void Recycle(nsString2* aString); -static nsString2* CreateString(eCharSize aCharSize=eTwoByte); +static void Recycle(nsString* aString); +static nsString* CreateString(eCharSize aCharSize=eTwoByte); virtual void DebugDump(ostream& aStream) const; @@ -739,8 +753,8 @@ virtual void DebugDump(ostream& aStream) const; }; -extern NS_COM int fputs(const nsString2& aString, FILE* out); -ostream& operator<<(ostream& aStream,const nsString2& aString); +extern NS_COM int fputs(const nsString& aString, FILE* out); +ostream& operator<<(ostream& aStream,const nsString& aString); /************************************************************** @@ -749,36 +763,30 @@ ostream& operator<<(ostream& aStream,const nsString2& aString); If the buffer needs to grow, it gets reallocated on the heap. **************************************************************/ -class NS_COM nsAutoString2 : public nsString2 { +class NS_COM nsAutoString : public nsString { public: - nsAutoString2(eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(nsStr& anExtBuffer,const char* aCString); + nsAutoString(eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); + nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - - nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(char* aCString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); - nsAutoString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(PRUnichar* aString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); - - nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(CSharedStrBuffer& aBuffer); + nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); #ifdef AIX - nsAutoString2(const nsSubsumeStr& aSubsumeStr); // AIX requires a const + nsAutoString(const nsSubsumeStr& aSubsumeStr); // AIX requires a const #else - nsAutoString2(nsSubsumeStr& aSubsumeStr); + nsAutoString(nsSubsumeStr& aSubsumeStr); #endif // AIX - nsAutoString2(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); - virtual ~nsAutoString2(); + nsAutoString(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); + virtual ~nsAutoString(); - nsAutoString2& operator=(const nsString2& aString) {nsString2::operator=(aString); return *this;} - nsAutoString2& operator=(const nsStr& aString) {nsString2::Assign(aString); return *this;} - nsAutoString2& operator=(const nsAutoString2& aString) {nsString2::operator=(aString); return *this;} - nsAutoString2& operator=(const char* aCString) {nsString2::operator=(aCString); return *this;} - nsAutoString2& operator=(char aChar) {nsString2::operator=(aChar); return *this;} - nsAutoString2& operator=(const PRUnichar* aBuffer) {nsString2::operator=(aBuffer); return *this;} - nsAutoString2& operator=(PRUnichar aChar) {nsString2::operator=(aChar); return *this;} + nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} + nsAutoString& operator=(const nsAutoString& aString) {nsString::operator=(aString); return *this;} + nsAutoString& operator=(const char* aCString) {nsString::operator=(aCString); return *this;} + nsAutoString& operator=(char aChar) {nsString::operator=(aChar); return *this;} + nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::operator=(aBuffer); return *this;} + nsAutoString& operator=(PRUnichar aChar) {nsString::operator=(aChar); return *this;} /** * Retrieve the size of this string @@ -803,24 +811,14 @@ public: You should probably not use this class unless you really know what you're doing. ***************************************************************/ -class NS_COM nsSubsumeStr : public nsString2 { +class NS_COM nsSubsumeStr : public nsString { public: - nsSubsumeStr(nsString2& aString); nsSubsumeStr(nsStr& aString); nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; -/*************************************************************** - - ***************************************************************/ -class NS_COM nsCAutoString: public nsAutoString{ -public: - nsCAutoString(const nsString2& aString); - operator const char*() const; -}; - #endif diff --git a/mozilla/xpcom/ds/bufferRoutines.h b/mozilla/xpcom/ds/bufferRoutines.h index ea4863c2a83..b1079491615 100644 --- a/mozilla/xpcom/ds/bufferRoutines.h +++ b/mozilla/xpcom/ds/bufferRoutines.h @@ -93,7 +93,7 @@ void ShiftCharsRight(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCo char* to = aDest+aLength+aCount; //Copy rightmost chars, up to offset+theDelta... - while(first<=last) { + while(first0) && aSet){ + PRInt32 theIndex=-1; + PRInt32 theMax=aDest.mLength; + PRInt32 theSetLen=nsCRT::strlen(aSet); + + if(aEliminateLeading) { + while(++theIndex<=theMax) { + PRUnichar theChar=GetCharAt(aDest,theIndex); + PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE); + if(kNotFound==thePos) + break; + } + if(00) { + PRUnichar theChar=GetCharAt(aDest,theIndex); //read at end now... + PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE); + if(kNotFound0) && (aTarget.mLength>0) && (anOffset0) && (aTarget.mLength>0)){ - int32 theTargetMax=aTarget.mLength; - while(++index<=theMax) { - int32 theSubIndex=-1; + PRInt32 result=kNotFound; + + if((0=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){ + PRInt32 theTargetMax=aTarget.mLength; + while(index<=theMax) { + PRInt32 theSubIndex=-1; PRBool matches=PR_TRUE; while((++theSubIndex0) && (aTarget.mLength>0)){ + if((0=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){ + + nsStr theCopy; + nsStr::Initialize(theCopy,eOneByte); + nsStr::Assign(theCopy,aTarget,0,aTarget.mLength,0); + if(aIgnoreCase){ + nsStr::ChangeCase(theCopy,PR_FALSE); //force to lowercase } - } //while - nsStr::Destroy(theCopy,0); + + PRInt32 theTargetMax=theCopy.mLength; + while(index>=0) { + PRInt32 theSubIndex=-1; + PRBool matches=PR_FALSE; + if(index+theCopy.mLength<=aDest.mLength) { + matches=PR_TRUE; + while((++theSubIndex=0) { - PRUnichar theChar=GetCharAt(aDest,offset); - thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); - if(kNotFound!=thePos) - return offset; - } //while + PRInt32 index=(0<=anOffset) ? anOffset : aDest.mLength; + PRInt32 thePos; + + //note that the search is inverted here. We're scanning aDest, one char at a time + //but doing the search against the given set. That's why we use 0 as the offset below. + if(0=0) { + PRUnichar theChar=GetCharAt(aDest,index); + thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); + if(kNotFound!=thePos) + return index; + } //while + } return kNotFound; } /** - * - * @update gess11/12/98 - * @param + * Compare source and dest strings, up to an (optional max) number of chars + * @param aDest is the first str to compare + * @param aSource is the second str to compare + * @param aCount -- if (-1), then we use length of longer string; if (0aSource=1 */ PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - int minlen=(aSource.mLength1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + +CSharedStrBuffer::CSharedStrBuffer(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eTwoByte; + mStackBased=aStackBased; + mLength=mCapacity=0; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + + +//---------------------------------------------------------------------------------------- diff --git a/mozilla/xpcom/ds/nsStr.h b/mozilla/xpcom/ds/nsStr.h index 07b753a25b0..8757ce92f85 100644 --- a/mozilla/xpcom/ds/nsStr.h +++ b/mozilla/xpcom/ds/nsStr.h @@ -48,6 +48,10 @@ enum eCharSize {eOneByte=0,eTwoByte=1}; #define kDefaultCharSize eTwoByte +#define kRadix10 (10) +#define kRadix16 (16) +#define kAutoDetect (100) +#define kRadixUnknown (kAutoDetect+1) const PRInt32 kNotFound = -1; @@ -55,8 +59,23 @@ class nsIMemoryAgent; //---------------------------------------------------------------------------------------- +class NS_COM CSharedStrBuffer { +public: + CSharedStrBuffer(char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CSharedStrBuffer(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); -struct nsStr { + char* mBuffer; + eCharSize mCharSize; + PRUint32 mCapacity; + PRInt32 mLength; + PRBool mStackBased; + +}; + +//---------------------------------------------------------------------------------------- + + +struct NS_COM nsStr { /** * This method initializes an nsStr for use @@ -169,16 +188,6 @@ struct nsStr { */ static void ChangeCase(nsStr& aDest,PRBool aToUpper); - /** - * This method removes chars (given in aSet) from the given buffer - * - * @update gess 01/04/99 - * @param aString is the buffer to be manipulated - * @param aDestOffset is starting pos in buffer for manipulation - * @param aCount is the number of chars to compare - * @param aSet tells us which chars to remove from given buffer - */ - static void StripChars(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,const char* aCharSet); /** * This method trims chars (given in aSet) from the edges of given buffer @@ -201,7 +210,7 @@ struct nsStr { * @param aEliminateLeading tells us whether to strip chars from the start of the buffer * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer */ - static void CompressSet(nsStr& aDest,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing); + static void CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing); /** * This method compares the data bewteen two nsStr's @@ -225,13 +234,13 @@ struct nsStr { * @param anOffset tells us where in the dest string to start searching * @return the index of the source (substr) in dest, or -1 (kNotFound) if not found. */ - static PRInt32 FindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 FindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset); + static PRInt32 FindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 FindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset); - static PRInt32 RFindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset); + static PRInt32 RFindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset); PRUint32 mLength; diff --git a/mozilla/xpcom/ds/nsString.cpp b/mozilla/xpcom/ds/nsString.cpp index d0d86da25a5..af2c0971ae8 100644 --- a/mozilla/xpcom/ds/nsString.cpp +++ b/mozilla/xpcom/ds/nsString.cpp @@ -1,3 +1,4 @@ + /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public License @@ -14,38 +15,28 @@ * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. - */ + */ #include #include -#include +#include #include #include "nsString.h" -#include "nsCRT.h" #include "nsDebug.h" -#include "prprf.h" +#include "nsCRT.h" +#include "nsDeque.h" + +#ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" -#include "nsStr.h" - -#include "nsUnicharUtilCIID.h" -#include "nsIServiceManager.h" -#include "nsICaseConversion.h" - -const PRInt32 kGrowthDelta = 8; -PRUnichar gBadChar = 0; -const char* kOutOfBoundsError = "Error: out of bounds"; -const char* kNullPointerError = "Error: unexpected null ptr"; -const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; - -PRUnichar kCommonEmptyBuffer[100]; //shared by all strings; NEVER WRITE HERE!!! - -#ifdef RICKG_DEBUG -PRBool nsString1::mSelfTested = PR_FALSE; #endif +static const char* kNullPointerError = "Error: unexpected null ptr"; +static const char* kWhitespace="\b\t\r\n "; -#define NOT_USED 0xfffd + + +#define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { NOT_USED, @@ -99,67 +90,26 @@ public: } } }; -static CTableConstructor gTableConstructor; -//---- XPCOM code to connect with UnicharUtil -class HandleCaseConversionShutdown2 : public nsIShutdownListener { -public : - NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); - HandleCaseConversionShutdown2(void) { NS_INIT_REFCNT(); } - virtual ~HandleCaseConversionShutdown2(void) {} - NS_DECL_ISUPPORTS -}; -static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); -static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); - -static nsICaseConversion * gCaseConv = NULL; - -static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); -NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown2, kIShutdownListenerIID); - -nsresult -HandleCaseConversionShutdown2::OnShutdown(const nsCID& cid, nsISupports* service) -{ - if (cid.Equals(kUnicharUtilCID)) { - NS_ASSERTION(service == gCaseConv, "wrong service!"); - gCaseConv->Release(); - gCaseConv = NULL; +static void CSubsume(nsStr& aDest,nsStr& aSource){ + if(aSource.mStr && aSource.mLength) { + if(aSource.mOwnsBuffer){ + nsStr::Destroy(aDest,0); + aDest.mStr=aSource.mStr; + aDest.mLength=aSource.mLength; + aDest.mCharSize=aSource.mCharSize; + aDest.mCapacity=aSource.mCapacity; + aDest.mOwnsBuffer=aSource.mOwnsBuffer; + aSource.mOwnsBuffer=PR_FALSE; + aSource.mStr=0; } - return NS_OK; -} - -static HandleCaseConversionShutdown2* gListener = NULL; - -static void StartUpCaseConversion() -{ - nsresult err; - - if ( NULL == gListener ) - { - gListener = new HandleCaseConversionShutdown2(); - gListener->AddRef(); + else{ + nsStr::Assign(aDest,aSource,0,aSource.mLength,0); } - err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, - (nsISupports**) &gCaseConv, gListener); + } + else nsStr::Truncate(aDest,0,0); } -static void CheckCaseConversion() -{ - if(NULL == gCaseConv ) - StartUpCaseConversion(); - - // NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); - -} - -/*********************************************************************** - IMPLEMENTATION NOTES: - - Man I hate writing string classes. - You'd think after about a qintrillion lines of code have been written, - that no poor soul would ever have to do this again. Sigh. - ***********************************************************************/ - /** @@ -168,102 +118,83 @@ static void CheckCaseConversion() * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString1::nsString1() { - - NS_ASSERTION(kCommonEmptyBuffer[0]==0,kFoolMsg); - mLength = mCapacity = 0; - mStr = kCommonEmptyBuffer; - -#ifdef RICKG_DEBUG - if(!mSelfTested) { - mSelfTested=PR_TRUE; - SelfTest(); - } -#endif +nsCString::nsCString(nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); } - - /** - * This constructor accepts an isolatin string - * @update gess7/30/98 + * This constructor accepts an ascii string + * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString1::nsString1(const char* aCString) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - if(aCString) { - PRInt32 len=strlen(aCString); - EnsureCapacityFor(len); - this->SetString(aCString,len); - } +nsCString::nsCString(const char* aCString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + Assign(aCString,aLength); } +/** + * This constructor accepts an ascii string + * @update gess 1/4/99 + * @param aCString is a ptr to a 1-byte cstr + */ +nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + Assign(aString,aLength); +} /** * This is our copy constructor - * @update gess7/30/98 - * @param reference to another nsString1 + * @update gess 1/4/99 + * @param reference to another nsCString */ -nsString1::nsString1(const nsString1 &aString) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - if(aString.mLength) { - EnsureCapacityFor(aString.mLength); - this->SetString(aString.mStr,aString.mLength); - } +nsCString::nsCString(const nsStr &aString,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } - /** - * Constructor from a unicode string - * @update gess7/30/98 - * @param anicodestr pts to a unicode string + * This is our copy constructor + * @update gess 1/4/99 + * @param reference to another nsCString */ -nsString1::nsString1(const PRUnichar* aUnicodeStr) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - - PRInt32 len=(aUnicodeStr) ? nsCRT::strlen(aUnicodeStr) : 0; - if(len>0) { - EnsureCapacityFor(len); - this->SetString(aUnicodeStr,len); - } - +nsCString::nsCString(const nsCString& aString) :mAgent(aString.mAgent) { + nsStr::Initialize(*this,aString.mCharSize); + nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } +/** + * construct off a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString + */ +nsCString::nsCString(nsSubsumeCStr& aSubsumeStr) :mAgent(0) { + CSubsume(*this,aSubsumeStr); +} /** * Destructor - * @update gess7/30/98 + * Make sure we call nsStr::Destroy. */ -nsString1::~nsString1() -{ - if(mStr && (mStr!=kCommonEmptyBuffer)) - delete [] mStr; - mStr=0; - mCapacity=mLength=0; +nsCString::~nsCString() { + nsStr::Destroy(*this,mAgent); } +void nsCString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity << mCharSize); +#endif +} /** * This method truncates this string to given length. * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param anIndex -- new length of string * @return nada */ -void nsString1::Truncate(PRInt32 anIndex) { - if((anIndex>-1) && (anIndexAdd(sizeof(*this)); - aHandler->Add(mCapacity * sizeof(chartype)); +void nsCString::Truncate(PRInt32 anIndex) { + nsStr::Truncate(*this,anIndex,mAgent); } /** @@ -273,10 +204,10 @@ void nsString1::SizeOf(nsISizeOfHandler* aHandler) const * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString1::IsOrdered(void) const { +PRBool nsCString::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { - PRInt32 theIndex; + PRUint32 theIndex; for(theIndex=1;theIndexmStr[theIndex]) { result=PR_FALSE; @@ -287,445 +218,396 @@ PRBool nsString1::IsOrdered(void) const { return result; } -/** - * This method gets called when the internal buffer needs - * to grow to a given size. - * @update gess 3/30/98 - * @param aNewLength -- new capacity of string - * @return void - */ -void nsString1::EnsureCapacityFor(PRInt32 aNewLength) -{ - PRInt32 newCapacity; - - if (mCapacity > 64) { - // When the string starts getting large, double the capacity as we - // grow. - newCapacity = mCapacity * 2; - if (newCapacity < aNewLength) { - newCapacity = mCapacity + aNewLength; - } - } else { - // When the string is small, keep it's capacity a multiple of - // kGrowthDelta - PRInt32 size =aNewLength+kGrowthDelta; - newCapacity=size-(size % kGrowthDelta); - } - - if(mCapacity 0) { - nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype) + sizeof(chartype)); - } - if(mStr && (mStr!=kCommonEmptyBuffer)) - delete [] mStr; - mStr = temp; - mStr[mLength]=0; - } -} - /** - * Call this method if you want string to report a shorter length. - * @update gess7/30/98 + * Call this method if you want to force the string to a certain capacity + * @update gess 1/4/99 * @param aLength -- contains new length for mStr * @return */ -void nsString1::SetLength(PRInt32 aLength) { - if(aLength>mLength) { - EnsureCapacityFor(aLength); - nsCRT::zero(mStr + mLength, (aLength - mLength) * sizeof(chartype)); +void nsCString::SetCapacity(PRUint32 aLength) { + if(aLength>mCapacity) { + GrowCapacity(*this,aLength,mAgent); } - if((aLength>=0) && (aLengthToLower(mStr, mStr, mLength); - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* cp = mStr; - chartype* end = cp + mLength; - while (cp < end) { - chartype ch = *cp; - if ((ch >= 'A') && (ch <= 'Z')) { - *cp = 'a' + (ch - 'A'); - } - cp++; - } - -} - -/** - * Converts all chars in internal string to upper - * @update gess 7/27/98 - */ -void nsString1::ToUpperCase() -{ - // I18N code begin - CheckCaseConversion(); - if(gCaseConv) { - nsresult err = gCaseConv->ToUpper(mStr, mStr, mLength); - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* cp = mStr; - chartype* end = cp + mLength; - while (cp < end) { - chartype ch = *cp; - if ((ch >= 'a') && (ch <= 'z')) { - *cp = 'A' + (ch - 'a'); - } - cp++; - } - -} +/********************************************************************** + Lexomorphic transforms... + *********************************************************************/ /** * Converts all chars in given string to UCS2 */ -void nsString1::ToUCS2(PRInt32 aStartOffset){ - if(aStartOffsetToLower(mStr, aOut.mStr, mLength); - (*(aOut.mStr+mLength)) = 0; - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - - chartype* to = aOut.mStr; - chartype* from = mStr; - chartype* end = from + mLength; - while (from < end) { - chartype ch = *from++; - if ((ch >= 'A') && (ch <= 'Z')) { - ch = 'a' + (ch - 'A'); - } - *to++ = ch; - } - *to = 0; +void nsCString::ToLowerCase(nsCString& aString) const { + aString=*this; + nsStr::ChangeCase(aString,PR_FALSE); } /** * Converts chars in this to lowercase, and * stores them in a given output string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aOut is a string to contain result */ -void nsString1::ToUpperCase(nsString1& aOut) const -{ - aOut.EnsureCapacityFor(mLength); - aOut.mLength = mLength; - - // I18N code begin - CheckCaseConversion(); - if(gCaseConv) { - nsresult err = gCaseConv->ToUpper(mStr, aOut.mStr, mLength); - (*(aOut.mStr+mLength)) = 0; - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* to = aOut.mStr; - chartype* from = mStr; - chartype* end = from + mLength; - while (from < end) { - chartype ch = *from++; - if ((ch >= 'a') && (ch <= 'z')) { - ch = 'A' + (ch - 'a'); - } - *to++ = ch; - } - *to = 0; +void nsCString::ToUpperCase(nsCString& aString) const { + aString=*this; + nsStr::ChangeCase(aString,PR_TRUE); } +/** + * This method is used to remove all occurances of the + * characters found in aSet from this string. + * + * @update gess 01/04/99 + * @param aSet -- characters to be cut from this + * @return *this + */ +nsCString& nsCString::StripChar(PRUnichar aChar){ + + PRInt32 theIndex=FindChar(aChar,PR_FALSE,0); + while(kNotFound= 0) { - *to++ = *from++; - } - *to++ = 0; - return rv; +PRUnichar* nsCString::ToNewUnicode() const { + nsString temp(mStr); + temp.SetCapacity(8); + PRUnichar* result=temp.mUStr; + temp.mStr=0; + temp.mOwnsBuffer=PR_FALSE; + return result; } /** - * Copies contents of this onto given string. - * @update gess 7/27/98 - * @param aString to hold copy of this - * @return nada. - */ -void nsString1::Copy(nsString1& aString) const -{ - aString.mLength = 0; - aString.Append(mStr, mLength); -} - -/** - * - * @update gess 7/27/98 + * Copies contents of this string into he given buffer + * @update gess 01/04/99 * @param * @return */ -char* nsString1::ToCString(char* aBuf, PRInt32 aBufLength) const -{ - aBufLength--; // leave room for the \0 - PRInt32 len = (mLength > aBufLength) ? aBufLength : mLength; - char* to = aBuf; - chartype* from = mStr; - while (--len >= 0) { - *to++ = char(*from++); +char* nsCString::ToCString(char* aBuf, PRUint32 aBufLength,PRUint32 anOffset) const{ + if(aBuf) { + CSharedStrBuffer theSB(aBuf,PR_FALSE,aBufLength,0); + nsCAutoString temp(theSB); + temp.Assign(*this); + temp.mStr=0; } - *to++ = '\0'; return aBuf; } /** * Perform string to float conversion. - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aErrorCode will contain error if one occurs * @return float rep of string value */ -float nsString1::ToFloat(PRInt32* aErrorCode) const -{ - char buf[40]; +float nsCString::ToFloat(PRInt32* aErrorCode) const { + char buf[100]; if (mLength > PRInt32(sizeof(buf)-1)) { *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; return 0.0f; @@ -741,67 +623,56 @@ float nsString1::ToFloat(PRInt32* aErrorCode) const /** * Perform numeric string to int conversion with given radix. + * NOTE: 1. This method mandates that the string is well formed. + * 2. This method will return an error if the string you give + contains chars outside the range for the specified radix. + * @update gess 10/01/98 * @param aErrorCode will contain error if one occurs * @param aRadix tells us what base to expect the string in. - * @return int rep of string value; aErrorCode gets set too: NS_OK, NS_ERROR_ILLEGAL_VALUE + * @return int rep of string value */ -PRInt32 nsString1::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { - PRInt32 result = 0; - PRInt32 decPt=Find(PRUnichar('.'),0); - PRUnichar* cp = (-1==decPt) ? mStr + mLength-1 : mStr+decPt-1; - char digit=0; - PRUnichar theChar; -// PRInt32 theShift=0; - PRInt32 theMult=1; +PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { - *aErrorCode = (0= mStr) { - theChar = *cp; - if((theChar>='0') && (theChar<='9')){ - break; - } - else if((theChar>='a') && (theChar<='f')) { - break; - } - else if((theChar>='A') && (theChar<='F')) { - break; - } - cp--; - } + *anErrorCode = NS_OK; //now iterate the numeric chars and build our result - while(cp>=mStr) { - theChar=*cp--; + char theChar=0; + char theDigit=0; + while(--cp>=aString.mStr){ + char theChar=*cp; if((theChar>='0') && (theChar<='9')){ - digit=theChar-'0'; - } - else if((theChar>='a') && (theChar<='f')) { - digit=(theChar-'a')+10; + theDigit=theChar-'0'; } else if((theChar>='A') && (theChar<='F')) { - digit=(theChar-'A')+10; + if(10==aRadix){ + *anErrorCode=NS_ERROR_ILLEGAL_VALUE; + result=0; + break; + } + theDigit=(theChar-'A')+10; } else if('-'==theChar) { result=-result; break; } - else if(('+'==theChar) || (' '==theChar) || ('#'==theChar)) { //stop in a good state if you see this... + else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this... break; } - else if((('x'==theChar) || ('X'==theChar)) && (16==aRadix)) { - //stop in a good state. - break; - } - else{ - *aErrorCode=NS_ERROR_ILLEGAL_VALUE; + else { + //we've encountered a char that's not a legal number or sign + *anErrorCode=NS_ERROR_ILLEGAL_VALUE; result=0; break; } - result+=digit*theMult; + + result+=theDigit*theMult; theMult*=aRadix; } @@ -809,275 +680,296 @@ PRInt32 nsString1::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { } /** - * assign given PRUnichar* to this string - * @update gess 7/27/98 - * @param PRUnichar: buffer to be assigned to this - * @return this + * Call this method to extract the rightmost numeric value from the given + * 1-byte input string, and simultaneously determine the radix. + * NOTE: This method mandates that the string is well formed. + * Leading and trailing gunk should be removed, and the case upper. + * @update gess 10/01/98 + * @param anInputString contains orig string + * @param anOutString contains numeric portion copy of input string + * @param aRadix (an out parm) tells the caller what base we think the string is in. + * @return non-zero error code if this string is non-numeric */ -nsString1& nsString1::SetString(const PRUnichar* aStr,PRInt32 aLength) { - if((0 == aLength) || (nsnull == aStr)) { - mLength=0; - if (nsnull != mStr) { - mStr[0]=0; +PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { + + aString.ToUpperCase(); + + PRInt32 decPt=nsStr::FindChar(aString,'.',PR_TRUE,0); + char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1; + + aRadix=kRadixUnknown; //assume for starters... + + // Skip trailing non-numeric... + while (cp >= aString.mStr) { + if((*cp>='0') && (*cp<='9')){ + if(kRadixUnknown==aRadix) + aRadix=kRadix10; + break; } - - } else { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aStr) : aLength; - if(mCapacity<=len) - EnsureCapacityFor(len); - nsCRT::memcpy(mStr,aStr,len*sizeof(chartype)); - mLength=len; - mStr[mLength]=0; + else if((*cp>='A') && (*cp<='F')) { + aRadix=16; + break; + } + cp--; } + aString.Truncate(cp-aString.mStr+1); - return *this; -} + //ok, now scan through chars until you find the start of this number... + //we delimit the number by the presence of: +,-,#,X - -/** - * assign given char* to this string - * @update gess 7/27/98 - * @param aCString: buffer to be assigned to this - * @return this - */ -nsString1& nsString1::SetString(const char* aCString,PRInt32 aLength) { - if(aCString!=0) { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aCString) : aLength; - if(mCapacity<=len) - EnsureCapacityFor(len); - const unsigned char* from = (const unsigned char*) aCString; - const unsigned char* end = from + len; - PRUnichar* dst = mStr; - while (from < end) { - *dst++ = PRUnichar(*from++); + // Skip trailing non-numeric... + cp++; + while (--cp >= aString.mStr) { + if((*cp>='0') && (*cp<='9')){ + continue; + } + else if((*cp>='A') && (*cp<='F')) { + continue; + } + else if((*cp=='-') || (*cp=='+')){ + break; + } + else { + if(('#'==(*cp)) || ('X'==(*cp))) + aRadix=kRadix16; + cp++; //move back by one + break; } - mLength=len; - mStr[mLength]=0; - } - else { - mLength=0; //This little bit of code handles the case - mStr[0]=0; //where some blockhead hands us a null string } + if(cp>aString.mStr) + aString.Cut(0,cp-aString.mStr); + PRInt32 result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; - return *this; + return result; } + /** - * assign given char* to this string - * @update gess 7/27/98 - * @param aCString: buffer to be assigned to this - * @return this + * This method tries to autodetect that radix given a string + * @update gess 10/01/98 + * @return 10,16,or 0 (meaning I don't know) */ -nsString1& nsString1::SetString(const nsString1& aString) { - - if(mCapacity<=aString.mLength) - EnsureCapacityFor(aString.mLength); - PRUnichar* from = (PRUnichar*) aString.mStr; - PRUnichar* end = from + aString.mLength; - PRUnichar* dst = (PRUnichar*) mStr; - while(fromSetString(aStr); +PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { + + //copy chars to local buffer -- step down from 2 bytes to 1 if necessary... + nsCAutoString theString(*this); + PRUint32 theRadix=aRadix; + PRInt32 result=0; + + *anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead + if(NS_OK==*anErrorCode){ + if(kAutoDetect==aRadix) + aRadix=theRadix; + if((kRadix10==aRadix) || (kRadix16==aRadix)) + result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one. + else *anErrorCode=NS_ERROR_ILLEGAL_VALUE; + } + + return result; } +/********************************************************************** + String manipulation methods... + *********************************************************************/ + + /** * assign given string to this one - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aString: string to be added to this * @return this */ -nsString1& nsString1::operator=(const nsString1& aString) { - return this->SetString(aString); +nsCString& nsCString::Assign(const nsStr& aString,PRInt32 aCount) { + if(this!=&aString){ + nsStr::Truncate(*this,0,0); + if(-1==aCount) aCount=aString.mLength; + nsStr::Assign(*this,aString,0,aCount,mAgent); + } + return *this; } - - + /** * assign given char* to this string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aCString: buffer to be assigned to this * @return this */ -nsString1& nsString1::operator=(const char* aCString) { - return SetString(aCString); +nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) { + nsStr::Truncate(*this,0,0); + if(aCString){ + Append(aCString,aCount); + } + return *this; } - /** - * assign given char to this string - * @update gess 7/27/98 - * @param aChar: char to be assignd to this + * assign given unichar* to this string + * @update gess 01/04/99 + * @param aString: buffer to be assigned to this * @return this */ -nsString1& nsString1::operator=(char aChar) { - return this->operator=(PRUnichar(aChar)); +nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) { + nsStr::Truncate(*this,0,0); + if(aString){ + Append(aString,aCount); + } + return *this; } /** * assign given char to this string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aChar: char to be assignd to this * @return this */ -nsString1& nsString1::operator=(PRUnichar aChar) { - if(mCapacity<1) - EnsureCapacityFor(kGrowthDelta); - mStr[0]=aChar; - mLength=1; - mStr[mLength]=0; - return *this; -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const nsString1& aString,PRInt32 aLength) { - return Append(aString.mStr,aString.mLength); -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const char* aCString,PRInt32 aLength) { - - if(aCString!=0) { - PRInt32 len=(aLength<0) ? strlen(aCString) : aLength; - if(mLength+len >= mCapacity) { - EnsureCapacityFor(mLength+len); - } - const unsigned char* from = (const unsigned char*) aCString; - const unsigned char* end = from + len; - PRUnichar* to = mStr + mLength; - while (from < end) { - *to++ = PRUnichar(*from++); - } - mLength+=len; - mStr[mLength]=0; - } - return *this; -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(char aChar) { - return Append(PRUnichar(aChar)); -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const PRUnichar* aString,PRInt32 aLength) { - - if(aString!=0) { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aString) : aLength; - if(mLength+len >= mCapacity) { - EnsureCapacityFor(mLength+len); - } - if(len>0) - nsCRT::memcpy(&mStr[mLength],aString,len*sizeof(chartype)); - mLength+=len; - mStr[mLength]=0; - } - return *this; -} - - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(PRUnichar aChar) { - if(mLength < mCapacity) { - mStr[mLength++]=aChar; // the new string len < capacity, so just copy - mStr[mLength]=0; - } - else { // The new string exceeds our capacity - EnsureCapacityFor(mLength+1); - mStr[mLength++]=aChar; - mStr[mLength]=0; - } - return *this; -} - - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const nsString1 &aString) { - return this->Append(aString.mStr,aString.mLength); -} - - -/** - * append given buffer to this string - * @update gess 7/27/98 - * @param aCString: buffer to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const char* aCString) { - return Append(aCString); -} - - -/** - * append given buffer to this string - * @update gess 7/27/98 - * @param aBuffer: buffer to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const PRUnichar* aBuffer) { - return Append(aBuffer); -} - - -/** - * append given char to this string - * @update gess 7/27/98 - * @param aChar: char to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(PRUnichar aChar) { +nsCString& nsCString::Assign(char aChar) { + nsStr::Truncate(*this,0,0); return Append(aChar); } /** - * - * @update gess 7/27/98 + * assign given char to this string + * @update gess 01/04/99 + * @param aChar: char to be assignd to this + * @return this + */ +nsCString& nsCString::Assign(PRUnichar aChar) { + nsStr::Truncate(*this,0,0); + return Append(aChar); +} + +/** + * WARNING! THIS IS A VERY SPECIAL METHOD. + * This method "steals" the contents of aSource and hands it to aDest. + * Ordinarily a copy is made, but not in this version. + * @update gess10/30/98 * @param * @return */ -nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { +#ifdef AIX +nsCString& nsCString::operator=(const nsSubsumeCStr& aSubsumeString) { + nsSubsumeCStr temp(aSubsumeString); // a temp is needed for the AIX compiler + CSubsume(*this,temp); +#else +nsCString& nsCString::operator=(nsSubsumeCStr& aSubsumeString) { + CSubsume(*this,aSubsumeString); +#endif // AIX + return *this; +} + + +/** + * append given string to this string + * @update gess 01/04/99 + * @param aString : string to be appended to this + * @return this + */ +nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { + if(-1==aCount) aCount=aString.mLength; + if(0=0) && (aChar<128)) { + return Append((char)aChar); + } + return *this; +} + +/** + * + * @update gess 01/04/99 + * @param + * @return + */ +nsCString& nsCString::Append(PRInt32 aInteger,PRInt32 aRadix) { char* fmt = "%d"; if (8 == aRadix) { fmt = "%o"; @@ -1085,27 +977,28 @@ nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { fmt = "%x"; } char buf[40]; - PR_snprintf(buf, sizeof(buf), fmt, aInteger); - Append(buf); - return *this; + // *** XX UNCOMMENT THIS LINE + //PR_snprintf(buf, sizeof(buf), fmt, aInteger); + sprintf(buf,fmt,aInteger); + return Append(buf); } /** * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param * @return */ -nsString1& nsString1::Append(float aFloat){ +nsCString& nsCString::Append(float aFloat){ char buf[40]; - PR_snprintf(buf, sizeof(buf), "%g", aFloat); - Append(buf); - return *this; + // *** XX UNCOMMENT THIS LINE + //PR_snprintf(buf, sizeof(buf), "%g", aFloat); + sprintf(buf,"%g",aFloat); + return Append(buf); } - /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -1116,8 +1009,9 @@ nsString1& nsString1::Append(float aFloat){ * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { - return Mid(aCopy,0,aCount); +PRUint32 nsCString::Left(nsCString& aDest,PRInt32 aCount) const{ + nsStr::Assign(aDest,*this,0,aCount,mAgent); + return aDest.mLength; } /* @@ -1126,27 +1020,14 @@ PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { * * * @update gess 4/1/98 - * @param aCopy -- Receiving string + * @param aDest -- Receiving string * @param aCount -- number of chars to copy * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 nsString1::Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const { - aCopy.Truncate(); - if(anOffsetaCopy.mLength) ? aCopy.mLength : aCount; //don't try to copy more than you are given - if (aCount < 0) aCount = aCopy.mLength; - if(0<=anOffset) { - if(aCount>0) { - - //1st optimization: If you're inserting at end, then simply append! - if(anOffset>=mLength){ - Append(aCopy,aCopy.mLength); - return aCopy.mLength; - } - - if(mLength+aCount >= mCapacity) { - EnsureCapacityFor(mLength+aCount); - } - - PRUnichar* last = mStr + mLength; - PRUnichar* first = mStr + anOffset-1; - PRUnichar* next = mStr + mLength + aCount; - - //Copy rightmost chars, up to offset+aCount... - while(first=mCapacity) { - EnsureCapacityFor(mLength+1); +nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ + if(aCString){ + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mStr=(char*)aCString; + temp.mLength=nsCRT::strlen(aCString); + if(aCount<0) + aCount=temp.mLength; + if(temp.mLength && (0= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is a valid space character - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -PRBool nsString1::IsSpace(PRUnichar aChar) { - // XXX i18n - if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is valid digit - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */PRBool nsString1::IsDigit(PRUnichar aChar) { - // XXX i18n - return PRBool((aChar >= '0') && (aChar <= '9')); -} - - -/** - * This method trims characters found in aTrimSet from - * either end of the underlying string. - * - * @update gess 3/31/98 - * @param aTrimSet -- contains chars to be trimmed from - * both ends - * @return this - */ -nsString1& nsString1::Trim(const char* aTrimSet, - PRBool aEliminateLeading, - PRBool aEliminateTrailing) -{ - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength-1; - PRUnichar* to = mStr; - - //begin by find the first char not in aTrimSet - if(aEliminateLeading) { - while (from < end) { - PRUnichar ch = *from; - if(!strchr(aTrimSet,char(ch))) { - break; - } - from++; - } - } - - //Now, find last char not in aTrimSet - if(aEliminateTrailing) { - while(end> from) { - PRUnichar ch = *end; - if(!strchr(aTrimSet,char(ch))) { - break; - } - end--; - } - } - - //now rewrite your string without unwanted - //leading or trailing characters. - if (from != to) { - while (from <= end) { - *to++ = *from++; - } - } - else { - to = ++end; - } - - *to = '\0'; - mLength = to - mStr; - return *this; -} - -/** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. - * - * @update gess 3/31/98 - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this - */ -nsString1& nsString1::CompressWhitespace( PRBool aEliminateLeading, - PRBool aEliminateTrailing) -{ - Trim(" \r\n\t",aEliminateLeading,aEliminateTrailing); - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength; - PRUnichar* to = from; - - //this code converts /n, /t, /r into normal space ' '; - //it also eliminates runs of whitespace... - while (from < end) { - PRUnichar ch = *from++; - if (IsSpace(ch)) { - *to++ = ' '; - while (from < end) { - ch = *from++; - if (!IsSpace(ch)) { - *to++ = ch; - break; - } - } - } else { - *to++ = ch; - } - } - - *to = '\0'; - mLength = to - mStr; - return *this; -} - -/** - * This method strips whitespace throughout the string - * - * @update gess 7/27/98 - * @return this - */ -nsString1& nsString1::StripWhitespace() -{ - Trim(" \r\n\t"); - return StripChars("\r\t\n"); -} - -/** - * This method is used to replace all occurances of the - * given source char with the given dest char - * - * @param - * @return *this - */ -nsString1& nsString1::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength; - - while (from < end) { - PRUnichar ch = *from; - if(ch==aSourceChar) { - *from = aDestChar; - } - from++; - } -return *this; -} - +/********************************************************************** + Searching methods... + *********************************************************************/ + /** * Search for given character within this string. * This method does so by using a binary search, * so your string HAD BETTER BE ORDERED! * - * @update gess 3/25/98 * @param aChar is the unicode char to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { +PRInt32 nsCString::BinarySearch(PRUnichar aChar) const{ PRInt32 low=0; PRInt32 high=mLength-1; while (low <= high) { int middle = (low + high) >> 1; - if (mStr[middle]==aChar) + PRUnichar theChar=GetCharAt(*this,middle); + if (theChar==aChar) return middle; - if (mStr[middle]>aChar) + if (theChar>aChar) high = middle - 1; else low = middle + 1; @@ -1514,64 +1182,60 @@ PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { * @param aCStringBuf - charstr to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString1::Find(const char* aCStringBuf) const{ - NS_ASSERTION(0!=aCStringBuf,kNullPointerError); +PRInt32 nsCString::Find(const char* aCString,PRBool aIgnoreCase,PRInt32 anOffset) const{ PRInt32 result=kNotFound; - if(aCStringBuf) { - PRInt32 len=strlen(aCStringBuf); - if((00;i--){ - char* pos=strchr(anAsciiSet,char(mStr[i])); - if(pos) - return i; - } - } - return kNotFound; -} - -/** - * - * - * @update gess 3/25/98 - * @param - * @return - */ -PRInt32 nsString1::RFindCharInSet(nsString1& aSet,PRInt32 anOffset) const{ - if(aSet.Length()) { - for(PRInt32 i=mLength-1;i>0;i--){ - PRInt32 pos=aSet.Find(mStr[i]); - if(kNotFound!=pos) - return i; - } - } - return kNotFound; -} - -/** - * - * - * @update gess 3/25/98 - * @param - * @return - */ -PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ +PRInt32 nsCString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset) const{ NS_ASSERTION(0!=aString,kNullPointerError); + PRInt32 result=kNotFound; if(aString) { - PRInt32 len=nsCRT::strlen(aString); - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],aString,len); - else result=nsCRT::strncmp(&mStr[offset],aString,len); - if(0==result) - return offset; //in this case, 0 means they match - } - } + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mLength=nsCRT::strlen(aString); + temp.mStr=(char*)aString; + result=nsStr::RFindSubstr(*this,temp,aIgnoreCase,anOffset); } - return kNotFound; + return result; +} + +/** + * Search for a given char, starting at given offset + * + * @update gess 3/25/98 + * @param + * @return offset of found char, or -1 (kNotFound) + */ +PRInt32 nsCString::RFind(PRUnichar aChar,PRUint32 anOffset,PRBool aIgnoreCase) const{ + PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset); + return result; +} + + + +/** + * Search for a given char, starting at given offset + * + * @update gess 3/25/98 + * @param + * @return offset of found char, or -1 (kNotFound) + */ +PRInt32 nsCString::RFindChar(PRUnichar aChar,PRBool aIgnoreCase,PRInt32 anOffset) const{ + PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset); + return result; } /** @@ -1697,21 +1371,20 @@ PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ * * @update gess 3/25/98 * @param - * @return + * @return */ -PRInt32 nsString1::RFind(const nsString1& aString,PRBool aIgnoreCase) const{ - PRInt32 len=aString.mLength; - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],aString.mStr,len); - else result=nsCRT::strncmp(&mStr[offset],aString.mStr,len); - if(0==result) - return offset; //in this case, 0 means they match - } +PRInt32 nsCString::RFindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{ + NS_ASSERTION(0!=aCStringSet,kNullPointerError); + + PRInt32 result=kNotFound; + if(aCStringSet) { + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mLength=nsCRT::strlen(aCStringSet); + temp.mStr=(char*)aCStringSet; + result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset); } - return kNotFound; + return result; } /** @@ -1719,316 +1392,313 @@ PRInt32 nsString1::RFind(const nsString1& aString,PRBool aIgnoreCase) const{ * * @update gess 3/25/98 * @param - * @return + * @return */ -PRInt32 nsString1::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=anAsciiSet,kNullPointerError); +PRInt32 nsCString::RFindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{ + NS_ASSERTION(0!=aStringSet,kNullPointerError); - if(anAsciiSet) { - PRInt32 len=strlen(anAsciiSet); - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],anAsciiSet,len); - else result=nsCRT::strncmp(&mStr[offset],anAsciiSet,len); - if(0==result) - return offset; //in this case, 0 means they match - } - } + PRInt32 result=kNotFound; + if(aStringSet) { + nsStr temp; + nsStr::Initialize(temp,eTwoByte); + temp.mLength=nsCRT::strlen(aStringSet); + temp.mUStr=(PRUnichar*)aStringSet; + result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset); } - return kNotFound; + return result; } /** - * Scans this string backwards for first occurance of - * the given char. + * * * @update gess 3/25/98 * @param - * @return offset of char in string, or -1 (kNotFound) + * @return */ -PRInt32 nsString1::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ - chartype uc=nsCRT::ToUpper(aChar); - for(PRInt32 offset=mLength-1;offset>=0;offset--) - if(aIgnoreCase) { - if(nsCRT::ToUpper(mStr[offset])==uc) - return offset; - } - else if(mStr[offset]==aChar) - return offset; //in this case, 0 means they match - return kNotFound; - +PRInt32 nsCString::RFindCharInSet(const nsStr& aSet,PRInt32 anOffset) const{ + PRInt32 result=nsStr::RFindCharInSet(*this,aSet,PR_FALSE,anOffset); + return result; } + /************************************************************** COMPARISON METHODS... **************************************************************/ + /** * Compares given cstring to this string. - * @update gess 7/27/98 - * @param aCString pts to a cstring + * @update gess 01/04/99 + * @param aCString points to a cstring * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length * @return -1,0,1 */ -PRInt32 nsString1::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { +PRInt32 nsCString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount) const { NS_ASSERTION(0!=aCString,kNullPointerError); - if(-1!=aLength) { - - //if you're given a length, use it to determine the max # of bytes to compare. - //In some cases, this can speed up the string comparison. - - int maxlen=(aLength(const nsString1 &S) const {return PRBool(Compare(S)>0);} -PRBool nsString1::operator>(const char *s) const {return PRBool(Compare(s)>0);} -PRBool nsString1::operator>(const PRUnichar *s) const {return PRBool(Compare(s)>0);} -PRBool nsString1::operator<=(const nsString1 &S) const {return PRBool(Compare(S)<=0);} -PRBool nsString1::operator<=(const char *s) const {return PRBool(Compare(s)<=0);} -PRBool nsString1::operator<=(const PRUnichar *s) const {return PRBool(Compare(s)<=0);} -PRBool nsString1::operator>=(const nsString1 &S) const {return PRBool(Compare(S)>=0);} -PRBool nsString1::operator>=(const char *s) const {return PRBool(Compare(s)>=0);} -PRBool nsString1::operator>=(const PRUnichar *s) const {return PRBool(Compare(s)>=0);} +/** + * Compare given nsStr with this cstring. + * + * @param aString is an nsStr instance to be compared + * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length + * @return -1,0,1 + */ +PRInt32 nsCString::Compare(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + return nsStr::Compare(*this,aString,aCount,aIgnoreCase); +} + + +PRBool nsCString::operator==(const nsStr& S) const {return Equals(S);} +PRBool nsCString::operator==(const char* s) const {return Equals(s);} +PRBool nsCString::operator==(const PRUnichar* s) const {return Equals(s);} + +PRBool nsCString::operator!=(const nsStr& S) const {return PRBool(Compare(S)!=0);} +PRBool nsCString::operator!=(const char* s) const {return PRBool(Compare(s)!=0);} +PRBool nsCString::operator!=(const PRUnichar* s) const {return PRBool(Compare(s)!=0);} + +PRBool nsCString::operator<(const nsStr& S) const {return PRBool(Compare(S)<0);} +PRBool nsCString::operator<(const char* s) const {return PRBool(Compare(s)<0);} +PRBool nsCString::operator<(const PRUnichar* s) const {return PRBool(Compare(s)<0);} + +PRBool nsCString::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} +PRBool nsCString::operator>(const char* s) const {return PRBool(Compare(s)>0);} +PRBool nsCString::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} + +PRBool nsCString::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} +PRBool nsCString::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} +PRBool nsCString::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} + +PRBool nsCString::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} +PRBool nsCString::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} +PRBool nsCString::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} + + +PRBool nsCString::EqualsIgnoreCase(const nsStr& aString) const { + return Equals(aString,PR_TRUE); +} + +PRBool nsCString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); +} + +PRBool nsCString::EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); +} /** * Compare this to given string; note that we compare full strings here. * - * @update gess 7/27/98 - * @param aString is the other nsString1 to be compared to + * @update gess 01/04/99 + * @param aString is the other nsCString to be compared to + * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString1::Equals(const nsString1& aString) const { - if(aString.mLength==mLength) { - PRInt32 result=nsCRT::strcmp(mStr,aString.mStr); - return PRBool(0==result); - } - return PR_FALSE; +PRBool nsCString::Equals(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); + return result; } - /** * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 7/27/98 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. + * + * @param aString is the CString to be compared + * @param aCount tells us how many chars you want to compare starting with start of string + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString1::Equals(const char* aCString,PRInt32 aLength) const{ - NS_ASSERTION(0!=aCString,kNullPointerError); - - if((aLength>0) && (aLength!=mLength)) - return PR_FALSE; - - PRInt32 result=nsCRT::strcmp(mStr,aCString); - return PRBool(0==result); +PRBool nsCString::Equals(const char* aCString,PRBool aIgnoreCase,PRInt32 aCount) const{ + PRInt32 theAnswer=Compare(aCString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); + return result; } - -/** - * Compare this to given atom - * @update gess 7/27/98 - * @param aAtom -- atom to compare to this - * @return TRUE if equal - */ -PRBool nsString1::Equals(const nsIAtom* aAtom) const -{ - NS_ASSERTION(0!=aAtom,kNullPointerError); - PRInt32 result=nsCRT::strcmp(mStr,aAtom->GetUnicode()); - return PRBool(0==result); -} - - -/** - * Compare given strings - * @update gess 7/27/98 - * @param s1 -- first string to be compared - * @param s2 -- second string to be compared - * @return TRUE if equal - */ -PRBool nsString1::Equals(const PRUnichar* s1, const PRUnichar* s2) const { - NS_ASSERTION(0!=s1,kNullPointerError); - NS_ASSERTION(0!=s2,kNullPointerError); - PRBool result=PR_FALSE; - if((s1) && (s2)){ - PRInt32 cmp=nsCRT::strcmp(s1,s2); - result=PRBool(0==cmp); - } +PRBool nsCString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); return result; } /** - * Compares all chars in both strings w/o regard to case - * @update gess 7/27/98 - * @param - * @return + * Determine if given char in valid alpha range + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if in alpha range */ -PRBool nsString1::EqualsIgnoreCase(const nsString1& aString) const{ - if(aString.mLength==mLength) { - PRInt32 result=nsCRT::strcasecmp(mStr,aString.mStr); - return PRBool(0==result); +PRBool nsCString::IsAlpha(PRUnichar aChar) { + // XXX i18n + if (((aChar >= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { + return PR_TRUE; } return PR_FALSE; } +/** + * Determine if given char is a valid space character + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if is valid space char + */ +PRBool nsCString::IsSpace(PRUnichar aChar) { + // XXX i18n + if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { + return PR_TRUE; + } + return PR_FALSE; +} + +/** + * Determine if given char is valid digit + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if char is a valid digit + */ +PRBool nsCString::IsDigit(PRUnichar aChar) { + // XXX i18n + return PRBool((aChar >= '0') && (aChar <= '9')); +} + + +/************************************************************** + Define the string deallocator class... + **************************************************************/ +#ifndef RICKG_TESTBED +class nsCStringDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + static nsMemoryAgent theAgent; + nsCString* aString= (nsCString*)anObject; + if(aString){ + aString->mAgent=&theAgent; + delete aString; + } + return 0; + } +}; +#endif + +/**************************************************************************** + * This class, appropriately enough, creates and recycles nsCString objects.. + ****************************************************************************/ + + + +#ifndef RICKG_TESTBED +class nsCStringRecycler { +public: + nsCStringRecycler() : mDeque(0) { + } + + ~nsCStringRecycler() { + nsCStringDeallocator theDeallocator; + mDeque.ForEach(theDeallocator); //now delete the strings + } + + void Recycle(nsCString* aString) { + mDeque.Push(aString); + } + + nsCString* CreateString(void){ + nsCString* result=(nsCString*)mDeque.Pop(); + if(!result) + result=new nsCString(); + return result; + } + nsDeque mDeque; +}; +static nsCStringRecycler& GetRecycler(void); + /** * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param * @return */ -PRBool nsString1::EqualsIgnoreCase(const nsIAtom *aAtom) const{ - NS_ASSERTION(0!=aAtom,kNullPointerError); - PRBool result=PR_FALSE; - if(aAtom){ - PRInt32 cmp=nsCRT::strcasecmp(mStr,aAtom->GetUnicode()); - result=PRBool(0==cmp); - } - return result; +nsCStringRecycler& GetRecycler(void){ + static nsCStringRecycler gCRecycler; + return gCRecycler; } - +#endif /** - * Compares given unicode string to this w/o regard to case - * @update gess 7/27/98 - * @param s1 is the unicode string to be compared with this - * @param aLength is the length of s1, not # of bytes to compare - * @return true if full length of both strings are equal (modulo case) - */ -PRBool nsString1::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { - NS_ASSERTION(0!=s1,kNullPointerError); - NS_ASSERTION(0!=s2,kNullPointerError); - PRBool result=PR_FALSE; - if((s1) && (s2)){ - PRInt32 cmp=nsCRT::strcasecmp(s1,s2); - result=PRBool(0==cmp); - } - return result; -} - - -/** - * Compare this to given string w/o regard to case; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an optimization. - * - * @update gess 7/27/98 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString1::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { - NS_ASSERTION(0!=aCString,kNullPointerError); - - if((aLength>0) && (aLength!=mLength)) - return PR_FALSE; - - PRInt32 cmp=nsCRT::strcasecmp(mStr,aCString); - return PRBool(0==cmp); -} - - -/** - * - * @update gess 7/27/98 + * Call this mehod when you're done + * @update gess 01/04/99 * @param * @return */ -void nsString1::DebugDump(ostream& aStream) const { - for(int i=0;i= PRInt32(sizeof(buf))) { + cp = aString.ToNewCString(); + } else { + aString.ToCString(cp, len + 1); + } + if(len>0) + ::fwrite(cp, 1, len, out); + if (cp != buf) { + delete[] cp; + } + return (int) len; +} + + +/*********************************************************************** + IMPLEMENTATION NOTES: AUTOSTRING... + ***********************************************************************/ + + +/** + * Special case constructor, that allows the consumer to provide + * an underlying buffer for performance reasons. + * @param aBuffer points to your buffer + * @param aBufSize defines the size of your buffer + * @param aCurrentLength tells us the current length of the buffer + */ +nsCAutoString::nsCAutoString() : nsCString(){ + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); } /** - * - * @update gess 7/27/98 - * @param - * @return + * construct from external buffer and given string + * @param anExtBuffer describes an external buffer + * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString1::nsAutoString1(const char* aCString) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(aCString); +nsCAutoString::nsCAutoString(const nsCAutoString& aString) : nsCString() { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); + Append(aString); +} + + +/** + * Copy construct from ascii c-string + * @param aCString is a ptr to a 1-byte cstr + */ +nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); + Append(aCString,aLength); } /** - * - * @update gess 7/27/98 - * @param - * @return + * Copy construct from ascii c-string + * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString1::nsAutoString1(const nsString1& other) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(other.GetUnicode(),other.Length()); +nsCAutoString::nsCAutoString(CSharedStrBuffer& aBuffer) : nsCString() { + mAgent=0; + if(!aBuffer.mBuffer) { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + } + else { + nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); + } + AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. } /** - * - * @update gess 7/27/98 - * @param - * @return + * Copy construct from uni-string + * @param aString is a ptr to a unistr */ -nsAutoString1::nsAutoString1(PRUnichar aChar) - : nsString1() -{ - INIT_AUTO_STRING(); +nsCAutoString::nsCAutoString(const PRUnichar* aString,PRInt32 aLength) : nsCString() { + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); + Append(aString,aLength); +} + +/** + * Copy construct from an nsCString + * @param + */ +nsCAutoString::nsCAutoString(const nsStr& aString) : nsCString() { + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); + Append(aString); +} + + + +/** + * Copy construct from an nsCString + * @param + */ +nsCAutoString::nsCAutoString(PRUnichar aChar) : nsCString(){ + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); Append(aChar); } /** - * - * @update gess 7/27/98 - * @param - * @return + * construct from a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString */ -nsAutoString1::nsAutoString1(const nsAutoString1& other) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(other.GetUnicode(),other.mLength); +#ifdef AIX +nsCAutoString::nsCAutoString(const nsSubsumeCStr& aSubsumeStr) :nsCString() { + mAgent=0; + nsSubsumeCStr temp(aSubsumeStr); // a temp is needed for the AIX compiler + CSubsume(*this,temp); +#else +nsCAutoString::nsCAutoString( nsSubsumeCStr& aSubsumeStr) :nsCString() { + mAgent=0; + CSubsume(*this,aSubsumeStr); +#endif // AIX } /** - * - * @update gess 7/27/98 - * @param - * @return + * deconstruct the autstring + * @param */ -nsAutoString1::nsAutoString1(const PRUnichar* unicode, PRInt32 uslen) - : nsString1() -{ - INIT_AUTO_STRING(); - Append(unicode, uslen ? uslen : nsCRT::strlen(unicode)); +nsCAutoString::~nsCAutoString(){ } -/** - * - * @update gess 7/27/98 - * @param - * @return - */ -nsAutoString1::~nsAutoString1() -{ - if (mStr == mBuf) { - // Force to null so that baseclass dtor doesn't do damage - mStr = nsnull; - } -} - -/** - * nsAutoString1's buffer growing routine uses a different algorithm - * than nsString1 because the lifetime of the auto string is assumed - * to be shorter. Therefore, we double the size of the buffer each - * time we grow so that (hopefully) we quickly get to the right - * size. - */ -void nsAutoString1::EnsureCapacityFor(PRInt32 aNewLength) { - if (aNewLength > mCapacity) { - PRInt32 size = mCapacity * 2; - if (size < aNewLength) { - size = mCapacity + aNewLength; - } - mCapacity=size; - chartype* temp = new chartype[mCapacity+1]; - if (mLength > 0) { - nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype) + sizeof(chartype)); - } - if ((mStr != mBuf) && (0 != mStr)) { - delete [] mStr; - } - mStr = temp; - } -} - -void -nsAutoString1::SizeOf(nsISizeOfHandler* aHandler) const -{ +void nsCAutoString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); - if (mStr != mBuf) { - aHandler->Add(mCapacity * sizeof(chartype)); - } -} - - -/** - * - * - * @update gess 3/31/98 - * @param - * @return - */ -void nsAutoString1::SelfTest(){ - nsAutoString1 xas("Hello there"); - xas.Append("this string exceeds the max size"); - xas.DebugDump(cout); -} - -/** - * - * @update gess8/8/98 - * @param - * @return - */ -ostream& operator<<(ostream& os,nsAutoString1& aString){ - const PRUnichar* uc=aString.GetUnicode(); - int len=aString.Length(); - for(int i=0;i= PRInt32(sizeof(buf))) { - cp = aString.ToNewCString(); - } else { - aString.ToCString(cp, aLength + 1); - } - if(aLength >0) - ::fwrite(cp, 1, aLength, out); - if (cp != buf) { - delete [] cp; - } - return (int) aLength; -} - - - -/** - * - * @update gess 7/27/98 - * @param - * @return - */ -void nsString1::SelfTest(void) { - -#ifdef RICKG_DEBUG - mSelfTested=PR_TRUE; - - { - nsAutoString1 a("foobar"); - nsAutoString1 b("foo"); - nsAutoString1 c(".5111"); - nsAutoString1 d(" 5"); - PRInt32 result=a.Compare(b); - PRInt32 result2=result; - result=c.ToInteger(&result2); - result=d.ToInteger(&result2); - result2=result; - } - - static const char* kConstructorError = kConstructorError; - static const char* kComparisonError = "Comparision error!"; - static const char* kEqualsError = "Equals error!"; - - - nsAutoString1 as("Hello there"); - as.SelfTest(); - - static const char* temp="hello"; - - //first, let's test the constructors... - nsString1 empty; - empty=""; - empty="xxx"; - empty=""; - - nsString1 a(temp); - nsString1* a_=new nsString1(a); //test copy constructor - nsString1 b("world!"); - - //verify destructor... - delete a_; - a_=0; - - //Let's verify the Length() method... - NS_ASSERTION(5==a.Length(),"Error: constructor probably bad!"); - - //********************************************** - //Let's check out the ACCESSORS... - //********************************************** - - const chartype* p1=a.GetUnicode(); - for(int i=0;itemp3.Length(),kConstructorError); //should be char longer - - nsString1* es1=temp2.ToNewString(); //this should make us a new string - char* es2=temp2.ToNewCString(); - for(i=0;itemp8.Compare(temp9),kComparisonError); - NS_ASSERTION(0temp8,kComparisonError); - NS_ASSERTION(temp9>aaaa,kComparisonError); - - NS_ASSERTION(temp8<=temp8,kComparisonError); - NS_ASSERTION(temp8<=temp9,kComparisonError); - NS_ASSERTION(temp8<=bbbb,kComparisonError); - - NS_ASSERTION(temp9>=temp9,kComparisonError); - NS_ASSERTION(temp9>=temp8,kComparisonError); - NS_ASSERTION(temp9>=aaaa,kComparisonError); - - NS_ASSERTION(temp8.Equals(temp8),kEqualsError); - NS_ASSERTION(temp8.Equals(aaaa),kEqualsError); - - nsString1 temp10(temp8); - temp10.ToUpperCase(); - NS_ASSERTION(temp8.EqualsIgnoreCase(temp10),kEqualsError); - NS_ASSERTION(temp8.EqualsIgnoreCase("AAAA"),kEqualsError); - - - //********************************************** - //Now let's test a few string MANIPULATORS... - //********************************************** - - nsAutoString1 ab("ab"); - nsString1 abcde("cde"); - abcde.Insert(ab,0,2); - nsAutoString1 xxx("xxx"); - abcde.Insert(xxx,2,3); - - temp2.ToUpperCase(); - for(i=0;iAdd(mCapacity << mCharSize); #endif } + +nsSubsumeCStr::nsSubsumeCStr(nsStr& aString) : nsCString() { + CSubsume(*this,aString); +} + +nsSubsumeCStr::nsSubsumeCStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() { + mUStr=aString; + mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; +} + +nsSubsumeCStr::nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() { + mStr=aString; + mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; +} + + diff --git a/mozilla/xpcom/ds/nsString.h b/mozilla/xpcom/ds/nsString.h index f3a9f8c9030..521d49015db 100644 --- a/mozilla/xpcom/ds/nsString.h +++ b/mozilla/xpcom/ds/nsString.h @@ -20,32 +20,33 @@ /*********************************************************************** MODULE NOTES: - A. There are two philosophies to building string classes: - 1. Hide the underlying buffer & offer API's allow indirect iteration - 2. Reveal underlying buffer, risk corruption, but gain performance - - We chose the second option for performance reasons. - - B Our internal buffer always holds capacity+1 bytes. + This version of the nsString class offers many improvements over the + original version: + 1. Wide and narrow chars + 2. Allocators + 3. Much smarter autostrings + 4. Subsumable strings + 5. Memory pools and recycling ***********************************************************************/ -#ifndef _nsString1 -#define _nsString1 - +#ifndef _nsCString_ +#define _nsCString_ +#include "nsString2.h" #include "prtypes.h" #include "nscore.h" -#include "nsIAtom.h" #include #include +#include "nsCRT.h" #include "nsStr.h" +#include "nsIAtom.h" -#include "nsString2.h" //get new string class -class nsISizeOfHandler; +class NS_COM nsSubsumeCStr; + +class NS_COM nsCString : public nsStr { -class NS_COM nsString1 { public: /** @@ -54,46 +55,74 @@ class NS_COM nsString1 { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString1(); +nsCString(nsIMemoryAgent* anAgent=0); + /** * This constructor accepts an isolatin string - * @param an ascii is a ptr to a 1-byte cstr + * @param aCString is a ptr to a 1-byte cstr */ -nsString1(const char* aCString); +nsCString(const char* aCString,PRInt32 aLength=-1,nsIMemoryAgent* anAgent=0); + +/** + * This constructor accepts a unichar string + * @param aCString is a ptr to a 2-byte cstr + */ +nsCString(const PRUnichar* aString,PRInt32 aLength=1,nsIMemoryAgent* anAgent=0); + +/** + * This is a copy constructor that accepts an nsStr + * @param reference to another nsCString + */ +nsCString(const nsStr&,nsIMemoryAgent* anAgent=0); /** * This is our copy constructor - * @param reference to another nsString1 + * @param reference to another nsCString */ -nsString1(const nsString1&); +nsCString(const nsCString& aString); /** - * Constructor from a unicode string - * @param anicodestr pts to a unicode string + * This constructor takes a subsumestr + * @param reference to subsumestr */ -nsString1(const PRUnichar* aUnicode); +nsCString(nsSubsumeCStr& aSubsumeStr); /** - * Virtual Destructor + * Destructor + * */ -virtual ~nsString1(); - +virtual ~nsCString(); /** * Retrieve the length of this string * @return string length */ -PRInt32 Length() const { return mLength; } +inline PRInt32 Length() const { return (PRInt32)mLength; } +/** + * Retrieve the size of this string + * @return string length + */ +virtual void SizeOf(nsISizeOfHandler* aHandler) const; + + +/** + * Call this method if you want to force a different string capacity + * @update gess7/30/98 + * @param aLength -- contains new length for mStr + * @return + */ +void SetLength(PRUint32 aLength) { + Truncate(aLength); +} /** * Sets the new length of the string. * @param aLength is new string length. * @return nada */ -void SetLength(PRInt32 aLength); - +void SetCapacity(PRUint32 aLength); /** * This method truncates this string to given length. * @@ -103,20 +132,6 @@ void SetLength(PRInt32 aLength); void Truncate(PRInt32 anIndex=0); -/** - * This method gets called when the internal buffer needs - * to grow to a given size. - * @param aNewLength -- new capacity of string - * @return void - */ -virtual void EnsureCapacityFor(PRInt32 aNewLength); - -/** - * - * @param - */ -virtual void SizeOf(nsISizeOfHandler* aHandler) const; - /** * Determine whether or not the characters in this * string are in sorted order. @@ -125,6 +140,17 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const; */ PRBool IsOrdered(void) const; +/** + * Determine whether or not the characters in this + * string are in store as 1 or 2 byte (unicode) strings. + * + * @return TRUE if ordered. + */ +PRBool IsUnicode(void) const { + PRBool result=PRBool(mCharSize==eTwoByte); + return result; +} + /** * Determine whether or not this string has a length of 0 * @@ -138,54 +164,20 @@ PRBool IsEmpty(void) const { Accessor methods... *********************************************************************/ -/** - * Retrieve pointer to internal string value - * @return PRUnichar* to internal string - */ -const PRUnichar* GetUnicode(void) const; +const char* GetBuffer(void) const; -/** - * - * @param - * @return - */ -operator const PRUnichar*() const; -/** - * Retrieve unicode char at given index - * @param offset into string - * @return PRUnichar* to internal string - */ -//PRUnichar operator()(PRInt32 anIndex) const; + /** + * Get nth character. + */ +PRUnichar operator[](PRUint32 anIndex) const; +PRUnichar CharAt(PRUint32 anIndex) const; +PRUnichar First(void) const; +PRUnichar Last(void) const; -/** - * Retrieve reference to unicode char at given index - * @param offset into string - * @return PRUnichar& from internal string - */ -PRUnichar& operator[](PRInt32 anIndex) const; - -/** - * Retrieve reference to unicode char at given index - * @param offset into string - * @return PRUnichar& from internal string - */ -PRUnichar& CharAt(PRInt32 anIndex) const; - -/** - * Retrieve reference to first unicode char in string - * @return PRUnichar from internal string - */ -PRUnichar& First() const; - -/** - * Retrieve reference to last unicode char in string - * @return PRUnichar from internal string - */ -PRUnichar& Last() const; - -PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); +PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); +operator nsStr&() {return *this;} /********************************************************************** String creation methods... @@ -196,95 +188,165 @@ PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString1 operator+(const nsString1& aString); +nsSubsumeCStr operator+(const nsStr& aString); /** * create a new string by adding this to the given buffer. * @param aCString is a ptr to cstring to be added to this * @return newly created string */ -nsString1 operator+(const char* aCString); +nsSubsumeCStr operator+(const char* aCString); + +/** + * create a new string by adding this to the given wide buffer. + * @param aString is a ptr to UC-string to be added to this + * @return newly created string + */ +nsSubsumeCStr operator+(const PRUnichar* aString); /** * create a new string by adding this to the given char. * @param aChar is a char to be added to this * @return newly created string */ -nsString1 operator+(char aChar); - -/** - * create a new string by adding this to the given buffer. - * @param aStr unichar buffer to be added to this - * @return newly created string - */ -nsString1 operator+(const PRUnichar* aBuffer); +nsSubsumeCStr operator+(char aChar); /** * create a new string by adding this to the given char. * @param aChar is a unichar to be added to this * @return newly created string */ -nsString1 operator+(PRUnichar aChar); +nsSubsumeCStr operator+(PRUnichar aChar); -/** - * Converts all chars in internal string to lower - */ -void ToLowerCase(); - -/** - * Converts all chars in given string to lower - */ -void ToLowerCase(nsString1& aString) const; - -/** - * Converts all chars in given string to upper - */ -void ToUpperCase(); +/********************************************************************** + Lexomorphic transforms... + *********************************************************************/ /** * Converts all chars in given string to UCS2 * which ensure that the lower 256 chars are correct. */ -void ToUCS2(PRInt32 aStartOffset); +void ToUCS2(PRUint32 aStartOffset); /** - * Converts all chars in internal string to upper + * Converts chars in this to lowercase + * @update gess 7/27/98 */ -void ToUpperCase(nsString1& aString) const; +void ToLowerCase(); + /** - * Creates a duplicate clone (ptr) of this string. - * @return ptr to clone of this string + * Converts chars in this to lowercase, and + * stores them in aOut + * @update gess 7/27/98 + * @param aOut is a string to contain result */ -nsString1* ToNewString() const; +void ToLowerCase(nsCString& aString) const; /** - * Creates an ascii clone of this string - * NOTE: This string is allocated with new; YOU MUST deallocate with delete[]! - * @return ptr to new c-String string + * Converts chars in this to uppercase + * @update gess 7/27/98 + */ +void ToUpperCase(); + +/** + * Converts chars in this to lowercase, and + * stores them in a given output string + * @update gess 7/27/98 + * @param aOut is a string to contain result + */ +void ToUpperCase(nsCString& aString) const; + + +/** + * This method is used to remove all occurances of the + * characters found in aSet from this string. + * + * @param aSet -- characters to be cut from this + * @return *this + */ +nsCString& StripChars(const char* aSet); +nsCString& StripChar(PRUnichar aChar); + +/** + * This method strips whitespace throughout the string + * + * @return this + */ +nsCString& StripWhitespace(); + +/** + * swaps occurence of 1 string for another + * + * @return this + */ +nsCString& ReplaceChar(PRUnichar aOldChar,PRUnichar aNewChar); +nsCString& ReplaceChar(const char* aSet,PRUnichar aNewChar); + +PRInt32 CountChar(PRUnichar aChar); + +/** + * This method trims characters found in aTrimSet from + * either end of the underlying string. + * + * @param aTrimSet -- contains chars to be trimmed from + * both ends + * @return this + */ +nsCString& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/** + * This method strips whitespace from string. + * You can control whether whitespace is yanked from + * start and end of string as well. + * + * @param aEliminateLeading controls stripping of leading ws + * @param aEliminateTrailing controls stripping of trailing ws + * @return this + */ +nsCString& CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/** + * This method strips whitespace from string. + * You can control whether whitespace is yanked from + * start and end of string as well. + * + * @param aEliminateLeading controls stripping of leading ws + * @param aEliminateTrailing controls stripping of trailing ws + * @return this + */ +nsCString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/********************************************************************** + string conversion methods... + *********************************************************************/ + +/** + * This method constructs a new nsCString on the stack that is a copy + * of this string. + * + */ +nsCString* ToNewString() const; + +/** + * Creates an ISOLatin1 clone of this string + * @return ptr to new isolatin1 string */ char* ToNewCString() const; +/** + * Creates a unicode clone of this string + * @return ptr to new unicode string + */ +PRUnichar* ToNewUnicode() const; + /** * Copies data from internal buffer onto given char* buffer * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer */ -char* ToCString(char* aBuf,PRInt32 aBufLength) const; - -/** - * Copies contents of this onto given string. - * @param aString to hold copy of this - * @return nada. - */ -void Copy(nsString1& aString) const; - -/** - * Creates an unichar clone of this string - * @return ptr to new unichar string - */ -PRUnichar* ToNewUnicode() const; +char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const; /** * Perform string to float conversion. @@ -293,147 +355,99 @@ PRUnichar* ToNewUnicode() const; */ float ToFloat(PRInt32* aErrorCode) const; +/** + * Try to derive the radix from the value contained in this string + * @return kRadix10, kRadix16 or kAutoDetect (meaning unknown) + */ +PRUint32 DetermineRadix(void); + /** * Perform string to int conversion. * @param aErrorCode will contain error if one occurs * @return int rep of string value */ -PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; +PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const; + /********************************************************************** String manipulation methods... *********************************************************************/ /** - * assign given PRUnichar* to this string + * Functionally equivalent to assign or operator= + * + */ +nsCString& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsCString& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsCString& SetString(const nsStr& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} + +/** + * assign given string to this string * @param aStr: buffer to be assigned to this * @param alength is the length of the given str (or -1) if you want me to determine its length * @return this */ -nsString1& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); -nsString1& SetString(const char* aCString,PRInt32 aLength=-1); -nsString1& SetString(const nsString1& aString); +nsCString& Assign(const nsStr& aString,PRInt32 aCount=-1); +nsCString& Assign(const char* aString,PRInt32 aCount=-1); +nsCString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); +nsCString& Assign(char aChar); +nsCString& Assign(PRUnichar aChar); /** - * assign given string to this one + * here come a bunch of assignment operators... * @param aString: string to be added to this * @return this */ -nsString1& operator=(const nsString1& aString); +nsCString& operator=(const nsStr& aString) {return Assign(aString);} +nsCString& operator=(const nsCString& aString) {return Assign(aString);} +nsCString& operator=(char aChar) {return Assign(aChar);} +nsCString& operator=(PRUnichar aChar) {return Assign(aChar);} +nsCString& operator=(const char* aCString) {return Assign(aCString);} +nsCString& operator=(const PRUnichar* aString) {return Assign(aString);} +#ifdef AIX +nsCString& operator=(const nsSubsumeCStr& aSubsumeString); // AIX requires a const here +#else +nsCString& operator=(nsSubsumeCStr& aSubsumeString); +#endif /** - * assign given char* to this string - * @param aCString: buffer to be assigned to this - * @return this - */ -nsString1& operator=(const char* aCString); - -/** - * assign given char to this string - * @param aChar: char to be assignd to this - * @return this - */ -nsString1& operator=(char aChar); - -/** - * assign given unichar* to this string - * @param aBuffer: unichar buffer to be assigned to this - * @return this - */ -nsString1& operator=(const PRUnichar* aBuffer); - -/** - * assign given char to this string - * @param aChar: char to be assignd to this - * @return this - */ -nsString1& operator=(PRUnichar aChar); - -/** - * append given string to this string + * Here's a bunch of append mehtods for varying types... * @param aString : string to be appended to this * @return this */ -nsString1& operator+=(const nsString1& aString); +nsCString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} +nsCString& operator+=(const char* aCString) {return Append(aCString);} +//nsCString& operator+=(char aChar){return Append(aChar);} +nsCString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} +nsCString& operator+=(PRUnichar aChar){return Append(aChar);} -/** - * append given buffer to this string - * @param aCString: buffer to be appended to this - * @return this +/* + * Appends n characters from given string to this, + * This version computes the length of your given string + * + * @param aString is the source to be appended to this + * @return number of chars copied */ -nsString1& operator+=(const char* aCString); +nsCString& Append(const nsStr& aString) {return Append(aString,aString.mLength);} +nsCString& Append(const nsCString& aString) {return Append(aString,aString.mLength);} + -/** - * append given buffer to this string - * @param aBuffer: buffer to be appended to this - * @return this +/* + * Appends n characters from given string to this, + * + * @param aString is the source to be appended to this + * @param aCount -- number of chars to copy + * @return number of chars copied */ -nsString1& operator+=(const PRUnichar* aBuffer); - -/** - * append given char to this string - * @param aChar: char to be appended to this - * @return this - */ -nsString1& operator+=(PRUnichar aChar); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const nsString1& aString,PRInt32 aLength=-1); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const char* aCString,PRInt32 aLength=-1); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @return this - */ -nsString1& Append(char aChar); - -/** - * append given unichar buffer to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1); - -/** - * append given unichar character to this string - * @param aChar is the char to be appended to this - * @return this - */ -nsString1& Append(PRUnichar aChar); - -/** - * Append an integer onto this string - * @param aInteger is the int to be appended - * @param aRadix specifies 8,10,16 - * @return this - */ -nsString1& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16 - -/** - * Append a float value onto this string - * @param aFloat is the float to be appended - * @return this - */ -nsString1& Append(float aFloat); - +nsCString& Append(const nsStr& aString,PRInt32 aCount); +nsCString& Append(const char* aString,PRInt32 aCount=-1); +nsCString& Append(const PRUnichar* aString,PRInt32 aCount=-1); +nsCString& Append(char aChar); +nsCString& Append(PRUnichar aChar); +nsCString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 +nsCString& Append(float aFloat); + /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -443,7 +457,7 @@ nsString1& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; +PRUint32 Left(nsCString& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -455,7 +469,7 @@ PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; +PRUint32 Mid(nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -466,7 +480,7 @@ PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; +PRUint32 Right(nsCString& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -477,17 +491,29 @@ PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -PRInt32 Insert(const nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** - * Insert a single unicode char into this string at + * Insert a given string into this string at * a specified offset. * - * @param aChar char to be inserted into this string + * @param aString* to be inserted into this string * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); +nsCString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); + +/** + * Insert a single char into this string at + * a specified offset. + * + * @param character to be inserted into this string + * @param anOffset is insert pos in str + * @return the number of chars inserted into this string + */ +//nsCString& Insert(char aChar,PRUint32 anOffset); +nsCString& Insert(PRUnichar aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -497,56 +523,180 @@ PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString1& Cut(PRInt32 anOffset,PRInt32 aCount); +nsCString& Cut(PRUint32 anOffset,PRInt32 aCount); + + +/********************************************************************** + Searching methods... + *********************************************************************/ + +/** + * Search for given character within this string. + * This method does so by using a binary search, + * so your string HAD BETTER BE ORDERED! + * + * @param aChar is the unicode char to be found + * @return offset in string, or -1 (kNotFound) + */ +PRInt32 BinarySearch(PRUnichar aChar) const; /** - * This method is used to remove all occurances of the - * characters found in aSet from this string. + * Search for given substring within this string * - * @param aSet -- characters to be cut from this - * @return *this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @return offset in string, or -1 (kNotFound) */ -nsString1& StripChars(const char* aSet); +PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** - * This method is used to replace all occurances of the - * given source char with the given dest char + * Search for given char within this string * - * @param - * @return *this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) */ -nsString1& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); +PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** - * This method strips whitespace throughout the string - * - * @return this + * This method searches this string for the first character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this */ -nsString1& StripWhitespace(); +PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + /** - * This method trims characters found in aTrimSet from - * either end of the underlying string. - * - * @param aTrimSet -- contains chars to be trimmed from - * both ends - * @return this + * This methods scans the string backwards, looking for the given string + * @param aString is substring to be sought in this + * @param aIgnoreCase tells us whether or not to do caseless compare + * @return offset in string, or -1 (kNotFound) */ -nsString1& Trim(const char* aSet, - PRBool aEliminateLeading=PR_TRUE, - PRBool aEliminateTrailing=PR_TRUE); +PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + /** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. + * Search for given char within this string * - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) */ -nsString1& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, - PRBool aEliminateTrailing=PR_TRUE); +PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + +/** + * This method searches this string for the last character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this + */ +PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + + + +/********************************************************************** + Comparison methods... + *********************************************************************/ + +/** + * Compares a given string type to this string. + * @update gess 7/27/98 + * @param S is the string to be compared + * @param aIgnoreCase tells us how to treat case + * @return -1,0,1 + */ +virtual PRInt32 Compare(const nsStr &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; + +/** + * These methods compare a given string type to this one + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator==(const nsStr &aString) const; +PRBool operator==(const char *aString) const; +PRBool operator==(const PRUnichar* aString) const; + +/** + * These methods perform a !compare of a given string type to this + * @param aString is the string to be compared to this + * @return TRUE + */ +PRBool operator!=(const nsStr &aString) const; +PRBool operator!=(const char* aString) const; +PRBool operator!=(const PRUnichar* aString) const; + +/** + * These methods test if a given string is < than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator<(const nsStr &aString) const; +PRBool operator<(const char* aString) const; +PRBool operator<(const PRUnichar* aString) const; + +/** + * These methods test if a given string is > than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator>(const nsStr &S) const; +PRBool operator>(const char* aString) const; +PRBool operator>(const PRUnichar* aString) const; + +/** + * These methods test if a given string is <= than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator<=(const nsStr &S) const; +PRBool operator<=(const char* aString) const; +PRBool operator<=(const PRUnichar* aString) const; + +/** + * These methods test if a given string is >= than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator>=(const nsStr &S) const; +PRBool operator>=(const char* aString) const; +PRBool operator>=(const PRUnichar* aString) const; + +/** + * Compare this to given string; note that we compare full strings here. + * The optional length argument just lets us know how long the given string is. + * If you provide a length, it is compared to length of this string as an + * optimization. + * + * @param aString -- the string to compare to this + * @param aCount -- number of chars in given string you want to compare + * @return TRUE if equal + */ +PRBool Equals(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; + +PRBool EqualsIgnoreCase(const nsStr& aString) const; +PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; +PRBool EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aCount=-1) const; + /** * Determine if given char is a valid space character @@ -572,248 +722,83 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -/********************************************************************** - Searching methods... - *********************************************************************/ - -/** - * Search for given character within this string. - * This method does so by using a binary search, - * so your string HAD BETTER BE ORDERED! - * - * @param aChar is the unicode char to be found - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 BinarySearch(PRUnichar aChar) const; +static void Recycle(nsCString* aString); +static nsCString* CreateString(void); -/** - * Search for given substring within this string - * - * @param aString is substring to be sought in this - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 Find(const char* aString) const; -PRInt32 Find(const PRUnichar* aString) const; -PRInt32 Find(const nsString1& aString) const; +virtual void DebugDump(ostream& aStream) const; -/** - * Search for given char within this string - * - * @param aChar - char to be found - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const; + nsIMemoryAgent* mAgent; -/** - * This method searches this string for the first character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 FindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; - -/** - * This method searches this string for the last character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 RFindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; - - -/** - * This methods scans the string backwards, looking for the given string - * @param aString is substring to be sought in this - * @param aIgnoreCase tells us whether or not to do caseless compare - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString1& aString,PRBool aIgnoreCase=PR_FALSE) const; - -/** - * This methods scans the string backwards, looking for the given char - * @param char is the char to be sought in this - * @param aIgnoreCase tells us whether or not to do caseless compare - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const; - -/********************************************************************** - Comparison methods... - *********************************************************************/ - -/** - * Compares a given string type to this string. - * @update gess 7/27/98 - * @param S is the string to be compared - * @param aIgnoreCase tells us how to treat case - * @return -1,0,1 - */ -virtual PRInt32 Compare(const nsString1 &aString,PRBool aIgnoreCase=PR_FALSE) const; -virtual PRInt32 Compare(const char *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; -virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; - -/** - * These methods compare a given string type to this one - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator==(const nsString1 &aString) const; -PRBool operator==(const char *aString) const; -PRBool operator==(const PRUnichar* aString) const; -PRBool operator==(PRUnichar* aString) const; - -/** - * These methods perform a !compare of a given string type to this - * @param aString is the string to be compared to this - * @return TRUE - */ -PRBool operator!=(const nsString1 &aString) const; -PRBool operator!=(const char *aString) const; -PRBool operator!=(const PRUnichar* aString) const; - -/** - * These methods test if a given string is < than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator<(const nsString1 &aString) const; -PRBool operator<(const char *aString) const; -PRBool operator<(const PRUnichar* aString) const; - -/** - * These methods test if a given string is > than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator>(const nsString1 &S) const; -PRBool operator>(const char *aCString) const; -PRBool operator>(const PRUnichar* aString) const; - -/** - * These methods test if a given string is <= than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator<=(const nsString1 &S) const; -PRBool operator<=(const char *aCString) const; -PRBool operator<=(const PRUnichar* aString) const; - -/** - * These methods test if a given string is >= than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator>=(const nsString1 &S) const; -PRBool operator>=(const char* aCString) const; -PRBool operator>=(const PRUnichar* aString) const; - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. - * @return TRUE if equal - */ -PRBool Equals(const nsString1& aString) const; -PRBool Equals(const char* aString,PRInt32 aLength=-1) const; -PRBool Equals(const nsIAtom *aAtom) const; - - -/** - * Compares to unichar string ptrs to each other - * @param s1 is a ptr to a unichar buffer - * @param s2 is a ptr to a unichar buffer - * @return TRUE if they match - */ -PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const; - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. - * @return TRUE if equal - */ -PRBool EqualsIgnoreCase(const nsString1& aString) const; -PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; -PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; - -/** - * Compares to unichar string ptrs to each other without respect to case - * @param s1 is a ptr to a unichar buffer - * @param s2 is a ptr to a unichar buffer - * @return TRUE if they match - */ -PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; - - -static void SelfTest(); -virtual void DebugDump(ostream& aStream) const; - - protected: - -typedef PRUnichar chartype; - - chartype* mStr; - PRInt32 mLength; - PRInt32 mCapacity; -#ifdef RICKG_DEBUG - static PRBool mSelfTested; -#endif }; -ostream& operator<<(ostream& os,nsString1& aString); -extern NS_COM int fputs(const nsString1& aString, FILE* out); +extern NS_COM int fputs(const nsCString& aString, FILE* out); +ostream& operator<<(ostream& aStream,const nsCString& aString); -//---------------------------------------------------------------------- -/** - * A version of nsString1 which is designed to be used as an automatic - * variable. It attempts to operate out of a fixed size internal - * buffer until too much data is added; then a dynamic buffer is - * allocated and grown as necessary. - */ -// XXX template this with a parameter for the size of the buffer? -class NS_COM nsAutoString1 : public nsString1 { +/************************************************************** + Here comes the AutoString class which uses internal memory + (typically found on the stack) for its default buffer. + If the buffer needs to grow, it gets reallocated on the heap. + **************************************************************/ + +class NS_COM nsCAutoString : public nsCString { +public: + + nsCAutoString(); + nsCAutoString(const char* aString,PRInt32 aLength=-1); + nsCAutoString(CSharedStrBuffer& aBuffer); + nsCAutoString(const PRUnichar* aString,PRInt32 aLength=-1); + nsCAutoString(const nsStr& aString); + nsCAutoString(const nsCAutoString& aString); + +#ifdef AIX + nsCAutoString(const nsSubsumeCStr& aSubsumeStr); // AIX requires a const +#else + nsCAutoString(nsSubsumeCStr& aSubsumeStr); +#endif // AIX + nsCAutoString(PRUnichar aChar); + virtual ~nsCAutoString(); + + nsCAutoString& operator=(const nsStr& aString) {nsCString::Assign(aString); return *this;} + nsCAutoString& operator=(const char* aCString) {nsCString::operator=(aCString); return *this;} + nsCAutoString& operator=(char aChar) {nsCString::operator=(aChar); return *this;} + nsCAutoString& operator=(const PRUnichar* aBuffer) {nsCString::operator=(aBuffer); return *this;} + nsCAutoString& operator=(PRUnichar aChar) {nsCString::operator=(aChar); return *this;} + + operator char*() {return mStr;} + operator const char*() {return (const char*)mStr;} + + /** + * Retrieve the size of this string + * @return string length + */ + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + + char mBuffer[32]; +}; + +/*************************************************************** + The subsumestr class is very unusual. + It differs from a normal string in that it doesn't use normal + copy semantics when another string is assign to this. + Instead, it "steals" the contents of the source string. + + This is very handy for returning nsString classes as part of + an operator+(...) for example, in that it cuts down the number + of copy operations that must occur. + + You should probably not use this class unless you really know + what you're doing. + ***************************************************************/ +class NS_COM nsSubsumeCStr : public nsCString { public: - nsAutoString1(); - nsAutoString1(const nsString1& other); - nsAutoString1(const nsAutoString1& other); - nsAutoString1(PRUnichar aChar); - nsAutoString1(const char* aCString); - nsAutoString1(const PRUnichar* us, PRInt32 uslen = -1); - virtual ~nsAutoString1(); - - nsAutoString1& operator=(const nsString1& aString) {nsString1::operator=(aString); return *this;} - nsAutoString1& operator=(const nsAutoString1& aString) {nsString1::operator=(aString); return *this;} - nsAutoString1& operator=(const char* aCString) {nsString1::operator=(aCString); return *this;} - nsAutoString1& operator=(char aChar) {nsString1::operator=(aChar); return *this;} - nsAutoString1& operator=(const PRUnichar* aBuffer) {nsString1::operator=(aBuffer); return *this;} - nsAutoString1& operator=(PRUnichar aChar) {nsString1::operator=(aChar); return *this;} - - virtual void SizeOf(nsISizeOfHandler* aHandler) const; - - static void SelfTest(); - -protected: - virtual void EnsureCapacityFor(PRInt32 aNewLength); - - chartype mBuf[32]; + nsSubsumeCStr(nsStr& aString); + nsSubsumeCStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); + nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; -ostream& operator<<(ostream& os,nsAutoString1& aString); + #endif + diff --git a/mozilla/xpcom/ds/nsString2.cpp b/mozilla/xpcom/ds/nsString2.cpp index 917657481fb..0cf0074773c 100644 --- a/mozilla/xpcom/ds/nsString2.cpp +++ b/mozilla/xpcom/ds/nsString2.cpp @@ -17,26 +17,23 @@ * Reserved. */ -#include #include #include -#include -#include "nsString2.h" +#include "nsString.h" #include "nsDebug.h" -#include "nsCRT.h" -#include "nsIAtom.h" #include "nsDeque.h" + #ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" #endif + static const char* kNullPointerError = "Error: unexpected null ptr"; static const char* kWhitespace="\b\t\r\n "; - #define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { @@ -92,17 +89,11 @@ public: } }; -/*********************************************************************** - IMPLEMENTATION NOTES: - Man I hate writing string classes. - You'd think after about a qintrillion lines of code have been written, - that no poor soul would ever have to do this again. Sigh. - ***********************************************************************/ - -void Subsume(nsStr& aDest,nsStr& aSource){ +static void Subsume(nsStr& aDest,nsStr& aSource){ if(aSource.mStr && aSource.mLength) { if(aSource.mOwnsBuffer){ + nsStr::Destroy(aDest,0); aDest.mStr=aSource.mStr; aDest.mLength=aSource.mLength; aDest.mCharSize=aSource.mCharSize; @@ -118,17 +109,6 @@ void Subsume(nsStr& aDest,nsStr& aSource){ else nsStr::Truncate(aDest,0,0); } -//#define RICKG_DEBUG -#undef RICKG_DEBUG -#ifdef RICKG_DEBUG -/******************************************************** - This class's only purpose in life is to test nsString2. - ********************************************************/ -class CStringTester { -public: - CStringTester(); -}; -#endif /** * Default constructor. Note that we actually allocate a small buffer @@ -136,10 +116,7 @@ public: * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { -#ifdef RICKG_DEBUG - static CStringTester gStringTester; -#endif +nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); } @@ -148,9 +125,10 @@ nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAge * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString2::nsString2(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aCString); + } /** @@ -158,7 +136,7 @@ nsString2::nsString2(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* an * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString2::nsString2(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aString); } @@ -166,9 +144,9 @@ nsString2::nsString2(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent /** * This is our copy constructor * @update gess 1/4/99 - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } @@ -176,9 +154,9 @@ nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* an /** * This is our copy constructor * @update gess 1/4/99 - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { +nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { nsStr::Initialize(*this,aString.mCharSize); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } @@ -188,7 +166,7 @@ nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { * @update gess 1/4/99 * @param reference to a subsumeString */ -nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { +nsString::nsString(nsSubsumeStr& aSubsumeStr) :mAgent(0) { Subsume(*this,aSubsumeStr); } @@ -196,13 +174,15 @@ nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { * Destructor * Make sure we call nsStr::Destroy. */ -nsString2::~nsString2() { +nsString::~nsString() { nsStr::Destroy(*this,mAgent); } -void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { +void nsString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); aHandler->Add(mCapacity << mCharSize); +#endif } /** @@ -212,7 +192,7 @@ void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { * @param anIndex -- new length of string * @return nada */ -void nsString2::Truncate(PRInt32 anIndex) { +void nsString::Truncate(PRInt32 anIndex) { nsStr::Truncate(*this,anIndex,mAgent); } @@ -223,7 +203,7 @@ void nsString2::Truncate(PRInt32 anIndex) { * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString2::IsOrdered(void) const { +PRBool nsString::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { PRUint32 theIndex; @@ -248,11 +228,10 @@ PRBool nsString2::IsOrdered(void) const { * @param aLength -- contains new length for mStr * @return */ -void nsString2::SetCapacity(PRUint32 aLength) { - if(aLength>mLength) { +void nsString::SetCapacity(PRUint32 aLength) { + if(aLength>mCapacity) { GrowCapacity(*this,aLength,mAgent); } - mLength=aLength; AddNullTerminator(*this); } @@ -263,50 +242,54 @@ void nsString2::SetCapacity(PRUint32 aLength) { //static char gChar=0; -/** +/** * * @update gess1/4/99 * @return ptr to internal buffer (if 1-byte), otherwise NULL */ -const char* nsString2::GetBuffer(void) const { +const char* nsString::GetBuffer(void) const { const char* result=(eOneByte==mCharSize) ? mStr : 0; return result; } + /** - * + * This method returns the internal unicode buffer. + * Now that we've factored the string class, this should never + * be able to return a 1 byte string. + * * @update gess1/4/99 - * @return ptr to internal buffer (if 2-byte), otherwise NULL + * @return ptr to internal (2-byte) buffer; */ -const PRUnichar* nsString2::GetUnicode(void) const { - const PRUnichar* result=(eTwoByte==mCharSize) ? mUStr : 0; +const PRUnichar* nsString::GetUnicode(void) const { + const PRUnichar* result=(eOneByte==mCharSize) ? 0 : mUStr; return result; } /** * Get nth character. */ -PRUnichar nsString2::operator[](PRUint32 anIndex) const { +PRUnichar nsString::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::CharAt(PRUint32 anIndex) const { +PRUnichar nsString::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::First(void) const{ +PRUnichar nsString::First(void) const{ return GetCharAt(*this,0); } -PRUnichar nsString2::Last(void) const{ +PRUnichar nsString::Last(void) const{ return GetCharAt(*this,mLength-1); } -PRBool nsString2::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ +PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex PRInt32(sizeof(buf)-1)) { *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; @@ -655,7 +701,7 @@ float nsString2::ToFloat(PRInt32* aErrorCode) const { * @param aRadix tells us what base to expect the string in. * @return int rep of string value */ -PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { +PRInt32 _ToInteger(nsString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { //copy chars to local buffer -- step down from 2 bytes to 1 if necessary... PRInt32 result=0; @@ -698,7 +744,6 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { result+=theDigit*theMult; theMult*=aRadix; } - return result; } @@ -713,11 +758,11 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * @param aRadix (an out parm) tells the caller what base we think the string is in. * @return non-zero error code if this string is non-numeric */ -PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { +PRInt32 GetNumericSubstring(nsString& aString,PRUint32& aRadix) { aString.ToUpperCase(); - PRInt32 decPt=aString.FindChar(aString,'.',PR_TRUE,0); + PRInt32 decPt=nsStr::FindChar(aString,'.',PR_TRUE,0); char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1; aRadix=kRadixUnknown; //assume for starters... @@ -762,7 +807,6 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { if(cp>aString.mStr) aString.Cut(0,cp-aString.mStr); PRInt32 result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; - return result; } @@ -772,10 +816,10 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { * @update gess 10/01/98 * @return 10,16,or 0 (meaning I don't know) */ -PRUint32 nsString2::DetermineRadix(void) { +PRUint32 nsString::DetermineRadix(void) { PRUint32 result=kRadixUnknown; if(0(const nsString2& S) const {return PRBool(Compare(S)>0);} -PRBool nsString2::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} -PRBool nsString2::operator>(const char* s) const {return PRBool(Compare(s)>0);} -PRBool nsString2::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} +PRBool nsString::operator>(const nsString& S) const {return PRBool(Compare(S)>0);} +PRBool nsString::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} +PRBool nsString::operator>(const char* s) const {return PRBool(Compare(s)>0);} +PRBool nsString::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} -PRBool nsString2::operator<=(const nsString2& S) const {return PRBool(Compare(S)<=0);} -PRBool nsString2::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} -PRBool nsString2::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} -PRBool nsString2::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} +PRBool nsString::operator<=(const nsString& S) const {return PRBool(Compare(S)<=0);} +PRBool nsString::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} +PRBool nsString::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} +PRBool nsString::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} -PRBool nsString2::operator>=(const nsString2& S) const {return PRBool(Compare(S)>=0);} -PRBool nsString2::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} -PRBool nsString2::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} -PRBool nsString2::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} +PRBool nsString::operator>=(const nsString& S) const {return PRBool(Compare(S)>=0);} +PRBool nsString::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} +PRBool nsString::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} +PRBool nsString::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} -PRBool nsString2::EqualsIgnoreCase(const nsString2& aString) const { +PRBool nsString::EqualsIgnoreCase(const nsString& aString) const { return Equals(aString,PR_TRUE); } -PRBool nsString2::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { - return Equals(aString,aLength,PR_TRUE); +PRBool nsString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); } -PRBool nsString2::EqualsIgnoreCase(const nsIAtom *aAtom) const { +PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const { return Equals(aAtom,PR_TRUE); } -PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { return Equals(s1,s2,PR_TRUE); } @@ -1562,66 +1659,61 @@ PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) con * Compare this to given string; note that we compare full strings here. * * @update gess 01/04/99 - * @param aString is the other nsString2 to be compared to + * @param aString is the other nsString to be compared to + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString2::Equals(const nsString2& aString,PRBool aIgnoreCase) const { - PRInt32 result=nsStr::Compare(*this,aString,MinInt(mLength,aString.mLength),aIgnoreCase); - return PRBool(0==result); +PRBool nsString::Equals(const nsString& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); + return result; + } /** * Compare this to given string; note that we compare full strings here. * * @update gess 01/04/99 - * @param aString is the other nsString2 to be compared to + * @param aString is the other nsString to be compared to + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString2::Equals(const nsStr& aString,PRBool aIgnoreCase) const { - PRInt32 result=nsStr::Compare(*this,aString,MinInt(mLength,aString.mLength),aIgnoreCase); - return PRBool(0==result); -} - -PRBool nsString2::Equals(const char* aString,PRBool aIgnoreCase) const { - if(aString) { - return Equals(aString,nsCRT::strlen(aString),aIgnoreCase); - } - return 0; -} - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 01/04/99 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString2::Equals(const char* aCString,PRUint32 aCount,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=aCString,kNullPointerError); - PRBool result=PR_FALSE; - if(aCString) { - PRInt32 theAnswer=Compare(aCString,aIgnoreCase,aCount); - result=PRBool(0==theAnswer); - } +PRBool nsString::Equals(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); return result; } -PRBool nsString2::Equals(const PRUnichar* aString,PRBool aIgnoreCase) const { - NS_ASSERTION(0!=aString,kNullPointerError); - PRBool result=PR_FALSE; - if(aString) { - result=Equals(aString,nsCRT::strlen(aString),aIgnoreCase); - } +/** + * Compare this to given c-string; note that we compare full strings here. + * + * @param aString is the CString to be compared + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length + * @return TRUE if equal + */ +PRBool nsString::Equals(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); return result; } +/** + * Compare this to given unicode string; note that we compare full strings here. + * + * @param aString is the U-String to be compared + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length + * @return TRUE if equal + */ +PRBool nsString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); + return result; +} /** - * Compare this to given string; note that we compare full strings here. + * Compare this to given atom; note that we compare full strings here. * The optional length argument just lets us know how long the given string is. * If you provide a length, it is compared to length of this string as an * optimization. @@ -1631,45 +1723,29 @@ PRBool nsString2::Equals(const PRUnichar* aString,PRBool aIgnoreCase) const { * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString2::Equals(const PRUnichar* aString,PRUint32 aCount,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=aString,kNullPointerError); - PRBool result=PR_FALSE; - if(aString){ - PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); - result=PRBool(0==theAnswer); - } - return result; -} - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 01/04/99 - * @param aString -- unistring to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString2::Equals(const nsIAtom* aAtom,PRBool aIgnoreCase) const{ +PRBool nsString::Equals(const nsIAtom* aAtom,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=aAtom,kNullPointerError); PRBool result=PR_FALSE; if(aAtom){ - PRInt32 cmp=nsCRT::strcasecmp(mUStr,aAtom->GetUnicode()); + PRInt32 cmp=0; + if (aIgnoreCase) + cmp=nsCRT::strcasecmp(mUStr,aAtom->GetUnicode()); + else + cmp=nsCRT::strcmp(mUStr,aAtom->GetUnicode()); result=PRBool(0==cmp); } - return result; + + return result; } /** * Compare given strings * @update gess 7/27/98 - * @param s1 -- first string to be compared - * @param s2 -- second string to be compared + * @param s1 -- first unichar string to be compared + * @param s2 -- second unichar string to be compared * @return TRUE if equal */ -PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase) const { +PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1677,7 +1753,8 @@ PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnore PRInt32 cmp=(aIgnoreCase) ? nsCRT::strcasecmp(s1,s2) : nsCRT::strcmp(s1,s2); result=PRBool(0==cmp); } - return result; + + return result; } /** @@ -1687,7 +1764,7 @@ PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnore * @param aChar is character to be tested * @return TRUE if in alpha range */ -PRBool nsString2::IsAlpha(PRUnichar aChar) { +PRBool nsString::IsAlpha(PRUnichar aChar) { // XXX i18n if (((aChar >= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { return PR_TRUE; @@ -1702,7 +1779,7 @@ PRBool nsString2::IsAlpha(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if is valid space char */ -PRBool nsString2::IsSpace(PRUnichar aChar) { +PRBool nsString::IsSpace(PRUnichar aChar) { // XXX i18n if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { return PR_TRUE; @@ -1717,12 +1794,12 @@ PRBool nsString2::IsSpace(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if char is a valid digit */ -PRBool nsString2::IsDigit(PRUnichar aChar) { +PRBool nsString::IsDigit(PRUnichar aChar) { // XXX i18n return PRBool((aChar >= '0') && (aChar <= '9')); } - +#ifndef RICKG_TESTBED /************************************************************** Define the string deallocator class... **************************************************************/ @@ -1730,7 +1807,7 @@ class nsStringDeallocator: public nsDequeFunctor{ public: virtual void* operator()(void* anObject) { static nsMemoryAgent theAgent; - nsString2* aString= (nsString2*)anObject; + nsString* aString= (nsString*)anObject; if(aString){ aString->mAgent=&theAgent; delete aString; @@ -1740,10 +1817,9 @@ public: }; /**************************************************************************** - * This class, appropriately enough, creates and recycles nsString2 objects.. + * This class, appropriately enough, creates and recycles nsString objects.. ****************************************************************************/ - class nsStringRecycler { public: nsStringRecycler() : mDeque(0) { @@ -1754,22 +1830,19 @@ public: mDeque.ForEach(theDeallocator); //now delete the strings } - void Recycle(nsString2* aString) { + void Recycle(nsString* aString) { mDeque.Push(aString); } - nsString2* CreateString(eCharSize aCharSize){ - nsString2* result=(nsString2*)mDeque.Pop(); + nsString* CreateString(eCharSize aCharSize){ + nsString* result=(nsString*)mDeque.Pop(); if(!result) - result=new nsString2(aCharSize); + result=new nsString(aCharSize); return result; } nsDeque mDeque; }; - -void Subsume(nsStr& aDest,nsStr& aSource); -nsStringRecycler& GetRecycler(void); - +static nsStringRecycler& GetRecycler(void); /** * @@ -1781,6 +1854,8 @@ nsStringRecycler& GetRecycler(void){ static nsStringRecycler gRecycler; return gRecycler; } +#endif + /** * Call this mehod when you're done @@ -1788,8 +1863,11 @@ nsStringRecycler& GetRecycler(void){ * @param * @return */ -nsString2* nsString2::CreateString(eCharSize aCharSize){ - nsString2* result=GetRecycler().CreateString(aCharSize); +nsString* nsString::CreateString(eCharSize aCharSize){ + nsString* result=0; +#ifndef RICKG_TESTBED + GetRecycler().CreateString(aCharSize); +#endif return result; } @@ -1799,8 +1877,12 @@ nsString2* nsString2::CreateString(eCharSize aCharSize){ * @param * @return */ -void nsString2::Recycle(nsString2* aString){ +void nsString::Recycle(nsString* aString){ +#ifndef RICKG_TESTBED GetRecycler().Recycle(aString); +#else + delete aString; +#endif } @@ -1810,7 +1892,7 @@ void nsString2::Recycle(nsString2* aString){ * @param * @return */ -void nsString2::DebugDump(ostream& aStream) const { +void nsString::DebugDump(ostream& aStream) const { for(PRUint32 i=0;i>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); -} -/** - * construct from external buffer and given string - * @param anExtBuffer describes an external buffer - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2(aStr.mCharSize) { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,aStr.mCharSize,PR_FALSE); - mAgent=0; - AddNullTerminator(*this); - Assign(aCString); } /** * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const char* aCString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); - Assign(aCString); -} + Append(aCString,aLength); + -/** - * Copy construct from ascii c-string - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(char* aCString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - mAgent=0; - if(assumeOwnership && aCString) { - aCapacity = (-1==aCapacity) ? strlen(aCString) : aCapacity-1; - nsStr::Initialize(*this,aCString,aCapacity,aCapacity,eOneByte,PR_TRUE); - AddNullTerminator(*this); - } - else { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aCString); - } } /** * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const PRUnichar* aString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); + Append(aString,aLength); + } /** * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString2::nsAutoString2(PRUnichar* aString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { +nsAutoString::nsAutoString(CSharedStrBuffer& aBuffer) : nsString(eTwoByte) { mAgent=0; - if(assumeOwnership && aString) { - aCapacity = (-1==aCapacity) ? nsCRT::strlen(aString) : aCapacity-1; - nsStr::Initialize(*this,(char*)aString,aCapacity,aCapacity,eTwoByte,PR_TRUE); - AddNullTerminator(*this); + if(!aBuffer.mBuffer) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); } else { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aString); + nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } + AddNullTerminator(*this); } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(const nsStr& aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const nsStr& aString,eCharSize aCharSize) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); + Append(aString); + } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const nsAutoString& aString,eCharSize aCharSize) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsString2& aString,eCharSize aCharSize) : nsString2(aCharSize){ - mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aString); + Append(aString); + } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ +nsAutoString::nsAutoString(PRUnichar aChar,eCharSize aCharSize) : nsString(aCharSize){ mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aChar); + Append(aChar); + } /** @@ -2012,12 +2057,12 @@ nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aC * @param reference to a subsumeString */ #ifdef AIX -nsAutoString2::nsAutoString2(const nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { +nsAutoString::nsAutoString(const nsSubsumeStr& aSubsumeStr) :nsString(aSubsumeStr.mCharSize) { mAgent=0; nsSubsumeStr temp(aSubsumeStr); // a temp is needed for the AIX compiler Subsume(*this,temp); #else -nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { +nsAutoString::nsAutoString( nsSubsumeStr& aSubsumeStr) :nsString(aSubsumeStr.mCharSize) { mAgent=0; Subsume(*this,aSubsumeStr); #endif // AIX @@ -2027,384 +2072,29 @@ nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr. * deconstruct the autstring * @param */ -nsAutoString2::~nsAutoString2(){ -// bool b=true; -// mStr=0; +nsAutoString::~nsAutoString(){ } -void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { +void nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); aHandler->Add(mCapacity << mCharSize); +#endif } -nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2(aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString(aString.mCharSize) { Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2(aString.mCharSize) { - Subsume(*this,aString); -} - -nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eTwoByte) { +nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString(eTwoByte) { mUStr=aString; mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } -nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { +nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString(eOneByte) { mStr=aString; mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } -/*********************************************************************** - IMPLEMENTATION of nsCAutoString, which is a vanilla string class that - only ever stores 1 byte character strings. Typically you'll use this - class to hold a pointer to a char*, which comes from an nsString. - ***********************************************************************/ -nsCAutoString::nsCAutoString(const nsString2& aString) : nsAutoString(aString,eOneByte){ -} - -nsCAutoString::operator const char*() const { - return (const char*)mStr; -} - - - -#ifdef RICKG_DEBUG -/*********************************************************************** - IMPLEMENTATION of CStringTester... - ***********************************************************************/ -CStringTester::CStringTester() { - - static const char* kConstructorError = kConstructorError; - static const char* kComparisonError = "Comparision error!"; - static const char* kEqualsError = "Equals error!"; - - eCharSize theSize=eTwoByte; - - //begin by testing the constructors... - { - { - nsString2 theString0("foo",theSize); //watch it construct and destruct - } - - //Let's test our autoCStrings... - { - nsString2 theString("hello"); - nsCAutoString theCStr(theString); - } - - - { - //this test makes sure that autostrings who assume ownership of a buffer, - //don't also try to copy that buffer onto itself... (was a bug) - char* theStr="hello rick"; - nsAutoString2(theStr,5,eOneByte,PR_FALSE); - - } - - { - nsString2 theString("hello"); - nsString2 temp1=theString+" there!"; - nsString2 temp2=theString+'!'; - nsSubsumeStr temp3=theString+'?'; - nsString2 temp4(temp3); - temp1=temp3; - nsAutoString2 temp5("hello"); - nsSubsumeStr temp6(temp5); - } - - nsString2 theString1(theSize); - nsString2 theString("hello",theSize); - nsString2 theString3(theString,theSize); - nsStr& si=theString; - nsString2 theString4(si,theSize); - PRUint32 theLen=theString3.Length(); - - //and hey, why not do a few lexo-morphic tests... - theString.ToUpperCase(); - theString.ToLowerCase(); - - //while we're here, let's try truncation and setting the length. - theString3.Truncate(3); - theLen=theString3.Length(); - - - theString.SetCapacity(3); - const char* theBuffer=theString.GetBuffer(); - const char* theOther=theBuffer; - - { - nsString2 temp(" hello there rick ",theSize); - temp.CompressWhitespace(); - } - - nsString2 theString5(" hello there rick ",theSize); - theString5.StripChars("reo"); - theString5.Trim(" ",PR_TRUE,PR_FALSE); - theString5.StripWhitespace(); - - nsString2* theString6=theString5.ToNewString(); - nsString2::Recycle(theString6); - - char* str=theString5.ToNewCString(); - delete [] str; - - char buffer[100]; - theString5.ToCString(buffer,sizeof(buffer)-1); - theOther=theString5.GetBuffer(); - } - - //try a few numeric conversion routines... - { - nsString2 str1("10000",theSize); - nsString2 str2("hello"); - nsString2 str3; - PRInt32 err; - PRInt32 theInt=str1.ToInteger(&err); - theInt=str2.ToInteger(&err); - theInt=str3.ToInteger(&err); - - str1="100.100"; - float theFloat=str1.ToFloat(&err); - } - //Now test the character accessor methods... - { - nsString2 theString("hello",theSize); - PRUint32 len=theString.Length(); - PRUnichar ch; - for(PRUint32 i=0;itemp8.Compare(bbbb),kComparisonError); - NS_ASSERTION(0>temp8.Compare(temp9),kComparisonError); - NS_ASSERTION(0=temp8)),kComparisonError); - - NS_ASSERTION(((temp9>temp8) && (temp8<=temp9)),kComparisonError); - NS_ASSERTION(temp9>aaaa,kComparisonError); - - NS_ASSERTION(temp8<=temp8,kComparisonError); - NS_ASSERTION(temp8<=temp9,kComparisonError); - NS_ASSERTION(temp8<=bbbb,kComparisonError); - - NS_ASSERTION(((temp9>=temp8) && (temp8=temp8,kComparisonError); - NS_ASSERTION(temp9>=aaaa,kComparisonError); - - NS_ASSERTION(temp8.Equals(temp8),kEqualsError); - NS_ASSERTION(temp8.Equals(aaaa),kEqualsError); - - nsString2 temp10(temp8); - temp10.ToUpperCase(); - NS_ASSERTION(temp8.Equals(temp10,PR_TRUE),kEqualsError); - NS_ASSERTION(temp8.Equals("AAAA",PR_TRUE),kEqualsError); - - - //********************************************** - //Now let's test a few string MANIPULATORS... - //********************************************** - - { - nsAutoString2 ab("ab",theSize); - nsString2 abcde("cde",theSize); - nsString2 cut("abcdef",theSize); - cut.Cut(7,10); //this is out of bounds, so ignore... - cut.DebugDump(cout); - cut.Cut(5,2); //cut last chars - cut.DebugDump(cout); - cut.Cut(1,1); //cut first char - cut.DebugDump(cout); - cut.Cut(2,1); //cut one from the middle - cut.DebugDump(cout); - cut="Hello there Rick"; - cut.DebugDump(cout); - - cut="'\"abcdef\"'"; - cut.Trim("'"); - cut.DebugDump(cout); - cut.Trim("\"",PR_TRUE,PR_FALSE); - cut.DebugDump(cout); - cut.Trim("\"",PR_FALSE,PR_TRUE); - cut.DebugDump(cout); - - cut="abc def\n\n ghi"; - cut.StripWhitespace(); - cut.DebugDump(cout); - cut="abc def\n\n ghi"; - cut.CompressWhitespace(); - cut.DebugDump(cout); - } - - - //********************************************** - //Now let's test the SEARCHING operations... - //********************************************** - - { - nsString2 find1("abcdefghijk",theSize); - nsString2 find2("ijk",theSize); - - PRInt32 pos=find1.Find("efg"); - NS_ASSERTION(pos==4,"Error: Find routine"); - - pos=find1.Find("EFG",PR_TRUE); - NS_ASSERTION(pos==4,"Error: Find routine"); - - pos=find1.Find('d'); - NS_ASSERTION(pos==3,"Error: Find char routine"); - - pos=find1.Find(find2); - NS_ASSERTION(pos==8,"Error: Find char routine"); - - pos=find1.FindCharInSet("12k"); - NS_ASSERTION(pos==10,"Error: FindFirstInChar routine"); - - pos=find1.RFindCharInSet("12k"); - NS_ASSERTION(pos==10,"Error: FindLastInChar routine"); - - pos=find1.RFind("efg"); - NS_ASSERTION(pos==4,"Error: RFind routine"); - - pos=find1.RFind("xxx"); - NS_ASSERTION(pos==-1,"Error: RFind routine"); //this should fail - - pos=find1.RFind(""); - NS_ASSERTION(pos==-1,"Error: RFind routine"); //this too should fail. - - pos=find1.RFind('a'); - NS_ASSERTION(pos==0,"Error: RFind routine"); - - pos=find1.BinarySearch('a'); - pos=find1.BinarySearch('b'); - pos=find1.BinarySearch('c'); - pos=find1.BinarySearch('d'); - pos=find1.BinarySearch('e'); - pos=find1.BinarySearch('f'); - pos=find1.BinarySearch('g'); - pos=find1.BinarySearch('h'); - pos=find1.BinarySearch('i'); - pos=find1.BinarySearch('z'); - } - - { - //now let's try a memory allocation test... - nsString2 temp; - for(int i=0;i<100;i++){ - temp+="hello "; - } - int x=5; - } -}; - -#endif //rickg_debug diff --git a/mozilla/xpcom/ds/nsString2.h b/mozilla/xpcom/ds/nsString2.h index 79d0658aae7..29490974262 100644 --- a/mozilla/xpcom/ds/nsString2.h +++ b/mozilla/xpcom/ds/nsString2.h @@ -30,19 +30,17 @@ ***********************************************************************/ -#ifndef _nsString2 -#define _nsString2 +#ifndef _nsString_ +#define _nsString_ #include "prtypes.h" #include "nscore.h" #include #include #include "nsCRT.h" - -#include "nsStr.h" -#include -#include +#include "nsString.h" #include "nsIAtom.h" +#include "nsStr.h" class nsISizeOfHandler; @@ -50,14 +48,9 @@ class nsISizeOfHandler; #define nsString2 nsString #define nsAutoString2 nsAutoString -#define kRadix10 (10) -#define kRadix16 (16) -#define kAutoDetect (100) -#define kRadixUnknown (kAutoDetect+1) - class NS_COM nsSubsumeStr; -class NS_COM nsString2 : public nsStr { +class NS_COM nsString : public nsStr { public: @@ -67,44 +60,44 @@ class NS_COM nsString2 : public nsStr { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString2(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This constructor accepts an isolatin string * @param aCString is a ptr to a 1-byte cstr */ -nsString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This constructor accepts a unichar string * @param aCString is a ptr to a 2-byte cstr */ -nsString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This is a copy constructor that accepts an nsStr - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This is our copy constructor - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2(const nsString2& aString); +nsString(const nsString& aString); /** * This constructor takes a subsumestr * @param reference to subsumestr */ -nsString2(nsSubsumeStr& aSubsumeStr); +nsString(nsSubsumeStr& aSubsumeStr); /** * Destructor * */ -virtual ~nsString2(); +virtual ~nsString(); /** * Retrieve the length of this string @@ -120,13 +113,13 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const; /** - * Call this method if you want to force a different string capacity + * Call this method if you want to force a different string length * @update gess7/30/98 * @param aLength -- contains new length for mStr * @return */ void SetLength(PRUint32 aLength) { - SetCapacity(aLength); + Truncate(aLength); } /** @@ -173,7 +166,7 @@ PRBool IsEmpty(void) const { } /********************************************************************** - Accessor methods... + Getters/Setters... *********************************************************************/ const char* GetBuffer(void) const; @@ -188,11 +181,14 @@ PRUnichar CharAt(PRUint32 anIndex) const; PRUnichar First(void) const; PRUnichar Last(void) const; + /** + * Set nth character. + */ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); /********************************************************************** - String creation methods... + String concatenation methods... *********************************************************************/ /** @@ -201,13 +197,7 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); * @return new string */ nsSubsumeStr operator+(const nsStr& aString); - -/** - * Create a new string by appending given string to this - * @param aString -- 2nd string to be appended - * @return new string - */ -nsSubsumeStr operator+(const nsString2& aString); +nsSubsumeStr operator+(const nsString& aString); /** * create a new string by adding this to the given buffer. @@ -260,7 +250,7 @@ void ToLowerCase(); * @update gess 7/27/98 * @param aOut is a string to contain result */ -void ToLowerCase(nsString2& aString) const; +void ToLowerCase(nsString& aString) const; /** * Converts chars in this to uppercase @@ -274,7 +264,7 @@ void ToUpperCase(); * @update gess 7/27/98 * @param aOut is a string to contain result */ -void ToUpperCase(nsString2& aString) const; +void ToUpperCase(nsString& aString) const; /** @@ -284,21 +274,25 @@ void ToUpperCase(nsString2& aString) const; * @param aSet -- characters to be cut from this * @return *this */ -nsString2& StripChars(const char* aSet); +nsString& StripChars(const char* aSet); +nsString& StripChar(PRUnichar aChar); /** * This method strips whitespace throughout the string * * @return this */ -nsString2& StripWhitespace(); +nsString& StripWhitespace(); /** * swaps occurence of 1 string for another * * @return this */ -nsString2& ReplaceChar(PRUnichar aSourceChar,PRUnichar aDestChar); +nsString& ReplaceChar(PRUnichar anOldChar,PRUnichar aNewChar); +nsString& ReplaceChar(const char* aSet,PRUnichar aNewChar); + +PRInt32 CountChar(PRUnichar aChar); /** * This method trims characters found in aTrimSet from @@ -308,7 +302,7 @@ nsString2& ReplaceChar(PRUnichar aSourceChar,PRUnichar aDestChar); * both ends * @return this */ -nsString2& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /** * This method strips whitespace from string. @@ -319,7 +313,7 @@ nsString2& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aElimin * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString2& CompressSet(const char* aSet, char aChar, PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /** * This method strips whitespace from string. @@ -330,18 +324,18 @@ nsString2& CompressSet(const char* aSet, char aChar, PRBool aEliminateLeading=PR * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString2& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /********************************************************************** string conversion methods... *********************************************************************/ /** - * This method constructs a new nsString2 on the stack that is a copy + * This method constructs a new nsString on the stack that is a copy * of this string. * */ -nsString2* ToNewString() const; +nsString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string @@ -392,9 +386,9 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const; * Functionally equivalent to assign or operator= * */ -nsString2& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} -nsString2& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} -nsString2& SetString(const nsString2& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const nsString& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} /** * assign given string to this string @@ -403,28 +397,27 @@ nsString2& SetString(const nsString2& aString,PRInt32 aLength=-1) {return Assign if you want me to determine its length * @return this */ -nsString2& Assign(const nsString2& aString,PRInt32 aCount=-1); -nsString2& Assign(const nsStr& aString,PRInt32 aCount=-1); -nsString2& Assign(const char* aString,PRInt32 aCount=-1); -nsString2& Assign(const PRUnichar* aString,PRInt32 aCount=-1); -nsString2& Assign(char aChar); -nsString2& Assign(PRUnichar aChar); +nsString& Assign(const nsStr& aString,PRInt32 aCount=-1); +nsString& Assign(const char* aString,PRInt32 aCount=-1); +nsString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); +nsString& Assign(char aChar); +nsString& Assign(PRUnichar aChar); /** * here come a bunch of assignment operators... * @param aString: string to be added to this * @return this */ -nsString2& operator=(const nsString2& aString) {return Assign(aString);} -nsString2& operator=(const nsStr& aString) {return Assign(aString);} -nsString2& operator=(char aChar) {return Assign(aChar);} -nsString2& operator=(PRUnichar aChar) {return Assign(aChar);} -nsString2& operator=(const char* aCString) {return Assign(aCString);} -nsString2& operator=(const PRUnichar* aString) {return Assign(aString);} +nsString& operator=(const nsString& aString) {return Assign(aString);} +nsString& operator=(const nsStr& aString) {return Assign(aString);} +nsString& operator=(char aChar) {return Assign(aChar);} +nsString& operator=(PRUnichar aChar) {return Assign(aChar);} +nsString& operator=(const char* aCString) {return Assign(aCString);} +nsString& operator=(const PRUnichar* aString) {return Assign(aString);} #ifdef AIX -nsString2& operator=(const nsSubsumeStr& aSubsumeString); // AIX requires a const here +nsString& operator=(const nsSubsumeStr& aSubsumeString); // AIX requires a const here #else -nsString2& operator=(nsSubsumeStr& aSubsumeString); +nsString& operator=(nsSubsumeStr& aSubsumeString); #endif /** @@ -432,12 +425,12 @@ nsString2& operator=(nsSubsumeStr& aSubsumeString); * @param aString : string to be appended to this * @return this */ -nsString2& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} -nsString2& operator+=(const nsString2& aString){return Append(aString,aString.mLength);} -nsString2& operator+=(const char* aCString) {return Append(aCString);} -//nsString2& operator+=(char aChar){return Append(aChar);} -nsString2& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} -nsString2& operator+=(PRUnichar aChar){return Append(aChar);} +nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} +nsString& operator+=(const nsString& aString){return Append(aString,aString.mLength);} +nsString& operator+=(const char* aCString) {return Append(aCString);} +//nsString& operator+=(char aChar){return Append(aChar);} +nsString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} +nsString& operator+=(PRUnichar aChar){return Append(aChar);} /* * Appends n characters from given string to this, @@ -446,8 +439,8 @@ nsString2& operator+=(PRUnichar aChar){return Append(aChar);} * @param aString is the source to be appended to this * @return number of chars copied */ -nsString2& Append(const nsStr& aString) {return Append(aString,aString.mLength);} -nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLength);} +nsString& Append(const nsStr& aString) {return Append(aString,aString.mLength);} +nsString& Append(const nsString& aString) {return Append(aString,aString.mLength);} /* @@ -457,14 +450,14 @@ nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLeng * @param aCount -- number of chars to copy * @return number of chars copied */ -nsString2& Append(const nsStr& aString,PRInt32 aCount); -nsString2& Append(const nsString2& aString,PRInt32 aCount); -nsString2& Append(const char* aString,PRInt32 aCount=-1); -nsString2& Append(const PRUnichar* aString,PRInt32 aCount=-1); -nsString2& Append(char aChar); -nsString2& Append(PRUnichar aChar); -nsString2& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 -nsString2& Append(float aFloat); +nsString& Append(const nsStr& aString,PRInt32 aCount); +nsString& Append(const nsString& aString,PRInt32 aCount); +nsString& Append(const char* aString,PRInt32 aCount=-1); +nsString& Append(const PRUnichar* aString,PRInt32 aCount=-1); +nsString& Append(char aChar); +nsString& Append(PRUnichar aChar); +nsString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 +nsString& Append(float aFloat); /* * Copies n characters from this string to given string, @@ -475,7 +468,7 @@ nsString2& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRUint32 Left(nsString2& aCopy,PRInt32 aCount) const; +PRUint32 Left(nsString& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -487,7 +480,7 @@ PRUint32 Left(nsString2& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRUint32 Mid(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount) const; +PRUint32 Mid(nsString& aCopy,PRUint32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -498,7 +491,7 @@ PRUint32 Mid(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRUint32 Right(nsString2& aCopy,PRInt32 aCount) const; +PRUint32 Right(nsString& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -509,7 +502,7 @@ PRUint32 Right(nsString2& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -519,8 +512,8 @@ nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -nsString2& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); -nsString2& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a single char into this string at @@ -530,8 +523,8 @@ nsString2& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -//nsString2& Insert(char aChar,PRUint32 anOffset); -nsString2& Insert(PRUnichar aChar,PRUint32 anOffset); +//nsString& Insert(char aChar,PRUint32 anOffset); +nsString& Insert(PRUnichar aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -541,7 +534,7 @@ nsString2& Insert(PRUnichar aChar,PRUint32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString2& Cut(PRUint32 anOffset,PRInt32 aCount); +nsString& Cut(PRUint32 anOffset,PRInt32 aCount); /********************************************************************** @@ -564,11 +557,22 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * @param aString is substring to be sought in this * @return offset in string, or -1 (kNotFound) */ -PRInt32 Find(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 Find(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + + +/** + * Search for given char within this string + * + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) + */ PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** * This method searches this string for the first character @@ -577,20 +581,9 @@ PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) cons * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this */ -PRInt32 FindCharInSet(const char* aString,PRUint32 anOffset=0) const; -PRInt32 FindCharInSet(const PRUnichar* aString,PRUint32 anOffset=0) const; -PRInt32 FindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; - -/** - * This method searches this string for the last character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 RFindCharInSet(const char* aString,PRUint32 anOffset=0) const; -PRInt32 RFindCharInSet(const PRUnichar* aString,PRUint32 anOffset=0) const; -PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; +PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; /** @@ -599,11 +592,34 @@ PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; * @param aIgnoreCase tells us whether or not to do caseless compare * @return offset in string, or -1 (kNotFound) */ -PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + + +/** + * Search for given char within this string + * + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) + */ PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + +/** + * This method searches this string for the last character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this + */ +PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + /********************************************************************** Comparison methods... @@ -616,7 +632,7 @@ PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) con * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -virtual PRInt32 Compare(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const nsStr &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; @@ -626,7 +642,7 @@ virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRI * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator==(const nsString2 &aString) const; +PRBool operator==(const nsString &aString) const; PRBool operator==(const nsStr &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; @@ -636,7 +652,7 @@ PRBool operator==(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE */ -PRBool operator!=(const nsString2 &aString) const; +PRBool operator!=(const nsString &aString) const; PRBool operator!=(const nsStr &aString) const; PRBool operator!=(const char* aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -646,7 +662,7 @@ PRBool operator!=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<(const nsString2 &aString) const; +PRBool operator<(const nsString &aString) const; PRBool operator<(const nsStr &aString) const; PRBool operator<(const char* aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -656,7 +672,7 @@ PRBool operator<(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>(const nsString2 &aString) const; +PRBool operator>(const nsString &aString) const; PRBool operator>(const nsStr &S) const; PRBool operator>(const char* aString) const; PRBool operator>(const PRUnichar* aString) const; @@ -666,7 +682,7 @@ PRBool operator>(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<=(const nsString2 &aString) const; +PRBool operator<=(const nsString &aString) const; PRBool operator<=(const nsStr &S) const; PRBool operator<=(const char* aString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -676,7 +692,7 @@ PRBool operator<=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>=(const nsString2 &aString) const; +PRBool operator>=(const nsString &aString) const; PRBool operator>=(const nsStr &S) const; PRBool operator>=(const char* aString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -688,20 +704,18 @@ PRBool operator>=(const PRUnichar* aString) const; * optimization. * * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. + * @param aCount -- number of chars to be compared. * @return TRUE if equal */ -PRBool Equals(const nsString2 &aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const char* aString,PRUint32 aCount,PRBool aIgnoreCase) const; -PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const PRUnichar* aString,PRUint32 aCount,PRBool aIgnoreCase) const; +PRBool Equals(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; PRBool Equals(const nsIAtom* anAtom,PRBool aIgnoreCase) const; PRBool Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase=PR_FALSE) const; -PRBool EqualsIgnoreCase(const nsString2& aString) const; -PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; +PRBool EqualsIgnoreCase(const nsString& aString) const; +PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; @@ -730,8 +744,8 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -static void Recycle(nsString2* aString); -static nsString2* CreateString(eCharSize aCharSize=eTwoByte); +static void Recycle(nsString* aString); +static nsString* CreateString(eCharSize aCharSize=eTwoByte); virtual void DebugDump(ostream& aStream) const; @@ -739,8 +753,8 @@ virtual void DebugDump(ostream& aStream) const; }; -extern NS_COM int fputs(const nsString2& aString, FILE* out); -ostream& operator<<(ostream& aStream,const nsString2& aString); +extern NS_COM int fputs(const nsString& aString, FILE* out); +ostream& operator<<(ostream& aStream,const nsString& aString); /************************************************************** @@ -749,36 +763,30 @@ ostream& operator<<(ostream& aStream,const nsString2& aString); If the buffer needs to grow, it gets reallocated on the heap. **************************************************************/ -class NS_COM nsAutoString2 : public nsString2 { +class NS_COM nsAutoString : public nsString { public: - nsAutoString2(eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(nsStr& anExtBuffer,const char* aCString); + nsAutoString(eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); + nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - - nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(char* aCString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); - nsAutoString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(PRUnichar* aString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); - - nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(CSharedStrBuffer& aBuffer); + nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); #ifdef AIX - nsAutoString2(const nsSubsumeStr& aSubsumeStr); // AIX requires a const + nsAutoString(const nsSubsumeStr& aSubsumeStr); // AIX requires a const #else - nsAutoString2(nsSubsumeStr& aSubsumeStr); + nsAutoString(nsSubsumeStr& aSubsumeStr); #endif // AIX - nsAutoString2(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); - virtual ~nsAutoString2(); + nsAutoString(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); + virtual ~nsAutoString(); - nsAutoString2& operator=(const nsString2& aString) {nsString2::operator=(aString); return *this;} - nsAutoString2& operator=(const nsStr& aString) {nsString2::Assign(aString); return *this;} - nsAutoString2& operator=(const nsAutoString2& aString) {nsString2::operator=(aString); return *this;} - nsAutoString2& operator=(const char* aCString) {nsString2::operator=(aCString); return *this;} - nsAutoString2& operator=(char aChar) {nsString2::operator=(aChar); return *this;} - nsAutoString2& operator=(const PRUnichar* aBuffer) {nsString2::operator=(aBuffer); return *this;} - nsAutoString2& operator=(PRUnichar aChar) {nsString2::operator=(aChar); return *this;} + nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} + nsAutoString& operator=(const nsAutoString& aString) {nsString::operator=(aString); return *this;} + nsAutoString& operator=(const char* aCString) {nsString::operator=(aCString); return *this;} + nsAutoString& operator=(char aChar) {nsString::operator=(aChar); return *this;} + nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::operator=(aBuffer); return *this;} + nsAutoString& operator=(PRUnichar aChar) {nsString::operator=(aChar); return *this;} /** * Retrieve the size of this string @@ -803,24 +811,14 @@ public: You should probably not use this class unless you really know what you're doing. ***************************************************************/ -class NS_COM nsSubsumeStr : public nsString2 { +class NS_COM nsSubsumeStr : public nsString { public: - nsSubsumeStr(nsString2& aString); nsSubsumeStr(nsStr& aString); nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; -/*************************************************************** - - ***************************************************************/ -class NS_COM nsCAutoString: public nsAutoString{ -public: - nsCAutoString(const nsString2& aString); - operator const char*() const; -}; - #endif diff --git a/mozilla/xpcom/string/obsolete/nsStr.cpp b/mozilla/xpcom/string/obsolete/nsStr.cpp index 0a0b2ca937a..52b1cc2466f 100644 --- a/mozilla/xpcom/string/obsolete/nsStr.cpp +++ b/mozilla/xpcom/string/obsolete/nsStr.cpp @@ -31,11 +31,13 @@ #include "nsStr.h" #include "bufferRoutines.h" #include "stdio.h" //only used for printf -#include "nsDeque.h" #include "nsCRT.h" +#include "nsDeque.h" static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; +static const char* kCallFindChar = "For better performance, call FindChar() for targets whose length==1."; +static const char* kCallRFindChar = "For better performance, call RFindChar() for targets whose length==1."; //---------------------------------------------------------------------------------------- // The following is a memory agent who knows how to recycled (pool) freed memory... @@ -44,6 +46,8 @@ static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; /************************************************************** Define the char* (pooled) deallocator class... **************************************************************/ + +/* class nsBufferDeallocator: public nsDequeFunctor{ public: virtual void* operator()(void* anObject) { @@ -53,12 +57,6 @@ public: } }; -/** - * - * @update gess10/30/98 - * @param - * @return - */ class nsPoolingMemoryAgent : public nsMemoryAgent{ public: nsPoolingMemoryAgent() { @@ -128,6 +126,8 @@ public: nsDeque* mPools[16]; }; +*/ + static char* gCommonEmptyBuffer=0; /** * @@ -151,7 +151,8 @@ char* GetSharedEmptyBuffer() { } /** - * + * This method initializes all the members of the nsStr structure + * * @update gess10/30/98 * @param * @return @@ -166,7 +167,7 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { } /** - * + * This method initializes all the members of the nsStr structure * @update gess10/30/98 * @param * @return @@ -193,7 +194,7 @@ nsIMemoryAgent* GetDefaultAgent(void){ } /** - * + * This member destroys the memory buffer owned by an nsStr object (if it actually owns it) * @update gess10/30/98 * @param * @return @@ -386,9 +387,10 @@ void nsStr::Truncate(nsStr& aDest,PRUint32 aDestOffset,nsIMemoryAgent* anAgent){ /** - * + * This method forces the given string to upper or lowercase * @update gess1/7/99 - * @param + * @param aDest is the string you're going to change + * @param aToUpper: if TRUE, then we go uppercase, otherwise we go lowercase * @return */ void nsStr::ChangeCase(nsStr& aDest,PRBool aToUpper) { @@ -397,19 +399,6 @@ void nsStr::ChangeCase(nsStr& aDest,PRBool aToUpper) { NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } -/** - * - * @update gess1/7/99 - * @param - * @return - */ -void nsStr::StripChars(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,const char* aCharSet){ - PRUint32 aNewLen=gStripChars[aDest.mCharSize](aDest.mStr,aDestOffset,aCount,aCharSet); - aDest.mLength=aNewLen; - NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); -} - - /** * * @update gess1/7/99 @@ -417,8 +406,43 @@ void nsStr::StripChars(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,const ch * @return */ void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){ - PRUint32 aNewLen=gTrimChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet,aEliminateLeading,aEliminateTrailing); - aDest.mLength=aNewLen; + + if((aDest.mLength>0) && aSet){ + PRInt32 theIndex=-1; + PRInt32 theMax=aDest.mLength; + PRInt32 theSetLen=nsCRT::strlen(aSet); + + if(aEliminateLeading) { + while(++theIndex<=theMax) { + PRUnichar theChar=GetCharAt(aDest,theIndex); + PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE); + if(kNotFound==thePos) + break; + } + if(00) { + PRUnichar theChar=GetCharAt(aDest,theIndex); //read at end now... + PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE); + if(kNotFound0) && (aTarget.mLength>0) && (anOffset0) && (aTarget.mLength>0)){ - int32 theTargetMax=aTarget.mLength; - while(++index<=theMax) { - int32 theSubIndex=-1; + PRInt32 result=kNotFound; + + if((0=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){ + PRInt32 theTargetMax=aTarget.mLength; + while(index<=theMax) { + PRInt32 theSubIndex=-1; PRBool matches=PR_TRUE; while((++theSubIndex0) && (aTarget.mLength>0)){ + if((0=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){ + + nsStr theCopy; + nsStr::Initialize(theCopy,eOneByte); + nsStr::Assign(theCopy,aTarget,0,aTarget.mLength,0); + if(aIgnoreCase){ + nsStr::ChangeCase(theCopy,PR_FALSE); //force to lowercase } - } //while - nsStr::Destroy(theCopy,0); + + PRInt32 theTargetMax=theCopy.mLength; + while(index>=0) { + PRInt32 theSubIndex=-1; + PRBool matches=PR_FALSE; + if(index+theCopy.mLength<=aDest.mLength) { + matches=PR_TRUE; + while((++theSubIndex=0) { - PRUnichar theChar=GetCharAt(aDest,offset); - thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); - if(kNotFound!=thePos) - return offset; - } //while + PRInt32 index=(0<=anOffset) ? anOffset : aDest.mLength; + PRInt32 thePos; + + //note that the search is inverted here. We're scanning aDest, one char at a time + //but doing the search against the given set. That's why we use 0 as the offset below. + if(0=0) { + PRUnichar theChar=GetCharAt(aDest,index); + thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); + if(kNotFound!=thePos) + return index; + } //while + } return kNotFound; } /** - * - * @update gess11/12/98 - * @param + * Compare source and dest strings, up to an (optional max) number of chars + * @param aDest is the first str to compare + * @param aSource is the second str to compare + * @param aCount -- if (-1), then we use length of longer string; if (0aSource=1 */ PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { - int minlen=(aSource.mLength1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + +CSharedStrBuffer::CSharedStrBuffer(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength) { + mBuffer=(char*)aString; + mCharSize=eTwoByte; + mStackBased=aStackBased; + mLength=mCapacity=0; + if(aString && aCapacity>1) { + mCapacity=aCapacity-1; + mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength; + if(mLength>PRInt32(mCapacity)) + mLength=mCapacity; + } +} + + +//---------------------------------------------------------------------------------------- diff --git a/mozilla/xpcom/string/obsolete/nsStr.h b/mozilla/xpcom/string/obsolete/nsStr.h index 07b753a25b0..8757ce92f85 100644 --- a/mozilla/xpcom/string/obsolete/nsStr.h +++ b/mozilla/xpcom/string/obsolete/nsStr.h @@ -48,6 +48,10 @@ enum eCharSize {eOneByte=0,eTwoByte=1}; #define kDefaultCharSize eTwoByte +#define kRadix10 (10) +#define kRadix16 (16) +#define kAutoDetect (100) +#define kRadixUnknown (kAutoDetect+1) const PRInt32 kNotFound = -1; @@ -55,8 +59,23 @@ class nsIMemoryAgent; //---------------------------------------------------------------------------------------- +class NS_COM CSharedStrBuffer { +public: + CSharedStrBuffer(char* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); + CSharedStrBuffer(PRUnichar* aString,PRBool aStackBased,PRUint32 aCapacity,PRInt32 aLength=-1); -struct nsStr { + char* mBuffer; + eCharSize mCharSize; + PRUint32 mCapacity; + PRInt32 mLength; + PRBool mStackBased; + +}; + +//---------------------------------------------------------------------------------------- + + +struct NS_COM nsStr { /** * This method initializes an nsStr for use @@ -169,16 +188,6 @@ struct nsStr { */ static void ChangeCase(nsStr& aDest,PRBool aToUpper); - /** - * This method removes chars (given in aSet) from the given buffer - * - * @update gess 01/04/99 - * @param aString is the buffer to be manipulated - * @param aDestOffset is starting pos in buffer for manipulation - * @param aCount is the number of chars to compare - * @param aSet tells us which chars to remove from given buffer - */ - static void StripChars(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,const char* aCharSet); /** * This method trims chars (given in aSet) from the edges of given buffer @@ -201,7 +210,7 @@ struct nsStr { * @param aEliminateLeading tells us whether to strip chars from the start of the buffer * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer */ - static void CompressSet(nsStr& aDest,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing); + static void CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing); /** * This method compares the data bewteen two nsStr's @@ -225,13 +234,13 @@ struct nsStr { * @param anOffset tells us where in the dest string to start searching * @return the index of the source (substr) in dest, or -1 (kNotFound) if not found. */ - static PRInt32 FindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 FindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset); + static PRInt32 FindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 FindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset); - static PRInt32 RFindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset); - static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset); + static PRInt32 RFindSubstr(const nsStr& aDest,const nsStr& aSource, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRInt32 anOffset); + static PRInt32 RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRInt32 anOffset); PRUint32 mLength; diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp index d0d86da25a5..af2c0971ae8 100644 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ b/mozilla/xpcom/string/obsolete/nsString.cpp @@ -1,3 +1,4 @@ + /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public License @@ -14,38 +15,28 @@ * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. - */ + */ #include #include -#include +#include #include #include "nsString.h" -#include "nsCRT.h" #include "nsDebug.h" -#include "prprf.h" +#include "nsCRT.h" +#include "nsDeque.h" + +#ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" -#include "nsStr.h" - -#include "nsUnicharUtilCIID.h" -#include "nsIServiceManager.h" -#include "nsICaseConversion.h" - -const PRInt32 kGrowthDelta = 8; -PRUnichar gBadChar = 0; -const char* kOutOfBoundsError = "Error: out of bounds"; -const char* kNullPointerError = "Error: unexpected null ptr"; -const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; - -PRUnichar kCommonEmptyBuffer[100]; //shared by all strings; NEVER WRITE HERE!!! - -#ifdef RICKG_DEBUG -PRBool nsString1::mSelfTested = PR_FALSE; #endif +static const char* kNullPointerError = "Error: unexpected null ptr"; +static const char* kWhitespace="\b\t\r\n "; -#define NOT_USED 0xfffd + + +#define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { NOT_USED, @@ -99,67 +90,26 @@ public: } } }; -static CTableConstructor gTableConstructor; -//---- XPCOM code to connect with UnicharUtil -class HandleCaseConversionShutdown2 : public nsIShutdownListener { -public : - NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); - HandleCaseConversionShutdown2(void) { NS_INIT_REFCNT(); } - virtual ~HandleCaseConversionShutdown2(void) {} - NS_DECL_ISUPPORTS -}; -static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); -static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); - -static nsICaseConversion * gCaseConv = NULL; - -static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); -NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown2, kIShutdownListenerIID); - -nsresult -HandleCaseConversionShutdown2::OnShutdown(const nsCID& cid, nsISupports* service) -{ - if (cid.Equals(kUnicharUtilCID)) { - NS_ASSERTION(service == gCaseConv, "wrong service!"); - gCaseConv->Release(); - gCaseConv = NULL; +static void CSubsume(nsStr& aDest,nsStr& aSource){ + if(aSource.mStr && aSource.mLength) { + if(aSource.mOwnsBuffer){ + nsStr::Destroy(aDest,0); + aDest.mStr=aSource.mStr; + aDest.mLength=aSource.mLength; + aDest.mCharSize=aSource.mCharSize; + aDest.mCapacity=aSource.mCapacity; + aDest.mOwnsBuffer=aSource.mOwnsBuffer; + aSource.mOwnsBuffer=PR_FALSE; + aSource.mStr=0; } - return NS_OK; -} - -static HandleCaseConversionShutdown2* gListener = NULL; - -static void StartUpCaseConversion() -{ - nsresult err; - - if ( NULL == gListener ) - { - gListener = new HandleCaseConversionShutdown2(); - gListener->AddRef(); + else{ + nsStr::Assign(aDest,aSource,0,aSource.mLength,0); } - err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, - (nsISupports**) &gCaseConv, gListener); + } + else nsStr::Truncate(aDest,0,0); } -static void CheckCaseConversion() -{ - if(NULL == gCaseConv ) - StartUpCaseConversion(); - - // NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); - -} - -/*********************************************************************** - IMPLEMENTATION NOTES: - - Man I hate writing string classes. - You'd think after about a qintrillion lines of code have been written, - that no poor soul would ever have to do this again. Sigh. - ***********************************************************************/ - /** @@ -168,102 +118,83 @@ static void CheckCaseConversion() * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString1::nsString1() { - - NS_ASSERTION(kCommonEmptyBuffer[0]==0,kFoolMsg); - mLength = mCapacity = 0; - mStr = kCommonEmptyBuffer; - -#ifdef RICKG_DEBUG - if(!mSelfTested) { - mSelfTested=PR_TRUE; - SelfTest(); - } -#endif +nsCString::nsCString(nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); } - - /** - * This constructor accepts an isolatin string - * @update gess7/30/98 + * This constructor accepts an ascii string + * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString1::nsString1(const char* aCString) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - if(aCString) { - PRInt32 len=strlen(aCString); - EnsureCapacityFor(len); - this->SetString(aCString,len); - } +nsCString::nsCString(const char* aCString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + Assign(aCString,aLength); } +/** + * This constructor accepts an ascii string + * @update gess 1/4/99 + * @param aCString is a ptr to a 1-byte cstr + */ +nsCString::nsCString(const PRUnichar* aString,PRInt32 aLength,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + Assign(aString,aLength); +} /** * This is our copy constructor - * @update gess7/30/98 - * @param reference to another nsString1 + * @update gess 1/4/99 + * @param reference to another nsCString */ -nsString1::nsString1(const nsString1 &aString) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - if(aString.mLength) { - EnsureCapacityFor(aString.mLength); - this->SetString(aString.mStr,aString.mLength); - } +nsCString::nsCString(const nsStr &aString,nsIMemoryAgent* anAgent) : mAgent(anAgent) { + nsStr::Initialize(*this,eOneByte); + nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } - /** - * Constructor from a unicode string - * @update gess7/30/98 - * @param anicodestr pts to a unicode string + * This is our copy constructor + * @update gess 1/4/99 + * @param reference to another nsCString */ -nsString1::nsString1(const PRUnichar* aUnicodeStr) { - mLength=mCapacity=0; - mStr = kCommonEmptyBuffer; - - PRInt32 len=(aUnicodeStr) ? nsCRT::strlen(aUnicodeStr) : 0; - if(len>0) { - EnsureCapacityFor(len); - this->SetString(aUnicodeStr,len); - } - +nsCString::nsCString(const nsCString& aString) :mAgent(aString.mAgent) { + nsStr::Initialize(*this,aString.mCharSize); + nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } +/** + * construct off a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString + */ +nsCString::nsCString(nsSubsumeCStr& aSubsumeStr) :mAgent(0) { + CSubsume(*this,aSubsumeStr); +} /** * Destructor - * @update gess7/30/98 + * Make sure we call nsStr::Destroy. */ -nsString1::~nsString1() -{ - if(mStr && (mStr!=kCommonEmptyBuffer)) - delete [] mStr; - mStr=0; - mCapacity=mLength=0; +nsCString::~nsCString() { + nsStr::Destroy(*this,mAgent); } +void nsCString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity << mCharSize); +#endif +} /** * This method truncates this string to given length. * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param anIndex -- new length of string * @return nada */ -void nsString1::Truncate(PRInt32 anIndex) { - if((anIndex>-1) && (anIndexAdd(sizeof(*this)); - aHandler->Add(mCapacity * sizeof(chartype)); +void nsCString::Truncate(PRInt32 anIndex) { + nsStr::Truncate(*this,anIndex,mAgent); } /** @@ -273,10 +204,10 @@ void nsString1::SizeOf(nsISizeOfHandler* aHandler) const * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString1::IsOrdered(void) const { +PRBool nsCString::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { - PRInt32 theIndex; + PRUint32 theIndex; for(theIndex=1;theIndexmStr[theIndex]) { result=PR_FALSE; @@ -287,445 +218,396 @@ PRBool nsString1::IsOrdered(void) const { return result; } -/** - * This method gets called when the internal buffer needs - * to grow to a given size. - * @update gess 3/30/98 - * @param aNewLength -- new capacity of string - * @return void - */ -void nsString1::EnsureCapacityFor(PRInt32 aNewLength) -{ - PRInt32 newCapacity; - - if (mCapacity > 64) { - // When the string starts getting large, double the capacity as we - // grow. - newCapacity = mCapacity * 2; - if (newCapacity < aNewLength) { - newCapacity = mCapacity + aNewLength; - } - } else { - // When the string is small, keep it's capacity a multiple of - // kGrowthDelta - PRInt32 size =aNewLength+kGrowthDelta; - newCapacity=size-(size % kGrowthDelta); - } - - if(mCapacity 0) { - nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype) + sizeof(chartype)); - } - if(mStr && (mStr!=kCommonEmptyBuffer)) - delete [] mStr; - mStr = temp; - mStr[mLength]=0; - } -} - /** - * Call this method if you want string to report a shorter length. - * @update gess7/30/98 + * Call this method if you want to force the string to a certain capacity + * @update gess 1/4/99 * @param aLength -- contains new length for mStr * @return */ -void nsString1::SetLength(PRInt32 aLength) { - if(aLength>mLength) { - EnsureCapacityFor(aLength); - nsCRT::zero(mStr + mLength, (aLength - mLength) * sizeof(chartype)); +void nsCString::SetCapacity(PRUint32 aLength) { + if(aLength>mCapacity) { + GrowCapacity(*this,aLength,mAgent); } - if((aLength>=0) && (aLengthToLower(mStr, mStr, mLength); - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* cp = mStr; - chartype* end = cp + mLength; - while (cp < end) { - chartype ch = *cp; - if ((ch >= 'A') && (ch <= 'Z')) { - *cp = 'a' + (ch - 'A'); - } - cp++; - } - -} - -/** - * Converts all chars in internal string to upper - * @update gess 7/27/98 - */ -void nsString1::ToUpperCase() -{ - // I18N code begin - CheckCaseConversion(); - if(gCaseConv) { - nsresult err = gCaseConv->ToUpper(mStr, mStr, mLength); - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* cp = mStr; - chartype* end = cp + mLength; - while (cp < end) { - chartype ch = *cp; - if ((ch >= 'a') && (ch <= 'z')) { - *cp = 'A' + (ch - 'a'); - } - cp++; - } - -} +/********************************************************************** + Lexomorphic transforms... + *********************************************************************/ /** * Converts all chars in given string to UCS2 */ -void nsString1::ToUCS2(PRInt32 aStartOffset){ - if(aStartOffsetToLower(mStr, aOut.mStr, mLength); - (*(aOut.mStr+mLength)) = 0; - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - - chartype* to = aOut.mStr; - chartype* from = mStr; - chartype* end = from + mLength; - while (from < end) { - chartype ch = *from++; - if ((ch >= 'A') && (ch <= 'Z')) { - ch = 'a' + (ch - 'A'); - } - *to++ = ch; - } - *to = 0; +void nsCString::ToLowerCase(nsCString& aString) const { + aString=*this; + nsStr::ChangeCase(aString,PR_FALSE); } /** * Converts chars in this to lowercase, and * stores them in a given output string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aOut is a string to contain result */ -void nsString1::ToUpperCase(nsString1& aOut) const -{ - aOut.EnsureCapacityFor(mLength); - aOut.mLength = mLength; - - // I18N code begin - CheckCaseConversion(); - if(gCaseConv) { - nsresult err = gCaseConv->ToUpper(mStr, aOut.mStr, mLength); - (*(aOut.mStr+mLength)) = 0; - if( NS_SUCCEEDED(err)) - return; - } - // I18N code end - - // somehow UnicharUtil return failed, fallback to the old ascii only code - chartype* to = aOut.mStr; - chartype* from = mStr; - chartype* end = from + mLength; - while (from < end) { - chartype ch = *from++; - if ((ch >= 'a') && (ch <= 'z')) { - ch = 'A' + (ch - 'a'); - } - *to++ = ch; - } - *to = 0; +void nsCString::ToUpperCase(nsCString& aString) const { + aString=*this; + nsStr::ChangeCase(aString,PR_TRUE); } +/** + * This method is used to remove all occurances of the + * characters found in aSet from this string. + * + * @update gess 01/04/99 + * @param aSet -- characters to be cut from this + * @return *this + */ +nsCString& nsCString::StripChar(PRUnichar aChar){ + + PRInt32 theIndex=FindChar(aChar,PR_FALSE,0); + while(kNotFound= 0) { - *to++ = *from++; - } - *to++ = 0; - return rv; +PRUnichar* nsCString::ToNewUnicode() const { + nsString temp(mStr); + temp.SetCapacity(8); + PRUnichar* result=temp.mUStr; + temp.mStr=0; + temp.mOwnsBuffer=PR_FALSE; + return result; } /** - * Copies contents of this onto given string. - * @update gess 7/27/98 - * @param aString to hold copy of this - * @return nada. - */ -void nsString1::Copy(nsString1& aString) const -{ - aString.mLength = 0; - aString.Append(mStr, mLength); -} - -/** - * - * @update gess 7/27/98 + * Copies contents of this string into he given buffer + * @update gess 01/04/99 * @param * @return */ -char* nsString1::ToCString(char* aBuf, PRInt32 aBufLength) const -{ - aBufLength--; // leave room for the \0 - PRInt32 len = (mLength > aBufLength) ? aBufLength : mLength; - char* to = aBuf; - chartype* from = mStr; - while (--len >= 0) { - *to++ = char(*from++); +char* nsCString::ToCString(char* aBuf, PRUint32 aBufLength,PRUint32 anOffset) const{ + if(aBuf) { + CSharedStrBuffer theSB(aBuf,PR_FALSE,aBufLength,0); + nsCAutoString temp(theSB); + temp.Assign(*this); + temp.mStr=0; } - *to++ = '\0'; return aBuf; } /** * Perform string to float conversion. - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aErrorCode will contain error if one occurs * @return float rep of string value */ -float nsString1::ToFloat(PRInt32* aErrorCode) const -{ - char buf[40]; +float nsCString::ToFloat(PRInt32* aErrorCode) const { + char buf[100]; if (mLength > PRInt32(sizeof(buf)-1)) { *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; return 0.0f; @@ -741,67 +623,56 @@ float nsString1::ToFloat(PRInt32* aErrorCode) const /** * Perform numeric string to int conversion with given radix. + * NOTE: 1. This method mandates that the string is well formed. + * 2. This method will return an error if the string you give + contains chars outside the range for the specified radix. + * @update gess 10/01/98 * @param aErrorCode will contain error if one occurs * @param aRadix tells us what base to expect the string in. - * @return int rep of string value; aErrorCode gets set too: NS_OK, NS_ERROR_ILLEGAL_VALUE + * @return int rep of string value */ -PRInt32 nsString1::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { - PRInt32 result = 0; - PRInt32 decPt=Find(PRUnichar('.'),0); - PRUnichar* cp = (-1==decPt) ? mStr + mLength-1 : mStr+decPt-1; - char digit=0; - PRUnichar theChar; -// PRInt32 theShift=0; - PRInt32 theMult=1; +PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { - *aErrorCode = (0= mStr) { - theChar = *cp; - if((theChar>='0') && (theChar<='9')){ - break; - } - else if((theChar>='a') && (theChar<='f')) { - break; - } - else if((theChar>='A') && (theChar<='F')) { - break; - } - cp--; - } + *anErrorCode = NS_OK; //now iterate the numeric chars and build our result - while(cp>=mStr) { - theChar=*cp--; + char theChar=0; + char theDigit=0; + while(--cp>=aString.mStr){ + char theChar=*cp; if((theChar>='0') && (theChar<='9')){ - digit=theChar-'0'; - } - else if((theChar>='a') && (theChar<='f')) { - digit=(theChar-'a')+10; + theDigit=theChar-'0'; } else if((theChar>='A') && (theChar<='F')) { - digit=(theChar-'A')+10; + if(10==aRadix){ + *anErrorCode=NS_ERROR_ILLEGAL_VALUE; + result=0; + break; + } + theDigit=(theChar-'A')+10; } else if('-'==theChar) { result=-result; break; } - else if(('+'==theChar) || (' '==theChar) || ('#'==theChar)) { //stop in a good state if you see this... + else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this... break; } - else if((('x'==theChar) || ('X'==theChar)) && (16==aRadix)) { - //stop in a good state. - break; - } - else{ - *aErrorCode=NS_ERROR_ILLEGAL_VALUE; + else { + //we've encountered a char that's not a legal number or sign + *anErrorCode=NS_ERROR_ILLEGAL_VALUE; result=0; break; } - result+=digit*theMult; + + result+=theDigit*theMult; theMult*=aRadix; } @@ -809,275 +680,296 @@ PRInt32 nsString1::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { } /** - * assign given PRUnichar* to this string - * @update gess 7/27/98 - * @param PRUnichar: buffer to be assigned to this - * @return this + * Call this method to extract the rightmost numeric value from the given + * 1-byte input string, and simultaneously determine the radix. + * NOTE: This method mandates that the string is well formed. + * Leading and trailing gunk should be removed, and the case upper. + * @update gess 10/01/98 + * @param anInputString contains orig string + * @param anOutString contains numeric portion copy of input string + * @param aRadix (an out parm) tells the caller what base we think the string is in. + * @return non-zero error code if this string is non-numeric */ -nsString1& nsString1::SetString(const PRUnichar* aStr,PRInt32 aLength) { - if((0 == aLength) || (nsnull == aStr)) { - mLength=0; - if (nsnull != mStr) { - mStr[0]=0; +PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { + + aString.ToUpperCase(); + + PRInt32 decPt=nsStr::FindChar(aString,'.',PR_TRUE,0); + char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1; + + aRadix=kRadixUnknown; //assume for starters... + + // Skip trailing non-numeric... + while (cp >= aString.mStr) { + if((*cp>='0') && (*cp<='9')){ + if(kRadixUnknown==aRadix) + aRadix=kRadix10; + break; } - - } else { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aStr) : aLength; - if(mCapacity<=len) - EnsureCapacityFor(len); - nsCRT::memcpy(mStr,aStr,len*sizeof(chartype)); - mLength=len; - mStr[mLength]=0; + else if((*cp>='A') && (*cp<='F')) { + aRadix=16; + break; + } + cp--; } + aString.Truncate(cp-aString.mStr+1); - return *this; -} + //ok, now scan through chars until you find the start of this number... + //we delimit the number by the presence of: +,-,#,X - -/** - * assign given char* to this string - * @update gess 7/27/98 - * @param aCString: buffer to be assigned to this - * @return this - */ -nsString1& nsString1::SetString(const char* aCString,PRInt32 aLength) { - if(aCString!=0) { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aCString) : aLength; - if(mCapacity<=len) - EnsureCapacityFor(len); - const unsigned char* from = (const unsigned char*) aCString; - const unsigned char* end = from + len; - PRUnichar* dst = mStr; - while (from < end) { - *dst++ = PRUnichar(*from++); + // Skip trailing non-numeric... + cp++; + while (--cp >= aString.mStr) { + if((*cp>='0') && (*cp<='9')){ + continue; + } + else if((*cp>='A') && (*cp<='F')) { + continue; + } + else if((*cp=='-') || (*cp=='+')){ + break; + } + else { + if(('#'==(*cp)) || ('X'==(*cp))) + aRadix=kRadix16; + cp++; //move back by one + break; } - mLength=len; - mStr[mLength]=0; - } - else { - mLength=0; //This little bit of code handles the case - mStr[0]=0; //where some blockhead hands us a null string } + if(cp>aString.mStr) + aString.Cut(0,cp-aString.mStr); + PRInt32 result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; - return *this; + return result; } + /** - * assign given char* to this string - * @update gess 7/27/98 - * @param aCString: buffer to be assigned to this - * @return this + * This method tries to autodetect that radix given a string + * @update gess 10/01/98 + * @return 10,16,or 0 (meaning I don't know) */ -nsString1& nsString1::SetString(const nsString1& aString) { - - if(mCapacity<=aString.mLength) - EnsureCapacityFor(aString.mLength); - PRUnichar* from = (PRUnichar*) aString.mStr; - PRUnichar* end = from + aString.mLength; - PRUnichar* dst = (PRUnichar*) mStr; - while(fromSetString(aStr); +PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { + + //copy chars to local buffer -- step down from 2 bytes to 1 if necessary... + nsCAutoString theString(*this); + PRUint32 theRadix=aRadix; + PRInt32 result=0; + + *anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead + if(NS_OK==*anErrorCode){ + if(kAutoDetect==aRadix) + aRadix=theRadix; + if((kRadix10==aRadix) || (kRadix16==aRadix)) + result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one. + else *anErrorCode=NS_ERROR_ILLEGAL_VALUE; + } + + return result; } +/********************************************************************** + String manipulation methods... + *********************************************************************/ + + /** * assign given string to this one - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aString: string to be added to this * @return this */ -nsString1& nsString1::operator=(const nsString1& aString) { - return this->SetString(aString); +nsCString& nsCString::Assign(const nsStr& aString,PRInt32 aCount) { + if(this!=&aString){ + nsStr::Truncate(*this,0,0); + if(-1==aCount) aCount=aString.mLength; + nsStr::Assign(*this,aString,0,aCount,mAgent); + } + return *this; } - - + /** * assign given char* to this string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aCString: buffer to be assigned to this * @return this */ -nsString1& nsString1::operator=(const char* aCString) { - return SetString(aCString); +nsCString& nsCString::Assign(const char* aCString,PRInt32 aCount) { + nsStr::Truncate(*this,0,0); + if(aCString){ + Append(aCString,aCount); + } + return *this; } - /** - * assign given char to this string - * @update gess 7/27/98 - * @param aChar: char to be assignd to this + * assign given unichar* to this string + * @update gess 01/04/99 + * @param aString: buffer to be assigned to this * @return this */ -nsString1& nsString1::operator=(char aChar) { - return this->operator=(PRUnichar(aChar)); +nsCString& nsCString::Assign(const PRUnichar* aString,PRInt32 aCount) { + nsStr::Truncate(*this,0,0); + if(aString){ + Append(aString,aCount); + } + return *this; } /** * assign given char to this string - * @update gess 7/27/98 + * @update gess 01/04/99 * @param aChar: char to be assignd to this * @return this */ -nsString1& nsString1::operator=(PRUnichar aChar) { - if(mCapacity<1) - EnsureCapacityFor(kGrowthDelta); - mStr[0]=aChar; - mLength=1; - mStr[mLength]=0; - return *this; -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const nsString1& aString,PRInt32 aLength) { - return Append(aString.mStr,aString.mLength); -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const char* aCString,PRInt32 aLength) { - - if(aCString!=0) { - PRInt32 len=(aLength<0) ? strlen(aCString) : aLength; - if(mLength+len >= mCapacity) { - EnsureCapacityFor(mLength+len); - } - const unsigned char* from = (const unsigned char*) aCString; - const unsigned char* end = from + len; - PRUnichar* to = mStr + mLength; - while (from < end) { - *to++ = PRUnichar(*from++); - } - mLength+=len; - mStr[mLength]=0; - } - return *this; -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(char aChar) { - return Append(PRUnichar(aChar)); -} - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(const PRUnichar* aString,PRInt32 aLength) { - - if(aString!=0) { - PRInt32 len=(aLength<0) ? nsCRT::strlen(aString) : aLength; - if(mLength+len >= mCapacity) { - EnsureCapacityFor(mLength+len); - } - if(len>0) - nsCRT::memcpy(&mStr[mLength],aString,len*sizeof(chartype)); - mLength+=len; - mStr[mLength]=0; - } - return *this; -} - - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::Append(PRUnichar aChar) { - if(mLength < mCapacity) { - mStr[mLength++]=aChar; // the new string len < capacity, so just copy - mStr[mLength]=0; - } - else { // The new string exceeds our capacity - EnsureCapacityFor(mLength+1); - mStr[mLength++]=aChar; - mStr[mLength]=0; - } - return *this; -} - - -/** - * append given string to this string - * @update gess 7/27/98 - * @param aString : string to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const nsString1 &aString) { - return this->Append(aString.mStr,aString.mLength); -} - - -/** - * append given buffer to this string - * @update gess 7/27/98 - * @param aCString: buffer to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const char* aCString) { - return Append(aCString); -} - - -/** - * append given buffer to this string - * @update gess 7/27/98 - * @param aBuffer: buffer to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(const PRUnichar* aBuffer) { - return Append(aBuffer); -} - - -/** - * append given char to this string - * @update gess 7/27/98 - * @param aChar: char to be appended to this - * @return this - */ -nsString1& nsString1::operator+=(PRUnichar aChar) { +nsCString& nsCString::Assign(char aChar) { + nsStr::Truncate(*this,0,0); return Append(aChar); } /** - * - * @update gess 7/27/98 + * assign given char to this string + * @update gess 01/04/99 + * @param aChar: char to be assignd to this + * @return this + */ +nsCString& nsCString::Assign(PRUnichar aChar) { + nsStr::Truncate(*this,0,0); + return Append(aChar); +} + +/** + * WARNING! THIS IS A VERY SPECIAL METHOD. + * This method "steals" the contents of aSource and hands it to aDest. + * Ordinarily a copy is made, but not in this version. + * @update gess10/30/98 * @param * @return */ -nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { +#ifdef AIX +nsCString& nsCString::operator=(const nsSubsumeCStr& aSubsumeString) { + nsSubsumeCStr temp(aSubsumeString); // a temp is needed for the AIX compiler + CSubsume(*this,temp); +#else +nsCString& nsCString::operator=(nsSubsumeCStr& aSubsumeString) { + CSubsume(*this,aSubsumeString); +#endif // AIX + return *this; +} + + +/** + * append given string to this string + * @update gess 01/04/99 + * @param aString : string to be appended to this + * @return this + */ +nsCString& nsCString::Append(const nsStr& aString,PRInt32 aCount) { + if(-1==aCount) aCount=aString.mLength; + if(0=0) && (aChar<128)) { + return Append((char)aChar); + } + return *this; +} + +/** + * + * @update gess 01/04/99 + * @param + * @return + */ +nsCString& nsCString::Append(PRInt32 aInteger,PRInt32 aRadix) { char* fmt = "%d"; if (8 == aRadix) { fmt = "%o"; @@ -1085,27 +977,28 @@ nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { fmt = "%x"; } char buf[40]; - PR_snprintf(buf, sizeof(buf), fmt, aInteger); - Append(buf); - return *this; + // *** XX UNCOMMENT THIS LINE + //PR_snprintf(buf, sizeof(buf), fmt, aInteger); + sprintf(buf,fmt,aInteger); + return Append(buf); } /** * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param * @return */ -nsString1& nsString1::Append(float aFloat){ +nsCString& nsCString::Append(float aFloat){ char buf[40]; - PR_snprintf(buf, sizeof(buf), "%g", aFloat); - Append(buf); - return *this; + // *** XX UNCOMMENT THIS LINE + //PR_snprintf(buf, sizeof(buf), "%g", aFloat); + sprintf(buf,"%g",aFloat); + return Append(buf); } - /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -1116,8 +1009,9 @@ nsString1& nsString1::Append(float aFloat){ * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { - return Mid(aCopy,0,aCount); +PRUint32 nsCString::Left(nsCString& aDest,PRInt32 aCount) const{ + nsStr::Assign(aDest,*this,0,aCount,mAgent); + return aDest.mLength; } /* @@ -1126,27 +1020,14 @@ PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { * * * @update gess 4/1/98 - * @param aCopy -- Receiving string + * @param aDest -- Receiving string * @param aCount -- number of chars to copy * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 nsString1::Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const { - aCopy.Truncate(); - if(anOffsetaCopy.mLength) ? aCopy.mLength : aCount; //don't try to copy more than you are given - if (aCount < 0) aCount = aCopy.mLength; - if(0<=anOffset) { - if(aCount>0) { - - //1st optimization: If you're inserting at end, then simply append! - if(anOffset>=mLength){ - Append(aCopy,aCopy.mLength); - return aCopy.mLength; - } - - if(mLength+aCount >= mCapacity) { - EnsureCapacityFor(mLength+aCount); - } - - PRUnichar* last = mStr + mLength; - PRUnichar* first = mStr + anOffset-1; - PRUnichar* next = mStr + mLength + aCount; - - //Copy rightmost chars, up to offset+aCount... - while(first=mCapacity) { - EnsureCapacityFor(mLength+1); +nsCString& nsCString::Insert(const char* aCString,PRUint32 anOffset,PRInt32 aCount){ + if(aCString){ + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mStr=(char*)aCString; + temp.mLength=nsCRT::strlen(aCString); + if(aCount<0) + aCount=temp.mLength; + if(temp.mLength && (0= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is a valid space character - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if is valid space char - */ -PRBool nsString1::IsSpace(PRUnichar aChar) { - // XXX i18n - if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { - return PR_TRUE; - } - return PR_FALSE; -} - -/** - * Determine if given char is valid digit - * - * @update gess 3/31/98 - * @param aChar is character to be tested - * @return TRUE if char is a valid digit - */PRBool nsString1::IsDigit(PRUnichar aChar) { - // XXX i18n - return PRBool((aChar >= '0') && (aChar <= '9')); -} - - -/** - * This method trims characters found in aTrimSet from - * either end of the underlying string. - * - * @update gess 3/31/98 - * @param aTrimSet -- contains chars to be trimmed from - * both ends - * @return this - */ -nsString1& nsString1::Trim(const char* aTrimSet, - PRBool aEliminateLeading, - PRBool aEliminateTrailing) -{ - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength-1; - PRUnichar* to = mStr; - - //begin by find the first char not in aTrimSet - if(aEliminateLeading) { - while (from < end) { - PRUnichar ch = *from; - if(!strchr(aTrimSet,char(ch))) { - break; - } - from++; - } - } - - //Now, find last char not in aTrimSet - if(aEliminateTrailing) { - while(end> from) { - PRUnichar ch = *end; - if(!strchr(aTrimSet,char(ch))) { - break; - } - end--; - } - } - - //now rewrite your string without unwanted - //leading or trailing characters. - if (from != to) { - while (from <= end) { - *to++ = *from++; - } - } - else { - to = ++end; - } - - *to = '\0'; - mLength = to - mStr; - return *this; -} - -/** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. - * - * @update gess 3/31/98 - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this - */ -nsString1& nsString1::CompressWhitespace( PRBool aEliminateLeading, - PRBool aEliminateTrailing) -{ - Trim(" \r\n\t",aEliminateLeading,aEliminateTrailing); - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength; - PRUnichar* to = from; - - //this code converts /n, /t, /r into normal space ' '; - //it also eliminates runs of whitespace... - while (from < end) { - PRUnichar ch = *from++; - if (IsSpace(ch)) { - *to++ = ' '; - while (from < end) { - ch = *from++; - if (!IsSpace(ch)) { - *to++ = ch; - break; - } - } - } else { - *to++ = ch; - } - } - - *to = '\0'; - mLength = to - mStr; - return *this; -} - -/** - * This method strips whitespace throughout the string - * - * @update gess 7/27/98 - * @return this - */ -nsString1& nsString1::StripWhitespace() -{ - Trim(" \r\n\t"); - return StripChars("\r\t\n"); -} - -/** - * This method is used to replace all occurances of the - * given source char with the given dest char - * - * @param - * @return *this - */ -nsString1& nsString1::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { - - PRUnichar* from = mStr; - PRUnichar* end = mStr + mLength; - - while (from < end) { - PRUnichar ch = *from; - if(ch==aSourceChar) { - *from = aDestChar; - } - from++; - } -return *this; -} - +/********************************************************************** + Searching methods... + *********************************************************************/ + /** * Search for given character within this string. * This method does so by using a binary search, * so your string HAD BETTER BE ORDERED! * - * @update gess 3/25/98 * @param aChar is the unicode char to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { +PRInt32 nsCString::BinarySearch(PRUnichar aChar) const{ PRInt32 low=0; PRInt32 high=mLength-1; while (low <= high) { int middle = (low + high) >> 1; - if (mStr[middle]==aChar) + PRUnichar theChar=GetCharAt(*this,middle); + if (theChar==aChar) return middle; - if (mStr[middle]>aChar) + if (theChar>aChar) high = middle - 1; else low = middle + 1; @@ -1514,64 +1182,60 @@ PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { * @param aCStringBuf - charstr to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString1::Find(const char* aCStringBuf) const{ - NS_ASSERTION(0!=aCStringBuf,kNullPointerError); +PRInt32 nsCString::Find(const char* aCString,PRBool aIgnoreCase,PRInt32 anOffset) const{ PRInt32 result=kNotFound; - if(aCStringBuf) { - PRInt32 len=strlen(aCStringBuf); - if((00;i--){ - char* pos=strchr(anAsciiSet,char(mStr[i])); - if(pos) - return i; - } - } - return kNotFound; -} - -/** - * - * - * @update gess 3/25/98 - * @param - * @return - */ -PRInt32 nsString1::RFindCharInSet(nsString1& aSet,PRInt32 anOffset) const{ - if(aSet.Length()) { - for(PRInt32 i=mLength-1;i>0;i--){ - PRInt32 pos=aSet.Find(mStr[i]); - if(kNotFound!=pos) - return i; - } - } - return kNotFound; -} - -/** - * - * - * @update gess 3/25/98 - * @param - * @return - */ -PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ +PRInt32 nsCString::RFind(const char* aString,PRBool aIgnoreCase,PRInt32 anOffset) const{ NS_ASSERTION(0!=aString,kNullPointerError); + PRInt32 result=kNotFound; if(aString) { - PRInt32 len=nsCRT::strlen(aString); - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],aString,len); - else result=nsCRT::strncmp(&mStr[offset],aString,len); - if(0==result) - return offset; //in this case, 0 means they match - } - } + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mLength=nsCRT::strlen(aString); + temp.mStr=(char*)aString; + result=nsStr::RFindSubstr(*this,temp,aIgnoreCase,anOffset); } - return kNotFound; + return result; +} + +/** + * Search for a given char, starting at given offset + * + * @update gess 3/25/98 + * @param + * @return offset of found char, or -1 (kNotFound) + */ +PRInt32 nsCString::RFind(PRUnichar aChar,PRUint32 anOffset,PRBool aIgnoreCase) const{ + PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset); + return result; +} + + + +/** + * Search for a given char, starting at given offset + * + * @update gess 3/25/98 + * @param + * @return offset of found char, or -1 (kNotFound) + */ +PRInt32 nsCString::RFindChar(PRUnichar aChar,PRBool aIgnoreCase,PRInt32 anOffset) const{ + PRInt32 result=nsStr::RFindChar(*this,aChar,aIgnoreCase,anOffset); + return result; } /** @@ -1697,21 +1371,20 @@ PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ * * @update gess 3/25/98 * @param - * @return + * @return */ -PRInt32 nsString1::RFind(const nsString1& aString,PRBool aIgnoreCase) const{ - PRInt32 len=aString.mLength; - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],aString.mStr,len); - else result=nsCRT::strncmp(&mStr[offset],aString.mStr,len); - if(0==result) - return offset; //in this case, 0 means they match - } +PRInt32 nsCString::RFindCharInSet(const char* aCStringSet,PRInt32 anOffset) const{ + NS_ASSERTION(0!=aCStringSet,kNullPointerError); + + PRInt32 result=kNotFound; + if(aCStringSet) { + nsStr temp; + nsStr::Initialize(temp,eOneByte); + temp.mLength=nsCRT::strlen(aCStringSet); + temp.mStr=(char*)aCStringSet; + result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset); } - return kNotFound; + return result; } /** @@ -1719,316 +1392,313 @@ PRInt32 nsString1::RFind(const nsString1& aString,PRBool aIgnoreCase) const{ * * @update gess 3/25/98 * @param - * @return + * @return */ -PRInt32 nsString1::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=anAsciiSet,kNullPointerError); +PRInt32 nsCString::RFindCharInSet(const PRUnichar* aStringSet,PRInt32 anOffset) const{ + NS_ASSERTION(0!=aStringSet,kNullPointerError); - if(anAsciiSet) { - PRInt32 len=strlen(anAsciiSet); - if((len) && (len<=mLength)) { //only enter if abuffer length is <= mStr length. - for(PRInt32 offset=mLength-len;offset>=0;offset--) { - PRInt32 result=0; - if(aIgnoreCase) - result=nsCRT::strncasecmp(&mStr[offset],anAsciiSet,len); - else result=nsCRT::strncmp(&mStr[offset],anAsciiSet,len); - if(0==result) - return offset; //in this case, 0 means they match - } - } + PRInt32 result=kNotFound; + if(aStringSet) { + nsStr temp; + nsStr::Initialize(temp,eTwoByte); + temp.mLength=nsCRT::strlen(aStringSet); + temp.mUStr=(PRUnichar*)aStringSet; + result=nsStr::RFindCharInSet(*this,temp,PR_FALSE,anOffset); } - return kNotFound; + return result; } /** - * Scans this string backwards for first occurance of - * the given char. + * * * @update gess 3/25/98 * @param - * @return offset of char in string, or -1 (kNotFound) + * @return */ -PRInt32 nsString1::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ - chartype uc=nsCRT::ToUpper(aChar); - for(PRInt32 offset=mLength-1;offset>=0;offset--) - if(aIgnoreCase) { - if(nsCRT::ToUpper(mStr[offset])==uc) - return offset; - } - else if(mStr[offset]==aChar) - return offset; //in this case, 0 means they match - return kNotFound; - +PRInt32 nsCString::RFindCharInSet(const nsStr& aSet,PRInt32 anOffset) const{ + PRInt32 result=nsStr::RFindCharInSet(*this,aSet,PR_FALSE,anOffset); + return result; } + /************************************************************** COMPARISON METHODS... **************************************************************/ + /** * Compares given cstring to this string. - * @update gess 7/27/98 - * @param aCString pts to a cstring + * @update gess 01/04/99 + * @param aCString points to a cstring * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length * @return -1,0,1 */ -PRInt32 nsString1::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { +PRInt32 nsCString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aCount) const { NS_ASSERTION(0!=aCString,kNullPointerError); - if(-1!=aLength) { - - //if you're given a length, use it to determine the max # of bytes to compare. - //In some cases, this can speed up the string comparison. - - int maxlen=(aLength(const nsString1 &S) const {return PRBool(Compare(S)>0);} -PRBool nsString1::operator>(const char *s) const {return PRBool(Compare(s)>0);} -PRBool nsString1::operator>(const PRUnichar *s) const {return PRBool(Compare(s)>0);} -PRBool nsString1::operator<=(const nsString1 &S) const {return PRBool(Compare(S)<=0);} -PRBool nsString1::operator<=(const char *s) const {return PRBool(Compare(s)<=0);} -PRBool nsString1::operator<=(const PRUnichar *s) const {return PRBool(Compare(s)<=0);} -PRBool nsString1::operator>=(const nsString1 &S) const {return PRBool(Compare(S)>=0);} -PRBool nsString1::operator>=(const char *s) const {return PRBool(Compare(s)>=0);} -PRBool nsString1::operator>=(const PRUnichar *s) const {return PRBool(Compare(s)>=0);} +/** + * Compare given nsStr with this cstring. + * + * @param aString is an nsStr instance to be compared + * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length + * @return -1,0,1 + */ +PRInt32 nsCString::Compare(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + return nsStr::Compare(*this,aString,aCount,aIgnoreCase); +} + + +PRBool nsCString::operator==(const nsStr& S) const {return Equals(S);} +PRBool nsCString::operator==(const char* s) const {return Equals(s);} +PRBool nsCString::operator==(const PRUnichar* s) const {return Equals(s);} + +PRBool nsCString::operator!=(const nsStr& S) const {return PRBool(Compare(S)!=0);} +PRBool nsCString::operator!=(const char* s) const {return PRBool(Compare(s)!=0);} +PRBool nsCString::operator!=(const PRUnichar* s) const {return PRBool(Compare(s)!=0);} + +PRBool nsCString::operator<(const nsStr& S) const {return PRBool(Compare(S)<0);} +PRBool nsCString::operator<(const char* s) const {return PRBool(Compare(s)<0);} +PRBool nsCString::operator<(const PRUnichar* s) const {return PRBool(Compare(s)<0);} + +PRBool nsCString::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} +PRBool nsCString::operator>(const char* s) const {return PRBool(Compare(s)>0);} +PRBool nsCString::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} + +PRBool nsCString::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} +PRBool nsCString::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} +PRBool nsCString::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} + +PRBool nsCString::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} +PRBool nsCString::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} +PRBool nsCString::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} + + +PRBool nsCString::EqualsIgnoreCase(const nsStr& aString) const { + return Equals(aString,PR_TRUE); +} + +PRBool nsCString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); +} + +PRBool nsCString::EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); +} /** * Compare this to given string; note that we compare full strings here. * - * @update gess 7/27/98 - * @param aString is the other nsString1 to be compared to + * @update gess 01/04/99 + * @param aString is the other nsCString to be compared to + * @param aIgnoreCase tells us how to treat case + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString1::Equals(const nsString1& aString) const { - if(aString.mLength==mLength) { - PRInt32 result=nsCRT::strcmp(mStr,aString.mStr); - return PRBool(0==result); - } - return PR_FALSE; +PRBool nsCString::Equals(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); + return result; } - /** * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 7/27/98 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. + * + * @param aString is the CString to be compared + * @param aCount tells us how many chars you want to compare starting with start of string + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString1::Equals(const char* aCString,PRInt32 aLength) const{ - NS_ASSERTION(0!=aCString,kNullPointerError); - - if((aLength>0) && (aLength!=mLength)) - return PR_FALSE; - - PRInt32 result=nsCRT::strcmp(mStr,aCString); - return PRBool(0==result); +PRBool nsCString::Equals(const char* aCString,PRBool aIgnoreCase,PRInt32 aCount) const{ + PRInt32 theAnswer=Compare(aCString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); + return result; } - -/** - * Compare this to given atom - * @update gess 7/27/98 - * @param aAtom -- atom to compare to this - * @return TRUE if equal - */ -PRBool nsString1::Equals(const nsIAtom* aAtom) const -{ - NS_ASSERTION(0!=aAtom,kNullPointerError); - PRInt32 result=nsCRT::strcmp(mStr,aAtom->GetUnicode()); - return PRBool(0==result); -} - - -/** - * Compare given strings - * @update gess 7/27/98 - * @param s1 -- first string to be compared - * @param s2 -- second string to be compared - * @return TRUE if equal - */ -PRBool nsString1::Equals(const PRUnichar* s1, const PRUnichar* s2) const { - NS_ASSERTION(0!=s1,kNullPointerError); - NS_ASSERTION(0!=s2,kNullPointerError); - PRBool result=PR_FALSE; - if((s1) && (s2)){ - PRInt32 cmp=nsCRT::strcmp(s1,s2); - result=PRBool(0==cmp); - } +PRBool nsCString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); return result; } /** - * Compares all chars in both strings w/o regard to case - * @update gess 7/27/98 - * @param - * @return + * Determine if given char in valid alpha range + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if in alpha range */ -PRBool nsString1::EqualsIgnoreCase(const nsString1& aString) const{ - if(aString.mLength==mLength) { - PRInt32 result=nsCRT::strcasecmp(mStr,aString.mStr); - return PRBool(0==result); +PRBool nsCString::IsAlpha(PRUnichar aChar) { + // XXX i18n + if (((aChar >= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { + return PR_TRUE; } return PR_FALSE; } +/** + * Determine if given char is a valid space character + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if is valid space char + */ +PRBool nsCString::IsSpace(PRUnichar aChar) { + // XXX i18n + if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { + return PR_TRUE; + } + return PR_FALSE; +} + +/** + * Determine if given char is valid digit + * + * @update gess 3/31/98 + * @param aChar is character to be tested + * @return TRUE if char is a valid digit + */ +PRBool nsCString::IsDigit(PRUnichar aChar) { + // XXX i18n + return PRBool((aChar >= '0') && (aChar <= '9')); +} + + +/************************************************************** + Define the string deallocator class... + **************************************************************/ +#ifndef RICKG_TESTBED +class nsCStringDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + static nsMemoryAgent theAgent; + nsCString* aString= (nsCString*)anObject; + if(aString){ + aString->mAgent=&theAgent; + delete aString; + } + return 0; + } +}; +#endif + +/**************************************************************************** + * This class, appropriately enough, creates and recycles nsCString objects.. + ****************************************************************************/ + + + +#ifndef RICKG_TESTBED +class nsCStringRecycler { +public: + nsCStringRecycler() : mDeque(0) { + } + + ~nsCStringRecycler() { + nsCStringDeallocator theDeallocator; + mDeque.ForEach(theDeallocator); //now delete the strings + } + + void Recycle(nsCString* aString) { + mDeque.Push(aString); + } + + nsCString* CreateString(void){ + nsCString* result=(nsCString*)mDeque.Pop(); + if(!result) + result=new nsCString(); + return result; + } + nsDeque mDeque; +}; +static nsCStringRecycler& GetRecycler(void); + /** * - * @update gess 7/27/98 + * @update gess 01/04/99 * @param * @return */ -PRBool nsString1::EqualsIgnoreCase(const nsIAtom *aAtom) const{ - NS_ASSERTION(0!=aAtom,kNullPointerError); - PRBool result=PR_FALSE; - if(aAtom){ - PRInt32 cmp=nsCRT::strcasecmp(mStr,aAtom->GetUnicode()); - result=PRBool(0==cmp); - } - return result; +nsCStringRecycler& GetRecycler(void){ + static nsCStringRecycler gCRecycler; + return gCRecycler; } - +#endif /** - * Compares given unicode string to this w/o regard to case - * @update gess 7/27/98 - * @param s1 is the unicode string to be compared with this - * @param aLength is the length of s1, not # of bytes to compare - * @return true if full length of both strings are equal (modulo case) - */ -PRBool nsString1::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { - NS_ASSERTION(0!=s1,kNullPointerError); - NS_ASSERTION(0!=s2,kNullPointerError); - PRBool result=PR_FALSE; - if((s1) && (s2)){ - PRInt32 cmp=nsCRT::strcasecmp(s1,s2); - result=PRBool(0==cmp); - } - return result; -} - - -/** - * Compare this to given string w/o regard to case; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an optimization. - * - * @update gess 7/27/98 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString1::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { - NS_ASSERTION(0!=aCString,kNullPointerError); - - if((aLength>0) && (aLength!=mLength)) - return PR_FALSE; - - PRInt32 cmp=nsCRT::strcasecmp(mStr,aCString); - return PRBool(0==cmp); -} - - -/** - * - * @update gess 7/27/98 + * Call this mehod when you're done + * @update gess 01/04/99 * @param * @return */ -void nsString1::DebugDump(ostream& aStream) const { - for(int i=0;i= PRInt32(sizeof(buf))) { + cp = aString.ToNewCString(); + } else { + aString.ToCString(cp, len + 1); + } + if(len>0) + ::fwrite(cp, 1, len, out); + if (cp != buf) { + delete[] cp; + } + return (int) len; +} + + +/*********************************************************************** + IMPLEMENTATION NOTES: AUTOSTRING... + ***********************************************************************/ + + +/** + * Special case constructor, that allows the consumer to provide + * an underlying buffer for performance reasons. + * @param aBuffer points to your buffer + * @param aBufSize defines the size of your buffer + * @param aCurrentLength tells us the current length of the buffer + */ +nsCAutoString::nsCAutoString() : nsCString(){ + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); } /** - * - * @update gess 7/27/98 - * @param - * @return + * construct from external buffer and given string + * @param anExtBuffer describes an external buffer + * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString1::nsAutoString1(const char* aCString) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(aCString); +nsCAutoString::nsCAutoString(const nsCAutoString& aString) : nsCString() { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); + Append(aString); +} + + +/** + * Copy construct from ascii c-string + * @param aCString is a ptr to a 1-byte cstr + */ +nsCAutoString::nsCAutoString(const char* aCString,PRInt32 aLength) : nsCString() { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + mAgent=0; + AddNullTerminator(*this); + Append(aCString,aLength); } /** - * - * @update gess 7/27/98 - * @param - * @return + * Copy construct from ascii c-string + * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString1::nsAutoString1(const nsString1& other) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(other.GetUnicode(),other.Length()); +nsCAutoString::nsCAutoString(CSharedStrBuffer& aBuffer) : nsCString() { + mAgent=0; + if(!aBuffer.mBuffer) { + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + } + else { + nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); + } + AddNullTerminator(*this); //this isn't really needed, but it guarantees that folks don't pass string constants. } /** - * - * @update gess 7/27/98 - * @param - * @return + * Copy construct from uni-string + * @param aString is a ptr to a unistr */ -nsAutoString1::nsAutoString1(PRUnichar aChar) - : nsString1() -{ - INIT_AUTO_STRING(); +nsCAutoString::nsCAutoString(const PRUnichar* aString,PRInt32 aLength) : nsCString() { + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); + Append(aString,aLength); +} + +/** + * Copy construct from an nsCString + * @param + */ +nsCAutoString::nsCAutoString(const nsStr& aString) : nsCString() { + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); + Append(aString); +} + + + +/** + * Copy construct from an nsCString + * @param + */ +nsCAutoString::nsCAutoString(PRUnichar aChar) : nsCString(){ + mAgent=0; + nsStr::Initialize(*this,mBuffer,sizeof(mBuffer)-1,0,eOneByte,PR_FALSE); + AddNullTerminator(*this); Append(aChar); } /** - * - * @update gess 7/27/98 - * @param - * @return + * construct from a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString */ -nsAutoString1::nsAutoString1(const nsAutoString1& other) - : nsString1() -{ - INIT_AUTO_STRING(); - SetString(other.GetUnicode(),other.mLength); +#ifdef AIX +nsCAutoString::nsCAutoString(const nsSubsumeCStr& aSubsumeStr) :nsCString() { + mAgent=0; + nsSubsumeCStr temp(aSubsumeStr); // a temp is needed for the AIX compiler + CSubsume(*this,temp); +#else +nsCAutoString::nsCAutoString( nsSubsumeCStr& aSubsumeStr) :nsCString() { + mAgent=0; + CSubsume(*this,aSubsumeStr); +#endif // AIX } /** - * - * @update gess 7/27/98 - * @param - * @return + * deconstruct the autstring + * @param */ -nsAutoString1::nsAutoString1(const PRUnichar* unicode, PRInt32 uslen) - : nsString1() -{ - INIT_AUTO_STRING(); - Append(unicode, uslen ? uslen : nsCRT::strlen(unicode)); +nsCAutoString::~nsCAutoString(){ } -/** - * - * @update gess 7/27/98 - * @param - * @return - */ -nsAutoString1::~nsAutoString1() -{ - if (mStr == mBuf) { - // Force to null so that baseclass dtor doesn't do damage - mStr = nsnull; - } -} - -/** - * nsAutoString1's buffer growing routine uses a different algorithm - * than nsString1 because the lifetime of the auto string is assumed - * to be shorter. Therefore, we double the size of the buffer each - * time we grow so that (hopefully) we quickly get to the right - * size. - */ -void nsAutoString1::EnsureCapacityFor(PRInt32 aNewLength) { - if (aNewLength > mCapacity) { - PRInt32 size = mCapacity * 2; - if (size < aNewLength) { - size = mCapacity + aNewLength; - } - mCapacity=size; - chartype* temp = new chartype[mCapacity+1]; - if (mLength > 0) { - nsCRT::memcpy(temp, mStr, mLength * sizeof(chartype) + sizeof(chartype)); - } - if ((mStr != mBuf) && (0 != mStr)) { - delete [] mStr; - } - mStr = temp; - } -} - -void -nsAutoString1::SizeOf(nsISizeOfHandler* aHandler) const -{ +void nsCAutoString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); - if (mStr != mBuf) { - aHandler->Add(mCapacity * sizeof(chartype)); - } -} - - -/** - * - * - * @update gess 3/31/98 - * @param - * @return - */ -void nsAutoString1::SelfTest(){ - nsAutoString1 xas("Hello there"); - xas.Append("this string exceeds the max size"); - xas.DebugDump(cout); -} - -/** - * - * @update gess8/8/98 - * @param - * @return - */ -ostream& operator<<(ostream& os,nsAutoString1& aString){ - const PRUnichar* uc=aString.GetUnicode(); - int len=aString.Length(); - for(int i=0;i= PRInt32(sizeof(buf))) { - cp = aString.ToNewCString(); - } else { - aString.ToCString(cp, aLength + 1); - } - if(aLength >0) - ::fwrite(cp, 1, aLength, out); - if (cp != buf) { - delete [] cp; - } - return (int) aLength; -} - - - -/** - * - * @update gess 7/27/98 - * @param - * @return - */ -void nsString1::SelfTest(void) { - -#ifdef RICKG_DEBUG - mSelfTested=PR_TRUE; - - { - nsAutoString1 a("foobar"); - nsAutoString1 b("foo"); - nsAutoString1 c(".5111"); - nsAutoString1 d(" 5"); - PRInt32 result=a.Compare(b); - PRInt32 result2=result; - result=c.ToInteger(&result2); - result=d.ToInteger(&result2); - result2=result; - } - - static const char* kConstructorError = kConstructorError; - static const char* kComparisonError = "Comparision error!"; - static const char* kEqualsError = "Equals error!"; - - - nsAutoString1 as("Hello there"); - as.SelfTest(); - - static const char* temp="hello"; - - //first, let's test the constructors... - nsString1 empty; - empty=""; - empty="xxx"; - empty=""; - - nsString1 a(temp); - nsString1* a_=new nsString1(a); //test copy constructor - nsString1 b("world!"); - - //verify destructor... - delete a_; - a_=0; - - //Let's verify the Length() method... - NS_ASSERTION(5==a.Length(),"Error: constructor probably bad!"); - - //********************************************** - //Let's check out the ACCESSORS... - //********************************************** - - const chartype* p1=a.GetUnicode(); - for(int i=0;itemp3.Length(),kConstructorError); //should be char longer - - nsString1* es1=temp2.ToNewString(); //this should make us a new string - char* es2=temp2.ToNewCString(); - for(i=0;itemp8.Compare(temp9),kComparisonError); - NS_ASSERTION(0temp8,kComparisonError); - NS_ASSERTION(temp9>aaaa,kComparisonError); - - NS_ASSERTION(temp8<=temp8,kComparisonError); - NS_ASSERTION(temp8<=temp9,kComparisonError); - NS_ASSERTION(temp8<=bbbb,kComparisonError); - - NS_ASSERTION(temp9>=temp9,kComparisonError); - NS_ASSERTION(temp9>=temp8,kComparisonError); - NS_ASSERTION(temp9>=aaaa,kComparisonError); - - NS_ASSERTION(temp8.Equals(temp8),kEqualsError); - NS_ASSERTION(temp8.Equals(aaaa),kEqualsError); - - nsString1 temp10(temp8); - temp10.ToUpperCase(); - NS_ASSERTION(temp8.EqualsIgnoreCase(temp10),kEqualsError); - NS_ASSERTION(temp8.EqualsIgnoreCase("AAAA"),kEqualsError); - - - //********************************************** - //Now let's test a few string MANIPULATORS... - //********************************************** - - nsAutoString1 ab("ab"); - nsString1 abcde("cde"); - abcde.Insert(ab,0,2); - nsAutoString1 xxx("xxx"); - abcde.Insert(xxx,2,3); - - temp2.ToUpperCase(); - for(i=0;iAdd(mCapacity << mCharSize); #endif } + +nsSubsumeCStr::nsSubsumeCStr(nsStr& aString) : nsCString() { + CSubsume(*this,aString); +} + +nsSubsumeCStr::nsSubsumeCStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() { + mUStr=aString; + mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; +} + +nsSubsumeCStr::nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsCString() { + mStr=aString; + mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; +} + + diff --git a/mozilla/xpcom/string/obsolete/nsString.h b/mozilla/xpcom/string/obsolete/nsString.h index f3a9f8c9030..521d49015db 100644 --- a/mozilla/xpcom/string/obsolete/nsString.h +++ b/mozilla/xpcom/string/obsolete/nsString.h @@ -20,32 +20,33 @@ /*********************************************************************** MODULE NOTES: - A. There are two philosophies to building string classes: - 1. Hide the underlying buffer & offer API's allow indirect iteration - 2. Reveal underlying buffer, risk corruption, but gain performance - - We chose the second option for performance reasons. - - B Our internal buffer always holds capacity+1 bytes. + This version of the nsString class offers many improvements over the + original version: + 1. Wide and narrow chars + 2. Allocators + 3. Much smarter autostrings + 4. Subsumable strings + 5. Memory pools and recycling ***********************************************************************/ -#ifndef _nsString1 -#define _nsString1 - +#ifndef _nsCString_ +#define _nsCString_ +#include "nsString2.h" #include "prtypes.h" #include "nscore.h" -#include "nsIAtom.h" #include #include +#include "nsCRT.h" #include "nsStr.h" +#include "nsIAtom.h" -#include "nsString2.h" //get new string class -class nsISizeOfHandler; +class NS_COM nsSubsumeCStr; + +class NS_COM nsCString : public nsStr { -class NS_COM nsString1 { public: /** @@ -54,46 +55,74 @@ class NS_COM nsString1 { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString1(); +nsCString(nsIMemoryAgent* anAgent=0); + /** * This constructor accepts an isolatin string - * @param an ascii is a ptr to a 1-byte cstr + * @param aCString is a ptr to a 1-byte cstr */ -nsString1(const char* aCString); +nsCString(const char* aCString,PRInt32 aLength=-1,nsIMemoryAgent* anAgent=0); + +/** + * This constructor accepts a unichar string + * @param aCString is a ptr to a 2-byte cstr + */ +nsCString(const PRUnichar* aString,PRInt32 aLength=1,nsIMemoryAgent* anAgent=0); + +/** + * This is a copy constructor that accepts an nsStr + * @param reference to another nsCString + */ +nsCString(const nsStr&,nsIMemoryAgent* anAgent=0); /** * This is our copy constructor - * @param reference to another nsString1 + * @param reference to another nsCString */ -nsString1(const nsString1&); +nsCString(const nsCString& aString); /** - * Constructor from a unicode string - * @param anicodestr pts to a unicode string + * This constructor takes a subsumestr + * @param reference to subsumestr */ -nsString1(const PRUnichar* aUnicode); +nsCString(nsSubsumeCStr& aSubsumeStr); /** - * Virtual Destructor + * Destructor + * */ -virtual ~nsString1(); - +virtual ~nsCString(); /** * Retrieve the length of this string * @return string length */ -PRInt32 Length() const { return mLength; } +inline PRInt32 Length() const { return (PRInt32)mLength; } +/** + * Retrieve the size of this string + * @return string length + */ +virtual void SizeOf(nsISizeOfHandler* aHandler) const; + + +/** + * Call this method if you want to force a different string capacity + * @update gess7/30/98 + * @param aLength -- contains new length for mStr + * @return + */ +void SetLength(PRUint32 aLength) { + Truncate(aLength); +} /** * Sets the new length of the string. * @param aLength is new string length. * @return nada */ -void SetLength(PRInt32 aLength); - +void SetCapacity(PRUint32 aLength); /** * This method truncates this string to given length. * @@ -103,20 +132,6 @@ void SetLength(PRInt32 aLength); void Truncate(PRInt32 anIndex=0); -/** - * This method gets called when the internal buffer needs - * to grow to a given size. - * @param aNewLength -- new capacity of string - * @return void - */ -virtual void EnsureCapacityFor(PRInt32 aNewLength); - -/** - * - * @param - */ -virtual void SizeOf(nsISizeOfHandler* aHandler) const; - /** * Determine whether or not the characters in this * string are in sorted order. @@ -125,6 +140,17 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const; */ PRBool IsOrdered(void) const; +/** + * Determine whether or not the characters in this + * string are in store as 1 or 2 byte (unicode) strings. + * + * @return TRUE if ordered. + */ +PRBool IsUnicode(void) const { + PRBool result=PRBool(mCharSize==eTwoByte); + return result; +} + /** * Determine whether or not this string has a length of 0 * @@ -138,54 +164,20 @@ PRBool IsEmpty(void) const { Accessor methods... *********************************************************************/ -/** - * Retrieve pointer to internal string value - * @return PRUnichar* to internal string - */ -const PRUnichar* GetUnicode(void) const; +const char* GetBuffer(void) const; -/** - * - * @param - * @return - */ -operator const PRUnichar*() const; -/** - * Retrieve unicode char at given index - * @param offset into string - * @return PRUnichar* to internal string - */ -//PRUnichar operator()(PRInt32 anIndex) const; + /** + * Get nth character. + */ +PRUnichar operator[](PRUint32 anIndex) const; +PRUnichar CharAt(PRUint32 anIndex) const; +PRUnichar First(void) const; +PRUnichar Last(void) const; -/** - * Retrieve reference to unicode char at given index - * @param offset into string - * @return PRUnichar& from internal string - */ -PRUnichar& operator[](PRInt32 anIndex) const; - -/** - * Retrieve reference to unicode char at given index - * @param offset into string - * @return PRUnichar& from internal string - */ -PRUnichar& CharAt(PRInt32 anIndex) const; - -/** - * Retrieve reference to first unicode char in string - * @return PRUnichar from internal string - */ -PRUnichar& First() const; - -/** - * Retrieve reference to last unicode char in string - * @return PRUnichar from internal string - */ -PRUnichar& Last() const; - -PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); +PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); +operator nsStr&() {return *this;} /********************************************************************** String creation methods... @@ -196,95 +188,165 @@ PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString1 operator+(const nsString1& aString); +nsSubsumeCStr operator+(const nsStr& aString); /** * create a new string by adding this to the given buffer. * @param aCString is a ptr to cstring to be added to this * @return newly created string */ -nsString1 operator+(const char* aCString); +nsSubsumeCStr operator+(const char* aCString); + +/** + * create a new string by adding this to the given wide buffer. + * @param aString is a ptr to UC-string to be added to this + * @return newly created string + */ +nsSubsumeCStr operator+(const PRUnichar* aString); /** * create a new string by adding this to the given char. * @param aChar is a char to be added to this * @return newly created string */ -nsString1 operator+(char aChar); - -/** - * create a new string by adding this to the given buffer. - * @param aStr unichar buffer to be added to this - * @return newly created string - */ -nsString1 operator+(const PRUnichar* aBuffer); +nsSubsumeCStr operator+(char aChar); /** * create a new string by adding this to the given char. * @param aChar is a unichar to be added to this * @return newly created string */ -nsString1 operator+(PRUnichar aChar); +nsSubsumeCStr operator+(PRUnichar aChar); -/** - * Converts all chars in internal string to lower - */ -void ToLowerCase(); - -/** - * Converts all chars in given string to lower - */ -void ToLowerCase(nsString1& aString) const; - -/** - * Converts all chars in given string to upper - */ -void ToUpperCase(); +/********************************************************************** + Lexomorphic transforms... + *********************************************************************/ /** * Converts all chars in given string to UCS2 * which ensure that the lower 256 chars are correct. */ -void ToUCS2(PRInt32 aStartOffset); +void ToUCS2(PRUint32 aStartOffset); /** - * Converts all chars in internal string to upper + * Converts chars in this to lowercase + * @update gess 7/27/98 */ -void ToUpperCase(nsString1& aString) const; +void ToLowerCase(); + /** - * Creates a duplicate clone (ptr) of this string. - * @return ptr to clone of this string + * Converts chars in this to lowercase, and + * stores them in aOut + * @update gess 7/27/98 + * @param aOut is a string to contain result */ -nsString1* ToNewString() const; +void ToLowerCase(nsCString& aString) const; /** - * Creates an ascii clone of this string - * NOTE: This string is allocated with new; YOU MUST deallocate with delete[]! - * @return ptr to new c-String string + * Converts chars in this to uppercase + * @update gess 7/27/98 + */ +void ToUpperCase(); + +/** + * Converts chars in this to lowercase, and + * stores them in a given output string + * @update gess 7/27/98 + * @param aOut is a string to contain result + */ +void ToUpperCase(nsCString& aString) const; + + +/** + * This method is used to remove all occurances of the + * characters found in aSet from this string. + * + * @param aSet -- characters to be cut from this + * @return *this + */ +nsCString& StripChars(const char* aSet); +nsCString& StripChar(PRUnichar aChar); + +/** + * This method strips whitespace throughout the string + * + * @return this + */ +nsCString& StripWhitespace(); + +/** + * swaps occurence of 1 string for another + * + * @return this + */ +nsCString& ReplaceChar(PRUnichar aOldChar,PRUnichar aNewChar); +nsCString& ReplaceChar(const char* aSet,PRUnichar aNewChar); + +PRInt32 CountChar(PRUnichar aChar); + +/** + * This method trims characters found in aTrimSet from + * either end of the underlying string. + * + * @param aTrimSet -- contains chars to be trimmed from + * both ends + * @return this + */ +nsCString& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/** + * This method strips whitespace from string. + * You can control whether whitespace is yanked from + * start and end of string as well. + * + * @param aEliminateLeading controls stripping of leading ws + * @param aEliminateTrailing controls stripping of trailing ws + * @return this + */ +nsCString& CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/** + * This method strips whitespace from string. + * You can control whether whitespace is yanked from + * start and end of string as well. + * + * @param aEliminateLeading controls stripping of leading ws + * @param aEliminateTrailing controls stripping of trailing ws + * @return this + */ +nsCString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); + +/********************************************************************** + string conversion methods... + *********************************************************************/ + +/** + * This method constructs a new nsCString on the stack that is a copy + * of this string. + * + */ +nsCString* ToNewString() const; + +/** + * Creates an ISOLatin1 clone of this string + * @return ptr to new isolatin1 string */ char* ToNewCString() const; +/** + * Creates a unicode clone of this string + * @return ptr to new unicode string + */ +PRUnichar* ToNewUnicode() const; + /** * Copies data from internal buffer onto given char* buffer * @param aBuf is the buffer where data is stored * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer */ -char* ToCString(char* aBuf,PRInt32 aBufLength) const; - -/** - * Copies contents of this onto given string. - * @param aString to hold copy of this - * @return nada. - */ -void Copy(nsString1& aString) const; - -/** - * Creates an unichar clone of this string - * @return ptr to new unichar string - */ -PRUnichar* ToNewUnicode() const; +char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const; /** * Perform string to float conversion. @@ -293,147 +355,99 @@ PRUnichar* ToNewUnicode() const; */ float ToFloat(PRInt32* aErrorCode) const; +/** + * Try to derive the radix from the value contained in this string + * @return kRadix10, kRadix16 or kAutoDetect (meaning unknown) + */ +PRUint32 DetermineRadix(void); + /** * Perform string to int conversion. * @param aErrorCode will contain error if one occurs * @return int rep of string value */ -PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; +PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const; + /********************************************************************** String manipulation methods... *********************************************************************/ /** - * assign given PRUnichar* to this string + * Functionally equivalent to assign or operator= + * + */ +nsCString& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsCString& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsCString& SetString(const nsStr& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} + +/** + * assign given string to this string * @param aStr: buffer to be assigned to this * @param alength is the length of the given str (or -1) if you want me to determine its length * @return this */ -nsString1& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); -nsString1& SetString(const char* aCString,PRInt32 aLength=-1); -nsString1& SetString(const nsString1& aString); +nsCString& Assign(const nsStr& aString,PRInt32 aCount=-1); +nsCString& Assign(const char* aString,PRInt32 aCount=-1); +nsCString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); +nsCString& Assign(char aChar); +nsCString& Assign(PRUnichar aChar); /** - * assign given string to this one + * here come a bunch of assignment operators... * @param aString: string to be added to this * @return this */ -nsString1& operator=(const nsString1& aString); +nsCString& operator=(const nsStr& aString) {return Assign(aString);} +nsCString& operator=(const nsCString& aString) {return Assign(aString);} +nsCString& operator=(char aChar) {return Assign(aChar);} +nsCString& operator=(PRUnichar aChar) {return Assign(aChar);} +nsCString& operator=(const char* aCString) {return Assign(aCString);} +nsCString& operator=(const PRUnichar* aString) {return Assign(aString);} +#ifdef AIX +nsCString& operator=(const nsSubsumeCStr& aSubsumeString); // AIX requires a const here +#else +nsCString& operator=(nsSubsumeCStr& aSubsumeString); +#endif /** - * assign given char* to this string - * @param aCString: buffer to be assigned to this - * @return this - */ -nsString1& operator=(const char* aCString); - -/** - * assign given char to this string - * @param aChar: char to be assignd to this - * @return this - */ -nsString1& operator=(char aChar); - -/** - * assign given unichar* to this string - * @param aBuffer: unichar buffer to be assigned to this - * @return this - */ -nsString1& operator=(const PRUnichar* aBuffer); - -/** - * assign given char to this string - * @param aChar: char to be assignd to this - * @return this - */ -nsString1& operator=(PRUnichar aChar); - -/** - * append given string to this string + * Here's a bunch of append mehtods for varying types... * @param aString : string to be appended to this * @return this */ -nsString1& operator+=(const nsString1& aString); +nsCString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} +nsCString& operator+=(const char* aCString) {return Append(aCString);} +//nsCString& operator+=(char aChar){return Append(aChar);} +nsCString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} +nsCString& operator+=(PRUnichar aChar){return Append(aChar);} -/** - * append given buffer to this string - * @param aCString: buffer to be appended to this - * @return this +/* + * Appends n characters from given string to this, + * This version computes the length of your given string + * + * @param aString is the source to be appended to this + * @return number of chars copied */ -nsString1& operator+=(const char* aCString); +nsCString& Append(const nsStr& aString) {return Append(aString,aString.mLength);} +nsCString& Append(const nsCString& aString) {return Append(aString,aString.mLength);} + -/** - * append given buffer to this string - * @param aBuffer: buffer to be appended to this - * @return this +/* + * Appends n characters from given string to this, + * + * @param aString is the source to be appended to this + * @param aCount -- number of chars to copy + * @return number of chars copied */ -nsString1& operator+=(const PRUnichar* aBuffer); - -/** - * append given char to this string - * @param aChar: char to be appended to this - * @return this - */ -nsString1& operator+=(PRUnichar aChar); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const nsString1& aString,PRInt32 aLength=-1); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const char* aCString,PRInt32 aLength=-1); - -/** - * append given string to this string - * @param aString : string to be appended to this - * @return this - */ -nsString1& Append(char aChar); - -/** - * append given unichar buffer to this string - * @param aString : string to be appended to this - * @param alength is the length of the given str (or -1) - if you want me to determine its length - * @return this - */ -nsString1& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1); - -/** - * append given unichar character to this string - * @param aChar is the char to be appended to this - * @return this - */ -nsString1& Append(PRUnichar aChar); - -/** - * Append an integer onto this string - * @param aInteger is the int to be appended - * @param aRadix specifies 8,10,16 - * @return this - */ -nsString1& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16 - -/** - * Append a float value onto this string - * @param aFloat is the float to be appended - * @return this - */ -nsString1& Append(float aFloat); - +nsCString& Append(const nsStr& aString,PRInt32 aCount); +nsCString& Append(const char* aString,PRInt32 aCount=-1); +nsCString& Append(const PRUnichar* aString,PRInt32 aCount=-1); +nsCString& Append(char aChar); +nsCString& Append(PRUnichar aChar); +nsCString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 +nsCString& Append(float aFloat); + /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -443,7 +457,7 @@ nsString1& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; +PRUint32 Left(nsCString& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -455,7 +469,7 @@ PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; +PRUint32 Mid(nsCString& aCopy,PRUint32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -466,7 +480,7 @@ PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; +PRUint32 Right(nsCString& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -477,17 +491,29 @@ PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -PRInt32 Insert(const nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const nsStr& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** - * Insert a single unicode char into this string at + * Insert a given string into this string at * a specified offset. * - * @param aChar char to be inserted into this string + * @param aString* to be inserted into this string * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); +nsCString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsCString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); + +/** + * Insert a single char into this string at + * a specified offset. + * + * @param character to be inserted into this string + * @param anOffset is insert pos in str + * @return the number of chars inserted into this string + */ +//nsCString& Insert(char aChar,PRUint32 anOffset); +nsCString& Insert(PRUnichar aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -497,56 +523,180 @@ PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString1& Cut(PRInt32 anOffset,PRInt32 aCount); +nsCString& Cut(PRUint32 anOffset,PRInt32 aCount); + + +/********************************************************************** + Searching methods... + *********************************************************************/ + +/** + * Search for given character within this string. + * This method does so by using a binary search, + * so your string HAD BETTER BE ORDERED! + * + * @param aChar is the unicode char to be found + * @return offset in string, or -1 (kNotFound) + */ +PRInt32 BinarySearch(PRUnichar aChar) const; /** - * This method is used to remove all occurances of the - * characters found in aSet from this string. + * Search for given substring within this string * - * @param aSet -- characters to be cut from this - * @return *this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @return offset in string, or -1 (kNotFound) */ -nsString1& StripChars(const char* aSet); +PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** - * This method is used to replace all occurances of the - * given source char with the given dest char + * Search for given char within this string * - * @param - * @return *this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) */ -nsString1& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); +PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** - * This method strips whitespace throughout the string - * - * @return this + * This method searches this string for the first character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this */ -nsString1& StripWhitespace(); +PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + /** - * This method trims characters found in aTrimSet from - * either end of the underlying string. - * - * @param aTrimSet -- contains chars to be trimmed from - * both ends - * @return this + * This methods scans the string backwards, looking for the given string + * @param aString is substring to be sought in this + * @param aIgnoreCase tells us whether or not to do caseless compare + * @return offset in string, or -1 (kNotFound) */ -nsString1& Trim(const char* aSet, - PRBool aEliminateLeading=PR_TRUE, - PRBool aEliminateTrailing=PR_TRUE); +PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + /** - * This method strips whitespace from string. - * You can control whether whitespace is yanked from - * start and end of string as well. + * Search for given char within this string * - * @param aEliminateLeading controls stripping of leading ws - * @param aEliminateTrailing controls stripping of trailing ws - * @return this + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) */ -nsString1& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, - PRBool aEliminateTrailing=PR_TRUE); +PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + +/** + * This method searches this string for the last character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this + */ +PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + + + +/********************************************************************** + Comparison methods... + *********************************************************************/ + +/** + * Compares a given string type to this string. + * @update gess 7/27/98 + * @param S is the string to be compared + * @param aIgnoreCase tells us how to treat case + * @return -1,0,1 + */ +virtual PRInt32 Compare(const nsStr &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; + +/** + * These methods compare a given string type to this one + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator==(const nsStr &aString) const; +PRBool operator==(const char *aString) const; +PRBool operator==(const PRUnichar* aString) const; + +/** + * These methods perform a !compare of a given string type to this + * @param aString is the string to be compared to this + * @return TRUE + */ +PRBool operator!=(const nsStr &aString) const; +PRBool operator!=(const char* aString) const; +PRBool operator!=(const PRUnichar* aString) const; + +/** + * These methods test if a given string is < than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator<(const nsStr &aString) const; +PRBool operator<(const char* aString) const; +PRBool operator<(const PRUnichar* aString) const; + +/** + * These methods test if a given string is > than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator>(const nsStr &S) const; +PRBool operator>(const char* aString) const; +PRBool operator>(const PRUnichar* aString) const; + +/** + * These methods test if a given string is <= than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator<=(const nsStr &S) const; +PRBool operator<=(const char* aString) const; +PRBool operator<=(const PRUnichar* aString) const; + +/** + * These methods test if a given string is >= than this + * @param aString is the string to be compared to this + * @return TRUE or FALSE + */ +PRBool operator>=(const nsStr &S) const; +PRBool operator>=(const char* aString) const; +PRBool operator>=(const PRUnichar* aString) const; + +/** + * Compare this to given string; note that we compare full strings here. + * The optional length argument just lets us know how long the given string is. + * If you provide a length, it is compared to length of this string as an + * optimization. + * + * @param aString -- the string to compare to this + * @param aCount -- number of chars in given string you want to compare + * @return TRUE if equal + */ +PRBool Equals(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; + +PRBool EqualsIgnoreCase(const nsStr& aString) const; +PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; +PRBool EqualsIgnoreCase(const PRUnichar* aString,PRInt32 aCount=-1) const; + /** * Determine if given char is a valid space character @@ -572,248 +722,83 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -/********************************************************************** - Searching methods... - *********************************************************************/ - -/** - * Search for given character within this string. - * This method does so by using a binary search, - * so your string HAD BETTER BE ORDERED! - * - * @param aChar is the unicode char to be found - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 BinarySearch(PRUnichar aChar) const; +static void Recycle(nsCString* aString); +static nsCString* CreateString(void); -/** - * Search for given substring within this string - * - * @param aString is substring to be sought in this - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 Find(const char* aString) const; -PRInt32 Find(const PRUnichar* aString) const; -PRInt32 Find(const nsString1& aString) const; +virtual void DebugDump(ostream& aStream) const; -/** - * Search for given char within this string - * - * @param aChar - char to be found - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const; + nsIMemoryAgent* mAgent; -/** - * This method searches this string for the first character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 FindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; - -/** - * This method searches this string for the last character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 RFindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; - - -/** - * This methods scans the string backwards, looking for the given string - * @param aString is substring to be sought in this - * @param aIgnoreCase tells us whether or not to do caseless compare - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString1& aString,PRBool aIgnoreCase=PR_FALSE) const; - -/** - * This methods scans the string backwards, looking for the given char - * @param char is the char to be sought in this - * @param aIgnoreCase tells us whether or not to do caseless compare - * @return offset in string, or -1 (kNotFound) - */ -PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const; - -/********************************************************************** - Comparison methods... - *********************************************************************/ - -/** - * Compares a given string type to this string. - * @update gess 7/27/98 - * @param S is the string to be compared - * @param aIgnoreCase tells us how to treat case - * @return -1,0,1 - */ -virtual PRInt32 Compare(const nsString1 &aString,PRBool aIgnoreCase=PR_FALSE) const; -virtual PRInt32 Compare(const char *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; -virtual PRInt32 Compare(const PRUnichar *aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; - -/** - * These methods compare a given string type to this one - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator==(const nsString1 &aString) const; -PRBool operator==(const char *aString) const; -PRBool operator==(const PRUnichar* aString) const; -PRBool operator==(PRUnichar* aString) const; - -/** - * These methods perform a !compare of a given string type to this - * @param aString is the string to be compared to this - * @return TRUE - */ -PRBool operator!=(const nsString1 &aString) const; -PRBool operator!=(const char *aString) const; -PRBool operator!=(const PRUnichar* aString) const; - -/** - * These methods test if a given string is < than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator<(const nsString1 &aString) const; -PRBool operator<(const char *aString) const; -PRBool operator<(const PRUnichar* aString) const; - -/** - * These methods test if a given string is > than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator>(const nsString1 &S) const; -PRBool operator>(const char *aCString) const; -PRBool operator>(const PRUnichar* aString) const; - -/** - * These methods test if a given string is <= than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator<=(const nsString1 &S) const; -PRBool operator<=(const char *aCString) const; -PRBool operator<=(const PRUnichar* aString) const; - -/** - * These methods test if a given string is >= than this - * @param aString is the string to be compared to this - * @return TRUE or FALSE - */ -PRBool operator>=(const nsString1 &S) const; -PRBool operator>=(const char* aCString) const; -PRBool operator>=(const PRUnichar* aString) const; - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. - * @return TRUE if equal - */ -PRBool Equals(const nsString1& aString) const; -PRBool Equals(const char* aString,PRInt32 aLength=-1) const; -PRBool Equals(const nsIAtom *aAtom) const; - - -/** - * Compares to unichar string ptrs to each other - * @param s1 is a ptr to a unichar buffer - * @param s2 is a ptr to a unichar buffer - * @return TRUE if they match - */ -PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const; - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. - * @return TRUE if equal - */ -PRBool EqualsIgnoreCase(const nsString1& aString) const; -PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; -PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; - -/** - * Compares to unichar string ptrs to each other without respect to case - * @param s1 is a ptr to a unichar buffer - * @param s2 is a ptr to a unichar buffer - * @return TRUE if they match - */ -PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; - - -static void SelfTest(); -virtual void DebugDump(ostream& aStream) const; - - protected: - -typedef PRUnichar chartype; - - chartype* mStr; - PRInt32 mLength; - PRInt32 mCapacity; -#ifdef RICKG_DEBUG - static PRBool mSelfTested; -#endif }; -ostream& operator<<(ostream& os,nsString1& aString); -extern NS_COM int fputs(const nsString1& aString, FILE* out); +extern NS_COM int fputs(const nsCString& aString, FILE* out); +ostream& operator<<(ostream& aStream,const nsCString& aString); -//---------------------------------------------------------------------- -/** - * A version of nsString1 which is designed to be used as an automatic - * variable. It attempts to operate out of a fixed size internal - * buffer until too much data is added; then a dynamic buffer is - * allocated and grown as necessary. - */ -// XXX template this with a parameter for the size of the buffer? -class NS_COM nsAutoString1 : public nsString1 { +/************************************************************** + Here comes the AutoString class which uses internal memory + (typically found on the stack) for its default buffer. + If the buffer needs to grow, it gets reallocated on the heap. + **************************************************************/ + +class NS_COM nsCAutoString : public nsCString { +public: + + nsCAutoString(); + nsCAutoString(const char* aString,PRInt32 aLength=-1); + nsCAutoString(CSharedStrBuffer& aBuffer); + nsCAutoString(const PRUnichar* aString,PRInt32 aLength=-1); + nsCAutoString(const nsStr& aString); + nsCAutoString(const nsCAutoString& aString); + +#ifdef AIX + nsCAutoString(const nsSubsumeCStr& aSubsumeStr); // AIX requires a const +#else + nsCAutoString(nsSubsumeCStr& aSubsumeStr); +#endif // AIX + nsCAutoString(PRUnichar aChar); + virtual ~nsCAutoString(); + + nsCAutoString& operator=(const nsStr& aString) {nsCString::Assign(aString); return *this;} + nsCAutoString& operator=(const char* aCString) {nsCString::operator=(aCString); return *this;} + nsCAutoString& operator=(char aChar) {nsCString::operator=(aChar); return *this;} + nsCAutoString& operator=(const PRUnichar* aBuffer) {nsCString::operator=(aBuffer); return *this;} + nsCAutoString& operator=(PRUnichar aChar) {nsCString::operator=(aChar); return *this;} + + operator char*() {return mStr;} + operator const char*() {return (const char*)mStr;} + + /** + * Retrieve the size of this string + * @return string length + */ + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + + char mBuffer[32]; +}; + +/*************************************************************** + The subsumestr class is very unusual. + It differs from a normal string in that it doesn't use normal + copy semantics when another string is assign to this. + Instead, it "steals" the contents of the source string. + + This is very handy for returning nsString classes as part of + an operator+(...) for example, in that it cuts down the number + of copy operations that must occur. + + You should probably not use this class unless you really know + what you're doing. + ***************************************************************/ +class NS_COM nsSubsumeCStr : public nsCString { public: - nsAutoString1(); - nsAutoString1(const nsString1& other); - nsAutoString1(const nsAutoString1& other); - nsAutoString1(PRUnichar aChar); - nsAutoString1(const char* aCString); - nsAutoString1(const PRUnichar* us, PRInt32 uslen = -1); - virtual ~nsAutoString1(); - - nsAutoString1& operator=(const nsString1& aString) {nsString1::operator=(aString); return *this;} - nsAutoString1& operator=(const nsAutoString1& aString) {nsString1::operator=(aString); return *this;} - nsAutoString1& operator=(const char* aCString) {nsString1::operator=(aCString); return *this;} - nsAutoString1& operator=(char aChar) {nsString1::operator=(aChar); return *this;} - nsAutoString1& operator=(const PRUnichar* aBuffer) {nsString1::operator=(aBuffer); return *this;} - nsAutoString1& operator=(PRUnichar aChar) {nsString1::operator=(aChar); return *this;} - - virtual void SizeOf(nsISizeOfHandler* aHandler) const; - - static void SelfTest(); - -protected: - virtual void EnsureCapacityFor(PRInt32 aNewLength); - - chartype mBuf[32]; + nsSubsumeCStr(nsStr& aString); + nsSubsumeCStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); + nsSubsumeCStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; -ostream& operator<<(ostream& os,nsAutoString1& aString); + #endif + diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index 917657481fb..0cf0074773c 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -17,26 +17,23 @@ * Reserved. */ -#include #include #include -#include -#include "nsString2.h" +#include "nsString.h" #include "nsDebug.h" -#include "nsCRT.h" -#include "nsIAtom.h" #include "nsDeque.h" + #ifndef RICKG_TESTBED #include "prdtoa.h" #include "nsISizeOfHandler.h" #endif + static const char* kNullPointerError = "Error: unexpected null ptr"; static const char* kWhitespace="\b\t\r\n "; - #define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { @@ -92,17 +89,11 @@ public: } }; -/*********************************************************************** - IMPLEMENTATION NOTES: - Man I hate writing string classes. - You'd think after about a qintrillion lines of code have been written, - that no poor soul would ever have to do this again. Sigh. - ***********************************************************************/ - -void Subsume(nsStr& aDest,nsStr& aSource){ +static void Subsume(nsStr& aDest,nsStr& aSource){ if(aSource.mStr && aSource.mLength) { if(aSource.mOwnsBuffer){ + nsStr::Destroy(aDest,0); aDest.mStr=aSource.mStr; aDest.mLength=aSource.mLength; aDest.mCharSize=aSource.mCharSize; @@ -118,17 +109,6 @@ void Subsume(nsStr& aDest,nsStr& aSource){ else nsStr::Truncate(aDest,0,0); } -//#define RICKG_DEBUG -#undef RICKG_DEBUG -#ifdef RICKG_DEBUG -/******************************************************** - This class's only purpose in life is to test nsString2. - ********************************************************/ -class CStringTester { -public: - CStringTester(); -}; -#endif /** * Default constructor. Note that we actually allocate a small buffer @@ -136,10 +116,7 @@ public: * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { -#ifdef RICKG_DEBUG - static CStringTester gStringTester; -#endif +nsString::nsString(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); } @@ -148,9 +125,10 @@ nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAge * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString2::nsString2(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aCString); + } /** @@ -158,7 +136,7 @@ nsString2::nsString2(const char* aCString,eCharSize aCharSize,nsIMemoryAgent* an * @update gess 1/4/99 * @param aCString is a ptr to a 1-byte cstr */ -nsString2::nsString2(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); Assign(aString); } @@ -166,9 +144,9 @@ nsString2::nsString2(const PRUnichar* aString,eCharSize aCharSize,nsIMemoryAgent /** * This is our copy constructor * @update gess 1/4/99 - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { +nsString::nsString(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { nsStr::Initialize(*this,aCharSize); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } @@ -176,9 +154,9 @@ nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* an /** * This is our copy constructor * @update gess 1/4/99 - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { +nsString::nsString(const nsString& aString) :mAgent(aString.mAgent) { nsStr::Initialize(*this,aString.mCharSize); nsStr::Assign(*this,aString,0,aString.mLength,mAgent); } @@ -188,7 +166,7 @@ nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { * @update gess 1/4/99 * @param reference to a subsumeString */ -nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { +nsString::nsString(nsSubsumeStr& aSubsumeStr) :mAgent(0) { Subsume(*this,aSubsumeStr); } @@ -196,13 +174,15 @@ nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { * Destructor * Make sure we call nsStr::Destroy. */ -nsString2::~nsString2() { +nsString::~nsString() { nsStr::Destroy(*this,mAgent); } -void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { +void nsString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); aHandler->Add(mCapacity << mCharSize); +#endif } /** @@ -212,7 +192,7 @@ void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { * @param anIndex -- new length of string * @return nada */ -void nsString2::Truncate(PRInt32 anIndex) { +void nsString::Truncate(PRInt32 anIndex) { nsStr::Truncate(*this,anIndex,mAgent); } @@ -223,7 +203,7 @@ void nsString2::Truncate(PRInt32 anIndex) { * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString2::IsOrdered(void) const { +PRBool nsString::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { PRUint32 theIndex; @@ -248,11 +228,10 @@ PRBool nsString2::IsOrdered(void) const { * @param aLength -- contains new length for mStr * @return */ -void nsString2::SetCapacity(PRUint32 aLength) { - if(aLength>mLength) { +void nsString::SetCapacity(PRUint32 aLength) { + if(aLength>mCapacity) { GrowCapacity(*this,aLength,mAgent); } - mLength=aLength; AddNullTerminator(*this); } @@ -263,50 +242,54 @@ void nsString2::SetCapacity(PRUint32 aLength) { //static char gChar=0; -/** +/** * * @update gess1/4/99 * @return ptr to internal buffer (if 1-byte), otherwise NULL */ -const char* nsString2::GetBuffer(void) const { +const char* nsString::GetBuffer(void) const { const char* result=(eOneByte==mCharSize) ? mStr : 0; return result; } + /** - * + * This method returns the internal unicode buffer. + * Now that we've factored the string class, this should never + * be able to return a 1 byte string. + * * @update gess1/4/99 - * @return ptr to internal buffer (if 2-byte), otherwise NULL + * @return ptr to internal (2-byte) buffer; */ -const PRUnichar* nsString2::GetUnicode(void) const { - const PRUnichar* result=(eTwoByte==mCharSize) ? mUStr : 0; +const PRUnichar* nsString::GetUnicode(void) const { + const PRUnichar* result=(eOneByte==mCharSize) ? 0 : mUStr; return result; } /** * Get nth character. */ -PRUnichar nsString2::operator[](PRUint32 anIndex) const { +PRUnichar nsString::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::CharAt(PRUint32 anIndex) const { +PRUnichar nsString::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::First(void) const{ +PRUnichar nsString::First(void) const{ return GetCharAt(*this,0); } -PRUnichar nsString2::Last(void) const{ +PRUnichar nsString::Last(void) const{ return GetCharAt(*this,mLength-1); } -PRBool nsString2::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ +PRBool nsString::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex PRInt32(sizeof(buf)-1)) { *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; @@ -655,7 +701,7 @@ float nsString2::ToFloat(PRInt32* aErrorCode) const { * @param aRadix tells us what base to expect the string in. * @return int rep of string value */ -PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { +PRInt32 _ToInteger(nsString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { //copy chars to local buffer -- step down from 2 bytes to 1 if necessary... PRInt32 result=0; @@ -698,7 +744,6 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { result+=theDigit*theMult; theMult*=aRadix; } - return result; } @@ -713,11 +758,11 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * @param aRadix (an out parm) tells the caller what base we think the string is in. * @return non-zero error code if this string is non-numeric */ -PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { +PRInt32 GetNumericSubstring(nsString& aString,PRUint32& aRadix) { aString.ToUpperCase(); - PRInt32 decPt=aString.FindChar(aString,'.',PR_TRUE,0); + PRInt32 decPt=nsStr::FindChar(aString,'.',PR_TRUE,0); char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1; aRadix=kRadixUnknown; //assume for starters... @@ -762,7 +807,6 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { if(cp>aString.mStr) aString.Cut(0,cp-aString.mStr); PRInt32 result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; - return result; } @@ -772,10 +816,10 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) { * @update gess 10/01/98 * @return 10,16,or 0 (meaning I don't know) */ -PRUint32 nsString2::DetermineRadix(void) { +PRUint32 nsString::DetermineRadix(void) { PRUint32 result=kRadixUnknown; if(0(const nsString2& S) const {return PRBool(Compare(S)>0);} -PRBool nsString2::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} -PRBool nsString2::operator>(const char* s) const {return PRBool(Compare(s)>0);} -PRBool nsString2::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} +PRBool nsString::operator>(const nsString& S) const {return PRBool(Compare(S)>0);} +PRBool nsString::operator>(const nsStr& S) const {return PRBool(Compare(S)>0);} +PRBool nsString::operator>(const char* s) const {return PRBool(Compare(s)>0);} +PRBool nsString::operator>(const PRUnichar* s) const {return PRBool(Compare(s)>0);} -PRBool nsString2::operator<=(const nsString2& S) const {return PRBool(Compare(S)<=0);} -PRBool nsString2::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} -PRBool nsString2::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} -PRBool nsString2::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} +PRBool nsString::operator<=(const nsString& S) const {return PRBool(Compare(S)<=0);} +PRBool nsString::operator<=(const nsStr& S) const {return PRBool(Compare(S)<=0);} +PRBool nsString::operator<=(const char* s) const {return PRBool(Compare(s)<=0);} +PRBool nsString::operator<=(const PRUnichar* s) const {return PRBool(Compare(s)<=0);} -PRBool nsString2::operator>=(const nsString2& S) const {return PRBool(Compare(S)>=0);} -PRBool nsString2::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} -PRBool nsString2::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} -PRBool nsString2::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} +PRBool nsString::operator>=(const nsString& S) const {return PRBool(Compare(S)>=0);} +PRBool nsString::operator>=(const nsStr& S) const {return PRBool(Compare(S)>=0);} +PRBool nsString::operator>=(const char* s) const {return PRBool(Compare(s)>=0);} +PRBool nsString::operator>=(const PRUnichar* s) const {return PRBool(Compare(s)>=0);} -PRBool nsString2::EqualsIgnoreCase(const nsString2& aString) const { +PRBool nsString::EqualsIgnoreCase(const nsString& aString) const { return Equals(aString,PR_TRUE); } -PRBool nsString2::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { - return Equals(aString,aLength,PR_TRUE); +PRBool nsString::EqualsIgnoreCase(const char* aString,PRInt32 aLength) const { + return Equals(aString,PR_TRUE,aLength); } -PRBool nsString2::EqualsIgnoreCase(const nsIAtom *aAtom) const { +PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const { return Equals(aAtom,PR_TRUE); } -PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { return Equals(s1,s2,PR_TRUE); } @@ -1562,66 +1659,61 @@ PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) con * Compare this to given string; note that we compare full strings here. * * @update gess 01/04/99 - * @param aString is the other nsString2 to be compared to + * @param aString is the other nsString to be compared to + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString2::Equals(const nsString2& aString,PRBool aIgnoreCase) const { - PRInt32 result=nsStr::Compare(*this,aString,MinInt(mLength,aString.mLength),aIgnoreCase); - return PRBool(0==result); +PRBool nsString::Equals(const nsString& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); + return result; + } /** * Compare this to given string; note that we compare full strings here. * * @update gess 01/04/99 - * @param aString is the other nsString2 to be compared to + * @param aString is the other nsString to be compared to + * @param aCount tells us how many chars to test; -1 implies full length * @return TRUE if equal */ -PRBool nsString2::Equals(const nsStr& aString,PRBool aIgnoreCase) const { - PRInt32 result=nsStr::Compare(*this,aString,MinInt(mLength,aString.mLength),aIgnoreCase); - return PRBool(0==result); -} - -PRBool nsString2::Equals(const char* aString,PRBool aIgnoreCase) const { - if(aString) { - return Equals(aString,nsCRT::strlen(aString),aIgnoreCase); - } - return 0; -} - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 01/04/99 - * @param aCString -- Cstr to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString2::Equals(const char* aCString,PRUint32 aCount,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=aCString,kNullPointerError); - PRBool result=PR_FALSE; - if(aCString) { - PRInt32 theAnswer=Compare(aCString,aIgnoreCase,aCount); - result=PRBool(0==theAnswer); - } +PRBool nsString::Equals(const nsStr& aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=nsStr::Compare(*this,aString,aCount,aIgnoreCase); + PRBool result=PRBool(0==theAnswer); return result; } -PRBool nsString2::Equals(const PRUnichar* aString,PRBool aIgnoreCase) const { - NS_ASSERTION(0!=aString,kNullPointerError); - PRBool result=PR_FALSE; - if(aString) { - result=Equals(aString,nsCRT::strlen(aString),aIgnoreCase); - } +/** + * Compare this to given c-string; note that we compare full strings here. + * + * @param aString is the CString to be compared + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length + * @return TRUE if equal + */ +PRBool nsString::Equals(const char* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); return result; } +/** + * Compare this to given unicode string; note that we compare full strings here. + * + * @param aString is the U-String to be compared + * @param aIgnorecase tells us whether to be case sensitive + * @param aCount tells us how many chars to test; -1 implies full length + * @return TRUE if equal + */ +PRBool nsString::Equals(const PRUnichar* aString,PRBool aIgnoreCase,PRInt32 aCount) const { + PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); + PRBool result=PRBool(0==theAnswer); + return result; +} /** - * Compare this to given string; note that we compare full strings here. + * Compare this to given atom; note that we compare full strings here. * The optional length argument just lets us know how long the given string is. * If you provide a length, it is compared to length of this string as an * optimization. @@ -1631,45 +1723,29 @@ PRBool nsString2::Equals(const PRUnichar* aString,PRBool aIgnoreCase) const { * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString2::Equals(const PRUnichar* aString,PRUint32 aCount,PRBool aIgnoreCase) const{ - NS_ASSERTION(0!=aString,kNullPointerError); - PRBool result=PR_FALSE; - if(aString){ - PRInt32 theAnswer=Compare(aString,aIgnoreCase,aCount); - result=PRBool(0==theAnswer); - } - return result; -} - -/** - * Compare this to given string; note that we compare full strings here. - * The optional length argument just lets us know how long the given string is. - * If you provide a length, it is compared to length of this string as an - * optimization. - * - * @update gess 01/04/99 - * @param aString -- unistring to compare to this - * @param aLength -- length of given string. - * @return TRUE if equal - */ -PRBool nsString2::Equals(const nsIAtom* aAtom,PRBool aIgnoreCase) const{ +PRBool nsString::Equals(const nsIAtom* aAtom,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=aAtom,kNullPointerError); PRBool result=PR_FALSE; if(aAtom){ - PRInt32 cmp=nsCRT::strcasecmp(mUStr,aAtom->GetUnicode()); + PRInt32 cmp=0; + if (aIgnoreCase) + cmp=nsCRT::strcasecmp(mUStr,aAtom->GetUnicode()); + else + cmp=nsCRT::strcmp(mUStr,aAtom->GetUnicode()); result=PRBool(0==cmp); } - return result; + + return result; } /** * Compare given strings * @update gess 7/27/98 - * @param s1 -- first string to be compared - * @param s2 -- second string to be compared + * @param s1 -- first unichar string to be compared + * @param s2 -- second unichar string to be compared * @return TRUE if equal */ -PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase) const { +PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1677,7 +1753,8 @@ PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnore PRInt32 cmp=(aIgnoreCase) ? nsCRT::strcasecmp(s1,s2) : nsCRT::strcmp(s1,s2); result=PRBool(0==cmp); } - return result; + + return result; } /** @@ -1687,7 +1764,7 @@ PRBool nsString2::Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnore * @param aChar is character to be tested * @return TRUE if in alpha range */ -PRBool nsString2::IsAlpha(PRUnichar aChar) { +PRBool nsString::IsAlpha(PRUnichar aChar) { // XXX i18n if (((aChar >= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { return PR_TRUE; @@ -1702,7 +1779,7 @@ PRBool nsString2::IsAlpha(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if is valid space char */ -PRBool nsString2::IsSpace(PRUnichar aChar) { +PRBool nsString::IsSpace(PRUnichar aChar) { // XXX i18n if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { return PR_TRUE; @@ -1717,12 +1794,12 @@ PRBool nsString2::IsSpace(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if char is a valid digit */ -PRBool nsString2::IsDigit(PRUnichar aChar) { +PRBool nsString::IsDigit(PRUnichar aChar) { // XXX i18n return PRBool((aChar >= '0') && (aChar <= '9')); } - +#ifndef RICKG_TESTBED /************************************************************** Define the string deallocator class... **************************************************************/ @@ -1730,7 +1807,7 @@ class nsStringDeallocator: public nsDequeFunctor{ public: virtual void* operator()(void* anObject) { static nsMemoryAgent theAgent; - nsString2* aString= (nsString2*)anObject; + nsString* aString= (nsString*)anObject; if(aString){ aString->mAgent=&theAgent; delete aString; @@ -1740,10 +1817,9 @@ public: }; /**************************************************************************** - * This class, appropriately enough, creates and recycles nsString2 objects.. + * This class, appropriately enough, creates and recycles nsString objects.. ****************************************************************************/ - class nsStringRecycler { public: nsStringRecycler() : mDeque(0) { @@ -1754,22 +1830,19 @@ public: mDeque.ForEach(theDeallocator); //now delete the strings } - void Recycle(nsString2* aString) { + void Recycle(nsString* aString) { mDeque.Push(aString); } - nsString2* CreateString(eCharSize aCharSize){ - nsString2* result=(nsString2*)mDeque.Pop(); + nsString* CreateString(eCharSize aCharSize){ + nsString* result=(nsString*)mDeque.Pop(); if(!result) - result=new nsString2(aCharSize); + result=new nsString(aCharSize); return result; } nsDeque mDeque; }; - -void Subsume(nsStr& aDest,nsStr& aSource); -nsStringRecycler& GetRecycler(void); - +static nsStringRecycler& GetRecycler(void); /** * @@ -1781,6 +1854,8 @@ nsStringRecycler& GetRecycler(void){ static nsStringRecycler gRecycler; return gRecycler; } +#endif + /** * Call this mehod when you're done @@ -1788,8 +1863,11 @@ nsStringRecycler& GetRecycler(void){ * @param * @return */ -nsString2* nsString2::CreateString(eCharSize aCharSize){ - nsString2* result=GetRecycler().CreateString(aCharSize); +nsString* nsString::CreateString(eCharSize aCharSize){ + nsString* result=0; +#ifndef RICKG_TESTBED + GetRecycler().CreateString(aCharSize); +#endif return result; } @@ -1799,8 +1877,12 @@ nsString2* nsString2::CreateString(eCharSize aCharSize){ * @param * @return */ -void nsString2::Recycle(nsString2* aString){ +void nsString::Recycle(nsString* aString){ +#ifndef RICKG_TESTBED GetRecycler().Recycle(aString); +#else + delete aString; +#endif } @@ -1810,7 +1892,7 @@ void nsString2::Recycle(nsString2* aString){ * @param * @return */ -void nsString2::DebugDump(ostream& aStream) const { +void nsString::DebugDump(ostream& aStream) const { for(PRUint32 i=0;i>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); -} -/** - * construct from external buffer and given string - * @param anExtBuffer describes an external buffer - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2(aStr.mCharSize) { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,aStr.mCharSize,PR_FALSE); - mAgent=0; - AddNullTerminator(*this); - Assign(aCString); } /** * Copy construct from ascii c-string * @param aCString is a ptr to a 1-byte cstr */ -nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const char* aCString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); - Assign(aCString); -} + Append(aCString,aLength); + -/** - * Copy construct from ascii c-string - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(char* aCString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - mAgent=0; - if(assumeOwnership && aCString) { - aCapacity = (-1==aCapacity) ? strlen(aCString) : aCapacity-1; - nsStr::Initialize(*this,aCString,aCapacity,aCapacity,eOneByte,PR_TRUE); - AddNullTerminator(*this); - } - else { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aCString); - } } /** * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const PRUnichar* aString,eCharSize aCharSize,PRInt32 aLength) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); + Append(aString,aLength); + } /** * Copy construct from uni-string * @param aString is a ptr to a unistr */ -nsAutoString2::nsAutoString2(PRUnichar* aString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { +nsAutoString::nsAutoString(CSharedStrBuffer& aBuffer) : nsString(eTwoByte) { mAgent=0; - if(assumeOwnership && aString) { - aCapacity = (-1==aCapacity) ? nsCRT::strlen(aString) : aCapacity-1; - nsStr::Initialize(*this,(char*)aString,aCapacity,aCapacity,eTwoByte,PR_TRUE); - AddNullTerminator(*this); + if(!aBuffer.mBuffer) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>eTwoByte)-1,0,eTwoByte,PR_FALSE); } else { - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aString); + nsStr::Initialize(*this,aBuffer.mBuffer,aBuffer.mCapacity,aBuffer.mLength,aBuffer.mCharSize,!aBuffer.mStackBased); } + AddNullTerminator(*this); } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(const nsStr& aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const nsStr& aString,eCharSize aCharSize) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); + Append(aString); + } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize) : nsString2(aCharSize) { +nsAutoString::nsAutoString(const nsAutoString& aString,eCharSize aCharSize) : nsString(aCharSize) { mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsString2& aString,eCharSize aCharSize) : nsString2(aCharSize){ - mAgent=0; - nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); - AddNullTerminator(*this); - Assign(aString); + Append(aString); + } /** - * Copy construct from an nsString2 + * Copy construct from an nsString * @param */ -nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ +nsAutoString::nsAutoString(PRUnichar aChar,eCharSize aCharSize) : nsString(aCharSize){ mAgent=0; nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); AddNullTerminator(*this); - Assign(aChar); + Append(aChar); + } /** @@ -2012,12 +2057,12 @@ nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aC * @param reference to a subsumeString */ #ifdef AIX -nsAutoString2::nsAutoString2(const nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { +nsAutoString::nsAutoString(const nsSubsumeStr& aSubsumeStr) :nsString(aSubsumeStr.mCharSize) { mAgent=0; nsSubsumeStr temp(aSubsumeStr); // a temp is needed for the AIX compiler Subsume(*this,temp); #else -nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { +nsAutoString::nsAutoString( nsSubsumeStr& aSubsumeStr) :nsString(aSubsumeStr.mCharSize) { mAgent=0; Subsume(*this,aSubsumeStr); #endif // AIX @@ -2027,384 +2072,29 @@ nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr. * deconstruct the autstring * @param */ -nsAutoString2::~nsAutoString2(){ -// bool b=true; -// mStr=0; +nsAutoString::~nsAutoString(){ } -void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { +void nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const { +#ifndef RICKG_TESTBED aHandler->Add(sizeof(*this)); aHandler->Add(mCapacity << mCharSize); +#endif } -nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2(aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString(aString.mCharSize) { Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2(aString.mCharSize) { - Subsume(*this,aString); -} - -nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eTwoByte) { +nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString(eTwoByte) { mUStr=aString; mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } -nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { +nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString(eOneByte) { mStr=aString; mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } -/*********************************************************************** - IMPLEMENTATION of nsCAutoString, which is a vanilla string class that - only ever stores 1 byte character strings. Typically you'll use this - class to hold a pointer to a char*, which comes from an nsString. - ***********************************************************************/ -nsCAutoString::nsCAutoString(const nsString2& aString) : nsAutoString(aString,eOneByte){ -} - -nsCAutoString::operator const char*() const { - return (const char*)mStr; -} - - - -#ifdef RICKG_DEBUG -/*********************************************************************** - IMPLEMENTATION of CStringTester... - ***********************************************************************/ -CStringTester::CStringTester() { - - static const char* kConstructorError = kConstructorError; - static const char* kComparisonError = "Comparision error!"; - static const char* kEqualsError = "Equals error!"; - - eCharSize theSize=eTwoByte; - - //begin by testing the constructors... - { - { - nsString2 theString0("foo",theSize); //watch it construct and destruct - } - - //Let's test our autoCStrings... - { - nsString2 theString("hello"); - nsCAutoString theCStr(theString); - } - - - { - //this test makes sure that autostrings who assume ownership of a buffer, - //don't also try to copy that buffer onto itself... (was a bug) - char* theStr="hello rick"; - nsAutoString2(theStr,5,eOneByte,PR_FALSE); - - } - - { - nsString2 theString("hello"); - nsString2 temp1=theString+" there!"; - nsString2 temp2=theString+'!'; - nsSubsumeStr temp3=theString+'?'; - nsString2 temp4(temp3); - temp1=temp3; - nsAutoString2 temp5("hello"); - nsSubsumeStr temp6(temp5); - } - - nsString2 theString1(theSize); - nsString2 theString("hello",theSize); - nsString2 theString3(theString,theSize); - nsStr& si=theString; - nsString2 theString4(si,theSize); - PRUint32 theLen=theString3.Length(); - - //and hey, why not do a few lexo-morphic tests... - theString.ToUpperCase(); - theString.ToLowerCase(); - - //while we're here, let's try truncation and setting the length. - theString3.Truncate(3); - theLen=theString3.Length(); - - - theString.SetCapacity(3); - const char* theBuffer=theString.GetBuffer(); - const char* theOther=theBuffer; - - { - nsString2 temp(" hello there rick ",theSize); - temp.CompressWhitespace(); - } - - nsString2 theString5(" hello there rick ",theSize); - theString5.StripChars("reo"); - theString5.Trim(" ",PR_TRUE,PR_FALSE); - theString5.StripWhitespace(); - - nsString2* theString6=theString5.ToNewString(); - nsString2::Recycle(theString6); - - char* str=theString5.ToNewCString(); - delete [] str; - - char buffer[100]; - theString5.ToCString(buffer,sizeof(buffer)-1); - theOther=theString5.GetBuffer(); - } - - //try a few numeric conversion routines... - { - nsString2 str1("10000",theSize); - nsString2 str2("hello"); - nsString2 str3; - PRInt32 err; - PRInt32 theInt=str1.ToInteger(&err); - theInt=str2.ToInteger(&err); - theInt=str3.ToInteger(&err); - - str1="100.100"; - float theFloat=str1.ToFloat(&err); - } - //Now test the character accessor methods... - { - nsString2 theString("hello",theSize); - PRUint32 len=theString.Length(); - PRUnichar ch; - for(PRUint32 i=0;itemp8.Compare(bbbb),kComparisonError); - NS_ASSERTION(0>temp8.Compare(temp9),kComparisonError); - NS_ASSERTION(0=temp8)),kComparisonError); - - NS_ASSERTION(((temp9>temp8) && (temp8<=temp9)),kComparisonError); - NS_ASSERTION(temp9>aaaa,kComparisonError); - - NS_ASSERTION(temp8<=temp8,kComparisonError); - NS_ASSERTION(temp8<=temp9,kComparisonError); - NS_ASSERTION(temp8<=bbbb,kComparisonError); - - NS_ASSERTION(((temp9>=temp8) && (temp8=temp8,kComparisonError); - NS_ASSERTION(temp9>=aaaa,kComparisonError); - - NS_ASSERTION(temp8.Equals(temp8),kEqualsError); - NS_ASSERTION(temp8.Equals(aaaa),kEqualsError); - - nsString2 temp10(temp8); - temp10.ToUpperCase(); - NS_ASSERTION(temp8.Equals(temp10,PR_TRUE),kEqualsError); - NS_ASSERTION(temp8.Equals("AAAA",PR_TRUE),kEqualsError); - - - //********************************************** - //Now let's test a few string MANIPULATORS... - //********************************************** - - { - nsAutoString2 ab("ab",theSize); - nsString2 abcde("cde",theSize); - nsString2 cut("abcdef",theSize); - cut.Cut(7,10); //this is out of bounds, so ignore... - cut.DebugDump(cout); - cut.Cut(5,2); //cut last chars - cut.DebugDump(cout); - cut.Cut(1,1); //cut first char - cut.DebugDump(cout); - cut.Cut(2,1); //cut one from the middle - cut.DebugDump(cout); - cut="Hello there Rick"; - cut.DebugDump(cout); - - cut="'\"abcdef\"'"; - cut.Trim("'"); - cut.DebugDump(cout); - cut.Trim("\"",PR_TRUE,PR_FALSE); - cut.DebugDump(cout); - cut.Trim("\"",PR_FALSE,PR_TRUE); - cut.DebugDump(cout); - - cut="abc def\n\n ghi"; - cut.StripWhitespace(); - cut.DebugDump(cout); - cut="abc def\n\n ghi"; - cut.CompressWhitespace(); - cut.DebugDump(cout); - } - - - //********************************************** - //Now let's test the SEARCHING operations... - //********************************************** - - { - nsString2 find1("abcdefghijk",theSize); - nsString2 find2("ijk",theSize); - - PRInt32 pos=find1.Find("efg"); - NS_ASSERTION(pos==4,"Error: Find routine"); - - pos=find1.Find("EFG",PR_TRUE); - NS_ASSERTION(pos==4,"Error: Find routine"); - - pos=find1.Find('d'); - NS_ASSERTION(pos==3,"Error: Find char routine"); - - pos=find1.Find(find2); - NS_ASSERTION(pos==8,"Error: Find char routine"); - - pos=find1.FindCharInSet("12k"); - NS_ASSERTION(pos==10,"Error: FindFirstInChar routine"); - - pos=find1.RFindCharInSet("12k"); - NS_ASSERTION(pos==10,"Error: FindLastInChar routine"); - - pos=find1.RFind("efg"); - NS_ASSERTION(pos==4,"Error: RFind routine"); - - pos=find1.RFind("xxx"); - NS_ASSERTION(pos==-1,"Error: RFind routine"); //this should fail - - pos=find1.RFind(""); - NS_ASSERTION(pos==-1,"Error: RFind routine"); //this too should fail. - - pos=find1.RFind('a'); - NS_ASSERTION(pos==0,"Error: RFind routine"); - - pos=find1.BinarySearch('a'); - pos=find1.BinarySearch('b'); - pos=find1.BinarySearch('c'); - pos=find1.BinarySearch('d'); - pos=find1.BinarySearch('e'); - pos=find1.BinarySearch('f'); - pos=find1.BinarySearch('g'); - pos=find1.BinarySearch('h'); - pos=find1.BinarySearch('i'); - pos=find1.BinarySearch('z'); - } - - { - //now let's try a memory allocation test... - nsString2 temp; - for(int i=0;i<100;i++){ - temp+="hello "; - } - int x=5; - } -}; - -#endif //rickg_debug diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index 79d0658aae7..29490974262 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -30,19 +30,17 @@ ***********************************************************************/ -#ifndef _nsString2 -#define _nsString2 +#ifndef _nsString_ +#define _nsString_ #include "prtypes.h" #include "nscore.h" #include #include #include "nsCRT.h" - -#include "nsStr.h" -#include -#include +#include "nsString.h" #include "nsIAtom.h" +#include "nsStr.h" class nsISizeOfHandler; @@ -50,14 +48,9 @@ class nsISizeOfHandler; #define nsString2 nsString #define nsAutoString2 nsAutoString -#define kRadix10 (10) -#define kRadix16 (16) -#define kAutoDetect (100) -#define kRadixUnknown (kAutoDetect+1) - class NS_COM nsSubsumeStr; -class NS_COM nsString2 : public nsStr { +class NS_COM nsString : public nsStr { public: @@ -67,44 +60,44 @@ class NS_COM nsString2 : public nsStr { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString2(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This constructor accepts an isolatin string * @param aCString is a ptr to a 1-byte cstr */ -nsString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This constructor accepts a unichar string * @param aCString is a ptr to a 2-byte cstr */ -nsString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This is a copy constructor that accepts an nsStr - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); +nsString(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAgent=0); /** * This is our copy constructor - * @param reference to another nsString2 + * @param reference to another nsString */ -nsString2(const nsString2& aString); +nsString(const nsString& aString); /** * This constructor takes a subsumestr * @param reference to subsumestr */ -nsString2(nsSubsumeStr& aSubsumeStr); +nsString(nsSubsumeStr& aSubsumeStr); /** * Destructor * */ -virtual ~nsString2(); +virtual ~nsString(); /** * Retrieve the length of this string @@ -120,13 +113,13 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const; /** - * Call this method if you want to force a different string capacity + * Call this method if you want to force a different string length * @update gess7/30/98 * @param aLength -- contains new length for mStr * @return */ void SetLength(PRUint32 aLength) { - SetCapacity(aLength); + Truncate(aLength); } /** @@ -173,7 +166,7 @@ PRBool IsEmpty(void) const { } /********************************************************************** - Accessor methods... + Getters/Setters... *********************************************************************/ const char* GetBuffer(void) const; @@ -188,11 +181,14 @@ PRUnichar CharAt(PRUint32 anIndex) const; PRUnichar First(void) const; PRUnichar Last(void) const; + /** + * Set nth character. + */ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); /********************************************************************** - String creation methods... + String concatenation methods... *********************************************************************/ /** @@ -201,13 +197,7 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); * @return new string */ nsSubsumeStr operator+(const nsStr& aString); - -/** - * Create a new string by appending given string to this - * @param aString -- 2nd string to be appended - * @return new string - */ -nsSubsumeStr operator+(const nsString2& aString); +nsSubsumeStr operator+(const nsString& aString); /** * create a new string by adding this to the given buffer. @@ -260,7 +250,7 @@ void ToLowerCase(); * @update gess 7/27/98 * @param aOut is a string to contain result */ -void ToLowerCase(nsString2& aString) const; +void ToLowerCase(nsString& aString) const; /** * Converts chars in this to uppercase @@ -274,7 +264,7 @@ void ToUpperCase(); * @update gess 7/27/98 * @param aOut is a string to contain result */ -void ToUpperCase(nsString2& aString) const; +void ToUpperCase(nsString& aString) const; /** @@ -284,21 +274,25 @@ void ToUpperCase(nsString2& aString) const; * @param aSet -- characters to be cut from this * @return *this */ -nsString2& StripChars(const char* aSet); +nsString& StripChars(const char* aSet); +nsString& StripChar(PRUnichar aChar); /** * This method strips whitespace throughout the string * * @return this */ -nsString2& StripWhitespace(); +nsString& StripWhitespace(); /** * swaps occurence of 1 string for another * * @return this */ -nsString2& ReplaceChar(PRUnichar aSourceChar,PRUnichar aDestChar); +nsString& ReplaceChar(PRUnichar anOldChar,PRUnichar aNewChar); +nsString& ReplaceChar(const char* aSet,PRUnichar aNewChar); + +PRInt32 CountChar(PRUnichar aChar); /** * This method trims characters found in aTrimSet from @@ -308,7 +302,7 @@ nsString2& ReplaceChar(PRUnichar aSourceChar,PRUnichar aDestChar); * both ends * @return this */ -nsString2& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /** * This method strips whitespace from string. @@ -319,7 +313,7 @@ nsString2& Trim(const char* aSet,PRBool aEliminateLeading=PR_TRUE,PRBool aElimin * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString2& CompressSet(const char* aSet, char aChar, PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /** * This method strips whitespace from string. @@ -330,18 +324,18 @@ nsString2& CompressSet(const char* aSet, char aChar, PRBool aEliminateLeading=PR * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString2& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); +nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE,PRBool aEliminateTrailing=PR_TRUE); /********************************************************************** string conversion methods... *********************************************************************/ /** - * This method constructs a new nsString2 on the stack that is a copy + * This method constructs a new nsString on the stack that is a copy * of this string. * */ -nsString2* ToNewString() const; +nsString* ToNewString() const; /** * Creates an ISOLatin1 clone of this string @@ -392,9 +386,9 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const; * Functionally equivalent to assign or operator= * */ -nsString2& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} -nsString2& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} -nsString2& SetString(const nsString2& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const char* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const PRUnichar* aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} +nsString& SetString(const nsString& aString,PRInt32 aLength=-1) {return Assign(aString,aLength);} /** * assign given string to this string @@ -403,28 +397,27 @@ nsString2& SetString(const nsString2& aString,PRInt32 aLength=-1) {return Assign if you want me to determine its length * @return this */ -nsString2& Assign(const nsString2& aString,PRInt32 aCount=-1); -nsString2& Assign(const nsStr& aString,PRInt32 aCount=-1); -nsString2& Assign(const char* aString,PRInt32 aCount=-1); -nsString2& Assign(const PRUnichar* aString,PRInt32 aCount=-1); -nsString2& Assign(char aChar); -nsString2& Assign(PRUnichar aChar); +nsString& Assign(const nsStr& aString,PRInt32 aCount=-1); +nsString& Assign(const char* aString,PRInt32 aCount=-1); +nsString& Assign(const PRUnichar* aString,PRInt32 aCount=-1); +nsString& Assign(char aChar); +nsString& Assign(PRUnichar aChar); /** * here come a bunch of assignment operators... * @param aString: string to be added to this * @return this */ -nsString2& operator=(const nsString2& aString) {return Assign(aString);} -nsString2& operator=(const nsStr& aString) {return Assign(aString);} -nsString2& operator=(char aChar) {return Assign(aChar);} -nsString2& operator=(PRUnichar aChar) {return Assign(aChar);} -nsString2& operator=(const char* aCString) {return Assign(aCString);} -nsString2& operator=(const PRUnichar* aString) {return Assign(aString);} +nsString& operator=(const nsString& aString) {return Assign(aString);} +nsString& operator=(const nsStr& aString) {return Assign(aString);} +nsString& operator=(char aChar) {return Assign(aChar);} +nsString& operator=(PRUnichar aChar) {return Assign(aChar);} +nsString& operator=(const char* aCString) {return Assign(aCString);} +nsString& operator=(const PRUnichar* aString) {return Assign(aString);} #ifdef AIX -nsString2& operator=(const nsSubsumeStr& aSubsumeString); // AIX requires a const here +nsString& operator=(const nsSubsumeStr& aSubsumeString); // AIX requires a const here #else -nsString2& operator=(nsSubsumeStr& aSubsumeString); +nsString& operator=(nsSubsumeStr& aSubsumeString); #endif /** @@ -432,12 +425,12 @@ nsString2& operator=(nsSubsumeStr& aSubsumeString); * @param aString : string to be appended to this * @return this */ -nsString2& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} -nsString2& operator+=(const nsString2& aString){return Append(aString,aString.mLength);} -nsString2& operator+=(const char* aCString) {return Append(aCString);} -//nsString2& operator+=(char aChar){return Append(aChar);} -nsString2& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} -nsString2& operator+=(PRUnichar aChar){return Append(aChar);} +nsString& operator+=(const nsStr& aString){return Append(aString,aString.mLength);} +nsString& operator+=(const nsString& aString){return Append(aString,aString.mLength);} +nsString& operator+=(const char* aCString) {return Append(aCString);} +//nsString& operator+=(char aChar){return Append(aChar);} +nsString& operator+=(const PRUnichar* aUCString) {return Append(aUCString);} +nsString& operator+=(PRUnichar aChar){return Append(aChar);} /* * Appends n characters from given string to this, @@ -446,8 +439,8 @@ nsString2& operator+=(PRUnichar aChar){return Append(aChar);} * @param aString is the source to be appended to this * @return number of chars copied */ -nsString2& Append(const nsStr& aString) {return Append(aString,aString.mLength);} -nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLength);} +nsString& Append(const nsStr& aString) {return Append(aString,aString.mLength);} +nsString& Append(const nsString& aString) {return Append(aString,aString.mLength);} /* @@ -457,14 +450,14 @@ nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLeng * @param aCount -- number of chars to copy * @return number of chars copied */ -nsString2& Append(const nsStr& aString,PRInt32 aCount); -nsString2& Append(const nsString2& aString,PRInt32 aCount); -nsString2& Append(const char* aString,PRInt32 aCount=-1); -nsString2& Append(const PRUnichar* aString,PRInt32 aCount=-1); -nsString2& Append(char aChar); -nsString2& Append(PRUnichar aChar); -nsString2& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 -nsString2& Append(float aFloat); +nsString& Append(const nsStr& aString,PRInt32 aCount); +nsString& Append(const nsString& aString,PRInt32 aCount); +nsString& Append(const char* aString,PRInt32 aCount=-1); +nsString& Append(const PRUnichar* aString,PRInt32 aCount=-1); +nsString& Append(char aChar); +nsString& Append(PRUnichar aChar); +nsString& Append(PRInt32 aInteger,PRInt32 aRadix=10); //radix=8,10 or 16 +nsString& Append(float aFloat); /* * Copies n characters from this string to given string, @@ -475,7 +468,7 @@ nsString2& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRUint32 Left(nsString2& aCopy,PRInt32 aCount) const; +PRUint32 Left(nsString& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -487,7 +480,7 @@ PRUint32 Left(nsString2& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRUint32 Mid(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount) const; +PRUint32 Mid(nsString& aCopy,PRUint32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -498,7 +491,7 @@ PRUint32 Mid(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRUint32 Right(nsString2& aCopy,PRInt32 aCount) const; +PRUint32 Right(nsString& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -509,7 +502,7 @@ PRUint32 Right(nsString2& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const nsString& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -519,8 +512,8 @@ nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -nsString2& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); -nsString2& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const char* aChar,PRUint32 anOffset,PRInt32 aCount=-1); +nsString& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a single char into this string at @@ -530,8 +523,8 @@ nsString2& Insert(const PRUnichar* aChar,PRUint32 anOffset,PRInt32 aCount=-1); * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -//nsString2& Insert(char aChar,PRUint32 anOffset); -nsString2& Insert(PRUnichar aChar,PRUint32 anOffset); +//nsString& Insert(char aChar,PRUint32 anOffset); +nsString& Insert(PRUnichar aChar,PRUint32 anOffset); /* * This method is used to cut characters in this string @@ -541,7 +534,7 @@ nsString2& Insert(PRUnichar aChar,PRUint32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString2& Cut(PRUint32 anOffset,PRInt32 aCount); +nsString& Cut(PRUint32 anOffset,PRInt32 aCount); /********************************************************************** @@ -564,11 +557,22 @@ PRInt32 BinarySearch(PRUnichar aChar) const; * @param aString is substring to be sought in this * @return offset in string, or -1 (kNotFound) */ -PRInt32 Find(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 Find(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 Find(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + + +/** + * Search for given char within this string + * + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) + */ PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 FindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; /** * This method searches this string for the first character @@ -577,20 +581,9 @@ PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) cons * @param anOffset tells us where to start searching in this * @return -1 if not found, else the offset in this */ -PRInt32 FindCharInSet(const char* aString,PRUint32 anOffset=0) const; -PRInt32 FindCharInSet(const PRUnichar* aString,PRUint32 anOffset=0) const; -PRInt32 FindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; - -/** - * This method searches this string for the last character - * found in the given string - * @param aString contains set of chars to be found - * @param anOffset tells us where to start searching in this - * @return -1 if not found, else the offset in this - */ -PRInt32 RFindCharInSet(const char* aString,PRUint32 anOffset=0) const; -PRInt32 RFindCharInSet(const PRUnichar* aString,PRUint32 anOffset=0) const; -PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; +PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 FindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; /** @@ -599,11 +592,34 @@ PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; * @param aIgnoreCase tells us whether or not to do caseless compare * @return offset in string, or -1 (kNotFound) */ -PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; +PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + + +/** + * Search for given char within this string + * + * @param aString is substring to be sought in this + * @param anOffset tells us where in this strig to start searching + * @param aIgnoreCase selects case sensitivity + * @return find pos in string, or -1 (kNotFound) + */ PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; +PRInt32 RFindChar(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRInt32 anOffset=-1) const; + +/** + * This method searches this string for the last character + * found in the given string + * @param aString contains set of chars to be found + * @param anOffset tells us where to start searching in this + * @return -1 if not found, else the offset in this + */ +PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const PRUnichar* aString,PRInt32 anOffset=-1) const; +PRInt32 RFindCharInSet(const nsStr& aString,PRInt32 anOffset=-1) const; + /********************************************************************** Comparison methods... @@ -616,7 +632,7 @@ PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) con * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -virtual PRInt32 Compare(const nsString2& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; +virtual PRInt32 Compare(const nsString& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const nsStr &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aLength=-1) const; @@ -626,7 +642,7 @@ virtual PRInt32 Compare(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRI * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator==(const nsString2 &aString) const; +PRBool operator==(const nsString &aString) const; PRBool operator==(const nsStr &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; @@ -636,7 +652,7 @@ PRBool operator==(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE */ -PRBool operator!=(const nsString2 &aString) const; +PRBool operator!=(const nsString &aString) const; PRBool operator!=(const nsStr &aString) const; PRBool operator!=(const char* aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -646,7 +662,7 @@ PRBool operator!=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<(const nsString2 &aString) const; +PRBool operator<(const nsString &aString) const; PRBool operator<(const nsStr &aString) const; PRBool operator<(const char* aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -656,7 +672,7 @@ PRBool operator<(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>(const nsString2 &aString) const; +PRBool operator>(const nsString &aString) const; PRBool operator>(const nsStr &S) const; PRBool operator>(const char* aString) const; PRBool operator>(const PRUnichar* aString) const; @@ -666,7 +682,7 @@ PRBool operator>(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<=(const nsString2 &aString) const; +PRBool operator<=(const nsString &aString) const; PRBool operator<=(const nsStr &S) const; PRBool operator<=(const char* aString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -676,7 +692,7 @@ PRBool operator<=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>=(const nsString2 &aString) const; +PRBool operator>=(const nsString &aString) const; PRBool operator>=(const nsStr &S) const; PRBool operator>=(const char* aString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -688,20 +704,18 @@ PRBool operator>=(const PRUnichar* aString) const; * optimization. * * @param aString -- the string to compare to this - * @param aLength -- optional length of given string. + * @param aCount -- number of chars to be compared. * @return TRUE if equal */ -PRBool Equals(const nsString2 &aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const char* aString,PRUint32 aCount,PRBool aIgnoreCase) const; -PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRBool Equals(const PRUnichar* aString,PRUint32 aCount,PRBool aIgnoreCase) const; +PRBool Equals(const nsString &aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const nsStr& aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const char* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; +PRBool Equals(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE,PRInt32 aCount=-1) const; PRBool Equals(const nsIAtom* anAtom,PRBool aIgnoreCase) const; PRBool Equals(const PRUnichar* s1, const PRUnichar* s2,PRBool aIgnoreCase=PR_FALSE) const; -PRBool EqualsIgnoreCase(const nsString2& aString) const; -PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; +PRBool EqualsIgnoreCase(const nsString& aString) const; +PRBool EqualsIgnoreCase(const char* aString,PRInt32 aCount=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; PRBool EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const; @@ -730,8 +744,8 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -static void Recycle(nsString2* aString); -static nsString2* CreateString(eCharSize aCharSize=eTwoByte); +static void Recycle(nsString* aString); +static nsString* CreateString(eCharSize aCharSize=eTwoByte); virtual void DebugDump(ostream& aStream) const; @@ -739,8 +753,8 @@ virtual void DebugDump(ostream& aStream) const; }; -extern NS_COM int fputs(const nsString2& aString, FILE* out); -ostream& operator<<(ostream& aStream,const nsString2& aString); +extern NS_COM int fputs(const nsString& aString, FILE* out); +ostream& operator<<(ostream& aStream,const nsString& aString); /************************************************************** @@ -749,36 +763,30 @@ ostream& operator<<(ostream& aStream,const nsString2& aString); If the buffer needs to grow, it gets reallocated on the heap. **************************************************************/ -class NS_COM nsAutoString2 : public nsString2 { +class NS_COM nsAutoString : public nsString { public: - nsAutoString2(eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(nsStr& anExtBuffer,const char* aCString); + nsAutoString(eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const char* aCString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); + nsAutoString(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize,PRInt32 aLength=-1); - - nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(char* aCString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); - nsAutoString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(PRUnichar* aString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); - - nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(CSharedStrBuffer& aBuffer); + nsAutoString(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString(const nsAutoString& aString,eCharSize aCharSize=kDefaultCharSize); #ifdef AIX - nsAutoString2(const nsSubsumeStr& aSubsumeStr); // AIX requires a const + nsAutoString(const nsSubsumeStr& aSubsumeStr); // AIX requires a const #else - nsAutoString2(nsSubsumeStr& aSubsumeStr); + nsAutoString(nsSubsumeStr& aSubsumeStr); #endif // AIX - nsAutoString2(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); - virtual ~nsAutoString2(); + nsAutoString(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); + virtual ~nsAutoString(); - nsAutoString2& operator=(const nsString2& aString) {nsString2::operator=(aString); return *this;} - nsAutoString2& operator=(const nsStr& aString) {nsString2::Assign(aString); return *this;} - nsAutoString2& operator=(const nsAutoString2& aString) {nsString2::operator=(aString); return *this;} - nsAutoString2& operator=(const char* aCString) {nsString2::operator=(aCString); return *this;} - nsAutoString2& operator=(char aChar) {nsString2::operator=(aChar); return *this;} - nsAutoString2& operator=(const PRUnichar* aBuffer) {nsString2::operator=(aBuffer); return *this;} - nsAutoString2& operator=(PRUnichar aChar) {nsString2::operator=(aChar); return *this;} + nsAutoString& operator=(const nsStr& aString) {nsString::Assign(aString); return *this;} + nsAutoString& operator=(const nsAutoString& aString) {nsString::operator=(aString); return *this;} + nsAutoString& operator=(const char* aCString) {nsString::operator=(aCString); return *this;} + nsAutoString& operator=(char aChar) {nsString::operator=(aChar); return *this;} + nsAutoString& operator=(const PRUnichar* aBuffer) {nsString::operator=(aBuffer); return *this;} + nsAutoString& operator=(PRUnichar aChar) {nsString::operator=(aChar); return *this;} /** * Retrieve the size of this string @@ -803,24 +811,14 @@ public: You should probably not use this class unless you really know what you're doing. ***************************************************************/ -class NS_COM nsSubsumeStr : public nsString2 { +class NS_COM nsSubsumeStr : public nsString { public: - nsSubsumeStr(nsString2& aString); nsSubsumeStr(nsStr& aString); nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; -/*************************************************************** - - ***************************************************************/ -class NS_COM nsCAutoString: public nsAutoString{ -public: - nsCAutoString(const nsString2& aString); - operator const char*() const; -}; - #endif