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/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
6 | 6 |
7 #include "base/strings/string_piece.h" | 7 #include "base/strings/string_piece.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversion_utils.h" | 9 #include "base/strings/utf_string_conversion_utils.h" |
10 | 10 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 UTF16ToWide(utf16.data(), utf16.length(), &ret); | 118 UTF16ToWide(utf16.data(), utf16.length(), &ret); |
119 return ret; | 119 return ret; |
120 } | 120 } |
121 | 121 |
122 #endif // defined(WCHAR_T_IS_UTF32) | 122 #endif // defined(WCHAR_T_IS_UTF32) |
123 | 123 |
124 // UTF16 <-> UTF8 -------------------------------------------------------------- | 124 // UTF16 <-> UTF8 -------------------------------------------------------------- |
125 | 125 |
126 #if defined(WCHAR_T_IS_UTF32) | 126 #if defined(WCHAR_T_IS_UTF32) |
127 | 127 |
128 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 128 bool UTF8ToUTF16(const StringPiece& utf8, string16* output) { |
129 PrepareForUTF16Or32Output(src, src_len, output); | 129 PrepareForUTF16Or32Output(utf8.data(), utf8.size(), output); |
130 return ConvertUnicode(src, src_len, output); | 130 return ConvertUnicode(utf8.data(), utf8.size(), output); |
131 } | 131 } |
132 | 132 |
133 string16 UTF8ToUTF16(const StringPiece& utf8) { | 133 string16 UTF8ToUTF16(const StringPiece& utf8) { |
134 string16 ret; | 134 string16 ret; |
135 // 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 |
136 // invalid input, which is what we want here. | 136 // invalid input, which is what we want here. |
137 UTF8ToUTF16(utf8.data(), utf8.length(), &ret); | 137 UTF8ToUTF16(utf8, &ret); |
138 return ret; | 138 return ret; |
139 } | 139 } |
140 | 140 |
141 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { | 141 bool UTF16ToUTF8(const StringPiece16& utf16, std::string* output) { |
142 PrepareForUTF8Output(src, src_len, output); | 142 PrepareForUTF8Output(utf16.data(), utf16.length(), output); |
143 return ConvertUnicode(src, src_len, output); | 143 return ConvertUnicode(utf16.data(), utf16.length(), output); |
144 } | 144 } |
145 | 145 |
146 std::string UTF16ToUTF8(const string16& utf16) { | 146 std::string UTF16ToUTF8(const string16& utf16) { |
147 std::string ret; | 147 std::string ret; |
148 // 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 |
149 // invalid input, which is what we want here. | 149 // invalid input, which is what we want here. |
150 UTF16ToUTF8(utf16.data(), utf16.length(), &ret); | 150 UTF16ToUTF8(utf16, &ret); |
151 return ret; | 151 return ret; |
152 } | 152 } |
153 | 153 |
154 #elif defined(WCHAR_T_IS_UTF16) | 154 #elif defined(WCHAR_T_IS_UTF16) |
155 // 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. |
156 | 156 |
157 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 157 bool UTF8ToUTF16(const StringPiece& utf8, string16* output) { |
158 return UTF8ToWide(src, src_len, output); | 158 return UTF8ToWide(utf8.data(), utf8.size(), output); |
159 } | 159 } |
160 | 160 |
161 string16 UTF8ToUTF16(const StringPiece& utf8) { | 161 string16 UTF8ToUTF16(const StringPiece& utf8) { |
162 return UTF8ToWide(utf8); | 162 return UTF8ToWide(utf8); |
163 } | 163 } |
164 | 164 |
165 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { | 165 bool UTF16ToUTF8(const StringPiece16& utf16, std::string* output) { |
166 return WideToUTF8(src, src_len, output); | 166 return WideToUTF8(utf16.data(), utf16.size(), output); |
167 } | 167 } |
168 | 168 |
169 std::string UTF16ToUTF8(const string16& utf16) { | 169 std::string UTF16ToUTF8(const string16& utf16) { |
170 return WideToUTF8(utf16); | 170 return WideToUTF8(utf16); |
171 } | 171 } |
172 | 172 |
173 #endif | 173 #endif |
174 | 174 |
175 std::wstring ASCIIToWide(const StringPiece& ascii) { | 175 std::wstring ASCIIToWide(const StringPiece& ascii) { |
176 DCHECK(IsStringASCII(ascii)) << ascii; | 176 DCHECK(IsStringASCII(ascii)) << ascii; |
177 return std::wstring(ascii.begin(), ascii.end()); | 177 return std::wstring(ascii.begin(), ascii.end()); |
178 } | 178 } |
179 | 179 |
180 string16 ASCIIToUTF16(const StringPiece& ascii) { | 180 string16 ASCIIToUTF16(const StringPiece& ascii) { |
181 DCHECK(IsStringASCII(ascii)) << ascii; | 181 DCHECK(IsStringASCII(ascii)) << ascii; |
182 return string16(ascii.begin(), ascii.end()); | 182 return string16(ascii.begin(), ascii.end()); |
183 } | 183 } |
184 | 184 |
185 } // namespace base | 185 } // namespace base |
OLD | NEW |