4#ifndef GREM_CORE_FORMATS_UNICODE_HPP
5#define GREM_CORE_FORMATS_UNICODE_HPP
29 return codePoint <= 0x10FFFF && (codePoint < 0xD800 || codePoint > 0xDFFF);
56template <input_iterator InputIterator, sentinel_for<InputIterator> Sentinel>
62 const char8_t c0 =
static_cast<char8_t>(*it++);
63 if ((c0 & 0b10000000u) == 0) {
64 [[likely]] codePoint =
static_cast<char32_t>(c0);
65 }
else if ((c0 & 0b11100000u) == 0b11000000u) {
69 const char8_t c1 =
static_cast<char8_t>(*it++);
70 if ((c1 & 0b11000000u) != 0b10000000u) {
73 codePoint = ((c0 & 0b00011111u) << 6) | (c1 & 0b00111111u);
74 if (codePoint < 128) {
77 }
else if ((c0 & 0b11110000u) == 0b11100000u) {
81 const char8_t c1 =
static_cast<char8_t>(*it++);
85 const char8_t c2 =
static_cast<char8_t>(*it++);
86 if ((c1 & 0b11000000u) != 0b10000000u || (c2 & 0b11000000u) != 0b10000000u) {
89 codePoint = ((c0 & 0b00001111u) << 12) | ((c1 & 0b00111111u) << 6) | (c2 & 0b00111111u);
90 if (codePoint < 2048) {
93 if (codePoint >= 0xD800 && codePoint <= 0xDFFF) {
96 }
else if ((c0 & 0b11111000u) == 0b11110000u) {
100 const char8_t c1 =
static_cast<char8_t>(*it++);
104 const char8_t c2 =
static_cast<char8_t>(*it++);
108 const char8_t c3 =
static_cast<char8_t>(*it++);
109 if ((c1 & 0b11000000u) != 0b10000000u || (c2 & 0b11000000u) != 0b10000000u || (c3 & 0b11000000u) != 0b10000000u) {
112 codePoint = ((c0 & 0b00000111u) << 18) | ((c1 & 0b00111111u) << 12) | ((c2 & 0b00111111u) << 6) | (c3 & 0b00111111u);
113 if (codePoint < 65536) {
116 if (codePoint > 1114111) {
122 return {codePoint, it};
137 if (codePoint <= 0x7F) {
139 static_cast<char8_t>(codePoint),
142 if (codePoint <= 0x7FF) {
144 static_cast<char8_t>((codePoint >> 6) + 192),
145 static_cast<char8_t>((codePoint & 63) + 128),
148 if (codePoint <= 0xFFFF) {
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),
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),
175template <
typename BaseIterator,
typename BaseSentinel = BaseIterator>
192 if (it == end || other.it == other.end) {
193 return it == other.it && hasCodePoint == other.hasCodePoint;
196 other.ensureCodePoint();
197 return it == other.it;
201 return it == end && !hasCodePoint;
218 hasCodePoint =
false;
225 hasCodePoint =
false;
230 void ensureCodePoint()
const {
233 codePoint = newCodePoint;
239 mutable BaseIterator it{};
241 mutable char32_t codePoint{};
242 mutable bool hasCodePoint =
false;
252template <
typename BaseIterator,
typename BaseSentinel>
253requires forward_iterator<BaseIterator>
273 return it == other.it;
291 codePoint = newCodePoint;
302 [[nodiscard]]
constexpr BaseIterator
base()
const {
310 char32_t codePoint{};
330 : it(str.data(), str.data() + str.size()) {}
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));
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()=default
char32_t value_type
Definition unicode.hpp:257
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
Sentinel type for UTF8Iterator.
Definition unicode.hpp:166