Improve jsV_stringtonumber.

Tolerate leading and trailing whitespace, and hexadecimals.
Return NAN if there is trailing garbage or errors.
This commit is contained in:
Tor Andersson
2014-02-11 15:26:29 +01:00
parent 7d698ae923
commit 60bde0ada4
2 changed files with 13 additions and 2 deletions
+2
View File
@@ -44,6 +44,7 @@ static int jsB_parseInt(js_State *J, int argc)
{
const char *s = js_tostring(J, 1);
double radix = js_isdefined(J, 2) ? js_tonumber(J, 2) : 10;
while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
js_pushnumber(J, strtol(s, NULL, radix == 0 ? 10 : radix));
return 1;
}
@@ -51,6 +52,7 @@ static int jsB_parseInt(js_State *J, int argc)
static int jsB_parseFloat(js_State *J, int argc)
{
const char *s = js_tostring(J, 1);
while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
js_pushnumber(J, strtod(s, NULL));
return 1;
}
+11 -2
View File
@@ -1,4 +1,5 @@
#include "jsi.h"
#include "jslex.h"
#include "jscompile.h"
#include "jsvalue.h"
#include "utf.h"
@@ -113,8 +114,16 @@ int jsV_toboolean(js_State *J, const js_Value *v)
/* ToNumber() on a string */
double jsV_stringtonumber(js_State *J, const char *s)
{
/* TODO: use lexer to parse string grammar */
return strtod(s, NULL);
char *e;
double n;
while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
if (s[0] == '0' && s[1] == 'x' && s[2] != 0)
n = strtol(s + 2, &e, 16);
else
n = strtod(s, &e);
while (jsY_iswhite(*e) || jsY_isnewline(*e)) ++e;
if (*e) return NAN;
return n;
}
/* ToNumber() on a value */