ref: 7bb55ad3567ab7c11986727622662c3d1317080d
parent: 0cbd5326f23bd1106f14f934445df50db7481038
author: Tor Andersson <tor.andersson@artifex.com>
date: Sat Nov 29 07:39:57 EST 2014
Add js_itoa for faster conversion of unsigned int to string.
--- a/jsarray.c
+++ b/jsarray.c
@@ -20,29 +20,25 @@
int js_hasindex(js_State *J, int idx, unsigned int i)
{
char buf[32];
- sprintf(buf, "%u", i);
- return js_hasproperty(J, idx, buf);
+ return js_hasproperty(J, idx, js_itoa(buf, i));
}
void js_getindex(js_State *J, int idx, unsigned int i)
{
char buf[32];
- sprintf(buf, "%u", i);
- js_getproperty(J, idx, buf);
+ js_getproperty(J, idx, js_itoa(buf, i));
}
void js_setindex(js_State *J, int idx, unsigned int i)
{
char buf[32];
- sprintf(buf, "%u", i);
- js_setproperty(J, idx, buf);
+ js_setproperty(J, idx, js_itoa(buf, i));
}
void js_delindex(js_State *J, int idx, unsigned int i)
{
char buf[32];
- sprintf(buf, "%u", i);
- js_delproperty(J, idx, buf);
+ js_delproperty(J, idx, js_itoa(buf, i));
}
static void jsB_new_Array(js_State *J)
--- a/json.c
+++ b/json.c
@@ -183,7 +183,7 @@
static void fmtarray(js_State *J, js_Buffer **sb, const char *gap, int level)
{
unsigned int n, k;
- char buf[40];
+ char buf[32];
n = js_getlength(J, -1);
@@ -191,7 +191,7 @@
for (k = 0; k < n; ++k) {
if (k) js_putc(J, sb, ',');
if (gap) fmtindent(J, sb, gap, level + 1);
- sprintf(buf, "%u", k);
+ js_itoa(buf, k);
js_getproperty(J, -1, buf);
if (!fmtvalue(J, sb, js_intern(J, buf), gap, level + 1))
js_puts(J, sb, "null");
--- a/jsproperty.c
+++ b/jsproperty.c
@@ -269,7 +269,7 @@
if (obj->type == JS_CSTRING) {
for (k = 0; k < obj->u.s.length; ++k) {
- sprintf(buf, "%u", k);
+ js_itoa(buf, k);
if (!itshadow(J, top, obj, buf)) {
ITADD(js_intern(J, buf));
}
@@ -327,8 +327,7 @@
}
} else {
for (k = newlen; k < obj->u.a.length; ++k) {
- sprintf(buf, "%u", k);
- jsV_delproperty(J, obj, buf);
+ jsV_delproperty(J, obj, js_itoa(buf, k));
}
}
}
--- a/jsvalue.c
+++ b/jsvalue.c
@@ -134,6 +134,22 @@
}
}
+const char *js_itoa(char *out, unsigned int a)
+{
+ char buf[32], *s = out;
+ unsigned int i = 0;
+ while (a) {
+ buf[i++] = (a % 10) + '0';
+ a /= 10;
+ }
+ if (i == 0)
+ buf[i++] = '0';
+ while (i > 0)
+ *s++ = buf[--i];
+ *s = 0;
+ return out;
+}
+
double js_stringtofloat(const char *s, char **ep)
{
char *end;
--- a/jsvalue.h
+++ b/jsvalue.h
@@ -140,6 +140,7 @@
js_Object *jsV_toobject(js_State *J, const js_Value *v);
js_Value jsV_toprimitive(js_State *J, const js_Value *v, int preferred);
+const char *js_itoa(char buf[32], unsigned int a);
double js_stringtofloat(const char *s, char **ep);
double jsV_numbertointeger(double n);
int jsV_numbertoint32(double n);