Title: [126781] trunk
Revision
126781
Author
[email protected]
Date
2012-08-27 11:57:57 -0700 (Mon, 27 Aug 2012)

Log Message

Add ECMAScript Number to String conversion to WTF::String
https://bugs.webkit.org/show_bug.cgi?id=95016

Reviewed by Geoffrey Garen.

Source/_javascript_Core: 

Rename UString::number(double) to UString::numberToStringECMAScript(double) to
differenciate it from the fixed-width conversion performed by String::number().

* parser/ParserArena.h:
(JSC::IdentifierArena::makeNumericIdentifier):
* runtime/JSONObject.cpp:
(JSC::Stringifier::appendStringifiedValue):
* runtime/NumberPrototype.cpp:
(JSC::numberProtoFuncToExponential):
(JSC::numberProtoFuncToFixed):
(JSC::numberProtoFuncToPrecision):
(JSC::numberProtoFuncToString):
* runtime/NumericStrings.h:
(JSC::NumericStrings::add):
* runtime/UString.cpp:
(JSC::UString::numberToStringECMAScript):
* runtime/UString.h:
(UString):

Source/WTF: 

* wtf/text/WTFString.cpp:
(WTF::String::numberToStringECMAScript):
* wtf/text/WTFString.h:
Add the implementation of numberToStringECMAScript(double) from UString to String.
This will make it easier to replace UString in the future.

Tools: 

* TestWebKitAPI/Tests/WTF/WTFString.cpp:
(TestWebKitAPI::testNumberToStringECMAScript):
(TestWebKitAPI): Add tests for String's ECMAString number conversion.

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (126780 => 126781)


--- trunk/Source/_javascript_Core/ChangeLog	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/_javascript_Core/ChangeLog	2012-08-27 18:57:57 UTC (rev 126781)
@@ -1,3 +1,29 @@
+2012-08-27  Benjamin Poulain  <[email protected]>
+
+        Add ECMAScript Number to String conversion to WTF::String
+        https://bugs.webkit.org/show_bug.cgi?id=95016
+
+        Reviewed by Geoffrey Garen.
+
+        Rename UString::number(double) to UString::numberToStringECMAScript(double) to
+        differenciate it from the fixed-width conversion performed by String::number().
+
+        * parser/ParserArena.h:
+        (JSC::IdentifierArena::makeNumericIdentifier):
+        * runtime/JSONObject.cpp:
+        (JSC::Stringifier::appendStringifiedValue):
+        * runtime/NumberPrototype.cpp:
+        (JSC::numberProtoFuncToExponential):
+        (JSC::numberProtoFuncToFixed):
+        (JSC::numberProtoFuncToPrecision):
+        (JSC::numberProtoFuncToString):
+        * runtime/NumericStrings.h:
+        (JSC::NumericStrings::add):
+        * runtime/UString.cpp:
+        (JSC::UString::numberToStringECMAScript):
+        * runtime/UString.h:
+        (UString):
+
 2012-08-27  Mikhail Pozdnyakov  <[email protected]>
 
         Rename RegisterProtocolHandler API to NavigatorContentUtils

Modified: trunk/Source/_javascript_Core/parser/ParserArena.h (126780 => 126781)


--- trunk/Source/_javascript_Core/parser/ParserArena.h	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/_javascript_Core/parser/ParserArena.h	2012-08-27 18:57:57 UTC (rev 126781)
@@ -113,7 +113,7 @@
     
     inline const Identifier& IdentifierArena::makeNumericIdentifier(JSGlobalData* globalData, double number)
     {
-        m_identifiers.append(Identifier(globalData, UString::number(number)));
+        m_identifiers.append(Identifier(globalData, UString::numberToStringECMAScript(number)));
         return m_identifiers.last();
     }
 

Modified: trunk/Source/_javascript_Core/runtime/JSONObject.cpp (126780 => 126781)


--- trunk/Source/_javascript_Core/runtime/JSONObject.cpp	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/_javascript_Core/runtime/JSONObject.cpp	2012-08-27 18:57:57 UTC (rev 126781)
@@ -387,7 +387,7 @@
         if (!isfinite(number))
             builder.append("null");
         else
-            builder.append(UString::number(number));
+            builder.append(UString::numberToStringECMAScript(number));
         return StringifySucceeded;
     }
 

Modified: trunk/Source/_javascript_Core/runtime/NumberPrototype.cpp (126780 => 126781)


--- trunk/Source/_javascript_Core/runtime/NumberPrototype.cpp	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/_javascript_Core/runtime/NumberPrototype.cpp	2012-08-27 18:57:57 UTC (rev 126781)
@@ -382,7 +382,7 @@
 
     // Handle NaN and Infinity.
     if (!isfinite(x))
