diff --git a/mozilla/string/obsolete/nsStr.cpp b/mozilla/string/obsolete/nsStr.cpp index 28e641f585c..e099759c818 100644 --- a/mozilla/string/obsolete/nsStr.cpp +++ b/mozilla/string/obsolete/nsStr.cpp @@ -438,23 +438,31 @@ 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) { - PRInt32 index=anOffset-1; - PRInt32 theMax=aDest.mLength-aTarget.mLength; - 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; - while(++index<=theMax) { - PRInt32 theSubIndex=-1; - PRBool matches=PR_TRUE; - while((++theSubIndex0) && (aTarget.mLength>0) && (anOffset= 0 && GetCharAt(aDest,theBufIndex)==GetCharAt(aTarget,thePatIndex)){ + --theBufIndex; + --thePatIndex; } - if(matches) { - return index; + if(-1==thePatIndex){ + return anOffset+theBufIndex+1; } } }//if @@ -514,12 +522,22 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgno if(anOffset+aTarget.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); } //while } //if - if(matches) { + if(matches) return index; + if(-1>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aCString); } - AddNullTerminator(*this); - Assign(aCString); } /** @@ -1814,16 +1816,18 @@ nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsS * 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) { +nsAutoString2::nsAutoString2(PRUnichar* aString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { mAgent=0; - if(assumeOwnership) { - nsStr::Initialize(*this,(char*)aString,aCapacity,0,eTwoByte,PR_TRUE); + if(assumeOwnership && aString) { + aCapacity = (-1==aCapacity) ? nsCRT::strlen(aString) : aCapacity-1; + nsStr::Initialize(*this,(char*)aString,aCapacity,aCapacity,eTwoByte,PR_TRUE); + AddNullTerminator(*this); } else { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aString); } - AddNullTerminator(*this); - Assign(aString); } @@ -1904,12 +1908,19 @@ nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharS Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(const PRUnichar* aString) : nsString2(aString,eTwoByte) { +nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eTwoByte) { + mUStr=aString; + mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; } -nsSubsumeStr::nsSubsumeStr(const char* aString) : nsString2(aString,eOneByte) { +nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { + mStr=aString; + mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; } + #ifdef RICKG_DEBUG /*********************************************************************** IMPLEMENTATION of CStringTester... @@ -1928,6 +1939,14 @@ CStringTester::CStringTester() { nsString2 theString0("foo",theSize); //watch it construct and destruct } + { + //this test makes sure that autostrings who assume ownership of a buffer, + //don't also try to copy that buffer onto itself... (was a bug) + char* theStr="hello rick"; + nsAutoString2(theStr,5,eOneByte,PR_FALSE); + + } + { nsString2 theString("hello"); nsString2 temp1=theString+" there!"; diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index 4d9bab7bcab..bd7b74fad1d 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -52,8 +52,8 @@ class nsISizeOfHandler; #define nsAutoString2 nsAutoString #endif -class NS_COM nsSubsumeStr; +class NS_COM nsSubsumeStr; class NS_COM nsString2 : public nsStr { public: @@ -744,9 +744,9 @@ public: nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(char* aCString,PRUint32 aLength,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); + nsAutoString2(char* aCString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); nsAutoString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(PRUnichar* aString,PRUint32 aLength,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); + nsAutoString2(PRUnichar* aString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); @@ -790,8 +790,8 @@ class NS_COM nsSubsumeStr : public nsString2 { public: nsSubsumeStr(nsString2& aString); nsSubsumeStr(nsStr& aString); - nsSubsumeStr(const PRUnichar* aString); - nsSubsumeStr(const char* aString); + nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); + nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; #endif diff --git a/mozilla/xpcom/ds/nsStr.cpp b/mozilla/xpcom/ds/nsStr.cpp index 28e641f585c..e099759c818 100644 --- a/mozilla/xpcom/ds/nsStr.cpp +++ b/mozilla/xpcom/ds/nsStr.cpp @@ -438,23 +438,31 @@ 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) { - PRInt32 index=anOffset-1; - PRInt32 theMax=aDest.mLength-aTarget.mLength; - 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; - while(++index<=theMax) { - PRInt32 theSubIndex=-1; - PRBool matches=PR_TRUE; - while((++theSubIndex0) && (aTarget.mLength>0) && (anOffset= 0 && GetCharAt(aDest,theBufIndex)==GetCharAt(aTarget,thePatIndex)){ + --theBufIndex; + --thePatIndex; } - if(matches) { - return index; + if(-1==thePatIndex){ + return anOffset+theBufIndex+1; } } }//if @@ -514,12 +522,22 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgno if(anOffset+aTarget.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); } //while } //if - if(matches) { + if(matches) return index; + if(-1>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aCString); } - AddNullTerminator(*this); - Assign(aCString); } /** @@ -1814,16 +1816,18 @@ nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsS * 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) { +nsAutoString2::nsAutoString2(PRUnichar* aString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { mAgent=0; - if(assumeOwnership) { - nsStr::Initialize(*this,(char*)aString,aCapacity,0,eTwoByte,PR_TRUE); + if(assumeOwnership && aString) { + aCapacity = (-1==aCapacity) ? nsCRT::strlen(aString) : aCapacity-1; + nsStr::Initialize(*this,(char*)aString,aCapacity,aCapacity,eTwoByte,PR_TRUE); + AddNullTerminator(*this); } else { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aString); } - AddNullTerminator(*this); - Assign(aString); } @@ -1904,12 +1908,19 @@ nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharS Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(const PRUnichar* aString) : nsString2(aString,eTwoByte) { +nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eTwoByte) { + mUStr=aString; + mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; } -nsSubsumeStr::nsSubsumeStr(const char* aString) : nsString2(aString,eOneByte) { +nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { + mStr=aString; + mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; } + #ifdef RICKG_DEBUG /*********************************************************************** IMPLEMENTATION of CStringTester... @@ -1928,6 +1939,14 @@ CStringTester::CStringTester() { nsString2 theString0("foo",theSize); //watch it construct and destruct } + { + //this test makes sure that autostrings who assume ownership of a buffer, + //don't also try to copy that buffer onto itself... (was a bug) + char* theStr="hello rick"; + nsAutoString2(theStr,5,eOneByte,PR_FALSE); + + } + { nsString2 theString("hello"); nsString2 temp1=theString+" there!"; diff --git a/mozilla/xpcom/ds/nsString2.h b/mozilla/xpcom/ds/nsString2.h index 4d9bab7bcab..bd7b74fad1d 100644 --- a/mozilla/xpcom/ds/nsString2.h +++ b/mozilla/xpcom/ds/nsString2.h @@ -52,8 +52,8 @@ class nsISizeOfHandler; #define nsAutoString2 nsAutoString #endif -class NS_COM nsSubsumeStr; +class NS_COM nsSubsumeStr; class NS_COM nsString2 : public nsStr { public: @@ -744,9 +744,9 @@ public: nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(char* aCString,PRUint32 aLength,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); + nsAutoString2(char* aCString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); nsAutoString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(PRUnichar* aString,PRUint32 aLength,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); + nsAutoString2(PRUnichar* aString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); @@ -790,8 +790,8 @@ class NS_COM nsSubsumeStr : public nsString2 { public: nsSubsumeStr(nsString2& aString); nsSubsumeStr(nsStr& aString); - nsSubsumeStr(const PRUnichar* aString); - nsSubsumeStr(const char* aString); + nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); + nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; #endif diff --git a/mozilla/xpcom/string/obsolete/nsStr.cpp b/mozilla/xpcom/string/obsolete/nsStr.cpp index 28e641f585c..e099759c818 100644 --- a/mozilla/xpcom/string/obsolete/nsStr.cpp +++ b/mozilla/xpcom/string/obsolete/nsStr.cpp @@ -438,23 +438,31 @@ 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) { - PRInt32 index=anOffset-1; - PRInt32 theMax=aDest.mLength-aTarget.mLength; - 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; - while(++index<=theMax) { - PRInt32 theSubIndex=-1; - PRBool matches=PR_TRUE; - while((++theSubIndex0) && (aTarget.mLength>0) && (anOffset= 0 && GetCharAt(aDest,theBufIndex)==GetCharAt(aTarget,thePatIndex)){ + --theBufIndex; + --thePatIndex; } - if(matches) { - return index; + if(-1==thePatIndex){ + return anOffset+theBufIndex+1; } } }//if @@ -514,12 +522,22 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgno if(anOffset+aTarget.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); } //while } //if - if(matches) { + if(matches) return index; + if(-1>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aCString); } - AddNullTerminator(*this); - Assign(aCString); } /** @@ -1814,16 +1816,18 @@ nsAutoString2::nsAutoString2(const PRUnichar* aString,eCharSize aCharSize) : nsS * 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) { +nsAutoString2::nsAutoString2(PRUnichar* aString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) { mAgent=0; - if(assumeOwnership) { - nsStr::Initialize(*this,(char*)aString,aCapacity,0,eTwoByte,PR_TRUE); + if(assumeOwnership && aString) { + aCapacity = (-1==aCapacity) ? nsCRT::strlen(aString) : aCapacity-1; + nsStr::Initialize(*this,(char*)aString,aCapacity,aCapacity,eTwoByte,PR_TRUE); + AddNullTerminator(*this); } else { nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE); + AddNullTerminator(*this); + Assign(aString); } - AddNullTerminator(*this); - Assign(aString); } @@ -1904,12 +1908,19 @@ nsSubsumeStr::nsSubsumeStr(nsStr& aString) : nsString2((eCharSize)aString.mCharS Subsume(*this,aString); } -nsSubsumeStr::nsSubsumeStr(const PRUnichar* aString) : nsString2(aString,eTwoByte) { +nsSubsumeStr::nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eTwoByte) { + mUStr=aString; + mCapacity=mLength=(-1==aLength) ? nsCRT::strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; } -nsSubsumeStr::nsSubsumeStr(const char* aString) : nsString2(aString,eOneByte) { +nsSubsumeStr::nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength) : nsString2(eOneByte) { + mStr=aString; + mCapacity=mLength=(-1==aLength) ? strlen(aString) : aLength-1; + mOwnsBuffer=assumeOwnership; } + #ifdef RICKG_DEBUG /*********************************************************************** IMPLEMENTATION of CStringTester... @@ -1928,6 +1939,14 @@ CStringTester::CStringTester() { nsString2 theString0("foo",theSize); //watch it construct and destruct } + { + //this test makes sure that autostrings who assume ownership of a buffer, + //don't also try to copy that buffer onto itself... (was a bug) + char* theStr="hello rick"; + nsAutoString2(theStr,5,eOneByte,PR_FALSE); + + } + { nsString2 theString("hello"); nsString2 temp1=theString+" there!"; diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index 4d9bab7bcab..bd7b74fad1d 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -52,8 +52,8 @@ class nsISizeOfHandler; #define nsAutoString2 nsAutoString #endif -class NS_COM nsSubsumeStr; +class NS_COM nsSubsumeStr; class NS_COM nsString2 : public nsStr { public: @@ -744,9 +744,9 @@ public: nsAutoString2(const char* aCString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(char* aCString,PRUint32 aLength,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); + nsAutoString2(char* aCString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); nsAutoString2(const PRUnichar* aString,eCharSize aCharSize=kDefaultCharSize); - nsAutoString2(PRUnichar* aString,PRUint32 aLength,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); + nsAutoString2(PRUnichar* aString,PRInt32 aCapacity=-1,eCharSize aCharSize=kDefaultCharSize,PRBool assumeOwnership=PR_FALSE); nsAutoString2(const nsStr& aString,eCharSize aCharSize=kDefaultCharSize); nsAutoString2(const nsString2& aString,eCharSize aCharSize=kDefaultCharSize); @@ -790,8 +790,8 @@ class NS_COM nsSubsumeStr : public nsString2 { public: nsSubsumeStr(nsString2& aString); nsSubsumeStr(nsStr& aString); - nsSubsumeStr(const PRUnichar* aString); - nsSubsumeStr(const char* aString); + nsSubsumeStr(PRUnichar* aString,PRBool assumeOwnership,PRInt32 aLength=-1); + nsSubsumeStr(char* aString,PRBool assumeOwnership,PRInt32 aLength=-1); }; #endif