Cleanup of parseInt checkin, including comment changes. The code is

now a little more explit about when it expects js strings to be
null-terminated.
This commit is contained in:
mccabe 1998-06-02 23:21:08 +00:00
Родитель 3d4d028e30
Коммит 71be32cc6b
2 изменённых файлов: 25 добавлений и 14 удалений

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

@ -97,7 +97,7 @@ num_parseFloat(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
return JS_TRUE;
}
/* See ECMA 15.1.2.2 */
/* See ECMA 15.1.2.2. */
static JSBool
num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
@ -105,7 +105,8 @@ num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
jsint radix;
const jschar *chars, *start;
jsdouble sum;
intN negative, digit, newDigit;
JSBool negative;
uintN digit, newDigit;
jschar c;
jschar digitMax = '9';
jschar lowerCaseBound = 'a';
@ -115,7 +116,12 @@ num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if (!str)
return JS_FALSE;
chars = str->chars;
while (JS_ISSPACE(*chars) && *chars != 0)
/* This assumes that the char strings are null-terminated - JS_ISSPACE will
* always evaluate to false at the end of a string containing only
* whitespace.
*/
while (JS_ISSPACE(*chars))
chars++;
if ((negative = (*chars == '-')) != 0 || *chars == '+')
@ -143,10 +149,9 @@ num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
/* No radix supplied, or some radix that evaluated to 0. */
if (*chars == '0') {
/* It's either hex or octal; only increment char if str isn't '0' */
if ((*(chars + 1) != 0) &&
(*(++chars) == 'X' || *chars == 'x')) /* Hex */
if (*(chars + 1) == 'X' || *(chars + 1) == 'x') /* Hex */
{
chars++;
chars += 2;
radix = 16;
} else { /* Octal */
radix = 8;
@ -176,7 +181,7 @@ num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
}
digit = 0; /* how many digits have we seen? (if radix == 10) */
sum = 0;
while ((c = *chars) != 0) {
while ((c = *chars) != 0) { /* 0 is never a valid character. */
if ('0' <= c && c <= digitMax)
newDigit = c - '0';
else if ('a' <= c && c < lowerCaseBound)

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

@ -19,6 +19,7 @@
/*
* JS number type and wrapper class.
*/
#include "jsstddef.h"
#include <errno.h>
#ifdef XP_PC
#include <float.h>
@ -96,7 +97,7 @@ num_parseFloat(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
return JS_TRUE;
}
/* See ECMA 15.1.2.2 */
/* See ECMA 15.1.2.2. */
static JSBool
num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
@ -104,7 +105,8 @@ num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
jsint radix;
const jschar *chars, *start;
jsdouble sum;
intN negative, digit, newDigit;
JSBool negative;
uintN digit, newDigit;
jschar c;
jschar digitMax = '9';
jschar lowerCaseBound = 'a';
@ -114,7 +116,12 @@ num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if (!str)
return JS_FALSE;
chars = str->chars;
while (JS_ISSPACE(*chars) && *chars != 0)
/* This assumes that the char strings are null-terminated - JS_ISSPACE will
* always evaluate to false at the end of a string containing only
* whitespace.
*/
while (JS_ISSPACE(*chars))
chars++;
if ((negative = (*chars == '-')) != 0 || *chars == '+')
@ -142,10 +149,9 @@ num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
/* No radix supplied, or some radix that evaluated to 0. */
if (*chars == '0') {
/* It's either hex or octal; only increment char if str isn't '0' */
if ((*(chars + 1) != 0) &&
(*(++chars) == 'X' || *chars == 'x')) /* Hex */
if (*(chars + 1) == 'X' || *(chars + 1) == 'x') /* Hex */
{
chars++;
chars += 2;
radix = 16;
} else { /* Octal */
radix = 8;
@ -175,7 +181,7 @@ num_parseInt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
}
digit = 0; /* how many digits have we seen? (if radix == 10) */
sum = 0;
while ((c = *chars) != 0) {
while ((c = *chars) != 0) { /* 0 is never a valid character. */
if ('0' <= c && c <= digitMax)
newDigit = c - '0';
else if ('a' <= c && c < lowerCaseBound)