diff options
author | mmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-07 17:15:41 +0000 |
---|---|---|
committer | mmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-07 17:15:41 +0000 |
commit | d9023abb77c05d411835658b1091bdbb592b45c2 (patch) | |
tree | 0fb92b1786b59c2c1a42a0d29d58df697e1b7ec0 /base/string_util.cc | |
parent | 9f16de03664224cc286851ca03369cf699dd7c3c (diff) | |
download | chromium_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/string_util.cc')
-rw-r--r-- | base/string_util.cc | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 3c7bb87..4f095b7 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -144,7 +144,7 @@ class StringToLongTraits { return strtol(str, endptr, kBase); } static inline bool valid_func(const string_type& str) { - return !isspace(str[0]); + return !str.empty() && !isspace(str[0]); } }; @@ -158,7 +158,7 @@ class WStringToLongTraits { return wcstol(str, endptr, kBase); } static inline bool valid_func(const string_type& str) { - return !iswspace(str[0]); + return !str.empty() && !iswspace(str[0]); } }; @@ -176,7 +176,7 @@ class StringToInt64Traits { #endif } static inline bool valid_func(const string_type& str) { - return !isspace(str[0]); + return !str.empty() && !isspace(str[0]); } }; @@ -194,7 +194,7 @@ class WStringToInt64Traits { #endif } static inline bool valid_func(const string_type& str) { - return !iswspace(str[0]); + return !str.empty() && !iswspace(str[0]); } }; @@ -211,7 +211,7 @@ class HexStringToLongTraits { return strtoul(str, endptr, kBase); } static inline bool valid_func(const string_type& str) { - return !isspace(str[0]); + return !str.empty() && !isspace(str[0]); } }; @@ -225,7 +225,33 @@ class HexWStringToLongTraits { return wcstoul(str, endptr, kBase); } static inline bool valid_func(const string_type& str) { - return !iswspace(str[0]); + return !str.empty() && !iswspace(str[0]); + } +}; + +class StringToDoubleTraits { + public: + typedef std::string string_type; + typedef double value_type; + static inline value_type convert_func(const string_type::value_type* str, + string_type::value_type** endptr) { + return strtod(str, endptr); + } + static inline bool valid_func(const string_type& str) { + return !str.empty() && !isspace(str[0]); + } +}; + +class WStringToDoubleTraits { + public: + typedef std::wstring string_type; + typedef double value_type; + static inline value_type convert_func(const string_type::value_type* str, + string_type::value_type** endptr) { + return wcstod(str, endptr); + } + static inline bool valid_func(const string_type& str) { + return !str.empty() && !iswspace(str[0]); } }; @@ -1197,6 +1223,14 @@ bool HexStringToInt(const std::wstring& input, int* output) { input, reinterpret_cast<long*>(output)); } +bool StringToDouble(const std::string& input, double* output) { + return StringToNumber<StringToDoubleTraits>(input, output); +} + +bool StringToDouble(const std::wstring& input, double* output) { + return StringToNumber<WStringToDoubleTraits>(input, output); +} + int StringToInt(const std::string& value) { int result; StringToInt(value, &result); @@ -1232,3 +1266,15 @@ int HexStringToInt(const std::wstring& value) { HexStringToInt(value, &result); return result; } + +double StringToDouble(const std::string& value) { + double result; + StringToDouble(value, &result); + return result; +} + +double StringToDouble(const std::wstring& value) { + double result; + StringToDouble(value, &result); + return result; +} |