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

This commit is contained in:
alecf%netscape.com 2002-01-15 22:57:29 +00:00
Родитель 61df33f985
Коммит e26f0caa39
13 изменённых файлов: 573 добавлений и 183 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -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<aSource.mLength) ? theRealLen : (aSource.mLength-aSrcOffset);
return theLength;
}
void nsStr::AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSource, PRUint32 aSrcOffset, PRInt32 theLength) {
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;
}
}
void nsStr::StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){
NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 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)
@ -223,46 +261,138 @@ void nsStr::StrInsert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PR
if(0<aSource.mLength){
if(aDest.mLength){
if(aDestOffset<aDest.mLength){
PRInt32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength);
PRInt32 theLength=(aSrcOffset+theRealLen<aSource.mLength) ? theRealLen : (aSource.mLength-aSrcOffset);
PRInt32 theLength = GetSegmentLength(aSource, aSrcOffset, aCount);
if(aSrcOffset<aSource.mLength) {
//here's the only new case we have to handle.
//chars are really being inserted into our buffer...
if(aDest.mLength+theLength > 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<aSource.mLength){
if(aDest.mLength){
if(aDestOffset<aDest.mLength){
PRInt32 theLength = GetSegmentLength(aSource, aSrcOffset, aCount);
if(aSrcOffset<aSource.mLength) {
//here's the only new case we have to handle.
//chars are really being inserted into our buffer...
if(aDest.mLength+theLength > 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<aSource.mLength){
if(aDest.mLength){
if(aDestOffset<aDest.mLength){
PRInt32 theLength = GetSegmentLength(aSource, aSrcOffset, aCount);
if(aSrcOffset<aSource.mLength) {
//here's the only new case we have to handle.
//chars are really being inserted into our buffer...
if(aDest.mLength+theLength > 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<aSource.mLength){
if(aDest.mLength){
if(aDestOffset<aDest.mLength){
PRInt32 theLength = GetSegmentLength(aSource, aSrcOffset, aCount);
if(aSrcOffset<aSource.mLength) {
//here's the only new case we have to handle.
//chars are really being inserted into our buffer...
if(aDest.mLength+theLength > 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<aDest.mLength){
PRUint32 theDelta=aDest.mLength-aDestOffset;
PRUint32 theLength=(theDelta<aCount) ? theDelta : aCount;
PRUint32 theLength=GetDeleteLength(aDest, aDestOffset, aCount);
if(aDestOffset+theLength<aDest.mLength) {
//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][KSHIFTLEFT])(aDest.mStr,aDest.mLength,aDestOffset,theLength);
ShiftCharsLeft(aDest.mStr,aDest.mLength,aDestOffset,theLength);
aDest.mLength-=theLength;
AddNullTerminator(aDest);
NSSTR_SEEN(aDest);
@ -305,6 +438,35 @@ void nsStr::Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){
}//if
}
void nsStr::Delete2(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){
NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte");
if(aDestOffset<aDest.mLength){
PRUint32 theLength=GetDeleteLength(aDest, aDestOffset, aCount);
if(aDestOffset+theLength<aDest.mLength) {
//if you're here, it means we're cutting chars out of the middle of the string...
//so shift the chars left by theLength...
ShiftDoubleCharsLeft(aDest.mUStr,aDest.mLength,aDestOffset,theLength);
aDest.mLength-=theLength;
AddNullTerminator(aDest);
NSSTR_SEEN(aDest);
}
else StrTruncate(aDest,aDestOffset);
}//if
}
PRInt32 nsStr::GetDeleteLength(const nsStr& aDest, PRUint32 aDestOffset, PRUint32 aCount)
{
PRUint32 theDelta=aDest.mLength-aDestOffset;
PRUint32 theLength=(theDelta<aCount) ? theDelta : aCount;
return theLength;
}
/**
* This method truncates the given nsStr at given offset
* @update gess10/30/98
@ -319,7 +481,6 @@ void nsStr::StrTruncate(nsStr& aDest,PRUint32 aDestOffset){
}
}
/**
* This method removes characters from the given set from this string.
* NOTE: aSet is a char*, and it's length is computed using strlen, which assumes null termination.
@ -341,13 +502,17 @@ void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool a
if(aEliminateLeading) {
while(++theIndex<=theMax) {
PRUnichar theChar=GetCharAt(aDest,theIndex);
PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE,theSetLen);
PRInt32 thePos=FindChar1(aSet,theSetLen,0,theChar,PR_FALSE,theSetLen);
if(kNotFound==thePos)
break;
}
if(0<theIndex) {
if(theIndex<theMax) {
Delete(aDest,0,theIndex);
if (aDest.mCharSize == eOneByte)
Delete1(aDest,0,theIndex);
else
Delete2(aDest,0,theIndex);
}
else StrTruncate(aDest,0);
}
@ -358,7 +523,7 @@ void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool a
PRInt32 theNewLen=theIndex;
while(--theIndex>=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<thePos)
theNewLen=theIndex;
else break;
@ -377,9 +542,20 @@ void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool a
* @param
* @return
*/
void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
void nsStr::CompressSet1(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte");
Trim(aDest,aSet,aEliminateLeading,aEliminateTrailing);
PRUint32 aNewLen=gCompressChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet);
PRUint32 aNewLen=CompressChars1(aDest.mStr,aDest.mLength,aSet);
aDest.mLength=aNewLen;
NSSTR_SEEN(aDest);
}
void nsStr::CompressSet2(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 bytes");
Trim(aDest,aSet,aEliminateLeading,aEliminateTrailing);
PRUint32 aNewLen=CompressChars2(aDest.mUStr,aDest.mLength,aSet);
aDest.mLength=aNewLen;
NSSTR_SEEN(aDest);
}
@ -391,9 +567,21 @@ void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,P
* @param
* @return
*/
void nsStr::StripChars(nsStr& aDest,const char* aSet){
void nsStr::StripChars1(nsStr& aDest,const char* aSet){
NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte");
if((0<aDest.mLength) && (aSet)) {
PRUint32 aNewLen=gStripChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet);
PRUint32 aNewLen=::StripChars1(aDest.mStr, aDest.mLength, aSet);
aDest.mLength=aNewLen;
NSSTR_SEEN(aDest);
}
}
void nsStr::StripChars2(nsStr& aDest,const char* aSet){
NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 bytes");
if((0<aDest.mLength) && (aSet)) {
PRUint32 aNewLen=::StripChars2(aDest.mUStr, aDest.mLength, aSet);
aDest.mLength=aNewLen;
NSSTR_SEEN(aDest);
}
@ -522,7 +710,9 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget,PRBool aIgnor
if(aCount<0)
aCount = aDest.mLength;
if((0<aDest.mLength) && ((PRUint32)anOffset<aDest.mLength) && (aTarget.mLength)) {
if((0<aDest.mLength) &&
((PRUint32)anOffset<aDest.mLength) &&
(aTarget.mLength)) {
if(0<aCount) {
@ -642,6 +832,7 @@ TranslateCompareResult(const PRInt32 aDestLength, const PRInt32& aSourceLength,
return result;
}
/**
* Compare source and dest strings, up to an (optional max) number of chars
* @param aDest is the first str to compare

Просмотреть файл

@ -333,7 +333,18 @@ struct NS_COM nsStr {
* @param aCount tells us the (max) # of chars to insert
* @param anAgent is the allocator to be used for alloc/free operations
*/
static void StrInsert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
static void StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
static void StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
static void StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
static void StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
/**
* Helper routines for StrInsert1into1, etc
*/
static PRInt32 GetSegmentLength(const nsStr& aSource,
PRUint32 aSrcOffset, PRInt32 aCount);
static void AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSource, PRUint32 aSrcOffset, PRInt32 theLength);
/**
* This method deletes chars from the given str.
@ -345,7 +356,13 @@ struct NS_COM nsStr {
* @param aCount tells us the (max) # of chars to delete
* @param anAgent is the allocator to be used for alloc/free operations
*/
static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount);
static void Delete1(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount);
static void Delete2(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount);
/**
* helper routines for Delete1, Delete2
*/
static PRInt32 GetDeleteLength(const nsStr& aDest, PRUint32 aDestOffset, PRUint32 aCount);
/**
* This method is used to truncate the given string.
@ -381,7 +398,8 @@ struct NS_COM nsStr {
* @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 CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing);
static void CompressSet1(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing);
static void CompressSet2(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing);
/**
* This method removes all occurances of chars in given set from aDest
@ -393,7 +411,8 @@ struct NS_COM nsStr {
* @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);
static void StripChars1(nsStr& aDest,const char* aSet);
static void StripChars2(nsStr& aDest,const char* aSet);
/**
* This method compares the data bewteen two nsStr's

Просмотреть файл

@ -287,7 +287,7 @@ nsCString::StripChar(PRUnichar aChar,PRInt32 anOffset){
*/
void
nsCString::StripChars(const char* aSet){
nsStr::StripChars(*this,aSet);
nsStr::StripChars1(*this,aSet);
}
@ -396,14 +396,14 @@ nsCString::ReplaceSubstring(const nsCString& aTarget,const nsCString& aNewValue)
if(aNewValue.mLength<aTarget.mLength) {
//Since target is longer than newValue, we should delete a few chars first, then overwrite.
PRInt32 theDelLen=aTarget.mLength-aNewValue.mLength;
nsStr::Delete(*this,theIndex,theDelLen);
nsStr::Delete1(*this,theIndex,theDelLen);
nsStr::Overwrite(*this,aNewValue,theIndex);
}
else {
//this is the worst case: the newvalue is larger than the substr it's replacing
//so we have to insert some characters...
PRInt32 theInsLen=aNewValue.mLength-aTarget.mLength;
StrInsert(*this,theIndex,aNewValue,0,theInsLen);
StrInsert1into1(*this,theIndex,aNewValue,0,theInsLen);
nsStr::Overwrite(*this,aNewValue,theIndex);
theIndex += aNewValue.mLength;
}
@ -467,7 +467,7 @@ void
nsCString::CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){
if(aSet){
ReplaceChar(aSet,aChar);
nsStr::CompressSet(*this,aSet,aEliminateLeading,aEliminateTrailing);
nsStr::CompressSet1(*this,aSet,aEliminateLeading,aEliminateTrailing);
}
}
@ -846,7 +846,7 @@ void nsCString::InsertWithConversion(PRUnichar aChar,PRUint32 anOffset){
nsStr::Initialize(temp,eTwoByte);
temp.mUStr=theBuffer;
temp.mLength=1;
StrInsert(*this,anOffset,temp,0,1);
StrInsert2into1(*this,anOffset,temp,0,1);
}

Просмотреть файл

@ -299,7 +299,7 @@ nsString::StripChar(PRUnichar aChar,PRInt32 anOffset){
*/
void
nsString::StripChars(const char* aSet){
nsStr::StripChars(*this,aSet);
nsStr::StripChars2(*this,aSet);
}
@ -413,14 +413,14 @@ nsString::ReplaceSubstring(const nsString& aTarget,const nsString& aNewValue){
if(aNewValue.mLength<aTarget.mLength) {
//Since target is longer than newValue, we should delete a few chars first, then overwrite.
PRInt32 theDelLen=aTarget.mLength-aNewValue.mLength;
nsStr::Delete(*this,theIndex,theDelLen);
nsStr::Delete2(*this,theIndex,theDelLen);
nsStr::Overwrite(*this,aNewValue,theIndex);
}
else {
//this is the worst case: the newvalue is larger than the substr it's replacing
//so we have to insert some characters...
PRInt32 theInsLen=aNewValue.mLength-aTarget.mLength;
StrInsert(*this,theIndex,aNewValue,0,theInsLen);
StrInsert2into2(*this,theIndex,aNewValue,0,theInsLen);
nsStr::Overwrite(*this,aNewValue,theIndex);
theIndex += aNewValue.mLength;
}
@ -484,7 +484,7 @@ void
nsString::CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){
if(aSet){
ReplaceChar(aSet,aChar);
nsStr::CompressSet(*this,aSet,aEliminateLeading,aEliminateTrailing);
nsStr::CompressSet2(*this,aSet,aEliminateLeading,aEliminateTrailing);
}
}
@ -847,7 +847,7 @@ void nsString::InsertWithConversion(const char* aCString,PRUint32 anOffset,PRInt
else aCount=temp.mLength=nsCRT::strlen(aCString);
if(0<aCount){
StrInsert(*this,anOffset,temp,0,aCount);
StrInsert1into2(*this,anOffset,temp,0,aCount);
}
}
}

Просмотреть файл

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

Просмотреть файл

@ -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<aSource.mLength) ? theRealLen : (aSource.mLength-aSrcOffset);
return theLength;
}
void nsStr::AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSource, PRUint32 aSrcOffset, PRInt32 theLength) {
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;
}
}
void nsStr::StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount){
NS_ASSERTION(aSource.mCharSize == eOneByte, "Must be 1 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)
@ -223,46 +261,138 @@ void nsStr::StrInsert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PR
if(0<aSource.mLength){
if(aDest.mLength){
if(aDestOffset<aDest.mLength){
PRInt32 theRealLen=(aCount<0) ? aSource.mLength : MinInt(aCount,aSource.mLength);
PRInt32 theLength=(aSrcOffset+theRealLen<aSource.mLength) ? theRealLen : (aSource.mLength-aSrcOffset);
PRInt32 theLength = GetSegmentLength(aSource, aSrcOffset, aCount);
if(aSrcOffset<aSource.mLength) {
//here's the only new case we have to handle.
//chars are really being inserted into our buffer...
if(aDest.mLength+theLength > 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<aSource.mLength){
if(aDest.mLength){
if(aDestOffset<aDest.mLength){
PRInt32 theLength = GetSegmentLength(aSource, aSrcOffset, aCount);
if(aSrcOffset<aSource.mLength) {
//here's the only new case we have to handle.
//chars are really being inserted into our buffer...
if(aDest.mLength+theLength > 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<aSource.mLength){
if(aDest.mLength){
if(aDestOffset<aDest.mLength){
PRInt32 theLength = GetSegmentLength(aSource, aSrcOffset, aCount);
if(aSrcOffset<aSource.mLength) {
//here's the only new case we have to handle.
//chars are really being inserted into our buffer...
if(aDest.mLength+theLength > 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<aSource.mLength){
if(aDest.mLength){
if(aDestOffset<aDest.mLength){
PRInt32 theLength = GetSegmentLength(aSource, aSrcOffset, aCount);
if(aSrcOffset<aSource.mLength) {
//here's the only new case we have to handle.
//chars are really being inserted into our buffer...
if(aDest.mLength+theLength > 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<aDest.mLength){
PRUint32 theDelta=aDest.mLength-aDestOffset;
PRUint32 theLength=(theDelta<aCount) ? theDelta : aCount;
PRUint32 theLength=GetDeleteLength(aDest, aDestOffset, aCount);
if(aDestOffset+theLength<aDest.mLength) {
//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][KSHIFTLEFT])(aDest.mStr,aDest.mLength,aDestOffset,theLength);
ShiftCharsLeft(aDest.mStr,aDest.mLength,aDestOffset,theLength);
aDest.mLength-=theLength;
AddNullTerminator(aDest);
NSSTR_SEEN(aDest);
@ -305,6 +438,35 @@ void nsStr::Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){
}//if
}
void nsStr::Delete2(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount){
NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 byte");
if(aDestOffset<aDest.mLength){
PRUint32 theLength=GetDeleteLength(aDest, aDestOffset, aCount);
if(aDestOffset+theLength<aDest.mLength) {
//if you're here, it means we're cutting chars out of the middle of the string...
//so shift the chars left by theLength...
ShiftDoubleCharsLeft(aDest.mUStr,aDest.mLength,aDestOffset,theLength);
aDest.mLength-=theLength;
AddNullTerminator(aDest);
NSSTR_SEEN(aDest);
}
else StrTruncate(aDest,aDestOffset);
}//if
}
PRInt32 nsStr::GetDeleteLength(const nsStr& aDest, PRUint32 aDestOffset, PRUint32 aCount)
{
PRUint32 theDelta=aDest.mLength-aDestOffset;
PRUint32 theLength=(theDelta<aCount) ? theDelta : aCount;
return theLength;
}
/**
* This method truncates the given nsStr at given offset
* @update gess10/30/98
@ -319,7 +481,6 @@ void nsStr::StrTruncate(nsStr& aDest,PRUint32 aDestOffset){
}
}
/**
* This method removes characters from the given set from this string.
* NOTE: aSet is a char*, and it's length is computed using strlen, which assumes null termination.
@ -341,13 +502,17 @@ void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool a
if(aEliminateLeading) {
while(++theIndex<=theMax) {
PRUnichar theChar=GetCharAt(aDest,theIndex);
PRInt32 thePos=gFindChars[eOneByte](aSet,theSetLen,0,theChar,PR_FALSE,theSetLen);
PRInt32 thePos=FindChar1(aSet,theSetLen,0,theChar,PR_FALSE,theSetLen);
if(kNotFound==thePos)
break;
}
if(0<theIndex) {
if(theIndex<theMax) {
Delete(aDest,0,theIndex);
if (aDest.mCharSize == eOneByte)
Delete1(aDest,0,theIndex);
else
Delete2(aDest,0,theIndex);
}
else StrTruncate(aDest,0);
}
@ -358,7 +523,7 @@ void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool a
PRInt32 theNewLen=theIndex;
while(--theIndex>=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<thePos)
theNewLen=theIndex;
else break;
@ -377,9 +542,20 @@ void nsStr::Trim(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool a
* @param
* @return
*/
void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
void nsStr::CompressSet1(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte");
Trim(aDest,aSet,aEliminateLeading,aEliminateTrailing);
PRUint32 aNewLen=gCompressChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet);
PRUint32 aNewLen=CompressChars1(aDest.mStr,aDest.mLength,aSet);
aDest.mLength=aNewLen;
NSSTR_SEEN(aDest);
}
void nsStr::CompressSet2(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing){
NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 bytes");
Trim(aDest,aSet,aEliminateLeading,aEliminateTrailing);
PRUint32 aNewLen=CompressChars2(aDest.mUStr,aDest.mLength,aSet);
aDest.mLength=aNewLen;
NSSTR_SEEN(aDest);
}
@ -391,9 +567,21 @@ void nsStr::CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,P
* @param
* @return
*/
void nsStr::StripChars(nsStr& aDest,const char* aSet){
void nsStr::StripChars1(nsStr& aDest,const char* aSet){
NS_ASSERTION(aDest.mCharSize == eOneByte, "Must be 1 byte");
if((0<aDest.mLength) && (aSet)) {
PRUint32 aNewLen=gStripChars[aDest.mCharSize](aDest.mStr,aDest.mLength,aSet);
PRUint32 aNewLen=::StripChars1(aDest.mStr, aDest.mLength, aSet);
aDest.mLength=aNewLen;
NSSTR_SEEN(aDest);
}
}
void nsStr::StripChars2(nsStr& aDest,const char* aSet){
NS_ASSERTION(aDest.mCharSize == eTwoByte, "Must be 2 bytes");
if((0<aDest.mLength) && (aSet)) {
PRUint32 aNewLen=::StripChars2(aDest.mUStr, aDest.mLength, aSet);
aDest.mLength=aNewLen;
NSSTR_SEEN(aDest);
}
@ -522,7 +710,9 @@ PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget,PRBool aIgnor
if(aCount<0)
aCount = aDest.mLength;
if((0<aDest.mLength) && ((PRUint32)anOffset<aDest.mLength) && (aTarget.mLength)) {
if((0<aDest.mLength) &&
((PRUint32)anOffset<aDest.mLength) &&
(aTarget.mLength)) {
if(0<aCount) {
@ -642,6 +832,7 @@ TranslateCompareResult(const PRInt32 aDestLength, const PRInt32& aSourceLength,
return result;
}
/**
* Compare source and dest strings, up to an (optional max) number of chars
* @param aDest is the first str to compare

Просмотреть файл

@ -333,7 +333,18 @@ struct NS_COM nsStr {
* @param aCount tells us the (max) # of chars to insert
* @param anAgent is the allocator to be used for alloc/free operations
*/
static void StrInsert( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
static void StrInsert1into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
static void StrInsert1into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
static void StrInsert2into1( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
static void StrInsert2into2( nsStr& aDest,PRUint32 aDestOffset,const nsStr& aSource,PRUint32 aSrcOffset,PRInt32 aCount);
/**
* Helper routines for StrInsert1into1, etc
*/
static PRInt32 GetSegmentLength(const nsStr& aSource,
PRUint32 aSrcOffset, PRInt32 aCount);
static void AppendForInsert(nsStr& aDest, PRUint32 aDestOffset, const nsStr& aSource, PRUint32 aSrcOffset, PRInt32 theLength);
/**
* This method deletes chars from the given str.
@ -345,7 +356,13 @@ struct NS_COM nsStr {
* @param aCount tells us the (max) # of chars to delete
* @param anAgent is the allocator to be used for alloc/free operations
*/
static void Delete(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount);
static void Delete1(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount);
static void Delete2(nsStr& aDest,PRUint32 aDestOffset,PRUint32 aCount);
/**
* helper routines for Delete1, Delete2
*/
static PRInt32 GetDeleteLength(const nsStr& aDest, PRUint32 aDestOffset, PRUint32 aCount);
/**
* This method is used to truncate the given string.
@ -381,7 +398,8 @@ struct NS_COM nsStr {
* @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 CompressSet(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing);
static void CompressSet1(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing);
static void CompressSet2(nsStr& aDest,const char* aSet,PRBool aEliminateLeading,PRBool aEliminateTrailing);
/**
* This method removes all occurances of chars in given set from aDest
@ -393,7 +411,8 @@ struct NS_COM nsStr {
* @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);
static void StripChars1(nsStr& aDest,const char* aSet);
static void StripChars2(nsStr& aDest,const char* aSet);
/**
* This method compares the data bewteen two nsStr's

Просмотреть файл

@ -287,7 +287,7 @@ nsCString::StripChar(PRUnichar aChar,PRInt32 anOffset){
*/
void
nsCString::StripChars(const char* aSet){
nsStr::StripChars(*this,aSet);
nsStr::StripChars1(*this,aSet);
}
@ -396,14 +396,14 @@ nsCString::ReplaceSubstring(const nsCString& aTarget,const nsCString& aNewValue)
if(aNewValue.mLength<aTarget.mLength) {
//Since target is longer than newValue, we should delete a few chars first, then overwrite.
PRInt32 theDelLen=aTarget.mLength-aNewValue.mLength;
nsStr::Delete(*this,theIndex,theDelLen);
nsStr::Delete1(*this,theIndex,theDelLen);
nsStr::Overwrite(*this,aNewValue,theIndex);
}
else {
//this is the worst case: the newvalue is larger than the substr it's replacing
//so we have to insert some characters...
PRInt32 theInsLen=aNewValue.mLength-aTarget.mLength;
StrInsert(*this,theIndex,aNewValue,0,theInsLen);
StrInsert1into1(*this,theIndex,aNewValue,0,theInsLen);
nsStr::Overwrite(*this,aNewValue,theIndex);
theIndex += aNewValue.mLength;
}
@ -467,7 +467,7 @@ void
nsCString::CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){
if(aSet){
ReplaceChar(aSet,aChar);
nsStr::CompressSet(*this,aSet,aEliminateLeading,aEliminateTrailing);
nsStr::CompressSet1(*this,aSet,aEliminateLeading,aEliminateTrailing);
}
}
@ -846,7 +846,7 @@ void nsCString::InsertWithConversion(PRUnichar aChar,PRUint32 anOffset){
nsStr::Initialize(temp,eTwoByte);
temp.mUStr=theBuffer;
temp.mLength=1;
StrInsert(*this,anOffset,temp,0,1);
StrInsert2into1(*this,anOffset,temp,0,1);
}

Просмотреть файл

@ -299,7 +299,7 @@ nsString::StripChar(PRUnichar aChar,PRInt32 anOffset){
*/
void
nsString::StripChars(const char* aSet){
nsStr::StripChars(*this,aSet);
nsStr::StripChars2(*this,aSet);
}
@ -413,14 +413,14 @@ nsString::ReplaceSubstring(const nsString& aTarget,const nsString& aNewValue){
if(aNewValue.mLength<aTarget.mLength) {
//Since target is longer than newValue, we should delete a few chars first, then overwrite.
PRInt32 theDelLen=aTarget.mLength-aNewValue.mLength;
nsStr::Delete(*this,theIndex,theDelLen);
nsStr::Delete2(*this,theIndex,theDelLen);
nsStr::Overwrite(*this,aNewValue,theIndex);
}
else {
//this is the worst case: the newvalue is larger than the substr it's replacing
//so we have to insert some characters...
PRInt32 theInsLen=aNewValue.mLength-aTarget.mLength;
StrInsert(*this,theIndex,aNewValue,0,theInsLen);
StrInsert2into2(*this,theIndex,aNewValue,0,theInsLen);
nsStr::Overwrite(*this,aNewValue,theIndex);
theIndex += aNewValue.mLength;
}
@ -484,7 +484,7 @@ void
nsString::CompressSet(const char* aSet, PRUnichar aChar,PRBool aEliminateLeading,PRBool aEliminateTrailing){
if(aSet){
ReplaceChar(aSet,aChar);
nsStr::CompressSet(*this,aSet,aEliminateLeading,aEliminateTrailing);
nsStr::CompressSet2(*this,aSet,aEliminateLeading,aEliminateTrailing);
}
}
@ -847,7 +847,7 @@ void nsString::InsertWithConversion(const char* aCString,PRUint32 anOffset,PRInt
else aCount=temp.mLength=nsCRT::strlen(aCString);
if(0<aCount){
StrInsert(*this,anOffset,temp,0,aCount);
StrInsert1into2(*this,anOffset,temp,0,aCount);
}
}
}