From: Andre Noll Date: Sun, 28 Feb 2016 21:13:20 +0000 (+0100) Subject: string.c: Simplify and rename wide character helpers. X-Git-Tag: v0.5.6~23^2~9 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=5971e3303cf800603622c2475e07fdccc33e4915;ds=inline string.c: Simplify and rename wide character helpers. 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. --- diff --git a/string.c b/string.c index 701448e0..d9dcc62d 100644 --- 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; }