diff options
Diffstat (limited to 'src/qml/jsruntime/qv4numberobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4numberobject.cpp | 106 |
1 files changed, 67 insertions, 39 deletions
diff --git a/src/qml/jsruntime/qv4numberobject.cpp b/src/qml/jsruntime/qv4numberobject.cpp index 4ae30a7f35..0e653c18cb 100644 --- a/src/qml/jsruntime/qv4numberobject.cpp +++ b/src/qml/jsruntime/qv4numberobject.cpp @@ -1,31 +1,37 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtQml module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -39,13 +45,31 @@ #include <QtCore/qmath.h> #include <QtCore/QDebug> #include <cassert> -#include <double-conversion.h> using namespace QV4; DEFINE_OBJECT_VTABLE(NumberCtor); DEFINE_OBJECT_VTABLE(NumberObject); +struct NumberLocaleHolder : public NumberLocale +{ + NumberLocaleHolder() {} +}; + +Q_GLOBAL_STATIC(NumberLocaleHolder, numberLocaleHolder) + +NumberLocale::NumberLocale() : QLocale(QLocale::C), + // -128 means shortest string that can accurately represent the number. + defaultDoublePrecision(0xffffff80) +{ + setNumberOptions(QLocale::OmitGroupSeparator | QLocale::OmitLeadingZeroInExponent); +} + +const NumberLocale *NumberLocale::instance() +{ + return numberLocaleHolder(); +} + Heap::NumberCtor::NumberCtor(QV4::ExecutionContext *scope) : Heap::FunctionObject(scope, QStringLiteral("Number")) { @@ -71,7 +95,7 @@ void NumberPrototype::init(ExecutionEngine *engine, Object *ctor) ctor->defineReadonlyProperty(engine->id_prototype(), (o = this)); ctor->defineReadonlyProperty(engine->id_length(), Primitive::fromInt32(1)); - ctor->defineReadonlyProperty(QStringLiteral("NaN"), Primitive::fromDouble(qSNaN())); + ctor->defineReadonlyProperty(QStringLiteral("NaN"), Primitive::fromDouble(qt_qnan())); ctor->defineReadonlyProperty(QStringLiteral("NEGATIVE_INFINITY"), Primitive::fromDouble(-qInf())); ctor->defineReadonlyProperty(QStringLiteral("POSITIVE_INFINITY"), Primitive::fromDouble(qInf())); ctor->defineReadonlyProperty(QStringLiteral("MAX_VALUE"), Primitive::fromDouble(1.7976931348623158e+308)); @@ -125,7 +149,7 @@ ReturnedValue NumberPrototype::method_toString(CallContext *ctx) if (std::isnan(num)) { return scope.engine->newString(QStringLiteral("NaN"))->asReturnedValue(); - } else if (qIsInf(num)) { + } else if (qt_is_inf(num)) { return scope.engine->newString(QLatin1String(num < 0 ? "-Infinity" : "Infinity"))->asReturnedValue(); } @@ -199,17 +223,11 @@ ReturnedValue NumberPrototype::method_toFixed(CallContext *ctx) QString str; if (std::isnan(v)) str = QStringLiteral("NaN"); - else if (qIsInf(v)) + else if (qt_is_inf(v)) str = QString::fromLatin1(v < 0 ? "-Infinity" : "Infinity"); - else if (v < 1.e21) { - char buf[100]; - double_conversion::StringBuilder builder(buf, sizeof(buf)); - double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToFixed(v, fdigits, &builder); - str = QString::fromLatin1(builder.Finalize()); - // At some point, the 3rd party double-conversion code should be moved to qtcore. - // When that's done, we can use: -// str = QString::number(v, 'f', int (fdigits)); - } else + else if (v < 1.e21) + str = NumberLocale::instance()->toString(v, 'f', int(fdigits)); + else return RuntimeHelpers::stringFromNumber(ctx->engine(), v)->asReturnedValue(); return scope.engine->newString(str)->asReturnedValue(); } @@ -221,7 +239,7 @@ ReturnedValue NumberPrototype::method_toExponential(CallContext *ctx) if (scope.engine->hasException) return Encode::undefined(); - int fdigits = -1; + int fdigits = NumberLocale::instance()->defaultDoublePrecision; if (ctx->argc() && !ctx->args()[0].isUndefined()) { fdigits = ctx->args()[0].toInt32(); @@ -231,11 +249,7 @@ ReturnedValue NumberPrototype::method_toExponential(CallContext *ctx) } } - char str[100]; - double_conversion::StringBuilder builder(str, sizeof(str)); - double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToExponential(d, fdigits, &builder); - QString result = QString::fromLatin1(builder.Finalize()); - + QString result = NumberLocale::instance()->toString(d, 'e', fdigits); return scope.engine->newString(result)->asReturnedValue(); } @@ -249,16 +263,30 @@ ReturnedValue NumberPrototype::method_toPrecision(CallContext *ctx) if (!ctx->argc() || ctx->args()[0].isUndefined()) return RuntimeHelpers::toString(scope.engine, v); - double precision = ctx->args()[0].toInt32(); + int precision = ctx->args()[0].toInt32(); if (precision < 1 || precision > 21) { ScopedString error(scope, scope.engine->newString(QStringLiteral("Number.prototype.toPrecision: precision out of range"))); return ctx->engine()->throwRangeError(error); } - char str[100]; - double_conversion::StringBuilder builder(str, sizeof(str)); - double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToPrecision(v->asDouble(), precision, &builder); - QString result = QString::fromLatin1(builder.Finalize()); - + // TODO: Once we get a NumberOption to retain trailing zeroes, replace the code below with: + // QString result = NumberLocale::instance()->toString(v->asDouble(), 'g', precision); + QByteArray format = "%#." + QByteArray::number(precision) + "g"; + QString result = QString::asprintf(format.constData(), v->asDouble()); + if (result.endsWith(QLatin1Char('.'))) { + // This is 'f' notation, not 'e'. + result.chop(1); + } else { + int ePos = result.indexOf(QLatin1Char('e')); + if (ePos != -1) { + Q_ASSERT(ePos + 2 < result.length()); // always '+' or '-', and number, after 'e' + Q_ASSERT(ePos > 0); // 'e' is not the first character + + if (result.at(ePos + 2) == QLatin1Char('0')) // Drop leading zeroes in exponent + result = result.remove(ePos + 2, 1); + if (result.at(ePos - 1) == QLatin1Char('.')) // Drop trailing dots before 'e' + result = result.remove(ePos - 1, 1); + } + } return scope.engine->newString(result)->asReturnedValue(); } |