From e27a5e2bfde9ee052291a02748e479e9648113c1 Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Tue, 22 Mar 2016 18:00:13 -0700 Subject: [PATCH] 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. --- curs_lib.c | 30 +++++++++++++++--------------- protos.h | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/curs_lib.c b/curs_lib.c index b400db0a..3178f221 100644 --- a/curs_lib.c +++ b/curs_lib.c @@ -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; diff --git a/protos.h b/protos.h index 33e514f7..8e5f7aa9 100644 --- a/protos.h +++ b/protos.h @@ -356,7 +356,7 @@ int mutt_search_command (int, int); int mutt_smtp_send (const ADDRESS *, const ADDRESS *, const ADDRESS *, const ADDRESS *, const char *, int); #endif -int mutt_wstr_trunc (const char *, size_t, size_t, size_t *); +size_t mutt_wstr_trunc (const char *, size_t, size_t, size_t *); int mutt_charlen (const char *s, int *); int mutt_strwidth (const char *); int mutt_compose_menu (HEADER *, char *, size_t, HEADER *, int); -- 2.40.0