diff --git a/mozilla/string/obsolete/nsStr.cpp b/mozilla/string/obsolete/nsStr.cpp index 4211cab847b..c34f2cdd7b7 100644 --- a/mozilla/string/obsolete/nsStr.cpp +++ b/mozilla/string/obsolete/nsStr.cpp @@ -108,12 +108,15 @@ void nsStr::Destroy(nsStr& aDest,nsIMemoryAgent* anAgent) { * @param aNewLength -- new capacity of string in charSize units * @return void */ -void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { +PRBool nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { + PRBool result=PR_TRUE; if(aNewLength>aString.mCapacity) { nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - theAgent->Realloc(aString,aNewLength); - AddNullTerminator(aString); + result=theAgent->Realloc(aString,aNewLength); + if(aString.mStr) + AddNullTerminator(aString); } + return result; } /** @@ -123,24 +126,28 @@ void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* an * @param aNewLength -- new capacity of string in charSize units * @return void */ -void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { +PRBool nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { + PRBool result=PR_TRUE; if(aNewLength>aDest.mCapacity) { nsStr theTempStr; nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - EnsureCapacity(theTempStr,aNewLength,theAgent); - if(aDest.mLength) { - Append(theTempStr,aDest,0,aDest.mLength,theAgent); - } - theAgent->Free(aDest); - aDest.mStr = theTempStr.mStr; - theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... - aDest.mLength=theTempStr.mLength; - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + result=EnsureCapacity(theTempStr,aNewLength,theAgent); + if(result) { + if(aDest.mLength) { + Append(theTempStr,aDest,0,aDest.mLength,theAgent); + } + theAgent->Free(aDest); + aDest.mStr = theTempStr.mStr; + theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... + aDest.mLength=theTempStr.mLength; + aDest.mCapacity=theTempStr.mCapacity; + aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + } } + return result; } /** @@ -170,15 +177,19 @@ void nsStr::Append(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 a PRUint32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength); PRUint32 theLength=(anOffset+theRealLen aDest.mCapacity) { - GrowCapacity(aDest,aDest.mLength+theLength,anAgent); + isBigEnough=GrowCapacity(aDest,aDest.mLength+theLength,anAgent); } - //now append new chars, starting at offset - (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); + if(isBigEnough) { + //now append new chars, starting at offset + (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); - aDest.mLength+=theLength; - AddNullTerminator(aDest); + aDest.mLength+=theLength; + AddNullTerminator(aDest); + } } } } @@ -213,24 +224,26 @@ void nsStr::Insert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUin nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - EnsureCapacity(theTempStr,aDest.mLength+theLength,theAgent); //grow the temp buffer to the right size + PRBool isBigEnough=EnsureCapacity(theTempStr,aDest.mLength+theLength,theAgent); //grow the temp buffer to the right size - if(aDestOffset) { - Append(theTempStr,aDest,0,aDestOffset,theAgent); //first copy leftmost data... - } + if(isBigEnough) { + if(aDestOffset) { + Append(theTempStr,aDest,0,aDestOffset,theAgent); //first copy leftmost data... + } - Append(theTempStr,aSource,0,aSource.mLength,theAgent); //next copy inserted (new) data + Append(theTempStr,aSource,0,aSource.mLength,theAgent); //next copy inserted (new) data - PRUint32 theRemains=aDest.mLength-aDestOffset; - if(theRemains) { - Append(theTempStr,aDest,aDestOffset,theRemains,theAgent); //next copy rightmost data - } + PRUint32 theRemains=aDest.mLength-aDestOffset; + if(theRemains) { + Append(theTempStr,aDest,aDestOffset,theRemains,theAgent); //next copy rightmost data + } - theAgent->Free(aDest); - aDest.mStr = theTempStr.mStr; - theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + theAgent->Free(aDest); + aDest.mStr = theTempStr.mStr; + theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... + aDest.mCapacity=theTempStr.mCapacity; + aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + } } @@ -365,6 +378,21 @@ void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,P aDest.mLength=aNewLen; } + +/** + * + * @update gess1/7/99 + * @param + * @return + */ +void nsStr::StripChars(nsStr& aDest,const char* aSet){ + if((0 36 || aRadix< 2) { /* no conversion if wrong base */ - return *this; + char buf[]={'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + + PRInt32 radices[] = {1000000000,268435456}; + PRInt32 mask1=radices[16==aRadix]; + + PRInt32 charpos=0; + if(anInteger<0) { + theInt*=-1; + if(10==aRadix) { + buf[charpos++]='-'; + } + else theInt=(int)~(theInt-1); } - if (aInteger < 0) - *buffer++ = '-'; - r = ldiv (labs(aInteger), aRadix); - - /* output digits of val/base first */ - if (r.quot > 0) - buffer = ltoa ( r.quot, buf, aRadix); - - /* output last digit */ - int len=strlen(buffer); - buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; - buf[len+1] =0; - -#endif - - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + PRBool isfirst=true; + while(mask1>=1) { + PRInt32 div=theInt/mask1; + if((div) || (!isfirst)) { + buf[charpos++]="0123456789abcdef"[div]; + isfirst=false; + } + theInt-=div*mask1; + mask1/=aRadix; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); - return Append(buf); } diff --git a/mozilla/string/obsolete/nsString.h b/mozilla/string/obsolete/nsString.h index cdb8105aba2..05205d9a818 100644 --- a/mozilla/string/obsolete/nsString.h +++ b/mozilla/string/obsolete/nsString.h @@ -234,7 +234,7 @@ void ToUpperCase(nsCString& aString) const; * @return *this */ nsCString& StripChars(const char* aSet); -nsCString& StripChar(PRUnichar aChar); +nsCString& StripChar(char aChar); /** * This method strips whitespace throughout the string diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index 0d91c8c35af..0373ef90564 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -386,13 +386,9 @@ void nsString::ToUpperCase(nsString& aString) const { * @param aChar -- char to be stripped * @return *this */ -nsString& nsString::StripChar(PRUnichar aChar){ - - PRInt32 theIndex=FindChar(aChar,PR_FALSE,0); - while(kNotFound 36 || aRadix< 2) { /* no conversion if wrong base */ - return *this; + char buf[]={'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + + PRInt32 radices[] = {1000000000,268435456}; + PRInt32 mask1=radices[16==aRadix]; + + PRInt32 charpos=0; + if(anInteger<0) { + theInt*=-1; + if(10==aRadix) { + buf[charpos++]='-'; + } + else theInt=(int)~(theInt-1); } - if (aInteger < 0) - *buffer++ = '-'; - r = ldiv (labs(aInteger), aRadix); - - /* output digits of val/base first */ - if (r.quot > 0) - buffer = ltoa ( r.quot, buf, aRadix); - - /* output last digit */ - int len=strlen(buffer); - buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; - buf[len+1] =0; - -#endif - - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + PRBool isfirst=true; + while(mask1>=1) { + PRInt32 div=theInt/mask1; + if((div) || (!isfirst)) { + buf[charpos++]="0123456789abcdef"[div]; + isfirst=false; + } + theInt-=div*mask1; + mask1/=aRadix; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); - return Append(buf); } diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index fed9e738e6d..80332198c4d 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -269,7 +269,7 @@ void ToUpperCase(nsString& aString) const; * @return *this */ nsString& StripChars(const char* aSet); -nsString& StripChar(PRUnichar aChar); +nsString& StripChar(char aChar); /** * This method strips whitespace throughout the string diff --git a/mozilla/xpcom/ds/bufferRoutines.h b/mozilla/xpcom/ds/bufferRoutines.h index a2e9653c363..1b3cbde84ba 100644 --- a/mozilla/xpcom/ds/bufferRoutines.h +++ b/mozilla/xpcom/ds/bufferRoutines.h @@ -253,16 +253,21 @@ inline PRInt32 FindChar1(const char* aDest,PRUint32 aLength,PRUint32 anOffset,co PRInt32 theLength=(PRInt32)aLength; if(aIgnoreCase) { - PRUnichar theChar=nsCRT::ToUpper(aChar); - for(theIndex=(PRInt32)anOffset;theIndexRelease(); + gCaseConv = 0; } } - return rv; + return NS_OK; } @@ -532,9 +538,7 @@ public: mListener = new HandleCaseConversionShutdown3(); if(mListener){ mListener->AddRef(); - nsresult rv = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, - (nsISupports**) &gCaseConv, mListener); - NS_ASSERTION(NS_SUCCEEDED(rv), "can't get case conversion service"); + nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID,(nsISupports**) &gCaseConv, mListener); } } protected: @@ -689,5 +693,70 @@ PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet){ typedef PRInt32 (*CompressChars)(char* aString,PRUint32 aCount,const char* aSet); CompressChars gCompressChars[]={&CompressChars1,&CompressChars2}; +/** + * This method strips chars in a given set from the given buffer + * + * @update gess 01/04/99 + * @param aString is the buffer to be manipulated + * @param aLength is the length of the buffer + * @param aSet tells us which chars to compress from given buffer + * @param aEliminateLeading tells us whether to strip chars from the start of the buffer + * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer + * @return the new length of the given buffer + */ +PRInt32 StripChars1(char* aString,PRUint32 aLength,const char* aSet){ + + typedef char chartype; + chartype* to = aString; + chartype* from = aString-1; + chartype* end = aString + aLength; + + if(aSet && aString && (0 < aLength)){ + PRUint32 aSetLen=strlen(aSet); + while (++from < end) { + chartype theChar = *from; + if(kNotFound==FindChar1(aSet,aSetLen,0,theChar,PR_FALSE)){ + *to++ = theChar; + } + } + *to = 0; + } + return to - (chartype*)aString; +} + + +/** + * This method strips chars in a given set from the given buffer + * + * @update gess 01/04/99 + * @param aString is the buffer to be manipulated + * @param aLength is the length of the buffer + * @param aSet tells us which chars to compress from given buffer + * @param aEliminateLeading tells us whether to strip chars from the start of the buffer + * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer + * @return the new length of the given buffer + */ +PRInt32 StripChars2(char* aString,PRUint32 aLength,const char* aSet){ + + typedef PRUnichar chartype; + chartype* to = (chartype*)aString; + chartype* from = (chartype*)aString-1; + chartype* end = to + aLength; + + if(aSet && aString && (0 < aLength)){ + PRUint32 aSetLen=strlen(aSet); + while (++from < end) { + chartype theChar = *from; + if(kNotFound==FindChar1(aSet,aSetLen,0,theChar,PR_FALSE)){ + *to++ = theChar; + } + } + *to = 0; + } + return to - (chartype*)aString; +} + +typedef PRInt32 (*StripChars)(char* aString,PRUint32 aCount,const char* aSet); +StripChars gStripChars[]={&StripChars1,&StripChars2}; #endif diff --git a/mozilla/xpcom/ds/nsDeque.cpp b/mozilla/xpcom/ds/nsDeque.cpp index a56f96f39b6..3fff93e140e 100644 --- a/mozilla/xpcom/ds/nsDeque.cpp +++ b/mozilla/xpcom/ds/nsDeque.cpp @@ -78,17 +78,11 @@ nsDeque::~nsDeque() { /** - * Returns the number of elements currently stored in - * this deque. * * @update gess4/18/98 * @param - * @return int contains element count + * @return */ -PRInt32 nsDeque::GetSize(void) const { - return mSize; -} - void nsDeque::SetDeallocator(nsDequeFunctor* aDeallocator){ if(mDeallocator) { delete mDeallocator; diff --git a/mozilla/xpcom/ds/nsDeque.h b/mozilla/xpcom/ds/nsDeque.h index 7f9b51a6082..f73d314f215 100644 --- a/mozilla/xpcom/ds/nsDeque.h +++ b/mozilla/xpcom/ds/nsDeque.h @@ -82,8 +82,7 @@ friend class nsDequeIterator; * @param * @return int contains element count */ - PRInt32 GetSize() const; - + inline PRInt32 GetSize() const { return mSize;} /** * Pushes new member onto the end of the deque diff --git a/mozilla/xpcom/ds/nsStr.cpp b/mozilla/xpcom/ds/nsStr.cpp index 4211cab847b..c34f2cdd7b7 100644 --- a/mozilla/xpcom/ds/nsStr.cpp +++ b/mozilla/xpcom/ds/nsStr.cpp @@ -108,12 +108,15 @@ void nsStr::Destroy(nsStr& aDest,nsIMemoryAgent* anAgent) { * @param aNewLength -- new capacity of string in charSize units * @return void */ -void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { +PRBool nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { + PRBool result=PR_TRUE; if(aNewLength>aString.mCapacity) { nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - theAgent->Realloc(aString,aNewLength); - AddNullTerminator(aString); + result=theAgent->Realloc(aString,aNewLength); + if(aString.mStr) + AddNullTerminator(aString); } + return result; } /** @@ -123,24 +126,28 @@ void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* an * @param aNewLength -- new capacity of string in charSize units * @return void */ -void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { +PRBool nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { + PRBool result=PR_TRUE; if(aNewLength>aDest.mCapacity) { nsStr theTempStr; nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - EnsureCapacity(theTempStr,aNewLength,theAgent); - if(aDest.mLength) { - Append(theTempStr,aDest,0,aDest.mLength,theAgent); - } - theAgent->Free(aDest); - aDest.mStr = theTempStr.mStr; - theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... - aDest.mLength=theTempStr.mLength; - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + result=EnsureCapacity(theTempStr,aNewLength,theAgent); + if(result) { + if(aDest.mLength) { + Append(theTempStr,aDest,0,aDest.mLength,theAgent); + } + theAgent->Free(aDest); + aDest.mStr = theTempStr.mStr; + theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... + aDest.mLength=theTempStr.mLength; + aDest.mCapacity=theTempStr.mCapacity; + aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + } } + return result; } /** @@ -170,15 +177,19 @@ void nsStr::Append(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 a PRUint32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength); PRUint32 theLength=(anOffset+theRealLen aDest.mCapacity) { - GrowCapacity(aDest,aDest.mLength+theLength,anAgent); + isBigEnough=GrowCapacity(aDest,aDest.mLength+theLength,anAgent); } - //now append new chars, starting at offset - (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); + if(isBigEnough) { + //now append new chars, starting at offset + (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); - aDest.mLength+=theLength; - AddNullTerminator(aDest); + aDest.mLength+=theLength; + AddNullTerminator(aDest); + } } } } @@ -213,24 +224,26 @@ void nsStr::Insert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUin nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - EnsureCapacity(theTempStr,aDest.mLength+theLength,theAgent); //grow the temp buffer to the right size + PRBool isBigEnough=EnsureCapacity(theTempStr,aDest.mLength+theLength,theAgent); //grow the temp buffer to the right size - if(aDestOffset) { - Append(theTempStr,aDest,0,aDestOffset,theAgent); //first copy leftmost data... - } + if(isBigEnough) { + if(aDestOffset) { + Append(theTempStr,aDest,0,aDestOffset,theAgent); //first copy leftmost data... + } - Append(theTempStr,aSource,0,aSource.mLength,theAgent); //next copy inserted (new) data + Append(theTempStr,aSource,0,aSource.mLength,theAgent); //next copy inserted (new) data - PRUint32 theRemains=aDest.mLength-aDestOffset; - if(theRemains) { - Append(theTempStr,aDest,aDestOffset,theRemains,theAgent); //next copy rightmost data - } + PRUint32 theRemains=aDest.mLength-aDestOffset; + if(theRemains) { + Append(theTempStr,aDest,aDestOffset,theRemains,theAgent); //next copy rightmost data + } - theAgent->Free(aDest); - aDest.mStr = theTempStr.mStr; - theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + theAgent->Free(aDest); + aDest.mStr = theTempStr.mStr; + theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... + aDest.mCapacity=theTempStr.mCapacity; + aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + } } @@ -365,6 +378,21 @@ void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,P aDest.mLength=aNewLen; } + +/** + * + * @update gess1/7/99 + * @param + * @return + */ +void nsStr::StripChars(nsStr& aDest,const char* aSet){ + if((0 36 || aRadix< 2) { /* no conversion if wrong base */ - return *this; + char buf[]={'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + + PRInt32 radices[] = {1000000000,268435456}; + PRInt32 mask1=radices[16==aRadix]; + + PRInt32 charpos=0; + if(anInteger<0) { + theInt*=-1; + if(10==aRadix) { + buf[charpos++]='-'; + } + else theInt=(int)~(theInt-1); } - if (aInteger < 0) - *buffer++ = '-'; - r = ldiv (labs(aInteger), aRadix); - - /* output digits of val/base first */ - if (r.quot > 0) - buffer = ltoa ( r.quot, buf, aRadix); - - /* output last digit */ - int len=strlen(buffer); - buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; - buf[len+1] =0; - -#endif - - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + PRBool isfirst=true; + while(mask1>=1) { + PRInt32 div=theInt/mask1; + if((div) || (!isfirst)) { + buf[charpos++]="0123456789abcdef"[div]; + isfirst=false; + } + theInt-=div*mask1; + mask1/=aRadix; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); - return Append(buf); } diff --git a/mozilla/xpcom/ds/nsString.h b/mozilla/xpcom/ds/nsString.h index cdb8105aba2..05205d9a818 100644 --- a/mozilla/xpcom/ds/nsString.h +++ b/mozilla/xpcom/ds/nsString.h @@ -234,7 +234,7 @@ void ToUpperCase(nsCString& aString) const; * @return *this */ nsCString& StripChars(const char* aSet); -nsCString& StripChar(PRUnichar aChar); +nsCString& StripChar(char aChar); /** * This method strips whitespace throughout the string diff --git a/mozilla/xpcom/ds/nsString2.cpp b/mozilla/xpcom/ds/nsString2.cpp index 0d91c8c35af..0373ef90564 100644 --- a/mozilla/xpcom/ds/nsString2.cpp +++ b/mozilla/xpcom/ds/nsString2.cpp @@ -386,13 +386,9 @@ void nsString::ToUpperCase(nsString& aString) const { * @param aChar -- char to be stripped * @return *this */ -nsString& nsString::StripChar(PRUnichar aChar){ - - PRInt32 theIndex=FindChar(aChar,PR_FALSE,0); - while(kNotFound 36 || aRadix< 2) { /* no conversion if wrong base */ - return *this; + char buf[]={'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + + PRInt32 radices[] = {1000000000,268435456}; + PRInt32 mask1=radices[16==aRadix]; + + PRInt32 charpos=0; + if(anInteger<0) { + theInt*=-1; + if(10==aRadix) { + buf[charpos++]='-'; + } + else theInt=(int)~(theInt-1); } - if (aInteger < 0) - *buffer++ = '-'; - r = ldiv (labs(aInteger), aRadix); - - /* output digits of val/base first */ - if (r.quot > 0) - buffer = ltoa ( r.quot, buf, aRadix); - - /* output last digit */ - int len=strlen(buffer); - buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; - buf[len+1] =0; - -#endif - - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + PRBool isfirst=true; + while(mask1>=1) { + PRInt32 div=theInt/mask1; + if((div) || (!isfirst)) { + buf[charpos++]="0123456789abcdef"[div]; + isfirst=false; + } + theInt-=div*mask1; + mask1/=aRadix; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); - return Append(buf); } diff --git a/mozilla/xpcom/ds/nsString2.h b/mozilla/xpcom/ds/nsString2.h index fed9e738e6d..80332198c4d 100644 --- a/mozilla/xpcom/ds/nsString2.h +++ b/mozilla/xpcom/ds/nsString2.h @@ -269,7 +269,7 @@ void ToUpperCase(nsString& aString) const; * @return *this */ nsString& StripChars(const char* aSet); -nsString& StripChar(PRUnichar aChar); +nsString& StripChar(char aChar); /** * This method strips whitespace throughout the string diff --git a/mozilla/xpcom/string/obsolete/nsStr.cpp b/mozilla/xpcom/string/obsolete/nsStr.cpp index 4211cab847b..c34f2cdd7b7 100644 --- a/mozilla/xpcom/string/obsolete/nsStr.cpp +++ b/mozilla/xpcom/string/obsolete/nsStr.cpp @@ -108,12 +108,15 @@ void nsStr::Destroy(nsStr& aDest,nsIMemoryAgent* anAgent) { * @param aNewLength -- new capacity of string in charSize units * @return void */ -void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { +PRBool nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { + PRBool result=PR_TRUE; if(aNewLength>aString.mCapacity) { nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - theAgent->Realloc(aString,aNewLength); - AddNullTerminator(aString); + result=theAgent->Realloc(aString,aNewLength); + if(aString.mStr) + AddNullTerminator(aString); } + return result; } /** @@ -123,24 +126,28 @@ void nsStr::EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* an * @param aNewLength -- new capacity of string in charSize units * @return void */ -void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { +PRBool nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgent) { + PRBool result=PR_TRUE; if(aNewLength>aDest.mCapacity) { nsStr theTempStr; nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - EnsureCapacity(theTempStr,aNewLength,theAgent); - if(aDest.mLength) { - Append(theTempStr,aDest,0,aDest.mLength,theAgent); - } - theAgent->Free(aDest); - aDest.mStr = theTempStr.mStr; - theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... - aDest.mLength=theTempStr.mLength; - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + result=EnsureCapacity(theTempStr,aNewLength,theAgent); + if(result) { + if(aDest.mLength) { + Append(theTempStr,aDest,0,aDest.mLength,theAgent); + } + theAgent->Free(aDest); + aDest.mStr = theTempStr.mStr; + theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... + aDest.mLength=theTempStr.mLength; + aDest.mCapacity=theTempStr.mCapacity; + aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + } } + return result; } /** @@ -170,15 +177,19 @@ void nsStr::Append(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 a PRUint32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength); PRUint32 theLength=(anOffset+theRealLen aDest.mCapacity) { - GrowCapacity(aDest,aDest.mLength+theLength,anAgent); + isBigEnough=GrowCapacity(aDest,aDest.mLength+theLength,anAgent); } - //now append new chars, starting at offset - (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); + if(isBigEnough) { + //now append new chars, starting at offset + (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDest.mLength,aSource.mStr,anOffset,theLength); - aDest.mLength+=theLength; - AddNullTerminator(aDest); + aDest.mLength+=theLength; + AddNullTerminator(aDest); + } } } } @@ -213,24 +224,26 @@ void nsStr::Insert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUin nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); - EnsureCapacity(theTempStr,aDest.mLength+theLength,theAgent); //grow the temp buffer to the right size + PRBool isBigEnough=EnsureCapacity(theTempStr,aDest.mLength+theLength,theAgent); //grow the temp buffer to the right size - if(aDestOffset) { - Append(theTempStr,aDest,0,aDestOffset,theAgent); //first copy leftmost data... - } + if(isBigEnough) { + if(aDestOffset) { + Append(theTempStr,aDest,0,aDestOffset,theAgent); //first copy leftmost data... + } - Append(theTempStr,aSource,0,aSource.mLength,theAgent); //next copy inserted (new) data + Append(theTempStr,aSource,0,aSource.mLength,theAgent); //next copy inserted (new) data - PRUint32 theRemains=aDest.mLength-aDestOffset; - if(theRemains) { - Append(theTempStr,aDest,aDestOffset,theRemains,theAgent); //next copy rightmost data - } + PRUint32 theRemains=aDest.mLength-aDestOffset; + if(theRemains) { + Append(theTempStr,aDest,aDestOffset,theRemains,theAgent); //next copy rightmost data + } - theAgent->Free(aDest); - aDest.mStr = theTempStr.mStr; - theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... - aDest.mCapacity=theTempStr.mCapacity; - aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + theAgent->Free(aDest); + aDest.mStr = theTempStr.mStr; + theTempStr.mStr=0; //make sure to null this out so that you don't lose the buffer you just stole... + aDest.mCapacity=theTempStr.mCapacity; + aDest.mOwnsBuffer=theTempStr.mOwnsBuffer; + } } @@ -365,6 +378,21 @@ void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,P aDest.mLength=aNewLen; } + +/** + * + * @update gess1/7/99 + * @param + * @return + */ +void nsStr::StripChars(nsStr& aDest,const char* aSet){ + if((0 36 || aRadix< 2) { /* no conversion if wrong base */ - return *this; + char buf[]={'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + + PRInt32 radices[] = {1000000000,268435456}; + PRInt32 mask1=radices[16==aRadix]; + + PRInt32 charpos=0; + if(anInteger<0) { + theInt*=-1; + if(10==aRadix) { + buf[charpos++]='-'; + } + else theInt=(int)~(theInt-1); } - if (aInteger < 0) - *buffer++ = '-'; - r = ldiv (labs(aInteger), aRadix); - - /* output digits of val/base first */ - if (r.quot > 0) - buffer = ltoa ( r.quot, buf, aRadix); - - /* output last digit */ - int len=strlen(buffer); - buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; - buf[len+1] =0; - -#endif - - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + PRBool isfirst=true; + while(mask1>=1) { + PRInt32 div=theInt/mask1; + if((div) || (!isfirst)) { + buf[charpos++]="0123456789abcdef"[div]; + isfirst=false; + } + theInt-=div*mask1; + mask1/=aRadix; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); - return Append(buf); } diff --git a/mozilla/xpcom/string/obsolete/nsString.h b/mozilla/xpcom/string/obsolete/nsString.h index cdb8105aba2..05205d9a818 100644 --- a/mozilla/xpcom/string/obsolete/nsString.h +++ b/mozilla/xpcom/string/obsolete/nsString.h @@ -234,7 +234,7 @@ void ToUpperCase(nsCString& aString) const; * @return *this */ nsCString& StripChars(const char* aSet); -nsCString& StripChar(PRUnichar aChar); +nsCString& StripChar(char aChar); /** * This method strips whitespace throughout the string diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index 0d91c8c35af..0373ef90564 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -386,13 +386,9 @@ void nsString::ToUpperCase(nsString& aString) const { * @param aChar -- char to be stripped * @return *this */ -nsString& nsString::StripChar(PRUnichar aChar){ - - PRInt32 theIndex=FindChar(aChar,PR_FALSE,0); - while(kNotFound 36 || aRadix< 2) { /* no conversion if wrong base */ - return *this; + char buf[]={'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + + PRInt32 radices[] = {1000000000,268435456}; + PRInt32 mask1=radices[16==aRadix]; + + PRInt32 charpos=0; + if(anInteger<0) { + theInt*=-1; + if(10==aRadix) { + buf[charpos++]='-'; + } + else theInt=(int)~(theInt-1); } - if (aInteger < 0) - *buffer++ = '-'; - r = ldiv (labs(aInteger), aRadix); - - /* output digits of val/base first */ - if (r.quot > 0) - buffer = ltoa ( r.quot, buf, aRadix); - - /* output last digit */ - int len=strlen(buffer); - buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; - buf[len+1] =0; - -#endif - - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + PRBool isfirst=true; + while(mask1>=1) { + PRInt32 div=theInt/mask1; + if((div) || (!isfirst)) { + buf[charpos++]="0123456789abcdef"[div]; + isfirst=false; + } + theInt-=div*mask1; + mask1/=aRadix; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); - return Append(buf); } diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index fed9e738e6d..80332198c4d 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -269,7 +269,7 @@ void ToUpperCase(nsString& aString) const; * @return *this */ nsString& StripChars(const char* aSet); -nsString& StripChar(PRUnichar aChar); +nsString& StripChar(char aChar); /** * This method strips whitespace throughout the string