Index: base/string_util.cc |
=================================================================== |
--- base/string_util.cc (revision 10641) |
+++ base/string_util.cc (working copy) |
@@ -127,14 +127,25 @@ |
} |
}; |
-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 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 @@ |
#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 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 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 @@ |
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 @@ |
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 @@ |
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 @@ |
return result; |
} |
-int StringToInt(const std::wstring& value) { |
+int StringToInt(const string16& value) { |
int result; |
StringToInt(value, &result); |
return result; |
@@ -1512,7 +1542,7 @@ |
return result; |
} |
-int64 StringToInt64(const std::wstring& value) { |
+int64 StringToInt64(const string16& value) { |
int64 result; |
StringToInt64(value, &result); |
return result; |
@@ -1524,7 +1554,7 @@ |
return result; |
} |
-int HexStringToInt(const std::wstring& value) { |
+int HexStringToInt(const string16& value) { |
int result; |
HexStringToInt(value, &result); |
return result; |
@@ -1534,8 +1564,8 @@ |
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 @@ |
return result; |
} |
-double StringToDouble(const std::wstring& value) { |
+double StringToDouble(const string16& value) { |
double result; |
StringToDouble(value, &result); |
return result; |