Title: [101151] trunk/Source/_javascript_Core
Revision
101151
Author
msab...@apple.com
Date
2011-11-24 22:47:48 -0800 (Thu, 24 Nov 2011)

Log Message

_javascript_ string to number conversion functions use characters()
https://bugs.webkit.org/show_bug.cgi?id=72974

Change the various JS to number routines to process strings
using characters8() or characters16() as appropriate.
Implemented using static template methods.

Reviewed by Filip Pizlo.

* runtime/JSGlobalObjectFunctions.cpp:
(JSC::isInfinity):
(JSC::jsHexIntegerLiteral):
(JSC::jsStrDecimalLiteral):
(JSC::toDouble):
(JSC::jsToNumber):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (101150 => 101151)


--- trunk/Source/_javascript_Core/ChangeLog	2011-11-25 06:44:19 UTC (rev 101150)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-11-25 06:47:48 UTC (rev 101151)
@@ -1,5 +1,23 @@
 2011-11-24  Michael Saboff  <msab...@apple.com>
 
+        _javascript_ string to number conversion functions use characters()
+        https://bugs.webkit.org/show_bug.cgi?id=72974
+
+        Change the various JS to number routines to process strings
+        using characters8() or characters16() as appropriate.
+        Implemented using static template methods.
+
+        Reviewed by Filip Pizlo.
+
+        * runtime/JSGlobalObjectFunctions.cpp:
+        (JSC::isInfinity):
+        (JSC::jsHexIntegerLiteral):
+        (JSC::jsStrDecimalLiteral):
+        (JSC::toDouble):
+        (JSC::jsToNumber):
+
+2011-11-24  Michael Saboff  <msab...@apple.com>
+
         Empty JSStrings are created as 16 bit
         https://bugs.webkit.org/show_bug.cgi?id=72968
 

Modified: trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.cpp (101150 => 101151)


--- trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.cpp	2011-11-25 06:44:19 UTC (rev 101150)
+++ trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.cpp	2011-11-25 06:47:48 UTC (rev 101151)
@@ -277,7 +277,8 @@
 
 static const int SizeOfInfinity = 8;
 
-static bool isInfinity(const UChar* data, const UChar* end)
+template <typename CharType>
+static bool isInfinity(const CharType* data, const CharType* end)
 {
     return (end - data) >= SizeOfInfinity
         && data[0] == 'I'
@@ -291,11 +292,12 @@
 }
 
 // See ecma-262 9.3.1
-static double jsHexIntegerLiteral(const UChar*& data, const UChar* end)
+template <typename CharType>
+static double jsHexIntegerLiteral(const CharType*& data, const CharType* end)
 {
     // Hex number.
     data += 2;
-    const UChar* firstDigitPosition = data;
+    const CharType* firstDigitPosition = data;
     double number = 0;
     while (true) {
         number = number * 16 + toASCIIHexValue(*data);
@@ -312,15 +314,16 @@
 }
 
 // See ecma-262 9.3.1
-static double jsStrDecimalLiteral(const UChar*& data, const UChar* end)
+template <typename CharType>
+static double jsStrDecimalLiteral(const CharType*& data, const CharType* end)
 {
     ASSERT(data < end);
 
     // Copy the sting into a null-terminated byte buffer, and call strtod.
     Vector<char, 32> byteBuffer;
-    for (const UChar* characters = data; characters < end; ++characters) {
-        UChar character = *characters;
-        byteBuffer.append(isASCII(character) ? character : 0);
+    for (const CharType* characters = data; characters < end; ++characters) {
+        CharType character = *characters;
+        byteBuffer.append(isASCII(character) ? static_cast<char>(character) : 0);
     }
     byteBuffer.append(0);
     char* endOfNumber;
@@ -361,50 +364,57 @@
     return std::numeric_limits<double>::quiet_NaN();
 }
 
-// See ecma-262 9.3.1
-double jsToNumber(const UString& s)
+template <typename CharType>
+static double toDouble(const CharType* characters, unsigned size)
 {
-    unsigned size = s.length();
+    const CharType* endCharacters = characters + size;
 
-    if (size == 1) {
-        UChar c = s[0];
-        if (isASCIIDigit(c))
-            return c - '0';
-        if (isStrWhiteSpace(c))
-            return 0;
-        return std::numeric_limits<double>::quiet_NaN();
-    }
-
-    const UChar* data = ""
-    const UChar* end = data + size;
-
     // Skip leading white space.
-    for (; data < end; ++data) {
-        if (!isStrWhiteSpace(*data))
+    for (; characters < endCharacters; ++characters) {
+        if (!isStrWhiteSpace(*characters))
             break;
     }
-
+    
     // Empty string.
-    if (data == end)
+    if (characters == endCharacters)
         return 0.0;
-
+    
     double number;
-    if (data[0] == '0' && data + 2 < end && (data[1] | 0x20) == 'x' && isASCIIHexDigit(data[2]))
-        number = jsHexIntegerLiteral(data, end);
+    if (characters[0] == '0' && characters + 2 < endCharacters && (characters[1] | 0x20) == 'x' && isASCIIHexDigit(characters[2]))
+        number = jsHexIntegerLiteral(characters, endCharacters);
     else
-        number = jsStrDecimalLiteral(data, end);
-
+        number = jsStrDecimalLiteral(characters, endCharacters);
+    
     // Allow trailing white space.
-    for (; data < end; ++data) {
-        if (!isStrWhiteSpace(*data))
+    for (; characters < endCharacters; ++characters) {
+        if (!isStrWhiteSpace(*characters))
             break;
     }
-    if (data != end)
+    if (characters != endCharacters)
         return std::numeric_limits<double>::quiet_NaN();
-
+    
     return number;
 }
 
+// See ecma-262 9.3.1
+double jsToNumber(const UString& s)
+{
+    unsigned size = s.length();
+
+    if (size == 1) {
+        UChar c = s[0];
+        if (isASCIIDigit(c))
+            return c - '0';
+        if (isStrWhiteSpace(c))
+            return 0;
+        return std::numeric_limits<double>::quiet_NaN();
+    }
+
+    if (s.is8Bit())
+        return toDouble(s.characters8(), size);
+    return toDouble(s.characters16(), size);
+}
+
 static double parseFloat(const UString& s)
 {
     unsigned size = s.length();
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to