GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
unicode.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_FORMATS_UNICODE_HPP
5#define GREM_CORE_FORMATS_UNICODE_HPP
6
8
14
15#include <iterator> // std::input_iterator_tag, std::forward_iterator_tag
16#include <new> // std::launder
17
18namespace grem::unicode {
19
28[[nodiscard]] constexpr bool isValidCodePoint(char32_t codePoint) noexcept {
29 return codePoint <= 0x10FFFF && (codePoint < 0xD800 || codePoint > 0xDFFF);
30}
31
36inline constexpr char32_t CODE_POINT_ERROR{0xFFFFFFFF};
37
56template <input_iterator InputIterator, sentinel_for<InputIterator> Sentinel>
57[[nodiscard]] constexpr Pair<char32_t, InputIterator> decodeCodePointFromUTF8(InputIterator it, Sentinel end) {
58 if (it == end) {
59 [[unlikely]] return {CODE_POINT_ERROR, it}; // Reached end.
60 }
61 char32_t codePoint{};
62 const char8_t c0 = static_cast<char8_t>(*it++);
63 if ((c0 & 0b10000000u) == 0) { // 0-127
64 [[likely]] codePoint = static_cast<char32_t>(c0);
65 } else if ((c0 & 0b11100000u) == 0b11000000u) { // 128-2047
66 if (it == end) {
67 [[unlikely]] return {CODE_POINT_ERROR, it}; // Missing continuation.
68 }
69 const char8_t c1 = static_cast<char8_t>(*it++);
70 if ((c1 & 0b11000000u) != 0b10000000u) {
71 [[unlikely]] return {CODE_POINT_ERROR, it}; // Invalid continuation.
72 }
73 codePoint = ((c0 & 0b00011111u) << 6) | (c1 & 0b00111111u);
74 if (codePoint < 128) {
75 [[unlikely]] return {CODE_POINT_ERROR, it}; // Overlong sequence.
76 }
77 } else if ((c0 & 0b11110000u) == 0b11100000u) { // 2048-65535
78 if (it == end) {
79 [[unlikely]] return {CODE_POINT_ERROR, it}; // Missing continuation.
80 }
81 const char8_t c1 = static_cast<char8_t>(*it++);
82 if (it == end) {
83 [[unlikely]] return {CODE_POINT_ERROR, it}; // Missing continuation.
84 }
85 const char8_t c2 = static_cast<char8_t>(*it++);
86 if ((c1 & 0b11000000u) != 0b10000000u || (c2 & 0b11000000u) != 0b10000000u) {
87 [[unlikely]] return {CODE_POINT_ERROR, it}; // Invalid continuation.
88 }
89 codePoint = ((c0 & 0b00001111u) << 12) | ((c1 & 0b00111111u) << 6) | (c2 & 0b00111111u);
90 if (codePoint < 2048) {
91 [[unlikely]] return {CODE_POINT_ERROR, it}; // Overlong sequence.
92 }
93 if (codePoint >= 0xD800 && codePoint <= 0xDFFF) {
94 [[unlikely]] return {CODE_POINT_ERROR, it}; // Surrogate code point.
95 }
96 } else if ((c0 & 0b11111000u) == 0b11110000u) { // 65536-1114111
97 if (it == end) {
98 [[unlikely]] return {CODE_POINT_ERROR, it}; // Missing continuation.
99 }
100 const char8_t c1 = static_cast<char8_t>(*it++);
101 if (it == end) {
102 [[unlikely]] return {CODE_POINT_ERROR, it}; // Missing continuation.
103 }
104 const char8_t c2 = static_cast<char8_t>(*it++);
105 if (it == end) {
106 [[unlikely]] return {CODE_POINT_ERROR, it}; // Missing continuation.
107 }
108 const char8_t c3 = static_cast<char8_t>(*it++);
109 if ((c1 & 0b11000000u) != 0b10000000u || (c2 & 0b11000000u) != 0b10000000u || (c3 & 0b11000000u) != 0b10000000u) {
110 [[unlikely]] return {CODE_POINT_ERROR, it}; // Invalid continuation.
111 }
112 codePoint = ((c0 & 0b00000111u) << 18) | ((c1 & 0b00111111u) << 12) | ((c2 & 0b00111111u) << 6) | (c3 & 0b00111111u);
113 if (codePoint < 65536) {
114 [[unlikely]] return {CODE_POINT_ERROR, it}; // Overlong sequence.
115 }
116 if (codePoint > 1114111) {
117 [[unlikely]] return {CODE_POINT_ERROR, it}; // Invalid code point.
118 }
119 } else {
120 [[unlikely]] return {CODE_POINT_ERROR, it}; // Invalid code unit.
121 }
122 return {codePoint, it};
123}
124
136[[nodiscard]] constexpr InplaceBuffer<char8_t, 4> encodeUTF8FromCodePoint(char32_t codePoint) noexcept {
137 if (codePoint <= 0x7F) {
138 [[likely]] return {
139 static_cast<char8_t>(codePoint),
140 };
141 }
142 if (codePoint <= 0x7FF) {
143 return {
144 static_cast<char8_t>((codePoint >> 6) + 192),
145 static_cast<char8_t>((codePoint & 63) + 128),
146 };
147 }
148 if (codePoint <= 0xFFFF) {
149 return {
150 static_cast<char8_t>((codePoint >> 12) + 224),
151 static_cast<char8_t>(((codePoint >> 6) & 63) + 128),
152 static_cast<char8_t>((codePoint & 63) + 128),
153 };
154 }
155 return {
156 static_cast<char8_t>((codePoint >> 18) + 240),
157 static_cast<char8_t>(((codePoint >> 12) & 63) + 128),
158 static_cast<char8_t>(((codePoint >> 6) & 63) + 128),
159 static_cast<char8_t>((codePoint & 63) + 128),
160 };
161}
162
166struct UTF8Sentinel {};
167
175template <typename BaseIterator, typename BaseSentinel = BaseIterator>
177public:
178 using difference_type = ptrdiff_t;
179 using value_type = char32_t;
180 using reference = const value_type&;
181 using pointer = const value_type*;
182 using iterator_category = std::input_iterator_tag;
184
185 constexpr UTF8Iterator() = default;
186
187 constexpr UTF8Iterator(BaseIterator it, BaseSentinel end)
188 : it(it)
189 , end(end) {}
190
191 [[nodiscard]] constexpr bool operator==(const UTF8Iterator& other) const {
192 if (it == end || other.it == other.end) {
193 return it == other.it && hasCodePoint == other.hasCodePoint;
194 }
195 ensureCodePoint();
196 other.ensureCodePoint();
197 return it == other.it;
198 }
199
200 [[nodiscard]] constexpr bool operator==(const UTF8Sentinel&) const {
201 return it == end && !hasCodePoint;
202 }
203
204 [[nodiscard]] constexpr reference operator*() const {
205 ensureCodePoint();
206 return codePoint;
207 }
208
209 [[nodiscard]] constexpr pointer operator->() const {
210 return &**this;
211 }
212
214 if (!hasCodePoint) {
215 const auto [newCodePoint, newIt] = decodeCodePointFromUTF8(it, end);
216 it = newIt;
217 }
218 hasCodePoint = false;
219 return *this;
220 }
221
222 constexpr UTF8Iterator operator++(int) {
223 ensureCodePoint();
224 UTF8Iterator old = *this;
225 hasCodePoint = false;
226 return old;
227 }
228
229private:
230 void ensureCodePoint() const {
231 if (!hasCodePoint) {
232 const auto [newCodePoint, newIt] = decodeCodePointFromUTF8(it, end);
233 codePoint = newCodePoint;
234 hasCodePoint = true;
235 it = newIt;
236 }
237 }
238
239 mutable BaseIterator it{};
240 BaseSentinel end{};
241 mutable char32_t codePoint{};
242 mutable bool hasCodePoint = false;
243};
244
252template <typename BaseIterator, typename BaseSentinel>
253requires forward_iterator<BaseIterator> //
254class UTF8Iterator<BaseIterator, BaseSentinel> {
255public:
256 using difference_type = ptrdiff_t;
257 using value_type = char32_t;
258 using reference = const value_type&;
259 using pointer = const value_type*;
260 using iterator_category = std::forward_iterator_tag;
262
263 constexpr UTF8Iterator() = default;
264
265 constexpr UTF8Iterator(BaseIterator it, BaseSentinel end)
266 : it(it)
267 , next(it)
268 , end(end) {
269 ++*this;
270 }
271
272 [[nodiscard]] constexpr bool operator==(const UTF8Iterator& other) const {
273 return it == other.it;
274 }
275
276 [[nodiscard]] constexpr bool operator==(const UTF8Sentinel&) const {
277 return it == end;
278 }
279
280 [[nodiscard]] constexpr reference operator*() const {
281 return codePoint;
282 }
283
284 [[nodiscard]] constexpr pointer operator->() const {
285 return &**this;
286 }
287
289 it = next;
290 const auto [newCodePoint, newNext] = decodeCodePointFromUTF8(next, end);
291 codePoint = newCodePoint;
292 next = newNext;
293 return *this;
294 }
295
296 constexpr UTF8Iterator operator++(int) {
297 UTF8Iterator old = *this;
298 ++*this;
299 return old;
300 }
301
302 [[nodiscard]] constexpr BaseIterator base() const {
303 return it;
304 }
305
306private:
307 BaseIterator it{};
308 BaseIterator next{};
309 BaseSentinel end{};
310 char32_t codePoint{};
311};
312
317class UTF8View {
318public:
323 using pointer = typename iterator::pointer;
325 using sentinel = typename iterator::sentinel;
326
327 constexpr UTF8View() noexcept = default;
328
329 constexpr explicit UTF8View(UTF8StringView str) noexcept
330 : it(str.data(), str.data() + str.size()) {}
331
332 explicit UTF8View(StringView str) noexcept
333 : it(std::launder(reinterpret_cast<const char8_t*>(str.data())), std::launder(reinterpret_cast<const char8_t*>(str.data() + str.size()))) {
334 static_assert(sizeof(char) == sizeof(char8_t));
335 static_assert(alignof(char) == alignof(char8_t));
336 }
337
338 [[nodiscard]] constexpr const iterator& begin() const noexcept {
339 return it;
340 }
341
342 [[nodiscard]] constexpr sentinel end() const noexcept { // NOLINT(readability-convert-member-functions-to-static)
343 return {};
344 }
345
346private:
348};
349
350} // namespace grem::unicode
351
352#endif
Definition InplaceBuffer.hpp:24
constexpr UTF8Iterator operator++(int)
Definition unicode.hpp:296
constexpr BaseIterator base() const
Definition unicode.hpp:302
const value_type & reference
Definition unicode.hpp:258
constexpr pointer operator->() const
Definition unicode.hpp:284
ptrdiff_t difference_type
Definition unicode.hpp:256
constexpr reference operator*() const
Definition unicode.hpp:280
constexpr bool operator==(const UTF8Iterator &other) const
Definition unicode.hpp:272
std::forward_iterator_tag iterator_category
Definition unicode.hpp:260
UTF8Sentinel sentinel
Definition unicode.hpp:261
constexpr UTF8Iterator & operator++()
Definition unicode.hpp:288
constexpr bool operator==(const UTF8Sentinel &) const
Definition unicode.hpp:276
constexpr UTF8Iterator(BaseIterator it, BaseSentinel end)
Definition unicode.hpp:265
Iterator type for decoding Unicode code points from a UTF-8 string, wrapping an existing iterator for...
Definition unicode.hpp:176
const value_type * pointer
Definition unicode.hpp:181
constexpr UTF8Iterator operator++(int)
Definition unicode.hpp:222
const value_type & reference
Definition unicode.hpp:180
constexpr pointer operator->() const
Definition unicode.hpp:209
ptrdiff_t difference_type
Definition unicode.hpp:178
constexpr reference operator*() const
Definition unicode.hpp:204
std::input_iterator_tag iterator_category
Definition unicode.hpp:182
constexpr bool operator==(const UTF8Iterator &other) const
Definition unicode.hpp:191
UTF8Sentinel sentinel
Definition unicode.hpp:183
constexpr UTF8Iterator()=default
char32_t value_type
Definition unicode.hpp:179
constexpr UTF8Iterator & operator++()
Definition unicode.hpp:213
constexpr bool operator==(const UTF8Sentinel &) const
Definition unicode.hpp:200
constexpr UTF8Iterator(BaseIterator it, BaseSentinel end)
Definition unicode.hpp:187
UTF8Iterator< const char8_t * > iterator
Definition unicode.hpp:319
typename iterator::sentinel sentinel
Definition unicode.hpp:325
constexpr sentinel end() const noexcept
Definition unicode.hpp:342
typename iterator::difference_type difference_type
Definition unicode.hpp:320
typename iterator::value_type value_type
Definition unicode.hpp:321
constexpr UTF8View() noexcept=default
constexpr const iterator & begin() const noexcept
Definition unicode.hpp:338
UTF8View(StringView str) noexcept
Definition unicode.hpp:332
typename iterator::iterator_category iterator_category
Definition unicode.hpp:324
typename iterator::reference reference
Definition unicode.hpp:322
Definition concepts.hpp:106
Definition aliases.hpp:21
constexpr bool isValidCodePoint(char32_t codePoint) noexcept
Check if a 32-bit unsigned integer value falls within the valid ranges for a Unicode code point.
Definition unicode.hpp:28
constexpr char32_t CODE_POINT_ERROR
Invalid code point value, used as a return value in Unicode decoding algorithms for conveying encodin...
Definition unicode.hpp:36
constexpr InplaceBuffer< char8_t, 4 > encodeUTF8FromCodePoint(char32_t codePoint) noexcept
Encode a Unicode code point into a sequence of UTF-8 code units.
Definition unicode.hpp:136
constexpr Pair< char32_t, InputIterator > decodeCodePointFromUTF8(InputIterator it, Sentinel end)
Decode a single Unicode code point from an iterator of UTF-8 code units in a UTF-8-encoded string.
Definition unicode.hpp:57
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
StringViewBase< char8_t, std::char_traits< char8_t > > UTF8StringView
Definition StringView.hpp:17
Definition Pair.hpp:53
Sentinel type for UTF8Iterator.
Definition unicode.hpp:166