1#ifndef SIMDUTF_UTF16_TO_UTF8_H
2#define SIMDUTF_UTF16_TO_UTF8_H
9namespace utf16_to_utf8 {
11template <endianness big_endian,
typename InputPtr,
typename OutputPtr>
12#if SIMDUTF_CPLUSPLUS20
13 requires simdutf::detail::indexes_into_utf16<InputPtr>
16simdutf_constexpr23
size_t convert(InputPtr data,
size_t len,
17 OutputPtr utf8_output) {
19 const auto start = utf8_output;
21#if SIMDUTF_CPLUSPLUS23
29 ::memcpy(&v, data + pos,
sizeof(uint64_t));
30 if constexpr (!match_system(big_endian)) {
31 v = (v >> 8) | (v << (64 - 8));
33 if ((v & 0xFF80FF80FF80FF80) == 0) {
34 size_t final_pos = pos + 4;
35 while (pos < final_pos) {
36 *utf8_output++ = !match_system(big_endian)
37 ? char(u16_swap_bytes(data[pos]))
46 !match_system(big_endian) ? u16_swap_bytes(data[pos]) : data[pos];
47 if ((word & 0xFF80) == 0) {
49 *utf8_output++ = char(word);
51 }
else if ((word & 0xF800) == 0) {
54 *utf8_output++ = char((word >> 6) | 0b11000000);
55 *utf8_output++ = char((word & 0b111111) | 0b10000000);
57 }
else if ((word & 0xF800) != 0xD800) {
60 *utf8_output++ = char((word >> 12) | 0b11100000);
61 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
62 *utf8_output++ = char((word & 0b111111) | 0b10000000);
69 uint16_t diff = uint16_t(word - 0xD800);
73 uint16_t next_word = !match_system(big_endian)
74 ? u16_swap_bytes(data[pos + 1])
76 uint16_t diff2 = uint16_t(next_word - 0xDC00);
80 uint32_t value = (diff << 10) + diff2 + 0x10000;
83 *utf8_output++ = char((value >> 18) | 0b11110000);
84 *utf8_output++ = char(((value >> 12) & 0b111111) | 0b10000000);
85 *utf8_output++ = char(((value >> 6) & 0b111111) | 0b10000000);
86 *utf8_output++ = char((value & 0b111111) | 0b10000000);
90 return utf8_output - start;
93template <endianness big_endian,
bool check_output =
false,
typename InputPtr,
95#if SIMDUTF_CPLUSPLUS20
96 requires(simdutf::detail::indexes_into_utf16<InputPtr> &&
97 simdutf::detail::index_assignable_from_char<OutputPtr>)
99simdutf_constexpr23 full_result convert_with_errors(InputPtr data,
size_t len,
100 OutputPtr utf8_output,
101 size_t utf8_len = 0) {
102 if (check_output && utf8_len == 0) {
103 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, 0, 0);
107 auto start = utf8_output;
108 auto end = utf8_output + utf8_len;
111#if SIMDUTF_CPLUSPLUS23
116 if (pos + 4 <= len) {
119 ::memcpy(&v, data + pos,
sizeof(uint64_t));
120 if constexpr (!match_system(big_endian))
121 v = (v >> 8) | (v << (64 - 8));
122 if ((v & 0xFF80FF80FF80FF80) == 0) {
123 size_t final_pos = pos + 4;
124 while (pos < final_pos) {
125 if (check_output &&
size_t(end - utf8_output) < 1) {
126 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
127 utf8_output - start);
129 *utf8_output++ = !match_system(big_endian)
130 ? char(u16_swap_bytes(data[pos]))
140 !match_system(big_endian) ? u16_swap_bytes(data[pos]) : data[pos];
141 if ((word & 0xFF80) == 0) {
143 if (check_output &&
size_t(end - utf8_output) < 1) {
144 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
145 utf8_output - start);
147 *utf8_output++ = char(word);
149 }
else if ((word & 0xF800) == 0) {
152 if (check_output &&
size_t(end - utf8_output) < 2) {
153 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
154 utf8_output - start);
156 *utf8_output++ = char((word >> 6) | 0b11000000);
157 *utf8_output++ = char((word & 0b111111) | 0b10000000);
160 }
else if ((word & 0xF800) != 0xD800) {
163 if (check_output &&
size_t(end - utf8_output) < 3) {
164 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
165 utf8_output - start);
167 *utf8_output++ = char((word >> 12) | 0b11100000);
168 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
169 *utf8_output++ = char((word & 0b111111) | 0b10000000);
173 if (check_output &&
size_t(end - utf8_output) < 4) {
174 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
175 utf8_output - start);
178 if (pos + 1 >= len) {
179 return full_result(error_code::SURROGATE, pos, utf8_output - start);
181 uint16_t diff = uint16_t(word - 0xD800);
183 return full_result(error_code::SURROGATE, pos, utf8_output - start);
185 uint16_t next_word = !match_system(big_endian)
186 ? u16_swap_bytes(data[pos + 1])
188 uint16_t diff2 = uint16_t(next_word - 0xDC00);
190 return full_result(error_code::SURROGATE, pos, utf8_output - start);
192 uint32_t value = (diff << 10) + diff2 + 0x10000;
195 *utf8_output++ = char((value >> 18) | 0b11110000);
196 *utf8_output++ = char(((value >> 12) & 0b111111) | 0b10000000);
197 *utf8_output++ = char(((value >> 6) & 0b111111) | 0b10000000);
198 *utf8_output++ = char((value & 0b111111) | 0b10000000);
202 return full_result(error_code::SUCCESS, pos, utf8_output - start);
205template <endianness big_endian>
206inline result simple_convert_with_errors(
const char16_t *buf,
size_t len,
208 return convert_with_errors<big_endian, false>(buf, len, utf8_output, 0);
211template <endianness big_endian>
212simdutf_constexpr23
size_t convert_with_replacement(
const char16_t *data,
216 char *start = utf8_output;
218#if SIMDUTF_CPLUSPLUS23
223 if (pos + 4 <= len) {
226 ::memcpy(&v, data + pos,
sizeof(uint64_t));
227 if constexpr (!match_system(big_endian)) {
228 v = (v >> 8) | (v << (64 - 8));
230 if ((v & 0xFF80FF80FF80FF80) == 0) {
231 size_t final_pos = pos + 4;
232 while (pos < final_pos) {
233 *utf8_output++ = !match_system(big_endian)
234 ? char(u16_swap_bytes(data[pos]))
243 !match_system(big_endian) ? u16_swap_bytes(data[pos]) : data[pos];
244 if ((word & 0xFF80) == 0) {
246 *utf8_output++ = char(word);
248 }
else if ((word & 0xF800) == 0) {
251 *utf8_output++ = char((word >> 6) | 0b11000000);
252 *utf8_output++ = char((word & 0b111111) | 0b10000000);
254 }
else if ((word & 0xF800) != 0xD800) {
257 *utf8_output++ = char((word >> 12) | 0b11100000);
258 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
259 *utf8_output++ = char((word & 0b111111) | 0b10000000);
263 uint16_t diff = uint16_t(word - 0xD800);
264 if (diff <= 0x3FF && pos + 1 < len) {
266 uint16_t next_word = !match_system(big_endian)
267 ? u16_swap_bytes(data[pos + 1])
269 uint16_t diff2 = uint16_t(next_word - 0xDC00);
270 if (diff2 <= 0x3FF) {
272 uint32_t value = (diff << 10) + diff2 + 0x10000;
274 *utf8_output++ = char((value >> 18) | 0b11110000);
275 *utf8_output++ = char(((value >> 12) & 0b111111) | 0b10000000);
276 *utf8_output++ = char(((value >> 6) & 0b111111) | 0b10000000);
277 *utf8_output++ = char((value & 0b111111) | 0b10000000);
283 *utf8_output++ = char(0xef);
284 *utf8_output++ = char(0xbf);
285 *utf8_output++ = char(0xbd);
289 return utf8_output - start;
helpers placed in namespace detail are not a part of the public API