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 auto length = narrow_cast<uint8_t>(std::countl_one(char_cast<uint8_t>(cu)));
56 auto todo = length - 2;
57 hi_axiom(length >= 2);
58
59 // First part of the code-point.
60 auto cp = char_cast<char32_t>(cu & (0x7f >> length));
61
62 // Read the first continuation code-unit which is always here.
63 cu = *it++;
64 cp <<= 6;
65 cp |= cu & 0x3f;
66 if ((cu & 0xc0) != 0x80) [[unlikely]] {
67 // If the second code-unit is not a UTF-8 continuation character, treat the first
68 // code-unit as if it was CP-1252.
69 it -= 2;
70 return read_fallback(it, last);
71
72 } else if (todo >= std::distance(it, last)) [[unlikely]] {
73 // If there is a start and a continuation code-unit in a row we consider this to be UTF-8 encoded.
74 // So at this point any errors are replaced with 0xfffd.
75 it = last;
76 return {0xfffd, false};
77 }
78
79 while (todo--) {
80 cu = *it++;
81 cp <<= 6;
82 cp |= cu & 0x3f;
83 if ((cu & 0xc0) != 0x80) [[unlikely]] {
84 // Unexpected end of sequence.
85 --it;
86 return {0xfffd, false};
87 }
88 }
89
90 auto valid = true;
91 // Valid range.
92 valid &= cp < 0x11'0000;
93 // Not a surrogate.
94 valid &= cp < 0xd800 or cp >= 0xe000;
95 // Not overlong encoded.
96 valid &= length == narrow_cast<uint8_t>((cp > 0x7f) + (cp > 0x7ff) + (cp > 0xffff) + 1);
97 if (not valid) [[unlikely]] {
98 return {0xfffd, false};
99 }
100 return {cp, true};
101 }
102 };
103
104 [[nodiscard]] constexpr std::pair<uint8_t, bool> size(char32_t code_point) const noexcept
105 {
106 hi_axiom(code_point < 0x11'0000);
107 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
108
109 return {narrow_cast<uint8_t>((code_point > 0x7f) + (code_point > 0x7ff) + (code_point > 0xffff) + 1), true};
110 }
111
112 template<typename It>
113 constexpr void write(char32_t code_point, It& dst) const noexcept
114 {
115 hi_axiom(code_point < 0x11'0000);
116 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
117
118 auto length = truncate<uint8_t>((code_point > 0x7f) + (code_point > 0x7ff) + (code_point > 0xffff));
119 if (auto i = length) {
120 do {
121 dst[i] = truncate<char8_t>((code_point & 0x3f) | 0x80);
122 code_point >>= 6;
123 } while (--i);
124
125 code_point |= 0x780 >> length;
126 }
127 dst[0] = truncate<char8_t>(code_point);
128 dst += length + 1;
129 }
130
131#if defined(HI_HAS_SSE2)
132 template<typename It>
133 hi_force_inline __m128i read_ascii_chunk16(It it) const noexcept
134 {
135 return _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
136 }
137
138 template<typename It>
139 hi_force_inline void write_ascii_chunk16(__m128i chunk, It dst) const noexcept
140 {
141 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), chunk);
142 }
143#endif
144};
145
146}} // namespace hi::v1
#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.
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
Character encoder/decoder template.
Definition char_converter.hpp:85
T addressof(T... args)
T distance(T... args)