| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_UTF_STRING_CONVERSIONS_H_ | 5 #ifndef BASE_UTF_STRING_CONVERSIONS_H_ |
| 6 #define BASE_UTF_STRING_CONVERSIONS_H_ | 6 #define BASE_UTF_STRING_CONVERSIONS_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/string16.h" | 10 #include "base/string16.h" |
| 11 #include "base/string_piece.h" | |
| 12 | 11 |
| 13 // Like the conversions below, but also takes an offset into the source string, | 12 namespace base { |
| 14 // which will be adjusted to point at the same logical place in the result | 13 class StringPiece; |
| 15 // string. If this isn't possible because it points past the end of the source | 14 } |
| 16 // string or into the middle of a multibyte sequence, it will be set to | |
| 17 // std::wstring::npos. |offset_for_adjustment| may be NULL. | |
| 18 bool UTF8ToWideAndAdjustOffset(const char* src, | |
| 19 size_t src_len, | |
| 20 std::wstring* output, | |
| 21 size_t* offset_for_adjustment); | |
| 22 std::wstring UTF8ToWideAndAdjustOffset(const base::StringPiece& utf8, | |
| 23 size_t* offset_for_adjustment); | |
| 24 | |
| 25 bool UTF16ToWideAndAdjustOffset(const char16* src, | |
| 26 size_t src_len, | |
| 27 std::wstring* output, | |
| 28 size_t* offset_for_adjustment); | |
| 29 std::wstring UTF16ToWideAndAdjustOffset(const string16& utf16, | |
| 30 size_t* offset_for_adjustment); | |
| 31 | 15 |
| 32 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, | 16 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, |
| 33 // so avoid unnecessary conversions. The low-level versions return a boolean | 17 // so avoid unnecessary conversions. The low-level versions return a boolean |
| 34 // indicating whether the conversion was 100% valid. In this case, it will still | 18 // indicating whether the conversion was 100% valid. In this case, it will still |
| 35 // do the best it can and put the result in the output buffer. The versions that | 19 // do the best it can and put the result in the output buffer. The versions that |
| 36 // return strings ignore this error and just return the best conversion | 20 // return strings ignore this error and just return the best conversion |
| 37 // possible. | 21 // possible. |
| 38 // | 22 // |
| 39 // Note that only the structural validity is checked and non-character | 23 // Note that only the structural validity is checked and non-character |
| 40 // codepoints and unassigned are regarded as valid. | 24 // codepoints and unassigned are regarded as valid. |
| 41 // TODO(jungshik): Consider replacing an invalid input sequence with | 25 // TODO(jungshik): Consider replacing an invalid input sequence with |
| 42 // the Unicode replacement character or adding |replacement_char| parameter. | 26 // the Unicode replacement character or adding |replacement_char| parameter. |
| 43 // Currently, it's skipped in the ouput, which could be problematic in | 27 // Currently, it's skipped in the ouput, which could be problematic in |
| 44 // some situations. | 28 // some situations. |
| 45 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); | 29 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); |
| 46 std::string WideToUTF8(const std::wstring& wide); | 30 std::string WideToUTF8(const std::wstring& wide); |
| 47 inline bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { | 31 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); |
| 48 return UTF8ToWideAndAdjustOffset(src, src_len, output, NULL); | 32 std::wstring UTF8ToWide(const base::StringPiece& utf8); |
| 49 } | |
| 50 inline std::wstring UTF8ToWide(const base::StringPiece& utf8) { | |
| 51 return UTF8ToWideAndAdjustOffset(utf8, NULL); | |
| 52 } | |
| 53 | 33 |
| 54 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); | 34 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); |
| 55 string16 WideToUTF16(const std::wstring& wide); | 35 string16 WideToUTF16(const std::wstring& wide); |
| 56 inline bool UTF16ToWide(const char16* src, size_t src_len, | 36 bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); |
| 57 std::wstring* output) { | 37 std::wstring UTF16ToWide(const string16& utf16); |
| 58 return UTF16ToWideAndAdjustOffset(src, src_len, output, NULL); | |
| 59 } | |
| 60 inline std::wstring UTF16ToWide(const string16& utf16) { | |
| 61 return UTF16ToWideAndAdjustOffset(utf16, NULL); | |
| 62 } | |
| 63 | 38 |
| 64 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); | 39 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); |
| 65 string16 UTF8ToUTF16(const std::string& utf8); | 40 string16 UTF8ToUTF16(const std::string& utf8); |
| 66 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); | 41 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); |
| 67 std::string UTF16ToUTF8(const string16& utf16); | 42 std::string UTF16ToUTF8(const string16& utf16); |
| 68 | 43 |
| 69 // We are trying to get rid of wstring as much as possible, but it's too big | 44 // We are trying to get rid of wstring as much as possible, but it's too big |
| 70 // a mess to do it all at once. These conversions should be used when we | 45 // a mess to do it all at once. These conversions should be used when we |
| 71 // really should just be passing a string16 around, but we haven't finished | 46 // really should just be passing a string16 around, but we haven't finished |
| 72 // porting whatever module uses wstring and the conversion is being used as a | 47 // porting whatever module uses wstring and the conversion is being used as a |
| 73 // stopcock. This makes it easy to grep for the ones that should be removed. | 48 // stopcock. This makes it easy to grep for the ones that should be removed. |
| 74 #if defined(OS_WIN) | 49 #if defined(OS_WIN) |
| 75 # define WideToUTF16Hack | 50 # define WideToUTF16Hack |
| 76 # define UTF16ToWideHack | 51 # define UTF16ToWideHack |
| 77 #else | 52 #else |
| 78 # define WideToUTF16Hack WideToUTF16 | 53 # define WideToUTF16Hack WideToUTF16 |
| 79 # define UTF16ToWideHack UTF16ToWide | 54 # define UTF16ToWideHack UTF16ToWide |
| 80 #endif | 55 #endif |
| 81 | 56 |
| 82 #endif // BASE_UTF_STRING_CONVERSIONS_H_ | 57 #endif // BASE_UTF_STRING_CONVERSIONS_H_ |
| OLD | NEW |