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