HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
utf_8.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
9#pragma once
10
11#include "char_converter.hpp"
12#include "cp_1252.hpp"
13#include <bit>
14
15namespace hi { inline namespace v1 {
16
20template<>
21struct char_map<"utf-8"> {
22 using char_type = char;
23 using fallback_encoder_type = char_map<"cp-1252">;
24 using fallback_char_type = fallback_encoder_type::char_type;
25
26 [[nodiscard]] constexpr std::endian guess_endian(void const *ptr, size_t size, std::endian endian) const noexcept
27 {
28 return std::endian::native;
29 }
30
31 template<typename It, typename EndIt>
32 [[nodiscard]] constexpr std::pair<char32_t, bool> read_fallback(It& it, EndIt last) const noexcept
33 {
34 hilet[code_point, valid] = fallback_encoder_type{}.read(it, last);
35 return {code_point, false};
36 }
37
38 template<typename It, typename EndIt>
39 [[nodiscard]] constexpr std::pair<char32_t, bool> read(It& it, EndIt last) const noexcept
40 {
41 hi_axiom(it != last);
42
43 auto cu = *it++;
44 if (not to_bool(cu & 0x80)) [[likely]] {
45 // ASCII character.
46 return {char_cast<char32_t>(cu), true};
47
48 } else if (it == last or (cu & 0xc0) == 0x80) [[unlikely]] {
49 // A non-ASCII character at the end of string.
50 // or an unexpected continuation code-unit should be treated as CP-1252.
51 --it;
52 return read_fallback(it, last);
53
54 } else {
55 hilet length = narrow_cast<uint8_t>(std::countl_one(char_cast<uint8_t>(cu)));
56 hi_axiom(length >= 2);
57
58 // First part of the code-point.
59 auto cp = char_cast<char32_t>(cu & (0x7f >> length));
60
61 // Read the first continuation code-unit which is always here.
62 cu = *it++;
63 cp <<= 6;
64 cp |= cu & 0x3f;
65 if ((cu & 0xc0) != 0x80) [[unlikely]] {
66 // If the second code-unit is not a UTF-8 continuation character, treat the first
67 // code-unit as if it was CP-1252.
68 it -= 2;
69 return read_fallback(it, last);
70
71 }
72
73 // After we read the first two code-units how many more to do.
74 auto todo = length - 2;
75 if (todo > std::distance(it, last)) [[unlikely]] {
76 // If there is a start and a continuation code-unit in a row we consider this to be UTF-8 encoded.
77 // So at this point any errors are replaced with 0xfffd.
78 it = last;
79 return {0xfffd, false};
80 }
81
82 while (todo--) {
83 cu = *it++;
84 cp <<= 6;
85 cp |= cu & 0x3f;
86 if ((cu & 0xc0) != 0x80) [[unlikely]] {
87 // Unexpected end of sequence.
88 --it;
89 return {0xfffd, false};
90 }
91 }
92
93 auto valid = true;
94 // Valid range.
95 valid &= cp < 0x11'0000;
96 // Not a surrogate.
97 valid &= cp < 0xd800 or cp >= 0xe000;
98 // Not overlong encoded.
99 valid &= length == narrow_cast<uint8_t>((cp > 0x7f) + (cp > 0x7ff) + (cp > 0xffff) + 1);
100 if (not valid) [[unlikely]] {
101 return {0xfffd, false};
102 }
103 return {cp, true};
104 }
105 };
106
107 [[nodiscard]] constexpr std::pair<uint8_t, bool> size(char32_t code_point) const noexcept
108 {
109 hi_axiom(code_point < 0x11'0000);
110 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
111
112 return {narrow_cast<uint8_t>((code_point > 0x7f) + (code_point > 0x7ff) + (code_point > 0xffff) + 1), true};
113 }
114
115 template<typename It>
116 constexpr void write(char32_t code_point, It& dst) const noexcept
117 {
118 hi_axiom(code_point < 0x11'0000);
119 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
120
121 auto length = truncate<uint8_t>((code_point > 0x7f) + (code_point > 0x7ff) + (code_point > 0xffff));
122 if (auto i = length) {
123 do {
124 dst[i] = truncate<char8_t>((code_point & 0x3f) | 0x80);
125 code_point >>= 6;
126 } while (--i);
127
128 code_point |= 0x780 >> length;
129 }
130 dst[0] = truncate<char8_t>(code_point);
131 dst += length + 1;
132 }
133
134#if defined(HI_HAS_SSE2)
135 template<typename It>
136 hi_force_inline __m128i read_ascii_chunk16(It it) const noexcept
137 {
138 return _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
139 }
140
141 template<typename It>
142 hi_force_inline void write_ascii_chunk16(__m128i chunk, It dst) const noexcept
143 {
144 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), chunk);
145 }
146#endif
147};
148
149}} // namespace hi::v1
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
Definition of the CP-1252 / Windows-1252 character map.
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:15
geometry/margins.hpp
Definition assert.hpp:18
Character encoder/decoder template.
Definition char_converter.hpp:85
T addressof(T... args)
T distance(T... args)