final update to nsString2 before switchover

This commit is contained in:
rickg%netscape.com 1999-05-29 07:29:46 +00:00
parent 1fb836d048
commit 8f2ceea3de
12 changed files with 240 additions and 102 deletions

View File

@ -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((++theSubIndex<theTargetMax) && (matches)){
PRUnichar theChar=GetCharAt(aDest,index+theSubIndex);
PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex);
matches=PRBool(theChar==theTargetChar);
if((aDest.mLength>0) && (aTarget.mLength>0) && (anOffset<aTarget.mLength)){
//This little block of code builds up the boyer-moore skip table.
//It might be nicer if this could be generated externally as passed in to improve performance.
const int theSize=100;
int theSkipTable[theSize];
PRUint32 theIndex=0;
for (theIndex=0;theIndex<theSize;++theIndex) {
theSkipTable[theIndex]=aTarget.mLength;
}
for(theIndex=0;theIndex<aTarget.mLength-1;++theIndex) {
theSkipTable[(PRUint32)GetCharAt(aTarget,theIndex)]=(aTarget.mLength-theIndex-1);
}
//and now we do the actual searching.
PRUint32 theMaxIndex=aDest.mLength-anOffset;
for (theIndex=aTarget.mLength-1; theIndex< theMaxIndex; theIndex+= theSkipTable[(unsigned char)GetCharAt(aDest,theIndex)]) {
int theBufIndex=theIndex;
int thePatIndex=aTarget.mLength-1;
while (thePatIndex >= 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((++theSubIndex<theTargetMax) && (matches)){
PRUnichar theChar=GetCharAt(aDest,index+theSubIndex);
if(theSubIndex>0) {
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<theNewStartPos){
index=theNewStartPos-1;
}
}
}//if

View File

@ -130,6 +130,15 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const;
*/
PRBool IsOrdered(void) const;
/**
* Determine whether or not this string has a length of 0
*
* @return TRUE if empty.
*/
PRBool IsEmpty(void) const {
return PRBool(0==mLength);
}
/**********************************************************************
Accessor methods...
*********************************************************************/

View File

@ -1787,16 +1787,18 @@ nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsStrin
* 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) {
nsAutoString2::nsAutoString2(char* aCString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) {
mAgent=0;
if(assumeOwnership) {
nsStr::Initialize(*this,aCString,aCapacity,0,eOneByte,PR_TRUE);
if(assumeOwnership && aCString) {
aCapacity = (-1==aCapacity) ? strlen(aCString) : aCapacity-1;
nsStr::Initialize(*this,aCString,aCapacity,aCapacity,eOneByte,PR_TRUE);
AddNullTerminator(*this);
}
else {
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
AddNullTerminator(*this);
Assign(aCString);
}
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!";

View File

@ -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

View File

@ -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((++theSubIndex<theTargetMax) && (matches)){
PRUnichar theChar=GetCharAt(aDest,index+theSubIndex);
PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex);
matches=PRBool(theChar==theTargetChar);
if((aDest.mLength>0) && (aTarget.mLength>0) && (anOffset<aTarget.mLength)){
//This little block of code builds up the boyer-moore skip table.
//It might be nicer if this could be generated externally as passed in to improve performance.
const int theSize=100;
int theSkipTable[theSize];
PRUint32 theIndex=0;
for (theIndex=0;theIndex<theSize;++theIndex) {
theSkipTable[theIndex]=aTarget.mLength;
}
for(theIndex=0;theIndex<aTarget.mLength-1;++theIndex) {
theSkipTable[(PRUint32)GetCharAt(aTarget,theIndex)]=(aTarget.mLength-theIndex-1);
}
//and now we do the actual searching.
PRUint32 theMaxIndex=aDest.mLength-anOffset;
for (theIndex=aTarget.mLength-1; theIndex< theMaxIndex; theIndex+= theSkipTable[(unsigned char)GetCharAt(aDest,theIndex)]) {
int theBufIndex=theIndex;
int thePatIndex=aTarget.mLength-1;
while (thePatIndex >= 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((++theSubIndex<theTargetMax) && (matches)){
PRUnichar theChar=GetCharAt(aDest,index+theSubIndex);
if(theSubIndex>0) {
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<theNewStartPos){
index=theNewStartPos-1;
}
}
}//if

View File

@ -130,6 +130,15 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const;
*/
PRBool IsOrdered(void) const;
/**
* Determine whether or not this string has a length of 0
*
* @return TRUE if empty.
*/
PRBool IsEmpty(void) const {
return PRBool(0==mLength);
}
/**********************************************************************
Accessor methods...
*********************************************************************/

View File

@ -1787,16 +1787,18 @@ nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsStrin
* 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) {
nsAutoString2::nsAutoString2(char* aCString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) {
mAgent=0;
if(assumeOwnership) {
nsStr::Initialize(*this,aCString,aCapacity,0,eOneByte,PR_TRUE);
if(assumeOwnership && aCString) {
aCapacity = (-1==aCapacity) ? strlen(aCString) : aCapacity-1;
nsStr::Initialize(*this,aCString,aCapacity,aCapacity,eOneByte,PR_TRUE);
AddNullTerminator(*this);
}
else {
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
AddNullTerminator(*this);
Assign(aCString);
}
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!";

View File

@ -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

View File

@ -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((++theSubIndex<theTargetMax) && (matches)){
PRUnichar theChar=GetCharAt(aDest,index+theSubIndex);
PRUnichar theTargetChar=GetCharAt(aTarget,theSubIndex);
matches=PRBool(theChar==theTargetChar);
if((aDest.mLength>0) && (aTarget.mLength>0) && (anOffset<aTarget.mLength)){
//This little block of code builds up the boyer-moore skip table.
//It might be nicer if this could be generated externally as passed in to improve performance.
const int theSize=100;
int theSkipTable[theSize];
PRUint32 theIndex=0;
for (theIndex=0;theIndex<theSize;++theIndex) {
theSkipTable[theIndex]=aTarget.mLength;
}
for(theIndex=0;theIndex<aTarget.mLength-1;++theIndex) {
theSkipTable[(PRUint32)GetCharAt(aTarget,theIndex)]=(aTarget.mLength-theIndex-1);
}
//and now we do the actual searching.
PRUint32 theMaxIndex=aDest.mLength-anOffset;
for (theIndex=aTarget.mLength-1; theIndex< theMaxIndex; theIndex+= theSkipTable[(unsigned char)GetCharAt(aDest,theIndex)]) {
int theBufIndex=theIndex;
int thePatIndex=aTarget.mLength-1;
while (thePatIndex >= 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((++theSubIndex<theTargetMax) && (matches)){
PRUnichar theChar=GetCharAt(aDest,index+theSubIndex);
if(theSubIndex>0) {
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<theNewStartPos){
index=theNewStartPos-1;
}
}
}//if

View File

@ -130,6 +130,15 @@ virtual void SizeOf(nsISizeOfHandler* aHandler) const;
*/
PRBool IsOrdered(void) const;
/**
* Determine whether or not this string has a length of 0
*
* @return TRUE if empty.
*/
PRBool IsEmpty(void) const {
return PRBool(0==mLength);
}
/**********************************************************************
Accessor methods...
*********************************************************************/

View File

@ -1787,16 +1787,18 @@ nsAutoString2::nsAutoString2(const char* aCString,eCharSize aCharSize) : nsStrin
* 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) {
nsAutoString2::nsAutoString2(char* aCString,PRInt32 aCapacity,eCharSize aCharSize,PRBool assumeOwnership) : nsString2(aCharSize) {
mAgent=0;
if(assumeOwnership) {
nsStr::Initialize(*this,aCString,aCapacity,0,eOneByte,PR_TRUE);
if(assumeOwnership && aCString) {
aCapacity = (-1==aCapacity) ? strlen(aCString) : aCapacity-1;
nsStr::Initialize(*this,aCString,aCapacity,aCapacity,eOneByte,PR_TRUE);
AddNullTerminator(*this);
}
else {
nsStr::Initialize(*this,mBuffer,(sizeof(mBuffer)>>aCharSize)-1,0,aCharSize,PR_FALSE);
AddNullTerminator(*this);
Assign(aCString);
}
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!";

View File

@ -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