0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 9.1.0461: too many strlen() calls in drawline.c

Problem:  too many strlen() calls in drawline.c
Solution: Refactor code to avoid strlen()
          (John Marriott)

closes: #14890

Signed-off-by: John Marriott <basilisk@internode.on.net>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
John Marriott 2024-06-02 19:44:11 +02:00 committed by Christian Brabandt
parent 7577afd5ef
commit f51ff96532
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
2 changed files with 35 additions and 27 deletions

View File

@ -296,10 +296,9 @@ get_sign_display_info(
if (nrcol) if (nrcol)
{ {
wlv->c_extra = NUL; wlv->c_extra = NUL;
sprintf((char *)wlv->extra, "%-*c ", wlv->n_extra = vim_snprintf((char *)wlv->extra, sizeof(wlv->extra),
number_width(wp), SIGN_BYTE); "%-*c ", number_width(wp), SIGN_BYTE);
wlv->p_extra = wlv->extra; wlv->p_extra = wlv->extra;
wlv->n_extra = (int)STRLEN(wlv->p_extra);
} }
else else
wlv->c_extra = SIGN_BYTE; wlv->c_extra = SIGN_BYTE;
@ -310,10 +309,9 @@ get_sign_display_info(
if (nrcol) if (nrcol)
{ {
wlv->c_extra = NUL; wlv->c_extra = NUL;
sprintf((char *)wlv->extra, "%-*c ", number_width(wp), wlv->n_extra = vim_snprintf((char *)wlv->extra, sizeof(wlv->extra),
MULTISIGN_BYTE); "%-*c ", number_width(wp), MULTISIGN_BYTE);
wlv->p_extra = wlv->extra; wlv->p_extra = wlv->extra;
wlv->n_extra = (int)STRLEN(wlv->p_extra);
} }
else else
wlv->c_extra = MULTISIGN_BYTE; wlv->c_extra = MULTISIGN_BYTE;
@ -332,17 +330,18 @@ get_sign_display_info(
if (nrcol) if (nrcol)
{ {
int width = number_width(wp) - 2; int width = number_width(wp) - 2;
int n;
for (n = 0; n < width; n++) vim_memset(wlv->extra, ' ', width);
wlv->extra[n] = ' '; wlv->n_extra = width;
vim_snprintf((char *)wlv->extra + n, wlv->n_extra += vim_snprintf((char *)wlv->extra + width,
sizeof(wlv->extra) - n, "%s ", wlv->p_extra); sizeof(wlv->extra) - width, "%s ", wlv->p_extra);
wlv->p_extra = wlv->extra; wlv->p_extra = wlv->extra;
} }
else
wlv->n_extra = (int)STRLEN(wlv->p_extra);
wlv->c_extra = NUL; wlv->c_extra = NUL;
wlv->c_final = NUL; wlv->c_final = NUL;
wlv->n_extra = (int)STRLEN(wlv->p_extra);
} }
if (use_cursor_line_highlight(wp, wlv->lnum) if (use_cursor_line_highlight(wp, wlv->lnum)
@ -417,7 +416,7 @@ handle_lnum_col(
} }
} }
sprintf((char *)wlv->extra, fmt, number_width(wp), num); vim_snprintf((char *)wlv->extra, sizeof(wlv->extra), fmt, number_width(wp), num);
if (wp->w_skipcol > 0 && wlv->startrow == 0) if (wp->w_skipcol > 0 && wlv->startrow == 0)
for (wlv->p_extra = wlv->extra; *wlv->p_extra == ' '; for (wlv->p_extra = wlv->extra; *wlv->p_extra == ' ';
++wlv->p_extra) ++wlv->p_extra)
@ -621,25 +620,26 @@ textprop_size_after_trunc(
{ {
int space = (flags & (TP_FLAG_ALIGN_BELOW | TP_FLAG_ALIGN_ABOVE)) int space = (flags & (TP_FLAG_ALIGN_BELOW | TP_FLAG_ALIGN_ABOVE))
? wp->w_width - win_col_off(wp) : added; ? wp->w_width - win_col_off(wp) : added;
int len = (int)STRLEN(text);
int strsize = 0; int strsize = 0;
int n_used; char_u *p;
// if the remaining size is to small and 'wrap' is set we wrap anyway and // if the remaining size is too small and 'wrap' is set we wrap anyway and
// use the next line // use the next line
if (space < PROP_TEXT_MIN_CELLS && wp->w_p_wrap) if (space < PROP_TEXT_MIN_CELLS && wp->w_p_wrap)
space += wp->w_width; space += wp->w_width;
if (flags & (TP_FLAG_ALIGN_BELOW | TP_FLAG_ALIGN_ABOVE)) if (flags & (TP_FLAG_ALIGN_BELOW | TP_FLAG_ALIGN_ABOVE))
space -= padding; space -= padding;
for (n_used = 0; n_used < len; n_used += (*mb_ptr2len)(text + n_used))
for (p = text; *p != NUL; p += (*mb_ptr2len)(p))
{ {
int clen = ptr2cells(text + n_used); int clen = ptr2cells(p);
if (strsize + clen > space) if (strsize + clen > space)
break; break;
strsize += clen; strsize += clen;
} }
*n_used_ptr = n_used; *n_used_ptr = (int)(p - text);
return strsize; return strsize;
} }
@ -1807,7 +1807,7 @@ win_line(
line = ml_get_buf(wp->w_buffer, lnum, FALSE); line = ml_get_buf(wp->w_buffer, lnum, FALSE);
ptr = line + linecol; ptr = line + linecol;
if (len == 0 || (int)wp->w_cursor.col > ptr - line) if (len == 0 || (int)wp->w_cursor.col > linecol)
{ {
// no bad word found at line start, don't check until end of a // no bad word found at line start, don't check until end of a
// word // word
@ -2821,15 +2821,16 @@ win_line(
// head byte at end of line // head byte at end of line
mb_l = 1; mb_l = 1;
transchar_nonprint(wp->w_buffer, wlv.extra, c); transchar_nonprint(wp->w_buffer, wlv.extra, c);
wlv.n_extra = (int)STRLEN(wlv.extra) - 1;
} }
else else
{ {
// illegal tail byte // illegal tail byte
mb_l = 2; mb_l = 2;
STRCPY(wlv.extra, "XX"); STRCPY(wlv.extra, "XX");
wlv.n_extra = 1;
} }
wlv.p_extra = wlv.extra; wlv.p_extra = wlv.extra;
wlv.n_extra = (int)STRLEN(wlv.extra) - 1;
wlv.c_extra = NUL; wlv.c_extra = NUL;
wlv.c_final = NUL; wlv.c_final = NUL;
c = *wlv.p_extra++; c = *wlv.p_extra++;
@ -3388,11 +3389,16 @@ win_line(
c = *wlv.p_extra; c = *wlv.p_extra;
p = alloc(wlv.n_extra + 1); p = alloc(wlv.n_extra + 1);
vim_memset(p, ' ', wlv.n_extra); if (p == NULL)
STRNCPY(p, wlv.p_extra + 1, STRLEN(wlv.p_extra) - 1); wlv.n_extra = 0;
p[wlv.n_extra] = NUL; else
vim_free(wlv.p_extra_free); {
wlv.p_extra_free = wlv.p_extra = p; vim_memset(p, ' ', wlv.n_extra);
STRNCPY(p, wlv.p_extra + 1, STRLEN(wlv.p_extra) - 1);
p[wlv.n_extra] = NUL;
vim_free(wlv.p_extra_free);
wlv.p_extra_free = wlv.p_extra = p;
}
} }
else else
#endif #endif
@ -4251,7 +4257,7 @@ win_line(
if (!wp->w_p_wrap && text_prop_follows && !text_prop_above) if (!wp->w_p_wrap && text_prop_follows && !text_prop_above)
{ {
// do not output more of the line, only the "below" prop // do not output more of the line, only the "below" prop
ptr += STRLEN(ptr); ptr = line + (size_t)ml_get_buf_len(wp->w_buffer, lnum);
# ifdef FEAT_LINEBREAK # ifdef FEAT_LINEBREAK
wlv.dont_use_showbreak = TRUE; wlv.dont_use_showbreak = TRUE;
# endif # endif

View File

@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
461,
/**/ /**/
460, 460,
/**/ /**/