commit c70e8ef9b7fcfefcfb47f95af4589b91d042108a
parent f3b38b46ebbe34f5a56ca166da9cdb27d4827724
Author: zeertzjq <zeertzjq@outlook.com>
Date: Thu, 30 Oct 2025 21:31:27 +0800
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
https://github.com/vim/vim/commit/110656ba607d22bbd6b1b25f0c05a1f7bad205c0
Co-authored-by: John Marriott <basilisk@internode.on.net>
Diffstat:
2 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/src/nvim/eval/list.c b/src/nvim/eval/list.c
@@ -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;
}
}
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
@@ -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) {