fixed off-by-1 error in compare, and added ReplaceSubstring(). r=syd
git-svn-id: svn://10.0.0.236/trunk@55066 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
@@ -405,6 +405,73 @@ nsCString& nsCString::ReplaceChar(const char* aSet, PRUnichar aNewChar){
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to replace all occurances of the
|
||||
* given target with the given replacement
|
||||
*
|
||||
* @param
|
||||
* @return *this
|
||||
*/
|
||||
nsCString& nsCString::ReplaceSubstring(const char* aTarget,const char* aNewValue){
|
||||
if(aTarget && aNewValue) {
|
||||
|
||||
PRInt32 len=strlen(aTarget);
|
||||
if(0<len) {
|
||||
CBufDescriptor theDesc1(aTarget,PR_TRUE, len+1,len);
|
||||
nsCAutoString theTarget(theDesc1);
|
||||
|
||||
len=strlen(aNewValue);
|
||||
if(0<len) {
|
||||
CBufDescriptor theDesc2(aNewValue,PR_TRUE, len+1,len);
|
||||
nsCAutoString theNewValue(theDesc2);
|
||||
|
||||
ReplaceSubstring(theTarget,theNewValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to replace all occurances of the
|
||||
* given target substring with the given replacement substring
|
||||
*
|
||||
* @param aTarget
|
||||
* @param aNewValue
|
||||
* @return *this
|
||||
*/
|
||||
nsCString& nsCString::ReplaceSubstring(const nsCString& aTarget,const nsCString& aNewValue){
|
||||
|
||||
|
||||
//WARNING: This is not working yet!!!!!
|
||||
|
||||
if(aTarget.mLength && aNewValue.mLength) {
|
||||
PRBool isSameLen=(aTarget.mLength==aNewValue.mLength);
|
||||
|
||||
if((isSameLen) && (1==aNewValue.mLength)) {
|
||||
ReplaceChar(aTarget.CharAt(0),aNewValue.CharAt(0));
|
||||
}
|
||||
else {
|
||||
PRInt32 theIndex=0;
|
||||
while(kNotFound!=(theIndex=nsStr::FindSubstr(*this,aTarget,PR_FALSE,theIndex))) {
|
||||
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);
|
||||
}
|
||||
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;
|
||||
nsStr::Insert(*this,theIndex,aNewValue,0,theInsLen);
|
||||
}
|
||||
nsStr::Overwrite(*this,aNewValue,theIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to replace all occurances of the
|
||||
* given source char with the given dest char
|
||||
|
||||
Reference in New Issue
Block a user