diff options
author | Nicolas Hillegeer <nicolas@hillegeer.com> | 2014-05-29 10:13:08 +0200 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-06-08 12:48:22 -0400 |
commit | 46e4bc04819935327636e918cc8fdd2f8b3c9ddf (patch) | |
tree | abafb882fb6c6ad45723569b5e4872d7add1bbb9 | |
parent | 63a956112aefc18de86197edc05a76eae0da024a (diff) | |
download | rneovim-46e4bc04819935327636e918cc8fdd2f8b3c9ddf.tar.gz rneovim-46e4bc04819935327636e918cc8fdd2f8b3c9ddf.tar.bz2 rneovim-46e4bc04819935327636e918cc8fdd2f8b3c9ddf.zip |
text: remove useless arg from mb_string2cells
mb_string2cells was always called like mb_string2cells(..., -1) so that was
the only codepath that was tested. @tarruda was the first to try to input an
actual length, after which valgrind detected that funny business was going
on.
It's not even possible to do the right thing with the current text codec
infrastructure: they all assume to be working with C strings. Meaning that
if there is no NUL-terminator, they will happily keep on reading past the
end of Pascal strings. Ergo, passing the length parameter is moot. The
condition in the for-loop was wrong as well (but that's no longer relevant).
Also change the return value to size_t, by analogy with strlen.
ref:
https://github.com/neovim/neovim/commit/677d30d7966dd2766bbf20665791c568dacc427a
-rw-r--r-- | src/nvim/api/vim.c | 2 | ||||
-rw-r--r-- | src/nvim/eval.c | 4 | ||||
-rw-r--r-- | src/nvim/mbyte.c | 22 | ||||
-rw-r--r-- | src/nvim/message.c | 2 | ||||
-rw-r--r-- | src/nvim/screen.c | 2 |
5 files changed, 16 insertions, 16 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 59f4721da2..f07bfb196e 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -90,7 +90,7 @@ Integer vim_strwidth(String str, Error *err) } char *buf = xstrndup(str.data, str.size); - Integer rv = mb_string2cells((char_u *)buf, -1); + Integer rv = (Integer) mb_string2cells((char_u *) buf); free(buf); return rv; } diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 31070c1118..c209f5c4af 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -13696,9 +13696,7 @@ static void f_strwidth(typval_T *argvars, typval_T *rettv) { char_u *s = get_tv_string(&argvars[0]); - rettv->vval.v_number = (varnumber_T)( - mb_string2cells(s, -1) - ); + rettv->vval.v_number = (varnumber_T) mb_string2cells(s); } /* diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index 8a669b0bc8..899b94e0fb 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -1305,17 +1305,19 @@ static int dbcs_char2cells(int c) return MB_BYTE2LEN((unsigned)c >> 8); } -/* - * Return the number of cells occupied by string "p". - * Stop at a NUL character. When "len" >= 0 stop at character "p[len]". - */ -int mb_string2cells(const char_u *p, int len) +/// Calculate the number of cells occupied by string `str`. +/// +/// @param str The source string, may not be NULL, must be a NUL-terminated +/// string. +/// @return The number of cells occupied by string `str` +size_t mb_string2cells(const char_u *str) { - int i; - int clen = 0; + size_t clen = 0; + + for (const char_u *p = str; *p != NUL; p += (*mb_ptr2len)(p)) { + clen += (*mb_ptr2cells)(p); + } - for (i = 0; (len < 0 || i < len) && p[i] != NUL; i += (*mb_ptr2len)(p + i)) - clen += (*mb_ptr2cells)(p + i); return clen; } @@ -2953,7 +2955,7 @@ int utf_head_off(const char_u *base, const char_u *p) for (q = p;; --q) { /* Move s to the last byte of this char. */ const char_u *s; - for (s = q; (s[1] & 0xc0) == 0x80; ++s); + for (s = q; (s[1] & 0xc0) == 0x80; ++s) {} /* Move q to the first byte of this char. */ while (q > base && (*q & 0xc0) == 0x80) diff --git a/src/nvim/message.c b/src/nvim/message.c index 41a2345171..96be3bb17d 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -3352,7 +3352,7 @@ int vim_vsnprintf(char *str, size_t str_m, char *fmt, va_list ap, typval_T *tvs) if (fmt_spec == 'S') { if (min_field_width != 0) min_field_width += STRLEN(str_arg) - - mb_string2cells((char_u *)str_arg, -1); + - mb_string2cells((char_u *) str_arg); if (precision) { char_u *p1 = (char_u *)str_arg; size_t i; diff --git a/src/nvim/screen.c b/src/nvim/screen.c index d36ce6ca2b..18b1797c36 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -4834,7 +4834,7 @@ void win_redr_status(win_T *wp) int clen = 0, i; /* Count total number of display cells. */ - clen = mb_string2cells(p, -1); + clen = (int) mb_string2cells(p); /* Find first character that will fit. * Going from start to end is much faster for DBCS. */ |