diff --git a/mozilla/base/src/bufferRoutines.h b/mozilla/base/src/bufferRoutines.h index e327f3bd09e..d30785a93cb 100644 --- a/mozilla/base/src/bufferRoutines.h +++ b/mozilla/base/src/bufferRoutines.h @@ -31,6 +31,13 @@ #include "nsCRT.h" +#ifndef RICKG_TESTBED +#include "nsUnicharUtilCIID.h" +#include "nsIServiceManager.h" +#include "nsICaseConversion.h" +#endif + + inline PRUnichar GetUnicharAt(const char* aString,PRUint32 anIndex) { return ((PRUnichar*)aString)[anIndex]; } @@ -45,11 +52,6 @@ inline PRUnichar GetCharAt(const char* aString,PRUint32 anIndex) { // The functions are differentiated by shift direction and the underlying charsize. // -PRInt32 TrimChars2(char* aString,PRUint32 aLength,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing); -PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing); -PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing); - - /** * This method shifts single byte characters left by a given amount from an given offset. * @update gess 01/04/99 @@ -60,7 +62,7 @@ PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet,PRUint32 */ void ShiftCharsLeft(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount) { PRUint32 theMax=aLength-anOffset; -// PRUint32 theLength=(theMax=0;theIndex--){ @@ -358,7 +360,7 @@ inline PRInt32 RFindChar1(const char* aDest,PRUint32 aLength,PRUint32 anOffset,P * @param aIgnorecase tells us whether to use a case sensitive search * @return index of pos if found, else -1 (kNotFound) */ -inline PRInt32 RFindChar2(const char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUnichar aChar,PRBool aIgnoreCase) { +inline PRInt32 RFindChar2(const char* aDest,PRUint32 aLength,PRUint32 anOffset,const PRUnichar aChar,PRBool aIgnoreCase) { PRUnichar theCmpChar=(aIgnoreCase ? nsCRT::ToUpper(aChar) : aChar); PRUint32 theIndex=0; for(theIndex=aLength-1;theIndex>=0;theIndex--){ @@ -371,7 +373,7 @@ inline PRInt32 RFindChar2(const char* aDest,PRUint32 aLength,PRUint32 anOffset,P return kNotFound; } -typedef PRInt32 (*FindChars)(const char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUnichar aChar,PRBool aIgnoreCase); +typedef PRInt32 (*FindChars)(const char* aDest,PRUint32 aLength,PRUint32 anOffset,const PRUnichar aChar,PRBool aIgnoreCase); FindChars gFindChars[]={&FindChar1,&FindChar2}; FindChars gRFindChars[]={&RFindChar1,&RFindChar2}; @@ -499,6 +501,53 @@ PRInt32 ConvertCase1(char* aString,PRUint32 aCount,PRBool aToUpper){ return result; } +//---------------------------------------------------------------------------------------- + +#ifndef RICKG_TESTBED +class HandleCaseConversionShutdown3 : public nsIShutdownListener { +public : + NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); + HandleCaseConversionShutdown3(void) { NS_INIT_REFCNT(); } + virtual ~HandleCaseConversionShutdown3(void) {} + NS_DECL_ISUPPORTS +}; + +static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); +static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); +static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); +static nsICaseConversion * gCaseConv = 0; + +NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown3, kIShutdownListenerIID); + +nsresult HandleCaseConversionShutdown3::OnShutdown(const nsCID& cid, nsISupports* service) { + if (cid.Equals(kUnicharUtilCID)) { + NS_ASSERTION(service == gCaseConv, "wrong service!"); + if(gCaseConv){ + gCaseConv->Release(); + gCaseConv = 0; + } + } + return NS_OK; +} + + +class CCaseConversionServiceInitializer { +public: + CCaseConversionServiceInitializer(){ + mListener = new HandleCaseConversionShutdown3(); + if(mListener){ + mListener->AddRef(); + nsresult result=nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID,(nsISupports**) &gCaseConv, mListener); + } + } +protected: + HandleCaseConversionShutdown3* mListener; +}; + +#endif + +//---------------------------------------------------------------------------------------- + /** * This method performs a case conversion the data in the given buffer * @@ -509,14 +558,25 @@ PRInt32 ConvertCase1(char* aString,PRUint32 aCount,PRBool aToUpper){ * @return 0 */ PRInt32 ConvertCase2(char* aString,PRUint32 aCount,PRBool aToUpper){ + PRUnichar* cp = (PRUnichar*)aString; + PRUnichar* end = cp + aCount-1; PRInt32 result=0; - typedef PRUnichar chartype; - chartype* cp = (chartype*)aString; - chartype* end = cp + aCount-1; +#ifndef RICKG_TESTBED + static CCaseConversionServiceInitializer gCaseConversionServiceInitializer; + + // I18N code begin + if(gCaseConv) { + nsresult err=(aToUpper) ? gCaseConv->ToUpper(cp, cp, aCount) : gCaseConv->ToLower(cp, cp, aCount); + if(NS_SUCCEEDED(err)) + return 0; + } + // I18N code end +#endif + while (cp <= end) { - chartype ch = *cp; + PRUnichar ch = *cp; if(aToUpper) { if ((ch >= 'a') && (ch <= 'z')) { *cp = 'A' + (ch - 'a'); @@ -552,7 +612,7 @@ CaseConverters gCaseConverters[]={&ConvertCase1,&ConvertCase2}; * @return the new length of the given buffer */ PRInt32 StripChars1(char* aString,PRUint32 anOffset,PRUint32 aCount,const char* aSet){ -// PRInt32 result=0; + PRInt32 result=0; typedef char chartype; chartype* from = (chartype*)&aString[anOffset]; @@ -584,7 +644,7 @@ PRInt32 StripChars1(char* aString,PRUint32 anOffset,PRUint32 aCount,const char* * @return the new length of the given buffer */ PRInt32 StripChars2(char* aString,PRUint32 anOffset,PRUint32 aCount,const char* aSet){ -// PRInt32 result=0; + PRInt32 result=0; typedef PRUnichar chartype; chartype* from = (chartype*)&aString[anOffset]; @@ -625,7 +685,7 @@ StripChars gStripChars[]={&StripChars1,&StripChars2}; * @return the new length of the given buffer */ PRInt32 TrimChars1(char* aString,PRUint32 aLength,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){ -// PRInt32 result=0; + PRInt32 result=0; typedef char chartype; chartype* from = (chartype*)aString; @@ -748,7 +808,7 @@ TrimChars gTrimChars[]={&TrimChars1,&TrimChars2}; * @return the new length of the given buffer */ PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){ -// PRInt32 result=0; + PRInt32 result=0; TrimChars1(aString,aLength,aSet,aEliminateLeading,aEliminateTrailing); @@ -794,7 +854,7 @@ PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet,PRUint32 * @return the new length of the given buffer */ PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){ -// PRInt32 result=0; + PRInt32 result=0; TrimChars2(aString,aLength,aSet,aEliminateLeading,aEliminateTrailing); diff --git a/mozilla/base/src/nsStr.cpp b/mozilla/base/src/nsStr.cpp index 79e7afc9910..4d3fc6ab211 100644 --- a/mozilla/base/src/nsStr.cpp +++ b/mozilla/base/src/nsStr.cpp @@ -31,12 +31,104 @@ #include "nsStr.h" #include "bufferRoutines.h" #include "stdio.h" //only used for printf +#include "nsDeque.h" +#include "nsCRT.h" + static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; - //---------------------------------------------------------------------------------------- +// The following is a memory agent who knows how to recycled (pool) freed memory... +//---------------------------------------------------------------------------------------- + +/************************************************************** + Define the char* (pooled) deallocator class... + **************************************************************/ +class nsBufferDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + char* aCString= (char*)anObject; + delete [] aCString; + return 0; + } +}; +/** + * + * @update gess10/30/98 + * @param + * @return + */ +class nsPoolingMemoryAgent : public nsMemoryAgent{ +public: + nsPoolingMemoryAgent() { + memset(mPools,0,sizeof(mPools)); + } + + ~nsPoolingMemoryAgent() { + nsBufferDeallocator theDeallocator; + int i=0; + for(i=0;i<10;i++){ + if(mPools[i]){ + mPools[i]->ForEach(theDeallocator); //now delete the buffers + } + delete mPools[i]; + mPools[i]=0; + } + } + + virtual PRBool Alloc(nsStr& aDest,PRInt32 aCount) { + + //we're given the acount value in charunits; we have to scale up by the charsize. + int theShift=4; + PRInt32 theNewCapacity=eDefaultSize; + while(theNewCapacityPop(); + } + if(!aDest.mStr) { + //we're given the acount value in charunits; we have to scale up by the charsize. + size_t theSize=(theNewCapacity<Push(aDest.mStr); + } + else delete [] aDest.mStr; //it's too big. Just delete it. + } + aDest.mStr=0; + aDest.mOwnsBuffer=0; + return PR_TRUE; + } + return PR_FALSE; + } + nsDeque* mPools[16]; +}; + +static char* gCommonEmptyBuffer=0; /** * * @update gess10/30/98 @@ -44,9 +136,8 @@ static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; * @return */ char* GetSharedEmptyBuffer() { - static char* gCommonEmptyBuffer=0; if(!gCommonEmptyBuffer) { - const size_t theDfltSize=25; + const size_t theDfltSize=5; gCommonEmptyBuffer=new char[theDfltSize]; if(gCommonEmptyBuffer){ nsCRT::zero(gCommonEmptyBuffer,theDfltSize); @@ -69,9 +160,24 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { aDest.mStr=GetSharedEmptyBuffer(); aDest.mLength=0; aDest.mCapacity=0; - aDest.mMultibyte=aCharSize; + aDest.mCharSize=aCharSize; aDest.mOwnsBuffer=0; - NS_ASSERTION(aDest.mStr[0]==0,kFoolMsg); + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); +} + +/** + * + * @update gess10/30/98 + * @param + * @return + */ +void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer){ + aDest.mStr=(aCString) ? aCString : GetSharedEmptyBuffer(); + aDest.mLength=aLength; + aDest.mCapacity=aCapacity; + aDest.mCharSize=aCharSize; + aDest.mOwnsBuffer=aOwnsBuffer; + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -81,13 +187,9 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { * @return */ nsIMemoryAgent* GetDefaultAgent(void){ - static nsIMemoryAgent* gDefaultAgent=0; - if(!gDefaultAgent) - gDefaultAgent=new nsMemoryAgent(); - - NS_ASSERTION(gDefaultAgent,"You MUST always have an allocator!"); - - return gDefaultAgent; +// static nsPoolingMemoryAgent gDefaultAgent; + static nsMemoryAgent gDefaultAgent; + return (nsIMemoryAgent*)&gDefaultAgent; } /** @@ -110,20 +212,6 @@ void nsStr::Destroy(nsStr& aDest,nsIMemoryAgent* anAgent) { } } -/** - * - * @update gess11/12/98 - * @param - * @return - */ -PRUnichar nsStr::GetCharAt(const nsStr& aDest,PRUint32 anIndex) { - PRUnichar result=0; - if((anIndex>=0) && (anIndexRealloc(aString,aNewLength); AddNullTerminator(aString); } + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -150,13 +239,13 @@ void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* an void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { if(aNewLength>aDest.mCapacity) { nsStr theTempStr; - nsStr::Initialize(theTempStr,(eCharSize)aDest.mMultibyte); + nsStr::Initialize(theTempStr,(eCharSize)aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); EnsureCapacity(theTempStr,aNewLength,theAgent); - if(0Free(aDest); aDest.mStr = theTempStr.mStr; @@ -165,6 +254,7 @@ void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgen aDest.mCapacity=theTempStr.mCapacity; aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; } + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -182,13 +272,12 @@ void nsStr::Assign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 a /** * This method appends the given nsStr to this one. Note that we have to * pay attention to the underlying char-size of both structs. - * @update gess 04/04/99 + * @update gess10/30/98 * @param aDest is the nsStr to be manipulated * @param aSource is where char are copied from * @aCount is the number of bytes to be copied */ void nsStr::Append(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount,nsIMemoryAgent* anAgent){ - if(anOffset0) && (aTarget.mLength>0)){ PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=nsStr::GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=nsStr::GetCharAt(aTarget,aTarget.mLength-1); + PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); + PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); PRInt32 theTargetMax=aTarget.mLength; while(++index<=theMax) { PRInt32 theSubIndex=-1; PRBool matches=PR_TRUE; while((++theSubIndex0) { if(theFirstTargetChar==theChar){ - PRUnichar theDestJumpChar=nsStr::GetCharAt(aDest,index+theTargetMax); + PRUnichar theDestJumpChar=GetCharAt(aDest,index+theTargetMax); if(theDestJumpChar==theLastTargetChar) { theNewStartPos=index; //this lets us jump ahead during our search where possible. }//if }//if }//if - PRUnichar theTargetChar=nsStr::GetCharAt(aTarget,theSubIndex); + PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex); matches=PRBool(theChar==theTargetChar); } if(matches) @@ -384,8 +478,8 @@ PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIgn * @param * @return */ -PRInt32 nsStr::FindChar(const nsStr& aDest, PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 result=gFindChars[aDest.mMultibyte](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); +PRInt32 nsStr::FindChar(const nsStr& aDest,const PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { + PRInt32 result=gFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); return result; } @@ -403,7 +497,7 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore while(++index0) && (aTarget.mLength>0)){ PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=nsStr::GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=nsStr::GetCharAt(aTarget,aTarget.mLength-1); + PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); + PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); PRInt32 theTargetMax=aTarget.mLength; while(index--) { @@ -429,16 +523,16 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIg if(anOffset+aTarget.mLength<=aDest.mLength) { while((++theSubIndex0) { if(theFirstTargetChar==theChar){ - PRUnichar theDestJumpChar=nsStr::GetCharAt(aDest,index+theTargetMax); + PRUnichar theDestJumpChar=GetCharAt(aDest,index+theTargetMax); if(theDestJumpChar==theLastTargetChar) { theNewStartPos=index; //this lets us jump ahead during our search where possible. }//if }//if }//if - PRUnichar theTargetChar=nsStr::GetCharAt(aTarget,theSubIndex); + PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex); matches=PRBool(theChar==theTargetChar); } //while } //if @@ -459,8 +553,8 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIg * @param * @return */ -PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 result=gRFindChars[aDest.mMultibyte](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); +PRInt32 nsStr::RFindChar(const nsStr& aDest,const PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { + PRInt32 result=gRFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); return result; } @@ -472,12 +566,12 @@ PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase, * @return */ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 offset=aDest.mLength-anOffset; + PRUint32 offset=aDest.mLength-anOffset; PRInt32 thePos; while(--offset>=0) { - PRUnichar theChar=GetCharAt(aDest,PRUint32(offset)); - thePos=gRFindChars[aSet.mMultibyte](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); + PRUnichar theChar=GetCharAt(aDest,offset); + thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,anOffset,theChar,aIgnoreCase); if(kNotFound!=thePos) return offset; } //while @@ -491,7 +585,7 @@ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnor * @param * @return aDestaSource=1 */ -PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 /*aCount*/,PRBool aIgnoreCase) { +PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { int minlen=(aSource.mLength>aCharSize)-1), - mOwnsBuffer(aOwnsBuffer), - mMultibyte(aCharSize) -{ - mStr=aBuffer; - } - - PRUint32 mCapacity; - PRBool mOwnsBuffer; - eCharSize mMultibyte; -// UStrPtr mStr; - union { - char* mStr; - PRUnichar* mUStr; - }; -}; +const PRInt32 kNotFound = -1; class nsIMemoryAgent; @@ -99,6 +67,15 @@ struct nsStr { */ static void Initialize(nsStr& aDest,eCharSize aCharSize); + /** + * This method initializes an nsStr for use + * + * @update gess 01/04/99 + * @param aString is the nsStr to be initialized + * @param aCharSize tells us the requested char size (1 or 2 bytes) + */ + static void Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer); + /** * This method destroys the given nsStr, and *MAY* * deallocate it's memory depending on the setting @@ -168,7 +145,7 @@ struct nsStr { * @param aCount tells us the (max) # of chars to delete * @param anAgent is the allocator to be used for alloc/free operations */ - static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,nsIMemoryAgent* anAgent=0); + static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount,nsIMemoryAgent* anAgent=0); /** * This method is used to truncate the given string. @@ -256,21 +233,12 @@ struct nsStr { 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); - /** - * This method is used to access a given char in the given string - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be appended to - * @param anIndex tells us where in dest to get the char from - * @return the given char, or 0 if anIndex is out of range - */ - static PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex); - PRUint32 mLength : 30; - eCharSize mMultibyte : 2; - PRUint32 mCapacity: 30; - PRUint32 mOwnsBuffer: 1; - PRUint32 mUnused: 1; + PRUint32 mLength; + PRUint32 mCapacity; + eCharSize mCharSize; + PRBool mOwnsBuffer; + union { char* mStr; PRUnichar* mUStr; @@ -289,26 +257,27 @@ inline PRInt32 MaxInt(PRInt32 anInt1,PRInt32 anInt2){ return (anInt1aMax) - aValue=aMax; -} - -inline void ToRange(PRUint32& aValue,PRUint32 aMin,PRUint32 aMax){ - if(aValueaMax) - aValue=aMax; -} - inline void AddNullTerminator(nsStr& aDest) { - if(eTwoByte==aDest.mMultibyte) + if(eTwoByte==aDest.mCharSize) aDest.mUStr[aDest.mLength]=0; else aDest.mStr[aDest.mLength]=0; } +/** +* This method is used to access a given char in the given string +* +* @update gess 01/04/99 +* @param aDest is the nsStr to be appended to +* @param anIndex tells us where in dest to get the char from +* @return the given char, or 0 if anIndex is out of range +*/ +inline PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex){ + if(anIndex 64) { - // When the string starts getting large, double the capacity as we grow. - theNewCapacity = aDest.mCapacity * 2; - if (theNewCapacity < aCount) { - theNewCapacity = aDest.mCapacity + aCount; - } - } else { - // When the string is small, keep it's capacity a multiple of kGrowthDelta - PRInt32 unitDelta=(aCount/eGrowthDelta)+1; - theNewCapacity=unitDelta*eGrowthDelta; + //we're given the acount value in charunits; now scale up to next multiple. + PRUint32 theNewCapacity=eDefaultSize; + while(theNewCapacitySetString(aUnicodeStr,len); } + } @@ -233,7 +237,7 @@ nsString::nsString(const PRUnichar* aUnicodeStr){ * Destructor * @update gess7/30/98 */ -nsString::~nsString() +nsString1::~nsString1() { if(mStr && (mStr!=kCommonEmptyBuffer)) delete [] mStr; @@ -249,14 +253,14 @@ nsString::~nsString() * @param anIndex -- new length of string * @return nada */ -void nsString::Truncate(PRInt32 anIndex) { +void nsString1::Truncate(PRInt32 anIndex) { if((anIndex>-1) && (anIndexAdd(sizeof(*this)); aHandler->Add(mCapacity * sizeof(chartype)); @@ -269,7 +273,7 @@ void nsString::SizeOf(nsISizeOfHandler* aHandler) const * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString::IsOrdered(void) const { +PRBool nsString1::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { PRInt32 theIndex; @@ -290,7 +294,7 @@ PRBool nsString::IsOrdered(void) const { * @param aNewLength -- new capacity of string * @return void */ -void nsString::EnsureCapacityFor(PRInt32 aNewLength) +void nsString1::EnsureCapacityFor(PRInt32 aNewLength) { PRInt32 newCapacity; @@ -328,12 +332,12 @@ void nsString::EnsureCapacityFor(PRInt32 aNewLength) * @param aLength -- contains new length for mStr * @return */ -void nsString::SetLength(PRInt32 aLength) { +void nsString1::SetLength(PRInt32 aLength) { if(aLength>mLength) { EnsureCapacityFor(aLength); nsCRT::zero(mStr + mLength, (aLength - mLength) * sizeof(chartype)); } - if((aLength>0) && (aLength=0) && (aLength aBufLength) ? aBufLength : mLength; @@ -712,7 +723,7 @@ char* nsString::ToCString(char* aBuf, PRInt32 aBufLength) const * @param aErrorCode will contain error if one occurs * @return float rep of string value */ -float nsString::ToFloat(PRInt32* aErrorCode) const +float nsString1::ToFloat(PRInt32* aErrorCode) const { char buf[40]; if (mLength > PRInt32(sizeof(buf)-1)) { @@ -735,7 +746,7 @@ float nsString::ToFloat(PRInt32* aErrorCode) const * @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 */ -PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { +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; @@ -803,7 +814,7 @@ PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { * @param PRUnichar: buffer to be assigned to this * @return this */ -nsString& nsString::SetString(const PRUnichar* aStr,PRInt32 aLength) { +nsString1& nsString1::SetString(const PRUnichar* aStr,PRInt32 aLength) { if((0 == aLength) || (nsnull == aStr)) { mLength=0; if (nsnull != mStr) { @@ -829,7 +840,7 @@ nsString& nsString::SetString(const PRUnichar* aStr,PRInt32 aLength) { * @param aCString: buffer to be assigned to this * @return this */ -nsString& nsString::SetString(const char* aCString,PRInt32 aLength) { +nsString1& nsString1::SetString(const char* aCString,PRInt32 aLength) { if(aCString!=0) { PRInt32 len=(aLength<0) ? nsCRT::strlen(aCString) : aLength; if(mCapacity<=len) @@ -847,6 +858,29 @@ nsString& nsString::SetString(const char* aCString,PRInt32 aLength) { mLength=0; //This little bit of code handles the case mStr[0]=0; //where some blockhead hands us a null string } + + return *this; +} + +/** + * 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 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); } @@ -866,8 +900,8 @@ nsString& nsString::operator=(const PRUnichar* aStr) { * @param aString: string to be added to this * @return this */ -nsString& nsString::operator=(const nsString& aString) { - return this->SetString(aString.mStr,aString.mLength); +nsString1& nsString1::operator=(const nsString1& aString) { + return this->SetString(aString); } @@ -877,7 +911,7 @@ nsString& nsString::operator=(const nsString& aString) { * @param aCString: buffer to be assigned to this * @return this */ -nsString& nsString::operator=(const char* aCString) { +nsString1& nsString1::operator=(const char* aCString) { return SetString(aCString); } @@ -888,7 +922,7 @@ nsString& nsString::operator=(const char* aCString) { * @param aChar: char to be assignd to this * @return this */ -nsString& nsString::operator=(char aChar) { +nsString1& nsString1::operator=(char aChar) { return this->operator=(PRUnichar(aChar)); } @@ -898,7 +932,7 @@ nsString& nsString::operator=(char aChar) { * @param aChar: char to be assignd to this * @return this */ -nsString& nsString::operator=(PRUnichar aChar) { +nsString1& nsString1::operator=(PRUnichar aChar) { if(mCapacity<1) EnsureCapacityFor(kGrowthDelta); mStr[0]=aChar; @@ -913,7 +947,7 @@ nsString& nsString::operator=(PRUnichar aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const nsString& aString,PRInt32 aLength) { +nsString1& nsString1::Append(const nsString1& aString,PRInt32 aLength) { return Append(aString.mStr,aString.mLength); } @@ -923,7 +957,8 @@ nsString& nsString::Append(const nsString& aString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const char* aCString,PRInt32 aLength) { +nsString1& nsString1::Append(const char* aCString,PRInt32 aLength) { + if(aCString!=0) { PRInt32 len=(aLength<0) ? strlen(aCString) : aLength; if(mLength+len >= mCapacity) { @@ -947,7 +982,7 @@ nsString& nsString::Append(const char* aCString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(char aChar) { +nsString1& nsString1::Append(char aChar) { return Append(PRUnichar(aChar)); } @@ -957,7 +992,8 @@ nsString& nsString::Append(char aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const PRUnichar* aString,PRInt32 aLength) { +nsString1& nsString1::Append(const PRUnichar* aString,PRInt32 aLength) { + if(aString!=0) { PRInt32 len=(aLength<0) ? nsCRT::strlen(aString) : aLength; if(mLength+len >= mCapacity) { @@ -978,7 +1014,7 @@ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(PRUnichar aChar) { +nsString1& nsString1::Append(PRUnichar aChar) { if(mLength < mCapacity) { mStr[mLength++]=aChar; // the new string len < capacity, so just copy mStr[mLength]=0; @@ -998,7 +1034,7 @@ nsString& nsString::Append(PRUnichar aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::operator+=(const nsString &aString) { +nsString1& nsString1::operator+=(const nsString1 &aString) { return this->Append(aString.mStr,aString.mLength); } @@ -1009,7 +1045,7 @@ nsString& nsString::operator+=(const nsString &aString) { * @param aCString: buffer to be appended to this * @return this */ -nsString& nsString::operator+=(const char* aCString) { +nsString1& nsString1::operator+=(const char* aCString) { return Append(aCString); } @@ -1020,7 +1056,7 @@ nsString& nsString::operator+=(const char* aCString) { * @param aBuffer: buffer to be appended to this * @return this */ -nsString& nsString::operator+=(const PRUnichar* aBuffer) { +nsString1& nsString1::operator+=(const PRUnichar* aBuffer) { return Append(aBuffer); } @@ -1031,7 +1067,7 @@ nsString& nsString::operator+=(const PRUnichar* aBuffer) { * @param aChar: char to be appended to this * @return this */ -nsString& nsString::operator+=(PRUnichar aChar) { +nsString1& nsString1::operator+=(PRUnichar aChar) { return Append(aChar); } @@ -1041,7 +1077,7 @@ nsString& nsString::operator+=(PRUnichar aChar) { * @param * @return */ -nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { +nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { char* fmt = "%d"; if (8 == aRadix) { fmt = "%o"; @@ -1061,7 +1097,7 @@ nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { * @param * @return */ -nsString& nsString::Append(float aFloat){ +nsString1& nsString1::Append(float aFloat){ char buf[40]; PR_snprintf(buf, sizeof(buf), "%g", aFloat); Append(buf); @@ -1080,7 +1116,7 @@ nsString& nsString::Append(float aFloat){ * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 nsString::Left(nsString& aCopy,PRInt32 aCount) const { +PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { return Mid(aCopy,0,aCount); } @@ -1095,7 +1131,7 @@ PRInt32 nsString::Left(nsString& aCopy,PRInt32 aCount) const { * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 nsString::Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const { +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) { @@ -1191,7 +1228,7 @@ PRInt32 nsString::Insert(const nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -PRInt32 nsString::Insert(PRUnichar aChar,PRInt32 anOffset){ +PRInt32 nsString1::Insert(PRUnichar aChar,PRInt32 anOffset){ //1st optimization: If you're inserting at end, then simply append! if(anOffset= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { return PR_TRUE; @@ -1292,7 +1330,7 @@ PRBool nsString::IsAlpha(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if is valid space char */ -PRBool nsString::IsSpace(PRUnichar aChar) { +PRBool nsString1::IsSpace(PRUnichar aChar) { // XXX i18n if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { return PR_TRUE; @@ -1306,7 +1344,7 @@ PRBool nsString::IsSpace(PRUnichar aChar) { * @update gess 3/31/98 * @param aChar is character to be tested * @return TRUE if char is a valid digit - */PRBool nsString::IsDigit(PRUnichar aChar) { + */PRBool nsString1::IsDigit(PRUnichar aChar) { // XXX i18n return PRBool((aChar >= '0') && (aChar <= '9')); } @@ -1321,10 +1359,11 @@ PRBool nsString::IsSpace(PRUnichar aChar) { * both ends * @return this */ -nsString& nsString::Trim(const char* aTrimSet, +nsString1& nsString1::Trim(const char* aTrimSet, PRBool aEliminateLeading, PRBool aEliminateTrailing) { + PRUnichar* from = mStr; PRUnichar* end = mStr + mLength-1; PRUnichar* to = mStr; @@ -1377,10 +1416,9 @@ nsString& nsString::Trim(const char* aTrimSet, * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString& nsString::CompressWhitespace( PRBool aEliminateLeading, +nsString1& nsString1::CompressWhitespace( PRBool aEliminateLeading, PRBool aEliminateTrailing) { - Trim(" \r\n\t",aEliminateLeading,aEliminateTrailing); PRUnichar* from = mStr; @@ -1416,7 +1454,7 @@ nsString& nsString::CompressWhitespace( PRBool aEliminateLeading, * @update gess 7/27/98 * @return this */ -nsString& nsString::StripWhitespace() +nsString1& nsString1::StripWhitespace() { Trim(" \r\n\t"); return StripChars("\r\t\n"); @@ -1429,7 +1467,8 @@ nsString& nsString::StripWhitespace() * @param * @return *this */ -nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { +nsString1& nsString1::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { + PRUnichar* from = mStr; PRUnichar* end = mStr + mLength; @@ -1440,8 +1479,7 @@ nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { } from++; } - - return *this; +return *this; } /** @@ -1453,7 +1491,7 @@ nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { * @param aChar is the unicode char to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::BinarySearch(PRUnichar aChar) const { +PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { PRInt32 low=0; PRInt32 high=mLength-1; @@ -1476,7 +1514,7 @@ PRInt32 nsString::BinarySearch(PRUnichar aChar) const { * @param aCStringBuf - charstr to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const char* aCStringBuf) const{ +PRInt32 nsString1::Find(const char* aCStringBuf) const{ NS_ASSERTION(0!=aCStringBuf,kNullPointerError); PRInt32 result=kNotFound; if(aCStringBuf) { @@ -1498,7 +1536,7 @@ PRInt32 nsString::Find(const char* aCStringBuf) const{ * @param aString - PUnichar* to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const PRUnichar* aString) const{ +PRInt32 nsString1::Find(const PRUnichar* aString) const{ NS_ASSERTION(0!=aString,kNullPointerError); PRInt32 result=kNotFound; if(aString) { @@ -1518,10 +1556,10 @@ PRInt32 nsString::Find(const PRUnichar* aString) const{ * Search for given buffer within this string * * @update gess 3/25/98 - * @param nsString -- buffer to be found + * @param nsString1 -- buffer to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const nsString& aString) const{ +PRInt32 nsString1::Find(const nsString1& aString) const{ PRInt32 result=kNotFound; PRInt32 len=aString.mLength; @@ -1545,7 +1583,7 @@ PRInt32 nsString::Find(const nsString& aString) const{ * @param * @return offset of found char, or -1 (kNotFound) */ -PRInt32 nsString::Find(PRUnichar aChar, PRInt32 anOffset) const{ +PRInt32 nsString1::Find(PRUnichar aChar, PRInt32 anOffset) const{ for(PRInt32 i=anOffset;i0;i--){ @@ -1617,7 +1655,7 @@ PRInt32 nsString::RFindCharInSet(const char* anAsciiSet,PRInt32 anOffset) const{ * @param * @return */ -PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{ +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]); @@ -1635,7 +1673,7 @@ PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{ * @param * @return */ -PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=aString,kNullPointerError); if(aString) { @@ -1661,7 +1699,7 @@ PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ * @param * @return */ -PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase) const{ +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--) { @@ -1683,7 +1721,7 @@ PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase) const{ * @param * @return */ -PRInt32 nsString::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=anAsciiSet,kNullPointerError); if(anAsciiSet) { @@ -1711,7 +1749,7 @@ PRInt32 nsString::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ * @param * @return offset of char in string, or -1 (kNotFound) */ -PRInt32 nsString::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ chartype uc=nsCRT::ToUpper(aChar); for(PRInt32 offset=mLength-1;offset>=0;offset--) if(aIgnoreCase) { @@ -1735,7 +1773,7 @@ PRInt32 nsString::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { +PRInt32 nsString1::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { NS_ASSERTION(0!=aCString,kNullPointerError); if(-1!=aLength) { @@ -1769,7 +1807,7 @@ PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLengt * @param * @return */ -PRInt32 nsString::Compare(const nsString &S,PRBool aIgnoreCase) const { +PRInt32 nsString1::Compare(const nsString1 &S,PRBool aIgnoreCase) const { int maxlen=(S.mLength(const nsString &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 nsString::operator<=(const nsString &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 nsString::operator>=(const nsString &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 nsString1::operator==(const nsString1 &S) const {return Equals(S);} +PRBool nsString1::operator==(const char *s) const {return Equals(s);} +PRBool nsString1::operator==(const PRUnichar *s) const {return Equals(s);} +PRBool nsString1::operator==(PRUnichar *s) const {return Equals(s);} +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);} +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);} +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 this to given string; note that we compare full strings here. * * @update gess 7/27/98 - * @param aString is the other nsString to be compared to + * @param aString is the other nsString1 to be compared to * @return TRUE if equal */ -PRBool nsString::Equals(const nsString& aString) const { +PRBool nsString1::Equals(const nsString1& aString) const { if(aString.mLength==mLength) { PRInt32 result=nsCRT::strcmp(mStr,aString.mStr); return PRBool(0==result); @@ -1865,7 +1903,7 @@ PRBool nsString::Equals(const nsString& aString) const { * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString::Equals(const char* aCString,PRInt32 aLength) const{ +PRBool nsString1::Equals(const char* aCString,PRInt32 aLength) const{ NS_ASSERTION(0!=aCString,kNullPointerError); if((aLength>0) && (aLength!=mLength)) @@ -1882,7 +1920,7 @@ PRBool nsString::Equals(const char* aCString,PRInt32 aLength) const{ * @param aAtom -- atom to compare to this * @return TRUE if equal */ -PRBool nsString::Equals(const nsIAtom* aAtom) const +PRBool nsString1::Equals(const nsIAtom* aAtom) const { NS_ASSERTION(0!=aAtom,kNullPointerError); PRInt32 result=nsCRT::strcmp(mStr,aAtom->GetUnicode()); @@ -1897,7 +1935,7 @@ PRBool nsString::Equals(const nsIAtom* aAtom) const * @param s2 -- second string to be compared * @return TRUE if equal */ -PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString1::Equals(const PRUnichar* s1, const PRUnichar* s2) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1915,7 +1953,7 @@ PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2) const { * @param * @return */ -PRBool nsString::EqualsIgnoreCase(const nsString& aString) const{ +PRBool nsString1::EqualsIgnoreCase(const nsString1& aString) const{ if(aString.mLength==mLength) { PRInt32 result=nsCRT::strcasecmp(mStr,aString.mStr); return PRBool(0==result); @@ -1930,7 +1968,7 @@ PRBool nsString::EqualsIgnoreCase(const nsString& aString) const{ * @param * @return */ -PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const{ +PRBool nsString1::EqualsIgnoreCase(const nsIAtom *aAtom) const{ NS_ASSERTION(0!=aAtom,kNullPointerError); PRBool result=PR_FALSE; if(aAtom){ @@ -1949,7 +1987,7 @@ PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const{ * @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 nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString1::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1971,7 +2009,7 @@ PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) cons * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { +PRBool nsString1::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { NS_ASSERTION(0!=aCString,kNullPointerError); if((aLength>0) && (aLength!=mLength)) @@ -1988,7 +2026,7 @@ PRBool nsString::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { * @param * @return */ -void nsString::DebugDump(ostream& aStream) const { +void nsString1::DebugDump(ostream& aStream) const { for(int i=0;i mCapacity) { PRInt32 size = mCapacity * 2; if (size < aNewLength) { @@ -2135,7 +2174,7 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) { } void -nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const +nsAutoString1::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(sizeof(*this)); if (mStr != mBuf) { @@ -2151,8 +2190,8 @@ nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const * @param * @return */ -void nsAutoString::SelfTest(){ - nsAutoString xas("Hello there"); +void nsAutoString1::SelfTest(){ + nsAutoString1 xas("Hello there"); xas.Append("this string exceeds the max size"); xas.DebugDump(cout); } @@ -2163,7 +2202,7 @@ void nsAutoString::SelfTest(){ * @param * @return */ -ostream& operator<<(ostream& os,nsAutoString& aString){ +ostream& operator<<(ostream& os,nsAutoString1& aString){ const PRUnichar* uc=aString.GetUnicode(); int len=aString.Length(); for(int i=0;i= PRInt32(sizeof(buf))) { + PRInt32 aLength=aString.Length(); + if (aLength >= PRInt32(sizeof(buf))) { cp = aString.ToNewCString(); } else { - aString.ToCString(cp, len + 1); + aString.ToCString(cp, aLength + 1); } - if(len>0) - ::fwrite(cp, 1, len, out); + if(aLength >0) + ::fwrite(cp, 1, aLength, out); if (cp != buf) { delete [] cp; } - return (int) len; + return (int) aLength; } - + + /** * @@ -2203,38 +2242,42 @@ NS_BASE int fputs(const nsString& aString, FILE* out) * @param * @return */ -void nsString::SelfTest(void) { +void nsString1::SelfTest(void) { #ifdef RICKG_DEBUG mSelfTested=PR_TRUE; - nsAutoString a("foobar"); - nsAutoString b("foo"); - nsAutoString c(".5111"); - nsAutoString d(" 5"); - PRInt32 result=a.Compare(b); - PRInt32 result2=result; - result=c.ToInteger(&result2); - result=d.ToInteger(&result2); - result2=result; + + { + 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!"; + - nsAutoString as("Hello there"); + nsAutoString1 as("Hello there"); as.SelfTest(); static const char* temp="hello"; //first, let's test the constructors... - nsString empty; + nsString1 empty; empty=""; empty="xxx"; empty=""; - nsString a(temp); - nsString* a_=new nsString(a); //test copy constructor - nsString b("world!"); + nsString1 a(temp); + nsString1* a_=new nsString1(a); //test copy constructor + nsString1 b("world!"); //verify destructor... delete a_; @@ -2248,11 +2291,9 @@ void nsString::SelfTest(void) { //********************************************** const chartype* p1=a.GetUnicode(); - const chartype* p2=a; //should invoke the PRUnichar conversion operator... for(int i=0;itemp3.Length(),kConstructorError); //should be char longer - nsString* es1=temp2.ToNewString(); //this should make us a new string + nsString1* es1=temp2.ToNewString(); //this should make us a new string char* es2=temp2.ToNewCString(); for(i=0;i #include -class nsISizeOfHandler; #include "nsStr.h" -class NS_BASE nsString { +#ifndef USE_STRING2 +#define nsString1 nsString +#define nsAutoString1 nsAutoString +#else +#include "nsString2.h" //get new string class +#endif + +class nsISizeOfHandler; + +class NS_BASE nsString1 { public: /** @@ -51,30 +59,30 @@ class NS_BASE nsString { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString(); +nsString1(); /** * This constructor accepts an isolatin string * @param an ascii is a ptr to a 1-byte cstr */ -nsString(const char* aCString); +nsString1(const char* aCString); /** * This is our copy constructor - * @param reference to another nsString + * @param reference to another nsString1 */ -nsString(const nsString&); +nsString1(const nsString1&); /** * Constructor from a unicode string * @param anicodestr pts to a unicode string */ -nsString(const PRUnichar* aUnicode); +nsString1(const PRUnichar* aUnicode); /** * Virtual Destructor */ -virtual ~nsString(); +virtual ~nsString1(); /** @@ -144,7 +152,7 @@ operator const PRUnichar*() const; * @param offset into string * @return PRUnichar* to internal string */ -PRUnichar operator()(PRInt32 anIndex) const; +//PRUnichar operator()(PRInt32 anIndex) const; /** * Retrieve reference to unicode char at given index @@ -184,35 +192,35 @@ PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString operator+(const nsString& aString); +nsString1 operator+(const nsString1& 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 */ -nsString operator+(const char* aCString); +nsString1 operator+(const char* aCString); /** * 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 */ -nsString operator+(char aChar); +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 */ -nsString operator+(const PRUnichar* aBuffer); +nsString1 operator+(const PRUnichar* aBuffer); /** * 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 */ -nsString operator+(PRUnichar aChar); +nsString1 operator+(PRUnichar aChar); /** * Converts all chars in internal string to lower @@ -222,7 +230,7 @@ void ToLowerCase(); /** * Converts all chars in given string to lower */ -void ToLowerCase(nsString& aString) const; +void ToLowerCase(nsString1& aString) const; /** * Converts all chars in given string to upper @@ -238,13 +246,13 @@ void ToUCS2(PRInt32 aStartOffset); /** * Converts all chars in internal string to upper */ -void ToUpperCase(nsString& aString) const; +void ToUpperCase(nsString1& aString) const; /** * Creates a duplicate clone (ptr) of this string. * @return ptr to clone of this string */ -nsString* ToNewString() const; +nsString1* ToNewString() const; /** * Creates an ascii clone of this string @@ -266,7 +274,7 @@ char* ToCString(char* aBuf,PRInt32 aBufLength) const; * @param aString to hold copy of this * @return nada. */ -void Copy(nsString& aString) const; +void Copy(nsString1& aString) const; /** * Creates an unichar clone of this string @@ -299,79 +307,72 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; if you want me to determine its length * @return this */ -nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); - -/** - * assign given char* to this string - * @param aCString: 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 - */ -nsString& SetString(const char* aCString,PRInt32 aLength=-1); +nsString1& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); +nsString1& SetString(const char* aCString,PRInt32 aLength=-1); +nsString1& SetString(const nsString1& aString); /** * assign given string to this one * @param aString: string to be added to this * @return this */ -nsString& operator=(const nsString& aString); +nsString1& operator=(const nsString1& aString); /** * assign given char* to this string * @param aCString: buffer to be assigned to this * @return this */ -nsString& operator=(const char* aCString); +nsString1& operator=(const char* aCString); /** * assign given char to this string * @param aChar: char to be assignd to this * @return this */ -nsString& operator=(char aChar); +nsString1& operator=(char aChar); /** * assign given unichar* to this string * @param aBuffer: unichar buffer to be assigned to this * @return this */ -nsString& operator=(const PRUnichar* aBuffer); +nsString1& operator=(const PRUnichar* aBuffer); /** * assign given char to this string * @param aChar: char to be assignd to this * @return this */ -nsString& operator=(PRUnichar aChar); +nsString1& operator=(PRUnichar aChar); /** * append given string to this string * @param aString : string to be appended to this * @return this */ -nsString& operator+=(const nsString& aString); +nsString1& operator+=(const nsString1& aString); /** * append given buffer to this string * @param aCString: buffer to be appended to this * @return this */ -nsString& operator+=(const char* aCString); +nsString1& operator+=(const char* aCString); /** * append given buffer to this string * @param aBuffer: buffer to be appended to this * @return this */ -nsString& operator+=(const PRUnichar* aBuffer); +nsString1& operator+=(const PRUnichar* aBuffer); /** * append given char to this string * @param aChar: char to be appended to this * @return this */ -nsString& operator+=(PRUnichar aChar); +nsString1& operator+=(PRUnichar aChar); /** * append given string to this string @@ -380,7 +381,7 @@ nsString& operator+=(PRUnichar aChar); if you want me to determine its length * @return this */ -nsString& Append(const nsString& aString,PRInt32 aLength=-1); +nsString1& Append(const nsString1& aString,PRInt32 aLength=-1); /** * append given string to this string @@ -389,14 +390,14 @@ nsString& Append(const nsString& aString,PRInt32 aLength=-1); if you want me to determine its length * @return this */ -nsString& Append(const char* aCString,PRInt32 aLength=-1); +nsString1& Append(const char* aCString,PRInt32 aLength=-1); /** * append given string to this string * @param aString : string to be appended to this * @return this */ -nsString& Append(char aChar); +nsString1& Append(char aChar); /** * append given unichar buffer to this string @@ -405,14 +406,14 @@ nsString& Append(char aChar); if you want me to determine its length * @return this */ -nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1); +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 */ -nsString& Append(PRUnichar aChar); +nsString1& Append(PRUnichar aChar); /** * Append an integer onto this string @@ -420,14 +421,14 @@ nsString& Append(PRUnichar aChar); * @param aRadix specifies 8,10,16 * @return this */ -nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16 +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 */ -nsString& Append(float aFloat); +nsString1& Append(float aFloat); /* * Copies n characters from this string to given string, @@ -438,7 +439,7 @@ nsString& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Left(nsString& aCopy,PRInt32 aCount) const; +PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -450,7 +451,7 @@ PRInt32 Left(nsString& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const; +PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -461,7 +462,7 @@ PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Right(nsString& aCopy,PRInt32 aCount) const; +PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -472,7 +473,7 @@ PRInt32 Right(nsString& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -PRInt32 Insert(const nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); +PRInt32 Insert(const nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); /** * Insert a single unicode char into this string at @@ -492,7 +493,7 @@ PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString& Cut(PRInt32 anOffset,PRInt32 aCount); +nsString1& Cut(PRInt32 anOffset,PRInt32 aCount); /** * This method is used to remove all occurances of the @@ -501,7 +502,7 @@ nsString& Cut(PRInt32 anOffset,PRInt32 aCount); * @param aSet -- characters to be cut from this * @return *this */ -nsString& StripChars(const char* aSet); +nsString1& StripChars(const char* aSet); /** * This method is used to replace all occurances of the @@ -510,14 +511,14 @@ nsString& StripChars(const char* aSet); * @param * @return *this */ -nsString& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); +nsString1& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); /** * This method strips whitespace throughout the string * * @return this */ -nsString& StripWhitespace(); +nsString1& StripWhitespace(); /** * This method trims characters found in aTrimSet from @@ -527,7 +528,7 @@ nsString& StripWhitespace(); * both ends * @return this */ -nsString& Trim(const char* aSet, +nsString1& Trim(const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE); @@ -540,7 +541,7 @@ nsString& Trim(const char* aSet, * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, +nsString1& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE); /** @@ -589,7 +590,7 @@ PRInt32 BinarySearch(PRUnichar aChar) const; */ PRInt32 Find(const char* aString) const; PRInt32 Find(const PRUnichar* aString) const; -PRInt32 Find(const nsString& aString) const; +PRInt32 Find(const nsString1& aString) const; /** * Search for given char within this string @@ -607,7 +608,7 @@ PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const; * @return -1 if not found, else the offset in this */ PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const; +PRInt32 FindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; /** * This method searches this string for the last character @@ -617,7 +618,7 @@ PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const; * @return -1 if not found, else the offset in this */ PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const; +PRInt32 RFindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; /** @@ -628,7 +629,7 @@ PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const; */ PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString& 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 @@ -649,7 +650,7 @@ PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const; * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -virtual PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE) const; +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; @@ -658,7 +659,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 nsString &aString) const; +PRBool operator==(const nsString1 &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; PRBool operator==(PRUnichar* aString) const; @@ -668,7 +669,7 @@ PRBool operator==(PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE */ -PRBool operator!=(const nsString &aString) const; +PRBool operator!=(const nsString1 &aString) const; PRBool operator!=(const char *aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -677,7 +678,7 @@ PRBool operator!=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<(const nsString &aString) const; +PRBool operator<(const nsString1 &aString) const; PRBool operator<(const char *aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -686,7 +687,7 @@ PRBool operator<(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>(const nsString &S) const; +PRBool operator>(const nsString1 &S) const; PRBool operator>(const char *aCString) const; PRBool operator>(const PRUnichar* aString) const; @@ -695,7 +696,7 @@ PRBool operator>(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<=(const nsString &S) const; +PRBool operator<=(const nsString1 &S) const; PRBool operator<=(const char *aCString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -704,7 +705,7 @@ PRBool operator<=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>=(const nsString &S) const; +PRBool operator>=(const nsString1 &S) const; PRBool operator>=(const char* aCString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -718,7 +719,7 @@ PRBool operator>=(const PRUnichar* aString) const; * @param aLength -- optional length of given string. * @return TRUE if equal */ -PRBool Equals(const nsString& aString) const; +PRBool Equals(const nsString1& aString) const; PRBool Equals(const char* aString,PRInt32 aLength=-1) const; PRBool Equals(const nsIAtom *aAtom) const; @@ -741,7 +742,7 @@ PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const; * @param aLength -- optional length of given string. * @return TRUE if equal */ -PRBool EqualsIgnoreCase(const nsString& aString) const; +PRBool EqualsIgnoreCase(const nsString1& aString) const; PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; @@ -769,34 +770,34 @@ typedef PRUnichar chartype; #endif }; -ostream& operator<<(ostream& os,nsString& aString); -extern NS_BASE int fputs(const nsString& aString, FILE* out); +ostream& operator<<(ostream& os,nsString1& aString); +extern NS_BASE int fputs(const nsString1& aString, FILE* out); //---------------------------------------------------------------------- /** - * A version of nsString which is designed to be used as an automatic + * 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_BASE nsAutoString : public nsString { +class NS_BASE nsAutoString1 : public nsString1 { public: - nsAutoString(); - nsAutoString(const nsString& other); - nsAutoString(const nsAutoString& other); - nsAutoString(PRUnichar aChar); - nsAutoString(const char* aCString); - nsAutoString(const PRUnichar* us, PRInt32 uslen = -1); - virtual ~nsAutoString(); + 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(); - nsAutoString& operator=(const nsString& aString) {nsString::operator=(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;} + 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; @@ -808,7 +809,7 @@ protected: chartype mBuf[32]; }; -ostream& operator<<(ostream& os,nsAutoString& aString); +ostream& operator<<(ostream& os,nsAutoString1& aString); #endif diff --git a/mozilla/base/src/nsString2.cpp b/mozilla/base/src/nsString2.cpp index 95afc27ff6b..d1224ddc94f 100644 --- a/mozilla/base/src/nsString2.cpp +++ b/mozilla/base/src/nsString2.cpp @@ -19,30 +19,25 @@ #include #include -#include +#include #include #include "nsString2.h" #include "nsDebug.h" #include "nsCRT.h" -#include "nsIAtom.h" -#include "nsISizeOfHandler.h" -#include "prprf.h" -#include "prdtoa.h" +#include "nsIAtom.h" #include "nsDeque.h" -#include "nsUnicharUtilCIID.h" -#include "nsIServiceManager.h" -#include "nsICaseConversion.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 "; -#ifdef RICKG_DEBUG -PRBool nsString2::mSelfTested = PR_FALSE; -#endif -#define NOT_USED 0xfffd +#define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { NOT_USED, @@ -96,61 +91,7 @@ public: } } }; -static CTableConstructor gTableConstructor; -//---- XPCOM code to connect with UnicharUtil - -class HandleCaseConversionShutdown3 : public nsIShutdownListener { -public : - NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); - HandleCaseConversionShutdown3(void) { NS_INIT_REFCNT(); } - virtual ~HandleCaseConversionShutdown3(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(HandleCaseConversionShutdown3, kIShutdownListenerIID); - -nsresult -HandleCaseConversionShutdown3::OnShutdown(const nsCID& cid, nsISupports* service) -{ - if (cid.Equals(kUnicharUtilCID)) { - NS_ASSERTION(service == gCaseConv, "wrong service!"); - gCaseConv->Release(); - gCaseConv = NULL; - } - return NS_OK; -} - -#if 0 -/* not used yet */ -static HandleCaseConversionShutdown3* gListener = NULL; - -static void StartUpCaseConversion() -{ - nsresult err; - - if ( NULL == gListener ) - { - gListener = new HandleCaseConversionShutdown3(); - gListener->AddRef(); - } - err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, - (nsISupports**) &gCaseConv, gListener); -} -static void CheckCaseConversion() -{ - if(NULL == gCaseConv ) - StartUpCaseConversion(); - - // NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); - -} -#endif /*********************************************************************** IMPLEMENTATION NOTES: @@ -159,6 +100,15 @@ static void CheckCaseConversion() that no poor soul would ever have to do this again. Sigh. ***********************************************************************/ +void Subsume(nsStr& aDest,nsStr& aSource){ + 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; +} /** * Default constructor. Note that we actually allocate a small buffer @@ -167,14 +117,10 @@ static void CheckCaseConversion() * performance reasons. */ nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { - nsStr::Initialize(*this,aCharSize); - -#ifdef RICKG_DEBUG - if(!mSelfTested) { - mSelfTested=PR_TRUE; - SelfTest(); - } +#ifdef RICKG_DEBUG + static CStringTester gStringTester; #endif + nsStr::Initialize(*this,aCharSize); } /** @@ -213,10 +159,19 @@ nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* an * @param reference to another nsString2 */ nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { - nsStr::Initialize(*this,(eCharSize)aString.mMultibyte); + nsStr::Initialize(*this,(eCharSize)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 + */ +nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { + Subsume(*this,aSubsumeStr); +} + /** * Destructor * Make sure we call nsStr::Destroy. @@ -227,7 +182,7 @@ nsString2::~nsString2() { void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(sizeof(*this)); - aHandler->Add(mCapacity << mMultibyte); + aHandler->Add(mCapacity << mCharSize); } /** @@ -291,11 +246,10 @@ void nsString2::SetCapacity(PRUint32 aLength) { /** * * @update gess1/4/99 - * @param - * @return + * @return ptr to internal buffer (if 1-byte), otherwise NULL */ -char* nsString2::GetBuffer(void) const { - if(!mMultibyte) +const char* nsString2::GetBuffer(void) const { + if(!mCharSize) return mStr; return 0; } @@ -303,11 +257,10 @@ char* nsString2::GetBuffer(void) const { /** * * @update gess1/4/99 - * @param - * @return + * @return ptr to internal buffer (if 2-byte), otherwise NULL */ -PRUnichar* nsString2::GetUnicode(void) const { - if(mMultibyte) +const PRUnichar* nsString2::GetUnicode(void) const { + if(mCharSize) return (PRUnichar*)mUStr; return 0; } @@ -315,11 +268,11 @@ PRUnichar* nsString2::GetUnicode(void) const { /** * Get nth character. */ -PRUnichar nsString2::operator[](int anIndex) const { +PRUnichar nsString2::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::CharAt(int anIndex) const { +PRUnichar nsString2::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } @@ -334,7 +287,7 @@ PRUnichar nsString2::Last(void) const{ PRBool nsString2::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex(const nsStr &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 nsString2::operator<=(const nsStr &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 nsString2::operator>=(const nsStr &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);} @@ -1416,6 +1421,18 @@ PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) con return Equals(s1,s2,PR_TRUE); } +/** + * 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 + * @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); +} + /** * Compare this to given string; note that we compare full strings here. * @@ -1569,21 +1586,42 @@ PRBool nsString2::IsDigit(PRUnichar aChar) { } +/************************************************************** + Define the string deallocator class... + **************************************************************/ +class nsStringDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + static nsMemoryAgent theAgent; + nsString2* aString= (nsString2*)anObject; + if(aString){ + aString->mAgent=&theAgent; + delete aString; + } + return 0; + } +}; + /**************************************************************************** * This class, appropriately enough, creates and recycles nsString2 objects.. ****************************************************************************/ -#if 0 + class nsStringRecycler { public: nsStringRecycler() : mDeque(0) { } + ~nsStringRecycler() { + nsStringDeallocator theDeallocator; + mDeque.ForEach(theDeallocator); //now delete the strings + } + void Recycle(nsString2* aString) { mDeque.Push(aString); } - nsString2* NewString(eCharSize aCharSize){ + nsString2* CreateString(eCharSize aCharSize){ nsString2* result=(nsString2*)mDeque.Pop(); if(!result) result=new nsString2(aCharSize); @@ -1613,8 +1651,8 @@ nsStringRecycler& GetRecycler(void){ * @param * @return */ -nsString2* nsString2::NewString(eCharSize aCharSize){ - nsString2* result=GetRecycler().NewString(aCharSize); +nsString2* nsString2::CreateString(eCharSize aCharSize){ + nsString2* result=GetRecycler().CreateString(aCharSize); return result; } @@ -1628,7 +1666,6 @@ void nsString2::Recycle(nsString2* aString){ GetRecycler().Recycle(aString); } -#endif /** * @@ -1650,7 +1687,7 @@ void nsString2::DebugDump(ostream& aStream) const { * @return */ ostream& operator<<(ostream& os,nsString2& aString){ - if(PR_FALSE==aString.mMultibyte) { + if(PR_FALSE==aString.mCharSize) { os<>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((eCharSize)aStr.mCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,(eCharSize)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((eCharSize)aCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,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(char* aCString,PRUint32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { + mAgent=0; + if(assumeOwnership) { + nsStr::Initialize(*this,aCString,aCapacity,0,eOneByte,PR_TRUE); + } + 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) { + mAgent=0; + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aString); +} + +/** + * Copy construct from uni-string + * @param aString is a ptr to a unistr + */ +nsAutoString2::nsAutoString2(PRUnichar* aString,PRUint32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { + mAgent=0; + if(assumeOwnership) { + nsStr::Initialize(*this,(char*)aString,aCapacity,0,eTwoByte,PR_TRUE); + } + else { + 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 nsStr& aString,eCharSize aCharSize) : nsString2(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 nsAutoString2& aString,eCharSize aCharSize) : nsString2(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); +} + + +/** + * Copy construct from an nsString2 + * @param + */ +nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ + mAgent=0; + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aChar); +} + +/** + * construct from a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString + */ +nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2((eCharSize)aSubsumeStr.mCharSize) { + mAgent=0; + Subsume(*this,aSubsumeStr); +} + +/** + * deconstruct the autstring + * @param + */ +nsAutoString2::~nsAutoString2(){ +// bool b=true; +// mStr=0; +} + +void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity << mCharSize); +} + +nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2((eCharSize)aString.mCharSize) { + Subsume(*this,aString); +} + +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharSize) { + Subsume(*this,aString); +} + +nsSubsumeStr::nsSubsumeStr(const PRUnichar* aString) : nsString2(aString,eTwoByte) { +} + +nsSubsumeStr::nsSubsumeStr(const char* aString) : nsString2(aString,eOneByte) { +} + +#ifdef RICKG_DEBUG +/*********************************************************************** + IMPLEMENTATION of CStringTester... + ***********************************************************************/ +CStringTester::CStringTester() { -#ifdef RICKG_DEBUG static const char* kConstructorError = kConstructorError; static const char* kComparisonError = "Comparision error!"; static const char* kEqualsError = "Equals error!"; - mSelfTested=PR_TRUE; - eCharSize theSize=eOneByte; + eCharSize theSize=eTwoByte; //begin by testing the constructors... { { nsString2 theString0("foo",theSize); //watch it construct and destruct - } + + { + nsString2 theString("hello"); + nsString2 temp1=theString+" there!"; + nsString2 temp2=theString+'!'; + nsSubsumeStr temp3=theString+'?'; + nsString2 temp4(temp3); + temp1=temp3; + nsSubsumeStr temp5("hello"); + } + nsString2 theString1(theSize); nsString2 theString("hello",theSize); nsString2 theString3(theString,theSize); @@ -1725,6 +1934,8 @@ void nsString2::SelfTest(void) { //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; @@ -1740,12 +1951,17 @@ void nsString2::SelfTest(void) { 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... + + //try a few numeric conversion routines... { nsString2 str1("10000",theSize); PRInt32 err; @@ -1757,11 +1973,12 @@ void nsString2::SelfTest(void) { { nsString2 theString("hello",theSize); PRUint32 len=theString.Length(); + PRUnichar ch; for(PRUint32 i=0;i>aDest.mMultibyte)-1; - aDest.mOwnsBuffer=aBufDescriptor.mOwnsBuffer; - AddNullTerminator(aDest); -} - -/** - * 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 - */ -nsAutoString2::nsAutoString2(eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); -} - -/** - * 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(nsBufDescriptor& aBufDescriptor,const char* aCString) : nsString2(aBufDescriptor.mMultibyte) { - InitAutoStr(*this,aBufDescriptor); - 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) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aCString); -} - -/** - * Copy construct from ascii c-string - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(char* aCString,PRUint32 aLen,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - if(assumeOwnership) { - nsBufDescriptor theDescriptor(aCString,aLen,eOneByte,PR_TRUE); - InitAutoStr(*this,theDescriptor); + { + //now let's try a memory allocation test... + nsString2 temp; + for(int i=0;i<100;i++){ + temp+="hello "; + } + int x=5; } - else { - nsBufDescriptor theDescriptor(mBuffer,aLen,aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - } - Assign(aCString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - */ -nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - */ -nsAutoString2::nsAutoString2(PRUnichar* aString,PRUint32 aLength,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - if(assumeOwnership) { - nsBufDescriptor theDescriptor((char*)aString,aLength,eTwoByte,PR_TRUE); - InitAutoStr(*this,theDescriptor); - } - else { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - } - Assign(aString); -} - - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsStr& aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsString2& aString,eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aChar); -} - -/** - * deconstruct the autstring - * @param - */ -nsAutoString2::~nsAutoString2(){ -// bool b=true; -// mStr=0; -} - -void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { - aHandler->Add(sizeof(*this)); - aHandler->Add(mCapacity << mMultibyte); -} +}; +#endif //rickg_debug diff --git a/mozilla/base/src/nsString2.h b/mozilla/base/src/nsString2.h index d5e07d9f994..ec7b2be85fe 100644 --- a/mozilla/base/src/nsString2.h +++ b/mozilla/base/src/nsString2.h @@ -20,18 +20,19 @@ /*********************************************************************** MODULE NOTES: - This class provides a 1-byte ASCII string implementation that shares - a common API with all other strImpl derivatives. + 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 _nsString2 #define _nsString2 -//#define nsString2 nsString -//#define nsAutoString2 nsAutoString - - #include "prtypes.h" #include "nscore.h" #include @@ -46,6 +47,13 @@ class nsISizeOfHandler; +#ifdef USE_STRING2 +#define nsString2 nsString +#define nsAutoString2 nsAutoString +#endif + +class NS_BASE nsSubsumeStr; + class NS_BASE nsString2 : public nsStr { public: @@ -83,6 +91,12 @@ nsString2(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAg */ nsString2(const nsString2& aString); +/** + * This constructor takes a subsumestr + * @param reference to subsumestr + */ +nsString2(nsSubsumeStr& aSubsumeStr); + /** * Destructor * @@ -139,16 +153,15 @@ PRBool IsOrdered(void) const; Accessor methods... *********************************************************************/ -char* GetBuffer(void) const; -PRUnichar* GetUnicode(void) const; -operator PRUnichar*() const {return GetUnicode();} +const char* GetBuffer(void) const; +const PRUnichar* GetUnicode(void) const; /** * Get nth character. */ -PRUnichar operator[](int anIndex) const; -PRUnichar CharAt(int anIndex) const; +PRUnichar operator[](PRUint32 anIndex) const; +PRUnichar CharAt(PRUint32 anIndex) const; PRUnichar First(void) const; PRUnichar Last(void) const; @@ -164,42 +177,42 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString2 operator+(const nsStr& aString); +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 */ -nsString2 operator+(const nsString2& aString); +nsSubsumeStr operator+(const nsString2& 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 */ -nsString2 operator+(const char* aCString); +nsSubsumeStr 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 */ -nsString2 operator+(const PRUnichar* aString); +nsSubsumeStr 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 */ -nsString2 operator+(char aChar); +nsSubsumeStr 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 */ -nsString2 operator+(PRUnichar aChar); +nsSubsumeStr operator+(PRUnichar aChar); /********************************************************************** Lexomorphic transforms... @@ -353,7 +366,6 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=10) const; 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);} -void Copy(nsString2& aString) const; /** * assign given string to this string @@ -380,6 +392,7 @@ 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);} +nsString2& operator=(nsSubsumeStr& aSubsumeString); /** * Here's a bunch of append mehtods for varying types... @@ -402,8 +415,7 @@ nsString2& operator+=(PRUnichar aChar){return Append(aChar);} */ nsString2& Append(const nsStr& aString) {return Append(aString,aString.mLength);} nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLength);} -nsString2& Append(const char* aString) {if(aString) {Append(aString,nsCRT::strlen(aString));} return *this;} -nsString2& Append(const PRUnichar* aString) {if(aString) {Append(aString,nsCRT::strlen(aString));} return *this;} + /* * Appends n characters from given string to this, @@ -414,13 +426,13 @@ nsString2& Append(const PRUnichar* aString) {if(aString) {Append(aString,nsCRT:: */ nsString2& Append(const nsStr& aString,PRInt32 aCount); nsString2& Append(const nsString2& aString,PRInt32 aCount); -nsString2& Append(const char* aString,PRInt32 aCount); -nsString2& Append(const PRUnichar* 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); - + /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -464,7 +476,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(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -519,10 +531,11 @@ 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(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) const; +PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; /** * This method searches this string for the first character @@ -554,9 +567,10 @@ PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; * @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(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) const; +PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; /********************************************************************** Comparison methods... @@ -569,6 +583,7 @@ PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) 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 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; @@ -578,6 +593,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 nsStr &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; @@ -587,6 +603,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 nsStr &aString) const; PRBool operator!=(const char* aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -596,6 +613,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 nsStr &aString) const; PRBool operator<(const char* aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -605,6 +623,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 nsStr &S) const; PRBool operator>(const char* aString) const; PRBool operator>(const PRUnichar* aString) const; @@ -614,6 +633,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 nsStr &S) const; PRBool operator<=(const char* aString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -623,6 +643,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 nsStr &S) const; PRBool operator>=(const char* aString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -637,6 +658,7 @@ PRBool operator>=(const PRUnichar* aString) const; * @param aLength -- optional length of given string. * @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; @@ -675,18 +697,11 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -#if 0 static void Recycle(nsString2* aString); -static nsString2* NewString(eCharSize aCharSize=eTwoByte); -#endif +static nsString2* CreateString(eCharSize aCharSize=eTwoByte); -static void SelfTest(); virtual void DebugDump(ostream& aStream) const; -#ifdef RICKG_DEBUG - static PRBool mSelfTested; -#endif - nsIMemoryAgent* mAgent; }; @@ -701,12 +716,11 @@ ostream& operator<<(ostream& os,nsString2& aString); If the buffer needs to grow, it gets reallocated on the heap. **************************************************************/ - class NS_BASE nsAutoString2 : public nsString2 { public: nsAutoString2(eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(nsBufDescriptor& anExtBuffer,const char* aCString); + nsAutoString2(nsStr& anExtBuffer,const char* aCString); nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); @@ -717,6 +731,7 @@ public: nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString2(nsSubsumeStr& aSubsumeStr); nsAutoString2(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); virtual ~nsAutoString2(); @@ -738,5 +753,37 @@ public: }; +/*************************************************************** + 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_BASE nsSubsumeStr : public nsString2 { +public: + nsSubsumeStr(nsString2& aString); + nsSubsumeStr(nsStr& aString); + nsSubsumeStr(const PRUnichar* aString); + nsSubsumeStr(const char* aString); +}; + +#ifdef RICKG_DEBUG +/******************************************************** + This class's only purpose in life is to test nsString2. + ********************************************************/ +class CStringTester { +public: + CStringTester(); +}; #endif +#endif + + diff --git a/mozilla/string/obsolete/nsStr.cpp b/mozilla/string/obsolete/nsStr.cpp index 79e7afc9910..4d3fc6ab211 100644 --- a/mozilla/string/obsolete/nsStr.cpp +++ b/mozilla/string/obsolete/nsStr.cpp @@ -31,12 +31,104 @@ #include "nsStr.h" #include "bufferRoutines.h" #include "stdio.h" //only used for printf +#include "nsDeque.h" +#include "nsCRT.h" + static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; - //---------------------------------------------------------------------------------------- +// The following is a memory agent who knows how to recycled (pool) freed memory... +//---------------------------------------------------------------------------------------- + +/************************************************************** + Define the char* (pooled) deallocator class... + **************************************************************/ +class nsBufferDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + char* aCString= (char*)anObject; + delete [] aCString; + return 0; + } +}; +/** + * + * @update gess10/30/98 + * @param + * @return + */ +class nsPoolingMemoryAgent : public nsMemoryAgent{ +public: + nsPoolingMemoryAgent() { + memset(mPools,0,sizeof(mPools)); + } + + ~nsPoolingMemoryAgent() { + nsBufferDeallocator theDeallocator; + int i=0; + for(i=0;i<10;i++){ + if(mPools[i]){ + mPools[i]->ForEach(theDeallocator); //now delete the buffers + } + delete mPools[i]; + mPools[i]=0; + } + } + + virtual PRBool Alloc(nsStr& aDest,PRInt32 aCount) { + + //we're given the acount value in charunits; we have to scale up by the charsize. + int theShift=4; + PRInt32 theNewCapacity=eDefaultSize; + while(theNewCapacityPop(); + } + if(!aDest.mStr) { + //we're given the acount value in charunits; we have to scale up by the charsize. + size_t theSize=(theNewCapacity<Push(aDest.mStr); + } + else delete [] aDest.mStr; //it's too big. Just delete it. + } + aDest.mStr=0; + aDest.mOwnsBuffer=0; + return PR_TRUE; + } + return PR_FALSE; + } + nsDeque* mPools[16]; +}; + +static char* gCommonEmptyBuffer=0; /** * * @update gess10/30/98 @@ -44,9 +136,8 @@ static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; * @return */ char* GetSharedEmptyBuffer() { - static char* gCommonEmptyBuffer=0; if(!gCommonEmptyBuffer) { - const size_t theDfltSize=25; + const size_t theDfltSize=5; gCommonEmptyBuffer=new char[theDfltSize]; if(gCommonEmptyBuffer){ nsCRT::zero(gCommonEmptyBuffer,theDfltSize); @@ -69,9 +160,24 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { aDest.mStr=GetSharedEmptyBuffer(); aDest.mLength=0; aDest.mCapacity=0; - aDest.mMultibyte=aCharSize; + aDest.mCharSize=aCharSize; aDest.mOwnsBuffer=0; - NS_ASSERTION(aDest.mStr[0]==0,kFoolMsg); + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); +} + +/** + * + * @update gess10/30/98 + * @param + * @return + */ +void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer){ + aDest.mStr=(aCString) ? aCString : GetSharedEmptyBuffer(); + aDest.mLength=aLength; + aDest.mCapacity=aCapacity; + aDest.mCharSize=aCharSize; + aDest.mOwnsBuffer=aOwnsBuffer; + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -81,13 +187,9 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { * @return */ nsIMemoryAgent* GetDefaultAgent(void){ - static nsIMemoryAgent* gDefaultAgent=0; - if(!gDefaultAgent) - gDefaultAgent=new nsMemoryAgent(); - - NS_ASSERTION(gDefaultAgent,"You MUST always have an allocator!"); - - return gDefaultAgent; +// static nsPoolingMemoryAgent gDefaultAgent; + static nsMemoryAgent gDefaultAgent; + return (nsIMemoryAgent*)&gDefaultAgent; } /** @@ -110,20 +212,6 @@ void nsStr::Destroy(nsStr& aDest,nsIMemoryAgent* anAgent) { } } -/** - * - * @update gess11/12/98 - * @param - * @return - */ -PRUnichar nsStr::GetCharAt(const nsStr& aDest,PRUint32 anIndex) { - PRUnichar result=0; - if((anIndex>=0) && (anIndexRealloc(aString,aNewLength); AddNullTerminator(aString); } + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -150,13 +239,13 @@ void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* an void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { if(aNewLength>aDest.mCapacity) { nsStr theTempStr; - nsStr::Initialize(theTempStr,(eCharSize)aDest.mMultibyte); + nsStr::Initialize(theTempStr,(eCharSize)aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); EnsureCapacity(theTempStr,aNewLength,theAgent); - if(0Free(aDest); aDest.mStr = theTempStr.mStr; @@ -165,6 +254,7 @@ void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgen aDest.mCapacity=theTempStr.mCapacity; aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; } + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -182,13 +272,12 @@ void nsStr::Assign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 a /** * This method appends the given nsStr to this one. Note that we have to * pay attention to the underlying char-size of both structs. - * @update gess 04/04/99 + * @update gess10/30/98 * @param aDest is the nsStr to be manipulated * @param aSource is where char are copied from * @aCount is the number of bytes to be copied */ void nsStr::Append(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount,nsIMemoryAgent* anAgent){ - if(anOffset0) && (aTarget.mLength>0)){ PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=nsStr::GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=nsStr::GetCharAt(aTarget,aTarget.mLength-1); + PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); + PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); PRInt32 theTargetMax=aTarget.mLength; while(++index<=theMax) { PRInt32 theSubIndex=-1; PRBool matches=PR_TRUE; while((++theSubIndex0) { if(theFirstTargetChar==theChar){ - PRUnichar theDestJumpChar=nsStr::GetCharAt(aDest,index+theTargetMax); + PRUnichar theDestJumpChar=GetCharAt(aDest,index+theTargetMax); if(theDestJumpChar==theLastTargetChar) { theNewStartPos=index; //this lets us jump ahead during our search where possible. }//if }//if }//if - PRUnichar theTargetChar=nsStr::GetCharAt(aTarget,theSubIndex); + PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex); matches=PRBool(theChar==theTargetChar); } if(matches) @@ -384,8 +478,8 @@ PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIgn * @param * @return */ -PRInt32 nsStr::FindChar(const nsStr& aDest, PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 result=gFindChars[aDest.mMultibyte](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); +PRInt32 nsStr::FindChar(const nsStr& aDest,const PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { + PRInt32 result=gFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); return result; } @@ -403,7 +497,7 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore while(++index0) && (aTarget.mLength>0)){ PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=nsStr::GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=nsStr::GetCharAt(aTarget,aTarget.mLength-1); + PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); + PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); PRInt32 theTargetMax=aTarget.mLength; while(index--) { @@ -429,16 +523,16 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIg if(anOffset+aTarget.mLength<=aDest.mLength) { while((++theSubIndex0) { if(theFirstTargetChar==theChar){ - PRUnichar theDestJumpChar=nsStr::GetCharAt(aDest,index+theTargetMax); + PRUnichar theDestJumpChar=GetCharAt(aDest,index+theTargetMax); if(theDestJumpChar==theLastTargetChar) { theNewStartPos=index; //this lets us jump ahead during our search where possible. }//if }//if }//if - PRUnichar theTargetChar=nsStr::GetCharAt(aTarget,theSubIndex); + PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex); matches=PRBool(theChar==theTargetChar); } //while } //if @@ -459,8 +553,8 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIg * @param * @return */ -PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 result=gRFindChars[aDest.mMultibyte](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); +PRInt32 nsStr::RFindChar(const nsStr& aDest,const PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { + PRInt32 result=gRFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); return result; } @@ -472,12 +566,12 @@ PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase, * @return */ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 offset=aDest.mLength-anOffset; + PRUint32 offset=aDest.mLength-anOffset; PRInt32 thePos; while(--offset>=0) { - PRUnichar theChar=GetCharAt(aDest,PRUint32(offset)); - thePos=gRFindChars[aSet.mMultibyte](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); + PRUnichar theChar=GetCharAt(aDest,offset); + thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,anOffset,theChar,aIgnoreCase); if(kNotFound!=thePos) return offset; } //while @@ -491,7 +585,7 @@ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnor * @param * @return aDestaSource=1 */ -PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 /*aCount*/,PRBool aIgnoreCase) { +PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { int minlen=(aSource.mLength>aCharSize)-1), - mOwnsBuffer(aOwnsBuffer), - mMultibyte(aCharSize) -{ - mStr=aBuffer; - } - - PRUint32 mCapacity; - PRBool mOwnsBuffer; - eCharSize mMultibyte; -// UStrPtr mStr; - union { - char* mStr; - PRUnichar* mUStr; - }; -}; +const PRInt32 kNotFound = -1; class nsIMemoryAgent; @@ -99,6 +67,15 @@ struct nsStr { */ static void Initialize(nsStr& aDest,eCharSize aCharSize); + /** + * This method initializes an nsStr for use + * + * @update gess 01/04/99 + * @param aString is the nsStr to be initialized + * @param aCharSize tells us the requested char size (1 or 2 bytes) + */ + static void Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer); + /** * This method destroys the given nsStr, and *MAY* * deallocate it's memory depending on the setting @@ -168,7 +145,7 @@ struct nsStr { * @param aCount tells us the (max) # of chars to delete * @param anAgent is the allocator to be used for alloc/free operations */ - static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,nsIMemoryAgent* anAgent=0); + static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount,nsIMemoryAgent* anAgent=0); /** * This method is used to truncate the given string. @@ -256,21 +233,12 @@ struct nsStr { 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); - /** - * This method is used to access a given char in the given string - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be appended to - * @param anIndex tells us where in dest to get the char from - * @return the given char, or 0 if anIndex is out of range - */ - static PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex); - PRUint32 mLength : 30; - eCharSize mMultibyte : 2; - PRUint32 mCapacity: 30; - PRUint32 mOwnsBuffer: 1; - PRUint32 mUnused: 1; + PRUint32 mLength; + PRUint32 mCapacity; + eCharSize mCharSize; + PRBool mOwnsBuffer; + union { char* mStr; PRUnichar* mUStr; @@ -289,26 +257,27 @@ inline PRInt32 MaxInt(PRInt32 anInt1,PRInt32 anInt2){ return (anInt1aMax) - aValue=aMax; -} - -inline void ToRange(PRUint32& aValue,PRUint32 aMin,PRUint32 aMax){ - if(aValueaMax) - aValue=aMax; -} - inline void AddNullTerminator(nsStr& aDest) { - if(eTwoByte==aDest.mMultibyte) + if(eTwoByte==aDest.mCharSize) aDest.mUStr[aDest.mLength]=0; else aDest.mStr[aDest.mLength]=0; } +/** +* This method is used to access a given char in the given string +* +* @update gess 01/04/99 +* @param aDest is the nsStr to be appended to +* @param anIndex tells us where in dest to get the char from +* @return the given char, or 0 if anIndex is out of range +*/ +inline PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex){ + if(anIndex 64) { - // When the string starts getting large, double the capacity as we grow. - theNewCapacity = aDest.mCapacity * 2; - if (theNewCapacity < aCount) { - theNewCapacity = aDest.mCapacity + aCount; - } - } else { - // When the string is small, keep it's capacity a multiple of kGrowthDelta - PRInt32 unitDelta=(aCount/eGrowthDelta)+1; - theNewCapacity=unitDelta*eGrowthDelta; + //we're given the acount value in charunits; now scale up to next multiple. + PRUint32 theNewCapacity=eDefaultSize; + while(theNewCapacitySetString(aUnicodeStr,len); } + } @@ -233,7 +237,7 @@ nsString::nsString(const PRUnichar* aUnicodeStr){ * Destructor * @update gess7/30/98 */ -nsString::~nsString() +nsString1::~nsString1() { if(mStr && (mStr!=kCommonEmptyBuffer)) delete [] mStr; @@ -249,14 +253,14 @@ nsString::~nsString() * @param anIndex -- new length of string * @return nada */ -void nsString::Truncate(PRInt32 anIndex) { +void nsString1::Truncate(PRInt32 anIndex) { if((anIndex>-1) && (anIndexAdd(sizeof(*this)); aHandler->Add(mCapacity * sizeof(chartype)); @@ -269,7 +273,7 @@ void nsString::SizeOf(nsISizeOfHandler* aHandler) const * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString::IsOrdered(void) const { +PRBool nsString1::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { PRInt32 theIndex; @@ -290,7 +294,7 @@ PRBool nsString::IsOrdered(void) const { * @param aNewLength -- new capacity of string * @return void */ -void nsString::EnsureCapacityFor(PRInt32 aNewLength) +void nsString1::EnsureCapacityFor(PRInt32 aNewLength) { PRInt32 newCapacity; @@ -328,12 +332,12 @@ void nsString::EnsureCapacityFor(PRInt32 aNewLength) * @param aLength -- contains new length for mStr * @return */ -void nsString::SetLength(PRInt32 aLength) { +void nsString1::SetLength(PRInt32 aLength) { if(aLength>mLength) { EnsureCapacityFor(aLength); nsCRT::zero(mStr + mLength, (aLength - mLength) * sizeof(chartype)); } - if((aLength>0) && (aLength=0) && (aLength aBufLength) ? aBufLength : mLength; @@ -712,7 +723,7 @@ char* nsString::ToCString(char* aBuf, PRInt32 aBufLength) const * @param aErrorCode will contain error if one occurs * @return float rep of string value */ -float nsString::ToFloat(PRInt32* aErrorCode) const +float nsString1::ToFloat(PRInt32* aErrorCode) const { char buf[40]; if (mLength > PRInt32(sizeof(buf)-1)) { @@ -735,7 +746,7 @@ float nsString::ToFloat(PRInt32* aErrorCode) const * @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 */ -PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { +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; @@ -803,7 +814,7 @@ PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { * @param PRUnichar: buffer to be assigned to this * @return this */ -nsString& nsString::SetString(const PRUnichar* aStr,PRInt32 aLength) { +nsString1& nsString1::SetString(const PRUnichar* aStr,PRInt32 aLength) { if((0 == aLength) || (nsnull == aStr)) { mLength=0; if (nsnull != mStr) { @@ -829,7 +840,7 @@ nsString& nsString::SetString(const PRUnichar* aStr,PRInt32 aLength) { * @param aCString: buffer to be assigned to this * @return this */ -nsString& nsString::SetString(const char* aCString,PRInt32 aLength) { +nsString1& nsString1::SetString(const char* aCString,PRInt32 aLength) { if(aCString!=0) { PRInt32 len=(aLength<0) ? nsCRT::strlen(aCString) : aLength; if(mCapacity<=len) @@ -847,6 +858,29 @@ nsString& nsString::SetString(const char* aCString,PRInt32 aLength) { mLength=0; //This little bit of code handles the case mStr[0]=0; //where some blockhead hands us a null string } + + return *this; +} + +/** + * 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 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); } @@ -866,8 +900,8 @@ nsString& nsString::operator=(const PRUnichar* aStr) { * @param aString: string to be added to this * @return this */ -nsString& nsString::operator=(const nsString& aString) { - return this->SetString(aString.mStr,aString.mLength); +nsString1& nsString1::operator=(const nsString1& aString) { + return this->SetString(aString); } @@ -877,7 +911,7 @@ nsString& nsString::operator=(const nsString& aString) { * @param aCString: buffer to be assigned to this * @return this */ -nsString& nsString::operator=(const char* aCString) { +nsString1& nsString1::operator=(const char* aCString) { return SetString(aCString); } @@ -888,7 +922,7 @@ nsString& nsString::operator=(const char* aCString) { * @param aChar: char to be assignd to this * @return this */ -nsString& nsString::operator=(char aChar) { +nsString1& nsString1::operator=(char aChar) { return this->operator=(PRUnichar(aChar)); } @@ -898,7 +932,7 @@ nsString& nsString::operator=(char aChar) { * @param aChar: char to be assignd to this * @return this */ -nsString& nsString::operator=(PRUnichar aChar) { +nsString1& nsString1::operator=(PRUnichar aChar) { if(mCapacity<1) EnsureCapacityFor(kGrowthDelta); mStr[0]=aChar; @@ -913,7 +947,7 @@ nsString& nsString::operator=(PRUnichar aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const nsString& aString,PRInt32 aLength) { +nsString1& nsString1::Append(const nsString1& aString,PRInt32 aLength) { return Append(aString.mStr,aString.mLength); } @@ -923,7 +957,8 @@ nsString& nsString::Append(const nsString& aString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const char* aCString,PRInt32 aLength) { +nsString1& nsString1::Append(const char* aCString,PRInt32 aLength) { + if(aCString!=0) { PRInt32 len=(aLength<0) ? strlen(aCString) : aLength; if(mLength+len >= mCapacity) { @@ -947,7 +982,7 @@ nsString& nsString::Append(const char* aCString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(char aChar) { +nsString1& nsString1::Append(char aChar) { return Append(PRUnichar(aChar)); } @@ -957,7 +992,8 @@ nsString& nsString::Append(char aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const PRUnichar* aString,PRInt32 aLength) { +nsString1& nsString1::Append(const PRUnichar* aString,PRInt32 aLength) { + if(aString!=0) { PRInt32 len=(aLength<0) ? nsCRT::strlen(aString) : aLength; if(mLength+len >= mCapacity) { @@ -978,7 +1014,7 @@ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(PRUnichar aChar) { +nsString1& nsString1::Append(PRUnichar aChar) { if(mLength < mCapacity) { mStr[mLength++]=aChar; // the new string len < capacity, so just copy mStr[mLength]=0; @@ -998,7 +1034,7 @@ nsString& nsString::Append(PRUnichar aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::operator+=(const nsString &aString) { +nsString1& nsString1::operator+=(const nsString1 &aString) { return this->Append(aString.mStr,aString.mLength); } @@ -1009,7 +1045,7 @@ nsString& nsString::operator+=(const nsString &aString) { * @param aCString: buffer to be appended to this * @return this */ -nsString& nsString::operator+=(const char* aCString) { +nsString1& nsString1::operator+=(const char* aCString) { return Append(aCString); } @@ -1020,7 +1056,7 @@ nsString& nsString::operator+=(const char* aCString) { * @param aBuffer: buffer to be appended to this * @return this */ -nsString& nsString::operator+=(const PRUnichar* aBuffer) { +nsString1& nsString1::operator+=(const PRUnichar* aBuffer) { return Append(aBuffer); } @@ -1031,7 +1067,7 @@ nsString& nsString::operator+=(const PRUnichar* aBuffer) { * @param aChar: char to be appended to this * @return this */ -nsString& nsString::operator+=(PRUnichar aChar) { +nsString1& nsString1::operator+=(PRUnichar aChar) { return Append(aChar); } @@ -1041,7 +1077,7 @@ nsString& nsString::operator+=(PRUnichar aChar) { * @param * @return */ -nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { +nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { char* fmt = "%d"; if (8 == aRadix) { fmt = "%o"; @@ -1061,7 +1097,7 @@ nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { * @param * @return */ -nsString& nsString::Append(float aFloat){ +nsString1& nsString1::Append(float aFloat){ char buf[40]; PR_snprintf(buf, sizeof(buf), "%g", aFloat); Append(buf); @@ -1080,7 +1116,7 @@ nsString& nsString::Append(float aFloat){ * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 nsString::Left(nsString& aCopy,PRInt32 aCount) const { +PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { return Mid(aCopy,0,aCount); } @@ -1095,7 +1131,7 @@ PRInt32 nsString::Left(nsString& aCopy,PRInt32 aCount) const { * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 nsString::Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const { +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) { @@ -1191,7 +1228,7 @@ PRInt32 nsString::Insert(const nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -PRInt32 nsString::Insert(PRUnichar aChar,PRInt32 anOffset){ +PRInt32 nsString1::Insert(PRUnichar aChar,PRInt32 anOffset){ //1st optimization: If you're inserting at end, then simply append! if(anOffset= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { return PR_TRUE; @@ -1292,7 +1330,7 @@ PRBool nsString::IsAlpha(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if is valid space char */ -PRBool nsString::IsSpace(PRUnichar aChar) { +PRBool nsString1::IsSpace(PRUnichar aChar) { // XXX i18n if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { return PR_TRUE; @@ -1306,7 +1344,7 @@ PRBool nsString::IsSpace(PRUnichar aChar) { * @update gess 3/31/98 * @param aChar is character to be tested * @return TRUE if char is a valid digit - */PRBool nsString::IsDigit(PRUnichar aChar) { + */PRBool nsString1::IsDigit(PRUnichar aChar) { // XXX i18n return PRBool((aChar >= '0') && (aChar <= '9')); } @@ -1321,10 +1359,11 @@ PRBool nsString::IsSpace(PRUnichar aChar) { * both ends * @return this */ -nsString& nsString::Trim(const char* aTrimSet, +nsString1& nsString1::Trim(const char* aTrimSet, PRBool aEliminateLeading, PRBool aEliminateTrailing) { + PRUnichar* from = mStr; PRUnichar* end = mStr + mLength-1; PRUnichar* to = mStr; @@ -1377,10 +1416,9 @@ nsString& nsString::Trim(const char* aTrimSet, * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString& nsString::CompressWhitespace( PRBool aEliminateLeading, +nsString1& nsString1::CompressWhitespace( PRBool aEliminateLeading, PRBool aEliminateTrailing) { - Trim(" \r\n\t",aEliminateLeading,aEliminateTrailing); PRUnichar* from = mStr; @@ -1416,7 +1454,7 @@ nsString& nsString::CompressWhitespace( PRBool aEliminateLeading, * @update gess 7/27/98 * @return this */ -nsString& nsString::StripWhitespace() +nsString1& nsString1::StripWhitespace() { Trim(" \r\n\t"); return StripChars("\r\t\n"); @@ -1429,7 +1467,8 @@ nsString& nsString::StripWhitespace() * @param * @return *this */ -nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { +nsString1& nsString1::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { + PRUnichar* from = mStr; PRUnichar* end = mStr + mLength; @@ -1440,8 +1479,7 @@ nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { } from++; } - - return *this; +return *this; } /** @@ -1453,7 +1491,7 @@ nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { * @param aChar is the unicode char to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::BinarySearch(PRUnichar aChar) const { +PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { PRInt32 low=0; PRInt32 high=mLength-1; @@ -1476,7 +1514,7 @@ PRInt32 nsString::BinarySearch(PRUnichar aChar) const { * @param aCStringBuf - charstr to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const char* aCStringBuf) const{ +PRInt32 nsString1::Find(const char* aCStringBuf) const{ NS_ASSERTION(0!=aCStringBuf,kNullPointerError); PRInt32 result=kNotFound; if(aCStringBuf) { @@ -1498,7 +1536,7 @@ PRInt32 nsString::Find(const char* aCStringBuf) const{ * @param aString - PUnichar* to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const PRUnichar* aString) const{ +PRInt32 nsString1::Find(const PRUnichar* aString) const{ NS_ASSERTION(0!=aString,kNullPointerError); PRInt32 result=kNotFound; if(aString) { @@ -1518,10 +1556,10 @@ PRInt32 nsString::Find(const PRUnichar* aString) const{ * Search for given buffer within this string * * @update gess 3/25/98 - * @param nsString -- buffer to be found + * @param nsString1 -- buffer to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const nsString& aString) const{ +PRInt32 nsString1::Find(const nsString1& aString) const{ PRInt32 result=kNotFound; PRInt32 len=aString.mLength; @@ -1545,7 +1583,7 @@ PRInt32 nsString::Find(const nsString& aString) const{ * @param * @return offset of found char, or -1 (kNotFound) */ -PRInt32 nsString::Find(PRUnichar aChar, PRInt32 anOffset) const{ +PRInt32 nsString1::Find(PRUnichar aChar, PRInt32 anOffset) const{ for(PRInt32 i=anOffset;i0;i--){ @@ -1617,7 +1655,7 @@ PRInt32 nsString::RFindCharInSet(const char* anAsciiSet,PRInt32 anOffset) const{ * @param * @return */ -PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{ +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]); @@ -1635,7 +1673,7 @@ PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{ * @param * @return */ -PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=aString,kNullPointerError); if(aString) { @@ -1661,7 +1699,7 @@ PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ * @param * @return */ -PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase) const{ +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--) { @@ -1683,7 +1721,7 @@ PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase) const{ * @param * @return */ -PRInt32 nsString::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=anAsciiSet,kNullPointerError); if(anAsciiSet) { @@ -1711,7 +1749,7 @@ PRInt32 nsString::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ * @param * @return offset of char in string, or -1 (kNotFound) */ -PRInt32 nsString::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ chartype uc=nsCRT::ToUpper(aChar); for(PRInt32 offset=mLength-1;offset>=0;offset--) if(aIgnoreCase) { @@ -1735,7 +1773,7 @@ PRInt32 nsString::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { +PRInt32 nsString1::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { NS_ASSERTION(0!=aCString,kNullPointerError); if(-1!=aLength) { @@ -1769,7 +1807,7 @@ PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLengt * @param * @return */ -PRInt32 nsString::Compare(const nsString &S,PRBool aIgnoreCase) const { +PRInt32 nsString1::Compare(const nsString1 &S,PRBool aIgnoreCase) const { int maxlen=(S.mLength(const nsString &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 nsString::operator<=(const nsString &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 nsString::operator>=(const nsString &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 nsString1::operator==(const nsString1 &S) const {return Equals(S);} +PRBool nsString1::operator==(const char *s) const {return Equals(s);} +PRBool nsString1::operator==(const PRUnichar *s) const {return Equals(s);} +PRBool nsString1::operator==(PRUnichar *s) const {return Equals(s);} +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);} +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);} +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 this to given string; note that we compare full strings here. * * @update gess 7/27/98 - * @param aString is the other nsString to be compared to + * @param aString is the other nsString1 to be compared to * @return TRUE if equal */ -PRBool nsString::Equals(const nsString& aString) const { +PRBool nsString1::Equals(const nsString1& aString) const { if(aString.mLength==mLength) { PRInt32 result=nsCRT::strcmp(mStr,aString.mStr); return PRBool(0==result); @@ -1865,7 +1903,7 @@ PRBool nsString::Equals(const nsString& aString) const { * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString::Equals(const char* aCString,PRInt32 aLength) const{ +PRBool nsString1::Equals(const char* aCString,PRInt32 aLength) const{ NS_ASSERTION(0!=aCString,kNullPointerError); if((aLength>0) && (aLength!=mLength)) @@ -1882,7 +1920,7 @@ PRBool nsString::Equals(const char* aCString,PRInt32 aLength) const{ * @param aAtom -- atom to compare to this * @return TRUE if equal */ -PRBool nsString::Equals(const nsIAtom* aAtom) const +PRBool nsString1::Equals(const nsIAtom* aAtom) const { NS_ASSERTION(0!=aAtom,kNullPointerError); PRInt32 result=nsCRT::strcmp(mStr,aAtom->GetUnicode()); @@ -1897,7 +1935,7 @@ PRBool nsString::Equals(const nsIAtom* aAtom) const * @param s2 -- second string to be compared * @return TRUE if equal */ -PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString1::Equals(const PRUnichar* s1, const PRUnichar* s2) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1915,7 +1953,7 @@ PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2) const { * @param * @return */ -PRBool nsString::EqualsIgnoreCase(const nsString& aString) const{ +PRBool nsString1::EqualsIgnoreCase(const nsString1& aString) const{ if(aString.mLength==mLength) { PRInt32 result=nsCRT::strcasecmp(mStr,aString.mStr); return PRBool(0==result); @@ -1930,7 +1968,7 @@ PRBool nsString::EqualsIgnoreCase(const nsString& aString) const{ * @param * @return */ -PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const{ +PRBool nsString1::EqualsIgnoreCase(const nsIAtom *aAtom) const{ NS_ASSERTION(0!=aAtom,kNullPointerError); PRBool result=PR_FALSE; if(aAtom){ @@ -1949,7 +1987,7 @@ PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const{ * @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 nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString1::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1971,7 +2009,7 @@ PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) cons * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { +PRBool nsString1::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { NS_ASSERTION(0!=aCString,kNullPointerError); if((aLength>0) && (aLength!=mLength)) @@ -1988,7 +2026,7 @@ PRBool nsString::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { * @param * @return */ -void nsString::DebugDump(ostream& aStream) const { +void nsString1::DebugDump(ostream& aStream) const { for(int i=0;i mCapacity) { PRInt32 size = mCapacity * 2; if (size < aNewLength) { @@ -2135,7 +2174,7 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) { } void -nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const +nsAutoString1::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(sizeof(*this)); if (mStr != mBuf) { @@ -2151,8 +2190,8 @@ nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const * @param * @return */ -void nsAutoString::SelfTest(){ - nsAutoString xas("Hello there"); +void nsAutoString1::SelfTest(){ + nsAutoString1 xas("Hello there"); xas.Append("this string exceeds the max size"); xas.DebugDump(cout); } @@ -2163,7 +2202,7 @@ void nsAutoString::SelfTest(){ * @param * @return */ -ostream& operator<<(ostream& os,nsAutoString& aString){ +ostream& operator<<(ostream& os,nsAutoString1& aString){ const PRUnichar* uc=aString.GetUnicode(); int len=aString.Length(); for(int i=0;i= PRInt32(sizeof(buf))) { + PRInt32 aLength=aString.Length(); + if (aLength >= PRInt32(sizeof(buf))) { cp = aString.ToNewCString(); } else { - aString.ToCString(cp, len + 1); + aString.ToCString(cp, aLength + 1); } - if(len>0) - ::fwrite(cp, 1, len, out); + if(aLength >0) + ::fwrite(cp, 1, aLength, out); if (cp != buf) { delete [] cp; } - return (int) len; + return (int) aLength; } - + + /** * @@ -2203,38 +2242,42 @@ NS_BASE int fputs(const nsString& aString, FILE* out) * @param * @return */ -void nsString::SelfTest(void) { +void nsString1::SelfTest(void) { #ifdef RICKG_DEBUG mSelfTested=PR_TRUE; - nsAutoString a("foobar"); - nsAutoString b("foo"); - nsAutoString c(".5111"); - nsAutoString d(" 5"); - PRInt32 result=a.Compare(b); - PRInt32 result2=result; - result=c.ToInteger(&result2); - result=d.ToInteger(&result2); - result2=result; + + { + 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!"; + - nsAutoString as("Hello there"); + nsAutoString1 as("Hello there"); as.SelfTest(); static const char* temp="hello"; //first, let's test the constructors... - nsString empty; + nsString1 empty; empty=""; empty="xxx"; empty=""; - nsString a(temp); - nsString* a_=new nsString(a); //test copy constructor - nsString b("world!"); + nsString1 a(temp); + nsString1* a_=new nsString1(a); //test copy constructor + nsString1 b("world!"); //verify destructor... delete a_; @@ -2248,11 +2291,9 @@ void nsString::SelfTest(void) { //********************************************** const chartype* p1=a.GetUnicode(); - const chartype* p2=a; //should invoke the PRUnichar conversion operator... for(int i=0;itemp3.Length(),kConstructorError); //should be char longer - nsString* es1=temp2.ToNewString(); //this should make us a new string + nsString1* es1=temp2.ToNewString(); //this should make us a new string char* es2=temp2.ToNewCString(); for(i=0;i #include -class nsISizeOfHandler; #include "nsStr.h" -class NS_BASE nsString { +#ifndef USE_STRING2 +#define nsString1 nsString +#define nsAutoString1 nsAutoString +#else +#include "nsString2.h" //get new string class +#endif + +class nsISizeOfHandler; + +class NS_BASE nsString1 { public: /** @@ -51,30 +59,30 @@ class NS_BASE nsString { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString(); +nsString1(); /** * This constructor accepts an isolatin string * @param an ascii is a ptr to a 1-byte cstr */ -nsString(const char* aCString); +nsString1(const char* aCString); /** * This is our copy constructor - * @param reference to another nsString + * @param reference to another nsString1 */ -nsString(const nsString&); +nsString1(const nsString1&); /** * Constructor from a unicode string * @param anicodestr pts to a unicode string */ -nsString(const PRUnichar* aUnicode); +nsString1(const PRUnichar* aUnicode); /** * Virtual Destructor */ -virtual ~nsString(); +virtual ~nsString1(); /** @@ -144,7 +152,7 @@ operator const PRUnichar*() const; * @param offset into string * @return PRUnichar* to internal string */ -PRUnichar operator()(PRInt32 anIndex) const; +//PRUnichar operator()(PRInt32 anIndex) const; /** * Retrieve reference to unicode char at given index @@ -184,35 +192,35 @@ PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString operator+(const nsString& aString); +nsString1 operator+(const nsString1& 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 */ -nsString operator+(const char* aCString); +nsString1 operator+(const char* aCString); /** * 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 */ -nsString operator+(char aChar); +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 */ -nsString operator+(const PRUnichar* aBuffer); +nsString1 operator+(const PRUnichar* aBuffer); /** * 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 */ -nsString operator+(PRUnichar aChar); +nsString1 operator+(PRUnichar aChar); /** * Converts all chars in internal string to lower @@ -222,7 +230,7 @@ void ToLowerCase(); /** * Converts all chars in given string to lower */ -void ToLowerCase(nsString& aString) const; +void ToLowerCase(nsString1& aString) const; /** * Converts all chars in given string to upper @@ -238,13 +246,13 @@ void ToUCS2(PRInt32 aStartOffset); /** * Converts all chars in internal string to upper */ -void ToUpperCase(nsString& aString) const; +void ToUpperCase(nsString1& aString) const; /** * Creates a duplicate clone (ptr) of this string. * @return ptr to clone of this string */ -nsString* ToNewString() const; +nsString1* ToNewString() const; /** * Creates an ascii clone of this string @@ -266,7 +274,7 @@ char* ToCString(char* aBuf,PRInt32 aBufLength) const; * @param aString to hold copy of this * @return nada. */ -void Copy(nsString& aString) const; +void Copy(nsString1& aString) const; /** * Creates an unichar clone of this string @@ -299,79 +307,72 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; if you want me to determine its length * @return this */ -nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); - -/** - * assign given char* to this string - * @param aCString: 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 - */ -nsString& SetString(const char* aCString,PRInt32 aLength=-1); +nsString1& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); +nsString1& SetString(const char* aCString,PRInt32 aLength=-1); +nsString1& SetString(const nsString1& aString); /** * assign given string to this one * @param aString: string to be added to this * @return this */ -nsString& operator=(const nsString& aString); +nsString1& operator=(const nsString1& aString); /** * assign given char* to this string * @param aCString: buffer to be assigned to this * @return this */ -nsString& operator=(const char* aCString); +nsString1& operator=(const char* aCString); /** * assign given char to this string * @param aChar: char to be assignd to this * @return this */ -nsString& operator=(char aChar); +nsString1& operator=(char aChar); /** * assign given unichar* to this string * @param aBuffer: unichar buffer to be assigned to this * @return this */ -nsString& operator=(const PRUnichar* aBuffer); +nsString1& operator=(const PRUnichar* aBuffer); /** * assign given char to this string * @param aChar: char to be assignd to this * @return this */ -nsString& operator=(PRUnichar aChar); +nsString1& operator=(PRUnichar aChar); /** * append given string to this string * @param aString : string to be appended to this * @return this */ -nsString& operator+=(const nsString& aString); +nsString1& operator+=(const nsString1& aString); /** * append given buffer to this string * @param aCString: buffer to be appended to this * @return this */ -nsString& operator+=(const char* aCString); +nsString1& operator+=(const char* aCString); /** * append given buffer to this string * @param aBuffer: buffer to be appended to this * @return this */ -nsString& operator+=(const PRUnichar* aBuffer); +nsString1& operator+=(const PRUnichar* aBuffer); /** * append given char to this string * @param aChar: char to be appended to this * @return this */ -nsString& operator+=(PRUnichar aChar); +nsString1& operator+=(PRUnichar aChar); /** * append given string to this string @@ -380,7 +381,7 @@ nsString& operator+=(PRUnichar aChar); if you want me to determine its length * @return this */ -nsString& Append(const nsString& aString,PRInt32 aLength=-1); +nsString1& Append(const nsString1& aString,PRInt32 aLength=-1); /** * append given string to this string @@ -389,14 +390,14 @@ nsString& Append(const nsString& aString,PRInt32 aLength=-1); if you want me to determine its length * @return this */ -nsString& Append(const char* aCString,PRInt32 aLength=-1); +nsString1& Append(const char* aCString,PRInt32 aLength=-1); /** * append given string to this string * @param aString : string to be appended to this * @return this */ -nsString& Append(char aChar); +nsString1& Append(char aChar); /** * append given unichar buffer to this string @@ -405,14 +406,14 @@ nsString& Append(char aChar); if you want me to determine its length * @return this */ -nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1); +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 */ -nsString& Append(PRUnichar aChar); +nsString1& Append(PRUnichar aChar); /** * Append an integer onto this string @@ -420,14 +421,14 @@ nsString& Append(PRUnichar aChar); * @param aRadix specifies 8,10,16 * @return this */ -nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16 +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 */ -nsString& Append(float aFloat); +nsString1& Append(float aFloat); /* * Copies n characters from this string to given string, @@ -438,7 +439,7 @@ nsString& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Left(nsString& aCopy,PRInt32 aCount) const; +PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -450,7 +451,7 @@ PRInt32 Left(nsString& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const; +PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -461,7 +462,7 @@ PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Right(nsString& aCopy,PRInt32 aCount) const; +PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -472,7 +473,7 @@ PRInt32 Right(nsString& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -PRInt32 Insert(const nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); +PRInt32 Insert(const nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); /** * Insert a single unicode char into this string at @@ -492,7 +493,7 @@ PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString& Cut(PRInt32 anOffset,PRInt32 aCount); +nsString1& Cut(PRInt32 anOffset,PRInt32 aCount); /** * This method is used to remove all occurances of the @@ -501,7 +502,7 @@ nsString& Cut(PRInt32 anOffset,PRInt32 aCount); * @param aSet -- characters to be cut from this * @return *this */ -nsString& StripChars(const char* aSet); +nsString1& StripChars(const char* aSet); /** * This method is used to replace all occurances of the @@ -510,14 +511,14 @@ nsString& StripChars(const char* aSet); * @param * @return *this */ -nsString& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); +nsString1& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); /** * This method strips whitespace throughout the string * * @return this */ -nsString& StripWhitespace(); +nsString1& StripWhitespace(); /** * This method trims characters found in aTrimSet from @@ -527,7 +528,7 @@ nsString& StripWhitespace(); * both ends * @return this */ -nsString& Trim(const char* aSet, +nsString1& Trim(const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE); @@ -540,7 +541,7 @@ nsString& Trim(const char* aSet, * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, +nsString1& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE); /** @@ -589,7 +590,7 @@ PRInt32 BinarySearch(PRUnichar aChar) const; */ PRInt32 Find(const char* aString) const; PRInt32 Find(const PRUnichar* aString) const; -PRInt32 Find(const nsString& aString) const; +PRInt32 Find(const nsString1& aString) const; /** * Search for given char within this string @@ -607,7 +608,7 @@ PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const; * @return -1 if not found, else the offset in this */ PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const; +PRInt32 FindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; /** * This method searches this string for the last character @@ -617,7 +618,7 @@ PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const; * @return -1 if not found, else the offset in this */ PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const; +PRInt32 RFindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; /** @@ -628,7 +629,7 @@ PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const; */ PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString& 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 @@ -649,7 +650,7 @@ PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const; * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -virtual PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE) const; +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; @@ -658,7 +659,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 nsString &aString) const; +PRBool operator==(const nsString1 &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; PRBool operator==(PRUnichar* aString) const; @@ -668,7 +669,7 @@ PRBool operator==(PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE */ -PRBool operator!=(const nsString &aString) const; +PRBool operator!=(const nsString1 &aString) const; PRBool operator!=(const char *aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -677,7 +678,7 @@ PRBool operator!=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<(const nsString &aString) const; +PRBool operator<(const nsString1 &aString) const; PRBool operator<(const char *aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -686,7 +687,7 @@ PRBool operator<(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>(const nsString &S) const; +PRBool operator>(const nsString1 &S) const; PRBool operator>(const char *aCString) const; PRBool operator>(const PRUnichar* aString) const; @@ -695,7 +696,7 @@ PRBool operator>(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<=(const nsString &S) const; +PRBool operator<=(const nsString1 &S) const; PRBool operator<=(const char *aCString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -704,7 +705,7 @@ PRBool operator<=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>=(const nsString &S) const; +PRBool operator>=(const nsString1 &S) const; PRBool operator>=(const char* aCString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -718,7 +719,7 @@ PRBool operator>=(const PRUnichar* aString) const; * @param aLength -- optional length of given string. * @return TRUE if equal */ -PRBool Equals(const nsString& aString) const; +PRBool Equals(const nsString1& aString) const; PRBool Equals(const char* aString,PRInt32 aLength=-1) const; PRBool Equals(const nsIAtom *aAtom) const; @@ -741,7 +742,7 @@ PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const; * @param aLength -- optional length of given string. * @return TRUE if equal */ -PRBool EqualsIgnoreCase(const nsString& aString) const; +PRBool EqualsIgnoreCase(const nsString1& aString) const; PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; @@ -769,34 +770,34 @@ typedef PRUnichar chartype; #endif }; -ostream& operator<<(ostream& os,nsString& aString); -extern NS_BASE int fputs(const nsString& aString, FILE* out); +ostream& operator<<(ostream& os,nsString1& aString); +extern NS_BASE int fputs(const nsString1& aString, FILE* out); //---------------------------------------------------------------------- /** - * A version of nsString which is designed to be used as an automatic + * 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_BASE nsAutoString : public nsString { +class NS_BASE nsAutoString1 : public nsString1 { public: - nsAutoString(); - nsAutoString(const nsString& other); - nsAutoString(const nsAutoString& other); - nsAutoString(PRUnichar aChar); - nsAutoString(const char* aCString); - nsAutoString(const PRUnichar* us, PRInt32 uslen = -1); - virtual ~nsAutoString(); + 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(); - nsAutoString& operator=(const nsString& aString) {nsString::operator=(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;} + 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; @@ -808,7 +809,7 @@ protected: chartype mBuf[32]; }; -ostream& operator<<(ostream& os,nsAutoString& aString); +ostream& operator<<(ostream& os,nsAutoString1& aString); #endif diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index 95afc27ff6b..d1224ddc94f 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -19,30 +19,25 @@ #include #include -#include +#include #include #include "nsString2.h" #include "nsDebug.h" #include "nsCRT.h" -#include "nsIAtom.h" -#include "nsISizeOfHandler.h" -#include "prprf.h" -#include "prdtoa.h" +#include "nsIAtom.h" #include "nsDeque.h" -#include "nsUnicharUtilCIID.h" -#include "nsIServiceManager.h" -#include "nsICaseConversion.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 "; -#ifdef RICKG_DEBUG -PRBool nsString2::mSelfTested = PR_FALSE; -#endif -#define NOT_USED 0xfffd +#define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { NOT_USED, @@ -96,61 +91,7 @@ public: } } }; -static CTableConstructor gTableConstructor; -//---- XPCOM code to connect with UnicharUtil - -class HandleCaseConversionShutdown3 : public nsIShutdownListener { -public : - NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); - HandleCaseConversionShutdown3(void) { NS_INIT_REFCNT(); } - virtual ~HandleCaseConversionShutdown3(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(HandleCaseConversionShutdown3, kIShutdownListenerIID); - -nsresult -HandleCaseConversionShutdown3::OnShutdown(const nsCID& cid, nsISupports* service) -{ - if (cid.Equals(kUnicharUtilCID)) { - NS_ASSERTION(service == gCaseConv, "wrong service!"); - gCaseConv->Release(); - gCaseConv = NULL; - } - return NS_OK; -} - -#if 0 -/* not used yet */ -static HandleCaseConversionShutdown3* gListener = NULL; - -static void StartUpCaseConversion() -{ - nsresult err; - - if ( NULL == gListener ) - { - gListener = new HandleCaseConversionShutdown3(); - gListener->AddRef(); - } - err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, - (nsISupports**) &gCaseConv, gListener); -} -static void CheckCaseConversion() -{ - if(NULL == gCaseConv ) - StartUpCaseConversion(); - - // NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); - -} -#endif /*********************************************************************** IMPLEMENTATION NOTES: @@ -159,6 +100,15 @@ static void CheckCaseConversion() that no poor soul would ever have to do this again. Sigh. ***********************************************************************/ +void Subsume(nsStr& aDest,nsStr& aSource){ + 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; +} /** * Default constructor. Note that we actually allocate a small buffer @@ -167,14 +117,10 @@ static void CheckCaseConversion() * performance reasons. */ nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { - nsStr::Initialize(*this,aCharSize); - -#ifdef RICKG_DEBUG - if(!mSelfTested) { - mSelfTested=PR_TRUE; - SelfTest(); - } +#ifdef RICKG_DEBUG + static CStringTester gStringTester; #endif + nsStr::Initialize(*this,aCharSize); } /** @@ -213,10 +159,19 @@ nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* an * @param reference to another nsString2 */ nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { - nsStr::Initialize(*this,(eCharSize)aString.mMultibyte); + nsStr::Initialize(*this,(eCharSize)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 + */ +nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { + Subsume(*this,aSubsumeStr); +} + /** * Destructor * Make sure we call nsStr::Destroy. @@ -227,7 +182,7 @@ nsString2::~nsString2() { void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(sizeof(*this)); - aHandler->Add(mCapacity << mMultibyte); + aHandler->Add(mCapacity << mCharSize); } /** @@ -291,11 +246,10 @@ void nsString2::SetCapacity(PRUint32 aLength) { /** * * @update gess1/4/99 - * @param - * @return + * @return ptr to internal buffer (if 1-byte), otherwise NULL */ -char* nsString2::GetBuffer(void) const { - if(!mMultibyte) +const char* nsString2::GetBuffer(void) const { + if(!mCharSize) return mStr; return 0; } @@ -303,11 +257,10 @@ char* nsString2::GetBuffer(void) const { /** * * @update gess1/4/99 - * @param - * @return + * @return ptr to internal buffer (if 2-byte), otherwise NULL */ -PRUnichar* nsString2::GetUnicode(void) const { - if(mMultibyte) +const PRUnichar* nsString2::GetUnicode(void) const { + if(mCharSize) return (PRUnichar*)mUStr; return 0; } @@ -315,11 +268,11 @@ PRUnichar* nsString2::GetUnicode(void) const { /** * Get nth character. */ -PRUnichar nsString2::operator[](int anIndex) const { +PRUnichar nsString2::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::CharAt(int anIndex) const { +PRUnichar nsString2::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } @@ -334,7 +287,7 @@ PRUnichar nsString2::Last(void) const{ PRBool nsString2::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex(const nsStr &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 nsString2::operator<=(const nsStr &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 nsString2::operator>=(const nsStr &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);} @@ -1416,6 +1421,18 @@ PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) con return Equals(s1,s2,PR_TRUE); } +/** + * 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 + * @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); +} + /** * Compare this to given string; note that we compare full strings here. * @@ -1569,21 +1586,42 @@ PRBool nsString2::IsDigit(PRUnichar aChar) { } +/************************************************************** + Define the string deallocator class... + **************************************************************/ +class nsStringDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + static nsMemoryAgent theAgent; + nsString2* aString= (nsString2*)anObject; + if(aString){ + aString->mAgent=&theAgent; + delete aString; + } + return 0; + } +}; + /**************************************************************************** * This class, appropriately enough, creates and recycles nsString2 objects.. ****************************************************************************/ -#if 0 + class nsStringRecycler { public: nsStringRecycler() : mDeque(0) { } + ~nsStringRecycler() { + nsStringDeallocator theDeallocator; + mDeque.ForEach(theDeallocator); //now delete the strings + } + void Recycle(nsString2* aString) { mDeque.Push(aString); } - nsString2* NewString(eCharSize aCharSize){ + nsString2* CreateString(eCharSize aCharSize){ nsString2* result=(nsString2*)mDeque.Pop(); if(!result) result=new nsString2(aCharSize); @@ -1613,8 +1651,8 @@ nsStringRecycler& GetRecycler(void){ * @param * @return */ -nsString2* nsString2::NewString(eCharSize aCharSize){ - nsString2* result=GetRecycler().NewString(aCharSize); +nsString2* nsString2::CreateString(eCharSize aCharSize){ + nsString2* result=GetRecycler().CreateString(aCharSize); return result; } @@ -1628,7 +1666,6 @@ void nsString2::Recycle(nsString2* aString){ GetRecycler().Recycle(aString); } -#endif /** * @@ -1650,7 +1687,7 @@ void nsString2::DebugDump(ostream& aStream) const { * @return */ ostream& operator<<(ostream& os,nsString2& aString){ - if(PR_FALSE==aString.mMultibyte) { + if(PR_FALSE==aString.mCharSize) { os<>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((eCharSize)aStr.mCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,(eCharSize)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((eCharSize)aCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,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(char* aCString,PRUint32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { + mAgent=0; + if(assumeOwnership) { + nsStr::Initialize(*this,aCString,aCapacity,0,eOneByte,PR_TRUE); + } + 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) { + mAgent=0; + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aString); +} + +/** + * Copy construct from uni-string + * @param aString is a ptr to a unistr + */ +nsAutoString2::nsAutoString2(PRUnichar* aString,PRUint32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { + mAgent=0; + if(assumeOwnership) { + nsStr::Initialize(*this,(char*)aString,aCapacity,0,eTwoByte,PR_TRUE); + } + else { + 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 nsStr& aString,eCharSize aCharSize) : nsString2(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 nsAutoString2& aString,eCharSize aCharSize) : nsString2(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); +} + + +/** + * Copy construct from an nsString2 + * @param + */ +nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ + mAgent=0; + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aChar); +} + +/** + * construct from a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString + */ +nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2((eCharSize)aSubsumeStr.mCharSize) { + mAgent=0; + Subsume(*this,aSubsumeStr); +} + +/** + * deconstruct the autstring + * @param + */ +nsAutoString2::~nsAutoString2(){ +// bool b=true; +// mStr=0; +} + +void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity << mCharSize); +} + +nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2((eCharSize)aString.mCharSize) { + Subsume(*this,aString); +} + +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharSize) { + Subsume(*this,aString); +} + +nsSubsumeStr::nsSubsumeStr(const PRUnichar* aString) : nsString2(aString,eTwoByte) { +} + +nsSubsumeStr::nsSubsumeStr(const char* aString) : nsString2(aString,eOneByte) { +} + +#ifdef RICKG_DEBUG +/*********************************************************************** + IMPLEMENTATION of CStringTester... + ***********************************************************************/ +CStringTester::CStringTester() { -#ifdef RICKG_DEBUG static const char* kConstructorError = kConstructorError; static const char* kComparisonError = "Comparision error!"; static const char* kEqualsError = "Equals error!"; - mSelfTested=PR_TRUE; - eCharSize theSize=eOneByte; + eCharSize theSize=eTwoByte; //begin by testing the constructors... { { nsString2 theString0("foo",theSize); //watch it construct and destruct - } + + { + nsString2 theString("hello"); + nsString2 temp1=theString+" there!"; + nsString2 temp2=theString+'!'; + nsSubsumeStr temp3=theString+'?'; + nsString2 temp4(temp3); + temp1=temp3; + nsSubsumeStr temp5("hello"); + } + nsString2 theString1(theSize); nsString2 theString("hello",theSize); nsString2 theString3(theString,theSize); @@ -1725,6 +1934,8 @@ void nsString2::SelfTest(void) { //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; @@ -1740,12 +1951,17 @@ void nsString2::SelfTest(void) { 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... + + //try a few numeric conversion routines... { nsString2 str1("10000",theSize); PRInt32 err; @@ -1757,11 +1973,12 @@ void nsString2::SelfTest(void) { { nsString2 theString("hello",theSize); PRUint32 len=theString.Length(); + PRUnichar ch; for(PRUint32 i=0;i>aDest.mMultibyte)-1; - aDest.mOwnsBuffer=aBufDescriptor.mOwnsBuffer; - AddNullTerminator(aDest); -} - -/** - * 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 - */ -nsAutoString2::nsAutoString2(eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); -} - -/** - * 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(nsBufDescriptor& aBufDescriptor,const char* aCString) : nsString2(aBufDescriptor.mMultibyte) { - InitAutoStr(*this,aBufDescriptor); - 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) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aCString); -} - -/** - * Copy construct from ascii c-string - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(char* aCString,PRUint32 aLen,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - if(assumeOwnership) { - nsBufDescriptor theDescriptor(aCString,aLen,eOneByte,PR_TRUE); - InitAutoStr(*this,theDescriptor); + { + //now let's try a memory allocation test... + nsString2 temp; + for(int i=0;i<100;i++){ + temp+="hello "; + } + int x=5; } - else { - nsBufDescriptor theDescriptor(mBuffer,aLen,aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - } - Assign(aCString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - */ -nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - */ -nsAutoString2::nsAutoString2(PRUnichar* aString,PRUint32 aLength,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - if(assumeOwnership) { - nsBufDescriptor theDescriptor((char*)aString,aLength,eTwoByte,PR_TRUE); - InitAutoStr(*this,theDescriptor); - } - else { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - } - Assign(aString); -} - - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsStr& aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsString2& aString,eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aChar); -} - -/** - * deconstruct the autstring - * @param - */ -nsAutoString2::~nsAutoString2(){ -// bool b=true; -// mStr=0; -} - -void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { - aHandler->Add(sizeof(*this)); - aHandler->Add(mCapacity << mMultibyte); -} +}; +#endif //rickg_debug diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index d5e07d9f994..ec7b2be85fe 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -20,18 +20,19 @@ /*********************************************************************** MODULE NOTES: - This class provides a 1-byte ASCII string implementation that shares - a common API with all other strImpl derivatives. + 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 _nsString2 #define _nsString2 -//#define nsString2 nsString -//#define nsAutoString2 nsAutoString - - #include "prtypes.h" #include "nscore.h" #include @@ -46,6 +47,13 @@ class nsISizeOfHandler; +#ifdef USE_STRING2 +#define nsString2 nsString +#define nsAutoString2 nsAutoString +#endif + +class NS_BASE nsSubsumeStr; + class NS_BASE nsString2 : public nsStr { public: @@ -83,6 +91,12 @@ nsString2(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAg */ nsString2(const nsString2& aString); +/** + * This constructor takes a subsumestr + * @param reference to subsumestr + */ +nsString2(nsSubsumeStr& aSubsumeStr); + /** * Destructor * @@ -139,16 +153,15 @@ PRBool IsOrdered(void) const; Accessor methods... *********************************************************************/ -char* GetBuffer(void) const; -PRUnichar* GetUnicode(void) const; -operator PRUnichar*() const {return GetUnicode();} +const char* GetBuffer(void) const; +const PRUnichar* GetUnicode(void) const; /** * Get nth character. */ -PRUnichar operator[](int anIndex) const; -PRUnichar CharAt(int anIndex) const; +PRUnichar operator[](PRUint32 anIndex) const; +PRUnichar CharAt(PRUint32 anIndex) const; PRUnichar First(void) const; PRUnichar Last(void) const; @@ -164,42 +177,42 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString2 operator+(const nsStr& aString); +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 */ -nsString2 operator+(const nsString2& aString); +nsSubsumeStr operator+(const nsString2& 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 */ -nsString2 operator+(const char* aCString); +nsSubsumeStr 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 */ -nsString2 operator+(const PRUnichar* aString); +nsSubsumeStr 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 */ -nsString2 operator+(char aChar); +nsSubsumeStr 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 */ -nsString2 operator+(PRUnichar aChar); +nsSubsumeStr operator+(PRUnichar aChar); /********************************************************************** Lexomorphic transforms... @@ -353,7 +366,6 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=10) const; 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);} -void Copy(nsString2& aString) const; /** * assign given string to this string @@ -380,6 +392,7 @@ 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);} +nsString2& operator=(nsSubsumeStr& aSubsumeString); /** * Here's a bunch of append mehtods for varying types... @@ -402,8 +415,7 @@ nsString2& operator+=(PRUnichar aChar){return Append(aChar);} */ nsString2& Append(const nsStr& aString) {return Append(aString,aString.mLength);} nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLength);} -nsString2& Append(const char* aString) {if(aString) {Append(aString,nsCRT::strlen(aString));} return *this;} -nsString2& Append(const PRUnichar* aString) {if(aString) {Append(aString,nsCRT::strlen(aString));} return *this;} + /* * Appends n characters from given string to this, @@ -414,13 +426,13 @@ nsString2& Append(const PRUnichar* aString) {if(aString) {Append(aString,nsCRT:: */ nsString2& Append(const nsStr& aString,PRInt32 aCount); nsString2& Append(const nsString2& aString,PRInt32 aCount); -nsString2& Append(const char* aString,PRInt32 aCount); -nsString2& Append(const PRUnichar* 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); - + /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -464,7 +476,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(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -519,10 +531,11 @@ 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(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) const; +PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; /** * This method searches this string for the first character @@ -554,9 +567,10 @@ PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; * @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(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) const; +PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; /********************************************************************** Comparison methods... @@ -569,6 +583,7 @@ PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) 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 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; @@ -578,6 +593,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 nsStr &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; @@ -587,6 +603,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 nsStr &aString) const; PRBool operator!=(const char* aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -596,6 +613,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 nsStr &aString) const; PRBool operator<(const char* aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -605,6 +623,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 nsStr &S) const; PRBool operator>(const char* aString) const; PRBool operator>(const PRUnichar* aString) const; @@ -614,6 +633,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 nsStr &S) const; PRBool operator<=(const char* aString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -623,6 +643,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 nsStr &S) const; PRBool operator>=(const char* aString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -637,6 +658,7 @@ PRBool operator>=(const PRUnichar* aString) const; * @param aLength -- optional length of given string. * @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; @@ -675,18 +697,11 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -#if 0 static void Recycle(nsString2* aString); -static nsString2* NewString(eCharSize aCharSize=eTwoByte); -#endif +static nsString2* CreateString(eCharSize aCharSize=eTwoByte); -static void SelfTest(); virtual void DebugDump(ostream& aStream) const; -#ifdef RICKG_DEBUG - static PRBool mSelfTested; -#endif - nsIMemoryAgent* mAgent; }; @@ -701,12 +716,11 @@ ostream& operator<<(ostream& os,nsString2& aString); If the buffer needs to grow, it gets reallocated on the heap. **************************************************************/ - class NS_BASE nsAutoString2 : public nsString2 { public: nsAutoString2(eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(nsBufDescriptor& anExtBuffer,const char* aCString); + nsAutoString2(nsStr& anExtBuffer,const char* aCString); nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); @@ -717,6 +731,7 @@ public: nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString2(nsSubsumeStr& aSubsumeStr); nsAutoString2(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); virtual ~nsAutoString2(); @@ -738,5 +753,37 @@ public: }; +/*************************************************************** + 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_BASE nsSubsumeStr : public nsString2 { +public: + nsSubsumeStr(nsString2& aString); + nsSubsumeStr(nsStr& aString); + nsSubsumeStr(const PRUnichar* aString); + nsSubsumeStr(const char* aString); +}; + +#ifdef RICKG_DEBUG +/******************************************************** + This class's only purpose in life is to test nsString2. + ********************************************************/ +class CStringTester { +public: + CStringTester(); +}; #endif +#endif + + diff --git a/mozilla/xpcom/ds/bufferRoutines.h b/mozilla/xpcom/ds/bufferRoutines.h index e327f3bd09e..d30785a93cb 100644 --- a/mozilla/xpcom/ds/bufferRoutines.h +++ b/mozilla/xpcom/ds/bufferRoutines.h @@ -31,6 +31,13 @@ #include "nsCRT.h" +#ifndef RICKG_TESTBED +#include "nsUnicharUtilCIID.h" +#include "nsIServiceManager.h" +#include "nsICaseConversion.h" +#endif + + inline PRUnichar GetUnicharAt(const char* aString,PRUint32 anIndex) { return ((PRUnichar*)aString)[anIndex]; } @@ -45,11 +52,6 @@ inline PRUnichar GetCharAt(const char* aString,PRUint32 anIndex) { // The functions are differentiated by shift direction and the underlying charsize. // -PRInt32 TrimChars2(char* aString,PRUint32 aLength,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing); -PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing); -PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing); - - /** * This method shifts single byte characters left by a given amount from an given offset. * @update gess 01/04/99 @@ -60,7 +62,7 @@ PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet,PRUint32 */ void ShiftCharsLeft(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount) { PRUint32 theMax=aLength-anOffset; -// PRUint32 theLength=(theMax=0;theIndex--){ @@ -358,7 +360,7 @@ inline PRInt32 RFindChar1(const char* aDest,PRUint32 aLength,PRUint32 anOffset,P * @param aIgnorecase tells us whether to use a case sensitive search * @return index of pos if found, else -1 (kNotFound) */ -inline PRInt32 RFindChar2(const char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUnichar aChar,PRBool aIgnoreCase) { +inline PRInt32 RFindChar2(const char* aDest,PRUint32 aLength,PRUint32 anOffset,const PRUnichar aChar,PRBool aIgnoreCase) { PRUnichar theCmpChar=(aIgnoreCase ? nsCRT::ToUpper(aChar) : aChar); PRUint32 theIndex=0; for(theIndex=aLength-1;theIndex>=0;theIndex--){ @@ -371,7 +373,7 @@ inline PRInt32 RFindChar2(const char* aDest,PRUint32 aLength,PRUint32 anOffset,P return kNotFound; } -typedef PRInt32 (*FindChars)(const char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUnichar aChar,PRBool aIgnoreCase); +typedef PRInt32 (*FindChars)(const char* aDest,PRUint32 aLength,PRUint32 anOffset,const PRUnichar aChar,PRBool aIgnoreCase); FindChars gFindChars[]={&FindChar1,&FindChar2}; FindChars gRFindChars[]={&RFindChar1,&RFindChar2}; @@ -499,6 +501,53 @@ PRInt32 ConvertCase1(char* aString,PRUint32 aCount,PRBool aToUpper){ return result; } +//---------------------------------------------------------------------------------------- + +#ifndef RICKG_TESTBED +class HandleCaseConversionShutdown3 : public nsIShutdownListener { +public : + NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); + HandleCaseConversionShutdown3(void) { NS_INIT_REFCNT(); } + virtual ~HandleCaseConversionShutdown3(void) {} + NS_DECL_ISUPPORTS +}; + +static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); +static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); +static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); +static nsICaseConversion * gCaseConv = 0; + +NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown3, kIShutdownListenerIID); + +nsresult HandleCaseConversionShutdown3::OnShutdown(const nsCID& cid, nsISupports* service) { + if (cid.Equals(kUnicharUtilCID)) { + NS_ASSERTION(service == gCaseConv, "wrong service!"); + if(gCaseConv){ + gCaseConv->Release(); + gCaseConv = 0; + } + } + return NS_OK; +} + + +class CCaseConversionServiceInitializer { +public: + CCaseConversionServiceInitializer(){ + mListener = new HandleCaseConversionShutdown3(); + if(mListener){ + mListener->AddRef(); + nsresult result=nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID,(nsISupports**) &gCaseConv, mListener); + } + } +protected: + HandleCaseConversionShutdown3* mListener; +}; + +#endif + +//---------------------------------------------------------------------------------------- + /** * This method performs a case conversion the data in the given buffer * @@ -509,14 +558,25 @@ PRInt32 ConvertCase1(char* aString,PRUint32 aCount,PRBool aToUpper){ * @return 0 */ PRInt32 ConvertCase2(char* aString,PRUint32 aCount,PRBool aToUpper){ + PRUnichar* cp = (PRUnichar*)aString; + PRUnichar* end = cp + aCount-1; PRInt32 result=0; - typedef PRUnichar chartype; - chartype* cp = (chartype*)aString; - chartype* end = cp + aCount-1; +#ifndef RICKG_TESTBED + static CCaseConversionServiceInitializer gCaseConversionServiceInitializer; + + // I18N code begin + if(gCaseConv) { + nsresult err=(aToUpper) ? gCaseConv->ToUpper(cp, cp, aCount) : gCaseConv->ToLower(cp, cp, aCount); + if(NS_SUCCEEDED(err)) + return 0; + } + // I18N code end +#endif + while (cp <= end) { - chartype ch = *cp; + PRUnichar ch = *cp; if(aToUpper) { if ((ch >= 'a') && (ch <= 'z')) { *cp = 'A' + (ch - 'a'); @@ -552,7 +612,7 @@ CaseConverters gCaseConverters[]={&ConvertCase1,&ConvertCase2}; * @return the new length of the given buffer */ PRInt32 StripChars1(char* aString,PRUint32 anOffset,PRUint32 aCount,const char* aSet){ -// PRInt32 result=0; + PRInt32 result=0; typedef char chartype; chartype* from = (chartype*)&aString[anOffset]; @@ -584,7 +644,7 @@ PRInt32 StripChars1(char* aString,PRUint32 anOffset,PRUint32 aCount,const char* * @return the new length of the given buffer */ PRInt32 StripChars2(char* aString,PRUint32 anOffset,PRUint32 aCount,const char* aSet){ -// PRInt32 result=0; + PRInt32 result=0; typedef PRUnichar chartype; chartype* from = (chartype*)&aString[anOffset]; @@ -625,7 +685,7 @@ StripChars gStripChars[]={&StripChars1,&StripChars2}; * @return the new length of the given buffer */ PRInt32 TrimChars1(char* aString,PRUint32 aLength,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){ -// PRInt32 result=0; + PRInt32 result=0; typedef char chartype; chartype* from = (chartype*)aString; @@ -748,7 +808,7 @@ TrimChars gTrimChars[]={&TrimChars1,&TrimChars2}; * @return the new length of the given buffer */ PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){ -// PRInt32 result=0; + PRInt32 result=0; TrimChars1(aString,aLength,aSet,aEliminateLeading,aEliminateTrailing); @@ -794,7 +854,7 @@ PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet,PRUint32 * @return the new length of the given buffer */ PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet,PRUint32 aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){ -// PRInt32 result=0; + PRInt32 result=0; TrimChars2(aString,aLength,aSet,aEliminateLeading,aEliminateTrailing); diff --git a/mozilla/xpcom/ds/nsStr.cpp b/mozilla/xpcom/ds/nsStr.cpp index 79e7afc9910..4d3fc6ab211 100644 --- a/mozilla/xpcom/ds/nsStr.cpp +++ b/mozilla/xpcom/ds/nsStr.cpp @@ -31,12 +31,104 @@ #include "nsStr.h" #include "bufferRoutines.h" #include "stdio.h" //only used for printf +#include "nsDeque.h" +#include "nsCRT.h" + static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; - //---------------------------------------------------------------------------------------- +// The following is a memory agent who knows how to recycled (pool) freed memory... +//---------------------------------------------------------------------------------------- + +/************************************************************** + Define the char* (pooled) deallocator class... + **************************************************************/ +class nsBufferDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + char* aCString= (char*)anObject; + delete [] aCString; + return 0; + } +}; +/** + * + * @update gess10/30/98 + * @param + * @return + */ +class nsPoolingMemoryAgent : public nsMemoryAgent{ +public: + nsPoolingMemoryAgent() { + memset(mPools,0,sizeof(mPools)); + } + + ~nsPoolingMemoryAgent() { + nsBufferDeallocator theDeallocator; + int i=0; + for(i=0;i<10;i++){ + if(mPools[i]){ + mPools[i]->ForEach(theDeallocator); //now delete the buffers + } + delete mPools[i]; + mPools[i]=0; + } + } + + virtual PRBool Alloc(nsStr& aDest,PRInt32 aCount) { + + //we're given the acount value in charunits; we have to scale up by the charsize. + int theShift=4; + PRInt32 theNewCapacity=eDefaultSize; + while(theNewCapacityPop(); + } + if(!aDest.mStr) { + //we're given the acount value in charunits; we have to scale up by the charsize. + size_t theSize=(theNewCapacity<Push(aDest.mStr); + } + else delete [] aDest.mStr; //it's too big. Just delete it. + } + aDest.mStr=0; + aDest.mOwnsBuffer=0; + return PR_TRUE; + } + return PR_FALSE; + } + nsDeque* mPools[16]; +}; + +static char* gCommonEmptyBuffer=0; /** * * @update gess10/30/98 @@ -44,9 +136,8 @@ static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; * @return */ char* GetSharedEmptyBuffer() { - static char* gCommonEmptyBuffer=0; if(!gCommonEmptyBuffer) { - const size_t theDfltSize=25; + const size_t theDfltSize=5; gCommonEmptyBuffer=new char[theDfltSize]; if(gCommonEmptyBuffer){ nsCRT::zero(gCommonEmptyBuffer,theDfltSize); @@ -69,9 +160,24 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { aDest.mStr=GetSharedEmptyBuffer(); aDest.mLength=0; aDest.mCapacity=0; - aDest.mMultibyte=aCharSize; + aDest.mCharSize=aCharSize; aDest.mOwnsBuffer=0; - NS_ASSERTION(aDest.mStr[0]==0,kFoolMsg); + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); +} + +/** + * + * @update gess10/30/98 + * @param + * @return + */ +void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer){ + aDest.mStr=(aCString) ? aCString : GetSharedEmptyBuffer(); + aDest.mLength=aLength; + aDest.mCapacity=aCapacity; + aDest.mCharSize=aCharSize; + aDest.mOwnsBuffer=aOwnsBuffer; + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -81,13 +187,9 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { * @return */ nsIMemoryAgent* GetDefaultAgent(void){ - static nsIMemoryAgent* gDefaultAgent=0; - if(!gDefaultAgent) - gDefaultAgent=new nsMemoryAgent(); - - NS_ASSERTION(gDefaultAgent,"You MUST always have an allocator!"); - - return gDefaultAgent; +// static nsPoolingMemoryAgent gDefaultAgent; + static nsMemoryAgent gDefaultAgent; + return (nsIMemoryAgent*)&gDefaultAgent; } /** @@ -110,20 +212,6 @@ void nsStr::Destroy(nsStr& aDest,nsIMemoryAgent* anAgent) { } } -/** - * - * @update gess11/12/98 - * @param - * @return - */ -PRUnichar nsStr::GetCharAt(const nsStr& aDest,PRUint32 anIndex) { - PRUnichar result=0; - if((anIndex>=0) && (anIndexRealloc(aString,aNewLength); AddNullTerminator(aString); } + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -150,13 +239,13 @@ void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* an void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { if(aNewLength>aDest.mCapacity) { nsStr theTempStr; - nsStr::Initialize(theTempStr,(eCharSize)aDest.mMultibyte); + nsStr::Initialize(theTempStr,(eCharSize)aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); EnsureCapacity(theTempStr,aNewLength,theAgent); - if(0Free(aDest); aDest.mStr = theTempStr.mStr; @@ -165,6 +254,7 @@ void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgen aDest.mCapacity=theTempStr.mCapacity; aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; } + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -182,13 +272,12 @@ void nsStr::Assign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 a /** * This method appends the given nsStr to this one. Note that we have to * pay attention to the underlying char-size of both structs. - * @update gess 04/04/99 + * @update gess10/30/98 * @param aDest is the nsStr to be manipulated * @param aSource is where char are copied from * @aCount is the number of bytes to be copied */ void nsStr::Append(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount,nsIMemoryAgent* anAgent){ - if(anOffset0) && (aTarget.mLength>0)){ PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=nsStr::GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=nsStr::GetCharAt(aTarget,aTarget.mLength-1); + PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); + PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); PRInt32 theTargetMax=aTarget.mLength; while(++index<=theMax) { PRInt32 theSubIndex=-1; PRBool matches=PR_TRUE; while((++theSubIndex0) { if(theFirstTargetChar==theChar){ - PRUnichar theDestJumpChar=nsStr::GetCharAt(aDest,index+theTargetMax); + PRUnichar theDestJumpChar=GetCharAt(aDest,index+theTargetMax); if(theDestJumpChar==theLastTargetChar) { theNewStartPos=index; //this lets us jump ahead during our search where possible. }//if }//if }//if - PRUnichar theTargetChar=nsStr::GetCharAt(aTarget,theSubIndex); + PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex); matches=PRBool(theChar==theTargetChar); } if(matches) @@ -384,8 +478,8 @@ PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIgn * @param * @return */ -PRInt32 nsStr::FindChar(const nsStr& aDest, PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 result=gFindChars[aDest.mMultibyte](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); +PRInt32 nsStr::FindChar(const nsStr& aDest,const PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { + PRInt32 result=gFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); return result; } @@ -403,7 +497,7 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore while(++index0) && (aTarget.mLength>0)){ PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=nsStr::GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=nsStr::GetCharAt(aTarget,aTarget.mLength-1); + PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); + PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); PRInt32 theTargetMax=aTarget.mLength; while(index--) { @@ -429,16 +523,16 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIg if(anOffset+aTarget.mLength<=aDest.mLength) { while((++theSubIndex0) { if(theFirstTargetChar==theChar){ - PRUnichar theDestJumpChar=nsStr::GetCharAt(aDest,index+theTargetMax); + PRUnichar theDestJumpChar=GetCharAt(aDest,index+theTargetMax); if(theDestJumpChar==theLastTargetChar) { theNewStartPos=index; //this lets us jump ahead during our search where possible. }//if }//if }//if - PRUnichar theTargetChar=nsStr::GetCharAt(aTarget,theSubIndex); + PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex); matches=PRBool(theChar==theTargetChar); } //while } //if @@ -459,8 +553,8 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIg * @param * @return */ -PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 result=gRFindChars[aDest.mMultibyte](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); +PRInt32 nsStr::RFindChar(const nsStr& aDest,const PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { + PRInt32 result=gRFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); return result; } @@ -472,12 +566,12 @@ PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase, * @return */ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 offset=aDest.mLength-anOffset; + PRUint32 offset=aDest.mLength-anOffset; PRInt32 thePos; while(--offset>=0) { - PRUnichar theChar=GetCharAt(aDest,PRUint32(offset)); - thePos=gRFindChars[aSet.mMultibyte](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); + PRUnichar theChar=GetCharAt(aDest,offset); + thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,anOffset,theChar,aIgnoreCase); if(kNotFound!=thePos) return offset; } //while @@ -491,7 +585,7 @@ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnor * @param * @return aDestaSource=1 */ -PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 /*aCount*/,PRBool aIgnoreCase) { +PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { int minlen=(aSource.mLength>aCharSize)-1), - mOwnsBuffer(aOwnsBuffer), - mMultibyte(aCharSize) -{ - mStr=aBuffer; - } - - PRUint32 mCapacity; - PRBool mOwnsBuffer; - eCharSize mMultibyte; -// UStrPtr mStr; - union { - char* mStr; - PRUnichar* mUStr; - }; -}; +const PRInt32 kNotFound = -1; class nsIMemoryAgent; @@ -99,6 +67,15 @@ struct nsStr { */ static void Initialize(nsStr& aDest,eCharSize aCharSize); + /** + * This method initializes an nsStr for use + * + * @update gess 01/04/99 + * @param aString is the nsStr to be initialized + * @param aCharSize tells us the requested char size (1 or 2 bytes) + */ + static void Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer); + /** * This method destroys the given nsStr, and *MAY* * deallocate it's memory depending on the setting @@ -168,7 +145,7 @@ struct nsStr { * @param aCount tells us the (max) # of chars to delete * @param anAgent is the allocator to be used for alloc/free operations */ - static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,nsIMemoryAgent* anAgent=0); + static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount,nsIMemoryAgent* anAgent=0); /** * This method is used to truncate the given string. @@ -256,21 +233,12 @@ struct nsStr { 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); - /** - * This method is used to access a given char in the given string - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be appended to - * @param anIndex tells us where in dest to get the char from - * @return the given char, or 0 if anIndex is out of range - */ - static PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex); - PRUint32 mLength : 30; - eCharSize mMultibyte : 2; - PRUint32 mCapacity: 30; - PRUint32 mOwnsBuffer: 1; - PRUint32 mUnused: 1; + PRUint32 mLength; + PRUint32 mCapacity; + eCharSize mCharSize; + PRBool mOwnsBuffer; + union { char* mStr; PRUnichar* mUStr; @@ -289,26 +257,27 @@ inline PRInt32 MaxInt(PRInt32 anInt1,PRInt32 anInt2){ return (anInt1aMax) - aValue=aMax; -} - -inline void ToRange(PRUint32& aValue,PRUint32 aMin,PRUint32 aMax){ - if(aValueaMax) - aValue=aMax; -} - inline void AddNullTerminator(nsStr& aDest) { - if(eTwoByte==aDest.mMultibyte) + if(eTwoByte==aDest.mCharSize) aDest.mUStr[aDest.mLength]=0; else aDest.mStr[aDest.mLength]=0; } +/** +* This method is used to access a given char in the given string +* +* @update gess 01/04/99 +* @param aDest is the nsStr to be appended to +* @param anIndex tells us where in dest to get the char from +* @return the given char, or 0 if anIndex is out of range +*/ +inline PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex){ + if(anIndex 64) { - // When the string starts getting large, double the capacity as we grow. - theNewCapacity = aDest.mCapacity * 2; - if (theNewCapacity < aCount) { - theNewCapacity = aDest.mCapacity + aCount; - } - } else { - // When the string is small, keep it's capacity a multiple of kGrowthDelta - PRInt32 unitDelta=(aCount/eGrowthDelta)+1; - theNewCapacity=unitDelta*eGrowthDelta; + //we're given the acount value in charunits; now scale up to next multiple. + PRUint32 theNewCapacity=eDefaultSize; + while(theNewCapacitySetString(aUnicodeStr,len); } + } @@ -233,7 +237,7 @@ nsString::nsString(const PRUnichar* aUnicodeStr){ * Destructor * @update gess7/30/98 */ -nsString::~nsString() +nsString1::~nsString1() { if(mStr && (mStr!=kCommonEmptyBuffer)) delete [] mStr; @@ -249,14 +253,14 @@ nsString::~nsString() * @param anIndex -- new length of string * @return nada */ -void nsString::Truncate(PRInt32 anIndex) { +void nsString1::Truncate(PRInt32 anIndex) { if((anIndex>-1) && (anIndexAdd(sizeof(*this)); aHandler->Add(mCapacity * sizeof(chartype)); @@ -269,7 +273,7 @@ void nsString::SizeOf(nsISizeOfHandler* aHandler) const * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString::IsOrdered(void) const { +PRBool nsString1::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { PRInt32 theIndex; @@ -290,7 +294,7 @@ PRBool nsString::IsOrdered(void) const { * @param aNewLength -- new capacity of string * @return void */ -void nsString::EnsureCapacityFor(PRInt32 aNewLength) +void nsString1::EnsureCapacityFor(PRInt32 aNewLength) { PRInt32 newCapacity; @@ -328,12 +332,12 @@ void nsString::EnsureCapacityFor(PRInt32 aNewLength) * @param aLength -- contains new length for mStr * @return */ -void nsString::SetLength(PRInt32 aLength) { +void nsString1::SetLength(PRInt32 aLength) { if(aLength>mLength) { EnsureCapacityFor(aLength); nsCRT::zero(mStr + mLength, (aLength - mLength) * sizeof(chartype)); } - if((aLength>0) && (aLength=0) && (aLength aBufLength) ? aBufLength : mLength; @@ -712,7 +723,7 @@ char* nsString::ToCString(char* aBuf, PRInt32 aBufLength) const * @param aErrorCode will contain error if one occurs * @return float rep of string value */ -float nsString::ToFloat(PRInt32* aErrorCode) const +float nsString1::ToFloat(PRInt32* aErrorCode) const { char buf[40]; if (mLength > PRInt32(sizeof(buf)-1)) { @@ -735,7 +746,7 @@ float nsString::ToFloat(PRInt32* aErrorCode) const * @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 */ -PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { +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; @@ -803,7 +814,7 @@ PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { * @param PRUnichar: buffer to be assigned to this * @return this */ -nsString& nsString::SetString(const PRUnichar* aStr,PRInt32 aLength) { +nsString1& nsString1::SetString(const PRUnichar* aStr,PRInt32 aLength) { if((0 == aLength) || (nsnull == aStr)) { mLength=0; if (nsnull != mStr) { @@ -829,7 +840,7 @@ nsString& nsString::SetString(const PRUnichar* aStr,PRInt32 aLength) { * @param aCString: buffer to be assigned to this * @return this */ -nsString& nsString::SetString(const char* aCString,PRInt32 aLength) { +nsString1& nsString1::SetString(const char* aCString,PRInt32 aLength) { if(aCString!=0) { PRInt32 len=(aLength<0) ? nsCRT::strlen(aCString) : aLength; if(mCapacity<=len) @@ -847,6 +858,29 @@ nsString& nsString::SetString(const char* aCString,PRInt32 aLength) { mLength=0; //This little bit of code handles the case mStr[0]=0; //where some blockhead hands us a null string } + + return *this; +} + +/** + * 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 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); } @@ -866,8 +900,8 @@ nsString& nsString::operator=(const PRUnichar* aStr) { * @param aString: string to be added to this * @return this */ -nsString& nsString::operator=(const nsString& aString) { - return this->SetString(aString.mStr,aString.mLength); +nsString1& nsString1::operator=(const nsString1& aString) { + return this->SetString(aString); } @@ -877,7 +911,7 @@ nsString& nsString::operator=(const nsString& aString) { * @param aCString: buffer to be assigned to this * @return this */ -nsString& nsString::operator=(const char* aCString) { +nsString1& nsString1::operator=(const char* aCString) { return SetString(aCString); } @@ -888,7 +922,7 @@ nsString& nsString::operator=(const char* aCString) { * @param aChar: char to be assignd to this * @return this */ -nsString& nsString::operator=(char aChar) { +nsString1& nsString1::operator=(char aChar) { return this->operator=(PRUnichar(aChar)); } @@ -898,7 +932,7 @@ nsString& nsString::operator=(char aChar) { * @param aChar: char to be assignd to this * @return this */ -nsString& nsString::operator=(PRUnichar aChar) { +nsString1& nsString1::operator=(PRUnichar aChar) { if(mCapacity<1) EnsureCapacityFor(kGrowthDelta); mStr[0]=aChar; @@ -913,7 +947,7 @@ nsString& nsString::operator=(PRUnichar aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const nsString& aString,PRInt32 aLength) { +nsString1& nsString1::Append(const nsString1& aString,PRInt32 aLength) { return Append(aString.mStr,aString.mLength); } @@ -923,7 +957,8 @@ nsString& nsString::Append(const nsString& aString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const char* aCString,PRInt32 aLength) { +nsString1& nsString1::Append(const char* aCString,PRInt32 aLength) { + if(aCString!=0) { PRInt32 len=(aLength<0) ? strlen(aCString) : aLength; if(mLength+len >= mCapacity) { @@ -947,7 +982,7 @@ nsString& nsString::Append(const char* aCString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(char aChar) { +nsString1& nsString1::Append(char aChar) { return Append(PRUnichar(aChar)); } @@ -957,7 +992,8 @@ nsString& nsString::Append(char aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const PRUnichar* aString,PRInt32 aLength) { +nsString1& nsString1::Append(const PRUnichar* aString,PRInt32 aLength) { + if(aString!=0) { PRInt32 len=(aLength<0) ? nsCRT::strlen(aString) : aLength; if(mLength+len >= mCapacity) { @@ -978,7 +1014,7 @@ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(PRUnichar aChar) { +nsString1& nsString1::Append(PRUnichar aChar) { if(mLength < mCapacity) { mStr[mLength++]=aChar; // the new string len < capacity, so just copy mStr[mLength]=0; @@ -998,7 +1034,7 @@ nsString& nsString::Append(PRUnichar aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::operator+=(const nsString &aString) { +nsString1& nsString1::operator+=(const nsString1 &aString) { return this->Append(aString.mStr,aString.mLength); } @@ -1009,7 +1045,7 @@ nsString& nsString::operator+=(const nsString &aString) { * @param aCString: buffer to be appended to this * @return this */ -nsString& nsString::operator+=(const char* aCString) { +nsString1& nsString1::operator+=(const char* aCString) { return Append(aCString); } @@ -1020,7 +1056,7 @@ nsString& nsString::operator+=(const char* aCString) { * @param aBuffer: buffer to be appended to this * @return this */ -nsString& nsString::operator+=(const PRUnichar* aBuffer) { +nsString1& nsString1::operator+=(const PRUnichar* aBuffer) { return Append(aBuffer); } @@ -1031,7 +1067,7 @@ nsString& nsString::operator+=(const PRUnichar* aBuffer) { * @param aChar: char to be appended to this * @return this */ -nsString& nsString::operator+=(PRUnichar aChar) { +nsString1& nsString1::operator+=(PRUnichar aChar) { return Append(aChar); } @@ -1041,7 +1077,7 @@ nsString& nsString::operator+=(PRUnichar aChar) { * @param * @return */ -nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { +nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { char* fmt = "%d"; if (8 == aRadix) { fmt = "%o"; @@ -1061,7 +1097,7 @@ nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { * @param * @return */ -nsString& nsString::Append(float aFloat){ +nsString1& nsString1::Append(float aFloat){ char buf[40]; PR_snprintf(buf, sizeof(buf), "%g", aFloat); Append(buf); @@ -1080,7 +1116,7 @@ nsString& nsString::Append(float aFloat){ * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 nsString::Left(nsString& aCopy,PRInt32 aCount) const { +PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { return Mid(aCopy,0,aCount); } @@ -1095,7 +1131,7 @@ PRInt32 nsString::Left(nsString& aCopy,PRInt32 aCount) const { * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 nsString::Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const { +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) { @@ -1191,7 +1228,7 @@ PRInt32 nsString::Insert(const nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -PRInt32 nsString::Insert(PRUnichar aChar,PRInt32 anOffset){ +PRInt32 nsString1::Insert(PRUnichar aChar,PRInt32 anOffset){ //1st optimization: If you're inserting at end, then simply append! if(anOffset= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { return PR_TRUE; @@ -1292,7 +1330,7 @@ PRBool nsString::IsAlpha(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if is valid space char */ -PRBool nsString::IsSpace(PRUnichar aChar) { +PRBool nsString1::IsSpace(PRUnichar aChar) { // XXX i18n if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { return PR_TRUE; @@ -1306,7 +1344,7 @@ PRBool nsString::IsSpace(PRUnichar aChar) { * @update gess 3/31/98 * @param aChar is character to be tested * @return TRUE if char is a valid digit - */PRBool nsString::IsDigit(PRUnichar aChar) { + */PRBool nsString1::IsDigit(PRUnichar aChar) { // XXX i18n return PRBool((aChar >= '0') && (aChar <= '9')); } @@ -1321,10 +1359,11 @@ PRBool nsString::IsSpace(PRUnichar aChar) { * both ends * @return this */ -nsString& nsString::Trim(const char* aTrimSet, +nsString1& nsString1::Trim(const char* aTrimSet, PRBool aEliminateLeading, PRBool aEliminateTrailing) { + PRUnichar* from = mStr; PRUnichar* end = mStr + mLength-1; PRUnichar* to = mStr; @@ -1377,10 +1416,9 @@ nsString& nsString::Trim(const char* aTrimSet, * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString& nsString::CompressWhitespace( PRBool aEliminateLeading, +nsString1& nsString1::CompressWhitespace( PRBool aEliminateLeading, PRBool aEliminateTrailing) { - Trim(" \r\n\t",aEliminateLeading,aEliminateTrailing); PRUnichar* from = mStr; @@ -1416,7 +1454,7 @@ nsString& nsString::CompressWhitespace( PRBool aEliminateLeading, * @update gess 7/27/98 * @return this */ -nsString& nsString::StripWhitespace() +nsString1& nsString1::StripWhitespace() { Trim(" \r\n\t"); return StripChars("\r\t\n"); @@ -1429,7 +1467,8 @@ nsString& nsString::StripWhitespace() * @param * @return *this */ -nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { +nsString1& nsString1::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { + PRUnichar* from = mStr; PRUnichar* end = mStr + mLength; @@ -1440,8 +1479,7 @@ nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { } from++; } - - return *this; +return *this; } /** @@ -1453,7 +1491,7 @@ nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { * @param aChar is the unicode char to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::BinarySearch(PRUnichar aChar) const { +PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { PRInt32 low=0; PRInt32 high=mLength-1; @@ -1476,7 +1514,7 @@ PRInt32 nsString::BinarySearch(PRUnichar aChar) const { * @param aCStringBuf - charstr to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const char* aCStringBuf) const{ +PRInt32 nsString1::Find(const char* aCStringBuf) const{ NS_ASSERTION(0!=aCStringBuf,kNullPointerError); PRInt32 result=kNotFound; if(aCStringBuf) { @@ -1498,7 +1536,7 @@ PRInt32 nsString::Find(const char* aCStringBuf) const{ * @param aString - PUnichar* to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const PRUnichar* aString) const{ +PRInt32 nsString1::Find(const PRUnichar* aString) const{ NS_ASSERTION(0!=aString,kNullPointerError); PRInt32 result=kNotFound; if(aString) { @@ -1518,10 +1556,10 @@ PRInt32 nsString::Find(const PRUnichar* aString) const{ * Search for given buffer within this string * * @update gess 3/25/98 - * @param nsString -- buffer to be found + * @param nsString1 -- buffer to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const nsString& aString) const{ +PRInt32 nsString1::Find(const nsString1& aString) const{ PRInt32 result=kNotFound; PRInt32 len=aString.mLength; @@ -1545,7 +1583,7 @@ PRInt32 nsString::Find(const nsString& aString) const{ * @param * @return offset of found char, or -1 (kNotFound) */ -PRInt32 nsString::Find(PRUnichar aChar, PRInt32 anOffset) const{ +PRInt32 nsString1::Find(PRUnichar aChar, PRInt32 anOffset) const{ for(PRInt32 i=anOffset;i0;i--){ @@ -1617,7 +1655,7 @@ PRInt32 nsString::RFindCharInSet(const char* anAsciiSet,PRInt32 anOffset) const{ * @param * @return */ -PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{ +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]); @@ -1635,7 +1673,7 @@ PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{ * @param * @return */ -PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=aString,kNullPointerError); if(aString) { @@ -1661,7 +1699,7 @@ PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ * @param * @return */ -PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase) const{ +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--) { @@ -1683,7 +1721,7 @@ PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase) const{ * @param * @return */ -PRInt32 nsString::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=anAsciiSet,kNullPointerError); if(anAsciiSet) { @@ -1711,7 +1749,7 @@ PRInt32 nsString::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ * @param * @return offset of char in string, or -1 (kNotFound) */ -PRInt32 nsString::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ chartype uc=nsCRT::ToUpper(aChar); for(PRInt32 offset=mLength-1;offset>=0;offset--) if(aIgnoreCase) { @@ -1735,7 +1773,7 @@ PRInt32 nsString::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { +PRInt32 nsString1::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { NS_ASSERTION(0!=aCString,kNullPointerError); if(-1!=aLength) { @@ -1769,7 +1807,7 @@ PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLengt * @param * @return */ -PRInt32 nsString::Compare(const nsString &S,PRBool aIgnoreCase) const { +PRInt32 nsString1::Compare(const nsString1 &S,PRBool aIgnoreCase) const { int maxlen=(S.mLength(const nsString &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 nsString::operator<=(const nsString &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 nsString::operator>=(const nsString &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 nsString1::operator==(const nsString1 &S) const {return Equals(S);} +PRBool nsString1::operator==(const char *s) const {return Equals(s);} +PRBool nsString1::operator==(const PRUnichar *s) const {return Equals(s);} +PRBool nsString1::operator==(PRUnichar *s) const {return Equals(s);} +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);} +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);} +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 this to given string; note that we compare full strings here. * * @update gess 7/27/98 - * @param aString is the other nsString to be compared to + * @param aString is the other nsString1 to be compared to * @return TRUE if equal */ -PRBool nsString::Equals(const nsString& aString) const { +PRBool nsString1::Equals(const nsString1& aString) const { if(aString.mLength==mLength) { PRInt32 result=nsCRT::strcmp(mStr,aString.mStr); return PRBool(0==result); @@ -1865,7 +1903,7 @@ PRBool nsString::Equals(const nsString& aString) const { * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString::Equals(const char* aCString,PRInt32 aLength) const{ +PRBool nsString1::Equals(const char* aCString,PRInt32 aLength) const{ NS_ASSERTION(0!=aCString,kNullPointerError); if((aLength>0) && (aLength!=mLength)) @@ -1882,7 +1920,7 @@ PRBool nsString::Equals(const char* aCString,PRInt32 aLength) const{ * @param aAtom -- atom to compare to this * @return TRUE if equal */ -PRBool nsString::Equals(const nsIAtom* aAtom) const +PRBool nsString1::Equals(const nsIAtom* aAtom) const { NS_ASSERTION(0!=aAtom,kNullPointerError); PRInt32 result=nsCRT::strcmp(mStr,aAtom->GetUnicode()); @@ -1897,7 +1935,7 @@ PRBool nsString::Equals(const nsIAtom* aAtom) const * @param s2 -- second string to be compared * @return TRUE if equal */ -PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString1::Equals(const PRUnichar* s1, const PRUnichar* s2) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1915,7 +1953,7 @@ PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2) const { * @param * @return */ -PRBool nsString::EqualsIgnoreCase(const nsString& aString) const{ +PRBool nsString1::EqualsIgnoreCase(const nsString1& aString) const{ if(aString.mLength==mLength) { PRInt32 result=nsCRT::strcasecmp(mStr,aString.mStr); return PRBool(0==result); @@ -1930,7 +1968,7 @@ PRBool nsString::EqualsIgnoreCase(const nsString& aString) const{ * @param * @return */ -PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const{ +PRBool nsString1::EqualsIgnoreCase(const nsIAtom *aAtom) const{ NS_ASSERTION(0!=aAtom,kNullPointerError); PRBool result=PR_FALSE; if(aAtom){ @@ -1949,7 +1987,7 @@ PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const{ * @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 nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString1::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1971,7 +2009,7 @@ PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) cons * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { +PRBool nsString1::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { NS_ASSERTION(0!=aCString,kNullPointerError); if((aLength>0) && (aLength!=mLength)) @@ -1988,7 +2026,7 @@ PRBool nsString::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { * @param * @return */ -void nsString::DebugDump(ostream& aStream) const { +void nsString1::DebugDump(ostream& aStream) const { for(int i=0;i mCapacity) { PRInt32 size = mCapacity * 2; if (size < aNewLength) { @@ -2135,7 +2174,7 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) { } void -nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const +nsAutoString1::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(sizeof(*this)); if (mStr != mBuf) { @@ -2151,8 +2190,8 @@ nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const * @param * @return */ -void nsAutoString::SelfTest(){ - nsAutoString xas("Hello there"); +void nsAutoString1::SelfTest(){ + nsAutoString1 xas("Hello there"); xas.Append("this string exceeds the max size"); xas.DebugDump(cout); } @@ -2163,7 +2202,7 @@ void nsAutoString::SelfTest(){ * @param * @return */ -ostream& operator<<(ostream& os,nsAutoString& aString){ +ostream& operator<<(ostream& os,nsAutoString1& aString){ const PRUnichar* uc=aString.GetUnicode(); int len=aString.Length(); for(int i=0;i= PRInt32(sizeof(buf))) { + PRInt32 aLength=aString.Length(); + if (aLength >= PRInt32(sizeof(buf))) { cp = aString.ToNewCString(); } else { - aString.ToCString(cp, len + 1); + aString.ToCString(cp, aLength + 1); } - if(len>0) - ::fwrite(cp, 1, len, out); + if(aLength >0) + ::fwrite(cp, 1, aLength, out); if (cp != buf) { delete [] cp; } - return (int) len; + return (int) aLength; } - + + /** * @@ -2203,38 +2242,42 @@ NS_BASE int fputs(const nsString& aString, FILE* out) * @param * @return */ -void nsString::SelfTest(void) { +void nsString1::SelfTest(void) { #ifdef RICKG_DEBUG mSelfTested=PR_TRUE; - nsAutoString a("foobar"); - nsAutoString b("foo"); - nsAutoString c(".5111"); - nsAutoString d(" 5"); - PRInt32 result=a.Compare(b); - PRInt32 result2=result; - result=c.ToInteger(&result2); - result=d.ToInteger(&result2); - result2=result; + + { + 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!"; + - nsAutoString as("Hello there"); + nsAutoString1 as("Hello there"); as.SelfTest(); static const char* temp="hello"; //first, let's test the constructors... - nsString empty; + nsString1 empty; empty=""; empty="xxx"; empty=""; - nsString a(temp); - nsString* a_=new nsString(a); //test copy constructor - nsString b("world!"); + nsString1 a(temp); + nsString1* a_=new nsString1(a); //test copy constructor + nsString1 b("world!"); //verify destructor... delete a_; @@ -2248,11 +2291,9 @@ void nsString::SelfTest(void) { //********************************************** const chartype* p1=a.GetUnicode(); - const chartype* p2=a; //should invoke the PRUnichar conversion operator... for(int i=0;itemp3.Length(),kConstructorError); //should be char longer - nsString* es1=temp2.ToNewString(); //this should make us a new string + nsString1* es1=temp2.ToNewString(); //this should make us a new string char* es2=temp2.ToNewCString(); for(i=0;i #include -class nsISizeOfHandler; #include "nsStr.h" -class NS_BASE nsString { +#ifndef USE_STRING2 +#define nsString1 nsString +#define nsAutoString1 nsAutoString +#else +#include "nsString2.h" //get new string class +#endif + +class nsISizeOfHandler; + +class NS_BASE nsString1 { public: /** @@ -51,30 +59,30 @@ class NS_BASE nsString { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString(); +nsString1(); /** * This constructor accepts an isolatin string * @param an ascii is a ptr to a 1-byte cstr */ -nsString(const char* aCString); +nsString1(const char* aCString); /** * This is our copy constructor - * @param reference to another nsString + * @param reference to another nsString1 */ -nsString(const nsString&); +nsString1(const nsString1&); /** * Constructor from a unicode string * @param anicodestr pts to a unicode string */ -nsString(const PRUnichar* aUnicode); +nsString1(const PRUnichar* aUnicode); /** * Virtual Destructor */ -virtual ~nsString(); +virtual ~nsString1(); /** @@ -144,7 +152,7 @@ operator const PRUnichar*() const; * @param offset into string * @return PRUnichar* to internal string */ -PRUnichar operator()(PRInt32 anIndex) const; +//PRUnichar operator()(PRInt32 anIndex) const; /** * Retrieve reference to unicode char at given index @@ -184,35 +192,35 @@ PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString operator+(const nsString& aString); +nsString1 operator+(const nsString1& 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 */ -nsString operator+(const char* aCString); +nsString1 operator+(const char* aCString); /** * 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 */ -nsString operator+(char aChar); +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 */ -nsString operator+(const PRUnichar* aBuffer); +nsString1 operator+(const PRUnichar* aBuffer); /** * 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 */ -nsString operator+(PRUnichar aChar); +nsString1 operator+(PRUnichar aChar); /** * Converts all chars in internal string to lower @@ -222,7 +230,7 @@ void ToLowerCase(); /** * Converts all chars in given string to lower */ -void ToLowerCase(nsString& aString) const; +void ToLowerCase(nsString1& aString) const; /** * Converts all chars in given string to upper @@ -238,13 +246,13 @@ void ToUCS2(PRInt32 aStartOffset); /** * Converts all chars in internal string to upper */ -void ToUpperCase(nsString& aString) const; +void ToUpperCase(nsString1& aString) const; /** * Creates a duplicate clone (ptr) of this string. * @return ptr to clone of this string */ -nsString* ToNewString() const; +nsString1* ToNewString() const; /** * Creates an ascii clone of this string @@ -266,7 +274,7 @@ char* ToCString(char* aBuf,PRInt32 aBufLength) const; * @param aString to hold copy of this * @return nada. */ -void Copy(nsString& aString) const; +void Copy(nsString1& aString) const; /** * Creates an unichar clone of this string @@ -299,79 +307,72 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; if you want me to determine its length * @return this */ -nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); - -/** - * assign given char* to this string - * @param aCString: 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 - */ -nsString& SetString(const char* aCString,PRInt32 aLength=-1); +nsString1& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); +nsString1& SetString(const char* aCString,PRInt32 aLength=-1); +nsString1& SetString(const nsString1& aString); /** * assign given string to this one * @param aString: string to be added to this * @return this */ -nsString& operator=(const nsString& aString); +nsString1& operator=(const nsString1& aString); /** * assign given char* to this string * @param aCString: buffer to be assigned to this * @return this */ -nsString& operator=(const char* aCString); +nsString1& operator=(const char* aCString); /** * assign given char to this string * @param aChar: char to be assignd to this * @return this */ -nsString& operator=(char aChar); +nsString1& operator=(char aChar); /** * assign given unichar* to this string * @param aBuffer: unichar buffer to be assigned to this * @return this */ -nsString& operator=(const PRUnichar* aBuffer); +nsString1& operator=(const PRUnichar* aBuffer); /** * assign given char to this string * @param aChar: char to be assignd to this * @return this */ -nsString& operator=(PRUnichar aChar); +nsString1& operator=(PRUnichar aChar); /** * append given string to this string * @param aString : string to be appended to this * @return this */ -nsString& operator+=(const nsString& aString); +nsString1& operator+=(const nsString1& aString); /** * append given buffer to this string * @param aCString: buffer to be appended to this * @return this */ -nsString& operator+=(const char* aCString); +nsString1& operator+=(const char* aCString); /** * append given buffer to this string * @param aBuffer: buffer to be appended to this * @return this */ -nsString& operator+=(const PRUnichar* aBuffer); +nsString1& operator+=(const PRUnichar* aBuffer); /** * append given char to this string * @param aChar: char to be appended to this * @return this */ -nsString& operator+=(PRUnichar aChar); +nsString1& operator+=(PRUnichar aChar); /** * append given string to this string @@ -380,7 +381,7 @@ nsString& operator+=(PRUnichar aChar); if you want me to determine its length * @return this */ -nsString& Append(const nsString& aString,PRInt32 aLength=-1); +nsString1& Append(const nsString1& aString,PRInt32 aLength=-1); /** * append given string to this string @@ -389,14 +390,14 @@ nsString& Append(const nsString& aString,PRInt32 aLength=-1); if you want me to determine its length * @return this */ -nsString& Append(const char* aCString,PRInt32 aLength=-1); +nsString1& Append(const char* aCString,PRInt32 aLength=-1); /** * append given string to this string * @param aString : string to be appended to this * @return this */ -nsString& Append(char aChar); +nsString1& Append(char aChar); /** * append given unichar buffer to this string @@ -405,14 +406,14 @@ nsString& Append(char aChar); if you want me to determine its length * @return this */ -nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1); +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 */ -nsString& Append(PRUnichar aChar); +nsString1& Append(PRUnichar aChar); /** * Append an integer onto this string @@ -420,14 +421,14 @@ nsString& Append(PRUnichar aChar); * @param aRadix specifies 8,10,16 * @return this */ -nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16 +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 */ -nsString& Append(float aFloat); +nsString1& Append(float aFloat); /* * Copies n characters from this string to given string, @@ -438,7 +439,7 @@ nsString& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Left(nsString& aCopy,PRInt32 aCount) const; +PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -450,7 +451,7 @@ PRInt32 Left(nsString& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const; +PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -461,7 +462,7 @@ PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Right(nsString& aCopy,PRInt32 aCount) const; +PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -472,7 +473,7 @@ PRInt32 Right(nsString& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -PRInt32 Insert(const nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); +PRInt32 Insert(const nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); /** * Insert a single unicode char into this string at @@ -492,7 +493,7 @@ PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString& Cut(PRInt32 anOffset,PRInt32 aCount); +nsString1& Cut(PRInt32 anOffset,PRInt32 aCount); /** * This method is used to remove all occurances of the @@ -501,7 +502,7 @@ nsString& Cut(PRInt32 anOffset,PRInt32 aCount); * @param aSet -- characters to be cut from this * @return *this */ -nsString& StripChars(const char* aSet); +nsString1& StripChars(const char* aSet); /** * This method is used to replace all occurances of the @@ -510,14 +511,14 @@ nsString& StripChars(const char* aSet); * @param * @return *this */ -nsString& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); +nsString1& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); /** * This method strips whitespace throughout the string * * @return this */ -nsString& StripWhitespace(); +nsString1& StripWhitespace(); /** * This method trims characters found in aTrimSet from @@ -527,7 +528,7 @@ nsString& StripWhitespace(); * both ends * @return this */ -nsString& Trim(const char* aSet, +nsString1& Trim(const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE); @@ -540,7 +541,7 @@ nsString& Trim(const char* aSet, * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, +nsString1& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE); /** @@ -589,7 +590,7 @@ PRInt32 BinarySearch(PRUnichar aChar) const; */ PRInt32 Find(const char* aString) const; PRInt32 Find(const PRUnichar* aString) const; -PRInt32 Find(const nsString& aString) const; +PRInt32 Find(const nsString1& aString) const; /** * Search for given char within this string @@ -607,7 +608,7 @@ PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const; * @return -1 if not found, else the offset in this */ PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const; +PRInt32 FindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; /** * This method searches this string for the last character @@ -617,7 +618,7 @@ PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const; * @return -1 if not found, else the offset in this */ PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const; +PRInt32 RFindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; /** @@ -628,7 +629,7 @@ PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const; */ PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString& 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 @@ -649,7 +650,7 @@ PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const; * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -virtual PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE) const; +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; @@ -658,7 +659,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 nsString &aString) const; +PRBool operator==(const nsString1 &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; PRBool operator==(PRUnichar* aString) const; @@ -668,7 +669,7 @@ PRBool operator==(PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE */ -PRBool operator!=(const nsString &aString) const; +PRBool operator!=(const nsString1 &aString) const; PRBool operator!=(const char *aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -677,7 +678,7 @@ PRBool operator!=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<(const nsString &aString) const; +PRBool operator<(const nsString1 &aString) const; PRBool operator<(const char *aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -686,7 +687,7 @@ PRBool operator<(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>(const nsString &S) const; +PRBool operator>(const nsString1 &S) const; PRBool operator>(const char *aCString) const; PRBool operator>(const PRUnichar* aString) const; @@ -695,7 +696,7 @@ PRBool operator>(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<=(const nsString &S) const; +PRBool operator<=(const nsString1 &S) const; PRBool operator<=(const char *aCString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -704,7 +705,7 @@ PRBool operator<=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>=(const nsString &S) const; +PRBool operator>=(const nsString1 &S) const; PRBool operator>=(const char* aCString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -718,7 +719,7 @@ PRBool operator>=(const PRUnichar* aString) const; * @param aLength -- optional length of given string. * @return TRUE if equal */ -PRBool Equals(const nsString& aString) const; +PRBool Equals(const nsString1& aString) const; PRBool Equals(const char* aString,PRInt32 aLength=-1) const; PRBool Equals(const nsIAtom *aAtom) const; @@ -741,7 +742,7 @@ PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const; * @param aLength -- optional length of given string. * @return TRUE if equal */ -PRBool EqualsIgnoreCase(const nsString& aString) const; +PRBool EqualsIgnoreCase(const nsString1& aString) const; PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; @@ -769,34 +770,34 @@ typedef PRUnichar chartype; #endif }; -ostream& operator<<(ostream& os,nsString& aString); -extern NS_BASE int fputs(const nsString& aString, FILE* out); +ostream& operator<<(ostream& os,nsString1& aString); +extern NS_BASE int fputs(const nsString1& aString, FILE* out); //---------------------------------------------------------------------- /** - * A version of nsString which is designed to be used as an automatic + * 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_BASE nsAutoString : public nsString { +class NS_BASE nsAutoString1 : public nsString1 { public: - nsAutoString(); - nsAutoString(const nsString& other); - nsAutoString(const nsAutoString& other); - nsAutoString(PRUnichar aChar); - nsAutoString(const char* aCString); - nsAutoString(const PRUnichar* us, PRInt32 uslen = -1); - virtual ~nsAutoString(); + 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(); - nsAutoString& operator=(const nsString& aString) {nsString::operator=(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;} + 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; @@ -808,7 +809,7 @@ protected: chartype mBuf[32]; }; -ostream& operator<<(ostream& os,nsAutoString& aString); +ostream& operator<<(ostream& os,nsAutoString1& aString); #endif diff --git a/mozilla/xpcom/ds/nsString2.cpp b/mozilla/xpcom/ds/nsString2.cpp index 95afc27ff6b..d1224ddc94f 100644 --- a/mozilla/xpcom/ds/nsString2.cpp +++ b/mozilla/xpcom/ds/nsString2.cpp @@ -19,30 +19,25 @@ #include #include -#include +#include #include #include "nsString2.h" #include "nsDebug.h" #include "nsCRT.h" -#include "nsIAtom.h" -#include "nsISizeOfHandler.h" -#include "prprf.h" -#include "prdtoa.h" +#include "nsIAtom.h" #include "nsDeque.h" -#include "nsUnicharUtilCIID.h" -#include "nsIServiceManager.h" -#include "nsICaseConversion.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 "; -#ifdef RICKG_DEBUG -PRBool nsString2::mSelfTested = PR_FALSE; -#endif -#define NOT_USED 0xfffd +#define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { NOT_USED, @@ -96,61 +91,7 @@ public: } } }; -static CTableConstructor gTableConstructor; -//---- XPCOM code to connect with UnicharUtil - -class HandleCaseConversionShutdown3 : public nsIShutdownListener { -public : - NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); - HandleCaseConversionShutdown3(void) { NS_INIT_REFCNT(); } - virtual ~HandleCaseConversionShutdown3(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(HandleCaseConversionShutdown3, kIShutdownListenerIID); - -nsresult -HandleCaseConversionShutdown3::OnShutdown(const nsCID& cid, nsISupports* service) -{ - if (cid.Equals(kUnicharUtilCID)) { - NS_ASSERTION(service == gCaseConv, "wrong service!"); - gCaseConv->Release(); - gCaseConv = NULL; - } - return NS_OK; -} - -#if 0 -/* not used yet */ -static HandleCaseConversionShutdown3* gListener = NULL; - -static void StartUpCaseConversion() -{ - nsresult err; - - if ( NULL == gListener ) - { - gListener = new HandleCaseConversionShutdown3(); - gListener->AddRef(); - } - err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, - (nsISupports**) &gCaseConv, gListener); -} -static void CheckCaseConversion() -{ - if(NULL == gCaseConv ) - StartUpCaseConversion(); - - // NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); - -} -#endif /*********************************************************************** IMPLEMENTATION NOTES: @@ -159,6 +100,15 @@ static void CheckCaseConversion() that no poor soul would ever have to do this again. Sigh. ***********************************************************************/ +void Subsume(nsStr& aDest,nsStr& aSource){ + 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; +} /** * Default constructor. Note that we actually allocate a small buffer @@ -167,14 +117,10 @@ static void CheckCaseConversion() * performance reasons. */ nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { - nsStr::Initialize(*this,aCharSize); - -#ifdef RICKG_DEBUG - if(!mSelfTested) { - mSelfTested=PR_TRUE; - SelfTest(); - } +#ifdef RICKG_DEBUG + static CStringTester gStringTester; #endif + nsStr::Initialize(*this,aCharSize); } /** @@ -213,10 +159,19 @@ nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* an * @param reference to another nsString2 */ nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { - nsStr::Initialize(*this,(eCharSize)aString.mMultibyte); + nsStr::Initialize(*this,(eCharSize)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 + */ +nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { + Subsume(*this,aSubsumeStr); +} + /** * Destructor * Make sure we call nsStr::Destroy. @@ -227,7 +182,7 @@ nsString2::~nsString2() { void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(sizeof(*this)); - aHandler->Add(mCapacity << mMultibyte); + aHandler->Add(mCapacity << mCharSize); } /** @@ -291,11 +246,10 @@ void nsString2::SetCapacity(PRUint32 aLength) { /** * * @update gess1/4/99 - * @param - * @return + * @return ptr to internal buffer (if 1-byte), otherwise NULL */ -char* nsString2::GetBuffer(void) const { - if(!mMultibyte) +const char* nsString2::GetBuffer(void) const { + if(!mCharSize) return mStr; return 0; } @@ -303,11 +257,10 @@ char* nsString2::GetBuffer(void) const { /** * * @update gess1/4/99 - * @param - * @return + * @return ptr to internal buffer (if 2-byte), otherwise NULL */ -PRUnichar* nsString2::GetUnicode(void) const { - if(mMultibyte) +const PRUnichar* nsString2::GetUnicode(void) const { + if(mCharSize) return (PRUnichar*)mUStr; return 0; } @@ -315,11 +268,11 @@ PRUnichar* nsString2::GetUnicode(void) const { /** * Get nth character. */ -PRUnichar nsString2::operator[](int anIndex) const { +PRUnichar nsString2::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::CharAt(int anIndex) const { +PRUnichar nsString2::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } @@ -334,7 +287,7 @@ PRUnichar nsString2::Last(void) const{ PRBool nsString2::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex(const nsStr &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 nsString2::operator<=(const nsStr &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 nsString2::operator>=(const nsStr &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);} @@ -1416,6 +1421,18 @@ PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) con return Equals(s1,s2,PR_TRUE); } +/** + * 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 + * @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); +} + /** * Compare this to given string; note that we compare full strings here. * @@ -1569,21 +1586,42 @@ PRBool nsString2::IsDigit(PRUnichar aChar) { } +/************************************************************** + Define the string deallocator class... + **************************************************************/ +class nsStringDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + static nsMemoryAgent theAgent; + nsString2* aString= (nsString2*)anObject; + if(aString){ + aString->mAgent=&theAgent; + delete aString; + } + return 0; + } +}; + /**************************************************************************** * This class, appropriately enough, creates and recycles nsString2 objects.. ****************************************************************************/ -#if 0 + class nsStringRecycler { public: nsStringRecycler() : mDeque(0) { } + ~nsStringRecycler() { + nsStringDeallocator theDeallocator; + mDeque.ForEach(theDeallocator); //now delete the strings + } + void Recycle(nsString2* aString) { mDeque.Push(aString); } - nsString2* NewString(eCharSize aCharSize){ + nsString2* CreateString(eCharSize aCharSize){ nsString2* result=(nsString2*)mDeque.Pop(); if(!result) result=new nsString2(aCharSize); @@ -1613,8 +1651,8 @@ nsStringRecycler& GetRecycler(void){ * @param * @return */ -nsString2* nsString2::NewString(eCharSize aCharSize){ - nsString2* result=GetRecycler().NewString(aCharSize); +nsString2* nsString2::CreateString(eCharSize aCharSize){ + nsString2* result=GetRecycler().CreateString(aCharSize); return result; } @@ -1628,7 +1666,6 @@ void nsString2::Recycle(nsString2* aString){ GetRecycler().Recycle(aString); } -#endif /** * @@ -1650,7 +1687,7 @@ void nsString2::DebugDump(ostream& aStream) const { * @return */ ostream& operator<<(ostream& os,nsString2& aString){ - if(PR_FALSE==aString.mMultibyte) { + if(PR_FALSE==aString.mCharSize) { os<>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((eCharSize)aStr.mCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,(eCharSize)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((eCharSize)aCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,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(char* aCString,PRUint32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { + mAgent=0; + if(assumeOwnership) { + nsStr::Initialize(*this,aCString,aCapacity,0,eOneByte,PR_TRUE); + } + 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) { + mAgent=0; + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aString); +} + +/** + * Copy construct from uni-string + * @param aString is a ptr to a unistr + */ +nsAutoString2::nsAutoString2(PRUnichar* aString,PRUint32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { + mAgent=0; + if(assumeOwnership) { + nsStr::Initialize(*this,(char*)aString,aCapacity,0,eTwoByte,PR_TRUE); + } + else { + 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 nsStr& aString,eCharSize aCharSize) : nsString2(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 nsAutoString2& aString,eCharSize aCharSize) : nsString2(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); +} + + +/** + * Copy construct from an nsString2 + * @param + */ +nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ + mAgent=0; + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aChar); +} + +/** + * construct from a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString + */ +nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2((eCharSize)aSubsumeStr.mCharSize) { + mAgent=0; + Subsume(*this,aSubsumeStr); +} + +/** + * deconstruct the autstring + * @param + */ +nsAutoString2::~nsAutoString2(){ +// bool b=true; +// mStr=0; +} + +void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity << mCharSize); +} + +nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2((eCharSize)aString.mCharSize) { + Subsume(*this,aString); +} + +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharSize) { + Subsume(*this,aString); +} + +nsSubsumeStr::nsSubsumeStr(const PRUnichar* aString) : nsString2(aString,eTwoByte) { +} + +nsSubsumeStr::nsSubsumeStr(const char* aString) : nsString2(aString,eOneByte) { +} + +#ifdef RICKG_DEBUG +/*********************************************************************** + IMPLEMENTATION of CStringTester... + ***********************************************************************/ +CStringTester::CStringTester() { -#ifdef RICKG_DEBUG static const char* kConstructorError = kConstructorError; static const char* kComparisonError = "Comparision error!"; static const char* kEqualsError = "Equals error!"; - mSelfTested=PR_TRUE; - eCharSize theSize=eOneByte; + eCharSize theSize=eTwoByte; //begin by testing the constructors... { { nsString2 theString0("foo",theSize); //watch it construct and destruct - } + + { + nsString2 theString("hello"); + nsString2 temp1=theString+" there!"; + nsString2 temp2=theString+'!'; + nsSubsumeStr temp3=theString+'?'; + nsString2 temp4(temp3); + temp1=temp3; + nsSubsumeStr temp5("hello"); + } + nsString2 theString1(theSize); nsString2 theString("hello",theSize); nsString2 theString3(theString,theSize); @@ -1725,6 +1934,8 @@ void nsString2::SelfTest(void) { //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; @@ -1740,12 +1951,17 @@ void nsString2::SelfTest(void) { 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... + + //try a few numeric conversion routines... { nsString2 str1("10000",theSize); PRInt32 err; @@ -1757,11 +1973,12 @@ void nsString2::SelfTest(void) { { nsString2 theString("hello",theSize); PRUint32 len=theString.Length(); + PRUnichar ch; for(PRUint32 i=0;i>aDest.mMultibyte)-1; - aDest.mOwnsBuffer=aBufDescriptor.mOwnsBuffer; - AddNullTerminator(aDest); -} - -/** - * 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 - */ -nsAutoString2::nsAutoString2(eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); -} - -/** - * 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(nsBufDescriptor& aBufDescriptor,const char* aCString) : nsString2(aBufDescriptor.mMultibyte) { - InitAutoStr(*this,aBufDescriptor); - 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) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aCString); -} - -/** - * Copy construct from ascii c-string - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(char* aCString,PRUint32 aLen,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - if(assumeOwnership) { - nsBufDescriptor theDescriptor(aCString,aLen,eOneByte,PR_TRUE); - InitAutoStr(*this,theDescriptor); + { + //now let's try a memory allocation test... + nsString2 temp; + for(int i=0;i<100;i++){ + temp+="hello "; + } + int x=5; } - else { - nsBufDescriptor theDescriptor(mBuffer,aLen,aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - } - Assign(aCString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - */ -nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - */ -nsAutoString2::nsAutoString2(PRUnichar* aString,PRUint32 aLength,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - if(assumeOwnership) { - nsBufDescriptor theDescriptor((char*)aString,aLength,eTwoByte,PR_TRUE); - InitAutoStr(*this,theDescriptor); - } - else { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - } - Assign(aString); -} - - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsStr& aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsString2& aString,eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aChar); -} - -/** - * deconstruct the autstring - * @param - */ -nsAutoString2::~nsAutoString2(){ -// bool b=true; -// mStr=0; -} - -void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { - aHandler->Add(sizeof(*this)); - aHandler->Add(mCapacity << mMultibyte); -} +}; +#endif //rickg_debug diff --git a/mozilla/xpcom/ds/nsString2.h b/mozilla/xpcom/ds/nsString2.h index d5e07d9f994..ec7b2be85fe 100644 --- a/mozilla/xpcom/ds/nsString2.h +++ b/mozilla/xpcom/ds/nsString2.h @@ -20,18 +20,19 @@ /*********************************************************************** MODULE NOTES: - This class provides a 1-byte ASCII string implementation that shares - a common API with all other strImpl derivatives. + 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 _nsString2 #define _nsString2 -//#define nsString2 nsString -//#define nsAutoString2 nsAutoString - - #include "prtypes.h" #include "nscore.h" #include @@ -46,6 +47,13 @@ class nsISizeOfHandler; +#ifdef USE_STRING2 +#define nsString2 nsString +#define nsAutoString2 nsAutoString +#endif + +class NS_BASE nsSubsumeStr; + class NS_BASE nsString2 : public nsStr { public: @@ -83,6 +91,12 @@ nsString2(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAg */ nsString2(const nsString2& aString); +/** + * This constructor takes a subsumestr + * @param reference to subsumestr + */ +nsString2(nsSubsumeStr& aSubsumeStr); + /** * Destructor * @@ -139,16 +153,15 @@ PRBool IsOrdered(void) const; Accessor methods... *********************************************************************/ -char* GetBuffer(void) const; -PRUnichar* GetUnicode(void) const; -operator PRUnichar*() const {return GetUnicode();} +const char* GetBuffer(void) const; +const PRUnichar* GetUnicode(void) const; /** * Get nth character. */ -PRUnichar operator[](int anIndex) const; -PRUnichar CharAt(int anIndex) const; +PRUnichar operator[](PRUint32 anIndex) const; +PRUnichar CharAt(PRUint32 anIndex) const; PRUnichar First(void) const; PRUnichar Last(void) const; @@ -164,42 +177,42 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString2 operator+(const nsStr& aString); +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 */ -nsString2 operator+(const nsString2& aString); +nsSubsumeStr operator+(const nsString2& 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 */ -nsString2 operator+(const char* aCString); +nsSubsumeStr 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 */ -nsString2 operator+(const PRUnichar* aString); +nsSubsumeStr 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 */ -nsString2 operator+(char aChar); +nsSubsumeStr 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 */ -nsString2 operator+(PRUnichar aChar); +nsSubsumeStr operator+(PRUnichar aChar); /********************************************************************** Lexomorphic transforms... @@ -353,7 +366,6 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=10) const; 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);} -void Copy(nsString2& aString) const; /** * assign given string to this string @@ -380,6 +392,7 @@ 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);} +nsString2& operator=(nsSubsumeStr& aSubsumeString); /** * Here's a bunch of append mehtods for varying types... @@ -402,8 +415,7 @@ nsString2& operator+=(PRUnichar aChar){return Append(aChar);} */ nsString2& Append(const nsStr& aString) {return Append(aString,aString.mLength);} nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLength);} -nsString2& Append(const char* aString) {if(aString) {Append(aString,nsCRT::strlen(aString));} return *this;} -nsString2& Append(const PRUnichar* aString) {if(aString) {Append(aString,nsCRT::strlen(aString));} return *this;} + /* * Appends n characters from given string to this, @@ -414,13 +426,13 @@ nsString2& Append(const PRUnichar* aString) {if(aString) {Append(aString,nsCRT:: */ nsString2& Append(const nsStr& aString,PRInt32 aCount); nsString2& Append(const nsString2& aString,PRInt32 aCount); -nsString2& Append(const char* aString,PRInt32 aCount); -nsString2& Append(const PRUnichar* 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); - + /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -464,7 +476,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(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -519,10 +531,11 @@ 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(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) const; +PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; /** * This method searches this string for the first character @@ -554,9 +567,10 @@ PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; * @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(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) const; +PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; /********************************************************************** Comparison methods... @@ -569,6 +583,7 @@ PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) 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 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; @@ -578,6 +593,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 nsStr &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; @@ -587,6 +603,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 nsStr &aString) const; PRBool operator!=(const char* aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -596,6 +613,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 nsStr &aString) const; PRBool operator<(const char* aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -605,6 +623,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 nsStr &S) const; PRBool operator>(const char* aString) const; PRBool operator>(const PRUnichar* aString) const; @@ -614,6 +633,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 nsStr &S) const; PRBool operator<=(const char* aString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -623,6 +643,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 nsStr &S) const; PRBool operator>=(const char* aString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -637,6 +658,7 @@ PRBool operator>=(const PRUnichar* aString) const; * @param aLength -- optional length of given string. * @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; @@ -675,18 +697,11 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -#if 0 static void Recycle(nsString2* aString); -static nsString2* NewString(eCharSize aCharSize=eTwoByte); -#endif +static nsString2* CreateString(eCharSize aCharSize=eTwoByte); -static void SelfTest(); virtual void DebugDump(ostream& aStream) const; -#ifdef RICKG_DEBUG - static PRBool mSelfTested; -#endif - nsIMemoryAgent* mAgent; }; @@ -701,12 +716,11 @@ ostream& operator<<(ostream& os,nsString2& aString); If the buffer needs to grow, it gets reallocated on the heap. **************************************************************/ - class NS_BASE nsAutoString2 : public nsString2 { public: nsAutoString2(eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(nsBufDescriptor& anExtBuffer,const char* aCString); + nsAutoString2(nsStr& anExtBuffer,const char* aCString); nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); @@ -717,6 +731,7 @@ public: nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString2(nsSubsumeStr& aSubsumeStr); nsAutoString2(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); virtual ~nsAutoString2(); @@ -738,5 +753,37 @@ public: }; +/*************************************************************** + 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_BASE nsSubsumeStr : public nsString2 { +public: + nsSubsumeStr(nsString2& aString); + nsSubsumeStr(nsStr& aString); + nsSubsumeStr(const PRUnichar* aString); + nsSubsumeStr(const char* aString); +}; + +#ifdef RICKG_DEBUG +/******************************************************** + This class's only purpose in life is to test nsString2. + ********************************************************/ +class CStringTester { +public: + CStringTester(); +}; #endif +#endif + + diff --git a/mozilla/xpcom/string/obsolete/nsStr.cpp b/mozilla/xpcom/string/obsolete/nsStr.cpp index 79e7afc9910..4d3fc6ab211 100644 --- a/mozilla/xpcom/string/obsolete/nsStr.cpp +++ b/mozilla/xpcom/string/obsolete/nsStr.cpp @@ -31,12 +31,104 @@ #include "nsStr.h" #include "bufferRoutines.h" #include "stdio.h" //only used for printf +#include "nsDeque.h" +#include "nsCRT.h" + static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; - //---------------------------------------------------------------------------------------- +// The following is a memory agent who knows how to recycled (pool) freed memory... +//---------------------------------------------------------------------------------------- + +/************************************************************** + Define the char* (pooled) deallocator class... + **************************************************************/ +class nsBufferDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + char* aCString= (char*)anObject; + delete [] aCString; + return 0; + } +}; +/** + * + * @update gess10/30/98 + * @param + * @return + */ +class nsPoolingMemoryAgent : public nsMemoryAgent{ +public: + nsPoolingMemoryAgent() { + memset(mPools,0,sizeof(mPools)); + } + + ~nsPoolingMemoryAgent() { + nsBufferDeallocator theDeallocator; + int i=0; + for(i=0;i<10;i++){ + if(mPools[i]){ + mPools[i]->ForEach(theDeallocator); //now delete the buffers + } + delete mPools[i]; + mPools[i]=0; + } + } + + virtual PRBool Alloc(nsStr& aDest,PRInt32 aCount) { + + //we're given the acount value in charunits; we have to scale up by the charsize. + int theShift=4; + PRInt32 theNewCapacity=eDefaultSize; + while(theNewCapacityPop(); + } + if(!aDest.mStr) { + //we're given the acount value in charunits; we have to scale up by the charsize. + size_t theSize=(theNewCapacity<Push(aDest.mStr); + } + else delete [] aDest.mStr; //it's too big. Just delete it. + } + aDest.mStr=0; + aDest.mOwnsBuffer=0; + return PR_TRUE; + } + return PR_FALSE; + } + nsDeque* mPools[16]; +}; + +static char* gCommonEmptyBuffer=0; /** * * @update gess10/30/98 @@ -44,9 +136,8 @@ static const char* kFoolMsg = "Error: Some fool overwrote the shared buffer."; * @return */ char* GetSharedEmptyBuffer() { - static char* gCommonEmptyBuffer=0; if(!gCommonEmptyBuffer) { - const size_t theDfltSize=25; + const size_t theDfltSize=5; gCommonEmptyBuffer=new char[theDfltSize]; if(gCommonEmptyBuffer){ nsCRT::zero(gCommonEmptyBuffer,theDfltSize); @@ -69,9 +160,24 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { aDest.mStr=GetSharedEmptyBuffer(); aDest.mLength=0; aDest.mCapacity=0; - aDest.mMultibyte=aCharSize; + aDest.mCharSize=aCharSize; aDest.mOwnsBuffer=0; - NS_ASSERTION(aDest.mStr[0]==0,kFoolMsg); + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); +} + +/** + * + * @update gess10/30/98 + * @param + * @return + */ +void nsStr::Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer){ + aDest.mStr=(aCString) ? aCString : GetSharedEmptyBuffer(); + aDest.mLength=aLength; + aDest.mCapacity=aCapacity; + aDest.mCharSize=aCharSize; + aDest.mOwnsBuffer=aOwnsBuffer; + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -81,13 +187,9 @@ void nsStr::Initialize(nsStr& aDest,eCharSize aCharSize) { * @return */ nsIMemoryAgent* GetDefaultAgent(void){ - static nsIMemoryAgent* gDefaultAgent=0; - if(!gDefaultAgent) - gDefaultAgent=new nsMemoryAgent(); - - NS_ASSERTION(gDefaultAgent,"You MUST always have an allocator!"); - - return gDefaultAgent; +// static nsPoolingMemoryAgent gDefaultAgent; + static nsMemoryAgent gDefaultAgent; + return (nsIMemoryAgent*)&gDefaultAgent; } /** @@ -110,20 +212,6 @@ void nsStr::Destroy(nsStr& aDest,nsIMemoryAgent* anAgent) { } } -/** - * - * @update gess11/12/98 - * @param - * @return - */ -PRUnichar nsStr::GetCharAt(const nsStr& aDest,PRUint32 anIndex) { - PRUnichar result=0; - if((anIndex>=0) && (anIndexRealloc(aString,aNewLength); AddNullTerminator(aString); } + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -150,13 +239,13 @@ void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* an void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { if(aNewLength>aDest.mCapacity) { nsStr theTempStr; - nsStr::Initialize(theTempStr,(eCharSize)aDest.mMultibyte); + nsStr::Initialize(theTempStr,(eCharSize)aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); EnsureCapacity(theTempStr,aNewLength,theAgent); - if(0Free(aDest); aDest.mStr = theTempStr.mStr; @@ -165,6 +254,7 @@ void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgen aDest.mCapacity=theTempStr.mCapacity; aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; } + NS_ASSERTION(gCommonEmptyBuffer[0]==0,kFoolMsg); } /** @@ -182,13 +272,12 @@ void nsStr::Assign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 a /** * This method appends the given nsStr to this one. Note that we have to * pay attention to the underlying char-size of both structs. - * @update gess 04/04/99 + * @update gess10/30/98 * @param aDest is the nsStr to be manipulated * @param aSource is where char are copied from * @aCount is the number of bytes to be copied */ void nsStr::Append(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount,nsIMemoryAgent* anAgent){ - if(anOffset0) && (aTarget.mLength>0)){ PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=nsStr::GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=nsStr::GetCharAt(aTarget,aTarget.mLength-1); + PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); + PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); PRInt32 theTargetMax=aTarget.mLength; while(++index<=theMax) { PRInt32 theSubIndex=-1; PRBool matches=PR_TRUE; while((++theSubIndex0) { if(theFirstTargetChar==theChar){ - PRUnichar theDestJumpChar=nsStr::GetCharAt(aDest,index+theTargetMax); + PRUnichar theDestJumpChar=GetCharAt(aDest,index+theTargetMax); if(theDestJumpChar==theLastTargetChar) { theNewStartPos=index; //this lets us jump ahead during our search where possible. }//if }//if }//if - PRUnichar theTargetChar=nsStr::GetCharAt(aTarget,theSubIndex); + PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex); matches=PRBool(theChar==theTargetChar); } if(matches) @@ -384,8 +478,8 @@ PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIgn * @param * @return */ -PRInt32 nsStr::FindChar(const nsStr& aDest, PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 result=gFindChars[aDest.mMultibyte](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); +PRInt32 nsStr::FindChar(const nsStr& aDest,const PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { + PRInt32 result=gFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); return result; } @@ -403,7 +497,7 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore while(++index0) && (aTarget.mLength>0)){ PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=nsStr::GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=nsStr::GetCharAt(aTarget,aTarget.mLength-1); + PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); + PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); PRInt32 theTargetMax=aTarget.mLength; while(index--) { @@ -429,16 +523,16 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIg if(anOffset+aTarget.mLength<=aDest.mLength) { while((++theSubIndex0) { if(theFirstTargetChar==theChar){ - PRUnichar theDestJumpChar=nsStr::GetCharAt(aDest,index+theTargetMax); + PRUnichar theDestJumpChar=GetCharAt(aDest,index+theTargetMax); if(theDestJumpChar==theLastTargetChar) { theNewStartPos=index; //this lets us jump ahead during our search where possible. }//if }//if }//if - PRUnichar theTargetChar=nsStr::GetCharAt(aTarget,theSubIndex); + PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex); matches=PRBool(theChar==theTargetChar); } //while } //if @@ -459,8 +553,8 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool /*aIg * @param * @return */ -PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 result=gRFindChars[aDest.mMultibyte](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); +PRInt32 nsStr::RFindChar(const nsStr& aDest,const PRUnichar aChar, PRBool aIgnoreCase,PRUint32 anOffset) { + PRInt32 result=gRFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase); return result; } @@ -472,12 +566,12 @@ PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase, * @return */ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnoreCase,PRUint32 anOffset) { - PRInt32 offset=aDest.mLength-anOffset; + PRUint32 offset=aDest.mLength-anOffset; PRInt32 thePos; while(--offset>=0) { - PRUnichar theChar=GetCharAt(aDest,PRUint32(offset)); - thePos=gRFindChars[aSet.mMultibyte](aSet.mStr,aSet.mLength,0,theChar,aIgnoreCase); + PRUnichar theChar=GetCharAt(aDest,offset); + thePos=gRFindChars[aSet.mCharSize](aSet.mStr,aSet.mLength,anOffset,theChar,aIgnoreCase); if(kNotFound!=thePos) return offset; } //while @@ -491,7 +585,7 @@ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnor * @param * @return aDestaSource=1 */ -PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 /*aCount*/,PRBool aIgnoreCase) { +PRInt32 nsStr::Compare(const nsStr& aDest,const nsStr& aSource,PRInt32 aCount,PRBool aIgnoreCase) { int minlen=(aSource.mLength>aCharSize)-1), - mOwnsBuffer(aOwnsBuffer), - mMultibyte(aCharSize) -{ - mStr=aBuffer; - } - - PRUint32 mCapacity; - PRBool mOwnsBuffer; - eCharSize mMultibyte; -// UStrPtr mStr; - union { - char* mStr; - PRUnichar* mUStr; - }; -}; +const PRInt32 kNotFound = -1; class nsIMemoryAgent; @@ -99,6 +67,15 @@ struct nsStr { */ static void Initialize(nsStr& aDest,eCharSize aCharSize); + /** + * This method initializes an nsStr for use + * + * @update gess 01/04/99 + * @param aString is the nsStr to be initialized + * @param aCharSize tells us the requested char size (1 or 2 bytes) + */ + static void Initialize(nsStr& aDest,char* aCString,PRUint32 aCapacity,PRUint32 aLength,eCharSize aCharSize,PRBool aOwnsBuffer); + /** * This method destroys the given nsStr, and *MAY* * deallocate it's memory depending on the setting @@ -168,7 +145,7 @@ struct nsStr { * @param aCount tells us the (max) # of chars to delete * @param anAgent is the allocator to be used for alloc/free operations */ - static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRInt32 aCount,nsIMemoryAgent* anAgent=0); + static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount,nsIMemoryAgent* anAgent=0); /** * This method is used to truncate the given string. @@ -256,21 +233,12 @@ struct nsStr { 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); - /** - * This method is used to access a given char in the given string - * - * @update gess 01/04/99 - * @param aDest is the nsStr to be appended to - * @param anIndex tells us where in dest to get the char from - * @return the given char, or 0 if anIndex is out of range - */ - static PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex); - PRUint32 mLength : 30; - eCharSize mMultibyte : 2; - PRUint32 mCapacity: 30; - PRUint32 mOwnsBuffer: 1; - PRUint32 mUnused: 1; + PRUint32 mLength; + PRUint32 mCapacity; + eCharSize mCharSize; + PRBool mOwnsBuffer; + union { char* mStr; PRUnichar* mUStr; @@ -289,26 +257,27 @@ inline PRInt32 MaxInt(PRInt32 anInt1,PRInt32 anInt2){ return (anInt1aMax) - aValue=aMax; -} - -inline void ToRange(PRUint32& aValue,PRUint32 aMin,PRUint32 aMax){ - if(aValueaMax) - aValue=aMax; -} - inline void AddNullTerminator(nsStr& aDest) { - if(eTwoByte==aDest.mMultibyte) + if(eTwoByte==aDest.mCharSize) aDest.mUStr[aDest.mLength]=0; else aDest.mStr[aDest.mLength]=0; } +/** +* This method is used to access a given char in the given string +* +* @update gess 01/04/99 +* @param aDest is the nsStr to be appended to +* @param anIndex tells us where in dest to get the char from +* @return the given char, or 0 if anIndex is out of range +*/ +inline PRUnichar GetCharAt(const nsStr& aDest,PRUint32 anIndex){ + if(anIndex 64) { - // When the string starts getting large, double the capacity as we grow. - theNewCapacity = aDest.mCapacity * 2; - if (theNewCapacity < aCount) { - theNewCapacity = aDest.mCapacity + aCount; - } - } else { - // When the string is small, keep it's capacity a multiple of kGrowthDelta - PRInt32 unitDelta=(aCount/eGrowthDelta)+1; - theNewCapacity=unitDelta*eGrowthDelta; + //we're given the acount value in charunits; now scale up to next multiple. + PRUint32 theNewCapacity=eDefaultSize; + while(theNewCapacitySetString(aUnicodeStr,len); } + } @@ -233,7 +237,7 @@ nsString::nsString(const PRUnichar* aUnicodeStr){ * Destructor * @update gess7/30/98 */ -nsString::~nsString() +nsString1::~nsString1() { if(mStr && (mStr!=kCommonEmptyBuffer)) delete [] mStr; @@ -249,14 +253,14 @@ nsString::~nsString() * @param anIndex -- new length of string * @return nada */ -void nsString::Truncate(PRInt32 anIndex) { +void nsString1::Truncate(PRInt32 anIndex) { if((anIndex>-1) && (anIndexAdd(sizeof(*this)); aHandler->Add(mCapacity * sizeof(chartype)); @@ -269,7 +273,7 @@ void nsString::SizeOf(nsISizeOfHandler* aHandler) const * @update gess 8/25/98 * @return TRUE if ordered. */ -PRBool nsString::IsOrdered(void) const { +PRBool nsString1::IsOrdered(void) const { PRBool result=PR_TRUE; if(mLength>1) { PRInt32 theIndex; @@ -290,7 +294,7 @@ PRBool nsString::IsOrdered(void) const { * @param aNewLength -- new capacity of string * @return void */ -void nsString::EnsureCapacityFor(PRInt32 aNewLength) +void nsString1::EnsureCapacityFor(PRInt32 aNewLength) { PRInt32 newCapacity; @@ -328,12 +332,12 @@ void nsString::EnsureCapacityFor(PRInt32 aNewLength) * @param aLength -- contains new length for mStr * @return */ -void nsString::SetLength(PRInt32 aLength) { +void nsString1::SetLength(PRInt32 aLength) { if(aLength>mLength) { EnsureCapacityFor(aLength); nsCRT::zero(mStr + mLength, (aLength - mLength) * sizeof(chartype)); } - if((aLength>0) && (aLength=0) && (aLength aBufLength) ? aBufLength : mLength; @@ -712,7 +723,7 @@ char* nsString::ToCString(char* aBuf, PRInt32 aBufLength) const * @param aErrorCode will contain error if one occurs * @return float rep of string value */ -float nsString::ToFloat(PRInt32* aErrorCode) const +float nsString1::ToFloat(PRInt32* aErrorCode) const { char buf[40]; if (mLength > PRInt32(sizeof(buf)-1)) { @@ -735,7 +746,7 @@ float nsString::ToFloat(PRInt32* aErrorCode) const * @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 */ -PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { +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; @@ -803,7 +814,7 @@ PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { * @param PRUnichar: buffer to be assigned to this * @return this */ -nsString& nsString::SetString(const PRUnichar* aStr,PRInt32 aLength) { +nsString1& nsString1::SetString(const PRUnichar* aStr,PRInt32 aLength) { if((0 == aLength) || (nsnull == aStr)) { mLength=0; if (nsnull != mStr) { @@ -829,7 +840,7 @@ nsString& nsString::SetString(const PRUnichar* aStr,PRInt32 aLength) { * @param aCString: buffer to be assigned to this * @return this */ -nsString& nsString::SetString(const char* aCString,PRInt32 aLength) { +nsString1& nsString1::SetString(const char* aCString,PRInt32 aLength) { if(aCString!=0) { PRInt32 len=(aLength<0) ? nsCRT::strlen(aCString) : aLength; if(mCapacity<=len) @@ -847,6 +858,29 @@ nsString& nsString::SetString(const char* aCString,PRInt32 aLength) { mLength=0; //This little bit of code handles the case mStr[0]=0; //where some blockhead hands us a null string } + + return *this; +} + +/** + * 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 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); } @@ -866,8 +900,8 @@ nsString& nsString::operator=(const PRUnichar* aStr) { * @param aString: string to be added to this * @return this */ -nsString& nsString::operator=(const nsString& aString) { - return this->SetString(aString.mStr,aString.mLength); +nsString1& nsString1::operator=(const nsString1& aString) { + return this->SetString(aString); } @@ -877,7 +911,7 @@ nsString& nsString::operator=(const nsString& aString) { * @param aCString: buffer to be assigned to this * @return this */ -nsString& nsString::operator=(const char* aCString) { +nsString1& nsString1::operator=(const char* aCString) { return SetString(aCString); } @@ -888,7 +922,7 @@ nsString& nsString::operator=(const char* aCString) { * @param aChar: char to be assignd to this * @return this */ -nsString& nsString::operator=(char aChar) { +nsString1& nsString1::operator=(char aChar) { return this->operator=(PRUnichar(aChar)); } @@ -898,7 +932,7 @@ nsString& nsString::operator=(char aChar) { * @param aChar: char to be assignd to this * @return this */ -nsString& nsString::operator=(PRUnichar aChar) { +nsString1& nsString1::operator=(PRUnichar aChar) { if(mCapacity<1) EnsureCapacityFor(kGrowthDelta); mStr[0]=aChar; @@ -913,7 +947,7 @@ nsString& nsString::operator=(PRUnichar aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const nsString& aString,PRInt32 aLength) { +nsString1& nsString1::Append(const nsString1& aString,PRInt32 aLength) { return Append(aString.mStr,aString.mLength); } @@ -923,7 +957,8 @@ nsString& nsString::Append(const nsString& aString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const char* aCString,PRInt32 aLength) { +nsString1& nsString1::Append(const char* aCString,PRInt32 aLength) { + if(aCString!=0) { PRInt32 len=(aLength<0) ? strlen(aCString) : aLength; if(mLength+len >= mCapacity) { @@ -947,7 +982,7 @@ nsString& nsString::Append(const char* aCString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(char aChar) { +nsString1& nsString1::Append(char aChar) { return Append(PRUnichar(aChar)); } @@ -957,7 +992,8 @@ nsString& nsString::Append(char aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(const PRUnichar* aString,PRInt32 aLength) { +nsString1& nsString1::Append(const PRUnichar* aString,PRInt32 aLength) { + if(aString!=0) { PRInt32 len=(aLength<0) ? nsCRT::strlen(aString) : aLength; if(mLength+len >= mCapacity) { @@ -978,7 +1014,7 @@ nsString& nsString::Append(const PRUnichar* aString,PRInt32 aLength) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::Append(PRUnichar aChar) { +nsString1& nsString1::Append(PRUnichar aChar) { if(mLength < mCapacity) { mStr[mLength++]=aChar; // the new string len < capacity, so just copy mStr[mLength]=0; @@ -998,7 +1034,7 @@ nsString& nsString::Append(PRUnichar aChar) { * @param aString : string to be appended to this * @return this */ -nsString& nsString::operator+=(const nsString &aString) { +nsString1& nsString1::operator+=(const nsString1 &aString) { return this->Append(aString.mStr,aString.mLength); } @@ -1009,7 +1045,7 @@ nsString& nsString::operator+=(const nsString &aString) { * @param aCString: buffer to be appended to this * @return this */ -nsString& nsString::operator+=(const char* aCString) { +nsString1& nsString1::operator+=(const char* aCString) { return Append(aCString); } @@ -1020,7 +1056,7 @@ nsString& nsString::operator+=(const char* aCString) { * @param aBuffer: buffer to be appended to this * @return this */ -nsString& nsString::operator+=(const PRUnichar* aBuffer) { +nsString1& nsString1::operator+=(const PRUnichar* aBuffer) { return Append(aBuffer); } @@ -1031,7 +1067,7 @@ nsString& nsString::operator+=(const PRUnichar* aBuffer) { * @param aChar: char to be appended to this * @return this */ -nsString& nsString::operator+=(PRUnichar aChar) { +nsString1& nsString1::operator+=(PRUnichar aChar) { return Append(aChar); } @@ -1041,7 +1077,7 @@ nsString& nsString::operator+=(PRUnichar aChar) { * @param * @return */ -nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { +nsString1& nsString1::Append(PRInt32 aInteger,PRInt32 aRadix) { char* fmt = "%d"; if (8 == aRadix) { fmt = "%o"; @@ -1061,7 +1097,7 @@ nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { * @param * @return */ -nsString& nsString::Append(float aFloat){ +nsString1& nsString1::Append(float aFloat){ char buf[40]; PR_snprintf(buf, sizeof(buf), "%g", aFloat); Append(buf); @@ -1080,7 +1116,7 @@ nsString& nsString::Append(float aFloat){ * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 nsString::Left(nsString& aCopy,PRInt32 aCount) const { +PRInt32 nsString1::Left(nsString1& aCopy,PRInt32 aCount) const { return Mid(aCopy,0,aCount); } @@ -1095,7 +1131,7 @@ PRInt32 nsString::Left(nsString& aCopy,PRInt32 aCount) const { * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 nsString::Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const { +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) { @@ -1191,7 +1228,7 @@ PRInt32 nsString::Insert(const nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) * @param anOffset is insert pos in str * @return the number of chars inserted into this string */ -PRInt32 nsString::Insert(PRUnichar aChar,PRInt32 anOffset){ +PRInt32 nsString1::Insert(PRUnichar aChar,PRInt32 anOffset){ //1st optimization: If you're inserting at end, then simply append! if(anOffset= 'A') && (aChar <= 'Z')) || ((aChar >= 'a') && (aChar <= 'z'))) { return PR_TRUE; @@ -1292,7 +1330,7 @@ PRBool nsString::IsAlpha(PRUnichar aChar) { * @param aChar is character to be tested * @return TRUE if is valid space char */ -PRBool nsString::IsSpace(PRUnichar aChar) { +PRBool nsString1::IsSpace(PRUnichar aChar) { // XXX i18n if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) { return PR_TRUE; @@ -1306,7 +1344,7 @@ PRBool nsString::IsSpace(PRUnichar aChar) { * @update gess 3/31/98 * @param aChar is character to be tested * @return TRUE if char is a valid digit - */PRBool nsString::IsDigit(PRUnichar aChar) { + */PRBool nsString1::IsDigit(PRUnichar aChar) { // XXX i18n return PRBool((aChar >= '0') && (aChar <= '9')); } @@ -1321,10 +1359,11 @@ PRBool nsString::IsSpace(PRUnichar aChar) { * both ends * @return this */ -nsString& nsString::Trim(const char* aTrimSet, +nsString1& nsString1::Trim(const char* aTrimSet, PRBool aEliminateLeading, PRBool aEliminateTrailing) { + PRUnichar* from = mStr; PRUnichar* end = mStr + mLength-1; PRUnichar* to = mStr; @@ -1377,10 +1416,9 @@ nsString& nsString::Trim(const char* aTrimSet, * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString& nsString::CompressWhitespace( PRBool aEliminateLeading, +nsString1& nsString1::CompressWhitespace( PRBool aEliminateLeading, PRBool aEliminateTrailing) { - Trim(" \r\n\t",aEliminateLeading,aEliminateTrailing); PRUnichar* from = mStr; @@ -1416,7 +1454,7 @@ nsString& nsString::CompressWhitespace( PRBool aEliminateLeading, * @update gess 7/27/98 * @return this */ -nsString& nsString::StripWhitespace() +nsString1& nsString1::StripWhitespace() { Trim(" \r\n\t"); return StripChars("\r\t\n"); @@ -1429,7 +1467,8 @@ nsString& nsString::StripWhitespace() * @param * @return *this */ -nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { +nsString1& nsString1::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { + PRUnichar* from = mStr; PRUnichar* end = mStr + mLength; @@ -1440,8 +1479,7 @@ nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { } from++; } - - return *this; +return *this; } /** @@ -1453,7 +1491,7 @@ nsString& nsString::ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar) { * @param aChar is the unicode char to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::BinarySearch(PRUnichar aChar) const { +PRInt32 nsString1::BinarySearch(PRUnichar aChar) const { PRInt32 low=0; PRInt32 high=mLength-1; @@ -1476,7 +1514,7 @@ PRInt32 nsString::BinarySearch(PRUnichar aChar) const { * @param aCStringBuf - charstr to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const char* aCStringBuf) const{ +PRInt32 nsString1::Find(const char* aCStringBuf) const{ NS_ASSERTION(0!=aCStringBuf,kNullPointerError); PRInt32 result=kNotFound; if(aCStringBuf) { @@ -1498,7 +1536,7 @@ PRInt32 nsString::Find(const char* aCStringBuf) const{ * @param aString - PUnichar* to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const PRUnichar* aString) const{ +PRInt32 nsString1::Find(const PRUnichar* aString) const{ NS_ASSERTION(0!=aString,kNullPointerError); PRInt32 result=kNotFound; if(aString) { @@ -1518,10 +1556,10 @@ PRInt32 nsString::Find(const PRUnichar* aString) const{ * Search for given buffer within this string * * @update gess 3/25/98 - * @param nsString -- buffer to be found + * @param nsString1 -- buffer to be found * @return offset in string, or -1 (kNotFound) */ -PRInt32 nsString::Find(const nsString& aString) const{ +PRInt32 nsString1::Find(const nsString1& aString) const{ PRInt32 result=kNotFound; PRInt32 len=aString.mLength; @@ -1545,7 +1583,7 @@ PRInt32 nsString::Find(const nsString& aString) const{ * @param * @return offset of found char, or -1 (kNotFound) */ -PRInt32 nsString::Find(PRUnichar aChar, PRInt32 anOffset) const{ +PRInt32 nsString1::Find(PRUnichar aChar, PRInt32 anOffset) const{ for(PRInt32 i=anOffset;i0;i--){ @@ -1617,7 +1655,7 @@ PRInt32 nsString::RFindCharInSet(const char* anAsciiSet,PRInt32 anOffset) const{ * @param * @return */ -PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{ +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]); @@ -1635,7 +1673,7 @@ PRInt32 nsString::RFindCharInSet(nsString& aSet,PRInt32 anOffset) const{ * @param * @return */ -PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=aString,kNullPointerError); if(aString) { @@ -1661,7 +1699,7 @@ PRInt32 nsString::RFind(const PRUnichar* aString,PRBool aIgnoreCase) const{ * @param * @return */ -PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase) const{ +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--) { @@ -1683,7 +1721,7 @@ PRInt32 nsString::RFind(const nsString& aString,PRBool aIgnoreCase) const{ * @param * @return */ -PRInt32 nsString::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ NS_ASSERTION(0!=anAsciiSet,kNullPointerError); if(anAsciiSet) { @@ -1711,7 +1749,7 @@ PRInt32 nsString::RFind(const char* anAsciiSet,PRBool aIgnoreCase) const{ * @param * @return offset of char in string, or -1 (kNotFound) */ -PRInt32 nsString::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ +PRInt32 nsString1::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ chartype uc=nsCRT::ToUpper(aChar); for(PRInt32 offset=mLength-1;offset>=0;offset--) if(aIgnoreCase) { @@ -1735,7 +1773,7 @@ PRInt32 nsString::RFind(PRUnichar aChar,PRBool aIgnoreCase) const{ * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { +PRInt32 nsString1::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLength) const { NS_ASSERTION(0!=aCString,kNullPointerError); if(-1!=aLength) { @@ -1769,7 +1807,7 @@ PRInt32 nsString::Compare(const char *aCString,PRBool aIgnoreCase,PRInt32 aLengt * @param * @return */ -PRInt32 nsString::Compare(const nsString &S,PRBool aIgnoreCase) const { +PRInt32 nsString1::Compare(const nsString1 &S,PRBool aIgnoreCase) const { int maxlen=(S.mLength(const nsString &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 nsString::operator<=(const nsString &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 nsString::operator>=(const nsString &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 nsString1::operator==(const nsString1 &S) const {return Equals(S);} +PRBool nsString1::operator==(const char *s) const {return Equals(s);} +PRBool nsString1::operator==(const PRUnichar *s) const {return Equals(s);} +PRBool nsString1::operator==(PRUnichar *s) const {return Equals(s);} +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);} +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);} +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 this to given string; note that we compare full strings here. * * @update gess 7/27/98 - * @param aString is the other nsString to be compared to + * @param aString is the other nsString1 to be compared to * @return TRUE if equal */ -PRBool nsString::Equals(const nsString& aString) const { +PRBool nsString1::Equals(const nsString1& aString) const { if(aString.mLength==mLength) { PRInt32 result=nsCRT::strcmp(mStr,aString.mStr); return PRBool(0==result); @@ -1865,7 +1903,7 @@ PRBool nsString::Equals(const nsString& aString) const { * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString::Equals(const char* aCString,PRInt32 aLength) const{ +PRBool nsString1::Equals(const char* aCString,PRInt32 aLength) const{ NS_ASSERTION(0!=aCString,kNullPointerError); if((aLength>0) && (aLength!=mLength)) @@ -1882,7 +1920,7 @@ PRBool nsString::Equals(const char* aCString,PRInt32 aLength) const{ * @param aAtom -- atom to compare to this * @return TRUE if equal */ -PRBool nsString::Equals(const nsIAtom* aAtom) const +PRBool nsString1::Equals(const nsIAtom* aAtom) const { NS_ASSERTION(0!=aAtom,kNullPointerError); PRInt32 result=nsCRT::strcmp(mStr,aAtom->GetUnicode()); @@ -1897,7 +1935,7 @@ PRBool nsString::Equals(const nsIAtom* aAtom) const * @param s2 -- second string to be compared * @return TRUE if equal */ -PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString1::Equals(const PRUnichar* s1, const PRUnichar* s2) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1915,7 +1953,7 @@ PRBool nsString::Equals(const PRUnichar* s1, const PRUnichar* s2) const { * @param * @return */ -PRBool nsString::EqualsIgnoreCase(const nsString& aString) const{ +PRBool nsString1::EqualsIgnoreCase(const nsString1& aString) const{ if(aString.mLength==mLength) { PRInt32 result=nsCRT::strcasecmp(mStr,aString.mStr); return PRBool(0==result); @@ -1930,7 +1968,7 @@ PRBool nsString::EqualsIgnoreCase(const nsString& aString) const{ * @param * @return */ -PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const{ +PRBool nsString1::EqualsIgnoreCase(const nsIAtom *aAtom) const{ NS_ASSERTION(0!=aAtom,kNullPointerError); PRBool result=PR_FALSE; if(aAtom){ @@ -1949,7 +1987,7 @@ PRBool nsString::EqualsIgnoreCase(const nsIAtom *aAtom) const{ * @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 nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { +PRBool nsString1::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) const { NS_ASSERTION(0!=s1,kNullPointerError); NS_ASSERTION(0!=s2,kNullPointerError); PRBool result=PR_FALSE; @@ -1971,7 +2009,7 @@ PRBool nsString::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) cons * @param aLength -- length of given string. * @return TRUE if equal */ -PRBool nsString::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { +PRBool nsString1::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { NS_ASSERTION(0!=aCString,kNullPointerError); if((aLength>0) && (aLength!=mLength)) @@ -1988,7 +2026,7 @@ PRBool nsString::EqualsIgnoreCase(const char* aCString,PRInt32 aLength) const { * @param * @return */ -void nsString::DebugDump(ostream& aStream) const { +void nsString1::DebugDump(ostream& aStream) const { for(int i=0;i mCapacity) { PRInt32 size = mCapacity * 2; if (size < aNewLength) { @@ -2135,7 +2174,7 @@ void nsAutoString::EnsureCapacityFor(PRInt32 aNewLength) { } void -nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const +nsAutoString1::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(sizeof(*this)); if (mStr != mBuf) { @@ -2151,8 +2190,8 @@ nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const * @param * @return */ -void nsAutoString::SelfTest(){ - nsAutoString xas("Hello there"); +void nsAutoString1::SelfTest(){ + nsAutoString1 xas("Hello there"); xas.Append("this string exceeds the max size"); xas.DebugDump(cout); } @@ -2163,7 +2202,7 @@ void nsAutoString::SelfTest(){ * @param * @return */ -ostream& operator<<(ostream& os,nsAutoString& aString){ +ostream& operator<<(ostream& os,nsAutoString1& aString){ const PRUnichar* uc=aString.GetUnicode(); int len=aString.Length(); for(int i=0;i= PRInt32(sizeof(buf))) { + PRInt32 aLength=aString.Length(); + if (aLength >= PRInt32(sizeof(buf))) { cp = aString.ToNewCString(); } else { - aString.ToCString(cp, len + 1); + aString.ToCString(cp, aLength + 1); } - if(len>0) - ::fwrite(cp, 1, len, out); + if(aLength >0) + ::fwrite(cp, 1, aLength, out); if (cp != buf) { delete [] cp; } - return (int) len; + return (int) aLength; } - + + /** * @@ -2203,38 +2242,42 @@ NS_BASE int fputs(const nsString& aString, FILE* out) * @param * @return */ -void nsString::SelfTest(void) { +void nsString1::SelfTest(void) { #ifdef RICKG_DEBUG mSelfTested=PR_TRUE; - nsAutoString a("foobar"); - nsAutoString b("foo"); - nsAutoString c(".5111"); - nsAutoString d(" 5"); - PRInt32 result=a.Compare(b); - PRInt32 result2=result; - result=c.ToInteger(&result2); - result=d.ToInteger(&result2); - result2=result; + + { + 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!"; + - nsAutoString as("Hello there"); + nsAutoString1 as("Hello there"); as.SelfTest(); static const char* temp="hello"; //first, let's test the constructors... - nsString empty; + nsString1 empty; empty=""; empty="xxx"; empty=""; - nsString a(temp); - nsString* a_=new nsString(a); //test copy constructor - nsString b("world!"); + nsString1 a(temp); + nsString1* a_=new nsString1(a); //test copy constructor + nsString1 b("world!"); //verify destructor... delete a_; @@ -2248,11 +2291,9 @@ void nsString::SelfTest(void) { //********************************************** const chartype* p1=a.GetUnicode(); - const chartype* p2=a; //should invoke the PRUnichar conversion operator... for(int i=0;itemp3.Length(),kConstructorError); //should be char longer - nsString* es1=temp2.ToNewString(); //this should make us a new string + nsString1* es1=temp2.ToNewString(); //this should make us a new string char* es2=temp2.ToNewCString(); for(i=0;i #include -class nsISizeOfHandler; #include "nsStr.h" -class NS_BASE nsString { +#ifndef USE_STRING2 +#define nsString1 nsString +#define nsAutoString1 nsAutoString +#else +#include "nsString2.h" //get new string class +#endif + +class nsISizeOfHandler; + +class NS_BASE nsString1 { public: /** @@ -51,30 +59,30 @@ class NS_BASE nsString { * was to allow developers direct access to the underlying buffer for * performance reasons. */ -nsString(); +nsString1(); /** * This constructor accepts an isolatin string * @param an ascii is a ptr to a 1-byte cstr */ -nsString(const char* aCString); +nsString1(const char* aCString); /** * This is our copy constructor - * @param reference to another nsString + * @param reference to another nsString1 */ -nsString(const nsString&); +nsString1(const nsString1&); /** * Constructor from a unicode string * @param anicodestr pts to a unicode string */ -nsString(const PRUnichar* aUnicode); +nsString1(const PRUnichar* aUnicode); /** * Virtual Destructor */ -virtual ~nsString(); +virtual ~nsString1(); /** @@ -144,7 +152,7 @@ operator const PRUnichar*() const; * @param offset into string * @return PRUnichar* to internal string */ -PRUnichar operator()(PRInt32 anIndex) const; +//PRUnichar operator()(PRInt32 anIndex) const; /** * Retrieve reference to unicode char at given index @@ -184,35 +192,35 @@ PRBool SetCharAt(PRUnichar aChar,PRInt32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString operator+(const nsString& aString); +nsString1 operator+(const nsString1& 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 */ -nsString operator+(const char* aCString); +nsString1 operator+(const char* aCString); /** * 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 */ -nsString operator+(char aChar); +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 */ -nsString operator+(const PRUnichar* aBuffer); +nsString1 operator+(const PRUnichar* aBuffer); /** * 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 */ -nsString operator+(PRUnichar aChar); +nsString1 operator+(PRUnichar aChar); /** * Converts all chars in internal string to lower @@ -222,7 +230,7 @@ void ToLowerCase(); /** * Converts all chars in given string to lower */ -void ToLowerCase(nsString& aString) const; +void ToLowerCase(nsString1& aString) const; /** * Converts all chars in given string to upper @@ -238,13 +246,13 @@ void ToUCS2(PRInt32 aStartOffset); /** * Converts all chars in internal string to upper */ -void ToUpperCase(nsString& aString) const; +void ToUpperCase(nsString1& aString) const; /** * Creates a duplicate clone (ptr) of this string. * @return ptr to clone of this string */ -nsString* ToNewString() const; +nsString1* ToNewString() const; /** * Creates an ascii clone of this string @@ -266,7 +274,7 @@ char* ToCString(char* aBuf,PRInt32 aBufLength) const; * @param aString to hold copy of this * @return nada. */ -void Copy(nsString& aString) const; +void Copy(nsString1& aString) const; /** * Creates an unichar clone of this string @@ -299,79 +307,72 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; if you want me to determine its length * @return this */ -nsString& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); - -/** - * assign given char* to this string - * @param aCString: 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 - */ -nsString& SetString(const char* aCString,PRInt32 aLength=-1); +nsString1& SetString(const PRUnichar* aStr,PRInt32 aLength=-1); +nsString1& SetString(const char* aCString,PRInt32 aLength=-1); +nsString1& SetString(const nsString1& aString); /** * assign given string to this one * @param aString: string to be added to this * @return this */ -nsString& operator=(const nsString& aString); +nsString1& operator=(const nsString1& aString); /** * assign given char* to this string * @param aCString: buffer to be assigned to this * @return this */ -nsString& operator=(const char* aCString); +nsString1& operator=(const char* aCString); /** * assign given char to this string * @param aChar: char to be assignd to this * @return this */ -nsString& operator=(char aChar); +nsString1& operator=(char aChar); /** * assign given unichar* to this string * @param aBuffer: unichar buffer to be assigned to this * @return this */ -nsString& operator=(const PRUnichar* aBuffer); +nsString1& operator=(const PRUnichar* aBuffer); /** * assign given char to this string * @param aChar: char to be assignd to this * @return this */ -nsString& operator=(PRUnichar aChar); +nsString1& operator=(PRUnichar aChar); /** * append given string to this string * @param aString : string to be appended to this * @return this */ -nsString& operator+=(const nsString& aString); +nsString1& operator+=(const nsString1& aString); /** * append given buffer to this string * @param aCString: buffer to be appended to this * @return this */ -nsString& operator+=(const char* aCString); +nsString1& operator+=(const char* aCString); /** * append given buffer to this string * @param aBuffer: buffer to be appended to this * @return this */ -nsString& operator+=(const PRUnichar* aBuffer); +nsString1& operator+=(const PRUnichar* aBuffer); /** * append given char to this string * @param aChar: char to be appended to this * @return this */ -nsString& operator+=(PRUnichar aChar); +nsString1& operator+=(PRUnichar aChar); /** * append given string to this string @@ -380,7 +381,7 @@ nsString& operator+=(PRUnichar aChar); if you want me to determine its length * @return this */ -nsString& Append(const nsString& aString,PRInt32 aLength=-1); +nsString1& Append(const nsString1& aString,PRInt32 aLength=-1); /** * append given string to this string @@ -389,14 +390,14 @@ nsString& Append(const nsString& aString,PRInt32 aLength=-1); if you want me to determine its length * @return this */ -nsString& Append(const char* aCString,PRInt32 aLength=-1); +nsString1& Append(const char* aCString,PRInt32 aLength=-1); /** * append given string to this string * @param aString : string to be appended to this * @return this */ -nsString& Append(char aChar); +nsString1& Append(char aChar); /** * append given unichar buffer to this string @@ -405,14 +406,14 @@ nsString& Append(char aChar); if you want me to determine its length * @return this */ -nsString& Append(const PRUnichar* aBuffer,PRInt32 aLength=-1); +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 */ -nsString& Append(PRUnichar aChar); +nsString1& Append(PRUnichar aChar); /** * Append an integer onto this string @@ -420,14 +421,14 @@ nsString& Append(PRUnichar aChar); * @param aRadix specifies 8,10,16 * @return this */ -nsString& Append(PRInt32 aInteger,PRInt32 aRadix); //radix=8,10 or 16 +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 */ -nsString& Append(float aFloat); +nsString1& Append(float aFloat); /* * Copies n characters from this string to given string, @@ -438,7 +439,7 @@ nsString& Append(float aFloat); * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Left(nsString& aCopy,PRInt32 aCount) const; +PRInt32 Left(nsString1& aCopy,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -450,7 +451,7 @@ PRInt32 Left(nsString& aCopy,PRInt32 aCount) const; * @param anOffset -- position where copying begins * @return number of chars copied */ -PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const; +PRInt32 Mid(nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount) const; /* * Copies n characters from this string to given string, @@ -461,7 +462,7 @@ PRInt32 Mid(nsString& aCopy,PRInt32 anOffset,PRInt32 aCount) const; * @param aCount -- number of chars to copy * @return number of chars copied */ -PRInt32 Right(nsString& aCopy,PRInt32 aCount) const; +PRInt32 Right(nsString1& aCopy,PRInt32 aCount) const; /* * This method inserts n chars from given string into this @@ -472,7 +473,7 @@ PRInt32 Right(nsString& aCopy,PRInt32 aCount) const; * @param aCount -- number of chars to be copied from aCopy * @return number of chars inserted into this. */ -PRInt32 Insert(const nsString& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); +PRInt32 Insert(const nsString1& aCopy,PRInt32 anOffset,PRInt32 aCount=-1); /** * Insert a single unicode char into this string at @@ -492,7 +493,7 @@ PRInt32 Insert(PRUnichar aChar,PRInt32 anOffset); * @param aCount -- number of chars to be cut * @return *this */ -nsString& Cut(PRInt32 anOffset,PRInt32 aCount); +nsString1& Cut(PRInt32 anOffset,PRInt32 aCount); /** * This method is used to remove all occurances of the @@ -501,7 +502,7 @@ nsString& Cut(PRInt32 anOffset,PRInt32 aCount); * @param aSet -- characters to be cut from this * @return *this */ -nsString& StripChars(const char* aSet); +nsString1& StripChars(const char* aSet); /** * This method is used to replace all occurances of the @@ -510,14 +511,14 @@ nsString& StripChars(const char* aSet); * @param * @return *this */ -nsString& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); +nsString1& ReplaceChar(PRUnichar aSourceChar, PRUnichar aDestChar); /** * This method strips whitespace throughout the string * * @return this */ -nsString& StripWhitespace(); +nsString1& StripWhitespace(); /** * This method trims characters found in aTrimSet from @@ -527,7 +528,7 @@ nsString& StripWhitespace(); * both ends * @return this */ -nsString& Trim(const char* aSet, +nsString1& Trim(const char* aSet, PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE); @@ -540,7 +541,7 @@ nsString& Trim(const char* aSet, * @param aEliminateTrailing controls stripping of trailing ws * @return this */ -nsString& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, +nsString1& CompressWhitespace( PRBool aEliminateLeading=PR_TRUE, PRBool aEliminateTrailing=PR_TRUE); /** @@ -589,7 +590,7 @@ PRInt32 BinarySearch(PRUnichar aChar) const; */ PRInt32 Find(const char* aString) const; PRInt32 Find(const PRUnichar* aString) const; -PRInt32 Find(const nsString& aString) const; +PRInt32 Find(const nsString1& aString) const; /** * Search for given char within this string @@ -607,7 +608,7 @@ PRInt32 Find(PRUnichar aChar,PRInt32 offset=0) const; * @return -1 if not found, else the offset in this */ PRInt32 FindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const; +PRInt32 FindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; /** * This method searches this string for the last character @@ -617,7 +618,7 @@ PRInt32 FindCharInSet(nsString& aString,PRInt32 anOffset=0) const; * @return -1 if not found, else the offset in this */ PRInt32 RFindCharInSet(const char* aString,PRInt32 anOffset=0) const; -PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const; +PRInt32 RFindCharInSet(nsString1& aString,PRInt32 anOffset=0) const; /** @@ -628,7 +629,7 @@ PRInt32 RFindCharInSet(nsString& aString,PRInt32 anOffset=0) const; */ PRInt32 RFind(const char* aCString,PRBool aIgnoreCase=PR_FALSE) const; PRInt32 RFind(const PRUnichar* aString,PRBool aIgnoreCase=PR_FALSE) const; -PRInt32 RFind(const nsString& 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 @@ -649,7 +650,7 @@ PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE) const; * @param aIgnoreCase tells us how to treat case * @return -1,0,1 */ -virtual PRInt32 Compare(const nsString &aString,PRBool aIgnoreCase=PR_FALSE) const; +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; @@ -658,7 +659,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 nsString &aString) const; +PRBool operator==(const nsString1 &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; PRBool operator==(PRUnichar* aString) const; @@ -668,7 +669,7 @@ PRBool operator==(PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE */ -PRBool operator!=(const nsString &aString) const; +PRBool operator!=(const nsString1 &aString) const; PRBool operator!=(const char *aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -677,7 +678,7 @@ PRBool operator!=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<(const nsString &aString) const; +PRBool operator<(const nsString1 &aString) const; PRBool operator<(const char *aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -686,7 +687,7 @@ PRBool operator<(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>(const nsString &S) const; +PRBool operator>(const nsString1 &S) const; PRBool operator>(const char *aCString) const; PRBool operator>(const PRUnichar* aString) const; @@ -695,7 +696,7 @@ PRBool operator>(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator<=(const nsString &S) const; +PRBool operator<=(const nsString1 &S) const; PRBool operator<=(const char *aCString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -704,7 +705,7 @@ PRBool operator<=(const PRUnichar* aString) const; * @param aString is the string to be compared to this * @return TRUE or FALSE */ -PRBool operator>=(const nsString &S) const; +PRBool operator>=(const nsString1 &S) const; PRBool operator>=(const char* aCString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -718,7 +719,7 @@ PRBool operator>=(const PRUnichar* aString) const; * @param aLength -- optional length of given string. * @return TRUE if equal */ -PRBool Equals(const nsString& aString) const; +PRBool Equals(const nsString1& aString) const; PRBool Equals(const char* aString,PRInt32 aLength=-1) const; PRBool Equals(const nsIAtom *aAtom) const; @@ -741,7 +742,7 @@ PRBool Equals(const PRUnichar* s1, const PRUnichar* s2) const; * @param aLength -- optional length of given string. * @return TRUE if equal */ -PRBool EqualsIgnoreCase(const nsString& aString) const; +PRBool EqualsIgnoreCase(const nsString1& aString) const; PRBool EqualsIgnoreCase(const char* aString,PRInt32 aLength=-1) const; PRBool EqualsIgnoreCase(const nsIAtom *aAtom) const; @@ -769,34 +770,34 @@ typedef PRUnichar chartype; #endif }; -ostream& operator<<(ostream& os,nsString& aString); -extern NS_BASE int fputs(const nsString& aString, FILE* out); +ostream& operator<<(ostream& os,nsString1& aString); +extern NS_BASE int fputs(const nsString1& aString, FILE* out); //---------------------------------------------------------------------- /** - * A version of nsString which is designed to be used as an automatic + * 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_BASE nsAutoString : public nsString { +class NS_BASE nsAutoString1 : public nsString1 { public: - nsAutoString(); - nsAutoString(const nsString& other); - nsAutoString(const nsAutoString& other); - nsAutoString(PRUnichar aChar); - nsAutoString(const char* aCString); - nsAutoString(const PRUnichar* us, PRInt32 uslen = -1); - virtual ~nsAutoString(); + 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(); - nsAutoString& operator=(const nsString& aString) {nsString::operator=(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;} + 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; @@ -808,7 +809,7 @@ protected: chartype mBuf[32]; }; -ostream& operator<<(ostream& os,nsAutoString& aString); +ostream& operator<<(ostream& os,nsAutoString1& aString); #endif diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index 95afc27ff6b..d1224ddc94f 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -19,30 +19,25 @@ #include #include -#include +#include #include #include "nsString2.h" #include "nsDebug.h" #include "nsCRT.h" -#include "nsIAtom.h" -#include "nsISizeOfHandler.h" -#include "prprf.h" -#include "prdtoa.h" +#include "nsIAtom.h" #include "nsDeque.h" -#include "nsUnicharUtilCIID.h" -#include "nsIServiceManager.h" -#include "nsICaseConversion.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 "; -#ifdef RICKG_DEBUG -PRBool nsString2::mSelfTested = PR_FALSE; -#endif -#define NOT_USED 0xfffd +#define NOT_USED 0xfffd static PRUint16 PA_HackTable[] = { NOT_USED, @@ -96,61 +91,7 @@ public: } } }; -static CTableConstructor gTableConstructor; -//---- XPCOM code to connect with UnicharUtil - -class HandleCaseConversionShutdown3 : public nsIShutdownListener { -public : - NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); - HandleCaseConversionShutdown3(void) { NS_INIT_REFCNT(); } - virtual ~HandleCaseConversionShutdown3(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(HandleCaseConversionShutdown3, kIShutdownListenerIID); - -nsresult -HandleCaseConversionShutdown3::OnShutdown(const nsCID& cid, nsISupports* service) -{ - if (cid.Equals(kUnicharUtilCID)) { - NS_ASSERTION(service == gCaseConv, "wrong service!"); - gCaseConv->Release(); - gCaseConv = NULL; - } - return NS_OK; -} - -#if 0 -/* not used yet */ -static HandleCaseConversionShutdown3* gListener = NULL; - -static void StartUpCaseConversion() -{ - nsresult err; - - if ( NULL == gListener ) - { - gListener = new HandleCaseConversionShutdown3(); - gListener->AddRef(); - } - err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, - (nsISupports**) &gCaseConv, gListener); -} -static void CheckCaseConversion() -{ - if(NULL == gCaseConv ) - StartUpCaseConversion(); - - // NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); - -} -#endif /*********************************************************************** IMPLEMENTATION NOTES: @@ -159,6 +100,15 @@ static void CheckCaseConversion() that no poor soul would ever have to do this again. Sigh. ***********************************************************************/ +void Subsume(nsStr& aDest,nsStr& aSource){ + 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; +} /** * Default constructor. Note that we actually allocate a small buffer @@ -167,14 +117,10 @@ static void CheckCaseConversion() * performance reasons. */ nsString2::nsString2(eCharSize aCharSize,nsIMemoryAgent* anAgent) : mAgent(anAgent) { - nsStr::Initialize(*this,aCharSize); - -#ifdef RICKG_DEBUG - if(!mSelfTested) { - mSelfTested=PR_TRUE; - SelfTest(); - } +#ifdef RICKG_DEBUG + static CStringTester gStringTester; #endif + nsStr::Initialize(*this,aCharSize); } /** @@ -213,10 +159,19 @@ nsString2::nsString2(const nsStr &aString,eCharSize aCharSize,nsIMemoryAgent* an * @param reference to another nsString2 */ nsString2::nsString2(const nsString2& aString) :mAgent(aString.mAgent) { - nsStr::Initialize(*this,(eCharSize)aString.mMultibyte); + nsStr::Initialize(*this,(eCharSize)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 + */ +nsString2::nsString2(nsSubsumeStr& aSubsumeStr) :mAgent(0) { + Subsume(*this,aSubsumeStr); +} + /** * Destructor * Make sure we call nsStr::Destroy. @@ -227,7 +182,7 @@ nsString2::~nsString2() { void nsString2::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(sizeof(*this)); - aHandler->Add(mCapacity << mMultibyte); + aHandler->Add(mCapacity << mCharSize); } /** @@ -291,11 +246,10 @@ void nsString2::SetCapacity(PRUint32 aLength) { /** * * @update gess1/4/99 - * @param - * @return + * @return ptr to internal buffer (if 1-byte), otherwise NULL */ -char* nsString2::GetBuffer(void) const { - if(!mMultibyte) +const char* nsString2::GetBuffer(void) const { + if(!mCharSize) return mStr; return 0; } @@ -303,11 +257,10 @@ char* nsString2::GetBuffer(void) const { /** * * @update gess1/4/99 - * @param - * @return + * @return ptr to internal buffer (if 2-byte), otherwise NULL */ -PRUnichar* nsString2::GetUnicode(void) const { - if(mMultibyte) +const PRUnichar* nsString2::GetUnicode(void) const { + if(mCharSize) return (PRUnichar*)mUStr; return 0; } @@ -315,11 +268,11 @@ PRUnichar* nsString2::GetUnicode(void) const { /** * Get nth character. */ -PRUnichar nsString2::operator[](int anIndex) const { +PRUnichar nsString2::operator[](PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } -PRUnichar nsString2::CharAt(int anIndex) const { +PRUnichar nsString2::CharAt(PRUint32 anIndex) const { return GetCharAt(*this,anIndex); } @@ -334,7 +287,7 @@ PRUnichar nsString2::Last(void) const{ PRBool nsString2::SetCharAt(PRUnichar aChar,PRUint32 anIndex){ PRBool result=PR_FALSE; if(anIndex(const nsStr &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 nsString2::operator<=(const nsStr &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 nsString2::operator>=(const nsStr &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);} @@ -1416,6 +1421,18 @@ PRBool nsString2::EqualsIgnoreCase(const PRUnichar* s1, const PRUnichar* s2) con return Equals(s1,s2,PR_TRUE); } +/** + * 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 + * @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); +} + /** * Compare this to given string; note that we compare full strings here. * @@ -1569,21 +1586,42 @@ PRBool nsString2::IsDigit(PRUnichar aChar) { } +/************************************************************** + Define the string deallocator class... + **************************************************************/ +class nsStringDeallocator: public nsDequeFunctor{ +public: + virtual void* operator()(void* anObject) { + static nsMemoryAgent theAgent; + nsString2* aString= (nsString2*)anObject; + if(aString){ + aString->mAgent=&theAgent; + delete aString; + } + return 0; + } +}; + /**************************************************************************** * This class, appropriately enough, creates and recycles nsString2 objects.. ****************************************************************************/ -#if 0 + class nsStringRecycler { public: nsStringRecycler() : mDeque(0) { } + ~nsStringRecycler() { + nsStringDeallocator theDeallocator; + mDeque.ForEach(theDeallocator); //now delete the strings + } + void Recycle(nsString2* aString) { mDeque.Push(aString); } - nsString2* NewString(eCharSize aCharSize){ + nsString2* CreateString(eCharSize aCharSize){ nsString2* result=(nsString2*)mDeque.Pop(); if(!result) result=new nsString2(aCharSize); @@ -1613,8 +1651,8 @@ nsStringRecycler& GetRecycler(void){ * @param * @return */ -nsString2* nsString2::NewString(eCharSize aCharSize){ - nsString2* result=GetRecycler().NewString(aCharSize); +nsString2* nsString2::CreateString(eCharSize aCharSize){ + nsString2* result=GetRecycler().CreateString(aCharSize); return result; } @@ -1628,7 +1666,6 @@ void nsString2::Recycle(nsString2* aString){ GetRecycler().Recycle(aString); } -#endif /** * @@ -1650,7 +1687,7 @@ void nsString2::DebugDump(ostream& aStream) const { * @return */ ostream& operator<<(ostream& os,nsString2& aString){ - if(PR_FALSE==aString.mMultibyte) { + if(PR_FALSE==aString.mCharSize) { os<>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((eCharSize)aStr.mCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,(eCharSize)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((eCharSize)aCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,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(char* aCString,PRUint32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { + mAgent=0; + if(assumeOwnership) { + nsStr::Initialize(*this,aCString,aCapacity,0,eOneByte,PR_TRUE); + } + 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) { + mAgent=0; + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aString); +} + +/** + * Copy construct from uni-string + * @param aString is a ptr to a unistr + */ +nsAutoString2::nsAutoString2(PRUnichar* aString,PRUint32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { + mAgent=0; + if(assumeOwnership) { + nsStr::Initialize(*this,(char*)aString,aCapacity,0,eTwoByte,PR_TRUE); + } + else { + 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 nsStr& aString,eCharSize aCharSize) : nsString2(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 nsAutoString2& aString,eCharSize aCharSize) : nsString2(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); +} + + +/** + * Copy construct from an nsString2 + * @param + */ +nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ + mAgent=0; + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aChar); +} + +/** + * construct from a subsumeable string + * @update gess 1/4/99 + * @param reference to a subsumeString + */ +nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2((eCharSize)aSubsumeStr.mCharSize) { + mAgent=0; + Subsume(*this,aSubsumeStr); +} + +/** + * deconstruct the autstring + * @param + */ +nsAutoString2::~nsAutoString2(){ +// bool b=true; +// mStr=0; +} + +void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity << mCharSize); +} + +nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2((eCharSize)aString.mCharSize) { + Subsume(*this,aString); +} + +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharSize) { + Subsume(*this,aString); +} + +nsSubsumeStr::nsSubsumeStr(const PRUnichar* aString) : nsString2(aString,eTwoByte) { +} + +nsSubsumeStr::nsSubsumeStr(const char* aString) : nsString2(aString,eOneByte) { +} + +#ifdef RICKG_DEBUG +/*********************************************************************** + IMPLEMENTATION of CStringTester... + ***********************************************************************/ +CStringTester::CStringTester() { -#ifdef RICKG_DEBUG static const char* kConstructorError = kConstructorError; static const char* kComparisonError = "Comparision error!"; static const char* kEqualsError = "Equals error!"; - mSelfTested=PR_TRUE; - eCharSize theSize=eOneByte; + eCharSize theSize=eTwoByte; //begin by testing the constructors... { { nsString2 theString0("foo",theSize); //watch it construct and destruct - } + + { + nsString2 theString("hello"); + nsString2 temp1=theString+" there!"; + nsString2 temp2=theString+'!'; + nsSubsumeStr temp3=theString+'?'; + nsString2 temp4(temp3); + temp1=temp3; + nsSubsumeStr temp5("hello"); + } + nsString2 theString1(theSize); nsString2 theString("hello",theSize); nsString2 theString3(theString,theSize); @@ -1725,6 +1934,8 @@ void nsString2::SelfTest(void) { //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; @@ -1740,12 +1951,17 @@ void nsString2::SelfTest(void) { 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... + + //try a few numeric conversion routines... { nsString2 str1("10000",theSize); PRInt32 err; @@ -1757,11 +1973,12 @@ void nsString2::SelfTest(void) { { nsString2 theString("hello",theSize); PRUint32 len=theString.Length(); + PRUnichar ch; for(PRUint32 i=0;i>aDest.mMultibyte)-1; - aDest.mOwnsBuffer=aBufDescriptor.mOwnsBuffer; - AddNullTerminator(aDest); -} - -/** - * 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 - */ -nsAutoString2::nsAutoString2(eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); -} - -/** - * 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(nsBufDescriptor& aBufDescriptor,const char* aCString) : nsString2(aBufDescriptor.mMultibyte) { - InitAutoStr(*this,aBufDescriptor); - 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) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aCString); -} - -/** - * Copy construct from ascii c-string - * @param aCString is a ptr to a 1-byte cstr - */ -nsAutoString2::nsAutoString2(char* aCString,PRUint32 aLen,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - if(assumeOwnership) { - nsBufDescriptor theDescriptor(aCString,aLen,eOneByte,PR_TRUE); - InitAutoStr(*this,theDescriptor); + { + //now let's try a memory allocation test... + nsString2 temp; + for(int i=0;i<100;i++){ + temp+="hello "; + } + int x=5; } - else { - nsBufDescriptor theDescriptor(mBuffer,aLen,aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - } - Assign(aCString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - */ -nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from uni-string - * @param aString is a ptr to a unistr - */ -nsAutoString2::nsAutoString2(PRUnichar* aString,PRUint32 aLength,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { - if(assumeOwnership) { - nsBufDescriptor theDescriptor((char*)aString,aLength,eTwoByte,PR_TRUE); - InitAutoStr(*this,theDescriptor); - } - else { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - } - Assign(aString); -} - - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsStr& aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize) : nsString2(aCharSize) { - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(const nsString2& aString,eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aString); -} - - -/** - * Copy construct from an nsString2 - * @param - */ -nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aCharSize){ - nsBufDescriptor theDescriptor(mBuffer,sizeof(mBuffer),aCharSize,PR_FALSE); - InitAutoStr(*this,theDescriptor); - Assign(aChar); -} - -/** - * deconstruct the autstring - * @param - */ -nsAutoString2::~nsAutoString2(){ -// bool b=true; -// mStr=0; -} - -void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { - aHandler->Add(sizeof(*this)); - aHandler->Add(mCapacity << mMultibyte); -} +}; +#endif //rickg_debug diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index d5e07d9f994..ec7b2be85fe 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -20,18 +20,19 @@ /*********************************************************************** MODULE NOTES: - This class provides a 1-byte ASCII string implementation that shares - a common API with all other strImpl derivatives. + 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 _nsString2 #define _nsString2 -//#define nsString2 nsString -//#define nsAutoString2 nsAutoString - - #include "prtypes.h" #include "nscore.h" #include @@ -46,6 +47,13 @@ class nsISizeOfHandler; +#ifdef USE_STRING2 +#define nsString2 nsString +#define nsAutoString2 nsAutoString +#endif + +class NS_BASE nsSubsumeStr; + class NS_BASE nsString2 : public nsStr { public: @@ -83,6 +91,12 @@ nsString2(const nsStr&,eCharSize aCharSize=kDefaultCharSize,nsIMemoryAgent* anAg */ nsString2(const nsString2& aString); +/** + * This constructor takes a subsumestr + * @param reference to subsumestr + */ +nsString2(nsSubsumeStr& aSubsumeStr); + /** * Destructor * @@ -139,16 +153,15 @@ PRBool IsOrdered(void) const; Accessor methods... *********************************************************************/ -char* GetBuffer(void) const; -PRUnichar* GetUnicode(void) const; -operator PRUnichar*() const {return GetUnicode();} +const char* GetBuffer(void) const; +const PRUnichar* GetUnicode(void) const; /** * Get nth character. */ -PRUnichar operator[](int anIndex) const; -PRUnichar CharAt(int anIndex) const; +PRUnichar operator[](PRUint32 anIndex) const; +PRUnichar CharAt(PRUint32 anIndex) const; PRUnichar First(void) const; PRUnichar Last(void) const; @@ -164,42 +177,42 @@ PRBool SetCharAt(PRUnichar aChar,PRUint32 anIndex); * @param aString -- 2nd string to be appended * @return new string */ -nsString2 operator+(const nsStr& aString); +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 */ -nsString2 operator+(const nsString2& aString); +nsSubsumeStr operator+(const nsString2& 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 */ -nsString2 operator+(const char* aCString); +nsSubsumeStr 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 */ -nsString2 operator+(const PRUnichar* aString); +nsSubsumeStr 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 */ -nsString2 operator+(char aChar); +nsSubsumeStr 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 */ -nsString2 operator+(PRUnichar aChar); +nsSubsumeStr operator+(PRUnichar aChar); /********************************************************************** Lexomorphic transforms... @@ -353,7 +366,6 @@ PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=10) const; 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);} -void Copy(nsString2& aString) const; /** * assign given string to this string @@ -380,6 +392,7 @@ 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);} +nsString2& operator=(nsSubsumeStr& aSubsumeString); /** * Here's a bunch of append mehtods for varying types... @@ -402,8 +415,7 @@ nsString2& operator+=(PRUnichar aChar){return Append(aChar);} */ nsString2& Append(const nsStr& aString) {return Append(aString,aString.mLength);} nsString2& Append(const nsString2& aString) {return Append(aString,aString.mLength);} -nsString2& Append(const char* aString) {if(aString) {Append(aString,nsCRT::strlen(aString));} return *this;} -nsString2& Append(const PRUnichar* aString) {if(aString) {Append(aString,nsCRT::strlen(aString));} return *this;} + /* * Appends n characters from given string to this, @@ -414,13 +426,13 @@ nsString2& Append(const PRUnichar* aString) {if(aString) {Append(aString,nsCRT:: */ nsString2& Append(const nsStr& aString,PRInt32 aCount); nsString2& Append(const nsString2& aString,PRInt32 aCount); -nsString2& Append(const char* aString,PRInt32 aCount); -nsString2& Append(const PRUnichar* 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); - + /* * Copies n characters from this string to given string, * starting at the leftmost offset. @@ -464,7 +476,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(nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); +nsString2& Insert(const nsString2& aCopy,PRUint32 anOffset,PRInt32 aCount=-1); /** * Insert a given string into this string at @@ -519,10 +531,11 @@ 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(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) const; +PRInt32 Find(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; /** * This method searches this string for the first character @@ -554,9 +567,10 @@ PRInt32 RFindCharInSet(const nsString2& aString,PRUint32 anOffset=0) const; * @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(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) const; +PRInt32 RFind(PRUnichar aChar,PRUint32 offset=0,PRBool aIgnoreCase=PR_FALSE) const; /********************************************************************** Comparison methods... @@ -569,6 +583,7 @@ PRInt32 RFind(PRUnichar aChar,PRBool aIgnoreCase=PR_FALSE,PRUint32 offset=0) 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 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; @@ -578,6 +593,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 nsStr &aString) const; PRBool operator==(const char *aString) const; PRBool operator==(const PRUnichar* aString) const; @@ -587,6 +603,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 nsStr &aString) const; PRBool operator!=(const char* aString) const; PRBool operator!=(const PRUnichar* aString) const; @@ -596,6 +613,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 nsStr &aString) const; PRBool operator<(const char* aString) const; PRBool operator<(const PRUnichar* aString) const; @@ -605,6 +623,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 nsStr &S) const; PRBool operator>(const char* aString) const; PRBool operator>(const PRUnichar* aString) const; @@ -614,6 +633,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 nsStr &S) const; PRBool operator<=(const char* aString) const; PRBool operator<=(const PRUnichar* aString) const; @@ -623,6 +643,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 nsStr &S) const; PRBool operator>=(const char* aString) const; PRBool operator>=(const PRUnichar* aString) const; @@ -637,6 +658,7 @@ PRBool operator>=(const PRUnichar* aString) const; * @param aLength -- optional length of given string. * @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; @@ -675,18 +697,11 @@ static PRBool IsAlpha(PRUnichar ch); */ static PRBool IsDigit(PRUnichar ch); -#if 0 static void Recycle(nsString2* aString); -static nsString2* NewString(eCharSize aCharSize=eTwoByte); -#endif +static nsString2* CreateString(eCharSize aCharSize=eTwoByte); -static void SelfTest(); virtual void DebugDump(ostream& aStream) const; -#ifdef RICKG_DEBUG - static PRBool mSelfTested; -#endif - nsIMemoryAgent* mAgent; }; @@ -701,12 +716,11 @@ ostream& operator<<(ostream& os,nsString2& aString); If the buffer needs to grow, it gets reallocated on the heap. **************************************************************/ - class NS_BASE nsAutoString2 : public nsString2 { public: nsAutoString2(eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(nsBufDescriptor& anExtBuffer,const char* aCString); + nsAutoString2(nsStr& anExtBuffer,const char* aCString); nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); @@ -717,6 +731,7 @@ public: nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsAutoString2& aString,eCharSize aCharSize=kDefaultCharSize); + nsAutoString2(nsSubsumeStr& aSubsumeStr); nsAutoString2(PRUnichar aChar,eCharSize aCharSize=kDefaultCharSize); virtual ~nsAutoString2(); @@ -738,5 +753,37 @@ public: }; +/*************************************************************** + 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_BASE nsSubsumeStr : public nsString2 { +public: + nsSubsumeStr(nsString2& aString); + nsSubsumeStr(nsStr& aString); + nsSubsumeStr(const PRUnichar* aString); + nsSubsumeStr(const char* aString); +}; + +#ifdef RICKG_DEBUG +/******************************************************** + This class's only purpose in life is to test nsString2. + ********************************************************/ +class CStringTester { +public: + CStringTester(); +}; #endif +#endif + +