vim-patch:partial: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: vim/vim#18617

110656ba60

Co-authored-by: John Marriott <basilisk@internode.on.net>
This commit is contained in:
zeertzjq
2025-10-30 21:31:27 +08:00
parent f3b38b46eb
commit c70e8ef9b7
2 changed files with 5 additions and 6 deletions

View File

@@ -465,13 +465,12 @@ static varnumber_T count_string(const char *haystack, const char *needle, bool i
return 0;
}
size_t needlelen = strlen(needle);
if (ic) {
const size_t len = strlen(needle);
while (*p != NUL) {
if (mb_strnicmp(p, needle, len) == 0) {
if (mb_strnicmp(p, needle, needlelen) == 0) {
n++;
p += len;
p += needlelen;
} else {
MB_PTR_ADV(p);
}
@@ -480,7 +479,7 @@ static varnumber_T count_string(const char *haystack, const char *needle, bool i
const char *next;
while ((next = strstr(p, needle)) != NULL) {
n++;
p = next + strlen(needle);
p = next + needlelen;
}
}

View File

@@ -804,7 +804,7 @@ size_t vim_snprintf_safelen(char *str, size_t str_m, const char *fmt, ...)
int str_l;
va_start(ap, fmt);
str_l = vim_vsnprintf(str, str_m, fmt, ap);
str_l = vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
va_end(ap);
if (str_l < 0) {