diff options
author | dsh@google.com <dsh@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-28 01:01:50 +0000 |
---|---|---|
committer | dsh@google.com <dsh@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-28 01:01:50 +0000 |
commit | 41fb1d7d262e44d8561a5c4d6d7e353f638b56e8 (patch) | |
tree | 0b6159a725069a98f9fb6e6e451949a6ce37ccfa /base | |
parent | 3131a289c0edff9d58da46cae16beae6bd6c0650 (diff) | |
download | chromium_src-41fb1d7d262e44d8561a5c4d6d7e353f638b56e8.zip chromium_src-41fb1d7d262e44d8561a5c4d6d7e353f638b56e8.tar.gz chromium_src-41fb1d7d262e44d8561a5c4d6d7e353f638b56e8.tar.bz2 |
Get rid of wstring variants of StringToFoo.
Review URL: http://codereview.chromium.org/28281
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10664 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/json_reader.cc | 5 | ||||
-rw-r--r-- | base/string_util.cc | 84 | ||||
-rw-r--r-- | base/string_util.h | 18 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 32 |
4 files changed, 88 insertions, 51 deletions
diff --git a/base/json_reader.cc b/base/json_reader.cc index f3dfc75..b5013ef 100644 --- a/base/json_reader.cc +++ b/base/json_reader.cc @@ -357,11 +357,12 @@ Value* JSONReader::DecodeNumber(const Token& token) { const std::wstring num_string(token.begin, token.length); int num_int; - if (StringToInt(num_string, &num_int)) + if (StringToInt(WideToUTF16Hack(num_string), &num_int)) return Value::CreateIntegerValue(num_int); double num_double; - if (StringToDouble(num_string, &num_double) && base::IsFinite(num_double)) + if (StringToDouble(WideToUTF16Hack(num_string), &num_double) && + base::IsFinite(num_double)) return Value::CreateRealValue(num_double); return NULL; diff --git a/base/string_util.cc b/base/string_util.cc index d84fadb..64c0cdc 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -127,14 +127,25 @@ class StringToLongTraits { } }; -class WStringToLongTraits { +class String16ToLongTraits { public: - typedef std::wstring string_type; + typedef string16 string_type; typedef long value_type; static const int kBase = 10; static inline value_type convert_func(const string_type::value_type* str, string_type::value_type** endptr) { +#if defined(WCHAR_T_IS_UTF16) return wcstol(str, endptr, kBase); +#elif defined(WCHAR_T_IS_UTF32) + std::string ascii_string = UTF16ToASCII(string16(str)); + char* ascii_end = NULL; + value_type ret = strtol(ascii_string.c_str(), &ascii_end, kBase); + if (ascii_string.c_str() + ascii_string.length() == ascii_end) { + *endptr = + const_cast<string_type::value_type*>(str) + ascii_string.length(); + } + return ret; +#endif } static inline bool valid_func(const string_type& str) { return !str.empty() && !iswspace(str[0]); @@ -159,9 +170,9 @@ class StringToInt64Traits { } }; -class WStringToInt64Traits { +class String16ToInt64Traits { public: - typedef std::wstring string_type; + typedef string16 string_type; typedef int64 value_type; static const int kBase = 10; static inline value_type convert_func(const string_type::value_type* str, @@ -169,7 +180,14 @@ class WStringToInt64Traits { #ifdef OS_WIN return _wcstoi64(str, endptr, kBase); #else // assume OS_POSIX - return wcstoll(str, endptr, kBase); + std::string ascii_string = UTF16ToASCII(string16(str)); + char* ascii_end = NULL; + value_type ret = strtoll(ascii_string.c_str(), &ascii_end, kBase); + if (ascii_string.c_str() + ascii_string.length() == ascii_end) { + *endptr = + const_cast<string_type::value_type*>(str) + ascii_string.length(); + } + return ret; #endif } static inline bool valid_func(const string_type& str) { @@ -194,14 +212,25 @@ class HexStringToLongTraits { } }; -class HexWStringToLongTraits { +class HexString16ToLongTraits { public: - typedef std::wstring string_type; + typedef string16 string_type; typedef long value_type; static const int kBase = 16; static inline value_type convert_func(const string_type::value_type* str, string_type::value_type** endptr) { +#if defined(WCHAR_T_IS_UTF16) return wcstoul(str, endptr, kBase); +#elif defined(WCHAR_T_IS_UTF32) + std::string ascii_string = UTF16ToASCII(string16(str)); + char* ascii_end = NULL; + value_type ret = strtoul(ascii_string.c_str(), &ascii_end, kBase); + if (ascii_string.c_str() + ascii_string.length() == ascii_end) { + *endptr = + const_cast<string_type::value_type*>(str) + ascii_string.length(); + } + return ret; +#endif } static inline bool valid_func(const string_type& str) { return !str.empty() && !iswspace(str[0]); @@ -221,22 +250,23 @@ class StringToDoubleTraits { } }; -class WStringToDoubleTraits { +class String16ToDoubleTraits { public: - typedef std::wstring string_type; + typedef string16 string_type; typedef double value_type; static inline value_type convert_func(const string_type::value_type* str, string_type::value_type** endptr) { - // Because dmg_fp::strtod does not like wchar_t, we convert it to ASCII. - // In theory, this should be safe, but it's possible that wide chars + // Because dmg_fp::strtod does not like char16, we convert it to ASCII. + // In theory, this should be safe, but it's possible that 16-bit chars // might get ignored by accident causing something to be parsed when it // shouldn't. - std::string ascii_string = WideToASCII(std::wstring(str)); + std::string ascii_string = UTF16ToASCII(string16(str)); char* ascii_end = NULL; value_type ret = dmg_fp::strtod(ascii_string.c_str(), &ascii_end); if (ascii_string.c_str() + ascii_string.length() == ascii_end) { // Put endptr at end of input string, so it's not recognized as an error. - *endptr = const_cast<string_type::value_type*>(str) + wcslen(str); + *endptr = + const_cast<string_type::value_type*>(str) + ascii_string.length(); } return ret; @@ -1426,18 +1456,18 @@ bool StringToInt(const std::string& input, int* output) { reinterpret_cast<long*>(output)); } -bool StringToInt(const std::wstring& input, int* output) { +bool StringToInt(const string16& input, int* output) { COMPILE_ASSERT(sizeof(int) == sizeof(long), cannot_wcstol_to_int); - return StringToNumber<WStringToLongTraits>(input, - reinterpret_cast<long*>(output)); + return StringToNumber<String16ToLongTraits>(input, + reinterpret_cast<long*>(output)); } bool StringToInt64(const std::string& input, int64* output) { return StringToNumber<StringToInt64Traits>(input, output); } -bool StringToInt64(const std::wstring& input, int64* output) { - return StringToNumber<WStringToInt64Traits>(input, output); +bool StringToInt64(const string16& input, int64* output) { + return StringToNumber<String16ToInt64Traits>(input, output); } bool HexStringToInt(const std::string& input, int* output) { @@ -1446,9 +1476,9 @@ bool HexStringToInt(const std::string& input, int* output) { reinterpret_cast<long*>(output)); } -bool HexStringToInt(const std::wstring& input, int* output) { +bool HexStringToInt(const string16& input, int* output) { COMPILE_ASSERT(sizeof(int) == sizeof(long), cannot_wcstol_to_int); - return StringToNumber<HexWStringToLongTraits>( + return StringToNumber<HexString16ToLongTraits>( input, reinterpret_cast<long*>(output)); } @@ -1490,7 +1520,7 @@ bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { return HexStringToBytesT(input, output); } -bool HexStringToBytes(const std::wstring& input, std::vector<uint8>* output) { +bool HexStringToBytes(const string16& input, std::vector<uint8>* output) { return HexStringToBytesT(input, output); } @@ -1500,7 +1530,7 @@ int StringToInt(const std::string& value) { return result; } -int StringToInt(const std::wstring& value) { +int StringToInt(const string16& value) { int result; StringToInt(value, &result); return result; @@ -1512,7 +1542,7 @@ int64 StringToInt64(const std::string& value) { return result; } -int64 StringToInt64(const std::wstring& value) { +int64 StringToInt64(const string16& value) { int64 result; StringToInt64(value, &result); return result; @@ -1524,7 +1554,7 @@ int HexStringToInt(const std::string& value) { return result; } -int HexStringToInt(const std::wstring& value) { +int HexStringToInt(const string16& value) { int result; HexStringToInt(value, &result); return result; @@ -1534,8 +1564,8 @@ 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); +bool StringToDouble(const string16& input, double* output) { + return StringToNumber<String16ToDoubleTraits>(input, output); } double StringToDouble(const std::string& value) { @@ -1544,7 +1574,7 @@ double StringToDouble(const std::string& value) { return result; } -double StringToDouble(const std::wstring& value) { +double StringToDouble(const string16& value) { double result; StringToDouble(value, &result); return result; diff --git a/base/string_util.h b/base/string_util.h index b65253a..4e04cd8 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -408,18 +408,18 @@ std::wstring DoubleToWString(double value); // |*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 StringToInt(const string16& input, int* output); bool StringToInt64(const std::string& input, int64* output); -bool StringToInt64(const std::wstring& input, int64* output); +bool StringToInt64(const string16& input, int64* output); bool HexStringToInt(const std::string& input, int* output); -bool HexStringToInt(const std::wstring& input, int* output); +bool HexStringToInt(const string16& input, int* output); // Similar to the previous functions, except that output is a vector of bytes. // |*output| will contain as many bytes as were successfully parsed prior to the // error. There is no overflow, but input.size() must be evenly divisible by 2. // Leading 0x or +/- are not allowed. bool HexStringToBytes(const std::string& input, std::vector<uint8>* output); -bool HexStringToBytes(const std::wstring& input, std::vector<uint8>* output); +bool HexStringToBytes(const string16& input, std::vector<uint8>* output); // For floating-point conversions, only conversions of input strings in decimal // form are defined to work. Behavior with strings representing floating-point @@ -428,19 +428,19 @@ bool HexStringToBytes(const std::wstring& input, std::vector<uint8>* output); // variants. This expects the input string to NOT be specific to the locale. // If your input is locale specific, use ICU to read the number. bool StringToDouble(const std::string& input, double* output); -bool StringToDouble(const std::wstring& input, double* output); +bool StringToDouble(const string16& 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. int StringToInt(const std::string& value); -int StringToInt(const std::wstring& value); +int StringToInt(const string16& value); int64 StringToInt64(const std::string& value); -int64 StringToInt64(const std::wstring& value); +int64 StringToInt64(const string16& value); int HexStringToInt(const std::string& value); -int HexStringToInt(const std::wstring& value); +int HexStringToInt(const string16& value); double StringToDouble(const std::string& value); -double StringToDouble(const std::wstring& value); +double StringToDouble(const string16& value); // Return a C++ string given printf-like input. std::string StringPrintf(const char* format, ...); diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index 9d099ab..6fc6f7f 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -828,8 +828,9 @@ TEST(StringUtilTest, StringToInt) { EXPECT_EQ(cases[i].output, output); std::wstring wide_input = ASCIIToWide(cases[i].input); - EXPECT_EQ(cases[i].output, StringToInt(wide_input)); - EXPECT_EQ(cases[i].success, StringToInt(wide_input, &output)); + EXPECT_EQ(cases[i].output, StringToInt(WideToUTF16Hack(wide_input))); + EXPECT_EQ(cases[i].success, StringToInt(WideToUTF16Hack(wide_input), + &output)); EXPECT_EQ(cases[i].output, output); } @@ -843,7 +844,7 @@ TEST(StringUtilTest, StringToInt) { EXPECT_EQ(6, output); std::wstring wide_input = ASCIIToWide(input_string); - EXPECT_FALSE(StringToInt(wide_input, &output)); + EXPECT_FALSE(StringToInt(WideToUTF16Hack(wide_input), &output)); EXPECT_EQ(6, output); } @@ -892,8 +893,9 @@ TEST(StringUtilTest, StringToInt64) { EXPECT_EQ(cases[i].output, output); std::wstring wide_input = ASCIIToWide(cases[i].input); - EXPECT_EQ(cases[i].output, StringToInt64(wide_input)); - EXPECT_EQ(cases[i].success, StringToInt64(wide_input, &output)); + EXPECT_EQ(cases[i].output, StringToInt64(WideToUTF16Hack(wide_input))); + EXPECT_EQ(cases[i].success, StringToInt64(WideToUTF16Hack(wide_input), + &output)); EXPECT_EQ(cases[i].output, output); } @@ -907,7 +909,7 @@ TEST(StringUtilTest, StringToInt64) { EXPECT_EQ(6, output); std::wstring wide_input = ASCIIToWide(input_string); - EXPECT_FALSE(StringToInt64(wide_input, &output)); + EXPECT_FALSE(StringToInt64(WideToUTF16Hack(wide_input), &output)); EXPECT_EQ(6, output); } @@ -953,8 +955,9 @@ TEST(StringUtilTest, HexStringToInt) { EXPECT_EQ(cases[i].output, output); std::wstring wide_input = ASCIIToWide(cases[i].input); - EXPECT_EQ(cases[i].output, HexStringToInt(wide_input)); - EXPECT_EQ(cases[i].success, HexStringToInt(wide_input, &output)); + EXPECT_EQ(cases[i].output, HexStringToInt(WideToUTF16Hack(wide_input))); + EXPECT_EQ(cases[i].success, HexStringToInt(WideToUTF16Hack(wide_input), + &output)); EXPECT_EQ(cases[i].output, output); } // One additional test to verify that conversion of numbers in strings with @@ -967,7 +970,7 @@ TEST(StringUtilTest, HexStringToInt) { EXPECT_EQ(0xc0ffee, output); std::wstring wide_input = ASCIIToWide(input_string); - EXPECT_FALSE(HexStringToInt(wide_input, &output)); + EXPECT_FALSE(HexStringToInt(WideToUTF16Hack(wide_input), &output)); EXPECT_EQ(0xc0ffee, output); } @@ -1013,7 +1016,8 @@ TEST(StringUtilTest, HexStringToBytes) { compare.clear(); std::wstring wide_input = ASCIIToWide(cases[i].input); - EXPECT_EQ(cases[i].success, HexStringToBytes(wide_input, &output)) << + EXPECT_EQ(cases[i].success, + HexStringToBytes(WideToUTF16Hack(wide_input), &output)) << i << ": " << cases[i].input; for (size_t j = 0; j < cases[i].output_len; ++j) compare.push_back(static_cast<uint8>(cases[i].output[j])); @@ -1065,8 +1069,10 @@ TEST(StringUtilTest, StringToDouble) { EXPECT_DOUBLE_EQ(cases[i].output, output); std::wstring wide_input = ASCIIToWide(cases[i].input); - EXPECT_DOUBLE_EQ(cases[i].output, StringToDouble(wide_input)); - EXPECT_EQ(cases[i].success, StringToDouble(wide_input, &output)); + EXPECT_DOUBLE_EQ(cases[i].output, + StringToDouble(WideToUTF16Hack(wide_input))); + EXPECT_EQ(cases[i].success, StringToDouble(WideToUTF16Hack(wide_input), + &output)); EXPECT_DOUBLE_EQ(cases[i].output, output); } @@ -1080,7 +1086,7 @@ TEST(StringUtilTest, StringToDouble) { EXPECT_DOUBLE_EQ(3.14, output); std::wstring wide_input = ASCIIToWide(input_string); - EXPECT_FALSE(StringToDouble(wide_input, &output)); + EXPECT_FALSE(StringToDouble(WideToUTF16Hack(wide_input), &output)); EXPECT_DOUBLE_EQ(3.14, output); } |