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