Ignore:
Timestamp:
Nov 24, 2011, 10:47:48 PM (14 years ago)
Author:
[email protected]
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):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp

    r99812 r101151  
    278278static const int SizeOfInfinity = 8;
    279279
    280 static bool isInfinity(const UChar* data, const UChar* end)
     280template <typename CharType>
     281static bool isInfinity(const CharType* data, const CharType* end)
    281282{
    282283    return (end - data) >= SizeOfInfinity
     
    292293
    293294// See ecma-262 9.3.1
    294 static double jsHexIntegerLiteral(const UChar*& data, const UChar* end)
     295template <typename CharType>
     296static double jsHexIntegerLiteral(const CharType*& data, const CharType* end)
    295297{
    296298    // Hex number.
    297299    data += 2;
    298     const UChar* firstDigitPosition = data;
     300    const CharType* firstDigitPosition = data;
    299301    double number = 0;
    300302    while (true) {
     
    313315
    314316// See ecma-262 9.3.1
    315 static double jsStrDecimalLiteral(const UChar*& data, const UChar* end)
     317template <typename CharType>
     318static double jsStrDecimalLiteral(const CharType*& data, const CharType* end)
    316319{
    317320    ASSERT(data < end);
     
    319322    // Copy the sting into a null-terminated byte buffer, and call strtod.
    320323    Vector<char, 32> byteBuffer;
    321     for (const UChar* characters = data; characters < end; ++characters) {
    322         UChar character = *characters;
    323         byteBuffer.append(isASCII(character) ? character : 0);
     324    for (const CharType* characters = data; characters < end; ++characters) {
     325        CharType character = *characters;
     326        byteBuffer.append(isASCII(character) ? static_cast<char>(character) : 0);
    324327    }
    325328    byteBuffer.append(0);
     
    360363    // Not a number.
    361364    return std::numeric_limits<double>::quiet_NaN();
     365}
     366
     367template <typename CharType>
     368static double toDouble(const CharType* characters, unsigned size)
     369{
     370    const CharType* endCharacters = characters + size;
     371
     372    // Skip leading white space.
     373    for (; characters < endCharacters; ++characters) {
     374        if (!isStrWhiteSpace(*characters))
     375            break;
     376    }
     377   
     378    // Empty string.
     379    if (characters == endCharacters)
     380        return 0.0;
     381   
     382    double number;
     383    if (characters[0] == '0' && characters + 2 < endCharacters && (characters[1] | 0x20) == 'x' && isASCIIHexDigit(characters[2]))
     384        number = jsHexIntegerLiteral(characters, endCharacters);
     385    else
     386        number = jsStrDecimalLiteral(characters, endCharacters);
     387   
     388    // Allow trailing white space.
     389    for (; characters < endCharacters; ++characters) {
     390        if (!isStrWhiteSpace(*characters))
     391            break;
     392    }
     393    if (characters != endCharacters)
     394        return std::numeric_limits<double>::quiet_NaN();
     395   
     396    return number;
    362397}
    363398
     
    376411    }
    377412
    378     const UChar* data = s.characters();
    379     const UChar* end = data + size;
    380 
    381     // Skip leading white space.
    382     for (; data < end; ++data) {
    383         if (!isStrWhiteSpace(*data))
    384             break;
    385     }
    386 
    387     // Empty string.
    388     if (data == end)
    389         return 0.0;
    390 
    391     double number;
    392     if (data[0] == '0' && data + 2 < end && (data[1] | 0x20) == 'x' && isASCIIHexDigit(data[2]))
    393         number = jsHexIntegerLiteral(data, end);
    394     else
    395         number = jsStrDecimalLiteral(data, end);
    396 
    397     // Allow trailing white space.
    398     for (; data < end; ++data) {
    399         if (!isStrWhiteSpace(*data))
    400             break;
    401     }
    402     if (data != end)
    403         return std::numeric_limits<double>::quiet_NaN();
    404 
    405     return number;
     413    if (s.is8Bit())
     414        return toDouble(s.characters8(), size);
     415    return toDouble(s.characters16(), size);
    406416}
    407417
Note: See TracChangeset for help on using the changeset viewer.