-        return JSValue::encode(jsString(exec, UString::number(x)));
+        return JSValue::encode(jsString(exec, UString::numberToStringECMAScript(x)));
 
     // Round if the argument is not undefined, always format as exponential.
     char buffer[WTF::NumberToStringBufferLength];
@@ -415,7 +415,7 @@
     // This also covers Ininity, and structure the check so that NaN
     // values are also handled by numberToString
     if (!(fabs(x) < 1e+21))
-        return JSValue::encode(jsString(exec, UString::number(x)));
+        return JSValue::encode(jsString(exec, UString::numberToStringECMAScript(x)));
 
     // The check above will return false for NaN or Infinity, these will be
     // handled by numberToString.
@@ -446,11 +446,11 @@
 
     // To precision called with no argument is treated as ToString.
     if (isUndefined)
-        return JSValue::encode(jsString(exec, UString::number(x)));
+        return JSValue::encode(jsString(exec, UString::numberToStringECMAScript(x)));
 
     // Handle NaN and Infinity.
     if (!isfinite(x))
-        return JSValue::encode(jsString(exec, UString::number(x)));
+        return JSValue::encode(jsString(exec, UString::numberToStringECMAScript(x)));
 
     NumberToStringBuffer buffer;
     return JSValue::encode(jsString(exec, UString(numberToFixedPrecisionString(x, significantFigures, buffer))));
@@ -509,7 +509,7 @@
     }
 
     if (!isfinite(doubleValue))
-        return JSValue::encode(jsString(exec, UString::number(doubleValue)));
+        return JSValue::encode(jsString(exec, UString::numberToStringECMAScript(doubleValue)));
 
     RadixBuffer s;
     return JSValue::encode(jsString(exec, toStringWithRadix(s, doubleValue, radix)));

Modified: trunk/Source/_javascript_Core/runtime/NumericStrings.h (126780 => 126781)


--- trunk/Source/_javascript_Core/runtime/NumericStrings.h	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/_javascript_Core/runtime/NumericStrings.h	2012-08-27 18:57:57 UTC (rev 126781)
@@ -40,7 +40,7 @@
             if (d == entry.key && !entry.value.isNull())
                 return entry.value;
             entry.key = d;
-            entry.value = UString::number(d);
+            entry.value = UString::numberToStringECMAScript(d);
             return entry.value;
         }
 

Modified: trunk/Source/_javascript_Core/runtime/UString.cpp (126780 => 126781)


--- trunk/Source/_javascript_Core/runtime/UString.cpp	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/_javascript_Core/runtime/UString.cpp	2012-08-27 18:57:57 UTC (rev 126781)
@@ -90,7 +90,7 @@
 {
 }
 
-UString UString::number(double d)
+UString UString::numberToStringECMAScript(double d)
 {
     NumberToStringBuffer buffer;
     return UString(numberToString(d, buffer));

Modified: trunk/Source/_javascript_Core/runtime/UString.h (126780 => 126781)


--- trunk/Source/_javascript_Core/runtime/UString.h	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/_javascript_Core/runtime/UString.h	2012-08-27 18:57:57 UTC (rev 126781)
@@ -117,8 +117,9 @@
     static UString number(unsigned u) { return WTF::numberToStringImpl(u); }
     static UString number(long i) { return WTF::numberToStringImpl(i); }
     static UString number(long long i) { return WTF::numberToStringImpl(i); }
-    JS_EXPORT_PRIVATE static UString number(double);
 
+    static UString numberToStringECMAScript(double);
+
     // Find a single character or string, also with match function & latin1 forms.
     size_t find(UChar c, unsigned start = 0) const
         { return m_impl ? m_impl->find(c, start) : notFound; }

Modified: trunk/Source/WTF/ChangeLog (126780 => 126781)


--- trunk/Source/WTF/ChangeLog	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/WTF/ChangeLog	2012-08-27 18:57:57 UTC (rev 126781)
@@ -1,5 +1,18 @@
 2012-08-27  Benjamin Poulain  <[email protected]>
 
+        Add ECMAScript Number to String conversion to WTF::String
+        https://bugs.webkit.org/show_bug.cgi?id=95016
+
+        Reviewed by Geoffrey Garen.
+
+        * wtf/text/WTFString.cpp:
+        (WTF::String::numberToStringECMAScript):
+        * wtf/text/WTFString.h:
+        Add the implementation of numberToStringECMAScript(double) from UString to String.
+        This will make it easier to replace UString in the future.
+
+2012-08-27  Benjamin Poulain  <[email protected]>
+
         Even up WTF::String to CString functions
         https://bugs.webkit.org/show_bug.cgi?id=95008
 

Modified: trunk/Source/WTF/wtf/text/WTFString.cpp (126780 => 126781)


--- trunk/Source/WTF/wtf/text/WTFString.cpp	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/WTF/wtf/text/WTFString.cpp	2012-08-27 18:57:57 UTC (rev 126781)
@@ -430,6 +430,12 @@
     return String(numberToFixedWidthString(number, precision, buffer));
 }
 
