зеркало из https://github.com/mozilla/pjs.git
major perf mods for bug 27524, and removed deprecated methods; r=harishd
This commit is contained in:
Родитель
a93c35b663
Коммит
618b491821
|
@ -389,44 +389,53 @@ void nsStr::StripChars(nsStr& aDest,const char* aSet){
|
|||
Searching methods...
|
||||
**************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* This searches aDest for a given substring
|
||||
*
|
||||
* @update gess 3/25/98
|
||||
* @update gess 2/04/00: added aCount argument to restrict search
|
||||
* @param aDest string to search
|
||||
* @param aTarget is the substring you're trying to find.
|
||||
* @param aIgnorecase indicates case sensitivity of search
|
||||
* @param anOffset tells us where to start the search
|
||||
* @param aCount tells us how many iterations to make from offset; -1 means the full length of the string
|
||||
* @return index in aDest where member of aSet occurs, or -1 if not found
|
||||
*/
|
||||
PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
||||
// NS_PRECONDITION(aTarget.mLength!=1,kCallFindChar);
|
||||
PRInt32 theMaxPos = aDest.mLength-aTarget.mLength; //this is the last pos that is feasible for starting the search, with given lengths...
|
||||
|
||||
PRInt32 result=kNotFound;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<(PRInt32)aDest.mLength)) {
|
||||
PRInt32 theMax=aDest.mLength-aTarget.mLength;
|
||||
PRInt32 index=(0<=anOffset) ? anOffset : 0;
|
||||
|
||||
if((aDest.mLength>=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){
|
||||
PRInt32 theTargetMax=aTarget.mLength;
|
||||
while(index<=theMax) {
|
||||
PRInt32 theSubIndex=-1;
|
||||
PRBool matches=PR_TRUE;
|
||||
while((++theSubIndex<theTargetMax) && (matches)){
|
||||
PRUnichar theChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex);
|
||||
PRUnichar theTargetChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aTarget,theSubIndex)) : GetCharAt(aTarget,theSubIndex);
|
||||
matches=PRBool(theChar==theTargetChar);
|
||||
}
|
||||
if(matches) {
|
||||
result=index;
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
} //while
|
||||
}//if
|
||||
}//if
|
||||
return result;
|
||||
if(0<=theMaxPos) {
|
||||
|
||||
if(anOffset<0)
|
||||
anOffset=0;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<=theMaxPos) && (aTarget.mLength)) {
|
||||
|
||||
if(aCount<0)
|
||||
aCount = MaxInt(theMaxPos,1);
|
||||
|
||||
if(0<aCount) {
|
||||
|
||||
PRInt32 aDelta= (aDest.mCharSize == eOneByte) ? 1 : 2;
|
||||
const char* root = aDest.mStr;
|
||||
const char* left = root+(anOffset*aDelta);
|
||||
const char* last = left+((aCount)*aDelta);
|
||||
const char* max = root+(theMaxPos*aDelta);
|
||||
const char* right = (last<max) ? last : max;
|
||||
|
||||
while(left<=right){
|
||||
PRInt32 cmp=(*gCompare[aDest.mCharSize][aTarget.mCharSize])(left,aTarget.mStr,aTarget.mLength,aIgnoreCase);
|
||||
if(0==cmp) {
|
||||
return (left-root)/aDelta;
|
||||
}
|
||||
left+=aDelta;
|
||||
} //while
|
||||
|
||||
} //if
|
||||
}
|
||||
} //if
|
||||
|
||||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
|
@ -483,52 +492,47 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore
|
|||
/**
|
||||
* This searches aDest (in reverse) for a given substring
|
||||
*
|
||||
* @update gess 3/25/98
|
||||
* @update gess 2/04/00
|
||||
* @param aDest string to search
|
||||
* @param aTarget is the substring you're trying to find.
|
||||
* @param aIgnorecase indicates case sensitivity of search
|
||||
* @param anOffset tells us where to start the search (counting from left)
|
||||
* @param aCount tell us how many iterations to perform from offset
|
||||
* @return index in aDest where member of aSet occurs, or -1 if not found
|
||||
*/
|
||||
PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
||||
//NS_PRECONDITION(aTarget.mLength!=1,kCallRFindChar);
|
||||
if(anOffset<0)
|
||||
anOffset=(PRInt32)aDest.mLength-1;
|
||||
|
||||
PRInt32 result=kNotFound;
|
||||
if(aCount<0)
|
||||
aCount = aDest.mLength;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<(PRInt32)aDest.mLength)) {
|
||||
PRInt32 index=(0<=anOffset) ? anOffset : aDest.mLength-1;
|
||||
if((0<aDest.mLength) && ((PRUint32)anOffset<aDest.mLength) && (aTarget.mLength)) {
|
||||
|
||||
if(0<aCount) {
|
||||
|
||||
PRInt32 aDelta = (aDest.mCharSize == eOneByte) ? 1 : 2;
|
||||
const char* root = aDest.mStr;
|
||||
const char* destLast = root+((aDest.mLength-1)*aDelta); //pts to last char in aDest (likely null)
|
||||
|
||||
if((aDest.mLength>=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){
|
||||
const char* rightmost = root+(anOffset*aDelta);
|
||||
const char* min = rightmost-((aCount-1)*aDelta);
|
||||
|
||||
nsStr theCopy;
|
||||
nsStr::Initialize(theCopy,eOneByte);
|
||||
nsStr::Assign(theCopy,aTarget,0,aTarget.mLength);
|
||||
if(aIgnoreCase){
|
||||
nsStr::ChangeCase(theCopy,PR_FALSE); //force to lowercase
|
||||
}
|
||||
|
||||
PRInt32 theTargetMax=theCopy.mLength;
|
||||
while(index>=0) {
|
||||
PRInt32 theSubIndex=-1;
|
||||
PRBool matches=PR_FALSE;
|
||||
if(index+theCopy.mLength<=aDest.mLength) {
|
||||
matches=PR_TRUE;
|
||||
while((++theSubIndex<theTargetMax) && (matches)){
|
||||
PRUnichar theDestChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex);
|
||||
PRUnichar theTargetChar=GetCharAt(theCopy,theSubIndex);
|
||||
matches=PRBool(theDestChar==theTargetChar);
|
||||
} //while
|
||||
const char* leftmost = (min<root) ? root: min;
|
||||
|
||||
while(leftmost<=rightmost) {
|
||||
if(aTarget.mLength<=PRUint32(destLast-rightmost)) {
|
||||
PRInt32 result=(*gCompare[aDest.mCharSize][aTarget.mCharSize])(rightmost,aTarget.mStr,aTarget.mLength,aIgnoreCase);
|
||||
if(0==result) {
|
||||
return (rightmost-root)/aDelta;
|
||||
}
|
||||
} //if
|
||||
if(matches) {
|
||||
result=index;
|
||||
break;
|
||||
}
|
||||
index--;
|
||||
rightmost-=aDelta;
|
||||
} //while
|
||||
nsStr::Destroy(theCopy);
|
||||
}//if
|
||||
}//if
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
|
@ -547,6 +551,7 @@ PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,
|
|||
return gRFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase,aCount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This searches aDest (in reverese) for a character found in aSet.
|
||||
*
|
||||
|
@ -576,7 +581,6 @@ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnor
|
|||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compare source and dest strings, up to an (optional max) number of chars
|
||||
* @param aDest is the first str to compare
|
||||
|
|
|
@ -657,213 +657,16 @@ float nsCString::ToFloat(PRInt32* aErrorCode) const {
|
|||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform numeric string to int conversion with given radix.
|
||||
* NOTE: 1. This method mandates that the string is well formed and uppercased.
|
||||
* 2. This method will return an error if the string you give
|
||||
contains chars outside the range for the specified radix.
|
||||
|
||||
* @update gess 10/01/98
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the string in.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
|
||||
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
char* cp = aString.mStr + aString.mLength;
|
||||
PRInt32 theMult=1;
|
||||
|
||||
*anErrorCode = NS_OK;
|
||||
|
||||
//now iterate the numeric chars and build our result
|
||||
char theChar=0;
|
||||
char theDigit=0;
|
||||
while(--cp>=aString.mStr){
|
||||
theChar=*cp;
|
||||
if((theChar>='0') && (theChar<='9')){
|
||||
theDigit=theChar-'0';
|
||||
}
|
||||
else if((theChar>='A') && (theChar<='F')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'A')+10;
|
||||
}
|
||||
else if((theChar>='a') && (theChar<='f')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'a')+10;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
result=-result;
|
||||
break;
|
||||
}
|
||||
else if(('+'==theChar) || (' '==theChar) || ('X'==theChar) || ('x'==theChar)) { //stop in a good state if you see this...
|
||||
break;
|
||||
}
|
||||
else {
|
||||
//we've encountered a char that's not a legal number or sign
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
|
||||
result+=theDigit*theMult;
|
||||
theMult*=aRadix;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Call this method to extract the rightmost numeric value from the given
|
||||
* 1-byte input string, and simultaneously determine the radix.
|
||||
* NOTE: This method mandates that the string is well formed.
|
||||
* Leading and trailing gunk should be removed, and upper-cased.
|
||||
* @update gess 10/01/98
|
||||
* @param anInputString contains orig string
|
||||
* @param anOutString contains numeric portion copy of input string
|
||||
* @param aRadix (an out parm) tells the caller what base we think the string is in.
|
||||
* @return non-zero error code if this string is non-numeric
|
||||
*/
|
||||
static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) {
|
||||
|
||||
const char* cp=aString.mStr;
|
||||
PRInt32 result=NS_ERROR_ILLEGAL_VALUE;
|
||||
if(cp) {
|
||||
|
||||
aRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* to=(char*)cp;
|
||||
const char* endcp=cp+aString.mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while(!done){
|
||||
switch(*cp) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
aRadix=16;
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
case '-': case '+': case '#':
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
default:
|
||||
cp++;
|
||||
done=(cp==endcp);
|
||||
break;
|
||||
} //switch
|
||||
}
|
||||
|
||||
while(cp<endcp) {
|
||||
char theChar=*cp;
|
||||
|
||||
if('A'<=theChar) {
|
||||
if('F'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('X'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
else if('a'<=theChar) {
|
||||
if('f'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++='A'+(theChar-'a');
|
||||
}
|
||||
else if('x'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
}
|
||||
else break; //bad char
|
||||
}
|
||||
else if((theChar>='0') && (theChar<='9')) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if(('#'!=theChar) && ('+'!=theChar)){
|
||||
break; //terminate on invalid char!
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
aString.Truncate(to-aString.mStr);
|
||||
result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method tries to autodetect that radix given a string
|
||||
* @update gess 10/01/98
|
||||
* @return 10,16,or 0 (meaning I don't know)
|
||||
*/
|
||||
PRUint32 nsCString::DetermineRadix(void) {
|
||||
PRUint32 result=kRadixUnknown;
|
||||
if(0<mLength) {
|
||||
//nsCAutoString theString(*this);
|
||||
nsSubsumeCStr theString(mStr,mLength);
|
||||
if(NS_OK!=GetNumericSubstring(theString,result))
|
||||
result=kRadixUnknown;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform decimal numeric string to int conversion.
|
||||
* NOTE: In this version, we use the radix you give, even if it's wrong.
|
||||
* @update gess 10/01/98
|
||||
* @update gess 02/14/00
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the given string in.
|
||||
* @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
||||
#if 1
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
if(0<mLength) {
|
||||
|
||||
nsCAutoString theString(mStr,mLength);
|
||||
PRUint32 theRadix=aRadix;
|
||||
|
||||
*anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
|
||||
|
||||
if(NS_OK==*anErrorCode){
|
||||
if(kAutoDetect==aRadix)
|
||||
aRadix=theRadix;
|
||||
if((kRadix10==aRadix) || (kRadix16==aRadix))
|
||||
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
|
||||
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
#else
|
||||
char* cp=aString.mStr;
|
||||
char* cp=mStr;
|
||||
PRInt32 theRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
PRInt32 result=0;
|
||||
PRBool negate=PR_FALSE;
|
||||
|
@ -875,7 +678,7 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* endcp=cp+aString.mLength;
|
||||
char* endcp=cp+mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while((cp<endcp) && (!done)){
|
||||
|
@ -961,7 +764,6 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
} //if
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
|
|
|
@ -343,16 +343,12 @@ public:
|
|||
*/
|
||||
float ToFloat(PRInt32* aErrorCode) const;
|
||||
|
||||
/**
|
||||
* Try to derive the radix from the value contained in this string
|
||||
* @return kRadix10, kRadix16 or kAutoDetect (meaning unknown)
|
||||
*/
|
||||
PRUint32 DetermineRadix(void);
|
||||
|
||||
/**
|
||||
* Perform string to int conversion.
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @return int rep of string value
|
||||
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
|
||||
* @return int rep of string value, and possible (out) error code
|
||||
*/
|
||||
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
|
||||
|
||||
|
|
|
@ -827,201 +827,17 @@ float nsString::ToFloat(PRInt32* aErrorCode) const {
|
|||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform numeric string to int conversion with given radix.
|
||||
* NOTE: 1. This method mandates that the string is well formed and uppercased
|
||||
* 2. This method will return an error if the string you give
|
||||
contains chars outside the range for the specified radix.
|
||||
|
||||
* @update gess 10/01/98
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the string in.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
static PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
|
||||
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
char* cp = aString.mStr + aString.mLength;
|
||||
PRInt32 theMult=1;
|
||||
|
||||
*anErrorCode = NS_OK;
|
||||
|
||||
//now iterate the numeric chars and build our result
|
||||
char theDigit=0;
|
||||
while(--cp>=aString.mStr){
|
||||
char theChar=*cp;
|
||||
if((theChar>='0') && (theChar<='9')){
|
||||
theDigit=theChar-'0';
|
||||
}
|
||||
else if((theChar>='A') && (theChar<='F')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'A')+10;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
result=-result;
|
||||
break;
|
||||
}
|
||||
else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this...
|
||||
break;
|
||||
}
|
||||
else {
|
||||
//we've encountered a char that's not a legal number or sign
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
|
||||
result+=theDigit*theMult;
|
||||
theMult*=aRadix;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this method to extract the rightmost numeric value from the given
|
||||
* 1-byte input string, and simultaneously determine the radix.
|
||||
* NOTE: This method mandates that the string is well formed.
|
||||
* Leading and trailing gunk should be removed, and the case upper.
|
||||
* @update gess 10/01/98
|
||||
* @param anInputString contains orig string
|
||||
* @param anOutString contains numeric portion copy of input string
|
||||
* @param aRadix (an out parm) tells the caller what base we think the string is in.
|
||||
* @return non-zero error code if this string is non-numeric
|
||||
*/
|
||||
static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) {
|
||||
|
||||
const char* cp=aString.mStr;
|
||||
PRInt32 result=NS_ERROR_ILLEGAL_VALUE;
|
||||
if(cp) {
|
||||
|
||||
aRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* to=(char*)cp;
|
||||
const char* endcp=cp+aString.mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while(!done){
|
||||
switch(*cp) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
aRadix=16;
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
case '-': case '+': case '#':
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
default:
|
||||
cp++;
|
||||
done=(cp==endcp);
|
||||
break;
|
||||
} //switch
|
||||
}
|
||||
|
||||
while(cp<endcp) {
|
||||
char theChar=*cp;
|
||||
|
||||
if('A'<=theChar) {
|
||||
if('F'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('X'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
else if('a'<=theChar) {
|
||||
if('f'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++='A'+(theChar-'a');
|
||||
}
|
||||
else if('x'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
}
|
||||
else break; //bad char
|
||||
}
|
||||
else if((theChar>='0') && (theChar<='9')) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if(('#'!=theChar) && ('+'!=theChar)){
|
||||
break; //terminate on invalid char!
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
aString.Truncate(to-aString.mStr);
|
||||
result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method tries to autodetect that radix given a string
|
||||
* @update gess 10/01/98
|
||||
* @return 10,16,or 0 (meaning I don't know)
|
||||
*/
|
||||
PRUint32 nsString::DetermineRadix(void) {
|
||||
PRUint32 result=kRadixUnknown;
|
||||
if(0<mLength) {
|
||||
nsCAutoString theString(*this);
|
||||
if(NS_OK!=GetNumericSubstring(theString,result))
|
||||
result=kRadixUnknown;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform decimal numeric string to int conversion.
|
||||
* NOTE: In this version, we use the radix you give, even if it's wrong.
|
||||
* @update gess 10/01/98
|
||||
* @update gess 02/14/00
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the given string in.
|
||||
* @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
||||
#if 1
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
if(0<mLength) {
|
||||
|
||||
nsCAutoString theString(mUStr,mLength);
|
||||
PRUint32 theRadix=aRadix;
|
||||
|
||||
*anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
|
||||
|
||||
if(NS_OK==*anErrorCode){
|
||||
if(kAutoDetect==aRadix)
|
||||
aRadix=theRadix;
|
||||
if((kRadix10==aRadix) || (kRadix16==aRadix))
|
||||
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
|
||||
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
#else
|
||||
PRUnichar* cp=aString.mUStr;
|
||||
PRUnichar* cp=mUStr;
|
||||
PRInt32 theRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
PRInt32 result=0;
|
||||
PRBool negate=PR_FALSE;
|
||||
|
@ -1033,7 +849,7 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
PRUnichar* endcp=cp+aString.mLength;
|
||||
PRUnichar* endcp=cp+mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while((cp<endcp) && (!done)){
|
||||
|
@ -1119,7 +935,6 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
} //if
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
|
|
|
@ -386,16 +386,11 @@ public:
|
|||
*/
|
||||
float ToFloat(PRInt32* aErrorCode) const;
|
||||
|
||||
/**
|
||||
* Try to derive the radix from the value contained in this string
|
||||
* @return kRadix10, kRadix16 or kAutoDetect (meaning unknown)
|
||||
*/
|
||||
PRUint32 DetermineRadix(void);
|
||||
|
||||
/**
|
||||
* Perform string to int conversion.
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @return int rep of string value
|
||||
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
|
||||
* @return int rep of string value, and possible (out) error code
|
||||
*/
|
||||
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
|
||||
|
||||
|
|
|
@ -389,44 +389,53 @@ void nsStr::StripChars(nsStr& aDest,const char* aSet){
|
|||
Searching methods...
|
||||
**************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* This searches aDest for a given substring
|
||||
*
|
||||
* @update gess 3/25/98
|
||||
* @update gess 2/04/00: added aCount argument to restrict search
|
||||
* @param aDest string to search
|
||||
* @param aTarget is the substring you're trying to find.
|
||||
* @param aIgnorecase indicates case sensitivity of search
|
||||
* @param anOffset tells us where to start the search
|
||||
* @param aCount tells us how many iterations to make from offset; -1 means the full length of the string
|
||||
* @return index in aDest where member of aSet occurs, or -1 if not found
|
||||
*/
|
||||
PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
||||
// NS_PRECONDITION(aTarget.mLength!=1,kCallFindChar);
|
||||
PRInt32 theMaxPos = aDest.mLength-aTarget.mLength; //this is the last pos that is feasible for starting the search, with given lengths...
|
||||
|
||||
PRInt32 result=kNotFound;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<(PRInt32)aDest.mLength)) {
|
||||
PRInt32 theMax=aDest.mLength-aTarget.mLength;
|
||||
PRInt32 index=(0<=anOffset) ? anOffset : 0;
|
||||
|
||||
if((aDest.mLength>=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){
|
||||
PRInt32 theTargetMax=aTarget.mLength;
|
||||
while(index<=theMax) {
|
||||
PRInt32 theSubIndex=-1;
|
||||
PRBool matches=PR_TRUE;
|
||||
while((++theSubIndex<theTargetMax) && (matches)){
|
||||
PRUnichar theChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex);
|
||||
PRUnichar theTargetChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aTarget,theSubIndex)) : GetCharAt(aTarget,theSubIndex);
|
||||
matches=PRBool(theChar==theTargetChar);
|
||||
}
|
||||
if(matches) {
|
||||
result=index;
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
} //while
|
||||
}//if
|
||||
}//if
|
||||
return result;
|
||||
if(0<=theMaxPos) {
|
||||
|
||||
if(anOffset<0)
|
||||
anOffset=0;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<=theMaxPos) && (aTarget.mLength)) {
|
||||
|
||||
if(aCount<0)
|
||||
aCount = MaxInt(theMaxPos,1);
|
||||
|
||||
if(0<aCount) {
|
||||
|
||||
PRInt32 aDelta= (aDest.mCharSize == eOneByte) ? 1 : 2;
|
||||
const char* root = aDest.mStr;
|
||||
const char* left = root+(anOffset*aDelta);
|
||||
const char* last = left+((aCount)*aDelta);
|
||||
const char* max = root+(theMaxPos*aDelta);
|
||||
const char* right = (last<max) ? last : max;
|
||||
|
||||
while(left<=right){
|
||||
PRInt32 cmp=(*gCompare[aDest.mCharSize][aTarget.mCharSize])(left,aTarget.mStr,aTarget.mLength,aIgnoreCase);
|
||||
if(0==cmp) {
|
||||
return (left-root)/aDelta;
|
||||
}
|
||||
left+=aDelta;
|
||||
} //while
|
||||
|
||||
} //if
|
||||
}
|
||||
} //if
|
||||
|
||||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
|
@ -483,52 +492,47 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore
|
|||
/**
|
||||
* This searches aDest (in reverse) for a given substring
|
||||
*
|
||||
* @update gess 3/25/98
|
||||
* @update gess 2/04/00
|
||||
* @param aDest string to search
|
||||
* @param aTarget is the substring you're trying to find.
|
||||
* @param aIgnorecase indicates case sensitivity of search
|
||||
* @param anOffset tells us where to start the search (counting from left)
|
||||
* @param aCount tell us how many iterations to perform from offset
|
||||
* @return index in aDest where member of aSet occurs, or -1 if not found
|
||||
*/
|
||||
PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
||||
//NS_PRECONDITION(aTarget.mLength!=1,kCallRFindChar);
|
||||
if(anOffset<0)
|
||||
anOffset=(PRInt32)aDest.mLength-1;
|
||||
|
||||
PRInt32 result=kNotFound;
|
||||
if(aCount<0)
|
||||
aCount = aDest.mLength;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<(PRInt32)aDest.mLength)) {
|
||||
PRInt32 index=(0<=anOffset) ? anOffset : aDest.mLength-1;
|
||||
if((0<aDest.mLength) && ((PRUint32)anOffset<aDest.mLength) && (aTarget.mLength)) {
|
||||
|
||||
if(0<aCount) {
|
||||
|
||||
PRInt32 aDelta = (aDest.mCharSize == eOneByte) ? 1 : 2;
|
||||
const char* root = aDest.mStr;
|
||||
const char* destLast = root+((aDest.mLength-1)*aDelta); //pts to last char in aDest (likely null)
|
||||
|
||||
if((aDest.mLength>=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){
|
||||
const char* rightmost = root+(anOffset*aDelta);
|
||||
const char* min = rightmost-((aCount-1)*aDelta);
|
||||
|
||||
nsStr theCopy;
|
||||
nsStr::Initialize(theCopy,eOneByte);
|
||||
nsStr::Assign(theCopy,aTarget,0,aTarget.mLength);
|
||||
if(aIgnoreCase){
|
||||
nsStr::ChangeCase(theCopy,PR_FALSE); //force to lowercase
|
||||
}
|
||||
|
||||
PRInt32 theTargetMax=theCopy.mLength;
|
||||
while(index>=0) {
|
||||
PRInt32 theSubIndex=-1;
|
||||
PRBool matches=PR_FALSE;
|
||||
if(index+theCopy.mLength<=aDest.mLength) {
|
||||
matches=PR_TRUE;
|
||||
while((++theSubIndex<theTargetMax) && (matches)){
|
||||
PRUnichar theDestChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex);
|
||||
PRUnichar theTargetChar=GetCharAt(theCopy,theSubIndex);
|
||||
matches=PRBool(theDestChar==theTargetChar);
|
||||
} //while
|
||||
const char* leftmost = (min<root) ? root: min;
|
||||
|
||||
while(leftmost<=rightmost) {
|
||||
if(aTarget.mLength<=PRUint32(destLast-rightmost)) {
|
||||
PRInt32 result=(*gCompare[aDest.mCharSize][aTarget.mCharSize])(rightmost,aTarget.mStr,aTarget.mLength,aIgnoreCase);
|
||||
if(0==result) {
|
||||
return (rightmost-root)/aDelta;
|
||||
}
|
||||
} //if
|
||||
if(matches) {
|
||||
result=index;
|
||||
break;
|
||||
}
|
||||
index--;
|
||||
rightmost-=aDelta;
|
||||
} //while
|
||||
nsStr::Destroy(theCopy);
|
||||
}//if
|
||||
}//if
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
|
@ -547,6 +551,7 @@ PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,
|
|||
return gRFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase,aCount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This searches aDest (in reverese) for a character found in aSet.
|
||||
*
|
||||
|
@ -576,7 +581,6 @@ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnor
|
|||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compare source and dest strings, up to an (optional max) number of chars
|
||||
* @param aDest is the first str to compare
|
||||
|
|
|
@ -657,213 +657,16 @@ float nsCString::ToFloat(PRInt32* aErrorCode) const {
|
|||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform numeric string to int conversion with given radix.
|
||||
* NOTE: 1. This method mandates that the string is well formed and uppercased.
|
||||
* 2. This method will return an error if the string you give
|
||||
contains chars outside the range for the specified radix.
|
||||
|
||||
* @update gess 10/01/98
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the string in.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
|
||||
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
char* cp = aString.mStr + aString.mLength;
|
||||
PRInt32 theMult=1;
|
||||
|
||||
*anErrorCode = NS_OK;
|
||||
|
||||
//now iterate the numeric chars and build our result
|
||||
char theChar=0;
|
||||
char theDigit=0;
|
||||
while(--cp>=aString.mStr){
|
||||
theChar=*cp;
|
||||
if((theChar>='0') && (theChar<='9')){
|
||||
theDigit=theChar-'0';
|
||||
}
|
||||
else if((theChar>='A') && (theChar<='F')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'A')+10;
|
||||
}
|
||||
else if((theChar>='a') && (theChar<='f')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'a')+10;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
result=-result;
|
||||
break;
|
||||
}
|
||||
else if(('+'==theChar) || (' '==theChar) || ('X'==theChar) || ('x'==theChar)) { //stop in a good state if you see this...
|
||||
break;
|
||||
}
|
||||
else {
|
||||
//we've encountered a char that's not a legal number or sign
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
|
||||
result+=theDigit*theMult;
|
||||
theMult*=aRadix;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Call this method to extract the rightmost numeric value from the given
|
||||
* 1-byte input string, and simultaneously determine the radix.
|
||||
* NOTE: This method mandates that the string is well formed.
|
||||
* Leading and trailing gunk should be removed, and upper-cased.
|
||||
* @update gess 10/01/98
|
||||
* @param anInputString contains orig string
|
||||
* @param anOutString contains numeric portion copy of input string
|
||||
* @param aRadix (an out parm) tells the caller what base we think the string is in.
|
||||
* @return non-zero error code if this string is non-numeric
|
||||
*/
|
||||
static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) {
|
||||
|
||||
const char* cp=aString.mStr;
|
||||
PRInt32 result=NS_ERROR_ILLEGAL_VALUE;
|
||||
if(cp) {
|
||||
|
||||
aRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* to=(char*)cp;
|
||||
const char* endcp=cp+aString.mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while(!done){
|
||||
switch(*cp) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
aRadix=16;
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
case '-': case '+': case '#':
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
default:
|
||||
cp++;
|
||||
done=(cp==endcp);
|
||||
break;
|
||||
} //switch
|
||||
}
|
||||
|
||||
while(cp<endcp) {
|
||||
char theChar=*cp;
|
||||
|
||||
if('A'<=theChar) {
|
||||
if('F'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('X'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
else if('a'<=theChar) {
|
||||
if('f'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++='A'+(theChar-'a');
|
||||
}
|
||||
else if('x'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
}
|
||||
else break; //bad char
|
||||
}
|
||||
else if((theChar>='0') && (theChar<='9')) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if(('#'!=theChar) && ('+'!=theChar)){
|
||||
break; //terminate on invalid char!
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
aString.Truncate(to-aString.mStr);
|
||||
result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method tries to autodetect that radix given a string
|
||||
* @update gess 10/01/98
|
||||
* @return 10,16,or 0 (meaning I don't know)
|
||||
*/
|
||||
PRUint32 nsCString::DetermineRadix(void) {
|
||||
PRUint32 result=kRadixUnknown;
|
||||
if(0<mLength) {
|
||||
//nsCAutoString theString(*this);
|
||||
nsSubsumeCStr theString(mStr,mLength);
|
||||
if(NS_OK!=GetNumericSubstring(theString,result))
|
||||
result=kRadixUnknown;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform decimal numeric string to int conversion.
|
||||
* NOTE: In this version, we use the radix you give, even if it's wrong.
|
||||
* @update gess 10/01/98
|
||||
* @update gess 02/14/00
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the given string in.
|
||||
* @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
||||
#if 1
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
if(0<mLength) {
|
||||
|
||||
nsCAutoString theString(mStr,mLength);
|
||||
PRUint32 theRadix=aRadix;
|
||||
|
||||
*anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
|
||||
|
||||
if(NS_OK==*anErrorCode){
|
||||
if(kAutoDetect==aRadix)
|
||||
aRadix=theRadix;
|
||||
if((kRadix10==aRadix) || (kRadix16==aRadix))
|
||||
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
|
||||
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
#else
|
||||
char* cp=aString.mStr;
|
||||
char* cp=mStr;
|
||||
PRInt32 theRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
PRInt32 result=0;
|
||||
PRBool negate=PR_FALSE;
|
||||
|
@ -875,7 +678,7 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* endcp=cp+aString.mLength;
|
||||
char* endcp=cp+mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while((cp<endcp) && (!done)){
|
||||
|
@ -961,7 +764,6 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
} //if
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
|
|
|
@ -343,16 +343,12 @@ public:
|
|||
*/
|
||||
float ToFloat(PRInt32* aErrorCode) const;
|
||||
|
||||
/**
|
||||
* Try to derive the radix from the value contained in this string
|
||||
* @return kRadix10, kRadix16 or kAutoDetect (meaning unknown)
|
||||
*/
|
||||
PRUint32 DetermineRadix(void);
|
||||
|
||||
/**
|
||||
* Perform string to int conversion.
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @return int rep of string value
|
||||
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
|
||||
* @return int rep of string value, and possible (out) error code
|
||||
*/
|
||||
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
|
||||
|
||||
|
|
|
@ -827,201 +827,17 @@ float nsString::ToFloat(PRInt32* aErrorCode) const {
|
|||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform numeric string to int conversion with given radix.
|
||||
* NOTE: 1. This method mandates that the string is well formed and uppercased
|
||||
* 2. This method will return an error if the string you give
|
||||
contains chars outside the range for the specified radix.
|
||||
|
||||
* @update gess 10/01/98
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the string in.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
static PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
|
||||
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
char* cp = aString.mStr + aString.mLength;
|
||||
PRInt32 theMult=1;
|
||||
|
||||
*anErrorCode = NS_OK;
|
||||
|
||||
//now iterate the numeric chars and build our result
|
||||
char theDigit=0;
|
||||
while(--cp>=aString.mStr){
|
||||
char theChar=*cp;
|
||||
if((theChar>='0') && (theChar<='9')){
|
||||
theDigit=theChar-'0';
|
||||
}
|
||||
else if((theChar>='A') && (theChar<='F')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'A')+10;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
result=-result;
|
||||
break;
|
||||
}
|
||||
else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this...
|
||||
break;
|
||||
}
|
||||
else {
|
||||
//we've encountered a char that's not a legal number or sign
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
|
||||
result+=theDigit*theMult;
|
||||
theMult*=aRadix;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this method to extract the rightmost numeric value from the given
|
||||
* 1-byte input string, and simultaneously determine the radix.
|
||||
* NOTE: This method mandates that the string is well formed.
|
||||
* Leading and trailing gunk should be removed, and the case upper.
|
||||
* @update gess 10/01/98
|
||||
* @param anInputString contains orig string
|
||||
* @param anOutString contains numeric portion copy of input string
|
||||
* @param aRadix (an out parm) tells the caller what base we think the string is in.
|
||||
* @return non-zero error code if this string is non-numeric
|
||||
*/
|
||||
static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) {
|
||||
|
||||
const char* cp=aString.mStr;
|
||||
PRInt32 result=NS_ERROR_ILLEGAL_VALUE;
|
||||
if(cp) {
|
||||
|
||||
aRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* to=(char*)cp;
|
||||
const char* endcp=cp+aString.mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while(!done){
|
||||
switch(*cp) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
aRadix=16;
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
case '-': case '+': case '#':
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
default:
|
||||
cp++;
|
||||
done=(cp==endcp);
|
||||
break;
|
||||
} //switch
|
||||
}
|
||||
|
||||
while(cp<endcp) {
|
||||
char theChar=*cp;
|
||||
|
||||
if('A'<=theChar) {
|
||||
if('F'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('X'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
else if('a'<=theChar) {
|
||||
if('f'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++='A'+(theChar-'a');
|
||||
}
|
||||
else if('x'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
}
|
||||
else break; //bad char
|
||||
}
|
||||
else if((theChar>='0') && (theChar<='9')) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if(('#'!=theChar) && ('+'!=theChar)){
|
||||
break; //terminate on invalid char!
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
aString.Truncate(to-aString.mStr);
|
||||
result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method tries to autodetect that radix given a string
|
||||
* @update gess 10/01/98
|
||||
* @return 10,16,or 0 (meaning I don't know)
|
||||
*/
|
||||
PRUint32 nsString::DetermineRadix(void) {
|
||||
PRUint32 result=kRadixUnknown;
|
||||
if(0<mLength) {
|
||||
nsCAutoString theString(*this);
|
||||
if(NS_OK!=GetNumericSubstring(theString,result))
|
||||
result=kRadixUnknown;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform decimal numeric string to int conversion.
|
||||
* NOTE: In this version, we use the radix you give, even if it's wrong.
|
||||
* @update gess 10/01/98
|
||||
* @update gess 02/14/00
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the given string in.
|
||||
* @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
||||
#if 1
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
if(0<mLength) {
|
||||
|
||||
nsCAutoString theString(mUStr,mLength);
|
||||
PRUint32 theRadix=aRadix;
|
||||
|
||||
*anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
|
||||
|
||||
if(NS_OK==*anErrorCode){
|
||||
if(kAutoDetect==aRadix)
|
||||
aRadix=theRadix;
|
||||
if((kRadix10==aRadix) || (kRadix16==aRadix))
|
||||
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
|
||||
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
#else
|
||||
PRUnichar* cp=aString.mUStr;
|
||||
PRUnichar* cp=mUStr;
|
||||
PRInt32 theRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
PRInt32 result=0;
|
||||
PRBool negate=PR_FALSE;
|
||||
|
@ -1033,7 +849,7 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
PRUnichar* endcp=cp+aString.mLength;
|
||||
PRUnichar* endcp=cp+mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while((cp<endcp) && (!done)){
|
||||
|
@ -1119,7 +935,6 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
} //if
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
|
|
|
@ -386,16 +386,11 @@ public:
|
|||
*/
|
||||
float ToFloat(PRInt32* aErrorCode) const;
|
||||
|
||||
/**
|
||||
* Try to derive the radix from the value contained in this string
|
||||
* @return kRadix10, kRadix16 or kAutoDetect (meaning unknown)
|
||||
*/
|
||||
PRUint32 DetermineRadix(void);
|
||||
|
||||
/**
|
||||
* Perform string to int conversion.
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @return int rep of string value
|
||||
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
|
||||
* @return int rep of string value, and possible (out) error code
|
||||
*/
|
||||
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
|
||||
|
||||
|
|
|
@ -389,44 +389,53 @@ void nsStr::StripChars(nsStr& aDest,const char* aSet){
|
|||
Searching methods...
|
||||
**************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* This searches aDest for a given substring
|
||||
*
|
||||
* @update gess 3/25/98
|
||||
* @update gess 2/04/00: added aCount argument to restrict search
|
||||
* @param aDest string to search
|
||||
* @param aTarget is the substring you're trying to find.
|
||||
* @param aIgnorecase indicates case sensitivity of search
|
||||
* @param anOffset tells us where to start the search
|
||||
* @param aCount tells us how many iterations to make from offset; -1 means the full length of the string
|
||||
* @return index in aDest where member of aSet occurs, or -1 if not found
|
||||
*/
|
||||
PRInt32 nsStr::FindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
||||
// NS_PRECONDITION(aTarget.mLength!=1,kCallFindChar);
|
||||
PRInt32 theMaxPos = aDest.mLength-aTarget.mLength; //this is the last pos that is feasible for starting the search, with given lengths...
|
||||
|
||||
PRInt32 result=kNotFound;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<(PRInt32)aDest.mLength)) {
|
||||
PRInt32 theMax=aDest.mLength-aTarget.mLength;
|
||||
PRInt32 index=(0<=anOffset) ? anOffset : 0;
|
||||
|
||||
if((aDest.mLength>=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){
|
||||
PRInt32 theTargetMax=aTarget.mLength;
|
||||
while(index<=theMax) {
|
||||
PRInt32 theSubIndex=-1;
|
||||
PRBool matches=PR_TRUE;
|
||||
while((++theSubIndex<theTargetMax) && (matches)){
|
||||
PRUnichar theChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex);
|
||||
PRUnichar theTargetChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aTarget,theSubIndex)) : GetCharAt(aTarget,theSubIndex);
|
||||
matches=PRBool(theChar==theTargetChar);
|
||||
}
|
||||
if(matches) {
|
||||
result=index;
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
} //while
|
||||
}//if
|
||||
}//if
|
||||
return result;
|
||||
if(0<=theMaxPos) {
|
||||
|
||||
if(anOffset<0)
|
||||
anOffset=0;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<=theMaxPos) && (aTarget.mLength)) {
|
||||
|
||||
if(aCount<0)
|
||||
aCount = MaxInt(theMaxPos,1);
|
||||
|
||||
if(0<aCount) {
|
||||
|
||||
PRInt32 aDelta= (aDest.mCharSize == eOneByte) ? 1 : 2;
|
||||
const char* root = aDest.mStr;
|
||||
const char* left = root+(anOffset*aDelta);
|
||||
const char* last = left+((aCount)*aDelta);
|
||||
const char* max = root+(theMaxPos*aDelta);
|
||||
const char* right = (last<max) ? last : max;
|
||||
|
||||
while(left<=right){
|
||||
PRInt32 cmp=(*gCompare[aDest.mCharSize][aTarget.mCharSize])(left,aTarget.mStr,aTarget.mLength,aIgnoreCase);
|
||||
if(0==cmp) {
|
||||
return (left-root)/aDelta;
|
||||
}
|
||||
left+=aDelta;
|
||||
} //while
|
||||
|
||||
} //if
|
||||
}
|
||||
} //if
|
||||
|
||||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
|
@ -483,52 +492,47 @@ PRInt32 nsStr::FindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnore
|
|||
/**
|
||||
* This searches aDest (in reverse) for a given substring
|
||||
*
|
||||
* @update gess 3/25/98
|
||||
* @update gess 2/04/00
|
||||
* @param aDest string to search
|
||||
* @param aTarget is the substring you're trying to find.
|
||||
* @param aIgnorecase indicates case sensitivity of search
|
||||
* @param anOffset tells us where to start the search (counting from left)
|
||||
* @param aCount tell us how many iterations to perform from offset
|
||||
* @return index in aDest where member of aSet occurs, or -1 if not found
|
||||
*/
|
||||
PRInt32 nsStr::RFindSubstr(const nsStr& aDest,const nsStr& aTarget, PRBool aIgnoreCase,PRInt32 anOffset,PRInt32 aCount) {
|
||||
//NS_PRECONDITION(aTarget.mLength!=1,kCallRFindChar);
|
||||
if(anOffset<0)
|
||||
anOffset=(PRInt32)aDest.mLength-1;
|
||||
|
||||
PRInt32 result=kNotFound;
|
||||
if(aCount<0)
|
||||
aCount = aDest.mLength;
|
||||
|
||||
if((0<aDest.mLength) && (anOffset<(PRInt32)aDest.mLength)) {
|
||||
PRInt32 index=(0<=anOffset) ? anOffset : aDest.mLength-1;
|
||||
if((0<aDest.mLength) && ((PRUint32)anOffset<aDest.mLength) && (aTarget.mLength)) {
|
||||
|
||||
if(0<aCount) {
|
||||
|
||||
PRInt32 aDelta = (aDest.mCharSize == eOneByte) ? 1 : 2;
|
||||
const char* root = aDest.mStr;
|
||||
const char* destLast = root+((aDest.mLength-1)*aDelta); //pts to last char in aDest (likely null)
|
||||
|
||||
if((aDest.mLength>=aTarget.mLength) && (aTarget.mLength>0) && (index>=0)){
|
||||
const char* rightmost = root+(anOffset*aDelta);
|
||||
const char* min = rightmost-((aCount-1)*aDelta);
|
||||
|
||||
nsStr theCopy;
|
||||
nsStr::Initialize(theCopy,eOneByte);
|
||||
nsStr::Assign(theCopy,aTarget,0,aTarget.mLength);
|
||||
if(aIgnoreCase){
|
||||
nsStr::ChangeCase(theCopy,PR_FALSE); //force to lowercase
|
||||
}
|
||||
|
||||
PRInt32 theTargetMax=theCopy.mLength;
|
||||
while(index>=0) {
|
||||
PRInt32 theSubIndex=-1;
|
||||
PRBool matches=PR_FALSE;
|
||||
if(index+theCopy.mLength<=aDest.mLength) {
|
||||
matches=PR_TRUE;
|
||||
while((++theSubIndex<theTargetMax) && (matches)){
|
||||
PRUnichar theDestChar=(aIgnoreCase) ? nsCRT::ToLower(GetCharAt(aDest,index+theSubIndex)) : GetCharAt(aDest,index+theSubIndex);
|
||||
PRUnichar theTargetChar=GetCharAt(theCopy,theSubIndex);
|
||||
matches=PRBool(theDestChar==theTargetChar);
|
||||
} //while
|
||||
const char* leftmost = (min<root) ? root: min;
|
||||
|
||||
while(leftmost<=rightmost) {
|
||||
if(aTarget.mLength<=PRUint32(destLast-rightmost)) {
|
||||
PRInt32 result=(*gCompare[aDest.mCharSize][aTarget.mCharSize])(rightmost,aTarget.mStr,aTarget.mLength,aIgnoreCase);
|
||||
if(0==result) {
|
||||
return (rightmost-root)/aDelta;
|
||||
}
|
||||
} //if
|
||||
if(matches) {
|
||||
result=index;
|
||||
break;
|
||||
}
|
||||
index--;
|
||||
rightmost-=aDelta;
|
||||
} //while
|
||||
nsStr::Destroy(theCopy);
|
||||
}//if
|
||||
}//if
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
|
@ -547,6 +551,7 @@ PRInt32 nsStr::RFindChar(const nsStr& aDest,PRUnichar aChar, PRBool aIgnoreCase,
|
|||
return gRFindChars[aDest.mCharSize](aDest.mStr,aDest.mLength,anOffset,aChar,aIgnoreCase,aCount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This searches aDest (in reverese) for a character found in aSet.
|
||||
*
|
||||
|
@ -576,7 +581,6 @@ PRInt32 nsStr::RFindCharInSet(const nsStr& aDest,const nsStr& aSet,PRBool aIgnor
|
|||
return kNotFound;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compare source and dest strings, up to an (optional max) number of chars
|
||||
* @param aDest is the first str to compare
|
||||
|
|
|
@ -657,213 +657,16 @@ float nsCString::ToFloat(PRInt32* aErrorCode) const {
|
|||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform numeric string to int conversion with given radix.
|
||||
* NOTE: 1. This method mandates that the string is well formed and uppercased.
|
||||
* 2. This method will return an error if the string you give
|
||||
contains chars outside the range for the specified radix.
|
||||
|
||||
* @update gess 10/01/98
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the string in.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
|
||||
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
char* cp = aString.mStr + aString.mLength;
|
||||
PRInt32 theMult=1;
|
||||
|
||||
*anErrorCode = NS_OK;
|
||||
|
||||
//now iterate the numeric chars and build our result
|
||||
char theChar=0;
|
||||
char theDigit=0;
|
||||
while(--cp>=aString.mStr){
|
||||
theChar=*cp;
|
||||
if((theChar>='0') && (theChar<='9')){
|
||||
theDigit=theChar-'0';
|
||||
}
|
||||
else if((theChar>='A') && (theChar<='F')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'A')+10;
|
||||
}
|
||||
else if((theChar>='a') && (theChar<='f')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'a')+10;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
result=-result;
|
||||
break;
|
||||
}
|
||||
else if(('+'==theChar) || (' '==theChar) || ('X'==theChar) || ('x'==theChar)) { //stop in a good state if you see this...
|
||||
break;
|
||||
}
|
||||
else {
|
||||
//we've encountered a char that's not a legal number or sign
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
|
||||
result+=theDigit*theMult;
|
||||
theMult*=aRadix;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Call this method to extract the rightmost numeric value from the given
|
||||
* 1-byte input string, and simultaneously determine the radix.
|
||||
* NOTE: This method mandates that the string is well formed.
|
||||
* Leading and trailing gunk should be removed, and upper-cased.
|
||||
* @update gess 10/01/98
|
||||
* @param anInputString contains orig string
|
||||
* @param anOutString contains numeric portion copy of input string
|
||||
* @param aRadix (an out parm) tells the caller what base we think the string is in.
|
||||
* @return non-zero error code if this string is non-numeric
|
||||
*/
|
||||
static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) {
|
||||
|
||||
const char* cp=aString.mStr;
|
||||
PRInt32 result=NS_ERROR_ILLEGAL_VALUE;
|
||||
if(cp) {
|
||||
|
||||
aRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* to=(char*)cp;
|
||||
const char* endcp=cp+aString.mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while(!done){
|
||||
switch(*cp) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
aRadix=16;
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
case '-': case '+': case '#':
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
default:
|
||||
cp++;
|
||||
done=(cp==endcp);
|
||||
break;
|
||||
} //switch
|
||||
}
|
||||
|
||||
while(cp<endcp) {
|
||||
char theChar=*cp;
|
||||
|
||||
if('A'<=theChar) {
|
||||
if('F'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('X'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
else if('a'<=theChar) {
|
||||
if('f'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++='A'+(theChar-'a');
|
||||
}
|
||||
else if('x'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
}
|
||||
else break; //bad char
|
||||
}
|
||||
else if((theChar>='0') && (theChar<='9')) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if(('#'!=theChar) && ('+'!=theChar)){
|
||||
break; //terminate on invalid char!
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
aString.Truncate(to-aString.mStr);
|
||||
result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method tries to autodetect that radix given a string
|
||||
* @update gess 10/01/98
|
||||
* @return 10,16,or 0 (meaning I don't know)
|
||||
*/
|
||||
PRUint32 nsCString::DetermineRadix(void) {
|
||||
PRUint32 result=kRadixUnknown;
|
||||
if(0<mLength) {
|
||||
//nsCAutoString theString(*this);
|
||||
nsSubsumeCStr theString(mStr,mLength);
|
||||
if(NS_OK!=GetNumericSubstring(theString,result))
|
||||
result=kRadixUnknown;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform decimal numeric string to int conversion.
|
||||
* NOTE: In this version, we use the radix you give, even if it's wrong.
|
||||
* @update gess 10/01/98
|
||||
* @update gess 02/14/00
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the given string in.
|
||||
* @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
||||
#if 1
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
if(0<mLength) {
|
||||
|
||||
nsCAutoString theString(mStr,mLength);
|
||||
PRUint32 theRadix=aRadix;
|
||||
|
||||
*anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
|
||||
|
||||
if(NS_OK==*anErrorCode){
|
||||
if(kAutoDetect==aRadix)
|
||||
aRadix=theRadix;
|
||||
if((kRadix10==aRadix) || (kRadix16==aRadix))
|
||||
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
|
||||
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
#else
|
||||
char* cp=aString.mStr;
|
||||
char* cp=mStr;
|
||||
PRInt32 theRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
PRInt32 result=0;
|
||||
PRBool negate=PR_FALSE;
|
||||
|
@ -875,7 +678,7 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* endcp=cp+aString.mLength;
|
||||
char* endcp=cp+mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while((cp<endcp) && (!done)){
|
||||
|
@ -961,7 +764,6 @@ PRInt32 nsCString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
} //if
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
|
|
|
@ -343,16 +343,12 @@ public:
|
|||
*/
|
||||
float ToFloat(PRInt32* aErrorCode) const;
|
||||
|
||||
/**
|
||||
* Try to derive the radix from the value contained in this string
|
||||
* @return kRadix10, kRadix16 or kAutoDetect (meaning unknown)
|
||||
*/
|
||||
PRUint32 DetermineRadix(void);
|
||||
|
||||
/**
|
||||
* Perform string to int conversion.
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @return int rep of string value
|
||||
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
|
||||
* @return int rep of string value, and possible (out) error code
|
||||
*/
|
||||
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
|
||||
|
||||
|
|
|
@ -827,201 +827,17 @@ float nsString::ToFloat(PRInt32* aErrorCode) const {
|
|||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform numeric string to int conversion with given radix.
|
||||
* NOTE: 1. This method mandates that the string is well formed and uppercased
|
||||
* 2. This method will return an error if the string you give
|
||||
contains chars outside the range for the specified radix.
|
||||
|
||||
* @update gess 10/01/98
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the string in.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
static PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
|
||||
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
char* cp = aString.mStr + aString.mLength;
|
||||
PRInt32 theMult=1;
|
||||
|
||||
*anErrorCode = NS_OK;
|
||||
|
||||
//now iterate the numeric chars and build our result
|
||||
char theDigit=0;
|
||||
while(--cp>=aString.mStr){
|
||||
char theChar=*cp;
|
||||
if((theChar>='0') && (theChar<='9')){
|
||||
theDigit=theChar-'0';
|
||||
}
|
||||
else if((theChar>='A') && (theChar<='F')) {
|
||||
if(10==aRadix){
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
theDigit=(theChar-'A')+10;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
result=-result;
|
||||
break;
|
||||
}
|
||||
else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this...
|
||||
break;
|
||||
}
|
||||
else {
|
||||
//we've encountered a char that's not a legal number or sign
|
||||
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
result=0;
|
||||
break;
|
||||
}
|
||||
|
||||
result+=theDigit*theMult;
|
||||
theMult*=aRadix;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this method to extract the rightmost numeric value from the given
|
||||
* 1-byte input string, and simultaneously determine the radix.
|
||||
* NOTE: This method mandates that the string is well formed.
|
||||
* Leading and trailing gunk should be removed, and the case upper.
|
||||
* @update gess 10/01/98
|
||||
* @param anInputString contains orig string
|
||||
* @param anOutString contains numeric portion copy of input string
|
||||
* @param aRadix (an out parm) tells the caller what base we think the string is in.
|
||||
* @return non-zero error code if this string is non-numeric
|
||||
*/
|
||||
static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) {
|
||||
|
||||
const char* cp=aString.mStr;
|
||||
PRInt32 result=NS_ERROR_ILLEGAL_VALUE;
|
||||
if(cp) {
|
||||
|
||||
aRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
char* to=(char*)cp;
|
||||
const char* endcp=cp+aString.mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while(!done){
|
||||
switch(*cp) {
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
aRadix=16;
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
case '-': case '+': case '#':
|
||||
done=PR_TRUE;
|
||||
break;
|
||||
default:
|
||||
cp++;
|
||||
done=(cp==endcp);
|
||||
break;
|
||||
} //switch
|
||||
}
|
||||
|
||||
while(cp<endcp) {
|
||||
char theChar=*cp;
|
||||
|
||||
if('A'<=theChar) {
|
||||
if('F'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('X'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
else if('a'<=theChar) {
|
||||
if('f'>=theChar) {
|
||||
aRadix=16;
|
||||
*to++='A'+(theChar-'a');
|
||||
}
|
||||
else if('x'==theChar) {
|
||||
if('-'==aString.mStr[0])
|
||||
to=&aString.mStr[1];
|
||||
else to=aString.mStr;
|
||||
aRadix=16;
|
||||
}
|
||||
}
|
||||
else break; //bad char
|
||||
}
|
||||
else if((theChar>='0') && (theChar<='9')) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if('-'==theChar) {
|
||||
*to++=theChar;
|
||||
}
|
||||
else if(('#'!=theChar) && ('+'!=theChar)){
|
||||
break; //terminate on invalid char!
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
aString.Truncate(to-aString.mStr);
|
||||
result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method tries to autodetect that radix given a string
|
||||
* @update gess 10/01/98
|
||||
* @return 10,16,or 0 (meaning I don't know)
|
||||
*/
|
||||
PRUint32 nsString::DetermineRadix(void) {
|
||||
PRUint32 result=kRadixUnknown;
|
||||
if(0<mLength) {
|
||||
nsCAutoString theString(*this);
|
||||
if(NS_OK!=GetNumericSubstring(theString,result))
|
||||
result=kRadixUnknown;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform decimal numeric string to int conversion.
|
||||
* NOTE: In this version, we use the radix you give, even if it's wrong.
|
||||
* @update gess 10/01/98
|
||||
* @update gess 02/14/00
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @param aRadix tells us what base to expect the given string in.
|
||||
* @param aRadix tells us what base to expect the given string in. kAutoDetect tells us to determine the radix.
|
||||
* @return int rep of string value
|
||||
*/
|
||||
PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
||||
#if 1
|
||||
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
|
||||
PRInt32 result=0;
|
||||
|
||||
if(0<mLength) {
|
||||
|
||||
nsCAutoString theString(mUStr,mLength);
|
||||
PRUint32 theRadix=aRadix;
|
||||
|
||||
*anErrorCode=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
|
||||
|
||||
if(NS_OK==*anErrorCode){
|
||||
if(kAutoDetect==aRadix)
|
||||
aRadix=theRadix;
|
||||
if((kRadix10==aRadix) || (kRadix16==aRadix))
|
||||
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
|
||||
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
#else
|
||||
PRUnichar* cp=aString.mUStr;
|
||||
PRUnichar* cp=mUStr;
|
||||
PRInt32 theRadix = (kAutoDetect==aRadix) ? 10 : aRadix;
|
||||
PRInt32 result=0;
|
||||
PRBool negate=PR_FALSE;
|
||||
|
@ -1033,7 +849,7 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
|
||||
//begin by skipping over leading chars that shouldn't be part of the number...
|
||||
|
||||
PRUnichar* endcp=cp+aString.mLength;
|
||||
PRUnichar* endcp=cp+mLength;
|
||||
PRBool done=PR_FALSE;
|
||||
|
||||
while((cp<endcp) && (!done)){
|
||||
|
@ -1119,7 +935,6 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
|
|||
} //if
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
|
|
|
@ -386,16 +386,11 @@ public:
|
|||
*/
|
||||
float ToFloat(PRInt32* aErrorCode) const;
|
||||
|
||||
/**
|
||||
* Try to derive the radix from the value contained in this string
|
||||
* @return kRadix10, kRadix16 or kAutoDetect (meaning unknown)
|
||||
*/
|
||||
PRUint32 DetermineRadix(void);
|
||||
|
||||
/**
|
||||
* Perform string to int conversion.
|
||||
* @param aErrorCode will contain error if one occurs
|
||||
* @return int rep of string value
|
||||
* @param aRadix tells us which radix to assume; kAutoDetect tells us to determine the radix for you.
|
||||
* @return int rep of string value, and possible (out) error code
|
||||
*/
|
||||
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче