1#ifndef SIMDUTF_LATIN1_TO_UTF8_H
2#define SIMDUTF_LATIN1_TO_UTF8_H
9namespace latin1_to_utf8 {
11template <
typename InputPtr,
typename OutputPtr>
12#if SIMDUTF_CPLUSPLUS20
13 requires(simdutf::detail::indexes_into_byte_like<InputPtr> &&
14 simdutf::detail::index_assignable_from_char<OutputPtr>)
16simdutf_constexpr23
size_t convert(InputPtr data,
size_t len,
17 OutputPtr utf8_output) {
23#if SIMDUTF_CPLUSPLUS23
28 if (pos + 16 <= len) {
31 ::memcpy(&v1, data + pos,
sizeof(uint64_t));
33 ::memcpy(&v2, data + pos +
sizeof(uint64_t),
sizeof(uint64_t));
37 if ((v & 0x8080808080808080) ==
40 size_t final_pos = pos + 16;
41 while (pos < final_pos) {
42 utf8_output[utf8_pos++] = char(data[pos]);
50 unsigned char byte = data[pos];
51 if ((
byte & 0x80) == 0) {
53 utf8_output[utf8_pos++] = char(
byte);
57 utf8_output[utf8_pos++] = char((
byte >> 6) | 0b11000000);
58 utf8_output[utf8_pos++] = char((
byte & 0b111111) | 0b10000000);
65simdutf_really_inline
size_t convert(
const char *buf,
size_t len,
67 return convert(
reinterpret_cast<const unsigned char *
>(buf), len,
71inline size_t convert_safe(
const char *buf,
size_t len,
char *utf8_output,
73 const unsigned char *data =
reinterpret_cast<const unsigned char *
>(buf);
77 while (pos < len && utf8_pos < utf8_len) {
79 if (pos >= skip_pos && pos + 16 <= len &&
80 utf8_pos + 16 <= utf8_len) {
83 ::memcpy(&v1, data + pos,
sizeof(uint64_t));
85 ::memcpy(&v2, data + pos +
sizeof(uint64_t),
sizeof(uint64_t));
89 if ((v & 0x8080808080808080) ==
92 ::memcpy(utf8_output + utf8_pos, buf + pos, 16);
101 const auto byte = data[pos];
102 if ((
byte & 0x80) == 0) {
104 utf8_output[utf8_pos++] = char(
byte);
106 }
else if (utf8_pos + 2 <= utf8_len) {
108 utf8_output[utf8_pos++] = char((
byte >> 6) | 0b11000000);
109 utf8_output[utf8_pos++] = char((
byte & 0b111111) | 0b10000000);
119template <
typename InputPtr,
typename OutputPtr>
120#if SIMDUTF_CPLUSPLUS20
121 requires(simdutf::detail::indexes_into_byte_like<InputPtr> &&
122 simdutf::detail::index_assignable_from_char<OutputPtr>)
124simdutf_constexpr23
size_t convert_safe_constexpr(InputPtr data,
size_t len,
125 OutputPtr utf8_output,
129 while (pos < len && utf8_pos < utf8_len) {
130 const unsigned char byte = data[pos];
131 if ((
byte & 0x80) == 0) {
133 utf8_output[utf8_pos++] = char(
byte);
135 }
else if (utf8_pos + 2 <= utf8_len) {
137 utf8_output[utf8_pos++] = char((
byte >> 6) | 0b11000000);
138 utf8_output[utf8_pos++] = char((
byte & 0b111111) | 0b10000000);
147template <
typename InputPtr>
148#if SIMDUTF_CPLUSPLUS20
149 requires simdutf::detail::indexes_into_byte_like<InputPtr>
151simdutf_constexpr23 simdutf_warn_unused
size_t
153 size_t answer = length;
156#if SIMDUTF_CPLUSPLUS23
160 auto pop = [](uint64_t v) {
161 return (
size_t)(((v >> 7) & UINT64_C(0x0101010101010101)) *
162 UINT64_C(0x0101010101010101) >>
165 for (; i + 32 <= length; i += 32) {
167 memcpy(&v, input + i, 8);
169 memcpy(&v, input + i + 8,
sizeof(v));
171 memcpy(&v, input + i + 16,
sizeof(v));
173 memcpy(&v, input + i + 24,
sizeof(v));
176 for (; i + 8 <= length; i += 8) {
178 memcpy(&v, input + i,
sizeof(v));
182 for (; i + 1 <= length; i += 1) {
183 answer +=
static_cast<uint8_t
>(input[i]) >> 7;
helpers placed in namespace detail are not a part of the public API
simdutf_warn_unused size_t utf8_length_from_latin1(const char *input, size_t length) noexcept
Return the number of bytes that this Latin1 string would require in UTF-8 format.