diff options
author | mmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-05 22:46:15 +0000 |
---|---|---|
committer | mmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-05 22:46:15 +0000 |
commit | a218f15b205006e999a0dcabd7ebbe631def4e3b (patch) | |
tree | 6a6a5bec8ae5a81ddab077b7c0ee1c14f991bae0 /base/string_util.h | |
parent | 96d570e43c3f4cd1c8a5244b5fbf3712fba74db1 (diff) | |
download | chromium_src-a218f15b205006e999a0dcabd7ebbe631def4e3b.zip chromium_src-a218f15b205006e999a0dcabd7ebbe631def4e3b.tar.gz chromium_src-a218f15b205006e999a0dcabd7ebbe631def4e3b.tar.bz2 |
Allow string-to-int conversions to provide a return value indicating success. Implement them consistently on all platforms. Fill in a couple of necessary missing pieces. Eliminate the need for many uses of sscanf. Add tests.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@405 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.h')
-rw-r--r-- | base/string_util.h | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/base/string_util.h b/base/string_util.h index e5fd147..d47d5f2 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -328,8 +328,33 @@ std::string IntToString(int value); std::string Int64ToString(int64 value); std::wstring Int64ToWString(int64 value); std::wstring IntToWString(int value); + +// Perform a best-effort conversion of the input string to a numeric type, +// setting |*output| to the result of the conversion. Returns true for +// "perfect" conversions; returns false in the following cases: +// - Overflow/underflow. |*output| will be set to the maximum value supported +// by the data type. +// - Trailing characters in the string after parsing the number. |*output| +// will be set to the value of the number that was parsed. +// - No characters parseable as a number at the beginning of the string. +// |*output| will be set to 0. +// - Empty string. |*output| will be set to 0. +bool StringToInt(const std::string& input, int* output); +bool StringToInt(const std::wstring& input, int* output); +bool StringToInt64(const std::string& input, int64* output); +bool StringToInt64(const std::wstring& input, int64* output); +bool HexStringToInt(const std::string& input, int* output); +bool HexStringToInt(const std::wstring& input, int* 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. +int StringToInt(const std::string& value); +int StringToInt(const std::wstring& value); int64 StringToInt64(const std::string& value); int64 StringToInt64(const std::wstring& value); +int HexStringToInt(const std::string& value); +int HexStringToInt(const std::wstring& value); // Return a C++ string given printf-like input. std::string StringPrintf(const char* format, ...); |