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