blob: 3101a60288837ab193584fb85fd8393cabea8af6 [file] [log] [blame]
[email protected]b9f93832009-11-13 19:27:481// 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
3// found in the LICENSE file.
4
[email protected]a3f721892013-02-07 03:59:065#include "base/strings/utf_string_conversion_utils.h"
[email protected]b9f93832009-11-13 19:27:486
7#include "base/third_party/icu/icu_utf.h"
8
9namespace base {
10
11// ReadUnicodeCharacter --------------------------------------------------------
12
13bool ReadUnicodeCharacter(const char* src,
avi84f37e12015-12-25 09:31:4214 int32_t src_len,
15 int32_t* char_index,
16 uint32_t* code_point_out) {
[email protected]b9f93832009-11-13 19:27:4817 // U8_NEXT expects to be able to use -1 to signal an error, so we must
18 // use a signed type for code_point. But this function returns false
19 // on error anyway, so code_point_out is unsigned.
avi84f37e12015-12-25 09:31:4220 int32_t code_point;
[email protected]b9f93832009-11-13 19:27:4821 CBU8_NEXT(src, *char_index, src_len, code_point);
avi84f37e12015-12-25 09:31:4222 *code_point_out = static_cast<uint32_t>(code_point);
[email protected]b9f93832009-11-13 19:27:4823
24 // The ICU macro above moves to the next char, we want to point to the last
25 // char consumed.
26 (*char_index)--;
27
28 // Validate the decoded value.
29 return IsValidCodepoint(code_point);
30}
31
32bool ReadUnicodeCharacter(const char16* src,
avi84f37e12015-12-25 09:31:4233 int32_t src_len,
34 int32_t* char_index,
35 uint32_t* code_point) {
[email protected]b9f93832009-11-13 19:27:4836 if (CBU16_IS_SURROGATE(src[*char_index])) {
37 if (!CBU16_IS_SURROGATE_LEAD(src[*char_index]) ||
38 *char_index + 1 >= src_len ||
39 !CBU16_IS_TRAIL(src[*char_index + 1])) {
40 // Invalid surrogate pair.
41 return false;
42 }
43
44 // Valid surrogate pair.
45 *code_point = CBU16_GET_SUPPLEMENTARY(src[*char_index],
46 src[*char_index + 1]);
47 (*char_index)++;
48 } else {
49 // Not a surrogate, just one 16-bit word.
50 *code_point = src[*char_index];
51 }
52
53 return IsValidCodepoint(*code_point);
54}
55
56#if defined(WCHAR_T_IS_UTF32)
57bool ReadUnicodeCharacter(const wchar_t* src,
avi84f37e12015-12-25 09:31:4258 int32_t src_len,
59 int32_t* char_index,
60 uint32_t* code_point) {
[email protected]b9f93832009-11-13 19:27:4861 // Conversion is easy since the source is 32-bit.
62 *code_point = src[*char_index];
63
64 // Validate the value.
65 return IsValidCodepoint(*code_point);
66}
67#endif // defined(WCHAR_T_IS_UTF32)
68
69// WriteUnicodeCharacter -------------------------------------------------------
70
avi84f37e12015-12-25 09:31:4271size_t WriteUnicodeCharacter(uint32_t code_point, std::string* output) {
[email protected]b9f93832009-11-13 19:27:4872 if (code_point <= 0x7f) {
73 // Fast path the common case of one byte.
pkasting9cf9b94a2014-10-01 22:18:4374 output->push_back(static_cast<char>(code_point));
[email protected]b9f93832009-11-13 19:27:4875 return 1;
76 }
77
78
79 // CBU8_APPEND_UNSAFE can append up to 4 bytes.
80 size_t char_offset = output->length();
81 size_t original_char_offset = char_offset;
82 output->resize(char_offset + CBU8_MAX_LENGTH);
83
84 CBU8_APPEND_UNSAFE(&(*output)[0], char_offset, code_point);
85
86 // CBU8_APPEND_UNSAFE will advance our pointer past the inserted character, so
87 // it will represent the new length of the string.
88 output->resize(char_offset);
89 return char_offset - original_char_offset;
90}
91
avi84f37e12015-12-25 09:31:4292size_t WriteUnicodeCharacter(uint32_t code_point, string16* output) {
[email protected]b9f93832009-11-13 19:27:4893 if (CBU16_LENGTH(code_point) == 1) {
94 // Thie code point is in the Basic Multilingual Plane (BMP).
95 output->push_back(static_cast<char16>(code_point));
96 return 1;
97 }
98 // Non-BMP characters use a double-character encoding.
99 size_t char_offset = output->length();
100 output->resize(char_offset + CBU16_MAX_LENGTH);
101 CBU16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point);
102 return CBU16_MAX_LENGTH;
103}
104
105// Generalized Unicode converter -----------------------------------------------
106
107template<typename CHAR>
108void PrepareForUTF8Output(const CHAR* src,
109 size_t src_len,
110 std::string* output) {
111 output->clear();
112 if (src_len == 0)
113 return;
114 if (src[0] < 0x80) {
115 // Assume that the entire input will be ASCII.
116 output->reserve(src_len);
117 } else {
118 // Assume that the entire input is non-ASCII and will have 3 bytes per char.
119 output->reserve(src_len * 3);
120 }
121}
122
123// Instantiate versions we know callers will need.
124template void PrepareForUTF8Output(const wchar_t*, size_t, std::string*);
125template void PrepareForUTF8Output(const char16*, size_t, std::string*);
126
127template<typename STRING>
128void PrepareForUTF16Or32Output(const char* src,
129 size_t src_len,
130 STRING* output) {
131 output->clear();
132 if (src_len == 0)
133 return;
134 if (static_cast<unsigned char>(src[0]) < 0x80) {
135 // Assume the input is all ASCII, which means 1:1 correspondence.
136 output->reserve(src_len);
137 } else {
138 // Otherwise assume that the UTF-8 sequences will have 2 bytes for each
139 // character.
140 output->reserve(src_len / 2);
141 }
142}
143
144// Instantiate versions we know callers will need.
145template void PrepareForUTF16Or32Output(const char*, size_t, std::wstring*);
146template void PrepareForUTF16Or32Output(const char*, size_t, string16*);
147
148} // namespace base