Reviewers: Lasse Reichstein,

Message:
Simplified version...

Description:
Improve ScanJsonNumber.

Please review this at http://codereview.chromium.org/6334106/

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
  M     src/array.js
  M     src/conversions.cc
  M     src/parser.cc
  M     src/scanner.h
  M     src/scanner.cc


Index: src/conversions.cc
===================================================================
--- src/conversions.cc  (revision 6635)
+++ src/conversions.cc  (working copy)
@@ -702,26 +702,12 @@


 const char* DoubleToCString(double v, Vector<char> buffer) {
-  StringBuilder builder(buffer.start(), buffer.length());
-
   switch (fpclassify(v)) {
-    case FP_NAN:
-      builder.AddString("NaN");
-      break;
-
-    case FP_INFINITE:
-      if (v < 0.0) {
-        builder.AddString("-Infinity");
-      } else {
-        builder.AddString("Infinity");
-      }
-      break;
-
-    case FP_ZERO:
-      builder.AddCharacter('0');
-      break;
-
+    case FP_NAN: return "NaN";
+    case FP_INFINITE: return (v < 0.0 ? "-Infinity" : "Infinity");
+    case FP_ZERO: return "0";
     default: {
+      StringBuilder builder(buffer.start(), buffer.length());
       int decimal_point;
       int sign;
       const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1;
@@ -764,9 +750,9 @@
         if (exponent < 0) exponent = -exponent;
         builder.AddFormatted("%d", exponent);
       }
+    return builder.Finalize();
     }
   }
-  return builder.Finalize();
 }


Index: src/parser.cc
===================================================================
--- src/parser.cc       (revision 6635)
+++ src/parser.cc       (working copy)
@@ -3941,16 +3941,10 @@
 Handle<Object> JsonParser::ParseJsonValue() {
   Token::Value token = scanner_.Next();
   switch (token) {
-    case Token::STRING: {
+    case Token::STRING:
       return GetString();
-    }
-    case Token::NUMBER: {
-      ASSERT(scanner_.is_literal_ascii());
-      double value = StringToDouble(scanner_.literal_ascii_string(),
- NO_FLAGS, // Hex, octal or trailing junk.
-                                    OS::nan_value());
-      return Factory::NewNumber(value);
-    }
+    case Token::NUMBER:
+      return Factory::NewNumber(scanner_.number);
     case Token::FALSE_LITERAL:
       return Factory::false_value();
     case Token::TRUE_LITERAL:
Index: src/array.js
===================================================================
--- src/array.js        (revision 6635)
+++ src/array.js        (working copy)
@@ -1018,9 +1018,11 @@
   } else {
     index = TO_INTEGER(index);
     // If index is negative, index from the end of the array.
-    if (index < 0) index = length + index;
-    // If index is still negative, search the entire array.
-    if (index < 0) index = 0;
+    if (index < 0) {
+      index = length + index;
+      // If index is still negative, search the entire array.
+      if (index < 0) index = 0;
+    }
   }
   var min = index;
   var max = length;
Index: src/scanner.cc
===================================================================
--- src/scanner.cc      (revision 6635)
+++ src/scanner.cc      (working copy)
@@ -516,17 +516,27 @@

 Token::Value JsonScanner::ScanJsonNumber() {
   LiteralScope literal(this);
-  if (c0_ == '-') AddLiteralCharAdvance();
+  bool sign = (c0_ == '-');
+
+  if (sign) AddLiteralCharAdvance();
   if (c0_ == '0') {
     AddLiteralCharAdvance();
     // Prefix zero is only allowed if it's the only digit before
     // a decimal point or exponent.
     if ('0' <= c0_ && c0_ <= '9') return Token::ILLEGAL;
   } else {
+    int i = 0;
+    int digits = 0;
     if (c0_ < '1' || c0_ > '9') return Token::ILLEGAL;
     do {
+      i = i * 10 + c0_ - '0';
+      digits++;
       AddLiteralCharAdvance();
     } while (c0_ >= '0' && c0_ <= '9');
+    if (c0_ != '.' && c0_ != 'e' && c0_ != 'E' && digits < 10) {
+      number = (sign ? -i : i);
+      return Token::NUMBER;
+    }
   }
   if (c0_ == '.') {
     AddLiteralCharAdvance();
@@ -544,6 +554,10 @@
     } while (c0_ >= '0' && c0_ <= '9');
   }
   literal.Complete();
+  ASSERT_NOT_NULL(next_.literal_chars);
+  number = StringToDouble(next_.literal_chars->ascii_literal(),
+                          NO_FLAGS,  // Hex, octal or trailing junk.
+                          OS::nan_value());
   return Token::NUMBER;
 }

Index: src/scanner.h
===================================================================
--- src/scanner.h       (revision 6635)
+++ src/scanner.h       (working copy)
@@ -148,6 +148,8 @@
   // Returns the next token.
   Token::Value Next();

+  double number;
+
  protected:
// Skip past JSON whitespace (only space, tab, newline and carrige-return).
   bool SkipJsonWhiteSpace();


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to