| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/json/json_parser.h" | 5 #include "base/json/json_parser.h" |
| 6 | 6 |
| 7 #include "base/float_util.h" | 7 #include "base/float_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 } | 645 } |
| 646 | 646 |
| 647 int hex_digit = 0; | 647 int hex_digit = 0; |
| 648 if (!HexStringToInt(StringPiece(NextChar(), 2), &hex_digit)) { | 648 if (!HexStringToInt(StringPiece(NextChar(), 2), &hex_digit)) { |
| 649 ReportError(JSONReader::JSON_INVALID_ESCAPE, -1); | 649 ReportError(JSONReader::JSON_INVALID_ESCAPE, -1); |
| 650 return false; | 650 return false; |
| 651 } | 651 } |
| 652 NextChar(); | 652 NextChar(); |
| 653 | 653 |
| 654 if (hex_digit < kExtendedASCIIStart) | 654 if (hex_digit < kExtendedASCIIStart) |
| 655 string.Append(hex_digit); | 655 string.Append(static_cast<char>(hex_digit)); |
| 656 else | 656 else |
| 657 DecodeUTF8(hex_digit, &string); | 657 DecodeUTF8(hex_digit, &string); |
| 658 break; | 658 break; |
| 659 } | 659 } |
| 660 case 'u': { // UTF-16 sequence. | 660 case 'u': { // UTF-16 sequence. |
| 661 // UTF units are of the form \uXXXX. | 661 // UTF units are of the form \uXXXX. |
| 662 if (!CanConsume(5)) { // 5 being 'u' and four HEX digits. | 662 if (!CanConsume(5)) { // 5 being 'u' and four HEX digits. |
| 663 ReportError(JSONReader::JSON_INVALID_ESCAPE, 0); | 663 ReportError(JSONReader::JSON_INVALID_ESCAPE, 0); |
| 664 return false; | 664 return false; |
| 665 } | 665 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 default: | 707 default: |
| 708 ReportError(JSONReader::JSON_INVALID_ESCAPE, 0); | 708 ReportError(JSONReader::JSON_INVALID_ESCAPE, 0); |
| 709 return false; | 709 return false; |
| 710 } | 710 } |
| 711 } else if (next_char == '"') { | 711 } else if (next_char == '"') { |
| 712 --index_; // Rewind by one because of CBU8_NEXT. | 712 --index_; // Rewind by one because of CBU8_NEXT. |
| 713 out->Swap(&string); | 713 out->Swap(&string); |
| 714 return true; | 714 return true; |
| 715 } else { | 715 } else { |
| 716 if (next_char < kExtendedASCIIStart) | 716 if (next_char < kExtendedASCIIStart) |
| 717 string.Append(next_char); | 717 string.Append(static_cast<char>(next_char)); |
| 718 else | 718 else |
| 719 DecodeUTF8(next_char, &string); | 719 DecodeUTF8(next_char, &string); |
| 720 } | 720 } |
| 721 } | 721 } |
| 722 | 722 |
| 723 ReportError(JSONReader::JSON_SYNTAX_ERROR, 0); | 723 ReportError(JSONReader::JSON_SYNTAX_ERROR, 0); |
| 724 return false; | 724 return false; |
| 725 } | 725 } |
| 726 | 726 |
| 727 // Entry is at the first X in \uXXXX. | 727 // Entry is at the first X in \uXXXX. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 } | 784 } |
| 785 | 785 |
| 786 dest_string->append(code_unit8); | 786 dest_string->append(code_unit8); |
| 787 return true; | 787 return true; |
| 788 } | 788 } |
| 789 | 789 |
| 790 void JSONParser::DecodeUTF8(const int32& point, StringBuilder* dest) { | 790 void JSONParser::DecodeUTF8(const int32& point, StringBuilder* dest) { |
| 791 // Anything outside of the basic ASCII plane will need to be decoded from | 791 // Anything outside of the basic ASCII plane will need to be decoded from |
| 792 // int32 to a multi-byte sequence. | 792 // int32 to a multi-byte sequence. |
| 793 if (point < kExtendedASCIIStart) { | 793 if (point < kExtendedASCIIStart) { |
| 794 dest->Append(point); | 794 dest->Append(static_cast<char>(point)); |
| 795 } else { | 795 } else { |
| 796 char utf8_units[4] = { 0 }; | 796 char utf8_units[4] = { 0 }; |
| 797 int offset = 0; | 797 int offset = 0; |
| 798 CBU8_APPEND_UNSAFE(utf8_units, offset, point); | 798 CBU8_APPEND_UNSAFE(utf8_units, offset, point); |
| 799 dest->Convert(); | 799 dest->Convert(); |
| 800 // CBU8_APPEND_UNSAFE can overwrite up to 4 bytes, so utf8_units may not be | 800 // CBU8_APPEND_UNSAFE can overwrite up to 4 bytes, so utf8_units may not be |
| 801 // zero terminated at this point. |offset| contains the correct length. | 801 // zero terminated at this point. |offset| contains the correct length. |
| 802 dest->AppendString(std::string(utf8_units, offset)); | 802 dest->AppendString(std::string(utf8_units, offset)); |
| 803 } | 803 } |
| 804 } | 804 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 const std::string& description) { | 956 const std::string& description) { |
| 957 if (line || column) { | 957 if (line || column) { |
| 958 return StringPrintf("Line: %i, column: %i, %s", | 958 return StringPrintf("Line: %i, column: %i, %s", |
| 959 line, column, description.c_str()); | 959 line, column, description.c_str()); |
| 960 } | 960 } |
| 961 return description; | 961 return description; |
| 962 } | 962 } |
| 963 | 963 |
| 964 } // namespace internal | 964 } // namespace internal |
| 965 } // namespace base | 965 } // namespace base |
| OLD | NEW |