simdutf 8.0.0
Unicode at GB/s.
Loading...
Searching...
No Matches
latin1_to_utf16.h
1#ifndef SIMDUTF_LATIN1_TO_UTF16_H
2#define SIMDUTF_LATIN1_TO_UTF16_H
3
4namespace simdutf {
5namespace scalar {
6namespace {
7namespace latin1_to_utf16 {
8
9template <endianness big_endian, typename InputPtr>
10#if SIMDUTF_CPLUSPLUS20
11 requires simdutf::detail::indexes_into_byte_like<InputPtr>
12#endif
13simdutf_constexpr23 size_t convert(InputPtr data, size_t len,
14 char16_t *utf16_output) {
15 size_t pos = 0;
16 char16_t *start{utf16_output};
17
18 while (pos < len) {
19 uint16_t word =
20 uint8_t(data[pos]); // extend Latin-1 char to 16-bit Unicode code point
21 *utf16_output++ =
22 char16_t(match_system(big_endian) ? word : u16_swap_bytes(word));
23 pos++;
24 }
25
26 return utf16_output - start;
27}
28
29template <endianness big_endian>
30inline result convert_with_errors(const char *buf, size_t len,
31 char16_t *utf16_output) {
32 const uint8_t *data = reinterpret_cast<const uint8_t *>(buf);
33 size_t pos = 0;
34 char16_t *start{utf16_output};
35
36 while (pos < len) {
37 uint16_t word =
38 uint16_t(data[pos]); // extend Latin-1 char to 16-bit Unicode code point
39 *utf16_output++ =
40 char16_t(match_system(big_endian) ? word : u16_swap_bytes(word));
41 pos++;
42 }
43
44 return result(error_code::SUCCESS, utf16_output - start);
45}
46
47} // namespace latin1_to_utf16
48} // unnamed namespace
49} // namespace scalar
50} // namespace simdutf
51
52#endif