summaryrefslogtreecommitdiffstats
path: root/base/json_reader.cc
diff options
context:
space:
mode:
authormmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-07 17:15:41 +0000
committermmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-07 17:15:41 +0000
commitd9023abb77c05d411835658b1091bdbb592b45c2 (patch)
tree0fb92b1786b59c2c1a42a0d29d58df697e1b7ec0 /base/json_reader.cc
parent9f16de03664224cc286851ca03369cf699dd7c3c (diff)
downloadchromium_src-d9023abb77c05d411835658b1091bdbb592b45c2.zip
chromium_src-d9023abb77c05d411835658b1091bdbb592b45c2.tar.gz
chromium_src-d9023abb77c05d411835658b1091bdbb592b45c2.tar.bz2
Cross-platform portability fixes for JSONReader. Adds generic string-to-double parsing and tests in string_util.
There is one behavior change here: numbers which "look" like integers by virtue of being free of '.', 'e', and 'E' are no longer rejected if they're not within the [INT_MIN .. INT_MAX] range. Instead, they'll be parsed and stored internally as doubles. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@519 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/json_reader.cc')
-rw-r--r--base/json_reader.cc44
1 files changed, 12 insertions, 32 deletions
diff --git a/base/json_reader.cc b/base/json_reader.cc
index 257f635..66d8f68 100644
--- a/base/json_reader.cc
+++ b/base/json_reader.cc
@@ -29,8 +29,7 @@
#include "base/json_reader.h"
-#include <float.h>
-
+#include "base/float_util.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/values.h"
@@ -343,39 +342,20 @@ JSONReader::Token JSONReader::ParseNumberToken() {
}
bool JSONReader::DecodeNumber(const Token& token, Value** node) {
- // Determine if we want to try to parse as an int or a double.
- bool is_double = false;
- for (int i = 0; i < token.length; ++i) {
- wchar_t c = *(token.begin + i);
- if ('e' == c || 'E' == c || '.' == c) {
- is_double = true;
- break;
- }
+ const std::wstring num_string(token.begin, token.length);
+
+ int num_int;
+ if (StringToInt(num_string, &num_int)) {
+ *node = Value::CreateIntegerValue(num_int);
+ return true;
}
- if (is_double) {
- // Try parsing as a double.
- double num_double;
- int parsed_values = swscanf_s(token.begin, L"%lf", &num_double);
- // Make sure we're not -INF, INF or NAN.
- if (1 == parsed_values && _finite(num_double)) {
- *node = Value::CreateRealValue(num_double);
- return true;
- }
- } else {
- int num_int;
- int parsed_values = swscanf_s(token.begin, L"%d", &num_int);
- if (1 == parsed_values) {
- // Ensure the parsed value matches the string. This makes sure we don't
- // overflow/underflow.
- const std::wstring& back_to_str = StringPrintf(L"%d", num_int);
- if (0 == wcsncmp(back_to_str.c_str(), token.begin,
- back_to_str.length())) {
- *node = Value::CreateIntegerValue(num_int);
- return true;
- }
- }
+ double num_double;
+ if (StringToDouble(num_string, &num_double) && base::IsFinite(num_double)) {
+ *node = Value::CreateRealValue(num_double);
+ return true;
}
+
return false;
}