diff --git a/mozilla/string/obsolete/nsStr.cpp b/mozilla/string/obsolete/nsStr.cpp index 5974b5c5c83..3097e2ad5fd 100644 --- a/mozilla/string/obsolete/nsStr.cpp +++ b/mozilla/string/obsolete/nsStr.cpp @@ -8,14 +8,14 @@ * Software distributed under the NPL is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL * for the specific language governing rights and limitations under the - * NPL. + * NPL. * * The Initial Developer of this code under the NPL is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ - + /****************************************************************************************** MODULE NOTES: @@ -98,7 +98,7 @@ public: aDest.mStr=new char[theSize]; } aDest.mOwnsBuffer=1; - return PR_TRUE; + return PRBool(aDest.mStr!=0); } @@ -239,7 +239,7 @@ 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.mCharSize); + nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); EnsureCapacity(theTempStr,aNewLength,theAgent); @@ -265,8 +265,10 @@ void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgen * @param aCount is the number of chars copied from aSource */ void nsStr::Assign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount,nsIMemoryAgent* anAgent){ - Truncate(aDest,0,anAgent); - Append(aDest,aSource,anOffset,aCount,anAgent); + if(&aDest!=&aSource){ + Truncate(aDest,0,anAgent); + Append(aDest,aSource,anOffset,aCount,anAgent); + } } /** @@ -323,7 +325,7 @@ void nsStr::Insert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUin GrowCapacity(aDest,aDest.mLength+theLength,anAgent); //shift the chars right by theDelta... - (*gShiftChars[aDest.mCharSize][PR_TRUE])(aDest.mStr,aDest.mLength,aDestOffset,theLength); + (*gShiftChars[aDest.mCharSize][KSHIFTRIGHT])(aDest.mStr,aDest.mLength,aDestOffset,theLength); //now insert new chars, starting at offset (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); @@ -360,7 +362,7 @@ void nsStr::Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount,nsIMemoryAg //if you're here, it means we're cutting chars out of the middle of the string... //so shift the chars left by theLength... - (*gShiftChars[aDest.mCharSize][PR_FALSE])(aDest.mStr,aDest.mLength,aDestOffset,theLength); + (*gShiftChars[aDest.mCharSize][KSHIFTLEFT])(aDest.mStr,aDest.mLength,aDestOffset,theLength); aDest.mLength-=theLength; } else Truncate(aDest,aDestOffset,anAgent); @@ -440,31 +442,48 @@ void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRUint32 aChar,PRBool aEli PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRUint32 anOffset) { if((aDest.mLength>0) && (aTarget.mLength>0) && (anOffset=0) && (GetCharAt(aDest,theBufIndex)==GetCharAt(aTarget,thePatIndex))){ - --theBufIndex; - --thePatIndex; + int iBuf =theIndex; + int iPat=aTarget.mLength-1; + + PRBool matches=PR_TRUE; + while((iPat>=0) && (matches)){ + PRUnichar theTargetChar=GetCharAt(theCopy,iPat); + PRUnichar theDestChar=GetCharAt(aDest,iBuf); + if(aIgnoreCase) + theDestChar=nsCRT::ToLower(theDestChar); + matches=PRBool(theTargetChar==theDestChar); + if(matches){ + --iBuf; + --iPat; + } } - if(-1==thePatIndex){ - return anOffset+theBufIndex+1; + if(-1==iPat){ + return anOffset+iBuf+1; } - } + } //for + nsStr::Destroy(theCopy,0); }//if return kNotFound; } @@ -509,41 +528,38 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRUint32 anOffset) { PRInt32 index=(anOffset ? anOffset : aDest.mLength-aTarget.mLength+1); - if((aDest.mLength>0) && (aTarget.mLength>0)){ - PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); - PRInt32 theTargetMax=aTarget.mLength; + PRInt32 result=kNotFound; - while(index--) { - PRInt32 theSubIndex=-1; - PRBool matches=PR_TRUE; + if((aDest.mLength>0) && (aTarget.mLength>0)){ + + nsStr theCopy; + nsStr::Initialize(theCopy,eOneByte); + nsStr::Assign(theCopy,aTarget,0,aTarget.mLength,0); + if(aIgnoreCase){ + nsStr::ChangeCase(theCopy,false); //force to lowercase + } - if(anOffset+aTarget.mLength<=aDest.mLength) { + int32 theTargetMax=theCopy.mLength; + while(index--) { + int32 theSubIndex=-1; + PRBool matches=PR_TRUE; + if(anOffset+theCopy.mLength<=aDest.mLength) { while((++theSubIndex0) { - if(theFirstTargetChar==theChar){ - 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=GetCharAt(aTarget,theSubIndex); - matches=PRBool(theChar==theTargetChar); + PRUnichar theDestChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex); + PRUnichar theTargetChar=GetCharAt(theCopy,theSubIndex); + matches=PRBool(theDestChar==theTargetChar); } //while } //if - if(matches) - return index; - if(-1>aStr.mCharSize)-1,0,(eCharSize)aStr.mCharSize,PR_FALSE); +nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2(aStr.mCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,aStr.mCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); Assign(aCString); @@ -1776,7 +1801,7 @@ nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2((eCha * 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) { +nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsString2(aCharSize) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); @@ -1881,7 +1906,7 @@ nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aC * @update gess 1/4/99 * @param reference to a subsumeString */ -nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2((eCharSize)aSubsumeStr.mCharSize) { +nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { mAgent=0; Subsume(*this,aSubsumeStr); } @@ -1900,11 +1925,11 @@ void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(mCapacity << mCharSize); } -nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2((eCharSize)aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2(aString.mCharSize) { Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2(aString.mCharSize) { Subsume(*this,aString); } @@ -1913,13 +1938,26 @@ nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLe mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } - + nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { mStr=aString; mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } +/*********************************************************************** + IMPLEMENTATION of nsCAutoString, which is a vanilla string class that + only ever stores 1 byte character strings. Typically you'll use this + class to hold a pointer to a char*, which comes from an nsString. + ***********************************************************************/ +nsCAutoString::nsCAutoString(const nsString2& aString) : nsAutoString(aString,eOneByte){ +} + +nsCAutoString::operator const char*() const { + return (const char*)mStr; +} + + #ifdef RICKG_DEBUG /*********************************************************************** @@ -1939,6 +1977,21 @@ CStringTester::CStringTester() { nsString2 theString0("foo",theSize); //watch it construct and destruct } + //Let's test our autoCStrings... + { + nsString2 theString("hello"); + nsCAutoString theCStr(theString); + } + + //now let's test out various features of out SubsumeStrs... + { + nsString2 theString("hello"); + + nsString2 theString2=theString+" there!"; //a subsumestr should be constructed and returned by operator+() + nsSubsumeStr theSubStr=theString2.left(5); + int x=5; + } + { //this test makes sure that autostrings who assume ownership of a buffer, //don't also try to copy that buffer onto itself... (was a bug) diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index f5d5063871c..a1fe3bf894f 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -356,7 +356,7 @@ PRUnichar* ToNewUnicode() const; * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer */ -char* ToCString(char* aBuf,PRUint32 aBufLength) const; +char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const; /** * Perform string to float conversion. @@ -725,7 +725,7 @@ virtual void DebugDump(ostream& aStream) const; }; extern NS_COM int fputs(const nsString2& aString, FILE* out); -ostream& operator<<(ostream& os,nsString2& aString); +ostream& operator<<(ostream& aStream,const nsString2& aString); /************************************************************** @@ -792,6 +792,17 @@ public: nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; + +/*************************************************************** + + ***************************************************************/ +class NS_COM nsCAutoString: public nsAutoString{ +public: + nsCAutoString(const nsString2& aString); + operator const char*() const; +}; + + #endif diff --git a/mozilla/xpcom/ds/bufferRoutines.h b/mozilla/xpcom/ds/bufferRoutines.h index b663dc375d6..260cdb1c096 100644 --- a/mozilla/xpcom/ds/bufferRoutines.h +++ b/mozilla/xpcom/ds/bufferRoutines.h @@ -37,6 +37,9 @@ #include "nsICaseConversion.h" #endif +#define KSHIFTLEFT (0) +#define KSHIFTRIGHT (1) + inline PRUnichar GetUnicharAt(const char* aString,PRUint32 anIndex) { return ((PRUnichar*)aString)[anIndex]; @@ -226,7 +229,7 @@ void CopyChars2To1(char* aDest,PRInt32 anDestOffset,const char* aSource,PRUint32 //now loop over characters, shifting them left... while(first=0;theIndex--){ + PRInt32 theIndex=0; + PRInt32 thePos=(PRInt32)aLength-anOffset-1; + for(theIndex=thePos;theIndex>=0;theIndex--){ PRUnichar theChar=GetCharAt(aDest,theIndex); if(aIgnoreCase) theChar=nsCRT::ToUpper(theChar); @@ -345,7 +350,6 @@ inline PRInt32 RFindChar1(const char* aDest,PRUint32 aLength,PRUint32 anOffset,c } - /** * This methods cans the given buffer for the given char * @@ -360,7 +364,8 @@ inline PRInt32 RFindChar1(const char* aDest,PRUint32 aLength,PRUint32 anOffset,c inline PRInt32 RFindChar2(const char* aDest,PRUint32 aLength,PRUint32 anOffset,const PRUnichar aChar,PRBool aIgnoreCase) { PRUnichar theCmpChar=(aIgnoreCase ? nsCRT::ToUpper(aChar) : aChar); PRInt32 theIndex=0; - for(theIndex=aLength-1;theIndex>=0;theIndex--){ + PRInt32 thePos=(PRInt32)aLength-anOffset-1; + for(theIndex=thePos;theIndex>=0;theIndex--){ PRUnichar theChar=GetUnicharAt(aDest,theIndex); if(aIgnoreCase) theChar=nsCRT::ToUpper(theChar); @@ -449,7 +454,7 @@ PRInt32 Compare1To2(const char* aStr1,const char* aStr2,PRUint32 aCount,PRBool a PRInt32 result; if(aIgnoreCase) result=nsCRT::strncasecmp((PRUnichar*)aStr2,aStr1,aCount)*-1; - else result=nsCRT::strncasecmp((PRUnichar*)aStr2,aStr1,aCount)*-1; + else result=nsCRT::strncmp((PRUnichar*)aStr2,aStr1,aCount)*-1; return result; } @@ -821,7 +826,7 @@ PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet,PRUint32 while (from <= end) { chartype ch = *from++; if(kNotFound!=FindChar1(aSet,aSetLen,0,ch,PR_FALSE)){ - *to++ = ' '; + *to++ = (char)aChar; while (from <= end) { ch = *from++; if(kNotFound==FindChar1(aSet,aSetLen,0,ch,PR_FALSE)){ @@ -867,7 +872,7 @@ PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet,PRUint32 while (from <= end) { chartype ch = *from++; if(kNotFound!=FindChar1(aSet,aSetLen,0,ch,PR_FALSE)){ - *to++ = ' '; + *to++ = (PRUnichar)aChar; while (from <= end) { ch = *from++; if(kNotFound==FindChar1(aSet,aSetLen,0,ch,PR_FALSE)){ diff --git a/mozilla/xpcom/ds/nsStr.cpp b/mozilla/xpcom/ds/nsStr.cpp index 5974b5c5c83..3097e2ad5fd 100644 --- a/mozilla/xpcom/ds/nsStr.cpp +++ b/mozilla/xpcom/ds/nsStr.cpp @@ -8,14 +8,14 @@ * Software distributed under the NPL is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL * for the specific language governing rights and limitations under the - * NPL. + * NPL. * * The Initial Developer of this code under the NPL is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ - + /****************************************************************************************** MODULE NOTES: @@ -98,7 +98,7 @@ public: aDest.mStr=new char[theSize]; } aDest.mOwnsBuffer=1; - return PR_TRUE; + return PRBool(aDest.mStr!=0); } @@ -239,7 +239,7 @@ 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.mCharSize); + nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); EnsureCapacity(theTempStr,aNewLength,theAgent); @@ -265,8 +265,10 @@ void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgen * @param aCount is the number of chars copied from aSource */ void nsStr::Assign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount,nsIMemoryAgent* anAgent){ - Truncate(aDest,0,anAgent); - Append(aDest,aSource,anOffset,aCount,anAgent); + if(&aDest!=&aSource){ + Truncate(aDest,0,anAgent); + Append(aDest,aSource,anOffset,aCount,anAgent); + } } /** @@ -323,7 +325,7 @@ void nsStr::Insert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUin GrowCapacity(aDest,aDest.mLength+theLength,anAgent); //shift the chars right by theDelta... - (*gShiftChars[aDest.mCharSize][PR_TRUE])(aDest.mStr,aDest.mLength,aDestOffset,theLength); + (*gShiftChars[aDest.mCharSize][KSHIFTRIGHT])(aDest.mStr,aDest.mLength,aDestOffset,theLength); //now insert new chars, starting at offset (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); @@ -360,7 +362,7 @@ void nsStr::Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount,nsIMemoryAg //if you're here, it means we're cutting chars out of the middle of the string... //so shift the chars left by theLength... - (*gShiftChars[aDest.mCharSize][PR_FALSE])(aDest.mStr,aDest.mLength,aDestOffset,theLength); + (*gShiftChars[aDest.mCharSize][KSHIFTLEFT])(aDest.mStr,aDest.mLength,aDestOffset,theLength); aDest.mLength-=theLength; } else Truncate(aDest,aDestOffset,anAgent); @@ -440,31 +442,48 @@ void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRUint32 aChar,PRBool aEli PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRUint32 anOffset) { if((aDest.mLength>0) && (aTarget.mLength>0) && (anOffset=0) && (GetCharAt(aDest,theBufIndex)==GetCharAt(aTarget,thePatIndex))){ - --theBufIndex; - --thePatIndex; + int iBuf =theIndex; + int iPat=aTarget.mLength-1; + + PRBool matches=PR_TRUE; + while((iPat>=0) && (matches)){ + PRUnichar theTargetChar=GetCharAt(theCopy,iPat); + PRUnichar theDestChar=GetCharAt(aDest,iBuf); + if(aIgnoreCase) + theDestChar=nsCRT::ToLower(theDestChar); + matches=PRBool(theTargetChar==theDestChar); + if(matches){ + --iBuf; + --iPat; + } } - if(-1==thePatIndex){ - return anOffset+theBufIndex+1; + if(-1==iPat){ + return anOffset+iBuf+1; } - } + } //for + nsStr::Destroy(theCopy,0); }//if return kNotFound; } @@ -509,41 +528,38 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRUint32 anOffset) { PRInt32 index=(anOffset ? anOffset : aDest.mLength-aTarget.mLength+1); - if((aDest.mLength>0) && (aTarget.mLength>0)){ - PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); - PRInt32 theTargetMax=aTarget.mLength; + PRInt32 result=kNotFound; - while(index--) { - PRInt32 theSubIndex=-1; - PRBool matches=PR_TRUE; + if((aDest.mLength>0) && (aTarget.mLength>0)){ + + nsStr theCopy; + nsStr::Initialize(theCopy,eOneByte); + nsStr::Assign(theCopy,aTarget,0,aTarget.mLength,0); + if(aIgnoreCase){ + nsStr::ChangeCase(theCopy,false); //force to lowercase + } - if(anOffset+aTarget.mLength<=aDest.mLength) { + int32 theTargetMax=theCopy.mLength; + while(index--) { + int32 theSubIndex=-1; + PRBool matches=PR_TRUE; + if(anOffset+theCopy.mLength<=aDest.mLength) { while((++theSubIndex0) { - if(theFirstTargetChar==theChar){ - 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=GetCharAt(aTarget,theSubIndex); - matches=PRBool(theChar==theTargetChar); + PRUnichar theDestChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex); + PRUnichar theTargetChar=GetCharAt(theCopy,theSubIndex); + matches=PRBool(theDestChar==theTargetChar); } //while } //if - if(matches) - return index; - if(-1>aStr.mCharSize)-1,0,(eCharSize)aStr.mCharSize,PR_FALSE); +nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2(aStr.mCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,aStr.mCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); Assign(aCString); @@ -1776,7 +1801,7 @@ nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2((eCha * 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) { +nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsString2(aCharSize) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); @@ -1881,7 +1906,7 @@ nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aC * @update gess 1/4/99 * @param reference to a subsumeString */ -nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2((eCharSize)aSubsumeStr.mCharSize) { +nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { mAgent=0; Subsume(*this,aSubsumeStr); } @@ -1900,11 +1925,11 @@ void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(mCapacity << mCharSize); } -nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2((eCharSize)aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2(aString.mCharSize) { Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2(aString.mCharSize) { Subsume(*this,aString); } @@ -1913,13 +1938,26 @@ nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLe mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } - + nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { mStr=aString; mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } +/*********************************************************************** + IMPLEMENTATION of nsCAutoString, which is a vanilla string class that + only ever stores 1 byte character strings. Typically you'll use this + class to hold a pointer to a char*, which comes from an nsString. + ***********************************************************************/ +nsCAutoString::nsCAutoString(const nsString2& aString) : nsAutoString(aString,eOneByte){ +} + +nsCAutoString::operator const char*() const { + return (const char*)mStr; +} + + #ifdef RICKG_DEBUG /*********************************************************************** @@ -1939,6 +1977,21 @@ CStringTester::CStringTester() { nsString2 theString0("foo",theSize); //watch it construct and destruct } + //Let's test our autoCStrings... + { + nsString2 theString("hello"); + nsCAutoString theCStr(theString); + } + + //now let's test out various features of out SubsumeStrs... + { + nsString2 theString("hello"); + + nsString2 theString2=theString+" there!"; //a subsumestr should be constructed and returned by operator+() + nsSubsumeStr theSubStr=theString2.left(5); + int x=5; + } + { //this test makes sure that autostrings who assume ownership of a buffer, //don't also try to copy that buffer onto itself... (was a bug) diff --git a/mozilla/xpcom/ds/nsString2.h b/mozilla/xpcom/ds/nsString2.h index f5d5063871c..a1fe3bf894f 100644 --- a/mozilla/xpcom/ds/nsString2.h +++ b/mozilla/xpcom/ds/nsString2.h @@ -356,7 +356,7 @@ PRUnichar* ToNewUnicode() const; * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer */ -char* ToCString(char* aBuf,PRUint32 aBufLength) const; +char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const; /** * Perform string to float conversion. @@ -725,7 +725,7 @@ virtual void DebugDump(ostream& aStream) const; }; extern NS_COM int fputs(const nsString2& aString, FILE* out); -ostream& operator<<(ostream& os,nsString2& aString); +ostream& operator<<(ostream& aStream,const nsString2& aString); /************************************************************** @@ -792,6 +792,17 @@ public: nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; + +/*************************************************************** + + ***************************************************************/ +class NS_COM nsCAutoString: public nsAutoString{ +public: + nsCAutoString(const nsString2& aString); + operator const char*() const; +}; + + #endif diff --git a/mozilla/xpcom/string/obsolete/nsStr.cpp b/mozilla/xpcom/string/obsolete/nsStr.cpp index 5974b5c5c83..3097e2ad5fd 100644 --- a/mozilla/xpcom/string/obsolete/nsStr.cpp +++ b/mozilla/xpcom/string/obsolete/nsStr.cpp @@ -8,14 +8,14 @@ * Software distributed under the NPL is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL * for the specific language governing rights and limitations under the - * NPL. + * NPL. * * The Initial Developer of this code under the NPL is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ - + /****************************************************************************************** MODULE NOTES: @@ -98,7 +98,7 @@ public: aDest.mStr=new char[theSize]; } aDest.mOwnsBuffer=1; - return PR_TRUE; + return PRBool(aDest.mStr!=0); } @@ -239,7 +239,7 @@ 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.mCharSize); + nsStr::Initialize(theTempStr,aDest.mCharSize); nsIMemoryAgent* theAgent=(anAgent) ? anAgent : GetDefaultAgent(); EnsureCapacity(theTempStr,aNewLength,theAgent); @@ -265,8 +265,10 @@ void nsStr::GrowCapacity(nsStr& aDest,PRUint32 aNewLength,nsIMemoryAgent* anAgen * @param aCount is the number of chars copied from aSource */ void nsStr::Assign(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt32 aCount,nsIMemoryAgent* anAgent){ - Truncate(aDest,0,anAgent); - Append(aDest,aSource,anOffset,aCount,anAgent); + if(&aDest!=&aSource){ + Truncate(aDest,0,anAgent); + Append(aDest,aSource,anOffset,aCount,anAgent); + } } /** @@ -323,7 +325,7 @@ void nsStr::Insert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUin GrowCapacity(aDest,aDest.mLength+theLength,anAgent); //shift the chars right by theDelta... - (*gShiftChars[aDest.mCharSize][PR_TRUE])(aDest.mStr,aDest.mLength,aDestOffset,theLength); + (*gShiftChars[aDest.mCharSize][KSHIFTRIGHT])(aDest.mStr,aDest.mLength,aDestOffset,theLength); //now insert new chars, starting at offset (*gCopyChars[aSource.mCharSize][aDest.mCharSize])(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); @@ -360,7 +362,7 @@ void nsStr::Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount,nsIMemoryAg //if you're here, it means we're cutting chars out of the middle of the string... //so shift the chars left by theLength... - (*gShiftChars[aDest.mCharSize][PR_FALSE])(aDest.mStr,aDest.mLength,aDestOffset,theLength); + (*gShiftChars[aDest.mCharSize][KSHIFTLEFT])(aDest.mStr,aDest.mLength,aDestOffset,theLength); aDest.mLength-=theLength; } else Truncate(aDest,aDestOffset,anAgent); @@ -440,31 +442,48 @@ void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRUint32 aChar,PRBool aEli PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRUint32 anOffset) { if((aDest.mLength>0) && (aTarget.mLength>0) && (anOffset=0) && (GetCharAt(aDest,theBufIndex)==GetCharAt(aTarget,thePatIndex))){ - --theBufIndex; - --thePatIndex; + int iBuf =theIndex; + int iPat=aTarget.mLength-1; + + PRBool matches=PR_TRUE; + while((iPat>=0) && (matches)){ + PRUnichar theTargetChar=GetCharAt(theCopy,iPat); + PRUnichar theDestChar=GetCharAt(aDest,iBuf); + if(aIgnoreCase) + theDestChar=nsCRT::ToLower(theDestChar); + matches=PRBool(theTargetChar==theDestChar); + if(matches){ + --iBuf; + --iPat; + } } - if(-1==thePatIndex){ - return anOffset+theBufIndex+1; + if(-1==iPat){ + return anOffset+iBuf+1; } - } + } //for + nsStr::Destroy(theCopy,0); }//if return kNotFound; } @@ -509,41 +528,38 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRUint32 anOffset) { PRInt32 index=(anOffset ? anOffset : aDest.mLength-aTarget.mLength+1); - if((aDest.mLength>0) && (aTarget.mLength>0)){ - PRInt32 theNewStartPos=-1; - PRUnichar theFirstTargetChar=GetCharAt(aTarget,0); - PRUnichar theLastTargetChar=GetCharAt(aTarget,aTarget.mLength-1); - PRInt32 theTargetMax=aTarget.mLength; + PRInt32 result=kNotFound; - while(index--) { - PRInt32 theSubIndex=-1; - PRBool matches=PR_TRUE; + if((aDest.mLength>0) && (aTarget.mLength>0)){ + + nsStr theCopy; + nsStr::Initialize(theCopy,eOneByte); + nsStr::Assign(theCopy,aTarget,0,aTarget.mLength,0); + if(aIgnoreCase){ + nsStr::ChangeCase(theCopy,false); //force to lowercase + } - if(anOffset+aTarget.mLength<=aDest.mLength) { + int32 theTargetMax=theCopy.mLength; + while(index--) { + int32 theSubIndex=-1; + PRBool matches=PR_TRUE; + if(anOffset+theCopy.mLength<=aDest.mLength) { while((++theSubIndex0) { - if(theFirstTargetChar==theChar){ - 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=GetCharAt(aTarget,theSubIndex); - matches=PRBool(theChar==theTargetChar); + PRUnichar theDestChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex); + PRUnichar theTargetChar=GetCharAt(theCopy,theSubIndex); + matches=PRBool(theDestChar==theTargetChar); } //while } //if - if(matches) - return index; - if(-1>aStr.mCharSize)-1,0,(eCharSize)aStr.mCharSize,PR_FALSE); +nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2(aStr.mCharSize) { + nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aStr.mCharSize)-1,0,aStr.mCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); Assign(aCString); @@ -1776,7 +1801,7 @@ nsAutoString2::nsAutoString2(nsStr& aStr,const char* aCString) : nsString2((eCha * 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) { +nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsString2(aCharSize) { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); mAgent=0; AddNullTerminator(*this); @@ -1881,7 +1906,7 @@ nsAutoString2::nsAutoString2(PRUnichar aChar,eCharSize aCharSize) : nsString2(aC * @update gess 1/4/99 * @param reference to a subsumeString */ -nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2((eCharSize)aSubsumeStr.mCharSize) { +nsAutoString2::nsAutoString2( nsSubsumeStr& aSubsumeStr) :nsString2(aSubsumeStr.mCharSize) { mAgent=0; Subsume(*this,aSubsumeStr); } @@ -1900,11 +1925,11 @@ void nsAutoString2::SizeOf(nsISizeOfHandler* aHandler) const { aHandler->Add(mCapacity << mCharSize); } -nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2((eCharSize)aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsString2& aString) : nsString2(aString.mCharSize) { Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharSize) { +nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2(aString.mCharSize) { Subsume(*this,aString); } @@ -1913,13 +1938,26 @@ nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLe mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } - + nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { mStr=aString; mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; mOwnsBuffer=assumeOwnership; } +/*********************************************************************** + IMPLEMENTATION of nsCAutoString, which is a vanilla string class that + only ever stores 1 byte character strings. Typically you'll use this + class to hold a pointer to a char*, which comes from an nsString. + ***********************************************************************/ +nsCAutoString::nsCAutoString(const nsString2& aString) : nsAutoString(aString,eOneByte){ +} + +nsCAutoString::operator const char*() const { + return (const char*)mStr; +} + + #ifdef RICKG_DEBUG /*********************************************************************** @@ -1939,6 +1977,21 @@ CStringTester::CStringTester() { nsString2 theString0("foo",theSize); //watch it construct and destruct } + //Let's test our autoCStrings... + { + nsString2 theString("hello"); + nsCAutoString theCStr(theString); + } + + //now let's test out various features of out SubsumeStrs... + { + nsString2 theString("hello"); + + nsString2 theString2=theString+" there!"; //a subsumestr should be constructed and returned by operator+() + nsSubsumeStr theSubStr=theString2.left(5); + int x=5; + } + { //this test makes sure that autostrings who assume ownership of a buffer, //don't also try to copy that buffer onto itself... (was a bug) diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index f5d5063871c..a1fe3bf894f 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -356,7 +356,7 @@ PRUnichar* ToNewUnicode() const; * @param aBuflength is the max # of chars to move to buffer * @return ptr to given buffer */ -char* ToCString(char* aBuf,PRUint32 aBufLength) const; +char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const; /** * Perform string to float conversion. @@ -725,7 +725,7 @@ virtual void DebugDump(ostream& aStream) const; }; extern NS_COM int fputs(const nsString2& aString, FILE* out); -ostream& operator<<(ostream& os,nsString2& aString); +ostream& operator<<(ostream& aStream,const nsString2& aString); /************************************************************** @@ -792,6 +792,17 @@ public: nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; + +/*************************************************************** + + ***************************************************************/ +class NS_COM nsCAutoString: public nsAutoString{ +public: + nsCAutoString(const nsString2& aString); + operator const char*() const; +}; + + #endif