diff options
author | Kevin McCarthy <kevin@8t8.us> | 2016-03-22 18:00:13 -0700 |
---|---|---|
committer | Kevin McCarthy <kevin@8t8.us> | 2016-03-22 18:00:13 -0700 |
commit | e27a5e2bfde9ee052291a02748e479e9648113c1 (patch) | |
tree | 2621c028aed1de97a1318be6f5cfd91950526018 /curs_lib.c | |
parent | 880750dd25bc783b7a4bba8d29cafa5ac2b0f55d (diff) |
Clean up mutt_wstr_trunc() some more.
* Change return type to size_t.
The return value is the cumulation of values from mbrtowc(), which
returns size_t. All callers already assign the return value to a
size_t, requiring no external changes.
* Change the local variables n, w, l, and cl to size_t.
n is the strlen of the src parameter. l and cl are used for the
return value. w is assigned to the *width parameter, which is
size_t.
cw is kept as an int, because wcwidth returns type int.
* Change error handling of mbrtowc to be the same as other functions
in mutt: only reset mbstate when the retval==-1. When retvat==-2,
set cl=n to break out of the loop. Also, set wc to replacement_char
and allow the logic below to determine the width instead of
hardcoding to 1.
Diffstat (limited to 'curs_lib.c')
-rw-r--r-- | curs_lib.c | 30 |
1 files changed, 15 insertions, 15 deletions
@@ -949,11 +949,11 @@ void mutt_paddstr (int n, const char *s) /* See how many bytes to copy from string so its at most maxlen bytes * long and maxwid columns wide */ -int mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *width) +size_t mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *width) { wchar_t wc; - int w = 0, l = 0, cl; - int cw, n; + size_t n, w = 0, l = 0, cl; + int cw; mbstate_t mbstate; if (!src) @@ -964,20 +964,20 @@ int mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *widt memset (&mbstate, 0, sizeof (mbstate)); for (w = 0; n && (cl = mbrtowc (&wc, src, n, &mbstate)); src += cl, n -= cl) { - if (cl == (size_t)(-1) || cl == (size_t)(-2)) { - cw = cl = 1; - memset(&mbstate, 0, sizeof (mbstate)); - } - else + if (cl == (size_t)(-1) || cl == (size_t)(-2)) { - cw = wcwidth (wc); - /* hack because M_TREE symbols aren't turned into characters - * until rendered by print_enriched_string (#3364) */ - if (cw < 0 && cl == 1 && src[0] && src[0] < M_TREE_MAX) - cw = 1; - else if (cw < 0) - cw = 0; /* unprintable wchar */ + if (cl == (size_t)(-1)) + memset (&mbstate, 0, sizeof (mbstate)); + cl = (cl == (size_t)(-1)) ? 1 : n; + wc = replacement_char (); } + cw = wcwidth (wc); + /* hack because M_TREE symbols aren't turned into characters + * until rendered by print_enriched_string (#3364) */ + if (cw < 0 && cl == 1 && src[0] && src[0] < M_TREE_MAX) + cw = 1; + else if (cw < 0) + cw = 0; /* unprintable wchar */ if (cl + l > maxlen || cw + w > maxwid) break; l += cl; |