Skip to content

Instantly share code, notes, and snippets.

@adsr
Created February 6, 2024 05:38
Show Gist options
  • Save adsr/55b9e98051b5383b2144ff1114a4d312 to your computer and use it in GitHub Desktop.
Save adsr/55b9e98051b5383b2144ff1114a4d312 to your computer and use it in GitHub Desktop.
diff --git a/termbox2.h b/termbox2.h
index a716dbd..0ec61e1 100644
--- a/termbox2.h
+++ b/termbox2.h
@@ -1816,7 +1816,12 @@ int tb_print_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
*out_w = 0;
}
while (*str) {
- str += tb_utf8_char_to_unicode(&uni, str);
+ if ((rv = tb_utf8_char_to_unicode(&uni, str)) == TB_ERR) {
+ uni = (uint32_t)*str;
+ str += 1;
+ } else {
+ str += rv;
+ }
w = wcwidth((wchar_t)uni);
if (w < 0) {
w = 1;
@@ -1893,7 +1898,7 @@ int tb_utf8_char_length(char c) {
}
int tb_utf8_char_to_unicode(uint32_t *out, const char *c) {
- if (*c == 0) {
+ if (*c == '\0') {
return TB_ERR;
}
@@ -1901,11 +1906,15 @@ int tb_utf8_char_to_unicode(uint32_t *out, const char *c) {
unsigned char len = tb_utf8_char_length(*c);
unsigned char mask = utf8_mask[len - 1];
uint32_t result = c[0] & mask;
- for (i = 1; i < len; ++i) {
+ for (i = 1; i < len && c[i] != '\0'; ++i) {
result <<= 6;
result |= c[i] & 0x3f;
}
+ if (i != len) {
+ return TB_ERR;
+ }
+
*out = result;
return (int)len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment