fix a utf8 bug

This commit is contained in:
blueloveTH 2024-12-18 18:52:11 +08:00
parent e733d7cd23
commit a9a4ef6dda

View File

@ -98,7 +98,10 @@ void c11_sbuf__write_quoted(c11_sbuf* self, c11_sv sv, char quote) {
case '\r': c11_sbuf__write_cstrn(self, "\\r", 2); break; case '\r': c11_sbuf__write_cstrn(self, "\\r", 2); break;
case '\t': c11_sbuf__write_cstrn(self, "\\t", 2); break; case '\t': c11_sbuf__write_cstrn(self, "\\t", 2); break;
case '\b': c11_sbuf__write_cstrn(self, "\\b", 2); break; case '\b': c11_sbuf__write_cstrn(self, "\\b", 2); break;
default: default: {
int u8bytes = c11__u8_header(c, true);
if(u8bytes <= 1) {
// not a valid utf8 char, or ascii
if(!isprint(c)) { if(!isprint(c)) {
unsigned char uc = (unsigned char)c; unsigned char uc = (unsigned char)c;
c11_sbuf__write_cstrn(self, "\\x", 2); c11_sbuf__write_cstrn(self, "\\x", 2);
@ -107,6 +110,14 @@ void c11_sbuf__write_quoted(c11_sbuf* self, c11_sv sv, char quote) {
} else { } else {
c11_sbuf__write_char(self, c); c11_sbuf__write_char(self, c);
} }
} else {
for(int j = 0; j < u8bytes; j++) {
c11_sbuf__write_char(self, sv.data[i + j]);
}
i += u8bytes - 1;
}
break;
}
} }
} }
c11_sbuf__write_char(self, quote); c11_sbuf__write_char(self, quote);