Skip to content

Commit a21240b

Browse files
John Marriottchrisbra
authored andcommitted
patch 9.1.0997: too many strlen() calls in drawscreen.c
Problem: too many strlen() calls in drawscreen.c Solution: refactor drawscreen.c and remove calls to strlen(), make get_keymap_str() (in screen.c) return string length instead of TRUE/FALSE (John Marriott). Signed-off-by: John Marriott <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 8ab1819 commit a21240b

File tree

5 files changed

+126
-124
lines changed

5 files changed

+126
-124
lines changed

src/buffer.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3914,7 +3914,7 @@ fileinfo(
39143914
n);
39153915
validate_virtcol();
39163916
len = STRLEN(buffer);
3917-
col_print((char_u *)buffer + len, IOSIZE - len,
3917+
(void)col_print((char_u *)buffer + len, IOSIZE - len,
39183918
(int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
39193919
}
39203920

@@ -3946,17 +3946,17 @@ fileinfo(
39463946
vim_free(buffer);
39473947
}
39483948

3949-
void
3949+
int
39503950
col_print(
39513951
char_u *buf,
39523952
size_t buflen,
39533953
int col,
39543954
int vcol)
39553955
{
39563956
if (col == vcol)
3957-
vim_snprintf((char *)buf, buflen, "%d", col);
3958-
else
3959-
vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
3957+
return vim_snprintf((char *)buf, buflen, "%d", col);
3958+
3959+
return vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
39603960
}
39613961

39623962
static char_u *lasttitle = NULL;
@@ -4820,7 +4820,7 @@ build_stl_str_hl(
48204820

48214821
case STL_ALTPERCENT:
48224822
str = buf_tmp;
4823-
get_rel_pos(wp, str, TMPLEN);
4823+
(void)get_rel_pos(wp, str, TMPLEN);
48244824
break;
48254825

48264826
case STL_SHOWCMD:
@@ -4837,7 +4837,7 @@ build_stl_str_hl(
48374837

48384838
case STL_KEYMAP:
48394839
fillable = FALSE;
4840-
if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4840+
if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
48414841
str = buf_tmp;
48424842
break;
48434843
case STL_PAGENUM:
@@ -5271,17 +5271,18 @@ build_stl_str_hl(
52715271
* Get relative cursor position in window into "buf[buflen]", in the localized
52725272
* percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
52735273
*/
5274-
void
5274+
int
52755275
get_rel_pos(
52765276
win_T *wp,
52775277
char_u *buf,
52785278
int buflen)
52795279
{
52805280
long above; // number of lines above window
52815281
long below; // number of lines below window
5282+
int len;
52825283

52835284
if (buflen < 3) // need at least 3 chars for writing
5284-
return;
5285+
return 0;
52855286
above = wp->w_topline - 1;
52865287
#ifdef FEAT_DIFF
52875288
above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
@@ -5292,28 +5293,27 @@ get_rel_pos(
52925293
#endif
52935294
below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
52945295
if (below <= 0)
5295-
vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5296-
(size_t)(buflen - 1));
5296+
len = vim_snprintf((char *)buf, buflen, "%s", (above == 0) ? _("All") : _("Bot"));
52975297
else if (above <= 0)
5298-
vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
5298+
len = vim_snprintf((char *)buf, buflen, "%s", _("Top"));
52995299
else
53005300
{
53015301
int perc = (above > 1000000L)
5302-
? (int)(above / ((above + below) / 100L))
5303-
: (int)(above * 100L / (above + below));
5302+
? (int)(above / ((above + below) / 100L))
5303+
: (int)(above * 100L / (above + below));
53045304

5305-
char *p = (char *)buf;
5306-
size_t l = buflen;
5307-
if (perc < 10)
5308-
{
5309-
// prepend one space
5310-
buf[0] = ' ';
5311-
++p;
5312-
--l;
5313-
}
53145305
// localized percentage value
5315-
vim_snprintf(p, l, _("%d%%"), perc);
5306+
len = vim_snprintf((char *)buf, buflen, _("%s%d%%"), (perc < 10) ? " " : "", perc);
53165307
}
5308+
if (len < 0)
5309+
{
5310+
buf[0] = NUL;
5311+
len = 0;
5312+
}
5313+
else if (len > buflen - 1)
5314+
len = buflen - 1;
5315+
5316+
return len;
53175317
}
53185318

53195319
/*

0 commit comments

Comments
 (0)