HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
cp_1252.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 "../utility/module.hpp"
13#include <cstdint>
14#include <utility>
15
16namespace hi { inline namespace v1 {
17
18namespace detail {
19
20constexpr auto cp_1252_make_table_0000_02DC() noexcept
21{
23 for (auto i = 0x0; i != 0x2dd; ++i) {
24 r[i] = 0x3f; // '?'
25 }
26 for (auto i = 0_uz; i != 0x80; ++i) {
27 r[i] = char_cast<uint8_t>(i);
28 }
29 r[0x81] = 0x81;
30 r[0x8d] = 0x8d;
31 r[0x8f] = 0x8f;
32 r[0x90] = 0x90;
33 r[0x9d] = 0x9d;
34 for (auto i = 0xa0; i != 0x100; ++i) {
35 r[i] = char_cast<uint8_t>(i);
36 }
37
38 r[0x192] = 0x83;
39 r[0x2c6] = 0x88;
40 r[0x160] = 0x8a;
41 r[0x152] = 0x8c;
42 r[0x17d] = 0x8e;
43
44 r[0x2dc] = 0x98;
45 r[0x161] = 0x9a;
46 r[0x153] = 0x9c;
47 r[0x17e] = 0x9e;
48 r[0x178] = 0x9f;
49
50 return r;
51}
52
53constexpr auto cp_1252_make_table_2000_2122() noexcept
54{
56 for (auto i = 0x0; i != 0x123; ++i) {
57 r[i] = 0x3f; // '?'
58 }
59
60 r[0xac] = 0x80;
61 r[0x1a] = 0x82;
62 r[0x1e] = 0x84;
63 r[0x26] = 0x85;
64 r[0x20] = 0x86;
65 r[0x21] = 0x87;
66 r[0x30] = 0x89;
67 r[0x39] = 0x8b;
68
69 r[0x18] = 0x91;
70 r[0x19] = 0x92;
71 r[0x1c] = 0x93;
72 r[0x1d] = 0x94;
73 r[0x22] = 0x95;
74 r[0x13] = 0x96;
75 r[0x14] = 0x97;
76 r[0x122] = 0x99;
77 r[0x3a] = 0x9b;
78
79 return r;
80}
81
82} // namespace detail
83
87template<>
88struct char_map<"cp-1252"> {
89 using char_type = char;
90
91 [[nodiscard]] constexpr std::endian guess_endian(void const *ptr, size_t size, std::endian endian) const noexcept
92 {
93 return std::endian::native;
94 }
95
96 template<typename It, typename EndIt>
97 [[nodiscard]] constexpr std::pair<char32_t, bool> read(It& it, EndIt last) const noexcept
98 {
99 // clang-format off
100 hi_axiom(it != last);
101 hilet c = char_cast<char8_t>(*it++);
102 switch (c) {
103 case 0x80: return {0x20ac, true};
104 case 0x81: return {0x81, true};
105 case 0x82: return {0x201a, true};
106 case 0x83: return {0x0192, true};
107 case 0x84: return {0x201e, true};
108 case 0x85: return {0x2026, true};
109 case 0x86: return {0x2020, true};
110 case 0x87: return {0x2021, true};
111 case 0x88: return {0x02c6, true};
112 case 0x89: return {0x2030, true};
113 case 0x8a: return {0x0160, true};
114 case 0x8b: return {0x2039, true};
115 case 0x8c: return {0x0152, true};
116 case 0x8d: return {0x8d, true};
117 case 0x8e: return {0x017d, true};
118 case 0x8f: return {0x8f, true};
119
120 case 0x90: return {0x90, true};
121 case 0x91: return {0x2018, true};
122 case 0x92: return {0x2019, true};
123 case 0x93: return {0x201c, true};
124 case 0x94: return {0x201d, true};
125 case 0x95: return {0x2022, true};
126 case 0x96: return {0x2013, true};
127 case 0x97: return {0x2014, true};
128 case 0x98: return {0x02dc, true};
129 case 0x99: return {0x2122, true};
130 case 0x9a: return {0x0161, true};
131 case 0x9b: return {0x203a, true};
132 case 0x9c: return {0x0153, true};
133 case 0x9d: return {0x9d, true};
134 case 0x9e: return {0x017e, true};
135 case 0x9f: return {0x0178, true};
136 default: return {c, true};
137 }
138 // clang-format on
139 }
140
141 constexpr static auto range_0000_02DC = detail::cp_1252_make_table_0000_02DC();
142 constexpr static auto range_2000_2122 = detail::cp_1252_make_table_2000_2122();
143
144 [[nodiscard]] constexpr std::pair<uint8_t, bool> size(char32_t code_point) const noexcept
145 {
146 hi_axiom(code_point < 0x11'0000);
147 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
148
149 if (code_point < 0x2dd) {
150 if (code_point == 0x3f) {
151 return {uint8_t{1}, true};
152 } else {
153 return {uint8_t{1}, range_0000_02DC[code_point] != 0x3f};
154 }
155 } else if (code_point < 0x2000) {
156 return {uint8_t{1}, false};
157
158 } else if (code_point < 0x2123) {
159 return {uint8_t{1}, range_2000_2122[wide_cast<size_t>(code_point) - 0x2000] != 0x3f};
160
161 } else {
162 return {uint8_t{1}, false};
163 }
164 }
165
166 template<typename It>
167 constexpr void write(char32_t code_point, It& dst) const noexcept
168 {
169 hi_axiom(code_point < 0x11'0000);
170 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
171
172 if (code_point < 0x2dd) {
173 *dst++ = char_cast<char_type>(range_0000_02DC[code_point]);
174
175 } else if (code_point < 0x2000) {
176 *dst++ = char_cast<char_type>(0x3f);
177
178 } else if (code_point < 0x2123) {
179 *dst++ = char_cast<char_type>(range_2000_2122[code_point - 0x2000]);
180
181 } else {
182 *dst++ = char_cast<char_type>(0x3f);
183 }
184 }
185
186#if defined(HI_HAS_SSE2)
187 template<typename It>
188 hi_force_inline __m128i read_ascii_chunk16(It it) const noexcept
189 {
190 return _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
191 }
192
193 template<typename It>
194 hi_force_inline void write_ascii_chunk16(__m128i chunk, It dst) const noexcept
195 {
196 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), chunk);
197 }
198#endif
199};
200
201}} // namespace hi::v1
Definition of the char_converter<From,To> functor.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ read
Allow read access to a file.
@ write
Allow write access to a file.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Character encoder/decoder template.
Definition char_converter.hpp:83
T addressof(T... args)