From d9023abb77c05d411835658b1091bdbb592b45c2 Mon Sep 17 00:00:00 2001 From: "mmentovai@google.com" Date: Thu, 7 Aug 2008 17:15:41 +0000 Subject: 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 --- base/string_util.h | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'base/string_util.h') diff --git a/base/string_util.h b/base/string_util.h index 129f124..b89694f 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -342,6 +342,14 @@ bool StringToInt64(const std::wstring& input, int64* output); bool HexStringToInt(const std::string& input, int* output); bool HexStringToInt(const std::wstring& input, int* output); +// For floating-point conversions, only conversions of input strings in decimal +// form are defined to work. Behavior with strings representing floating-point +// numbers in hexadecimal, and strings representing non-fininte values (such +// as NaN and inf) is undefined. Otherwise, these behave the same as the +// integral variants above. +bool StringToDouble(const std::string& input, double* output); +bool StringToDouble(const std::wstring& input, double* output); + // Convenience forms of the above, when the caller is uninterested in the // boolean return value. These return only the |*output| value from the // above conversions: a best-effort conversion when possible, otherwise, 0. @@ -351,6 +359,8 @@ int64 StringToInt64(const std::string& value); int64 StringToInt64(const std::wstring& value); int HexStringToInt(const std::string& value); int HexStringToInt(const std::wstring& value); +double StringToDouble(const std::string& value); +double StringToDouble(const std::wstring& value); // Return a C++ string given printf-like input. std::string StringPrintf(const char* format, ...); -- cgit v1.1