diff --git a/mozilla/string/obsolete/nsString.cpp b/mozilla/string/obsolete/nsString.cpp index 159748f2cb1..80fc1b9dada 100644 --- a/mozilla/string/obsolete/nsString.cpp +++ b/mozilla/string/obsolete/nsString.cpp @@ -634,7 +634,7 @@ float nsCString::ToFloat(PRInt32* aErrorCode) const { /** * Perform numeric string to int conversion with given radix. - * NOTE: 1. This method mandates that the string is well formed. + * 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. @@ -703,7 +703,7 @@ PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * 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. + * 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 @@ -711,52 +711,74 @@ PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * @return non-zero error code if this string is non-numeric */ static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { - static const char* validChars="0123456789abcdefABCDEF-+#"; const char* cp=aString.GetBuffer(); PRInt32 result=NS_ERROR_ILLEGAL_VALUE; if(cp) { + //begin by skipping over leading chars that shouldn't be part of the number... char* to=(char*)cp; const char* endcp=cp+aString.mLength; - int len=strlen(validChars); + PRBool done=PR_FALSE; - while(cp='0') && (theChar<='9')) { - *to++=theChar; + while(cp=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>='A') && (theChar<='F')) { - aRadix=16; + else if((theChar>='0') && (theChar<='9')) { *to++=theChar; } - else if('X'==theChar){ - if('-'==aString.mStr[0]) - to=&aString.mStr[1]; - else to=aString.mStr; - aRadix=16; - //root=cp; - } else if('-'==theChar) { *to++=theChar; } - else if(('#'==theChar) || ('+'==theChar)){ - //just skip this char... + else if(('#'!=theChar) && ('+'!=theChar)){ + break; //terminate on invalid char! } - else break; cp++; } + aString.Truncate(to-aString.mStr); result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; } diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index 4a6efefcd1f..9afcd9bb706 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -805,7 +805,7 @@ float nsString::ToFloat(PRInt32* aErrorCode) const { /** * Perform numeric string to int conversion with given radix. - * NOTE: 1. This method mandates that the string is well formed. + * 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. @@ -871,52 +871,74 @@ static PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadi * @return non-zero error code if this string is non-numeric */ static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { - static const char* validChars="0123456789abcdefABCDEF-+#"; const char* cp=aString.GetBuffer(); PRInt32 result=NS_ERROR_ILLEGAL_VALUE; if(cp) { + //begin by skipping over leading chars that shouldn't be part of the number... char* to=(char*)cp; const char* endcp=cp+aString.mLength; - int len=strlen(validChars); + PRBool done=PR_FALSE; - while(cp='0') && (theChar<='9')) { - *to++=theChar; + while(cp=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>='A') && (theChar<='F')) { - aRadix=16; + else if((theChar>='0') && (theChar<='9')) { *to++=theChar; } - else if('X'==theChar){ - if('-'==aString.mStr[0]) - to=&aString.mStr[1]; - else to=aString.mStr; - aRadix=16; - //root=cp; - } else if('-'==theChar) { *to++=theChar; } - else if(('#'==theChar) || ('+'==theChar)){ - //just skip this char... + else if(('#'!=theChar) && ('+'!=theChar)){ + break; //terminate on invalid char! } - else break; cp++; } + aString.Truncate(to-aString.mStr); result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; } @@ -956,6 +978,7 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { 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; diff --git a/mozilla/xpcom/ds/nsString.cpp b/mozilla/xpcom/ds/nsString.cpp index 159748f2cb1..80fc1b9dada 100644 --- a/mozilla/xpcom/ds/nsString.cpp +++ b/mozilla/xpcom/ds/nsString.cpp @@ -634,7 +634,7 @@ float nsCString::ToFloat(PRInt32* aErrorCode) const { /** * Perform numeric string to int conversion with given radix. - * NOTE: 1. This method mandates that the string is well formed. + * 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. @@ -703,7 +703,7 @@ PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * 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. + * 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 @@ -711,52 +711,74 @@ PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * @return non-zero error code if this string is non-numeric */ static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { - static const char* validChars="0123456789abcdefABCDEF-+#"; const char* cp=aString.GetBuffer(); PRInt32 result=NS_ERROR_ILLEGAL_VALUE; if(cp) { + //begin by skipping over leading chars that shouldn't be part of the number... char* to=(char*)cp; const char* endcp=cp+aString.mLength; - int len=strlen(validChars); + PRBool done=PR_FALSE; - while(cp='0') && (theChar<='9')) { - *to++=theChar; + while(cp=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>='A') && (theChar<='F')) { - aRadix=16; + else if((theChar>='0') && (theChar<='9')) { *to++=theChar; } - else if('X'==theChar){ - if('-'==aString.mStr[0]) - to=&aString.mStr[1]; - else to=aString.mStr; - aRadix=16; - //root=cp; - } else if('-'==theChar) { *to++=theChar; } - else if(('#'==theChar) || ('+'==theChar)){ - //just skip this char... + else if(('#'!=theChar) && ('+'!=theChar)){ + break; //terminate on invalid char! } - else break; cp++; } + aString.Truncate(to-aString.mStr); result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; } diff --git a/mozilla/xpcom/ds/nsString2.cpp b/mozilla/xpcom/ds/nsString2.cpp index 4a6efefcd1f..9afcd9bb706 100644 --- a/mozilla/xpcom/ds/nsString2.cpp +++ b/mozilla/xpcom/ds/nsString2.cpp @@ -805,7 +805,7 @@ float nsString::ToFloat(PRInt32* aErrorCode) const { /** * Perform numeric string to int conversion with given radix. - * NOTE: 1. This method mandates that the string is well formed. + * 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. @@ -871,52 +871,74 @@ static PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadi * @return non-zero error code if this string is non-numeric */ static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { - static const char* validChars="0123456789abcdefABCDEF-+#"; const char* cp=aString.GetBuffer(); PRInt32 result=NS_ERROR_ILLEGAL_VALUE; if(cp) { + //begin by skipping over leading chars that shouldn't be part of the number... char* to=(char*)cp; const char* endcp=cp+aString.mLength; - int len=strlen(validChars); + PRBool done=PR_FALSE; - while(cp='0') && (theChar<='9')) { - *to++=theChar; + while(cp=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>='A') && (theChar<='F')) { - aRadix=16; + else if((theChar>='0') && (theChar<='9')) { *to++=theChar; } - else if('X'==theChar){ - if('-'==aString.mStr[0]) - to=&aString.mStr[1]; - else to=aString.mStr; - aRadix=16; - //root=cp; - } else if('-'==theChar) { *to++=theChar; } - else if(('#'==theChar) || ('+'==theChar)){ - //just skip this char... + else if(('#'!=theChar) && ('+'!=theChar)){ + break; //terminate on invalid char! } - else break; cp++; } + aString.Truncate(to-aString.mStr); result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; } @@ -956,6 +978,7 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { 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; diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp index 159748f2cb1..80fc1b9dada 100644 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ b/mozilla/xpcom/string/obsolete/nsString.cpp @@ -634,7 +634,7 @@ float nsCString::ToFloat(PRInt32* aErrorCode) const { /** * Perform numeric string to int conversion with given radix. - * NOTE: 1. This method mandates that the string is well formed. + * 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. @@ -703,7 +703,7 @@ PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * 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. + * 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 @@ -711,52 +711,74 @@ PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadix) { * @return non-zero error code if this string is non-numeric */ static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { - static const char* validChars="0123456789abcdefABCDEF-+#"; const char* cp=aString.GetBuffer(); PRInt32 result=NS_ERROR_ILLEGAL_VALUE; if(cp) { + //begin by skipping over leading chars that shouldn't be part of the number... char* to=(char*)cp; const char* endcp=cp+aString.mLength; - int len=strlen(validChars); + PRBool done=PR_FALSE; - while(cp='0') && (theChar<='9')) { - *to++=theChar; + while(cp=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>='A') && (theChar<='F')) { - aRadix=16; + else if((theChar>='0') && (theChar<='9')) { *to++=theChar; } - else if('X'==theChar){ - if('-'==aString.mStr[0]) - to=&aString.mStr[1]; - else to=aString.mStr; - aRadix=16; - //root=cp; - } else if('-'==theChar) { *to++=theChar; } - else if(('#'==theChar) || ('+'==theChar)){ - //just skip this char... + else if(('#'!=theChar) && ('+'!=theChar)){ + break; //terminate on invalid char! } - else break; cp++; } + aString.Truncate(to-aString.mStr); result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; } diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index 4a6efefcd1f..9afcd9bb706 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -805,7 +805,7 @@ float nsString::ToFloat(PRInt32* aErrorCode) const { /** * Perform numeric string to int conversion with given radix. - * NOTE: 1. This method mandates that the string is well formed. + * 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. @@ -871,52 +871,74 @@ static PRInt32 _ToInteger(nsCString& aString,PRInt32* anErrorCode,PRUint32 aRadi * @return non-zero error code if this string is non-numeric */ static PRInt32 GetNumericSubstring(nsCString& aString,PRUint32& aRadix) { - static const char* validChars="0123456789abcdefABCDEF-+#"; const char* cp=aString.GetBuffer(); PRInt32 result=NS_ERROR_ILLEGAL_VALUE; if(cp) { + //begin by skipping over leading chars that shouldn't be part of the number... char* to=(char*)cp; const char* endcp=cp+aString.mLength; - int len=strlen(validChars); + PRBool done=PR_FALSE; - while(cp='0') && (theChar<='9')) { - *to++=theChar; + while(cp=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>='A') && (theChar<='F')) { - aRadix=16; + else if((theChar>='0') && (theChar<='9')) { *to++=theChar; } - else if('X'==theChar){ - if('-'==aString.mStr[0]) - to=&aString.mStr[1]; - else to=aString.mStr; - aRadix=16; - //root=cp; - } else if('-'==theChar) { *to++=theChar; } - else if(('#'==theChar) || ('+'==theChar)){ - //just skip this char... + else if(('#'!=theChar) && ('+'!=theChar)){ + break; //terminate on invalid char! } - else break; cp++; } + aString.Truncate(to-aString.mStr); result=(0==aString.mLength) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; } @@ -956,6 +978,7 @@ PRInt32 nsString::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const { 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;