Skip to content

Conversation

@basilisk0315
Copy link

This PR does some same refactoring of functions in strings.c to remove calls to STRLEN().

Specifically:
In vim_strsave_shellescape() a small cosmetic change.
In string_count() move the call to STRLEN() outside the while loop.
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().
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().

Cheers
John

@chrisbra chrisbra requested a review from Copilot October 26, 2025 19:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors several functions in strings.c to eliminate redundant calls to STRLEN(), improving code efficiency and readability by:

  • Computing string lengths once and reusing the values
  • Returning computed lengths from functions to avoid recalculation
  • Using more appropriate string functions that accept explicit lengths

Key Changes

  • Modified copy_first_char_to_tv() to return the computed string length instead of a status code, eliminating redundant STRLEN() calls in string_filter_map() and string_reduce()
  • Hoisted STRLEN(needle) out of loops in string_count() to avoid repeated calculations
  • Replaced vim_strsave() with vim_strnsave() where length is already known

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

src/strings.c Outdated
if (p == NULL || needle == NULL || *needle == NUL)
return 0;

needlelen = STRLEN(needle);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
needlelen = STRLEN(needle);
size_t needlelen = STRLEN(needle);

and remove the definition in line 883 then.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

@chrisbra chrisbra closed this in 110656b Oct 28, 2025
zeertzjq added a commit to zeertzjq/neovim that referenced this pull request Oct 30, 2025
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

vim/vim@110656b

Co-authored-by: John Marriott <basilisk@internode.on.net>
zeertzjq added a commit to zeertzjq/neovim that referenced this pull request Oct 30, 2025
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

vim/vim@110656b

Co-authored-by: John Marriott <basilisk@internode.on.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants