diff --git a/mozilla/base/src/nsString.cpp b/mozilla/base/src/nsString.cpp index 78e2d9e02b8..a63813ac50f 100644 --- a/mozilla/base/src/nsString.cpp +++ b/mozilla/base/src/nsString.cpp @@ -540,54 +540,60 @@ float nsString::ToFloat(PRInt32* aErrorCode) const } /** - * Perform string to int conversion. - * @update gess 7/27/98 + * Perform numeric string to int conversion with given 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 nsString::ToInteger(PRInt32* aErrorCode) const { - PRInt32 rv = 0; - PRUnichar* cp = mStr; - PRUnichar* end = mStr + mLength; +PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { + PRInt32 result = 0; + PRUnichar* cp = mStr + mLength-1; + char digit=0; + PRUnichar theChar; + PRInt32 theShift=0; + PRInt32 theMult=1; - // Skip leading whitespace - while (cp < end) { - PRUnichar ch = *cp; - if (!IsSpace(ch)) { + // Skip trailing non-numeric... + while (cp >= mStr) { + theChar = *cp; + if((theChar>='0') && (theChar<='9')){ break; } - cp++; - } - if (cp == end) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - return rv; - } - - // Check for sign - PRUnichar sign = '+'; - if ((*cp == '+') || (*cp == '-')) { - sign = *cp++; - } - if (cp == end) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - return rv; - } - - // Convert the number - while (cp < end) { - PRUnichar ch = *cp++; - if ((ch < '0') || (ch > '9')) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; + else if((theChar>='a') && (theChar<='f')) { break; } - rv = rv * 10 + (ch - '0'); + else if((theChar>='A') && (theChar<='F')) { + break; + } + cp--; } - if (sign == '-') { - rv = -rv; + //now iterate the numeric chars and build our result + while(cp>=mStr) { + theChar=*cp--; + if((theChar>='0') && (theChar<='9')){ + digit=theChar-'0'; + } + else if((theChar>='a') && (theChar<='f')) { + digit=(theChar-'a')+10; + } + else if((theChar>='A') && (theChar<='F')) { + digit=(theChar-'A')+10; + } + else if('-'==theChar) { + result=-result; + break; + } + else{ + break; + } + result+=digit*theMult; + theMult*=aRadix; } + *aErrorCode = NS_OK; - return rv; + return result; } /** diff --git a/mozilla/base/src/nsString.h b/mozilla/base/src/nsString.h index b37997feae7..c64e818ed75 100644 --- a/mozilla/base/src/nsString.h +++ b/mozilla/base/src/nsString.h @@ -275,7 +275,7 @@ float ToFloat(PRInt32* aErrorCode) const; * @param aErrorCode will contain error if one occurs * @return int rep of string value */ -PRInt32 ToInteger(PRInt32* aErrorCode) const; +PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; /********************************************************************** String manipulation methods... diff --git a/mozilla/string/obsolete/nsString.cpp b/mozilla/string/obsolete/nsString.cpp index 78e2d9e02b8..a63813ac50f 100644 --- a/mozilla/string/obsolete/nsString.cpp +++ b/mozilla/string/obsolete/nsString.cpp @@ -540,54 +540,60 @@ float nsString::ToFloat(PRInt32* aErrorCode) const } /** - * Perform string to int conversion. - * @update gess 7/27/98 + * Perform numeric string to int conversion with given 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 nsString::ToInteger(PRInt32* aErrorCode) const { - PRInt32 rv = 0; - PRUnichar* cp = mStr; - PRUnichar* end = mStr + mLength; +PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { + PRInt32 result = 0; + PRUnichar* cp = mStr + mLength-1; + char digit=0; + PRUnichar theChar; + PRInt32 theShift=0; + PRInt32 theMult=1; - // Skip leading whitespace - while (cp < end) { - PRUnichar ch = *cp; - if (!IsSpace(ch)) { + // Skip trailing non-numeric... + while (cp >= mStr) { + theChar = *cp; + if((theChar>='0') && (theChar<='9')){ break; } - cp++; - } - if (cp == end) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - return rv; - } - - // Check for sign - PRUnichar sign = '+'; - if ((*cp == '+') || (*cp == '-')) { - sign = *cp++; - } - if (cp == end) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - return rv; - } - - // Convert the number - while (cp < end) { - PRUnichar ch = *cp++; - if ((ch < '0') || (ch > '9')) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; + else if((theChar>='a') && (theChar<='f')) { break; } - rv = rv * 10 + (ch - '0'); + else if((theChar>='A') && (theChar<='F')) { + break; + } + cp--; } - if (sign == '-') { - rv = -rv; + //now iterate the numeric chars and build our result + while(cp>=mStr) { + theChar=*cp--; + if((theChar>='0') && (theChar<='9')){ + digit=theChar-'0'; + } + else if((theChar>='a') && (theChar<='f')) { + digit=(theChar-'a')+10; + } + else if((theChar>='A') && (theChar<='F')) { + digit=(theChar-'A')+10; + } + else if('-'==theChar) { + result=-result; + break; + } + else{ + break; + } + result+=digit*theMult; + theMult*=aRadix; } + *aErrorCode = NS_OK; - return rv; + return result; } /** diff --git a/mozilla/string/obsolete/nsString.h b/mozilla/string/obsolete/nsString.h index b37997feae7..c64e818ed75 100644 --- a/mozilla/string/obsolete/nsString.h +++ b/mozilla/string/obsolete/nsString.h @@ -275,7 +275,7 @@ float ToFloat(PRInt32* aErrorCode) const; * @param aErrorCode will contain error if one occurs * @return int rep of string value */ -PRInt32 ToInteger(PRInt32* aErrorCode) const; +PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; /********************************************************************** String manipulation methods... diff --git a/mozilla/xpcom/ds/nsString.cpp b/mozilla/xpcom/ds/nsString.cpp index 78e2d9e02b8..a63813ac50f 100644 --- a/mozilla/xpcom/ds/nsString.cpp +++ b/mozilla/xpcom/ds/nsString.cpp @@ -540,54 +540,60 @@ float nsString::ToFloat(PRInt32* aErrorCode) const } /** - * Perform string to int conversion. - * @update gess 7/27/98 + * Perform numeric string to int conversion with given 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 nsString::ToInteger(PRInt32* aErrorCode) const { - PRInt32 rv = 0; - PRUnichar* cp = mStr; - PRUnichar* end = mStr + mLength; +PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { + PRInt32 result = 0; + PRUnichar* cp = mStr + mLength-1; + char digit=0; + PRUnichar theChar; + PRInt32 theShift=0; + PRInt32 theMult=1; - // Skip leading whitespace - while (cp < end) { - PRUnichar ch = *cp; - if (!IsSpace(ch)) { + // Skip trailing non-numeric... + while (cp >= mStr) { + theChar = *cp; + if((theChar>='0') && (theChar<='9')){ break; } - cp++; - } - if (cp == end) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - return rv; - } - - // Check for sign - PRUnichar sign = '+'; - if ((*cp == '+') || (*cp == '-')) { - sign = *cp++; - } - if (cp == end) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - return rv; - } - - // Convert the number - while (cp < end) { - PRUnichar ch = *cp++; - if ((ch < '0') || (ch > '9')) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; + else if((theChar>='a') && (theChar<='f')) { break; } - rv = rv * 10 + (ch - '0'); + else if((theChar>='A') && (theChar<='F')) { + break; + } + cp--; } - if (sign == '-') { - rv = -rv; + //now iterate the numeric chars and build our result + while(cp>=mStr) { + theChar=*cp--; + if((theChar>='0') && (theChar<='9')){ + digit=theChar-'0'; + } + else if((theChar>='a') && (theChar<='f')) { + digit=(theChar-'a')+10; + } + else if((theChar>='A') && (theChar<='F')) { + digit=(theChar-'A')+10; + } + else if('-'==theChar) { + result=-result; + break; + } + else{ + break; + } + result+=digit*theMult; + theMult*=aRadix; } + *aErrorCode = NS_OK; - return rv; + return result; } /** diff --git a/mozilla/xpcom/ds/nsString.h b/mozilla/xpcom/ds/nsString.h index b37997feae7..c64e818ed75 100644 --- a/mozilla/xpcom/ds/nsString.h +++ b/mozilla/xpcom/ds/nsString.h @@ -275,7 +275,7 @@ float ToFloat(PRInt32* aErrorCode) const; * @param aErrorCode will contain error if one occurs * @return int rep of string value */ -PRInt32 ToInteger(PRInt32* aErrorCode) const; +PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; /********************************************************************** String manipulation methods... diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp index 78e2d9e02b8..a63813ac50f 100644 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ b/mozilla/xpcom/string/obsolete/nsString.cpp @@ -540,54 +540,60 @@ float nsString::ToFloat(PRInt32* aErrorCode) const } /** - * Perform string to int conversion. - * @update gess 7/27/98 + * Perform numeric string to int conversion with given 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 nsString::ToInteger(PRInt32* aErrorCode) const { - PRInt32 rv = 0; - PRUnichar* cp = mStr; - PRUnichar* end = mStr + mLength; +PRInt32 nsString::ToInteger(PRInt32* aErrorCode,PRInt32 aRadix) const { + PRInt32 result = 0; + PRUnichar* cp = mStr + mLength-1; + char digit=0; + PRUnichar theChar; + PRInt32 theShift=0; + PRInt32 theMult=1; - // Skip leading whitespace - while (cp < end) { - PRUnichar ch = *cp; - if (!IsSpace(ch)) { + // Skip trailing non-numeric... + while (cp >= mStr) { + theChar = *cp; + if((theChar>='0') && (theChar<='9')){ break; } - cp++; - } - if (cp == end) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - return rv; - } - - // Check for sign - PRUnichar sign = '+'; - if ((*cp == '+') || (*cp == '-')) { - sign = *cp++; - } - if (cp == end) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; - return rv; - } - - // Convert the number - while (cp < end) { - PRUnichar ch = *cp++; - if ((ch < '0') || (ch > '9')) { - *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; + else if((theChar>='a') && (theChar<='f')) { break; } - rv = rv * 10 + (ch - '0'); + else if((theChar>='A') && (theChar<='F')) { + break; + } + cp--; } - if (sign == '-') { - rv = -rv; + //now iterate the numeric chars and build our result + while(cp>=mStr) { + theChar=*cp--; + if((theChar>='0') && (theChar<='9')){ + digit=theChar-'0'; + } + else if((theChar>='a') && (theChar<='f')) { + digit=(theChar-'a')+10; + } + else if((theChar>='A') && (theChar<='F')) { + digit=(theChar-'A')+10; + } + else if('-'==theChar) { + result=-result; + break; + } + else{ + break; + } + result+=digit*theMult; + theMult*=aRadix; } + *aErrorCode = NS_OK; - return rv; + return result; } /** diff --git a/mozilla/xpcom/string/obsolete/nsString.h b/mozilla/xpcom/string/obsolete/nsString.h index b37997feae7..c64e818ed75 100644 --- a/mozilla/xpcom/string/obsolete/nsString.h +++ b/mozilla/xpcom/string/obsolete/nsString.h @@ -275,7 +275,7 @@ float ToFloat(PRInt32* aErrorCode) const; * @param aErrorCode will contain error if one occurs * @return int rep of string value */ -PRInt32 ToInteger(PRInt32* aErrorCode) const; +PRInt32 ToInteger(PRInt32* aErrorCode,PRInt32 aRadix=10) const; /********************************************************************** String manipulation methods...