OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 | 6 |
7 #include "base/string_piece.h" | 7 #include "base/string_piece.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/strings/utf_string_conversion_utils.h" | 9 #include "base/strings/utf_string_conversion_utils.h" |
10 | 10 |
11 using base::PrepareForUTF8Output; | 11 namespace base { |
12 using base::PrepareForUTF16Or32Output; | |
13 using base::ReadUnicodeCharacter; | |
14 using base::WriteUnicodeCharacter; | |
15 | 12 |
16 namespace { | 13 namespace { |
17 | 14 |
18 // Generalized Unicode converter ----------------------------------------------- | 15 // Generalized Unicode converter ----------------------------------------------- |
19 | 16 |
20 // Converts the given source Unicode character type to the given destination | 17 // Converts the given source Unicode character type to the given destination |
21 // Unicode character type as a STL string. The given input buffer and size | 18 // Unicode character type as a STL string. The given input buffer and size |
22 // determine the source, and the given output STL string will be replaced by | 19 // determine the source, and the given output STL string will be replaced by |
23 // the result. | 20 // the result. |
24 template<typename SRC_CHAR, typename DEST_STRING> | 21 template<typename SRC_CHAR, typename DEST_STRING> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // invalid input, which is what we want here. | 53 // invalid input, which is what we want here. |
57 WideToUTF8(wide.data(), wide.length(), &ret); | 54 WideToUTF8(wide.data(), wide.length(), &ret); |
58 return ret; | 55 return ret; |
59 } | 56 } |
60 | 57 |
61 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { | 58 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { |
62 PrepareForUTF16Or32Output(src, src_len, output); | 59 PrepareForUTF16Or32Output(src, src_len, output); |
63 return ConvertUnicode(src, src_len, output); | 60 return ConvertUnicode(src, src_len, output); |
64 } | 61 } |
65 | 62 |
66 std::wstring UTF8ToWide(const base::StringPiece& utf8) { | 63 std::wstring UTF8ToWide(const StringPiece& utf8) { |
67 std::wstring ret; | 64 std::wstring ret; |
68 UTF8ToWide(utf8.data(), utf8.length(), &ret); | 65 UTF8ToWide(utf8.data(), utf8.length(), &ret); |
69 return ret; | 66 return ret; |
70 } | 67 } |
71 | 68 |
72 // UTF-16 <-> Wide ------------------------------------------------------------- | 69 // UTF-16 <-> Wide ------------------------------------------------------------- |
73 | 70 |
74 #if defined(WCHAR_T_IS_UTF16) | 71 #if defined(WCHAR_T_IS_UTF16) |
75 | 72 |
76 // When wide == UTF-16, then conversions are a NOP. | 73 // When wide == UTF-16, then conversions are a NOP. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 123 |
127 // UTF16 <-> UTF8 -------------------------------------------------------------- | 124 // UTF16 <-> UTF8 -------------------------------------------------------------- |
128 | 125 |
129 #if defined(WCHAR_T_IS_UTF32) | 126 #if defined(WCHAR_T_IS_UTF32) |
130 | 127 |
131 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 128 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { |
132 PrepareForUTF16Or32Output(src, src_len, output); | 129 PrepareForUTF16Or32Output(src, src_len, output); |
133 return ConvertUnicode(src, src_len, output); | 130 return ConvertUnicode(src, src_len, output); |
134 } | 131 } |
135 | 132 |
136 string16 UTF8ToUTF16(const base::StringPiece& utf8) { | 133 string16 UTF8ToUTF16(const StringPiece& utf8) { |
137 string16 ret; | 134 string16 ret; |
138 // Ignore the success flag of this call, it will do the best it can for | 135 // Ignore the success flag of this call, it will do the best it can for |
139 // invalid input, which is what we want here. | 136 // invalid input, which is what we want here. |
140 UTF8ToUTF16(utf8.data(), utf8.length(), &ret); | 137 UTF8ToUTF16(utf8.data(), utf8.length(), &ret); |
141 return ret; | 138 return ret; |
142 } | 139 } |
143 | 140 |
144 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { | 141 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { |
145 PrepareForUTF8Output(src, src_len, output); | 142 PrepareForUTF8Output(src, src_len, output); |
146 return ConvertUnicode(src, src_len, output); | 143 return ConvertUnicode(src, src_len, output); |
147 } | 144 } |
148 | 145 |
149 std::string UTF16ToUTF8(const string16& utf16) { | 146 std::string UTF16ToUTF8(const string16& utf16) { |
150 std::string ret; | 147 std::string ret; |
151 // Ignore the success flag of this call, it will do the best it can for | 148 // Ignore the success flag of this call, it will do the best it can for |
152 // invalid input, which is what we want here. | 149 // invalid input, which is what we want here. |
153 UTF16ToUTF8(utf16.data(), utf16.length(), &ret); | 150 UTF16ToUTF8(utf16.data(), utf16.length(), &ret); |
154 return ret; | 151 return ret; |
155 } | 152 } |
156 | 153 |
157 #elif defined(WCHAR_T_IS_UTF16) | 154 #elif defined(WCHAR_T_IS_UTF16) |
158 // Easy case since we can use the "wide" versions we already wrote above. | 155 // Easy case since we can use the "wide" versions we already wrote above. |
159 | 156 |
160 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 157 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { |
161 return UTF8ToWide(src, src_len, output); | 158 return UTF8ToWide(src, src_len, output); |
162 } | 159 } |
163 | 160 |
164 string16 UTF8ToUTF16(const base::StringPiece& utf8) { | 161 string16 UTF8ToUTF16(const StringPiece& utf8) { |
165 return UTF8ToWide(utf8); | 162 return UTF8ToWide(utf8); |
166 } | 163 } |
167 | 164 |
168 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { | 165 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { |
169 return WideToUTF8(src, src_len, output); | 166 return WideToUTF8(src, src_len, output); |
170 } | 167 } |
171 | 168 |
172 std::string UTF16ToUTF8(const string16& utf16) { | 169 std::string UTF16ToUTF8(const string16& utf16) { |
173 return WideToUTF8(utf16); | 170 return WideToUTF8(utf16); |
174 } | 171 } |
175 | 172 |
176 #endif | 173 #endif |
177 | 174 |
178 std::wstring ASCIIToWide(const base::StringPiece& ascii) { | 175 std::wstring ASCIIToWide(const StringPiece& ascii) { |
179 DCHECK(IsStringASCII(ascii)) << ascii; | 176 DCHECK(IsStringASCII(ascii)) << ascii; |
180 return std::wstring(ascii.begin(), ascii.end()); | 177 return std::wstring(ascii.begin(), ascii.end()); |
181 } | 178 } |
182 | 179 |
183 string16 ASCIIToUTF16(const base::StringPiece& ascii) { | 180 string16 ASCIIToUTF16(const StringPiece& ascii) { |
184 DCHECK(IsStringASCII(ascii)) << ascii; | 181 DCHECK(IsStringASCII(ascii)) << ascii; |
185 return string16(ascii.begin(), ascii.end()); | 182 return string16(ascii.begin(), ascii.end()); |
186 } | 183 } |
| 184 |
| 185 } // namespace base |
OLD | NEW |