7 const auto lead =
static_cast<std::uint8_t
>(
text[
index]);
13 std::size_t continuation = 0;
14 std::uint32_t codepoint = 0;
15 if ((lead & 0xe0u) == 0xc0u) {
17 codepoint = lead & 0x1fu;
18 }
else if ((lead & 0xf0u) == 0xe0u) {
20 codepoint = lead & 0x0fu;
21 }
else if ((lead & 0xf8u) == 0xf0u) {
23 codepoint = lead & 0x07u;
27 if (continuation >=
text.size() -
index)
return false;
28 for (std::size_t part = 1; part <= continuation; ++part) {
29 const auto byte =
static_cast<std::uint8_t
>(
text[
index + part]);
30 if ((
byte & 0xc0u) != 0x80u)
return false;
31 codepoint = (codepoint << 6u) | (
byte & 0x3fu);
33 if ((continuation == 1 && codepoint < 0x80) ||
34 (continuation == 2 && codepoint < 0x800) ||
35 (continuation == 3 && codepoint < 0x10000) || codepoint > 0x10ffff ||
36 (codepoint >= 0xd800 && codepoint <= 0xdfff))
38 index += continuation + 1;
bool isValidUtf8(std::string_view text, Utf8NullPolicy nullPolicy) noexcept
Validate shortest-form UTF-8, Unicode scalar range and optional NUL exclusion.