20namespace ZenitUI::Utf8
29 return (b & 0xC0u) == 0x80u;
35 return cp <= 0x10FFFFu && !(cp >= 0xD800u && cp <= 0xDFFFu);
47 if ((b & 0xE0u) == 0xC0u)
49 if ((b & 0xF0u) == 0xE0u)
51 if ((b & 0xF8u) == 0xF0u)
60 inline size_t encode(uint32_t cp, std::string &out)
66 out.push_back(
static_cast<char>(cp));
71 out.push_back(
static_cast<char>(0xC0u | (cp >> 6)));
72 out.push_back(
static_cast<char>(0x80u | (cp & 0x3Fu)));
77 out.push_back(
static_cast<char>(0xE0u | (cp >> 12)));
78 out.push_back(
static_cast<char>(0x80u | ((cp >> 6) & 0x3Fu)));
79 out.push_back(
static_cast<char>(0x80u | (cp & 0x3Fu)));
82 out.push_back(
static_cast<char>(0xF0u | (cp >> 18)));
83 out.push_back(
static_cast<char>(0x80u | ((cp >> 12) & 0x3Fu)));
84 out.push_back(
static_cast<char>(0x80u | ((cp >> 6) & 0x3Fu)));
85 out.push_back(
static_cast<char>(0x80u | (cp & 0x3Fu)));
90 inline std::string
encode(uint32_t cp)
108 inline size_t decode(std::string_view s,
size_t pos, uint32_t &outCp)
noexcept
116 const unsigned char b0 =
static_cast<unsigned char>(s[pos]);
123 if ((b0 & 0xE0u) == 0xC0u && pos + 1 < s.size())
125 const unsigned char b1 =
static_cast<unsigned char>(s[pos + 1]);
128 uint32_t cp = ((b0 & 0x1Fu) << 6) | (b1 & 0x3Fu);
136 else if ((b0 & 0xF0u) == 0xE0u && pos + 2 < s.size())
138 const unsigned char b1 =
static_cast<unsigned char>(s[pos + 1]);
139 const unsigned char b2 =
static_cast<unsigned char>(s[pos + 2]);
142 uint32_t cp = ((b0 & 0x0Fu) << 12) | ((b1 & 0x3Fu) << 6) | (b2 & 0x3Fu);
150 else if ((b0 & 0xF8u) == 0xF0u && pos + 3 < s.size())
152 const unsigned char b1 =
static_cast<unsigned char>(s[pos + 1]);
153 const unsigned char b2 =
static_cast<unsigned char>(s[pos + 2]);
154 const unsigned char b3 =
static_cast<unsigned char>(s[pos + 3]);
157 uint32_t cp = ((b0 & 0x07u) << 18) | ((b1 & 0x3Fu) << 12) |
158 ((b2 & 0x3Fu) << 6) | (b3 & 0x3Fu);
175 inline uint32_t
next(std::string_view s,
size_t &pos)
noexcept
183 inline size_t snapFloor(std::string_view s,
size_t pos)
noexcept
187 while (pos > 0 &&
isContinuation(
static_cast<unsigned char>(s[pos])))
193 inline size_t snapCeil(std::string_view s,
size_t pos)
noexcept
197 while (pos < s.size() &&
isContinuation(
static_cast<unsigned char>(s[pos])))
208 return decode(s, pos, cp);
219 while (pos > 0 &&
isContinuation(
static_cast<unsigned char>(s[pos])))
229 while (pos < s.size())
245 return (cp >= 0x0300u && cp <= 0x036Fu) ||
246 (cp >= 0x1AB0u && cp <= 0x1AFFu) ||
247 (cp >= 0x1DC0u && cp <= 0x1DFFu) ||
248 (cp >= 0x20D0u && cp <= 0x20FFu) ||
249 (cp >= 0xFE00u && cp <= 0xFE0Fu) ||
250 (cp >= 0xFE20u && cp <= 0xFE2Fu) ||
251 (cp >= 0x1F3FBu && cp <= 0x1F3FFu) ||
252 (cp >= 0xE0100u && cp <= 0xE01EFu);
258 return cp >= 0x1F1E6u && cp <= 0x1F1FFu;
268 inline size_t nextCluster(std::string_view s,
size_t pos)
noexcept
274 uint32_t firstCp = 0;
275 size_t cur =
decode(s, pos, firstCp);
278 if (firstCp ==
'\r' && cur < s.size() && s[cur] ==
'\n')
285 size_t nextPos =
decode(s, cur, nextCp);
291 while (cur < s.size())
294 size_t after =
decode(s, cur, cp);
300 if (cp == 0x200Du && after < s.size())
302 uint32_t joinedCp = 0;
303 size_t afterJoined =
decode(s, after, joinedCp);
318 inline size_t prevCluster(std::string_view s,
size_t pos)
noexcept
326 if (pos < s.size() &&
isContinuation(
static_cast<unsigned char>(s[pos])))
334 if (cp ==
'\n' && cur > 0 && s[cur - 1] ==
'\r')
349 uint32_t beforeCp = 0;
350 decode(s, before, beforeCp);
351 if (beforeCp == 0x200Du && before > 0)
375 if ((runCount % 2u) == 1u)
391 std::vector<size_t> bounds;
392 bounds.reserve(s.size() + 1);
395 while (pos < s.size())
398 bounds.push_back(pos);
413 if (cp < 0x20u || (cp >= 0x7Fu && cp <= 0x9Fu))
426 if ((cp >=
'a' && cp <=
'z') || (cp >=
'A' && cp <=
'Z') ||
427 (cp >=
'0' && cp <=
'9') || cp ==
'_')
429 if (cp >= 0x00C0u && cp <= 0x00FFu)
430 return cp != 0x00D7u && cp != 0x00F7u;
431 if (cp >= 0x0100u && cp <= 0x024Fu)
435 if ((cp >= 0x0370u && cp <= 0x052Fu) ||
436 (cp >= 0x3040u && cp <= 0x30FFu) ||
437 (cp >= 0x4E00u && cp <= 0x9FFFu) ||
438 (cp >= 0xAC00u && cp <= 0xD7AFu))
472 while (pos < s.size())
480 while (pos < s.size())
500 out.reserve(src.size());
502 while (pos < src.size())
522 out.reserve(src.size());
524 while (pos < src.size())
526 if (src[pos] ==
'\r')
528 if (pos + 1 < src.size() && src[pos + 1] ==
'\n')
535 if (src[pos] ==
'\n')
568 CodepointSet() =
default;
574 cps_.push_back(
static_cast<int>(cp));
579 CodepointSet &
addRange(uint32_t first, uint32_t last)
583 if (last > 0x10FFFFu)
585 cps_.reserve(cps_.size() +
static_cast<size_t>(last - first + 1));
586 for (uint32_t cp = first; cp <= last; ++cp)
589 cps_.push_back(
static_cast<int>(cp));
603 while (pos < text.size())
608 cps_.push_back(
static_cast<int>(cp));
615 CodepointSet &
merge(
const CodepointSet &other)
617 cps_.insert(cps_.end(), other.cps_.begin(), other.cps_.end());
625 bool empty()
const {
return cps_.empty(); }
630 std::vector<int> out = cps_;
631 std::sort(out.begin(), out.end());
632 out.erase(std::unique(out.begin(), out.end()), out.end());
666 for (uint32_t cp : {0x2013u, 0x2014u, 0x2018u, 0x2019u, 0x201Cu,
667 0x201Du, 0x2022u, 0x2026u, 0x20ACu, 0x2122u})
703 for (uint32_t cp : {0x2212u, 0x221Eu, 0x2248u, 0x2260u, 0x2264u, 0x2265u,
704 0x25B2u, 0x25B6u, 0x25BCu, 0x25C0u,
706 0x2660u, 0x2663u, 0x2665u, 0x2666u,
713 std::vector<int> cps_;
std::vector< int > build() const
Build a sorted, deduplicated codepoint vector for font loading.
Definition Utf8.hpp:628
CodepointSet & addCodepoint(uint32_t cp)
Add a single valid Unicode scalar value.
Definition Utf8.hpp:571
static CodepointSet ascii()
Printable ASCII (U+0020..U+007E).
Definition Utf8.hpp:639
static CodepointSet symbols()
Common UI, math, arrow, and card/game symbols (←↑→↓ ≤≥≠∞ ▲▼◀▶ ★☆ ♠♥♦♣ ✓✗).
Definition Utf8.hpp:699
CodepointSet & merge(const CodepointSet &other)
Merge all codepoints from other into this set.
Definition Utf8.hpp:615
static CodepointSet typography()
Common European typography (– — ‘ ’ “ ” • … € ™).
Definition Utf8.hpp:663
static CodepointSet greek()
Greek and Coptic (U+0370..U+03FF).
Definition Utf8.hpp:683
void clear()
Remove all codepoints.
Definition Utf8.hpp:622
CodepointSet & addFromUtf8(std::string_view text)
Extract and add every valid printable codepoint from a UTF-8 string.
Definition Utf8.hpp:600
static CodepointSet latinExtendedA()
Latin Extended-A (U+0100..U+017F: Central/Eastern European letters, Œ, œ).
Definition Utf8.hpp:655
static CodepointSet cyrillic()
Cyrillic (U+0400..U+04FF).
Definition Utf8.hpp:691
bool empty() const
true if no codepoints have been added.
Definition Utf8.hpp:625
static CodepointSet defaultLatin()
Default preset: ASCII + Latin-1 + Œ/œ + common European typography.
Definition Utf8.hpp:673
CodepointSet & addRange(uint32_t first, uint32_t last)
Add an inclusive range [first, last], skipping UTF-16 surrogates.
Definition Utf8.hpp:579
static CodepointSet latin1()
Latin-1 Supplement (U+00A0..U+00FF: Western European accents, °, ×, ÷).
Definition Utf8.hpp:647
size_t codepointCount(std::string_view s) noexcept
Count the number of UTF-8 codepoints in s.
Definition Utf8.hpp:225
std::string sanitizeMultiLine(std::string_view src)
Sanitize a pasted string for multi-line text fields.
Definition Utf8.hpp:519
constexpr bool isContinuation(unsigned char b) noexcept
true if b is a UTF-8 continuation byte (10xxxxxx).
Definition Utf8.hpp:27
constexpr bool isValidCodepoint(uint32_t cp) noexcept
true if cp is a valid Unicode scalar value (excludes UTF-16 surrogates).
Definition Utf8.hpp:33
constexpr bool isClusterExtend(uint32_t cp) noexcept
true if cp extends the preceding grapheme cluster.
Definition Utf8.hpp:243
constexpr bool isWordChar(uint32_t cp) noexcept
true if cp belongs to a word (for double-click selection and Ctrl+Arrow).
Definition Utf8.hpp:424
constexpr bool isPrintable(uint32_t cp) noexcept
true if cp is a printable Unicode scalar value accepted by text inputs.
Definition Utf8.hpp:409
size_t nextCodepoint(std::string_view s, size_t pos) noexcept
Advance pos by one UTF-8 codepoint.
Definition Utf8.hpp:203
size_t nextWordBoundary(std::string_view s, size_t pos) noexcept
Move forward past the current/next word (Ctrl+Right / Ctrl+Delete).
Definition Utf8.hpp:469
constexpr bool isRegionalIndicator(uint32_t cp) noexcept
true if cp is a Regional Indicator symbol (U+1F1E6..U+1F1FF).
Definition Utf8.hpp:256
size_t prevCluster(std::string_view s, size_t pos) noexcept
Move pos back by one grapheme cluster.
Definition Utf8.hpp:318
std::string sanitizeSingleLine(std::string_view src)
Sanitize a pasted string for single-line text fields.
Definition Utf8.hpp:497
size_t prevWordBoundary(std::string_view s, size_t pos) noexcept
Move backward to the start of the previous word (Ctrl+Left / Ctrl+Backspace).
Definition Utf8.hpp:444
uint32_t next(std::string_view s, size_t &pos) noexcept
Decode one UTF-8 codepoint at pos and advance pos past it.
Definition Utf8.hpp:175
size_t nextCluster(std::string_view s, size_t pos) noexcept
Advance pos by one grapheme cluster.
Definition Utf8.hpp:268
size_t encode(uint32_t cp, std::string &out)
Append the UTF-8 encoding of cp to out.
Definition Utf8.hpp:60
size_t snapCeil(std::string_view s, size_t pos) noexcept
Clamp pos to s.size() and snap up to the nearest codepoint boundary.
Definition Utf8.hpp:193
size_t decode(std::string_view s, size_t pos, uint32_t &outCp) noexcept
Decode one UTF-8 codepoint starting at byte offset pos.
Definition Utf8.hpp:108
size_t snapFloor(std::string_view s, size_t pos) noexcept
Clamp pos to s.size() and snap down to the nearest codepoint boundary.
Definition Utf8.hpp:183
constexpr size_t seqLenFromLead(unsigned char b) noexcept
Expected UTF-8 byte length from a leading byte (1..4).
Definition Utf8.hpp:43
std::vector< size_t > clusterBoundaries(std::string_view s)
Return all grapheme-cluster byte boundaries in s, starting at 0 and ending at s.size().
Definition Utf8.hpp:389
size_t prevCodepoint(std::string_view s, size_t pos) noexcept
Move pos back by one UTF-8 codepoint.
Definition Utf8.hpp:212