perf update and bug fix: a=chofmann r=buster

git-svn-id: svn://10.0.0.236/trunk@49399 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
rickg%netscape.com
1999-09-30 04:03:49 +00:00
parent e531fd4153
commit 3ee654688b
21 changed files with 551 additions and 438 deletions

View File

@@ -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<aSource.mLength) ? theRealLen : (aSource.mLength-anOffset);
if(0<theLength){
PRBool isBigEnough=PR_TRUE;
if(aDest.mLength+theLength > 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<aDest.mLength) && (aSet)) {
PRUint32 aNewLen=gStripChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet);
aDest.mLength=aNewLen;
}
}
/**************************************************************
Searching methods...
**************************************************************/

View File

@@ -238,8 +238,8 @@ struct NS_COM nsStr {
* @param anAgent is the allocator to be used on the nsStr
* @return
*/
static void EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent=0);
static void GrowCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent=0);
static PRBool EnsureCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent=0);
static PRBool GrowCapacity(nsStr& aString,PRUint32 aNewLength,nsIMemoryAgent* anAgent=0);
/**
* These methods are used to append content to the given nsStr
@@ -336,6 +336,18 @@ struct NS_COM nsStr {
*/
static void CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing);
/**
* This method removes all occurances of chars in given set from aDest
*
* @update gess 01/04/99
* @param aDest is the buffer to be manipulated
* @param aSet tells us which chars to compress from given buffer
* @param aChar is the replacement char
* @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
*/
static void StripChars(nsStr& aDest,const char* aSet);
/**
* This method compares the data bewteen two nsStr's
*
@@ -445,8 +457,12 @@ public:
PRUint32 theSize=(theNewCapacity<<aDest.mCharSize);
aDest.mStr = (char*)nsAllocator::Alloc(theSize);
aDest.mOwnsBuffer=1;
return PR_TRUE;
PRBool result=PR_FALSE;
if(aDest.mStr) {
aDest.mOwnsBuffer=1;
result=PR_TRUE;
}
return result;
}
virtual PRBool Free(nsStr& aDest){
@@ -462,10 +478,23 @@ public:
}
virtual PRBool Realloc(nsStr& aDest,PRUint32 aCount){
Free(aDest);
return Alloc(aDest,aCount);
}
#if 0
nsStr temp;
memcpy(&temp,&aDest,sizeof(aDest));
PRBool result=Alloc(temp,aCount);
if(result) {
Free(aDest);
aDest.mStr=temp.mStr;
aDest.mCapacity=temp.mCapacity;
}
return result;
#endif
}
};
nsIMemoryAgent* GetDefaultAgent(void);

View File

@@ -355,13 +355,9 @@ void nsCString::ToUpperCase(nsCString& aString) const {
* @param aChar -- char to be stripped
* @return *this
*/
nsCString& nsCString::StripChar(PRUnichar aChar){
PRInt32 theIndex=FindChar(aChar,PR_FALSE,0);
while(kNotFound<theIndex) {
Cut(theIndex,1);
theIndex=FindChar(aChar,PR_FALSE,theIndex);
}
nsCString& nsCString::StripChar(char aChar){
char aSet[2]={aChar,0};
nsStr::StripChars(*this,aSet);
return *this;
}
@@ -374,13 +370,7 @@ nsCString& nsCString::StripChar(PRUnichar aChar){
* @return *this
*/
nsCString& nsCString::StripChars(const char* aSet){
if(aSet){
PRInt32 theIndex=FindCharInSet(aSet,0);
while(kNotFound<theIndex) {
Cut(theIndex,1);
theIndex=FindCharInSet(aSet,theIndex);
}
}
nsStr::StripChars(*this,aSet);
return *this;
}
@@ -952,43 +942,34 @@ nsCString& nsCString::Append(char aChar) {
* @param aRadix:
* @return
*/
nsCString& nsCString::Append(PRInt32 aInteger,PRInt32 aRadix) {
nsCString& nsCString::Append(PRInt32 anInteger,PRInt32 aRadix) {
#if 0
char buf[128]={0,0};
char* buffer=buf;
PRUint32 theInt=(PRUint32)anInteger;
ldiv_t r; /* result of val / base */
if (aRadix> 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);
}

View File

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

View File

@@ -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<theIndex) {
Cut(theIndex,1);
theIndex=FindChar(aChar,PR_FALSE,theIndex);
}
nsString& nsString::StripChar(char aChar){
char aSet[2]={aChar,0};
nsStr::StripChars(*this,aSet);
return *this;
}
@@ -405,14 +401,7 @@ nsString& nsString::StripChar(PRUnichar aChar){
* @return *this
*/
nsString& nsString::StripChars(const char* aSet){
if(aSet){
PRInt32 theIndex=FindCharInSet(aSet,0);
while(kNotFound<theIndex) {
Cut(theIndex,1);
theIndex=FindCharInSet(aSet,theIndex);
}
}
nsStr::StripChars(*this,aSet);
return *this;
}
@@ -424,8 +413,7 @@ nsString& nsString::StripChars(const char* aSet){
* @return this
*/
nsString& nsString::StripWhitespace() {
StripChars(kWhitespace);
return *this;
return StripChars(kWhitespace);
}
/**
@@ -1124,43 +1112,34 @@ nsString& nsString::Append(PRUnichar aChar) {
* @param aRadix:
* @return
*/
nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) {
nsString& nsString::Append(PRInt32 anInteger,PRInt32 aRadix) {
#if 0
char buf[128]={0,0};
char* buffer=buf;
PRUint32 theInt=(PRUint32)anInteger;
ldiv_t r; /* result of val / base */
if (aRadix> 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);
}

View File

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