From: ager@chromium.org Date: Mon, 15 Sep 2008 12:17:36 +0000 (+0000) Subject: Simplify logic in string-to-double conversion code. X-Git-Tag: upstream/4.7.83~25370 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cf1a6a0bbc68a7369ba2723dfd6d8a9a2d18acb0;p=platform%2Fupstream%2Fv8.git Simplify logic in string-to-double conversion code. Fast case for strings that are definitely not numbers. Review URL: http://codereview.chromium.org/2847 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@309 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/conversions.cc b/src/conversions.cc index 85637ee..a1adb22 100644 --- a/src/conversions.cc +++ b/src/conversions.cc @@ -269,18 +269,27 @@ static double InternalStringToDouble(S* str, // Skip leading spaces. while ((index < len) && IsSpace(str, index)) index++; - // Compute sign of result. + // Is the string empty? + if (index >= len) return empty_string_val; + + // Get the first character. + uint16_t first = GetChar(str, index); + + // Numbers can only start with '-', '+', '.', 'I' (Infinity), or a digit. + if (first != '-' && first != '+' && first != '.' && first != 'I' && + (first > '9' || first < '0')) { + return JUNK_STRING_VALUE; + } + + // Compute sign of result based on first character. int sign = 1; - if (index < len && GetChar(str, index) == '-') { + if (first == '-') { sign = -1; index++; // String only containing a '-' are junk chars. if (index == len) return JUNK_STRING_VALUE; } - // string is empty? - if (index >= len) return empty_string_val; - // do we have a hex number? // (since the string is 0-terminated, it's ok to look one char beyond the end) if ((flags & ALLOW_HEX) != 0 &&