/* ToNumber() on a string */ double jsV_stringtonumber(js_State *J, const char *s) { char *e; double n; while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s; if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && s[2] != 0) n = strtol(s + 2, &e, 16); else if (!strncmp(s, "Infinity", 8)) n = INFINITY, e = (char*)s + 8; else if (!strncmp(s, "+Infinity", 9)) n = INFINITY, e = (char*)s + 9; else if (!strncmp(s, "-Infinity", 9)) n = -INFINITY, e = (char*)s + 9; else n = js_stringtofloat(s, &e); while (jsY_iswhite(*e) || jsY_isnewline(*e)) ++e; if (*e) return NAN; return n; }
static void jsY_next(js_State *J) { Rune c; J->source += chartorune(&c, J->source); /* consume CR LF as one unit */ if (c == '\r' && *J->source == '\n') ++J->source; if (jsY_isnewline(c)) { J->line++; c = '\n'; } J->lexchar = c; }
static void jsB_parseInt(js_State *J) { const char *s = js_tostring(J, 1); double radix = js_isdefined(J, 2) ? js_tonumber(J, 2) : 10; char *e; double n; while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s; if (radix == 0) radix = 10; if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) { s += 2; radix = 16; } n = strtol(s, &e, radix); if (s == e) js_pushnumber(J, NAN); else js_pushnumber(J, n); }
static void jsB_parseFloat(js_State *J) { const char *s = js_tostring(J, 1); char *e; double n; while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s; if (!strncmp(s, "Infinity", 8)) js_pushnumber(J, INFINITY); else if (!strncmp(s, "+Infinity", 9)) js_pushnumber(J, INFINITY); else if (!strncmp(s, "-Infinity", 9)) js_pushnumber(J, -INFINITY); else { n = js_stringtofloat(s, &e); if (e == s) js_pushnumber(J, NAN); else js_pushnumber(J, n); } }