1#ifndef SIMDUTF_UTF16_TO_UTF8_H
2#define SIMDUTF_UTF16_TO_UTF8_H
7namespace utf16_to_utf8 {
9template <endianness big_endian,
typename InputPtr,
typename OutputPtr>
10#if SIMDUTF_CPLUSPLUS20
11 requires simdutf::detail::indexes_into_utf16<InputPtr>
14simdutf_constexpr23
size_t convert(InputPtr data,
size_t len,
15 OutputPtr utf8_output) {
17 const auto start = utf8_output;
19#if SIMDUTF_CPLUSPLUS23
27 ::memcpy(&v, data + pos,
sizeof(uint64_t));
28 if simdutf_constexpr (!match_system(big_endian)) {
29 v = (v >> 8) | (v << (64 - 8));
31 if ((v & 0xFF80FF80FF80FF80) == 0) {
32 size_t final_pos = pos + 4;
33 while (pos < final_pos) {
34 *utf8_output++ = !match_system(big_endian)
35 ? char(u16_swap_bytes(data[pos]))
44 !match_system(big_endian) ? u16_swap_bytes(data[pos]) : data[pos];
45 if ((word & 0xFF80) == 0) {
47 *utf8_output++ = char(word);
49 }
else if ((word & 0xF800) == 0) {
52 *utf8_output++ = char((word >> 6) | 0b11000000);
53 *utf8_output++ = char((word & 0b111111) | 0b10000000);
55 }
else if ((word & 0xF800) != 0xD800) {
58 *utf8_output++ = char((word >> 12) | 0b11100000);
59 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
60 *utf8_output++ = char((word & 0b111111) | 0b10000000);
67 uint16_t diff = uint16_t(word - 0xD800);
71 uint16_t next_word = !match_system(big_endian)
72 ? u16_swap_bytes(data[pos + 1])
74 uint16_t diff2 = uint16_t(next_word - 0xDC00);
78 uint32_t value = (diff << 10) + diff2 + 0x10000;
81 *utf8_output++ = char((value >> 18) | 0b11110000);
82 *utf8_output++ = char(((value >> 12) & 0b111111) | 0b10000000);
83 *utf8_output++ = char(((value >> 6) & 0b111111) | 0b10000000);
84 *utf8_output++ = char((value & 0b111111) | 0b10000000);
88 return utf8_output - start;
91template <endianness big_endian,
bool check_output =
false,
typename InputPtr,
93#if SIMDUTF_CPLUSPLUS20
94 requires(simdutf::detail::indexes_into_utf16<InputPtr> &&
95 simdutf::detail::index_assignable_from_char<OutputPtr>)
97simdutf_constexpr23 full_result convert_with_errors(InputPtr data,
size_t len,
98 OutputPtr utf8_output,
99 size_t utf8_len = 0) {
100 if (check_output && utf8_len == 0) {
101 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, 0, 0);
105 auto start = utf8_output;
106 auto end = utf8_output + utf8_len;
109#if SIMDUTF_CPLUSPLUS23
114 if (pos + 4 <= len) {
117 ::memcpy(&v, data + pos,
sizeof(uint64_t));
118 if simdutf_constexpr (!match_system(big_endian))
119 v = (v >> 8) | (v << (64 - 8));
120 if ((v & 0xFF80FF80FF80FF80) == 0) {
121 size_t final_pos = pos + 4;
122 while (pos < final_pos) {
123 if (check_output &&
size_t(end - utf8_output) < 1) {
124 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
125 utf8_output - start);
127 *utf8_output++ = !match_system(big_endian)
128 ? char(u16_swap_bytes(data[pos]))
138 !match_system(big_endian) ? u16_swap_bytes(data[pos]) : data[pos];
139 if ((word & 0xFF80) == 0) {
141 if (check_output &&
size_t(end - utf8_output) < 1) {
142 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
143 utf8_output - start);
145 *utf8_output++ = char(word);
147 }
else if ((word & 0xF800) == 0) {
150 if (check_output &&
size_t(end - utf8_output) < 2) {
151 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
152 utf8_output - start);
154 *utf8_output++ = char((word >> 6) | 0b11000000);
155 *utf8_output++ = char((word & 0b111111) | 0b10000000);
158 }
else if ((word & 0xF800) != 0xD800) {
161 if (check_output &&
size_t(end - utf8_output) < 3) {
162 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
163 utf8_output - start);
165 *utf8_output++ = char((word >> 12) | 0b11100000);
166 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
167 *utf8_output++ = char((word & 0b111111) | 0b10000000);
171 if (check_output &&
size_t(end - utf8_output) < 4) {
172 return full_result(error_code::OUTPUT_BUFFER_TOO_SMALL, pos,
173 utf8_output - start);
176 if (pos + 1 >= len) {
177 return full_result(error_code::SURROGATE, pos, utf8_output - start);
179 uint16_t diff = uint16_t(word - 0xD800);
181 return full_result(error_code::SURROGATE, pos, utf8_output - start);
183 uint16_t next_word = !match_system(big_endian)
184 ? u16_swap_bytes(data[pos + 1])
186 uint16_t diff2 = uint16_t(next_word - 0xDC00);
188 return full_result(error_code::SURROGATE, pos, utf8_output - start);
190 uint32_t value = (diff << 10) + diff2 + 0x10000;
193 *utf8_output++ = char((value >> 18) | 0b11110000);
194 *utf8_output++ = char(((value >> 12) & 0b111111) | 0b10000000);
195 *utf8_output++ = char(((value >> 6) & 0b111111) | 0b10000000);
196 *utf8_output++ = char((value & 0b111111) | 0b10000000);
200 return full_result(error_code::SUCCESS, pos, utf8_output - start);
203template <endianness big_endian>
204inline result simple_convert_with_errors(
const char16_t *buf,
size_t len,
206 return convert_with_errors<big_endian, false>(buf, len, utf8_output, 0);