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,15 +98,26 @@ 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: {
if(!isprint(c)) { int u8bytes = c11__u8_header(c, true);
unsigned char uc = (unsigned char)c; if(u8bytes <= 1) {
c11_sbuf__write_cstrn(self, "\\x", 2); // not a valid utf8 char, or ascii
c11_sbuf__write_char(self, PK_HEX_TABLE[uc >> 4]); if(!isprint(c)) {
c11_sbuf__write_char(self, PK_HEX_TABLE[uc & 0xf]); unsigned char uc = (unsigned char)c;
c11_sbuf__write_cstrn(self, "\\x", 2);
c11_sbuf__write_char(self, PK_HEX_TABLE[uc >> 4]);
c11_sbuf__write_char(self, PK_HEX_TABLE[uc & 0xf]);
} else {
c11_sbuf__write_char(self, c);
}
} else { } else {
c11_sbuf__write_char(self, c); 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);