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 "util/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_t * src,int32_t src_len,int32_t * char_index,uint32_t * code_point)33 bool ReadUnicodeCharacter(const char16_t* 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 || !CBU16_IS_TRAIL(src[*char_index + 1])) {
40 // Invalid surrogate pair.
41 return false;
42 }
43
44 // Valid surrogate pair.
45 *code_point =
46 CBU16_GET_SUPPLEMENTARY(src[*char_index], 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 // WriteUnicodeCharacter -------------------------------------------------------
57
WriteUnicodeCharacter(uint32_t code_point,std::string * output)58 size_t WriteUnicodeCharacter(uint32_t code_point, std::string* output) {
59 if (code_point <= 0x7f) {
60 // Fast path the common case of one byte.
61 output->push_back(static_cast<char>(code_point));
62 return 1;
63 }
64
65 // CBU8_APPEND_UNSAFE can append up to 4 bytes.
66 size_t char_offset = output->length();
67 size_t original_char_offset = char_offset;
68 output->resize(char_offset + CBU8_MAX_LENGTH);
69
70 CBU8_APPEND_UNSAFE(&(*output)[0], char_offset, code_point);
71
72 // CBU8_APPEND_UNSAFE will advance our pointer past the inserted character, so
73 // it will represent the new length of the string.
74 output->resize(char_offset);
75 return char_offset - original_char_offset;
76 }
77
WriteUnicodeCharacter(uint32_t code_point,std::u16string * output)78 size_t WriteUnicodeCharacter(uint32_t code_point, std::u16string* output) {
79 if (CBU16_LENGTH(code_point) == 1) {
80 // Thie code point is in the Basic Multilingual Plane (BMP).
81 output->push_back(static_cast<char16_t>(code_point));
82 return 1;
83 }
84 // Non-BMP characters use a double-character encoding.
85 size_t char_offset = output->length();
86 output->resize(char_offset + CBU16_MAX_LENGTH);
87 CBU16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point);
88 return CBU16_MAX_LENGTH;
89 }
90
91 // Generalized Unicode converter -----------------------------------------------
92
93 template <typename CHAR>
PrepareForUTF8Output(const CHAR * src,size_t src_len,std::string * output)94 void PrepareForUTF8Output(const CHAR* src,
95 size_t src_len,
96 std::string* output) {
97 output->clear();
98 if (src_len == 0)
99 return;
100 if (src[0] < 0x80) {
101 // Assume that the entire input will be ASCII.
102 output->reserve(src_len);
103 } else {
104 // Assume that the entire input is non-ASCII and will have 3 bytes per char.
105 output->reserve(src_len * 3);
106 }
107 }
108
109 // Instantiate versions we know callers will need.
110 template void PrepareForUTF8Output(const char16_t*, size_t, std::string*);
111
112 template <typename STRING>
PrepareForUTF16Or32Output(const char * src,size_t src_len,STRING * output)113 void PrepareForUTF16Or32Output(const char* src,
114 size_t src_len,
115 STRING* output) {
116 output->clear();
117 if (src_len == 0)
118 return;
119 if (static_cast<unsigned char>(src[0]) < 0x80) {
120 // Assume the input is all ASCII, which means 1:1 correspondence.
121 output->reserve(src_len);
122 } else {
123 // Otherwise assume that the UTF-8 sequences will have 2 bytes for each
124 // character.
125 output->reserve(src_len / 2);
126 }
127 }
128
129 // Instantiate versions we know callers will need.
130 template void PrepareForUTF16Or32Output(const char*, size_t, std::u16string*);
131
132 } // namespace base
133