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 #include "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 // Otherwise assume that the UTF-8 sequences will have 2 bytes for each | 214 // Otherwise assume that the UTF-8 sequences will have 2 bytes for each |
215 // character. | 215 // character. |
216 output->reserve(src_len / 2); | 216 output->reserve(src_len / 2); |
217 } | 217 } |
218 } | 218 } |
219 | 219 |
220 } // namespace | 220 } // namespace |
221 | 221 |
222 // UTF-8 <-> Wide -------------------------------------------------------------- | 222 // UTF-8 <-> Wide -------------------------------------------------------------- |
223 | 223 |
224 bool WideToUTF8AndAdjustOffset(const wchar_t* src, | 224 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) { |
225 size_t src_len, | |
226 std::string* output, | |
227 size_t* offset_for_adjustment) { | |
228 PrepareForUTF8Output(src, src_len, output); | 225 PrepareForUTF8Output(src, src_len, output); |
229 return ConvertUnicode<wchar_t, std::string>(src, src_len, output, | 226 return ConvertUnicode<wchar_t, std::string>(src, src_len, output, NULL); |
230 offset_for_adjustment); | |
231 } | 227 } |
232 | 228 |
233 std::string WideToUTF8AndAdjustOffset(const std::wstring& wide, | 229 std::string WideToUTF8(const std::wstring& wide) { |
234 size_t* offset_for_adjustment) { | |
235 std::string ret; | 230 std::string ret; |
236 // Ignore the success flag of this call, it will do the best it can for | 231 // Ignore the success flag of this call, it will do the best it can for |
237 // invalid input, which is what we want here. | 232 // invalid input, which is what we want here. |
238 WideToUTF8AndAdjustOffset(wide.data(), wide.length(), &ret, | 233 WideToUTF8(wide.data(), wide.length(), &ret); |
239 offset_for_adjustment); | |
240 return ret; | 234 return ret; |
241 } | 235 } |
242 | 236 |
243 bool UTF8ToWideAndAdjustOffset(const char* src, | 237 bool UTF8ToWideAndAdjustOffset(const char* src, |
244 size_t src_len, | 238 size_t src_len, |
245 std::wstring* output, | 239 std::wstring* output, |
246 size_t* offset_for_adjustment) { | 240 size_t* offset_for_adjustment) { |
247 PrepareForUTF16Or32Output(src, src_len, output); | 241 PrepareForUTF16Or32Output(src, src_len, output); |
248 return ConvertUnicode<char, std::wstring>(src, src_len, output, | 242 return ConvertUnicode<char, std::wstring>(src, src_len, output, |
249 offset_for_adjustment); | 243 offset_for_adjustment); |
250 } | 244 } |
251 | 245 |
252 std::wstring UTF8ToWideAndAdjustOffset(const base::StringPiece& utf8, | 246 std::wstring UTF8ToWideAndAdjustOffset(const base::StringPiece& utf8, |
253 size_t* offset_for_adjustment) { | 247 size_t* offset_for_adjustment) { |
254 std::wstring ret; | 248 std::wstring ret; |
255 UTF8ToWideAndAdjustOffset(utf8.data(), utf8.length(), &ret, | 249 UTF8ToWideAndAdjustOffset(utf8.data(), utf8.length(), &ret, |
256 offset_for_adjustment); | 250 offset_for_adjustment); |
257 return ret; | 251 return ret; |
258 } | 252 } |
259 | 253 |
260 // UTF-16 <-> Wide ------------------------------------------------------------- | 254 // UTF-16 <-> Wide ------------------------------------------------------------- |
261 | 255 |
262 #if defined(WCHAR_T_IS_UTF16) | 256 #if defined(WCHAR_T_IS_UTF16) |
263 | 257 |
264 // When wide == UTF-16, then conversions are a NOP. | 258 // When wide == UTF-16, then conversions are a NOP. |
265 bool WideToUTF16AndAdjustOffset(const wchar_t* src, | 259 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) { |
266 size_t src_len, | |
267 string16* output, | |
268 size_t* offset_for_adjustment) { | |
269 output->assign(src, src_len); | 260 output->assign(src, src_len); |
270 if (offset_for_adjustment && (*offset_for_adjustment >= src_len)) | |
271 *offset_for_adjustment = string16::npos; | |
272 return true; | 261 return true; |
273 } | 262 } |
274 | 263 |
275 string16 WideToUTF16AndAdjustOffset(const std::wstring& wide, | 264 string16 WideToUTF16(const std::wstring& wide) { |
276 size_t* offset_for_adjustment) { | |
277 if (offset_for_adjustment && (*offset_for_adjustment >= wide.length())) | |
278 *offset_for_adjustment = string16::npos; | |
279 return wide; | 265 return wide; |
280 } | 266 } |
281 | 267 |
282 bool UTF16ToWideAndAdjustOffset(const char16* src, | 268 bool UTF16ToWideAndAdjustOffset(const char16* src, |
283 size_t src_len, | 269 size_t src_len, |
284 std::wstring* output, | 270 std::wstring* output, |
285 size_t* offset_for_adjustment) { | 271 size_t* offset_for_adjustment) { |
286 output->assign(src, src_len); | 272 output->assign(src, src_len); |
287 if (offset_for_adjustment && (*offset_for_adjustment >= src_len)) | 273 if (offset_for_adjustment && (*offset_for_adjustment >= src_len)) |
288 *offset_for_adjustment = std::wstring::npos; | 274 *offset_for_adjustment = std::wstring::npos; |
289 return true; | 275 return true; |
290 } | 276 } |
291 | 277 |
292 std::wstring UTF16ToWideAndAdjustOffset(const string16& utf16, | 278 std::wstring UTF16ToWideAndAdjustOffset(const string16& utf16, |
293 size_t* offset_for_adjustment) { | 279 size_t* offset_for_adjustment) { |
294 if (offset_for_adjustment && (*offset_for_adjustment >= utf16.length())) | 280 if (offset_for_adjustment && (*offset_for_adjustment >= utf16.length())) |
295 *offset_for_adjustment = std::wstring::npos; | 281 *offset_for_adjustment = std::wstring::npos; |
296 return utf16; | 282 return utf16; |
297 } | 283 } |
298 | 284 |
299 #elif defined(WCHAR_T_IS_UTF32) | 285 #elif defined(WCHAR_T_IS_UTF32) |
300 | 286 |
301 bool WideToUTF16AndAdjustOffset(const wchar_t* src, | 287 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) { |
302 size_t src_len, | |
303 string16* output, | |
304 size_t* offset_for_adjustment) { | |
305 output->clear(); | 288 output->clear(); |
306 // Assume that normally we won't have any non-BMP characters so the counts | 289 // Assume that normally we won't have any non-BMP characters so the counts |
307 // will be the same. | 290 // will be the same. |
308 output->reserve(src_len); | 291 output->reserve(src_len); |
309 return ConvertUnicode<wchar_t, string16>(src, src_len, output, | 292 return ConvertUnicode<wchar_t, string16>(src, src_len, output, NULL); |
310 offset_for_adjustment); | |
311 } | 293 } |
312 | 294 |
313 string16 WideToUTF16AndAdjustOffset(const std::wstring& wide, | 295 string16 WideToUTF16(const std::wstring& wide) { |
314 size_t* offset_for_adjustment) { | |
315 string16 ret; | 296 string16 ret; |
316 WideToUTF16AndAdjustOffset(wide.data(), wide.length(), &ret, | 297 WideToUTF16(wide.data(), wide.length(), &ret); |
317 offset_for_adjustment); | |
318 return ret; | 298 return ret; |
319 } | 299 } |
320 | 300 |
321 bool UTF16ToWideAndAdjustOffset(const char16* src, | 301 bool UTF16ToWideAndAdjustOffset(const char16* src, |
322 size_t src_len, | 302 size_t src_len, |
323 std::wstring* output, | 303 std::wstring* output, |
324 size_t* offset_for_adjustment) { | 304 size_t* offset_for_adjustment) { |
325 output->clear(); | 305 output->clear(); |
326 // Assume that normally we won't have any non-BMP characters so the counts | 306 // Assume that normally we won't have any non-BMP characters so the counts |
327 // will be the same. | 307 // will be the same. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 | 363 |
384 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { | 364 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { |
385 return WideToUTF8(src, src_len, output); | 365 return WideToUTF8(src, src_len, output); |
386 } | 366 } |
387 | 367 |
388 std::string UTF16ToUTF8(const string16& utf16) { | 368 std::string UTF16ToUTF8(const string16& utf16) { |
389 return WideToUTF8(utf16); | 369 return WideToUTF8(utf16); |
390 } | 370 } |
391 | 371 |
392 #endif | 372 #endif |
OLD | NEW |