Skip to content
  • darin@apple.com's avatar
    Streamline strtod and fix some related problems · 9be7456e
    darin@apple.com authored
    https://bugs.webkit.org/show_bug.cgi?id=82857
    
    Reviewed by Geoffrey Garen.
    
    Source/JavaScriptCore:
    
    * parser/Lexer.cpp:
    (JSC::Lexer<>::lex): Use parseDouble. Since we have already scanned the number
    and we know it has only correct characters, leading spaces, trailing junk, and
    trailing spaces are not a possibility. No need to add a trailing null character.
    
    * runtime/JSGlobalObjectFunctions.cpp:
    (JSC::parseInt): Changed overflow based 10 case to use parseDouble. No need
    to allow trailing junk since the code above already allows only numeric digits
    in the string. This code path is used only in unusual cases, so it's not
    optimized for 8-bit strings, but easily could be.
    (JSC::jsStrDecimalLiteral): Removed the allow trailing junk argument to this
    function template because all the callers are OK with trailing junk. Use the
    parseDouble function. No need to copy the data into a byte buffer, because
    parseDouble handles that.
    (JSC::toDouble): Got rid of the DisallowTrailingJunk argument to the
    jsStrDecimalLiteral function template. That's OK because this function
    already checks for trailing junk and handles it appropriately. The old code
    path was doing it twice.
    (JSC::parseFloat): Got rid of the AllowTrailingJunk argument to the
    jsStrDecimalLiteral function template; the template allows junk unconditionally.
    
    * runtime/LiteralParser.cpp:
    (JSC::::Lexer::lexNumber): Use parseDouble. Since we have already scanned the number
    and we know it has only correct characters, leading spaces, trailing junk, and
    trailing spaces are not a possibility. No need to add a trailing null character.
    No need to copy the data into a byte buffer, because parseDouble handles that.
    We could optimize the UChar case even more because we know all the characters
    are ASCII, but not doing that at this time.
    
    * JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def: Updated.
    
    Source/WebCore:
    
    Refactoring of code covered by existing tests.
    
    * dom/ViewportArguments.cpp:
    (WebCore::numericPrefix): Removed a confusing comment that just said
    "we tolerate extra characters" in a roundabout way. Made the "ok"
    argument optional. Changed to call the new version of charactersToFloat
    that returns the number of characters parsed rather than using the
    charactersToFloatIgnoringJunk/didReadNumber solution from before.
    (WebCore::findSizeValue): Since numericPrefix is guaranteed to return 0
    when it can't parse, removed the "ok" code. Also changed the unusual
    syntax "float(1.0)" to just "1", which works just as well.
    (WebCore::findScaleValue): Ditto.
    (WebCore::findUserScalableValue): Ditto.
    
    * html/parser/HTMLParserIdioms.cpp:
    (WebCore::parseToDoubleForNumberType): Removed an unneeded code path
    and replaced it with an assertion; toDouble no longer will return infinity
    or not-a-number values.
    
    Source/WTF:
    
    Replaced the strtod function template with a parseDouble function, eliminating
    the following unneeded features:
    
    - need for a trailing null character and a call to strlen
    - needless conversion of string lengths from size_t to int and back that created
      the possibility of incorrect truncation
    - one level of function call; use inlining instead
    - construction of the StringToDoubleConverter object; it was used to pass
      arguments that are known at compile time
    - most of the StringToDoubleConverter::StringToDouble function's body; it was
      code we did not need
    - parsing of Infinity and NaN at the strtod level; added recently when we moved
      from the old strtod to the new one, and not needed or helpful at this level
    - multiple copies of code to narrow to single byte strings; in many cases
      this was done even when starting with an LChar string that is already
      single-byte, now we handle this with an overload of parseDouble
    
    * wtf/dtoa.cpp:
    Removed a long comment about the original strtod function that no longer
    applies since we deleted that function long ago. Removed a lot of includes.
    Removed the strtod function templates and its instantiations, since they
    are now replaced by the parseDouble function.
    (WTF::Internal::parseDoubleFromLongString): Added.
    * wtf/dtoa.h:
    Added an include of ASCIICType.h so we can use isASCII in a function in this
    header. Left the heretofore unneeded include of double-conversion.h, since we
    now want to use it in a function in this header. Removed the AllowTrailingJunkTag
    and AllowTrailingSpacesTag enumerations and the strtod function template. Added
    new parseDouble function, and inline implementation of it.
    
    * wtf/dtoa/double-conversion.cc: Removed quite a bit of unused code, hardcoding
    all the StringToDouble function arguments that come from data members so it can
    be a much smaller static member function. Also changed the types of arguments
    from int to size_t.
    * wtf/dtoa/double-conversion.h: Removed most of the StringToDoubleConverter
    class, leaving only the conversion function as a static member function.
    
    * wtf/text/StringImpl.cpp:
    (WTF::StringImpl::toDouble): Got rid of didReadNumber.
    (WTF::StringImpl::toFloat): Ditto.
    * wtf/text/StringImpl.h: Ditto.
    * wtf/text/WTFString.cpp:
    (WTF::String::toDouble): Got rid of didReadNumber.
    (WTF::String::toFloat): Ditto.
    (WTF::toDoubleType): Rewrote this function to use parseDouble. Moved the code
    to skip leading spaces here, because other callers of parseDouble don't want
    to do that. Repurposed the check for an empty string so it's now the same
    code shared by all the "parsed nothing" cases. Removed the code to convert
    the buffer to ASCII for two reasons: (1) We don't need that code at all when
    CharType is LChar, and (2) We now handle this through the two overloads for
    the parseDouble function. Disallowing trailing junk is now handled here,
    rather than inside parseDouble.
    (WTF::charactersToDouble): Updated for changes to toDoubleType. Removed the
    didReadNumber argument.
    (WTF::charactersToFloat): Ditto. Also added overloads that return the parsed
    length. These are a slightly more powerful way to do what didReadNumber was
    used for before.
    
    * wtf/text/WTFString.h: Added comments, eliminated didReadNumber, and added
    overloads of charactersToFloat that replace charactersToFloatIgnoringJunk.
    
    
    git-svn-id: http://svn.webkit.org/repository/webkit/trunk@113454 268f45cc-cd09-0410-ab3c-d52691b4dbfc
    9be7456e