patch 9.2.0291: too many strlen() calls

Problem:  too many strlen() calls
Solution: refactor concat_fname() and remove calls to strlen()
          (John Marriott)

Function `concat_fnames()` can make up to 5 calls to `STRLEN()` (either
directly or indirectly via `STRCAT()`). In many cases the lengths of
arguments `fname1` and/or `fname2` are either known or can simply be
calculated.

This Commit refactors this function to accept the lengths of arguments
`fname1` and `fname2` as arguments. It also adds new argument `ret` to
return the resulting string as a `string_T`.

Additionally:
- function `add_pack_dir_to_rtp()` in `scriptfile.c`:
   Use a `string_T` to store local variables `new_rtp` and `afterdir`.
   Replace calls to `STRCAT()` with calls to `STRCPY()`.
   Change type of variable `keep` to `size_t` for consistency with
   other lengths.

- function `qf_get_fnum()` in `quickfix.c`:
  Use a `string_T` to store local variables `ptr` and `bufname`
- function `qf_push_dir()` in `quickfix.c`:
  Use a `string_T` to store local variable `dirname`.
  Replace call to `vim_strsave()` with `vim_strnsave()`.

- function `qf_guess_filepath()` in `quickfix.c`:
  Use a `string_T` to store local variable `fullname`.

- function `make_percent_swname()` in `memline.c`:
  Rename some variables to better reflect their use.
  Use a `string_T` to store local variables `d` and `fixed_name`.
  Slightly refactor to remove need to create an extra string.
- function `get_file_in_dir()` in `memline.c`:
  Use a `string_T` to store local variables `tail` and `retval`.
  Move some variables closer to where they are used.

- function `cs_resolve_file()` in `if_cscope.c`:
  Use a `string_T` to store local variable `csdir`.
  Remove one call to `STRLEN()`.

- function `add_pathsep()` in `filepath.c`:
  Refactor and remove 1 call to `STRLEN()`

- function `set_init_xdg_rtp()` in `option.c`:
  Use a `string_T` to store local variable `vimrc_xdg`.

closes: #19854

Co-authored-by: Christian Brabandt <cb@256bit.org>
Signed-off-by: John Marriott <basilisk@internode.on.net>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
John Marriott
2026-04-03 15:08:48 +00:00
committed by Christian Brabandt
parent b7205b6426
commit cb51add7ae
12 changed files with 280 additions and 181 deletions
+25 -13
View File
@@ -3178,19 +3178,25 @@ vim_fnamencmp(char_u *x, char_u *y, size_t len)
* Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
*/
char_u *
concat_fnames(char_u *fname1, char_u *fname2, int sep)
concat_fnames(char_u *fname1, size_t fname1len, char_u *fname2, size_t fname2len, int sep, string_T *ret)
{
char_u *dest;
ret->string = alloc(fname1len + (sep ? STRLEN_LITERAL(PATHSEPSTR) : 0) + fname2len + 1);
if (ret->string == NULL)
ret->length = 0;
else
{
STRCPY(ret->string, fname1);
ret->length = fname1len;
if (sep && *ret->string != NUL && !after_pathsep(ret->string, ret->string + ret->length))
{
STRCPY(ret->string + ret->length, PATHSEPSTR);
ret->length += STRLEN_LITERAL(PATHSEPSTR);
}
STRCPY(ret->string + ret->length, fname2);
ret->length += fname2len;
}
dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3);
if (dest == NULL)
return NULL;
STRCPY(dest, fname1);
if (sep)
add_pathsep(dest);
STRCAT(dest, fname2);
return dest;
return ret->string;
}
/*
@@ -3200,8 +3206,14 @@ concat_fnames(char_u *fname1, char_u *fname2, int sep)
void
add_pathsep(char_u *p)
{
if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
STRCAT(p, PATHSEPSTR);
size_t plen;
if (*p == NUL)
return;
plen = STRLEN(p);
if (!after_pathsep(p, p + plen))
STRCPY(p + plen, PATHSEPSTR);
}
/*