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 /src/nvim/mbyte.c | |
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
Diffstat (limited to 'src/nvim/mbyte.c')
-rw-r--r-- | src/nvim/mbyte.c | 22 |
1 files changed, 12 insertions, 10 deletions
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) |