]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
string.c: Simplify and rename wide character helpers.
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 28 Feb 2016 21:13:20 +0000 (22:13 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 13 Mar 2016 17:11:32 +0000 (18:11 +0100)
Just assume non-printable characters are one character wide. This gives
a more reasonable approximation than the hack we have now.

This patch also renames mutt_wcwidth() to xwcwidth() and
mutt_wcswidth() to xwcswidth() since these functions have nothing in
common with mutt any more.

string.c

index 701448e041cfa9ff86dc1a6cccf37cd088b76c20..d9dcc62d2ea45efc6a3c2899185a2033d5e74339 100644 (file)
--- a/string.c
+++ b/string.c
@@ -955,36 +955,24 @@ static bool utf8_mode(void)
        return have_utf8;
 }
 
-/*
- * glibc's wcswidth returns -1 if the string contains a tab character, which
- * makes the function next to useless. The two functions below are taken from
- * mutt.
- */
-
-#define IsWPrint(wc) (iswprint(wc) || wc >= 0xa0)
-
-static int mutt_wcwidth(wchar_t wc, size_t pos)
+static int xwcwidth(wchar_t wc, size_t pos)
 {
        int n;
 
+       /* special-case for tab */
        if (wc == 0x09) /* tab */
                return (pos | 7) + 1 - pos;
        n = wcwidth(wc);
-       if (IsWPrint(wc) && n > 0)
-               return n;
-       if (!(wc & ~0x7f))
-               return 2;
-       if (!(wc & ~0xffff))
-               return 6;
-       return 10;
+       /* wcswidth() returns -1 for non-printable characters */
+       return n >= 0? n : 1;
 }
 
-static size_t mutt_wcswidth(const wchar_t *s, size_t n)
+static size_t xwcswidth(const wchar_t *s, size_t n)
 {
        size_t w = 0;
 
        while (n--)
-               w += mutt_wcwidth(*s++, w);
+               w += xwcwidth(*s++, w);
        return w;
 }
 
@@ -1029,7 +1017,7 @@ int skip_cells(const char *s, size_t cells_to_skip, size_t *bytes_to_skip)
                if (mbret == (size_t)-1 || mbret == (size_t)-2)
                        return -ERRNO_TO_PARA_ERROR(EILSEQ);
                bytes_parsed += mbret;
-               cells_skipped += mutt_wcwidth(wc, cells_skipped);
+               cells_skipped += xwcwidth(wc, cells_skipped);
        }
        *bytes_to_skip = bytes_parsed;
        return 1;
@@ -1078,7 +1066,7 @@ __must_check int strwidth(const char *s, size_t *result)
        memset(&state, 0, sizeof(state));
        num_wchars = mbsrtowcs(dest, &src, num_wchars, &state);
        assert(num_wchars > 0 && num_wchars != (size_t)-1);
-       *result = mutt_wcswidth(dest, num_wchars);
+       *result = xwcswidth(dest, num_wchars);
        free(dest);
        return 1;
 }