Created
November 6, 2019 09:47
-
-
Save tsutsui/b4bf64b17cd015c8e7cf73f860eebba8 to your computer and use it in GitHub Desktop.
more conditions to check unicode charactor width (especially emoji)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/nanotodon.c b/nanotodon.c | |
index f710d03..6a9c4e0 100755 | |
--- a/nanotodon.c | |
+++ b/nanotodon.c | |
@@ -31,15 +31,51 @@ int pad_x = 0, pad_y = 0; | |
int ustrwidth(const char *str) | |
{ | |
- int size, width; | |
+ int size, width, strwidth; | |
- width = 0; | |
+ strwidth = 0; | |
while (*str != '\0') { | |
- size = (((uint8_t)*str & 0xf0) == 0xe0) ? 3 : 1; | |
- width += (size == 1) ? 1 : 2; | |
+ uint8_t c; | |
+ c = (uint8_t)*str; | |
+ if (c >= 0x00 && c <= 0x7f) { | |
+ size = 1; | |
+ width = 1; | |
+ } else if (c >= 0xc2 && c <= 0xdf) { | |
+ size = 2; | |
+ width = 2; | |
+ } else if (c == 0xef) { | |
+ uint16_t p; | |
+ p = ((uint8_t)str[1] << 8) | (uint8_t)str[2]; | |
+ size = 3; | |
+ if (p >= 0xbda1 && p <= 0xbe9c) { | |
+ /* Halfwidth CJK punctuation */ | |
+ /* Halfwidth Katakana variants */ | |
+ /* Halfwidth Hangul variants */ | |
+ width = 1; | |
+ } else if (p >= 0xbfa8 && p <= 0xbfae) { | |
+ /* Halfwidth symbol variants */ | |
+ width = 1; | |
+ } else { | |
+ /* other BMP */ | |
+ width = 2; | |
+ } | |
+ } else if ((c & 0xf0) == 0xe0) { | |
+ /* other BMP */ | |
+ size = 3; | |
+ width = 2; | |
+ } else if ((c & 0xf8) == 0xf0) { | |
+ /* Emoji etc. */ | |
+ size = 4; | |
+ width = 2; | |
+ } else { | |
+ /* unexpected */ | |
+ size = 1; | |
+ width = 1; | |
+ } | |
+ strwidth += width; | |
str += size; | |
} | |
- return width; | |
+ return strwidth; | |
} | |
char *create_uri_string(char *api) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment