OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 // The JSON lexical grammar is specified in the ECMAScript 5 standard, | 67 // The JSON lexical grammar is specified in the ECMAScript 5 standard, |
68 // section 15.12.1.1. The only allowed whitespace characters between tokens | 68 // section 15.12.1.1. The only allowed whitespace characters between tokens |
69 // are tab, carriage-return, newline and space. | 69 // are tab, carriage-return, newline and space. |
70 | 70 |
71 inline void AdvanceSkipWhitespace() { | 71 inline void AdvanceSkipWhitespace() { |
72 do { | 72 do { |
73 Advance(); | 73 Advance(); |
74 } while (c0_ == '\t' || c0_ == '\r' || c0_ == '\n' || c0_ == ' '); | 74 } while (c0_ == ' ' || c0_ == '\r' || c0_ == '\n' || c0_ == '\t'); |
75 } | 75 } |
76 | 76 |
77 inline void SkipWhitespace() { | 77 inline void SkipWhitespace() { |
78 while (c0_ == '\t' || c0_ == '\r' || c0_ == '\n' || c0_ == ' ') { | 78 while (c0_ == ' ' || c0_ == '\r' || c0_ == '\n' || c0_ == '\t') { |
79 Advance(); | 79 Advance(); |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 inline uc32 AdvanceGetChar() { | 83 inline uc32 AdvanceGetChar() { |
84 Advance(); | 84 Advance(); |
85 return c0_; | 85 return c0_; |
86 } | 86 } |
87 | 87 |
88 // Checks that current charater is c. | 88 // Checks that current charater is c. |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 | 556 |
557 template <bool seq_ascii> | 557 template <bool seq_ascii> |
558 template <bool is_symbol> | 558 template <bool is_symbol> |
559 Handle<String> JsonParser<seq_ascii>::ScanJsonString() { | 559 Handle<String> JsonParser<seq_ascii>::ScanJsonString() { |
560 ASSERT_EQ('"', c0_); | 560 ASSERT_EQ('"', c0_); |
561 Advance(); | 561 Advance(); |
562 if (c0_ == '"') { | 562 if (c0_ == '"') { |
563 AdvanceSkipWhitespace(); | 563 AdvanceSkipWhitespace(); |
564 return Handle<String>(isolate()->heap()->empty_string()); | 564 return Handle<String>(isolate()->heap()->empty_string()); |
565 } | 565 } |
| 566 |
| 567 if (seq_ascii && is_symbol) { |
| 568 // Fast path for existing symbols. If the the string being parsed is not |
| 569 // a known symbol, contains backslashes or unexpectedly reaches the end of |
| 570 // string, return with an empty handle. |
| 571 uint32_t running_hash = isolate()->heap()->HashSeed(); |
| 572 int position = position_; |
| 573 uc32 c0 = c0_; |
| 574 do { |
| 575 if (c0 == '\\') { |
| 576 return SlowScanJsonString<SeqAsciiString, char>(source_, |
| 577 position_, |
| 578 position); |
| 579 } |
| 580 running_hash = StringHasher::AddCharacterCore(running_hash, c0); |
| 581 position++; |
| 582 if (position > source_length_) return Handle<String>::null(); |
| 583 c0 = seq_source_->SeqAsciiStringGet(position); |
| 584 } while (c0 != '"'); |
| 585 int length = position - position_; |
| 586 uint32_t hash = (length <= String::kMaxHashCalcLength) |
| 587 ? StringHasher::GetHashCore(running_hash) : length; |
| 588 Vector<const char> string_vector( |
| 589 seq_source_->GetChars() + position_, length); |
| 590 SymbolTable* symbol_table = isolate()->heap()->symbol_table(); |
| 591 uint32_t capacity = symbol_table->Capacity(); |
| 592 uint32_t index = SymbolTable::FirstProbe(hash, capacity); |
| 593 uint32_t count = 1; |
| 594 while (true) { |
| 595 Object* element = symbol_table->KeyAt(index); |
| 596 if (element == isolate()->heap()->raw_unchecked_undefined_value()) { |
| 597 // Lookup failure. |
| 598 break; |
| 599 } |
| 600 if (element != isolate()->heap()->raw_unchecked_the_hole_value() && |
| 601 String::cast(element)->IsAsciiEqualTo(string_vector)) { |
| 602 // Lookup success, update the current position. |
| 603 position_ = position; |
| 604 // Advance past the last '"'. |
| 605 AdvanceSkipWhitespace(); |
| 606 return Handle<String>(String::cast(element)); |
| 607 } |
| 608 index = SymbolTable::NextProbe(hash, count++, capacity); |
| 609 } |
| 610 } |
| 611 |
566 int beg_pos = position_; | 612 int beg_pos = position_; |
567 // Fast case for ASCII only without escape characters. | 613 // Fast case for ASCII only without escape characters. |
568 do { | 614 do { |
569 // Check for control character (0x00-0x1f) or unterminated string (<0). | 615 // Check for control character (0x00-0x1f) or unterminated string (<0). |
570 if (c0_ < 0x20) return Handle<String>::null(); | 616 if (c0_ < 0x20) return Handle<String>::null(); |
571 if (c0_ != '\\') { | 617 if (c0_ != '\\') { |
572 if (seq_ascii || c0_ <= kMaxAsciiCharCode) { | 618 if (seq_ascii || c0_ <= kMaxAsciiCharCode) { |
573 Advance(); | 619 Advance(); |
574 } else { | 620 } else { |
575 return SlowScanJsonString<SeqTwoByteString, uc16>(source_, | 621 return SlowScanJsonString<SeqTwoByteString, uc16>(source_, |
(...skipping 19 matching lines...) Expand all Loading... |
595 } | 641 } |
596 ASSERT_EQ('"', c0_); | 642 ASSERT_EQ('"', c0_); |
597 // Advance past the last '"'. | 643 // Advance past the last '"'. |
598 AdvanceSkipWhitespace(); | 644 AdvanceSkipWhitespace(); |
599 return result; | 645 return result; |
600 } | 646 } |
601 | 647 |
602 } } // namespace v8::internal | 648 } } // namespace v8::internal |
603 | 649 |
604 #endif // V8_JSON_PARSER_H_ | 650 #endif // V8_JSON_PARSER_H_ |
OLD | NEW |