Bug 1020420 part 2 - Refactor js_strtod. r=njn
authorJan de Mooij <jdemooij@mozilla.com>
Fri, 06 Jun 2014 11:17:49 +0200
changeset 206296 1962fd3aa8193c1a4678961833a1b15a4c241258
parent 206295 32b157ae5ed7752907b7158eaccd96499a14c789
child 206297 654117be57c2a674b7b80e8314ed962a67f97738
push id3741
push userasasaki@mozilla.com
push dateMon, 21 Jul 2014 20:25:18 +0000
treeherdermozilla-beta@4d6f46f5af68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnjn
bugs1020420
milestone32.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1020420 part 2 - Refactor js_strtod. r=njn
js/src/jsnum.cpp
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -1727,54 +1727,54 @@ js::ToUint16Slow(JSContext *cx, const Ha
     d = fmod(d, (double) m);
     if (d < 0)
         d += m;
     *out = (uint16_t) d;
     return true;
 }
 
 bool
-js_strtod(ThreadSafeContext *cx, const jschar *s, const jschar *send,
-          const jschar **ep, double *dp)
+js_strtod(ThreadSafeContext *cx, const jschar *begin, const jschar *end,
+          const jschar **dEnd, double *d)
 {
-    size_t i;
-    char cbuf[32];
-    char *cstr, *istr, *estr;
-    bool negative;
-    double d;
+    const jschar *s = SkipSpace(begin, end);
+    size_t length = end - s;
+
+    Vector<char, 32> chars(cx);
+    if (!chars.growByUninitialized(length + 1))
+        return false;
 
-    const jschar *s1 = SkipSpace(s, send);
-    size_t length = send - s1;
+    size_t i = 0;
+    for (; i < length; i++) {
+        if (s[i] >> 8)
+            break;
+        chars[i] = char(s[i]);
+    }
+    chars[i] = 0;
 
-    /* Use cbuf to avoid malloc */
-    if (length >= sizeof cbuf) {
-        cstr = (char *) cx->malloc_(length + 1);
-        if (!cstr)
-           return false;
-    } else {
-        cstr = cbuf;
+    /* Try to parse +Infinity, -Infinity or Infinity. */
+    {
+        char *afterSign = chars.begin();
+        bool negative = (*afterSign == '-');
+        if (negative || *afterSign == '+')
+            afterSign++;
+
+        if (*afterSign == 'I' && !strncmp(afterSign, "Infinity", 8)) {
+            *d = negative ? NegativeInfinity<double>() : PositiveInfinity<double>();
+            *dEnd = s + (afterSign - chars.begin()) + 8;
+            return true;
+        }
     }
 
-    for (i = 0; i != length; i++) {
-        if (s1[i] >> 8)
-            break;
-        cstr[i] = (char)s1[i];
-    }
-    cstr[i] = 0;
+    /* Everything else. */
+    int err;
+    char *ep;
+    *d = js_strtod_harder(cx->dtoaState(), chars.begin(), &ep, &err);
 
-    istr = cstr;
-    if ((negative = (*istr == '-')) != 0 || *istr == '+')
-        istr++;
-    if (*istr == 'I' && !strncmp(istr, "Infinity", 8)) {
-        d = negative ? NegativeInfinity<double>() : PositiveInfinity<double>();
-        estr = istr + 8;
-    } else {
-        int err;
-        d = js_strtod_harder(cx->dtoaState(), cstr, &estr, &err);
-    }
+    MOZ_ASSERT(ep >= chars.begin());
 
-    i = estr - cstr;
-    if (cstr != cbuf)
-        js_free(cstr);
-    *ep = i ? s1 + i : s;
-    *dp = d;
+    if (ep == chars.begin())
+        *dEnd = begin;
+    else
+        *dEnd = s + (ep - chars.begin());
+
     return true;
 }