| 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/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // Overridden from base::Value: | 141 // Overridden from base::Value: |
| 142 virtual bool GetAsString(std::string* out_value) const OVERRIDE { | 142 virtual bool GetAsString(std::string* out_value) const OVERRIDE { |
| 143 string_piece_.CopyToString(out_value); | 143 string_piece_.CopyToString(out_value); |
| 144 return true; | 144 return true; |
| 145 } | 145 } |
| 146 virtual bool GetAsString(string16* out_value) const OVERRIDE { | 146 virtual bool GetAsString(string16* out_value) const OVERRIDE { |
| 147 *out_value = UTF8ToUTF16(string_piece_); | 147 *out_value = UTF8ToUTF16(string_piece_); |
| 148 return true; | 148 return true; |
| 149 } | 149 } |
| 150 virtual Value* DeepCopy() const OVERRIDE { | 150 virtual Value* DeepCopy() const OVERRIDE { |
| 151 return Value::CreateStringValue(string_piece_.as_string()); | 151 return new StringValue(string_piece_.as_string()); |
| 152 } | 152 } |
| 153 virtual bool Equals(const Value* other) const OVERRIDE { | 153 virtual bool Equals(const Value* other) const OVERRIDE { |
| 154 std::string other_string; | 154 std::string other_string; |
| 155 return other->IsType(TYPE_STRING) && other->GetAsString(&other_string) && | 155 return other->IsType(TYPE_STRING) && other->GetAsString(&other_string) && |
| 156 StringPiece(other_string) == string_piece_; | 156 StringPiece(other_string) == string_piece_; |
| 157 } | 157 } |
| 158 | 158 |
| 159 private: | 159 private: |
| 160 // The location in the original input stream. | 160 // The location in the original input stream. |
| 161 base::StringPiece string_piece_; | 161 base::StringPiece string_piece_; |
| (...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 858 return NULL; | 858 return NULL; |
| 859 } | 859 } |
| 860 | 860 |
| 861 pos_ = exit_pos; | 861 pos_ = exit_pos; |
| 862 index_ = exit_index; | 862 index_ = exit_index; |
| 863 | 863 |
| 864 StringPiece num_string(num_start, end_index - start_index); | 864 StringPiece num_string(num_start, end_index - start_index); |
| 865 | 865 |
| 866 int num_int; | 866 int num_int; |
| 867 if (StringToInt(num_string, &num_int)) | 867 if (StringToInt(num_string, &num_int)) |
| 868 return Value::CreateIntegerValue(num_int); | 868 return new FundamentalValue(num_int); |
| 869 | 869 |
| 870 double num_double; | 870 double num_double; |
| 871 if (base::StringToDouble(num_string.as_string(), &num_double) && | 871 if (base::StringToDouble(num_string.as_string(), &num_double) && |
| 872 IsFinite(num_double)) { | 872 IsFinite(num_double)) { |
| 873 return Value::CreateDoubleValue(num_double); | 873 return new FundamentalValue(num_double); |
| 874 } | 874 } |
| 875 | 875 |
| 876 return NULL; | 876 return NULL; |
| 877 } | 877 } |
| 878 | 878 |
| 879 bool JSONParser::ReadInt(bool allow_leading_zeros) { | 879 bool JSONParser::ReadInt(bool allow_leading_zeros) { |
| 880 char first = *pos_; | 880 char first = *pos_; |
| 881 int len = 0; | 881 int len = 0; |
| 882 | 882 |
| 883 char c = first; | 883 char c = first; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 899 switch (*pos_) { | 899 switch (*pos_) { |
| 900 case 't': { | 900 case 't': { |
| 901 const char* kTrueLiteral = "true"; | 901 const char* kTrueLiteral = "true"; |
| 902 const int kTrueLen = static_cast<int>(strlen(kTrueLiteral)); | 902 const int kTrueLen = static_cast<int>(strlen(kTrueLiteral)); |
| 903 if (!CanConsume(kTrueLen - 1) || | 903 if (!CanConsume(kTrueLen - 1) || |
| 904 !StringsAreEqual(pos_, kTrueLiteral, kTrueLen)) { | 904 !StringsAreEqual(pos_, kTrueLiteral, kTrueLen)) { |
| 905 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); | 905 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 906 return NULL; | 906 return NULL; |
| 907 } | 907 } |
| 908 NextNChars(kTrueLen - 1); | 908 NextNChars(kTrueLen - 1); |
| 909 return Value::CreateBooleanValue(true); | 909 return new FundamentalValue(true); |
| 910 } | 910 } |
| 911 case 'f': { | 911 case 'f': { |
| 912 const char* kFalseLiteral = "false"; | 912 const char* kFalseLiteral = "false"; |
| 913 const int kFalseLen = static_cast<int>(strlen(kFalseLiteral)); | 913 const int kFalseLen = static_cast<int>(strlen(kFalseLiteral)); |
| 914 if (!CanConsume(kFalseLen - 1) || | 914 if (!CanConsume(kFalseLen - 1) || |
| 915 !StringsAreEqual(pos_, kFalseLiteral, kFalseLen)) { | 915 !StringsAreEqual(pos_, kFalseLiteral, kFalseLen)) { |
| 916 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); | 916 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 917 return NULL; | 917 return NULL; |
| 918 } | 918 } |
| 919 NextNChars(kFalseLen - 1); | 919 NextNChars(kFalseLen - 1); |
| 920 return Value::CreateBooleanValue(false); | 920 return new FundamentalValue(false); |
| 921 } | 921 } |
| 922 case 'n': { | 922 case 'n': { |
| 923 const char* kNullLiteral = "null"; | 923 const char* kNullLiteral = "null"; |
| 924 const int kNullLen = static_cast<int>(strlen(kNullLiteral)); | 924 const int kNullLen = static_cast<int>(strlen(kNullLiteral)); |
| 925 if (!CanConsume(kNullLen - 1) || | 925 if (!CanConsume(kNullLen - 1) || |
| 926 !StringsAreEqual(pos_, kNullLiteral, kNullLen)) { | 926 !StringsAreEqual(pos_, kNullLiteral, kNullLen)) { |
| 927 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); | 927 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 928 return NULL; | 928 return NULL; |
| 929 } | 929 } |
| 930 NextNChars(kNullLen - 1); | 930 NextNChars(kNullLen - 1); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 953 const std::string& description) { | 953 const std::string& description) { |
| 954 if (line || column) { | 954 if (line || column) { |
| 955 return StringPrintf("Line: %i, column: %i, %s", | 955 return StringPrintf("Line: %i, column: %i, %s", |
| 956 line, column, description.c_str()); | 956 line, column, description.c_str()); |
| 957 } | 957 } |
| 958 return description; | 958 return description; |
| 959 } | 959 } |
| 960 | 960 |
| 961 } // namespace internal | 961 } // namespace internal |
| 962 } // namespace base | 962 } // namespace base |
| OLD | NEW |