Chromium Code Reviews| Index: base/string_util.h |
| =================================================================== |
| --- base/string_util.h (revision 20014) |
| +++ base/string_util.h (working copy) |
| @@ -221,7 +221,8 @@ |
| # define UTF16ToWideHack UTF16ToWide |
| #endif |
| -// Defines the error handling modes of WideToCodepage and CodepageToWide. |
| +// Defines the error handling modes of UTF16ToCodepage, CodepageToUTF16, |
| +// WideToCodepage and CodepageToWide. |
| class OnStringUtilConversionError { |
| public: |
| enum Type { |
| @@ -231,15 +232,48 @@ |
| // The offending characters are skipped and the conversion will proceed as |
| // if they did not exist. |
| SKIP, |
| + |
| + // When converting to Unicode, the offending byte sequences are substituted |
| + // by Unicode replacement character (U+FFFD). When converting from Unicode, |
| + // this is the same as SKIP. |
| + SUBSTITUTE, |
| }; |
| private: |
| OnStringUtilConversionError(); |
| }; |
| +// Converts between UTF-16 strings and the encoding specified. If the |
| +// encoding doesn't exist or the encoding fails (when on_error is FAIL), |
| +// returns false. |
| +bool UTF16ToCodepage(const string16& utf16, |
| + const char* codepage_name, |
| + OnStringUtilConversionError::Type on_error, |
| + std::string* encoded); |
| + |
| +bool CodepageToUTF16(const std::string& encoded, |
| + const char* codepage_name, |
| + OnStringUtilConversionError::Type on_error, |
| + string16* utf16); |
| + |
| // Converts between wide strings and the encoding specified. If the |
| // encoding doesn't exist or the encoding fails (when on_error is FAIL), |
| // returns false. |
| +#if defined(WCHAR_T_IS_UTF16) |
| +inline bool WideToCodepage(const std::wstring& wide, |
|
brettw
2009/07/07 19:59:48
I think I would prefer just to have these defined
|
| + const char* codepage_name, |
| + OnStringUtilConversionError::Type on_error, |
| + std::string* encoded) { |
| + return UTF16ToCodepage(wide, codepage_name, on_error, encoded); |
| +} |
| + |
| +inline bool CodepageToWide(const std::string& encoded, |
| + const char* codepage_name, |
| + OnStringUtilConversionError::Type on_error, |
| + std::wstring* wide) { |
| + return CodepageToUTF16(encoded, codepage_name, on_error, wide); |
| +} |
| +#elif defined(WCHAR_T_IS_UTF32) |
| bool WideToCodepage(const std::wstring& wide, |
| const char* codepage_name, |
| OnStringUtilConversionError::Type on_error, |
| @@ -248,6 +282,9 @@ |
| const char* codepage_name, |
| OnStringUtilConversionError::Type on_error, |
| std::wstring* wide); |
| +#else |
| +#error wchar_t should be either UTF-16 or UTF-32 |
| +#endif |
| // Converts the given wide string to the corresponding Latin1. This will fail |
| // (return false) if any characters are more than 255. |