Bug 165877 - ToFloat never reported when the string wasn't a legal float. Also switch to strtod for a small performance win. r=jaggernaut@netscape.com, sr=alecf@netscape.com

This commit is contained in:
bratell%lysator.liu.se 2002-12-02 09:30:59 +00:00
Родитель 0362da69ed
Коммит 4910d76401
4 изменённых файлов: 60 добавлений и 36 удалений

Просмотреть файл

@ -464,19 +464,24 @@ nsCString::CompressWhitespace( PRBool aEliminateLeading,PRBool aEliminateTrailin
* @return float rep of string value * @return float rep of string value
*/ */
float nsCString::ToFloat(PRInt32* aErrorCode) const { float nsCString::ToFloat(PRInt32* aErrorCode) const {
char buf[100]; float res = 0.0f;
if (mLength > PRInt32(sizeof(buf)-1)) { if (mLength > 0) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; char *conv_stopped;
return 0.0f; const char *str = get();
} res = (float)strtod(str, &conv_stopped);
char *cp = strncpy(buf, get(), sizeof(buf) - 1); if (conv_stopped == str+mLength) {
buf[sizeof(buf)-1] = '\0';
float f = (float) PR_strtod(cp, &cp);
if (*cp != 0) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
*aErrorCode = (PRInt32) NS_OK; *aErrorCode = (PRInt32) NS_OK;
return f; }
else {
/* Not all the string was scanned */
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
}
else {
/* The string was too short (0 characters) */
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
return res;
} }
/** /**

Просмотреть файл

@ -536,18 +536,25 @@ char* nsString::ToCString(char* aBuf, PRUint32 aBufLength,PRUint32 anOffset) con
* @return float rep of string value * @return float rep of string value
*/ */
float nsString::ToFloat(PRInt32* aErrorCode) const { float nsString::ToFloat(PRInt32* aErrorCode) const {
float res = 0.0f;
char buf[100]; char buf[100];
if (mLength > PRInt32(sizeof(buf)-1)) { if (mLength > 0 && mLength < sizeof(buf)) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; char *conv_stopped;
return 0.0f; const char *str = ToCString(buf, sizeof(buf));
} res = (float)strtod(str, &conv_stopped);
char* cp = ToCString(buf, sizeof(buf)); if (*conv_stopped == '\0') {
float f = (float) PR_strtod(cp, &cp);
if (*cp != 0) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
*aErrorCode = (PRInt32) NS_OK; *aErrorCode = (PRInt32) NS_OK;
return f; }
else {
/* Not all the string was scanned */
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
}
else {
/* The string was too short (0 characters) or too long (sizeof(buf)) */
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
return res;
} }

Просмотреть файл

@ -464,19 +464,24 @@ nsCString::CompressWhitespace( PRBool aEliminateLeading,PRBool aEliminateTrailin
* @return float rep of string value * @return float rep of string value
*/ */
float nsCString::ToFloat(PRInt32* aErrorCode) const { float nsCString::ToFloat(PRInt32* aErrorCode) const {
char buf[100]; float res = 0.0f;
if (mLength > PRInt32(sizeof(buf)-1)) { if (mLength > 0) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; char *conv_stopped;
return 0.0f; const char *str = get();
} res = (float)strtod(str, &conv_stopped);
char *cp = strncpy(buf, get(), sizeof(buf) - 1); if (conv_stopped == str+mLength) {
buf[sizeof(buf)-1] = '\0';
float f = (float) PR_strtod(cp, &cp);
if (*cp != 0) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
*aErrorCode = (PRInt32) NS_OK; *aErrorCode = (PRInt32) NS_OK;
return f; }
else {
/* Not all the string was scanned */
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
}
else {
/* The string was too short (0 characters) */
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
return res;
} }
/** /**

Просмотреть файл

@ -536,18 +536,25 @@ char* nsString::ToCString(char* aBuf, PRUint32 aBufLength,PRUint32 anOffset) con
* @return float rep of string value * @return float rep of string value
*/ */
float nsString::ToFloat(PRInt32* aErrorCode) const { float nsString::ToFloat(PRInt32* aErrorCode) const {
float res = 0.0f;
char buf[100]; char buf[100];
if (mLength > PRInt32(sizeof(buf)-1)) { if (mLength > 0 && mLength < sizeof(buf)) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE; char *conv_stopped;
return 0.0f; const char *str = ToCString(buf, sizeof(buf));
} res = (float)strtod(str, &conv_stopped);
char* cp = ToCString(buf, sizeof(buf)); if (*conv_stopped == '\0') {
float f = (float) PR_strtod(cp, &cp);
if (*cp != 0) {
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
*aErrorCode = (PRInt32) NS_OK; *aErrorCode = (PRInt32) NS_OK;
return f; }
else {
/* Not all the string was scanned */
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
}
else {
/* The string was too short (0 characters) or too long (sizeof(buf)) */
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
}
return res;
} }