Skip to content

Commit 110656b

Browse files
John Marriottchrisbra
authored andcommitted
patch 9.1.1887: string handling in strings.c can be improved
Problem: string handling in strings.c can be improved Solution: Refactor strings.c and remove calls to STRLEN() (John Marriott) This change does: - In vim_strsave_shellescape() a small cosmetic change. - In string_count() move the call to STRLEN() outside the while loop. - In blob_from_string() refactor to remove call to STRLEN(). - In string_from_blob() call vim_strnsave() instead of vim_strsave(). - In vim_snprintf_safelen() call vim_vsnprintf_typval() directly instead of vim_vsnprintf() which then calls vim_vsnprintf_typval(). - In copy_first_char_to_tv() change to return -1 on failure or the length of resulting v_string. Change string_filter_map() and string_reduce() to use the return value of copy_first_char_to_tv(). closes: #18617 Signed-off-by: John Marriott <basilisk@internode.on.net> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 469f870 commit 110656b

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

src/strings.c

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
194194
if (*p == '\'')
195195
{
196196
if (powershell)
197-
length +=2; // ' => ''
197+
length += 2; // ' => ''
198198
else
199199
length += 3; // ' => '\''
200200
}
@@ -884,16 +884,15 @@ string_count(char_u *haystack, char_u *needle, int ic)
884884
if (p == NULL || needle == NULL || *needle == NUL)
885885
return 0;
886886

887+
size_t needlelen = STRLEN(needle);
887888
if (ic)
888889
{
889-
size_t len = STRLEN(needle);
890-
891890
while (*p != NUL)
892891
{
893-
if (MB_STRNICMP(p, needle, len) == 0)
892+
if (MB_STRNICMP(p, needle, needlelen) == 0)
894893
{
895894
++n;
896-
p += len;
895+
p += needlelen;
897896
}
898897
else
899898
MB_PTR_ADV(p);
@@ -903,15 +902,15 @@ string_count(char_u *haystack, char_u *needle, int ic)
903902
while ((next = (char_u *)strstr((char *)p, (char *)needle)) != NULL)
904903
{
905904
++n;
906-
p = next + STRLEN(needle);
905+
p = next + needlelen;
907906
}
908907

909908
return n;
910909
}
911910

912911
/*
913912
* Make a typval_T of the first character of "input" and store it in "output".
914-
* Return OK or FAIL.
913+
* Return -1 on failure or v_string's length on success.
915914
*/
916915
static int
917916
copy_first_char_to_tv(char_u *input, typval_T *output)
@@ -920,15 +919,15 @@ copy_first_char_to_tv(char_u *input, typval_T *output)
920919
int len;
921920

922921
if (input == NULL || output == NULL)
923-
return FAIL;
922+
return -1;
924923

925924
len = has_mbyte ? mb_ptr2len(input) : 1;
926925
STRNCPY(buf, input, len);
927926
buf[len] = NUL;
928927
output->v_type = VAR_STRING;
929-
output->vval.v_string = vim_strsave(buf);
928+
output->vval.v_string = vim_strnsave(buf, len);
930929

931-
return output->vval.v_string == NULL ? FAIL : OK;
930+
return output->vval.v_string == NULL ? -1 : len;
932931
}
933932

934933
/*
@@ -963,9 +962,9 @@ string_filter_map(
963962
ga_init2(&ga, sizeof(char), 80);
964963
for (p = str; *p != NUL; p += len)
965964
{
966-
if (copy_first_char_to_tv(p, &tv) == FAIL)
965+
len = copy_first_char_to_tv(p, &tv);
966+
if (len < 0)
967967
break;
968-
len = (int)STRLEN(tv.vval.v_string);
969968

970969
set_vim_var_nr(VV_KEY, idx);
971970
if (filter_map_one(&tv, expr, filtermap, fc, &newtv, &rem) == FAIL
@@ -1026,9 +1025,10 @@ string_reduce(
10261025
semsg(_(e_reduce_of_an_empty_str_with_no_initial_value), "String");
10271026
return;
10281027
}
1029-
if (copy_first_char_to_tv(p, rettv) == FAIL)
1028+
len = copy_first_char_to_tv(p, rettv);
1029+
if (len < 0)
10301030
return;
1031-
p += STRLEN(rettv->vval.v_string);
1031+
p += len;
10321032
}
10331033
else if (check_for_string_arg(argvars, 2) == FAIL)
10341034
return;
@@ -1041,9 +1041,9 @@ string_reduce(
10411041
for ( ; *p != NUL; p += len)
10421042
{
10431043
argv[0] = *rettv;
1044-
if (copy_first_char_to_tv(p, &argv[1]) == FAIL)
1044+
len = copy_first_char_to_tv(p, &argv[1]);
1045+
if (len < 0)
10451046
break;
1046-
len = (int)STRLEN(argv[1].vval.v_string);
10471047

10481048
r = eval_expr_typval(expr, TRUE, argv, 2, fc, rettv);
10491049

@@ -1221,17 +1221,12 @@ convert_string(char_u *str, char_u *from, char_u *to)
12211221
static void
12221222
blob_from_string(char_u *str, blob_T *blob)
12231223
{
1224-
size_t len = STRLEN(str);
1224+
char_u *p;
12251225

1226-
for (size_t i = 0; i < len; i++)
1226+
for (p = str; *p != NUL; ++p)
12271227
{
1228-
int ch = str[i];
1229-
1230-
if (str[i] == NL)
1231-
// Translate newlines in the string to NUL character
1232-
ch = NUL;
1233-
1234-
ga_append(&blob->bv_ga, ch);
1228+
// Translate newlines in the string to NUL character
1229+
ga_append(&blob->bv_ga, (*p == NL) ? NUL : (int)*p);
12351230
}
12361231
}
12371232

@@ -1267,9 +1262,7 @@ string_from_blob(blob_T *blob, long *start_idx)
12671262
ga_append(&str_ga, byte);
12681263
}
12691264

1270-
ga_append(&str_ga, NUL);
1271-
1272-
char_u *ret_str = vim_strsave(str_ga.ga_data);
1265+
char_u *ret_str = vim_strnsave(str_ga.ga_data, str_ga.ga_len);
12731266
*start_idx = idx;
12741267

12751268
ga_clear(&str_ga);
@@ -2648,7 +2641,7 @@ vim_snprintf_safelen(char *str, size_t str_m, const char *fmt, ...)
26482641
int str_l;
26492642

26502643
va_start(ap, fmt);
2651-
str_l = vim_vsnprintf(str, str_m, fmt, ap);
2644+
str_l = vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
26522645
va_end(ap);
26532646

26542647
if (str_l < 0)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ static char *(features[]) =
729729

730730
static int included_patches[] =
731731
{ /* Add new patch number below this line */
732+
/**/
733+
1887,
732734
/**/
733735
1886,
734736
/**/

0 commit comments

Comments
 (0)