jonas / tig

Text-mode interface for git
https://jonas.github.io/tig/
GNU General Public License v2.0
12.49k stars 611 forks source link

Fix display non-ascii character. #1299

Closed nonakap closed 1 year ago

nonakap commented 1 year ago

Should cast the argument passed to is*() function in ctype.h to unsigned char.

See https://wiki.sei.cmu.edu/confluence/display/c/STR37-C.+Arguments+to+character-handling+functions+must+be+representable+as+an+unsigned+char.

Should fix https://github.com/jonas/tig/issues/1294.

before: The commit log contains Japanese characters, but they are not displayed correctly. tig-display-broken-non-ascii-character

after: The commit log is displayed correctly. tig-fix-display-non-ascii-character

koutcher commented 1 year ago

Thank you @nonakap. Your proposed correction has the exact opposite effect on macOS, it works without and it doesn't work with, but it certainly goes in the right direction. Could you try the following ?

diff --git a/src/string.c b/src/string.c
index a90fc1bb..4cb0910b 100644
--- a/src/string.c
+++ b/src/string.c
@@ -103,7 +103,7 @@ string_expand(char *dst, size_t dstlen, const char *src, int srclen, int tabsize
                                expanded = dstlen - size - 1;
                        memcpy(dst + size, "        ", expanded);
                        size += expanded;
-               } else if (isspace((unsigned char)c) || iscntrl((unsigned char)c)) {
+               } else if (iswspace(c) || iswcntrl(c)) {
                        dst[size++] = ' ';
                } else {
                        dst[size++] = src[pos];
@@ -119,7 +119,7 @@ string_trim_end(char *name)
 {
        int namelen = strlen(name) - 1;

-       while (namelen > 0 && isspace((unsigned char)name[namelen]))
+       while (namelen > 0 && iswspace(name[namelen]))
                name[namelen--] = 0;

        return name;
@@ -128,7 +128,7 @@ string_trim_end(char *name)
 char *
 string_trim(char *name)
 {
-       while (isspace((unsigned char)*name))
+       while (iswspace(*name))
                name++;

        return string_trim_end(name);

EDIT: actually this is a macOS thing only, we'll handle it with a specific case.