diff options
Diffstat (limited to 'src/3rdparty/javascriptcore/JavaScriptCore/runtime/NumberPrototype.cpp')
-rw-r--r-- | src/3rdparty/javascriptcore/JavaScriptCore/runtime/NumberPrototype.cpp | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/3rdparty/javascriptcore/JavaScriptCore/runtime/NumberPrototype.cpp b/src/3rdparty/javascriptcore/JavaScriptCore/runtime/NumberPrototype.cpp index 947324c..67210fa 100644 --- a/src/3rdparty/javascriptcore/JavaScriptCore/runtime/NumberPrototype.cpp +++ b/src/3rdparty/javascriptcore/JavaScriptCore/runtime/NumberPrototype.cpp @@ -25,9 +25,10 @@ #include "Error.h" #include "JSFunction.h" #include "JSString.h" +#include "Operations.h" #include "PrototypeFunction.h" +#include "StringBuilder.h" #include "dtoa.h" -#include "Operations.h" #include <wtf/Assertions.h> #include <wtf/MathExtras.h> #include <wtf/Vector.h> @@ -45,7 +46,7 @@ static JSValue JSC_HOST_CALL numberProtoFuncToPrecision(ExecState*, JSObject*, J // ECMA 15.7.4 -NumberPrototype::NumberPrototype(ExecState* exec, PassRefPtr<Structure> structure, Structure* prototypeFunctionStructure) +NumberPrototype::NumberPrototype(ExecState* exec, NonNullPassRefPtr<Structure> structure, Structure* prototypeFunctionStructure) : NumberObject(structure) { setInternalValue(jsNumber(exec, 0)); @@ -73,11 +74,12 @@ static UString integerPartNoExp(double d) bool resultIsInfOrNan = (decimalPoint == 9999); size_t length = strlen(result); - UString str = sign ? "-" : ""; + StringBuilder builder; + builder.append(sign ? "-" : ""); if (resultIsInfOrNan) - str += result; + builder.append((const char*)result); else if (decimalPoint <= 0) - str += "0"; + builder.append("0"); else { Vector<char, 1024> buf(decimalPoint + 1); @@ -89,10 +91,10 @@ static UString integerPartNoExp(double d) strncpy(buf.data(), result, decimalPoint); buf[decimalPoint] = '\0'; - str.append(buf.data()); + builder.append((const char*)(buf.data())); } - return str; + return builder.release(); } static UString charSequence(char c, int count) @@ -236,13 +238,16 @@ JSValue JSC_HOST_CALL numberProtoFuncToFixed(ExecState* exec, JSObject*, JSValue UString s; if (x < 0) { - s.append('-'); + s = "-"; x = -x; - } else if (x == -0.0) - x = 0; + } else { + s = ""; + if (x == -0.0) + x = 0; + } if (x >= pow(10.0, 21.0)) - return jsString(exec, s + UString::from(x)); + return jsString(exec, makeString(s, UString::from(x))); const double tenToTheF = pow(10.0, f); double n = floor(x * tenToTheF); @@ -253,17 +258,19 @@ JSValue JSC_HOST_CALL numberProtoFuncToFixed(ExecState* exec, JSObject*, JSValue int k = m.size(); if (k <= f) { - UString z; + StringBuilder z; for (int i = 0; i < f + 1 - k; i++) z.append('0'); - m = z + m; + z.append(m); + m = z.release(); k = f + 1; ASSERT(k == m.size()); } int kMinusf = k - f; + if (kMinusf < m.size()) - return jsString(exec, s + m.substr(0, kMinusf) + "." + m.substr(kMinusf)); - return jsString(exec, s + m.substr(0, kMinusf)); + return jsString(exec, makeString(s, m.substr(0, kMinusf), ".", m.substr(kMinusf))); + return jsString(exec, makeString(s, m.substr(0, kMinusf))); } static void fractionalPartToString(char* buf, int& i, const char* result, int resultLength, int fractionalDigits) @@ -391,7 +398,8 @@ JSValue JSC_HOST_CALL numberProtoFuncToPrecision(ExecState* exec, JSObject*, JSV if (x < 0) { s = "-"; x = -x; - } + } else + s = ""; if (!(doublePrecision >= 1 && doublePrecision <= 21)) // true for NaN return throwError(exec, RangeError, "toPrecision() argument must be between 1 and 21"); @@ -422,10 +430,10 @@ JSValue JSC_HOST_CALL numberProtoFuncToPrecision(ExecState* exec, JSObject*, JSV m = integerPartNoExp(n); if (e < -6 || e >= precision) { if (m.size() > 1) - m = m.substr(0, 1) + "." + m.substr(1); + m = makeString(m.substr(0, 1), ".", m.substr(1)); if (e >= 0) - return jsNontrivialString(exec, s + m + "e+" + UString::from(e)); - return jsNontrivialString(exec, s + m + "e-" + UString::from(-e)); + return jsNontrivialString(exec, makeString(s, m, "e+", UString::from(e))); + return jsNontrivialString(exec, makeString(s, m, "e-", UString::from(-e))); } } else { m = charSequence('0', precision); @@ -433,13 +441,13 @@ JSValue JSC_HOST_CALL numberProtoFuncToPrecision(ExecState* exec, JSObject*, JSV } if (e == precision - 1) - return jsString(exec, s + m); + return jsString(exec, makeString(s, m)); if (e >= 0) { if (e + 1 < m.size()) - return jsString(exec, s + m.substr(0, e + 1) + "." + m.substr(e + 1)); - return jsString(exec, s + m); + return jsString(exec, makeString(s, m.substr(0, e + 1), ".", m.substr(e + 1))); + return jsString(exec, makeString(s, m)); } - return jsNontrivialString(exec, s + "0." + charSequence('0', -(e + 1)) + m); + return jsNontrivialString(exec, makeString(s, "0.", charSequence('0', -(e + 1)), m)); } } // namespace JSC |