mirror of
https://github.com/gbdev/rgbds.git
synced 2026-08-23 07:14:31 +00:00
Encapuslate UTF-8 decoder state in a struct
This commit is contained in:
+7
-6
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user