blob: 6ad6ca25e7fcd9a364443e1b188f8f9a1d517fbb [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]6b27db802008-08-07 15:29:494
initial.commitd7cae122008-07-26 21:49:385#include "base/string_util.h"
6
7#include <string.h>
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/logging.h"
12#include "base/singleton.h"
13#include "unicode/ucnv.h"
14#include "unicode/numfmt.h"
15#include "unicode/ustring.h"
16
[email protected]6b27db802008-08-07 15:29:4917namespace {
18
19// ReadUnicodeCharacter --------------------------------------------------------
20
21// Reads a UTF-8 stream, placing the next code point into the given output
22// |*code_point|. |src| represents the entire string to read, and |*char_index|
23// is the character offset within the string to start reading at. |*char_index|
24// will be updated to index the last character read, such that incrementing it
25// (as in a for loop) will take the reader to the next character.
26//
27// Returns true on success. On false, |*code_point| will be invalid.
28bool ReadUnicodeCharacter(const char* src, int32 src_len,
[email protected]d6b06672008-08-19 00:31:2429 int32* char_index, uint32* code_point_out) {
30 // U8_NEXT expects to be able to use -1 to signal an error, so we must
31 // use a signed type for code_point. But this function returns false
32 // on error anyway, so code_point_out is unsigned.
33 int32 code_point;
34 U8_NEXT(src, *char_index, src_len, code_point);
35 *code_point_out = static_cast<uint32>(code_point);
[email protected]6b27db802008-08-07 15:29:4936
37 // The ICU macro above moves to the next char, we want to point to the last
38 // char consumed.
39 (*char_index)--;
40
41 // Validate the decoded value.
[email protected]d6b06672008-08-19 00:31:2442 return U_IS_UNICODE_CHAR(code_point);
[email protected]6b27db802008-08-07 15:29:4943}
44
[email protected]39be4242008-08-07 18:31:4045// Reads a UTF-16 character. The usage is the same as the 8-bit version above.
[email protected]e6da5e1f2008-08-07 20:27:5746bool ReadUnicodeCharacter(const char16* src, int32 src_len,
[email protected]6b27db802008-08-07 15:29:4947 int32* char_index, uint32* code_point) {
48 if (U16_IS_SURROGATE(src[*char_index])) {
49 if (!U16_IS_SURROGATE_LEAD(src[*char_index]) ||
50 *char_index + 1 >= src_len ||
51 !U16_IS_TRAIL(src[*char_index + 1])) {
52 // Invalid surrogate pair.
53 return false;
54 }
55
56 // Valid surrogate pair.
57 *code_point = U16_GET_SUPPLEMENTARY(src[*char_index],
58 src[*char_index + 1]);
59 (*char_index)++;
60 } else {
61 // Not a surrogate, just one 16-bit word.
62 *code_point = src[*char_index];
63 }
64
65 return U_IS_UNICODE_CHAR(*code_point);
66}
[email protected]e6da5e1f2008-08-07 20:27:5767
68#if defined(WCHAR_T_IS_UTF32)
[email protected]39be4242008-08-07 18:31:4069// Reads UTF-32 character. The usage is the same as the 8-bit version above.
[email protected]a31e79e2008-08-07 22:36:0170bool ReadUnicodeCharacter(const wchar_t* src, int32 src_len,
[email protected]e6da5e1f2008-08-07 20:27:5771 int32* char_index, uint32* code_point) {
[email protected]6b27db802008-08-07 15:29:4972 // Conversion is easy since the source is 32-bit.
73 *code_point = src[*char_index];
74
75 // Validate the value.
76 return U_IS_UNICODE_CHAR(*code_point);
77}
[email protected]39be4242008-08-07 18:31:4078#endif // defined(WCHAR_T_IS_UTF32)
[email protected]6b27db802008-08-07 15:29:4979
80// WriteUnicodeCharacter -------------------------------------------------------
81
82// Appends a UTF-8 character to the given 8-bit string.
83void WriteUnicodeCharacter(uint32 code_point, std::basic_string<char>* output) {
84 if (code_point <= 0x7f) {
85 // Fast path the common case of one byte.
86 output->push_back(code_point);
87 return;
88 }
89
90 // U8_APPEND_UNSAFE can append up to 4 bytes.
91 int32 char_offset = static_cast<int32>(output->length());
92 output->resize(char_offset + U8_MAX_LENGTH);
93
94 U8_APPEND_UNSAFE(&(*output)[0], char_offset, code_point);
95
96 // U8_APPEND_UNSAFE will advance our pointer past the inserted character, so
97 // it will represent the new length of the string.
98 output->resize(char_offset);
99}
100
[email protected]39be4242008-08-07 18:31:40101// Appends the given code point as a UTF-16 character to the STL string.
[email protected]6b27db802008-08-07 15:29:49102void WriteUnicodeCharacter(uint32 code_point,
[email protected]e6da5e1f2008-08-07 20:27:57103 std::basic_string<char16>* output) {
[email protected]6b27db802008-08-07 15:29:49104 if (U16_LENGTH(code_point) == 1) {
105 // Thie code point is in the Basic Multilingual Plane (BMP).
[email protected]e6da5e1f2008-08-07 20:27:57106 output->push_back(static_cast<char16>(code_point));
[email protected]6b27db802008-08-07 15:29:49107 } else {
108 // Non-BMP characters use a double-character encoding.
109 int32 char_offset = static_cast<int32>(output->length());
110 output->resize(char_offset + U16_MAX_LENGTH);
111 U16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point);
112 }
113}
[email protected]e6da5e1f2008-08-07 20:27:57114
115#if defined(WCHAR_T_IS_UTF32)
[email protected]39be4242008-08-07 18:31:40116// Appends the given UTF-32 character to the given 32-bit string.
[email protected]6b27db802008-08-07 15:29:49117inline void WriteUnicodeCharacter(uint32 code_point,
118 std::basic_string<wchar_t>* output) {
119 // This is the easy case, just append the character.
120 output->push_back(code_point);
121}
[email protected]39be4242008-08-07 18:31:40122#endif // defined(WCHAR_T_IS_UTF32)
[email protected]6b27db802008-08-07 15:29:49123
124// Generalized Unicode converter -----------------------------------------------
125
126// Converts the given source Unicode character type to the given destination
127// Unicode character type as a STL string. The given input buffer and size
128// determine the source, and the given output STL string will be replaced by
129// the result.
130template<typename SRC_CHAR, typename DEST_CHAR>
131bool ConvertUnicode(const SRC_CHAR* src, size_t src_len,
132 std::basic_string<DEST_CHAR>* output) {
133 output->clear();
134
135 // ICU requires 32-bit numbers.
136 bool success = true;
137 int32 src_len32 = static_cast<int32>(src_len);
138 for (int32 i = 0; i < src_len32; i++) {
139 uint32 code_point;
140 if (ReadUnicodeCharacter(src, src_len32, &i, &code_point))
141 WriteUnicodeCharacter(code_point, output);
142 else
143 success = false;
144 }
145 return success;
146}
147
[email protected]f0fcfd32008-08-26 19:27:24148
149// Guesses the length of the output in UTF-8 in bytes, and reserves that amount
150// of space in the given string. We also assume that the input character types
151// are unsigned, which will be true for UTF-16 and -32 on our systems. We assume
152// the string length is greater than zero.
153template<typename CHAR>
154void ReserveUTF8Output(const CHAR* src, size_t src_len, std::string* output) {
155 if (src[0] < 0x80) {
156 // Assume that the entire input will be ASCII.
157 output->reserve(src_len);
158 } else {
159 // Assume that the entire input is non-ASCII and will have 3 bytes per char.
160 output->reserve(src_len * 3);
161 }
162}
163
164// Guesses the size of the output buffer (containing either UTF-16 or -32 data)
165// given some UTF-8 input that will be converted to it. See ReserveUTF8Output.
166// We assume the source length is > 0.
167template<typename CHAR>
168void ReserveUTF16Or32Output(const char* src, size_t src_len,
169 std::basic_string<CHAR>* output) {
170 if (static_cast<unsigned char>(src[0]) < 0x80) {
171 // Assume the input is all ASCII, which means 1:1 correspondence.
172 output->reserve(src_len);
173 } else {
174 // Otherwise assume that the UTF-8 sequences will have 2 bytes for each
175 // character.
176 output->reserve(src_len / 2);
177 }
178}
179
[email protected]6b27db802008-08-07 15:29:49180} // namespace
181
[email protected]e6da5e1f2008-08-07 20:27:57182// UTF-8 <-> Wide --------------------------------------------------------------
[email protected]6b27db802008-08-07 15:29:49183
184std::string WideToUTF8(const std::wstring& wide) {
185 std::string ret;
186 if (wide.empty())
187 return ret;
188
189 // Ignore the success flag of this call, it will do the best it can for
190 // invalid input, which is what we want here.
191 WideToUTF8(wide.data(), wide.length(), &ret);
192 return ret;
193}
194
195bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) {
196 if (src_len == 0) {
197 output->clear();
198 return true;
199 }
200
[email protected]f0fcfd32008-08-26 19:27:24201 ReserveUTF8Output(src, src_len, output);
[email protected]6b27db802008-08-07 15:29:49202 return ConvertUnicode<wchar_t, char>(src, src_len, output);
203}
204
205std::wstring UTF8ToWide(const std::string& utf8) {
206 std::wstring ret;
207 if (utf8.empty())
208 return ret;
209
210 UTF8ToWide(utf8.data(), utf8.length(), &ret);
211 return ret;
212}
213
214bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) {
215 if (src_len == 0) {
216 output->clear();
217 return true;
218 }
219
[email protected]f0fcfd32008-08-26 19:27:24220 ReserveUTF16Or32Output(src, src_len, output);
[email protected]6b27db802008-08-07 15:29:49221 return ConvertUnicode<char, wchar_t>(src, src_len, output);
222}
223
[email protected]e6da5e1f2008-08-07 20:27:57224// UTF-16 <-> Wide -------------------------------------------------------------
225
226#if defined(WCHAR_T_IS_UTF16)
227
228// When wide == UTF-16, then conversions are a NOP.
229std::string16 WideToUTF16(const std::wstring& wide) {
230 return wide;
231}
232
233bool WideToUTF16(const wchar_t* src, size_t src_len, std::string16* output) {
234 output->assign(src, src_len);
235 return true;
236}
237
238std::wstring UTF16ToWide(const std::string16& utf16) {
239 return utf16;
240}
241
242bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output) {
243 output->assign(src, src_len);
244 return true;
245}
246
247#elif defined(WCHAR_T_IS_UTF32)
248
249std::string16 WideToUTF16(const std::wstring& wide) {
250 std::string16 ret;
251 if (wide.empty())
252 return ret;
253
[email protected]a31e79e2008-08-07 22:36:01254 WideToUTF16(wide.data(), wide.length(), &ret);
[email protected]e6da5e1f2008-08-07 20:27:57255 return ret;
256}
257
258bool WideToUTF16(const wchar_t* src, size_t src_len, std::string16* output) {
259 if (src_len == 0) {
260 output->clear();
261 return true;
262 }
263
264 // Assume that normally we won't have any non-BMP characters so the counts
265 // will be the same.
266 output->reserve(src_len);
267 return ConvertUnicode<wchar_t, char16>(src, src_len, output);
268}
269
270std::wstring UTF16ToWide(const std::string16& utf16) {
271 std::wstring ret;
272 if (utf16.empty())
273 return ret;
274
[email protected]a31e79e2008-08-07 22:36:01275 UTF16ToWide(utf16.data(), utf16.length(), &ret);
[email protected]e6da5e1f2008-08-07 20:27:57276 return ret;
277}
278
279bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output) {
280 if (src_len == 0) {
281 output->clear();
282 return true;
283 }
284
285 // Assume that normally we won't have any non-BMP characters so the counts
286 // will be the same.
287 output->reserve(src_len);
288 return ConvertUnicode<char16, wchar_t>(src, src_len, output);
289}
290
291#endif // defined(WCHAR_T_IS_UTF32)
292
[email protected]f0fcfd32008-08-26 19:27:24293// UTF16 <-> UTF8 --------------------------------------------------------------
294
295#if defined(WCHAR_T_IS_UTF32)
296
297bool UTF8ToUTF16(const char* src, size_t src_len, std::string16* output) {
298 if (src_len == 0) {
299 output->clear();
300 return true;
301 }
302
303 ReserveUTF16Or32Output(src, src_len, output);
304 return ConvertUnicode<char, char16>(src, src_len, output);
305}
306
307std::string16 UTF8ToUTF16(const std::string& utf8) {
308 std::string16 ret;
309 if (utf8.empty())
310 return ret;
311
312 // Ignore the success flag of this call, it will do the best it can for
313 // invalid input, which is what we want here.
314 UTF8ToUTF16(utf8.data(), utf8.length(), &ret);
315 return ret;
316}
317
318bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
319 if (src_len == 0) {
320 output->clear();
321 return true;
322 }
323
324 ReserveUTF8Output(src, src_len, output);
325 return ConvertUnicode<char, char16>(src, src_len, output);
326}
327
328std::string UTF16ToUTF8(const std::string16& utf16) {
329 std::string ret;
330 if (utf16.empty())
331 return ret;
332
333 // Ignore the success flag of this call, it will do the best it can for
334 // invalid input, which is what we want here.
335 UTF16ToUTF8(utf16.data(), utf16.length(), &ret);
336 return ret;
337}
338
339#elif defined(WCHAR_T_IS_UTF16)
340// Easy case since we can use the "wide" versions we already wrote above.
341
342bool UTF8ToUTF16(const char* src, size_t src_len, std::string16* output) {
343 return UTF8ToWide(src, src_len, output);
344}
345
346std::string16 UTF8ToUTF16(const std::string& utf8) {
347 return UTF8ToWide(utf8);
348}
349
350bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
351 return WideToUTF8(src, src_len, output);
352}
353
354std::string UTF16ToUTF8(const std::string16& utf16) {
355 return WideToUTF8(utf16);
356}
357
358#endif
359
initial.commitd7cae122008-07-26 21:49:38360// Codepage <-> Wide -----------------------------------------------------------
361
362// Convert a unicode string into the specified codepage_name. If the codepage
363// isn't found, return false.
364bool WideToCodepage(const std::wstring& wide,
365 const char* codepage_name,
366 OnStringUtilConversionError::Type on_error,
367 std::string* encoded) {
368 encoded->clear();
369
370 UErrorCode status = U_ZERO_ERROR;
371 UConverter* converter = ucnv_open(codepage_name, &status);
372 if (!U_SUCCESS(status))
373 return false;
374
375 const UChar* uchar_src;
376 int uchar_len;
[email protected]39be4242008-08-07 18:31:40377#if defined(WCHAR_T_IS_UTF16)
initial.commitd7cae122008-07-26 21:49:38378 uchar_src = wide.c_str();
379 uchar_len = static_cast<int>(wide.length());
[email protected]39be4242008-08-07 18:31:40380#elif defined(WCHAR_T_IS_UTF32)
initial.commitd7cae122008-07-26 21:49:38381 // When wchar_t is wider than UChar (16 bits), transform |wide| into a
382 // UChar* string. Size the UChar* buffer to be large enough to hold twice
[email protected]703f427e2008-08-13 01:17:18383 // as many UTF-16 code points as there are UTF-16 characters, in case each
initial.commitd7cae122008-07-26 21:49:38384 // character translates to a UTF-16 surrogate pair, and leave room for a NUL
385 // terminator.
386 std::vector<UChar> wide_uchar(wide.length() * 2 + 1);
387 u_strFromWCS(&wide_uchar[0], wide_uchar.size(), &uchar_len,
388 wide.c_str(), wide.length(), &status);
389 uchar_src = &wide_uchar[0];
390 DCHECK(U_SUCCESS(status)) << "failed to convert wstring to UChar*";
[email protected]39be4242008-08-07 18:31:40391#endif // defined(WCHAR_T_IS_UTF32)
initial.commitd7cae122008-07-26 21:49:38392
393 int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(uchar_len,
394 ucnv_getMaxCharSize(converter));
395 encoded->resize(encoded_max_length);
396
397 // Setup our error handler.
398 switch (on_error) {
399 case OnStringUtilConversionError::FAIL:
400 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_STOP, 0,
401 NULL, NULL, &status);
402 break;
403 case OnStringUtilConversionError::SKIP:
404 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SKIP, 0,
405 NULL, NULL, &status);
406 break;
407 default:
408 NOTREACHED();
409 }
410
411 // ucnv_fromUChars returns size not including terminating null
412 int actual_size = ucnv_fromUChars(converter, &(*encoded)[0],
413 encoded_max_length, uchar_src, uchar_len, &status);
414 encoded->resize(actual_size);
415 ucnv_close(converter);
416 if (U_SUCCESS(status))
417 return true;
418 encoded->clear(); // Make sure the output is empty on error.
419 return false;
420}
421
422// Converts a string of the given codepage into unicode.
423// If the codepage isn't found, return false.
424bool CodepageToWide(const std::string& encoded,
425 const char* codepage_name,
426 OnStringUtilConversionError::Type on_error,
427 std::wstring* wide) {
428 wide->clear();
429
430 UErrorCode status = U_ZERO_ERROR;
431 UConverter* converter = ucnv_open(codepage_name, &status);
432 if (!U_SUCCESS(status))
433 return false;
434
435 // The worst case is all the input characters are non-BMP (32-bit) ones.
436 size_t uchar_max_length = encoded.length() * 2 + 1;
437
438 UChar* uchar_dst;
[email protected]39be4242008-08-07 18:31:40439#if defined(WCHAR_T_IS_UTF16)
initial.commitd7cae122008-07-26 21:49:38440 uchar_dst = WriteInto(wide, uchar_max_length);
[email protected]39be4242008-08-07 18:31:40441#elif defined(WCHAR_T_IS_UTF32)
initial.commitd7cae122008-07-26 21:49:38442 // When wchar_t is wider than UChar (16 bits), convert into a temporary
443 // UChar* buffer.
444 std::vector<UChar> wide_uchar(uchar_max_length);
445 uchar_dst = &wide_uchar[0];
[email protected]39be4242008-08-07 18:31:40446#endif // defined(WCHAR_T_IS_UTF32)
initial.commitd7cae122008-07-26 21:49:38447
448 // Setup our error handler.
449 switch (on_error) {
450 case OnStringUtilConversionError::FAIL:
451 ucnv_setToUCallBack(converter, UCNV_TO_U_CALLBACK_STOP, 0,
452 NULL, NULL, &status);
453 break;
454 case OnStringUtilConversionError::SKIP:
455 ucnv_setToUCallBack(converter, UCNV_TO_U_CALLBACK_SKIP, 0,
456 NULL, NULL, &status);
457 break;
458 default:
459 NOTREACHED();
460 }
461
462 int actual_size = ucnv_toUChars(converter,
463 uchar_dst,
464 static_cast<int>(uchar_max_length),
465 encoded.data(),
466 static_cast<int>(encoded.length()),
467 &status);
468 ucnv_close(converter);
469 if (!U_SUCCESS(status)) {
470 wide->clear(); // Make sure the output is empty on error.
471 return false;
472 }
473
[email protected]39be4242008-08-07 18:31:40474#ifdef WCHAR_T_IS_UTF32
initial.commitd7cae122008-07-26 21:49:38475 // When wchar_t is wider than UChar (16 bits), it's not possible to wind up
476 // with any more wchar_t elements than UChar elements. ucnv_toUChars
477 // returns the number of UChar elements not including the NUL terminator, so
478 // leave extra room for that.
479 u_strToWCS(WriteInto(wide, actual_size + 1), actual_size + 1, &actual_size,
480 uchar_dst, actual_size, &status);
481 DCHECK(U_SUCCESS(status)) << "failed to convert UChar* to wstring";
[email protected]39be4242008-08-07 18:31:40482#endif // WCHAR_T_IS_UTF32
initial.commitd7cae122008-07-26 21:49:38483
484 wide->resize(actual_size);
485 return true;
486}
487
488// Number formatting -----------------------------------------------------------
489
[email protected]89886992008-08-13 15:32:27490namespace {
491
492struct NumberFormatSingletonTraits
493 : public DefaultSingletonTraits<NumberFormat> {
494 static NumberFormat* New() {
495 UErrorCode status = U_ZERO_ERROR;
496 NumberFormat* formatter = NumberFormat::createInstance(status);
497 DCHECK(U_SUCCESS(status));
498 return formatter;
499 }
500 // There's no ICU call to destroy a NumberFormat object other than
501 // operator delete, so use the default Delete, which calls operator delete.
502 // This can cause problems if a different allocator is used by this file than
503 // by ICU.
504};
505
506} // namespace
initial.commitd7cae122008-07-26 21:49:38507
508std::wstring FormatNumber(int64 number) {
[email protected]89886992008-08-13 15:32:27509 NumberFormat* number_format =
510 Singleton<NumberFormat, NumberFormatSingletonTraits>::get();
511
initial.commitd7cae122008-07-26 21:49:38512 if (!number_format) {
513 // As a fallback, just return the raw number in a string.
514 return StringPrintf(L"%lld", number);
515 }
516 UnicodeString ustr;
517 number_format->format(number, ustr);
518
[email protected]39be4242008-08-07 18:31:40519#if defined(WCHAR_T_IS_UTF16)
initial.commitd7cae122008-07-26 21:49:38520 return std::wstring(ustr.getBuffer(),
521 static_cast<std::wstring::size_type>(ustr.length()));
[email protected]39be4242008-08-07 18:31:40522#elif defined(WCHAR_T_IS_UTF32)
initial.commitd7cae122008-07-26 21:49:38523 wchar_t buffer[64]; // A int64 is less than 20 chars long, so 64 chars
524 // leaves plenty of room for formating stuff.
525 int length = 0;
526 UErrorCode error = U_ZERO_ERROR;
527 u_strToWCS(buffer, 64, &length, ustr.getBuffer(), ustr.length() , &error);
528 if (U_FAILURE(error)) {
529 NOTREACHED();
530 // As a fallback, just return the raw number in a string.
531 return StringPrintf(L"%lld", number);
532 }
533 return std::wstring(buffer, static_cast<std::wstring::size_type>(length));
[email protected]39be4242008-08-07 18:31:40534#endif // defined(WCHAR_T_IS_UTF32)
initial.commitd7cae122008-07-26 21:49:38535}
license.botbf09a502008-08-24 00:55:55536