HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
utf_16.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8#pragma once
9
10#include "char_converter.hpp"
11#include "../macros.hpp"
12
13hi_warning_push();
14// C26490: Don't use reinterpret_cast.
15// Needed for SIMD intrinsics.
16hi_warning_ignore_msvc(26490);
17
18namespace hi { inline namespace v1 {
19
24template<>
25struct char_map<"utf-16"> {
26 using char_type = char16_t;
27
28 [[nodiscard]] std::endian guess_endian(void const *ptr, size_t size, std::endian endian) const noexcept
29 {
30 hi_assert_not_null(ptr);
31 auto *ptr_ = static_cast<uint8_t const *>(ptr);
32 hi_axiom_not_null(ptr_);
33
34 if (size < 2) {
35 return std::endian::native;
36 } else {
37 // Check for BOM.
38 if (ptr_[0] == 0xfe and ptr_[1] == 0xff) {
39 return std::endian::big;
40 } else if (ptr_[0] == 0xff and ptr_[1] == 0xfe) {
41 return std::endian::little;
42 }
43
44 // Check for sequences of zeros.
45 auto count = std::array<size_t, 2>{};
46 for (auto i = 0; i != size; ++i) {
47 count[i % 2] = ptr_[i] == 0 ? count[i % 2] + 1 : 0;
48 if (count[i % 2] >= 8) {
49 return i % 2 == 0 ? std::endian::big : std::endian::little;
50 }
51 }
52
53 return endian;
54 }
55 }
56
57 template<typename It, typename EndIt>
58 [[nodiscard]] constexpr std::pair<char32_t, bool> read(It& it, EndIt last) const noexcept
59 {
60 hi_axiom(it != last);
61
62 if (auto cu = *it++; cu < 0xd800) {
63 return {char_cast<char32_t>(cu), true};
64
65 } else if (cu < 0xdc00) {
66 if (it == last) {
67 // first surrogate at end of string.
68 return {0xfffd, false};
69
70 } else {
71 auto cp = char_cast<char32_t>(cu & 0x03ff);
72 cu = *it;
73 if (cu >= 0xdc00 and cu < 0xe000) {
74 ++it;
75 cp <<= 10;
76 cp |= cu & 0x03ff;
77 cp += 0x01'0000;
78 return {cp, true};
79
80 } else {
81 // unpaired surrogate.
82 return {0xfffd, false};
83 }
84 }
85
86 } else if (cu < 0xe000) {
87 // Invalid low surrogate.
88 return {0xfffd, false};
89
90 } else {
91 return {cu, true};
92 }
93 }
94
95 [[nodiscard]] constexpr std::pair<uint8_t, bool> size(char32_t code_point) const noexcept
96 {
97 hi_axiom(code_point < 0x11'0000);
98 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
99 return {truncate<uint8_t>((code_point >= 0x01'0000) + 1), true};
100 }
101
102 template<typename It>
103 constexpr void write(char32_t code_point, It& dst) const noexcept
104 {
105 hi_axiom(code_point <= 0x10'ffff);
106 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
107
108 if (hilet tmp = truncate<int32_t>(code_point) - 0x1'0000; tmp >= 0) {
109 *dst++ = char_cast<char16_t>((tmp >> 10) + 0xd800);
110 *dst++ = char_cast<char16_t>((tmp & 0x3ff) + 0xdc00);
111
112 } else {
113 *dst++ = char_cast<char16_t>(code_point);
114 }
115 }
116
117#if defined(HI_HAS_SSE2)
118 template<typename It>
119 hi_force_inline __m128i read_ascii_chunk16(It it) const noexcept
120 {
121 // Load the UTF-16 data.
122 hilet lo = _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
123 it += 8;
124 hilet hi = _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
125
126 // To get _mm_packus_epi16() to work we need to prepare the data as follows:
127 // - bit 15 must be '0'.
128 // - if bit 15 was originally set than we need to set any of bits [14:8].
129
130 // Positive numbers -> 0b0000'0000
131 // Negative numbers -> 0b1000'0000
132 hilet sign_lo = _mm_srai_epi16(lo, 15);
133 hilet sign_hi = _mm_srai_epi16(hi, 15);
135
136 // ASCII -> 0b0ccc'cccc
137 // positive numbers -> 0b1???'????
138 // negative numbers -> 0b0000'0000
139 hilet chunk = _mm_packus_epi16(lo, hi);
140
141 // ASCII -> 0b0ccc'cccc
142 // positive numbers -> 0b1???'????
143 // negative numbers -> 0b1000'0000
144 return _mm_or_si128(chunk, sign);
145 }
146
147 template<typename It>
148 hi_force_inline void write_ascii_chunk16(__m128i chunk, It dst) const noexcept
149 {
150 hilet zero = _mm_setzero_si128();
151 hilet lo = _mm_unpacklo_epi8(chunk, zero);
152 hilet hi = _mm_unpackhi_epi8(chunk, zero);
153
154 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), lo);
155 dst += 8;
156 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), hi);
157 }
158#endif
159};
160
161}} // namespace hi::v1
162
163hi_warning_pop();
Definition of the char_converter<From,To> functor.
@ read
Allow read access to a file.
@ write
Allow write access to a file.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
@ zero
The number was zero, and this means something in the current language.
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Character encoder/decoder template.
Definition char_converter.hpp:86
T addressof(T... args)