Encapuslate UTF-8 decoder state in a struct

This commit is contained in:
Rangi
2026-07-03 19:35:23 -04:00
parent e3c594c250
commit 0996a2f5ed
5 changed files with 36 additions and 34 deletions
+7 -6
View File
@@ -275,23 +275,24 @@ static void writeSymName(std::string const &name, FILE *file) {
// Output illegal characters using Unicode escapes ('\u' or '\U')
// Decode the UTF-8 codepoint; or at least attempt to
uint32_t state = UTF8_ACCEPT, codepoint;
Utf8Decoder decoder;
do {
decode(&state, &codepoint, *ptr);
if (state != UTF8_REJECT) {
if (decoder.update(*ptr) != UTF8_REJECT) {
++ptr;
continue;
}
// This sequence was invalid; emit a U+FFFD, and recover
codepoint = 0xFFFD;
decoder.codepoint = 0xFFFD;
// Skip continuation bytes
// A NUL byte does not qualify, so we're good
while ((*ptr & 0xC0) == 0x80) {
++ptr;
}
break;
} while (state != UTF8_ACCEPT);
fprintf(file, codepoint <= 0xFFFF ? "\\u%04" PRIx32 : "\\U%08" PRIx32, codepoint);
} while (decoder.state != UTF8_ACCEPT);
fprintf(
file, decoder.codepoint <= 0xFFFF ? "\\u%04" PRIx32 : "\\U%08" PRIx32, decoder.codepoint
);
}
}