Add portable strtod implementation.

Ignore locale problems with strtod.
This commit is contained in:
Tor Andersson
2014-09-01 16:56:38 +02:00
parent 1f137b8e48
commit 93cc0584df
6 changed files with 543 additions and 27 deletions

View File

@@ -74,7 +74,7 @@ static void jsB_parseFloat(js_State *J)
else if (!strncmp(s, "-Infinity", 9))
js_pushnumber(J, -INFINITY);
else {
n = js_strtod(s, &e);
n = js_stringtofloat(s, &e);
if (e == s)
js_pushnumber(J, NAN);
else

544
jsdtoa.c

File diff suppressed because it is too large Load Diff

6
jsi.h
View File

@@ -58,6 +58,12 @@ const char *js_intern(js_State *J, const char *s);
void jsS_dumpstrings(js_State *J);
void jsS_freestrings(js_State *J);
/* Portable strtod and printf float formatting */
void js_fmtexp(char *p, int e);
void js_dtoa(double f, char *digits, int *exp, int *neg, int *ndigits);
double js_strtod(const char *as, char **aas);
/* Private stack functions */
void js_newfunction(js_State *J, js_Function *function, js_Environment *scope);

View File

@@ -310,7 +310,7 @@ static int lexnumber(js_State *J)
if (jsY_isidentifierstart(PEEK))
jsY_error(J, "number with letter suffix");
J->number = strtod(s, NULL);
J->number = js_strtod(s, NULL);
return TK_NUMBER;
}

View File

@@ -122,7 +122,7 @@ int jsV_toboolean(js_State *J, const js_Value *v)
}
}
double js_strtod(const char *s, char **ep)
double js_stringtofloat(const char *s, char **ep)
{
char *end;
double n;
@@ -136,7 +136,7 @@ double js_strtod(const char *s, char **ep)
if (*e == '+' || *e == '-') ++e;
while (*e >= '0' && *e <= '9') ++e;
}
n = strtod(s, &end);
n = js_strtod(s, &end);
if (end == e) {
*ep = (char*)e;
return n;
@@ -160,7 +160,7 @@ double jsV_stringtonumber(js_State *J, const char *s)
else if (!strncmp(s, "-Infinity", 9))
n = -INFINITY, e = (char*)s + 9;
else
n = js_strtod(s, &e);
n = js_stringtofloat(s, &e);
while (jsY_iswhite(*e) || jsY_isnewline(*e)) ++e;
if (*e) return NAN;
return n;
@@ -199,7 +199,7 @@ const char *jsV_numbertostring(js_State *J, double f)
if (isinf(f)) return f < 0 ? "-Infinity" : "Infinity";
if (f == 0) return "0";
jsV_dtoa(f, digits, &exp, &neg, &ndigits);
js_dtoa(f, digits, &exp, &neg, &ndigits);
point = ndigits + exp;
if (neg)
@@ -213,7 +213,7 @@ const char *jsV_numbertostring(js_State *J, double f)
while (n--)
*p++ = *s++;
}
jsV_fmtexp(p, point - 1);
js_fmtexp(p, point - 1);
}
else if (point <= 0) {

View File

@@ -130,7 +130,7 @@ const char *jsV_tostring(js_State *J, const js_Value *v);
js_Object *jsV_toobject(js_State *J, const js_Value *v);
js_Value jsV_toprimitive(js_State *J, const js_Value *v, int preferred);
double js_strtod(const char *s, char **ep);
double js_stringtofloat(const char *s, char **ep);
double jsV_numbertointeger(double n);
int jsV_numbertoint32(double n);
unsigned int jsV_numbertouint32(double n);
@@ -139,10 +139,6 @@ unsigned short jsV_numbertouint16(double n);
const char *jsV_numbertostring(js_State *J, double number);
double jsV_stringtonumber(js_State *J, const char *string);
/* jsdtoa.c */
void jsV_fmtexp(char *p, int e);
void jsV_dtoa(double f, char *digits, int *exp, int *neg, int *ndigits);
/* jsproperty.c */
js_Object *jsV_newobject(js_State *J, enum js_Class type, js_Object *prototype);
js_Property *jsV_getownproperty(js_State *J, js_Object *obj, const char *name);