-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Refactor strings.c to remove calls to STRLEN() #18617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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 redundantSTRLEN()calls instring_filter_map()andstring_reduce() - Hoisted
STRLEN(needle)out of loops instring_count()to avoid repeated calculations - Replaced
vim_strsave()withvim_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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| needlelen = STRLEN(needle); | |
| size_t needlelen = STRLEN(needle); |
and remove the definition in line 883 then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep.
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>
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>
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 toSTRLEN()outside thewhileloop.In
copy_first_char_to_tv()change to return-1on failure or the length of resultingv_string. Changestring_filter_map()andstring_reduce()to use the return value ofcopy_first_char_to_tv().In
blob_from_string()refactor to remove call toSTRLEN().In
string_from_blob()callvim_strnsave()instead ofvim_strsave().In
vim_snprintf_safelen()callvim_vsnprintf_typval()directly instead ofvim_vsnprintf()which then callsvim_vsnprintf_typval().Cheers
John