From bfe2c85e5bec5ef2fad82a473890679cfc0e750e Mon Sep 17 00:00:00 2001 From: "alecf%netscape.com" Date: Tue, 15 Jan 2002 22:57:29 +0000 Subject: [PATCH] factor out some table-based string manipulation routines to make it easier to seperate unicode vs. 8-bit character strings. bug 114450, r=dbaron sr=jag git-svn-id: svn://10.0.0.236/trunk@112213 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/layout/generic/nsBulletFrame.cpp | 2 +- .../layout/html/base/src/nsBulletFrame.cpp | 2 +- mozilla/mailnews/compose/src/nsMsgCompose.cpp | 2 +- mozilla/string/obsolete/bufferRoutines.h | 41 +-- mozilla/string/obsolete/nsStr.cpp | 287 +++++++++++++++--- mozilla/string/obsolete/nsStr.h | 27 +- mozilla/string/obsolete/nsString.cpp | 10 +- mozilla/string/obsolete/nsString2.cpp | 10 +- .../xpcom/string/obsolete/bufferRoutines.h | 41 +-- mozilla/xpcom/string/obsolete/nsStr.cpp | 287 +++++++++++++++--- mozilla/xpcom/string/obsolete/nsStr.h | 27 +- mozilla/xpcom/string/obsolete/nsString.cpp | 10 +- mozilla/xpcom/string/obsolete/nsString2.cpp | 10 +- 13 files changed, 573 insertions(+), 183 deletions(-) diff --git a/mozilla/layout/generic/nsBulletFrame.cpp b/mozilla/layout/generic/nsBulletFrame.cpp index 1642c0a50b5..30504ddc47d 100644 --- a/mozilla/layout/generic/nsBulletFrame.cpp +++ b/mozilla/layout/generic/nsBulletFrame.cpp @@ -264,7 +264,7 @@ nsBulletFrame::Paint(nsIPresContext* aPresContext, GetListItemText(aPresContext, *myList, text); if (NS_STYLE_DIRECTION_RTL == vis->mDirection) { - nsStr::Delete(text, 0, 1); + nsStr::Delete2(text, 0, 1); text.Append(NS_LITERAL_STRING(".")); } break; diff --git a/mozilla/layout/html/base/src/nsBulletFrame.cpp b/mozilla/layout/html/base/src/nsBulletFrame.cpp index 1642c0a50b5..30504ddc47d 100644 --- a/mozilla/layout/html/base/src/nsBulletFrame.cpp +++ b/mozilla/layout/html/base/src/nsBulletFrame.cpp @@ -264,7 +264,7 @@ nsBulletFrame::Paint(nsIPresContext* aPresContext, GetListItemText(aPresContext, *myList, text); if (NS_STYLE_DIRECTION_RTL == vis->mDirection) { - nsStr::Delete(text, 0, 1); + nsStr::Delete2(text, 0, 1); text.Append(NS_LITERAL_STRING(".")); } break; diff --git a/mozilla/mailnews/compose/src/nsMsgCompose.cpp b/mozilla/mailnews/compose/src/nsMsgCompose.cpp index ebfbbf1ce3a..128fd702ca4 100644 --- a/mozilla/mailnews/compose/src/nsMsgCompose.cpp +++ b/mozilla/mailnews/compose/src/nsMsgCompose.cpp @@ -2853,7 +2853,7 @@ nsMsgCompose::LoadDataFromFile(nsFileSpec& fSpec, nsString &sigData) PRInt32 metaCharsetOffset = sigData.Find(metaCharset,PR_TRUE,0,-1); if (metaCharsetOffset != kNotFound) - nsStr::Delete(sigData, metaCharsetOffset, metaCharset.Length()); + nsStr::Delete2(sigData, metaCharsetOffset, metaCharset.Length()); } PR_FREEIF(readBuf); diff --git a/mozilla/string/obsolete/bufferRoutines.h b/mozilla/string/obsolete/bufferRoutines.h index 81bd1cc17e1..fc0be8c100c 100644 --- a/mozilla/string/obsolete/bufferRoutines.h +++ b/mozilla/string/obsolete/bufferRoutines.h @@ -284,9 +284,9 @@ void ShiftCharsRight(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCo * @param anOffset is the index into aDest where shifting shall begin * @param aCount is the number of chars to be "cut" */ -void ShiftDoubleCharsLeft(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount); -void ShiftDoubleCharsLeft(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount) { - PRUnichar* root=(PRUnichar*)aDest; +void ShiftDoubleCharsLeft(PRUnichar* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount); +void ShiftDoubleCharsLeft(PRUnichar* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount) { + PRUnichar* root= aDest; PRUnichar* dst = root+anOffset; PRUnichar* src = root+anOffset+aCount; @@ -302,23 +302,15 @@ void ShiftDoubleCharsLeft(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint3 * @param anOffset is the index into aDest where shifting shall begin * @param aCount is the number of chars to be "inserted" */ -void ShiftDoubleCharsRight(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount); -void ShiftDoubleCharsRight(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount) { - PRUnichar* root=(PRUnichar*)aDest; +void ShiftDoubleCharsRight(PRUnichar* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount); +void ShiftDoubleCharsRight(PRUnichar* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount) { + PRUnichar* root= aDest; PRUnichar* src = root+anOffset; PRUnichar* dst = root+anOffset+aCount; memmove(dst,src,sizeof(PRUnichar)*(aLength-anOffset)); } - -typedef void (*ShiftChars)(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount); -ShiftChars gShiftChars[2][2]= { - {&ShiftCharsLeft,&ShiftCharsRight}, - {&ShiftDoubleCharsLeft,&ShiftDoubleCharsRight} -}; - - //---------------------------------------------------------------------------------------- // // This set of methods is used to copy one buffer onto another. @@ -887,10 +879,10 @@ PRInt32 CompressChars1(char* aString,PRUint32 aLength,const char* aSet){ * @param aEliminateTrailing tells us whether to strip chars from the start of the buffer * @return the new length of the given buffer */ -PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet); -PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet){ +PRInt32 CompressChars2(PRUnichar* aString,PRUint32 aLength,const char* aSet); +PRInt32 CompressChars2(PRUnichar* aString,PRUint32 aLength,const char* aSet){ - PRUnichar* from = (PRUnichar*)aString; + PRUnichar* from = aString; PRUnichar* end = from + aLength; PRUnichar* to = from; @@ -919,9 +911,6 @@ PRInt32 CompressChars2(char* aString,PRUint32 aLength,const char* aSet){ return to - (PRUnichar*)aString; } -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 * @@ -965,11 +954,11 @@ PRInt32 StripChars1(char* aString,PRUint32 aLength,const char* aSet){ * @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); -PRInt32 StripChars2(char* aString,PRUint32 aLength,const char* aSet){ +PRInt32 StripChars2(PRUnichar* aString,PRUint32 aLength,const char* aSet); +PRInt32 StripChars2(PRUnichar* aString,PRUint32 aLength,const char* aSet){ - PRUnichar* to = (PRUnichar*)aString; - PRUnichar* from = (PRUnichar*)aString-1; + PRUnichar* to = aString; + PRUnichar* from = aString-1; PRUnichar* end = to + aLength; if(aSet && aString && (0 < aLength)){ @@ -988,8 +977,4 @@ PRInt32 StripChars2(char* aString,PRUint32 aLength,const char* aSet){ return to - (PRUnichar*)aString; } - -typedef PRInt32 (*StripChars)(char* aString,PRUint32 aCount,const char* aSet); -StripChars gStripChars[]={&StripChars1,&StripChars2}; - #endif diff --git a/mozilla/string/obsolete/nsStr.cpp b/mozilla/string/obsolete/nsStr.cpp index 4d2586b8131..77e2946e6c8 100644 --- a/mozilla/string/obsolete/nsStr.cpp +++ b/mozilla/string/obsolete/nsStr.cpp @@ -215,7 +215,45 @@ void nsStr::StrAppend(nsStr& aDest,const nsStr& aSource,PRUint32 anOffset,PRInt3 * @param aSrcOffset is where in aSource chars are copied from * @param aCount is the number of chars from aSource to be inserted into aDest */ -void nsStr::StrInsert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ + +PRInt32 nsStr::GetSegmentLength(const nsStr& aSource, + PRUint32 aSrcOffset, PRInt32 aCount) +{ + PRInt32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength); + PRInt32 theLength=(aSrcOffset+theRealLen aDest.mCapacity) { - nsStr theTempStr; - nsStr::Initialize(theTempStr,eCharSize(aDest.mCharSize)); - - PRBool isBigEnough=EnsureCapacity(theTempStr,aDest.mLength+theLength); //grow the temp buffer to the right size - - if(isBigEnough) { - if(aDestOffset) { - StrAppend(theTempStr,aDest,0,aDestOffset); //first copy leftmost data... - } - - StrAppend(theTempStr,aSource,aSrcOffset,theLength); //next copy inserted (new) data - - PRUint32 theRemains=aDest.mLength-aDestOffset; - if(theRemains) { - StrAppend(theTempStr,aDest,aDestOffset,theRemains); //next copy rightmost data - } - - 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; - } - - } - + if(aDest.mLength+theLength > aDest.mCapacity) + AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { - //shift the chars right by theDelta... - (*gShiftChars[aDest.mCharSize][KSHIFTRIGHT])(aDest.mStr,aDest.mLength,aDestOffset,theLength); + //shift the chars right by theDelta... + ShiftCharsRight(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); + CopyChars1To1(aDest.mStr, aDestOffset, aSource.mStr, aSrcOffset, theLength); + } + + //finally, make sure to update the string length... + aDest.mLength+=theLength; + AddNullTerminator(aDest); + NSSTR_SEEN(aDest); + }//if + //else nothing to do! + } + else StrAppend(aDest,aSource,0,aCount); + } + else StrAppend(aDest,aSource,0,aCount); + } +} + +void nsStr::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ + NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); + //there are a few cases for insert: + // 1. You're inserting chars into an empty string (assign) + // 2. You're inserting onto the end of a string (append) + // 3. You're inserting onto the 1..n-1 pos of a string (the hard case). + if(0 aDest.mCapacity) + AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); + else { + //shift the chars right by theDelta... + ShiftDoubleCharsRight(aDest.mUStr, aDest.mLength, aDestOffset, theLength); + + //now insert new chars, starting at offset + CopyChars1To2(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); + } + + //finally, make sure to update the string length... + aDest.mLength+=theLength; + AddNullTerminator(aDest); + NSSTR_SEEN(aDest); + }//if + //else nothing to do! + } + else StrAppend(aDest,aSource,0,aCount); + } + else StrAppend(aDest,aSource,0,aCount); + } +} + +void nsStr::StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ + NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + //there are a few cases for insert: + // 1. You're inserting chars into an empty string (assign) + // 2. You're inserting onto the end of a string (append) + // 3. You're inserting onto the 1..n-1 pos of a string (the hard case). + if(0 aDest.mCapacity) + AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); + else { + //shift the chars right by theDelta... + ShiftCharsRight(aDest.mStr, aDest.mLength, aDestOffset, theLength); + + //now insert new chars, starting at offset + CopyChars2To1(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); + } + + //finally, make sure to update the string length... + aDest.mLength+=theLength; + AddNullTerminator(aDest); + NSSTR_SEEN(aDest); + }//if + //else nothing to do! + } + else StrAppend(aDest,aSource,0,aCount); + } + else StrAppend(aDest,aSource,0,aCount); + } +} + +void nsStr::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ + NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 1 byte"); + NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); + //there are a few cases for insert: + // 1. You're inserting chars into an empty string (assign) + // 2. You're inserting onto the end of a string (append) + // 3. You're inserting onto the 1..n-1 pos of a string (the hard case). + if(0 aDest.mCapacity) + AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); + else { + + //shift the chars right by theDelta... + ShiftDoubleCharsRight(aDest.mUStr, aDest.mLength, aDestOffset, theLength); + + //now insert new chars, starting at offset + CopyChars2To2(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); } //finally, make sure to update the string length... @@ -286,17 +416,20 @@ void nsStr::StrInsert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PR * @param aDestOffset is where in aDest deletion is to occur * @param aCount is the number of chars to be deleted in aDest */ -void nsStr::Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){ + + +void nsStr::Delete1(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){ + NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + if(aDestOffset=0) { PRUnichar theChar=GetCharAt(aDest,theIndex); //read at end now... - PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE,theSetLen); + PRInt32 thePos=FindChar1(aSet,theSetLen,0,theChar,PR_FALSE,theSetLen); if(kNotFound aDest.mCapacity) { - nsStr theTempStr; - nsStr::Initialize(theTempStr,eCharSize(aDest.mCharSize)); - - PRBool isBigEnough=EnsureCapacity(theTempStr,aDest.mLength+theLength); //grow the temp buffer to the right size - - if(isBigEnough) { - if(aDestOffset) { - StrAppend(theTempStr,aDest,0,aDestOffset); //first copy leftmost data... - } - - StrAppend(theTempStr,aSource,aSrcOffset,theLength); //next copy inserted (new) data - - PRUint32 theRemains=aDest.mLength-aDestOffset; - if(theRemains) { - StrAppend(theTempStr,aDest,aDestOffset,theRemains); //next copy rightmost data - } - - 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; - } - - } - + if(aDest.mLength+theLength > aDest.mCapacity) + AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); else { - //shift the chars right by theDelta... - (*gShiftChars[aDest.mCharSize][KSHIFTRIGHT])(aDest.mStr,aDest.mLength,aDestOffset,theLength); + //shift the chars right by theDelta... + ShiftCharsRight(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); + CopyChars1To1(aDest.mStr, aDestOffset, aSource.mStr, aSrcOffset, theLength); + } + + //finally, make sure to update the string length... + aDest.mLength+=theLength; + AddNullTerminator(aDest); + NSSTR_SEEN(aDest); + }//if + //else nothing to do! + } + else StrAppend(aDest,aSource,0,aCount); + } + else StrAppend(aDest,aSource,0,aCount); + } +} + +void nsStr::StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ + NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 byte"); + NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); + //there are a few cases for insert: + // 1. You're inserting chars into an empty string (assign) + // 2. You're inserting onto the end of a string (append) + // 3. You're inserting onto the 1..n-1 pos of a string (the hard case). + if(0 aDest.mCapacity) + AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); + else { + //shift the chars right by theDelta... + ShiftDoubleCharsRight(aDest.mUStr, aDest.mLength, aDestOffset, theLength); + + //now insert new chars, starting at offset + CopyChars1To2(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); + } + + //finally, make sure to update the string length... + aDest.mLength+=theLength; + AddNullTerminator(aDest); + NSSTR_SEEN(aDest); + }//if + //else nothing to do! + } + else StrAppend(aDest,aSource,0,aCount); + } + else StrAppend(aDest,aSource,0,aCount); + } +} + +void nsStr::StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ + NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 2 byte"); + NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + //there are a few cases for insert: + // 1. You're inserting chars into an empty string (assign) + // 2. You're inserting onto the end of a string (append) + // 3. You're inserting onto the 1..n-1 pos of a string (the hard case). + if(0 aDest.mCapacity) + AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); + else { + //shift the chars right by theDelta... + ShiftCharsRight(aDest.mStr, aDest.mLength, aDestOffset, theLength); + + //now insert new chars, starting at offset + CopyChars2To1(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); + } + + //finally, make sure to update the string length... + aDest.mLength+=theLength; + AddNullTerminator(aDest); + NSSTR_SEEN(aDest); + }//if + //else nothing to do! + } + else StrAppend(aDest,aSource,0,aCount); + } + else StrAppend(aDest,aSource,0,aCount); + } +} + +void nsStr::StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){ + NS_ASSERTION(aSource.mCharSize == eTwoByte, "Must be 1 byte"); + NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte"); + //there are a few cases for insert: + // 1. You're inserting chars into an empty string (assign) + // 2. You're inserting onto the end of a string (append) + // 3. You're inserting onto the 1..n-1 pos of a string (the hard case). + if(0 aDest.mCapacity) + AppendForInsert(aDest, aDestOffset, aSource, aSrcOffset, theLength); + else { + + //shift the chars right by theDelta... + ShiftDoubleCharsRight(aDest.mUStr, aDest.mLength, aDestOffset, theLength); + + //now insert new chars, starting at offset + CopyChars2To2(aDest.mStr,aDestOffset,aSource.mStr,aSrcOffset,theLength); } //finally, make sure to update the string length... @@ -286,17 +416,20 @@ void nsStr::StrInsert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PR * @param aDestOffset is where in aDest deletion is to occur * @param aCount is the number of chars to be deleted in aDest */ -void nsStr::Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){ + + +void nsStr::Delete1(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){ + NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte"); + if(aDestOffset=0) { PRUnichar theChar=GetCharAt(aDest,theIndex); //read at end now... - PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE,theSetLen); + PRInt32 thePos=FindChar1(aSet,theSetLen,0,theChar,PR_FALSE,theSetLen); if(kNotFound