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:
677d30d796
This commit is contained in:
Nicolas Hillegeer
2014-05-29 10:13:08 +02:00
committed by Justin M. Keyes
parent 63a956112a
commit 46e4bc0481
5 changed files with 16 additions and 16 deletions

View File

@@ -90,7 +90,7 @@ Integer vim_strwidth(String str, Error *err)
} }
char *buf = xstrndup(str.data, str.size); 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); free(buf);
return rv; return rv;
} }

View File

@@ -13696,9 +13696,7 @@ static void f_strwidth(typval_T *argvars, typval_T *rettv)
{ {
char_u *s = get_tv_string(&argvars[0]); char_u *s = get_tv_string(&argvars[0]);
rettv->vval.v_number = (varnumber_T)( rettv->vval.v_number = (varnumber_T) mb_string2cells(s);
mb_string2cells(s, -1)
);
} }
/* /*

View File

@@ -1305,17 +1305,19 @@ static int dbcs_char2cells(int c)
return MB_BYTE2LEN((unsigned)c >> 8); return MB_BYTE2LEN((unsigned)c >> 8);
} }
/* /// Calculate the number of cells occupied by string `str`.
* Return the number of cells occupied by string "p". ///
* Stop at a NUL character. When "len" >= 0 stop at character "p[len]". /// @param str The source string, may not be NULL, must be a NUL-terminated
*/ /// string.
int mb_string2cells(const char_u *p, int len) /// @return The number of cells occupied by string `str`
size_t mb_string2cells(const char_u *str)
{ {
int i; size_t clen = 0;
int 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; return clen;
} }
@@ -2953,7 +2955,7 @@ int utf_head_off(const char_u *base, const char_u *p)
for (q = p;; --q) { for (q = p;; --q) {
/* Move s to the last byte of this char. */ /* Move s to the last byte of this char. */
const char_u *s; 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. */ /* Move q to the first byte of this char. */
while (q > base && (*q & 0xc0) == 0x80) while (q > base && (*q & 0xc0) == 0x80)

View File

@@ -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 (fmt_spec == 'S') {
if (min_field_width != 0) if (min_field_width != 0)
min_field_width += STRLEN(str_arg) min_field_width += STRLEN(str_arg)
- mb_string2cells((char_u *)str_arg, -1); - mb_string2cells((char_u *) str_arg);
if (precision) { if (precision) {
char_u *p1 = (char_u *)str_arg; char_u *p1 = (char_u *)str_arg;
size_t i; size_t i;

View File

@@ -4834,7 +4834,7 @@ void win_redr_status(win_T *wp)
int clen = 0, i; int clen = 0, i;
/* Count total number of display cells. */ /* Count total number of display cells. */
clen = mb_string2cells(p, -1); clen = (int) mb_string2cells(p);
/* Find first character that will fit. /* Find first character that will fit.
* Going from start to end is much faster for DBCS. */ * Going from start to end is much faster for DBCS. */