+String String::numberToStringECMAScript(double number)
+{
+    NumberToStringBuffer buffer;
+    return String(numberToString(number, buffer));
+}
+
 int String::toIntStrict(bool* ok, int base) const
 {
     if (!m_impl) {

Modified: trunk/Source/WTF/wtf/text/WTFString.h (126780 => 126781)


--- trunk/Source/WTF/wtf/text/WTFString.h	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Source/WTF/wtf/text/WTFString.h	2012-08-27 18:57:57 UTC (rev 126781)
@@ -231,6 +231,9 @@
 
     WTF_EXPORT_STRING_API static String number(double, unsigned = ShouldRoundSignificantFigures | ShouldTruncateTrailingZeros, unsigned precision = 6);
 
+    // Number to String conversion following the ECMAScript definition.
+    WTF_EXPORT_STRING_API static String numberToStringECMAScript(double);
+
     // Find a single character or string, also with match function & latin1 forms.
     size_t find(UChar c, unsigned start = 0) const
         { return m_impl ? m_impl->find(c, start) : notFound; }

Modified: trunk/Tools/ChangeLog (126780 => 126781)


--- trunk/Tools/ChangeLog	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Tools/ChangeLog	2012-08-27 18:57:57 UTC (rev 126781)
@@ -1,3 +1,14 @@
+2012-08-27  Benjamin Poulain  <[email protected]>
+
+        Add ECMAScript Number to String conversion to WTF::String
+        https://bugs.webkit.org/show_bug.cgi?id=95016
+
+        Reviewed by Geoffrey Garen.
+
+        * TestWebKitAPI/Tests/WTF/WTFString.cpp:
+        (TestWebKitAPI::testNumberToStringECMAScript):
+        (TestWebKitAPI): Add tests for String's ECMAString number conversion.
+
 2012-08-27  Philippe Normand  <[email protected]>
 
         [GStreamer][Qt] WebAudio support

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp (126780 => 126781)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp	2012-08-27 18:55:11 UTC (rev 126780)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp	2012-08-27 18:57:57 UTC (rev 126781)
@@ -25,6 +25,9 @@
 
 #include "config.h"
 
+#include <limits>
+#include <wtf/MathExtras.h>
+#include <wtf/text/CString.h>
 #include <wtf/text/WTFString.h>
 
 namespace TestWebKitAPI {
@@ -46,4 +49,56 @@
     ASSERT_TRUE(String("Template Literal") == stringWithTemplate);
 }
 
+static void testNumberToStringECMAScript(double number, const char* reference)
+{
+    CString numberString = String::numberToStringECMAScript(number).latin1();
+    ASSERT_STREQ(reference, numberString.data());
+}
+
+TEST(WTF, StringNumberToStringECMAScriptBoundaries)
+{
+    typedef std::numeric_limits<double> Limits;
+
+    // Infinity.
+    testNumberToStringECMAScript(Limits::infinity(), "Infinity");
+    testNumberToStringECMAScript(-Limits::infinity(), "-Infinity");
+
+    // NaN.
+    testNumberToStringECMAScript(-Limits::quiet_NaN(), "NaN");
+
+    // Zeros.
+    testNumberToStringECMAScript(0, "0");
+    testNumberToStringECMAScript(-0, "0");
+
+    // Min-Max.
+    testNumberToStringECMAScript(Limits::min(), "2.2250738585072014e-308");
+    testNumberToStringECMAScript(Limits::max(), "1.7976931348623157e+308");
+}
+
+TEST(WTF, StringNumberToStringECMAScriptRegularNumbers)
+{
+    // Pi.
+    testNumberToStringECMAScript(piDouble, "3.141592653589793");
+    testNumberToStringECMAScript(piFloat, "3.1415927410125732");
+    testNumberToStringECMAScript(piOverTwoDouble, "1.5707963267948966");
+    testNumberToStringECMAScript(piOverTwoFloat, "1.5707963705062866");
+    testNumberToStringECMAScript(piOverFourDouble, "0.7853981633974483");
+    testNumberToStringECMAScript(piOverFourFloat, "0.7853981852531433");
+
+    // e.
+    const double e = 2.71828182845904523536028747135266249775724709369995;
+    testNumberToStringECMAScript(e, "2.718281828459045");
+
+    // c, speed of light in m/s.
+    const double c = 299792458;
+    testNumberToStringECMAScript(c, "299792458");
+
+    // Golen ratio.
+    const double phi = 1.6180339887498948482;
+    testNumberToStringECMAScript(phi, "1.618033988749895");
+}
+
+
+
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to