зеркало из https://github.com/mozilla/pjs.git
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:
Родитель
0362da69ed
Коммит
4910d76401
|
@ -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);
|
||||||
|
if (conv_stopped == str+mLength) {
|
||||||
|
*aErrorCode = (PRInt32) NS_OK;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* Not all the string was scanned */
|
||||||
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
char *cp = strncpy(buf, get(), sizeof(buf) - 1);
|
else {
|
||||||
buf[sizeof(buf)-1] = '\0';
|
/* The string was too short (0 characters) */
|
||||||
float f = (float) PR_strtod(cp, &cp);
|
|
||||||
if (*cp != 0) {
|
|
||||||
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
||||||
}
|
}
|
||||||
*aErrorCode = (PRInt32) NS_OK;
|
return res;
|
||||||
return f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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);
|
||||||
|
if (*conv_stopped == '\0') {
|
||||||
|
*aErrorCode = (PRInt32) NS_OK;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* Not all the string was scanned */
|
||||||
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
char* cp = ToCString(buf, sizeof(buf));
|
else {
|
||||||
float f = (float) PR_strtod(cp, &cp);
|
/* The string was too short (0 characters) or too long (sizeof(buf)) */
|
||||||
if (*cp != 0) {
|
|
||||||
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
||||||
}
|
}
|
||||||
*aErrorCode = (PRInt32) NS_OK;
|
return res;
|
||||||
return f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
if (conv_stopped == str+mLength) {
|
||||||
|
*aErrorCode = (PRInt32) NS_OK;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* Not all the string was scanned */
|
||||||
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
char *cp = strncpy(buf, get(), sizeof(buf) - 1);
|
else {
|
||||||
buf[sizeof(buf)-1] = '\0';
|
/* The string was too short (0 characters) */
|
||||||
float f = (float) PR_strtod(cp, &cp);
|
|
||||||
if (*cp != 0) {
|
|
||||||
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
||||||
}
|
}
|
||||||
*aErrorCode = (PRInt32) NS_OK;
|
return res;
|
||||||
return f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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);
|
||||||
|
if (*conv_stopped == '\0') {
|
||||||
|
*aErrorCode = (PRInt32) NS_OK;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* Not all the string was scanned */
|
||||||
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
char* cp = ToCString(buf, sizeof(buf));
|
else {
|
||||||
float f = (float) PR_strtod(cp, &cp);
|
/* The string was too short (0 characters) or too long (sizeof(buf)) */
|
||||||
if (*cp != 0) {
|
|
||||||
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
*aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
|
||||||
}
|
}
|
||||||
*aErrorCode = (PRInt32) NS_OK;
|
return res;
|
||||||
return f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче