Use QString::toDouble to convert to numbers
authorLars Knoll <lars.knoll@digia.com>
Mon, 10 Dec 2012 23:41:35 +0000 (00:41 +0100)
committerErik Verbruggen <erik.verbruggen@digia.com>
Tue, 11 Dec 2012 09:36:05 +0000 (10:36 +0100)
QString::toDouble() is always using the C locale in Qt 5,
strtod_l seems to have some stability issues for me, and
creating a locale on the stack doesn't sound very performant.

Change-Id: I35705a125b0c5913a5390ed1429c4e7490300f92
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
qmljs_runtime.cpp
v4.pro

index 62e7be4..1e664d2 100644 (file)
@@ -44,6 +44,7 @@
 #include "qmljs_objects.h"
 #include "qv4ir_p.h"
 #include "qv4ecmaobjects_p.h"
+#include "private/qlocale_tools_p.h"
 
 #include <QtCore/qmath.h>
 #include <QtCore/qnumeric.h>
@@ -52,7 +53,6 @@
 #include <cassert>
 #include <typeinfo>
 #include <stdlib.h>
-#include <xlocale.h>
 
 namespace QQmlJS {
 namespace VM {
@@ -436,9 +436,17 @@ double __qmljs_string_to_number(ExecutionContext *, String *string)
     const QString s = string->toQString();
     if (s.startsWith(QLatin1String("0x")) || s.startsWith(QLatin1String("0X")))
         return s.toLong(0, 16);
-    locale_t c_locale = newlocale(LC_ALL_MASK, NULL, NULL);
-    double d = ::strtod_l(s.toUtf8().constData(), 0, c_locale);
-    freelocale(c_locale);
+    bool ok;
+    QByteArray ba = s.toLatin1();
+    const char *begin = ba.constData();
+    const char *end = 0;
+    double d = qstrtod(begin, &end, &ok);
+    if (end - begin != ba.size() - 1) {
+        if (ba == "Infinity")
+            d = INFINITY;
+        else
+            d = nan("");
+    }
     return d;
 }
 
diff --git a/v4.pro b/v4.pro
index 3aecdab..1f1604b 100644 (file)
--- a/v4.pro
+++ b/v4.pro
@@ -1,4 +1,4 @@
-QT = core qmldevtools-private
+QT = core-private qmldevtools-private
 CONFIG -= app_bundle
 CONFIG += console