| 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 <cmath> | 7 #include <cmath> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 } | 181 } |
| 182 | 182 |
| 183 private: | 183 private: |
| 184 int* const depth_; | 184 int* const depth_; |
| 185 | 185 |
| 186 DISALLOW_COPY_AND_ASSIGN(StackMarker); | 186 DISALLOW_COPY_AND_ASSIGN(StackMarker); |
| 187 }; | 187 }; |
| 188 | 188 |
| 189 } // namespace | 189 } // namespace |
| 190 | 190 |
| 191 // This is U+FFFD. |
| 192 const char kUnicodeReplacementString[] = "\xEF\xBF\xBD"; |
| 193 |
| 191 JSONParser::JSONParser(int options) | 194 JSONParser::JSONParser(int options) |
| 192 : options_(options), | 195 : options_(options), |
| 193 start_pos_(nullptr), | 196 start_pos_(nullptr), |
| 194 pos_(nullptr), | 197 pos_(nullptr), |
| 195 end_pos_(nullptr), | 198 end_pos_(nullptr), |
| 196 index_(0), | 199 index_(0), |
| 197 stack_depth_(0), | 200 stack_depth_(0), |
| 198 line_number_(0), | 201 line_number_(0), |
| 199 index_last_line_(0), | 202 index_last_line_(0), |
| 200 error_code_(JSONReader::JSON_NO_ERROR), | 203 error_code_(JSONReader::JSON_NO_ERROR), |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 | 622 |
| 620 // StringBuilder will internally build a StringPiece unless a UTF-16 | 623 // StringBuilder will internally build a StringPiece unless a UTF-16 |
| 621 // conversion occurs, at which point it will perform a copy into a | 624 // conversion occurs, at which point it will perform a copy into a |
| 622 // std::string. | 625 // std::string. |
| 623 StringBuilder string(NextChar()); | 626 StringBuilder string(NextChar()); |
| 624 | 627 |
| 625 int length = end_pos_ - start_pos_; | 628 int length = end_pos_ - start_pos_; |
| 626 int32_t next_char = 0; | 629 int32_t next_char = 0; |
| 627 | 630 |
| 628 while (CanConsume(1)) { | 631 while (CanConsume(1)) { |
| 632 int start_index = index_; |
| 629 pos_ = start_pos_ + index_; // CBU8_NEXT is postcrement. | 633 pos_ = start_pos_ + index_; // CBU8_NEXT is postcrement. |
| 630 CBU8_NEXT(start_pos_, index_, length, next_char); | 634 CBU8_NEXT(start_pos_, index_, length, next_char); |
| 631 if (next_char < 0 || !IsValidCharacter(next_char)) { | 635 if (next_char < 0 || !IsValidCharacter(next_char)) { |
| 632 ReportError(JSONReader::JSON_UNSUPPORTED_ENCODING, 1); | 636 if ((options_ & JSON_REPLACE_INVALID_CHARACTERS) == 0) { |
| 633 return false; | 637 ReportError(JSONReader::JSON_UNSUPPORTED_ENCODING, 1); |
| 638 return false; |
| 639 } |
| 640 CBU8_NEXT(start_pos_, start_index, length, next_char); |
| 641 string.Convert(); |
| 642 string.AppendString(kUnicodeReplacementString); |
| 643 continue; |
| 634 } | 644 } |
| 635 | 645 |
| 636 if (next_char == '"') { | 646 if (next_char == '"') { |
| 637 --index_; // Rewind by one because of CBU8_NEXT. | 647 --index_; // Rewind by one because of CBU8_NEXT. |
| 638 out->Swap(&string); | 648 out->Swap(&string); |
| 639 return true; | 649 return true; |
| 640 } | 650 } |
| 641 | 651 |
| 642 // If this character is not an escape sequence... | 652 // If this character is not an escape sequence... |
| 643 if (next_char != '\\') { | 653 if (next_char != '\\') { |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 979 const std::string& description) { | 989 const std::string& description) { |
| 980 if (line || column) { | 990 if (line || column) { |
| 981 return StringPrintf("Line: %i, column: %i, %s", | 991 return StringPrintf("Line: %i, column: %i, %s", |
| 982 line, column, description.c_str()); | 992 line, column, description.c_str()); |
| 983 } | 993 } |
| 984 return description; | 994 return description; |
| 985 } | 995 } |
| 986 | 996 |
| 987 } // namespace internal | 997 } // namespace internal |
| 988 } // namespace base | 998 } // namespace base |
| OLD | NEW |