approved update

This commit is contained in:
rickg%netscape.com 1999-06-17 07:24:13 +00:00
parent a4f7f5d5e8
commit 1e43cc1e92
6 changed files with 90 additions and 123 deletions

View File

@ -688,15 +688,6 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this...
break;
}
/* The following block can be replaced with the next block
else if(('X'==theChar) || ('#'==theChar)) {
if(10==aRadix) {
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
result=0;
}
break;
}
---cut above here...*/
else {
//we've encountered a char that's not a legal number or sign
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
@ -729,11 +720,13 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
PRInt32 decPt=aString.FindChar(aString,'.',PR_TRUE,0);
char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1;
aRadix=10; //assume for starters...
aRadix=kRadixUnknown; //assume for starters...
// Skip trailing non-numeric...
while (cp >= aString.mStr) {
if((*cp>='0') && (*cp<='9')){
if(kRadixUnknown==aRadix)
aRadix=kRadix10;
break;
}
else if((*cp>='A') && (*cp<='F')) {
@ -761,7 +754,7 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
}
else {
if(('#'==(*cp)) || ('X'==(*cp)))
aRadix=16;
aRadix=kRadix16;
cp++; //move back by one
break;
}
@ -773,41 +766,23 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
return result;
}
/**
* Perform numeric string to int conversion with given radix.
* This method tries to autodetect that radix given a string
* @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
* @return 10,16,or 0 (meaning I don't know)
*/
PRInt32 nsString2::ToInteger(PRInt32* anErrorCode) const {
/********************************************************************************
If you called this version, it's because you don't know which radix your
string is stored in (hex, dec, etc.).
This method will attempt to figure that out for you, and then call
toInteger(err,radix).
NOTE: If you know your string is in a given radix, then it's better to
call toInteger(err,radix) directly, because there are cases where
this method cannot make the correct determination. For example,
a string that contains "123" can not be differentiated (hex vs. dec).
********************************************************************************/
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
PRInt32 result=0;
PRUint32 nsString2::DetermineRadix(void) {
PRUint32 result=kRadixUnknown;
if(0<mLength) {
nsAutoString2 theString(*this,eOneByte);
PRUint32 theRadix=10;
*anErrorCode=GetNumericSubstring(theString,theRadix);
if(NS_OK==*anErrorCode){
result=_ToInteger(theString,anErrorCode,theRadix);
}
if(NS_OK!=GetNumericSubstring(theString,result))
result=kRadixUnknown;
}
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
return result;
}
/**
* Perform decimal numeric string to int conversion.
* NOTE: In this version, we use the radix you give, even if it's wrong.
@ -820,11 +795,15 @@ PRInt32 nsString2::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
nsAutoString2 theString(*this,eOneByte);
PRUint32 theRadix=10;
PRUint32 theRadix=aRadix;
PRInt32 result=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
if(NS_OK==result){
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
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 result=NS_ERROR_ILLEGAL_VALUE;
}
return result;

View File

@ -50,6 +50,11 @@ class nsISizeOfHandler;
#define nsString2 nsString
#define nsAutoString2 nsAutoString
#define kRadix10 (10)
#define kRadix16 (16)
#define kAutoDetect (100)
#define kRadixUnknown (kAutoDetect+1)
class NS_COM nsSubsumeStr;
class NS_COM nsString2 : public nsStr {
@ -365,13 +370,18 @@ char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const;
*/
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
*/
PRInt32 ToInteger(PRInt32* aErrorCode) const;
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix) const;
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
/**********************************************************************

View File

@ -688,15 +688,6 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this...
break;
}
/* The following block can be replaced with the next block
else if(('X'==theChar) || ('#'==theChar)) {
if(10==aRadix) {
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
result=0;
}
break;
}
---cut above here...*/
else {
//we've encountered a char that's not a legal number or sign
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
@ -729,11 +720,13 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
PRInt32 decPt=aString.FindChar(aString,'.',PR_TRUE,0);
char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1;
aRadix=10; //assume for starters...
aRadix=kRadixUnknown; //assume for starters...
// Skip trailing non-numeric...
while (cp >= aString.mStr) {
if((*cp>='0') && (*cp<='9')){
if(kRadixUnknown==aRadix)
aRadix=kRadix10;
break;
}
else if((*cp>='A') && (*cp<='F')) {
@ -761,7 +754,7 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
}
else {
if(('#'==(*cp)) || ('X'==(*cp)))
aRadix=16;
aRadix=kRadix16;
cp++; //move back by one
break;
}
@ -773,41 +766,23 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
return result;
}
/**
* Perform numeric string to int conversion with given radix.
* This method tries to autodetect that radix given a string
* @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
* @return 10,16,or 0 (meaning I don't know)
*/
PRInt32 nsString2::ToInteger(PRInt32* anErrorCode) const {
/********************************************************************************
If you called this version, it's because you don't know which radix your
string is stored in (hex, dec, etc.).
This method will attempt to figure that out for you, and then call
toInteger(err,radix).
NOTE: If you know your string is in a given radix, then it's better to
call toInteger(err,radix) directly, because there are cases where
this method cannot make the correct determination. For example,
a string that contains "123" can not be differentiated (hex vs. dec).
********************************************************************************/
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
PRInt32 result=0;
PRUint32 nsString2::DetermineRadix(void) {
PRUint32 result=kRadixUnknown;
if(0<mLength) {
nsAutoString2 theString(*this,eOneByte);
PRUint32 theRadix=10;
*anErrorCode=GetNumericSubstring(theString,theRadix);
if(NS_OK==*anErrorCode){
result=_ToInteger(theString,anErrorCode,theRadix);
}
if(NS_OK!=GetNumericSubstring(theString,result))
result=kRadixUnknown;
}
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
return result;
}
/**
* Perform decimal numeric string to int conversion.
* NOTE: In this version, we use the radix you give, even if it's wrong.
@ -820,11 +795,15 @@ PRInt32 nsString2::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
nsAutoString2 theString(*this,eOneByte);
PRUint32 theRadix=10;
PRUint32 theRadix=aRadix;
PRInt32 result=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
if(NS_OK==result){
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
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 result=NS_ERROR_ILLEGAL_VALUE;
}
return result;

View File

@ -50,6 +50,11 @@ class nsISizeOfHandler;
#define nsString2 nsString
#define nsAutoString2 nsAutoString
#define kRadix10 (10)
#define kRadix16 (16)
#define kAutoDetect (100)
#define kRadixUnknown (kAutoDetect+1)
class NS_COM nsSubsumeStr;
class NS_COM nsString2 : public nsStr {
@ -365,13 +370,18 @@ char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const;
*/
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
*/
PRInt32 ToInteger(PRInt32* aErrorCode) const;
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix) const;
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
/**********************************************************************

View File

@ -688,15 +688,6 @@ PRInt32 _ToInteger(nsString2& aString,PRInt32* anErrorCode,PRUint32 aRadix) {
else if(('+'==theChar) || (' '==theChar)) { //stop in a good state if you see this...
break;
}
/* The following block can be replaced with the next block
else if(('X'==theChar) || ('#'==theChar)) {
if(10==aRadix) {
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
result=0;
}
break;
}
---cut above here...*/
else {
//we've encountered a char that's not a legal number or sign
*anErrorCode=NS_ERROR_ILLEGAL_VALUE;
@ -729,11 +720,13 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
PRInt32 decPt=aString.FindChar(aString,'.',PR_TRUE,0);
char* cp = (kNotFound==decPt) ? aString.mStr + aString.mLength-1 : aString.mStr+decPt-1;
aRadix=10; //assume for starters...
aRadix=kRadixUnknown; //assume for starters...
// Skip trailing non-numeric...
while (cp >= aString.mStr) {
if((*cp>='0') && (*cp<='9')){
if(kRadixUnknown==aRadix)
aRadix=kRadix10;
break;
}
else if((*cp>='A') && (*cp<='F')) {
@ -761,7 +754,7 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
}
else {
if(('#'==(*cp)) || ('X'==(*cp)))
aRadix=16;
aRadix=kRadix16;
cp++; //move back by one
break;
}
@ -773,41 +766,23 @@ PRInt32 GetNumericSubstring(nsString2& aString,PRUint32& aRadix) {
return result;
}
/**
* Perform numeric string to int conversion with given radix.
* This method tries to autodetect that radix given a string
* @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
* @return 10,16,or 0 (meaning I don't know)
*/
PRInt32 nsString2::ToInteger(PRInt32* anErrorCode) const {
/********************************************************************************
If you called this version, it's because you don't know which radix your
string is stored in (hex, dec, etc.).
This method will attempt to figure that out for you, and then call
toInteger(err,radix).
NOTE: If you know your string is in a given radix, then it's better to
call toInteger(err,radix) directly, because there are cases where
this method cannot make the correct determination. For example,
a string that contains "123" can not be differentiated (hex vs. dec).
********************************************************************************/
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
PRInt32 result=0;
PRUint32 nsString2::DetermineRadix(void) {
PRUint32 result=kRadixUnknown;
if(0<mLength) {
nsAutoString2 theString(*this,eOneByte);
PRUint32 theRadix=10;
*anErrorCode=GetNumericSubstring(theString,theRadix);
if(NS_OK==*anErrorCode){
result=_ToInteger(theString,anErrorCode,theRadix);
}
if(NS_OK!=GetNumericSubstring(theString,result))
result=kRadixUnknown;
}
else *anErrorCode=NS_ERROR_ILLEGAL_VALUE;
return result;
}
/**
* Perform decimal numeric string to int conversion.
* NOTE: In this version, we use the radix you give, even if it's wrong.
@ -820,11 +795,15 @@ PRInt32 nsString2::ToInteger(PRInt32* anErrorCode,PRUint32 aRadix) const {
//copy chars to local buffer -- step down from 2 bytes to 1 if necessary...
nsAutoString2 theString(*this,eOneByte);
PRUint32 theRadix=10;
PRUint32 theRadix=aRadix;
PRInt32 result=GetNumericSubstring(theString,theRadix); //we actually don't use this radix; use given radix instead
if(NS_OK==result){
result=_ToInteger(theString,anErrorCode,aRadix); //note we use the given radix, not the computed one.
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 result=NS_ERROR_ILLEGAL_VALUE;
}
return result;

View File

@ -50,6 +50,11 @@ class nsISizeOfHandler;
#define nsString2 nsString
#define nsAutoString2 nsAutoString
#define kRadix10 (10)
#define kRadix16 (16)
#define kAutoDetect (100)
#define kRadixUnknown (kAutoDetect+1)
class NS_COM nsSubsumeStr;
class NS_COM nsString2 : public nsStr {
@ -365,13 +370,18 @@ char* ToCString(char* aBuf,PRUint32 aBufLength,PRUint32 anOffset=0) const;
*/
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
*/
PRInt32 ToInteger(PRInt32* aErrorCode) const;
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix) const;
PRInt32 ToInteger(PRInt32* aErrorCode,PRUint32 aRadix=kRadix10) const;
/**********************************************************************