patch 9.1.0644: Unnecessary STRLEN() when applying mapping

Problem:  Unnecessary STRLEN() when applying mapping.
          (after v9.1.0642)
Solution: Use m_keylen and vim_strnsave().
          (zeertzjq)

closes: #15394

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
zeertzjq
2024-07-30 19:17:56 +02:00
committed by Christian Brabandt
parent c4be066817
commit 74011dc1fa
3 changed files with 11 additions and 6 deletions

View File

@ -3157,6 +3157,7 @@ handle_mapping(
int save_m_silent; int save_m_silent;
char_u *save_m_keys; char_u *save_m_keys;
char_u *save_alt_m_keys; char_u *save_alt_m_keys;
int save_alt_m_keylen;
#else #else
# define save_m_noremap mp->m_noremap # define save_m_noremap mp->m_noremap
# define save_m_silent mp->m_silent # define save_m_silent mp->m_silent
@ -3206,6 +3207,7 @@ handle_mapping(
save_m_silent = mp->m_silent; save_m_silent = mp->m_silent;
save_m_keys = NULL; // only saved when needed save_m_keys = NULL; // only saved when needed
save_alt_m_keys = NULL; // only saved when needed save_alt_m_keys = NULL; // only saved when needed
save_alt_m_keylen = mp->m_alt != NULL ? mp->m_alt->m_keylen : 0;
/* /*
* Handle ":map <expr>": evaluate the {rhs} as an expression. Also * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
@ -3222,9 +3224,10 @@ handle_mapping(
vgetc_busy = 0; vgetc_busy = 0;
may_garbage_collect = FALSE; may_garbage_collect = FALSE;
save_m_keys = vim_strsave(mp->m_keys); save_m_keys = vim_strnsave(mp->m_keys, (size_t)mp->m_keylen);
save_alt_m_keys = mp->m_alt != NULL save_alt_m_keys = mp->m_alt != NULL
? vim_strsave(mp->m_alt->m_keys) : NULL; ? vim_strnsave(mp->m_alt->m_keys,
(size_t)save_alt_m_keylen) : NULL;
map_str = eval_map_expr(mp, NUL); map_str = eval_map_expr(mp, NUL);
// The mapping may do anything, but we expect it to take care of // The mapping may do anything, but we expect it to take care of
@ -3287,12 +3290,12 @@ handle_mapping(
&& STRNCMP(map_str, save_m_keys, (size_t)keylen) == 0) && STRNCMP(map_str, save_m_keys, (size_t)keylen) == 0)
|| (save_alt_m_keys != NULL || (save_alt_m_keys != NULL
&& STRNCMP(map_str, save_alt_m_keys, && STRNCMP(map_str, save_alt_m_keys,
STRLEN(save_alt_m_keys)) == 0) : (size_t)save_alt_m_keylen) == 0) :
#endif #endif
STRNCMP(map_str, mp->m_keys, (size_t)keylen) == 0 STRNCMP(map_str, mp->m_keys, (size_t)keylen) == 0
|| (mp->m_alt != NULL || (mp->m_alt != NULL
&& STRNCMP(map_str, mp->m_alt->m_keys, && STRNCMP(map_str, mp->m_alt->m_keys,
STRLEN(mp->m_alt->m_keys)) == 0)) (size_t)mp->m_alt->m_keylen) == 0))
noremap = REMAP_SKIP; noremap = REMAP_SKIP;
else else
noremap = REMAP_YES; noremap = REMAP_YES;

View File

@ -1789,11 +1789,11 @@ func Test_map_rhs_starts_with_lhs()
endif endif
let @a = 'foo' let @a = 'foo'
call feedkeys("S\<C-R>a", 'tx') call assert_nobeep('call feedkeys("S\<C-R>a", "tx")')
call assert_equal('foo', getline('.')) call assert_equal('foo', getline('.'))
let @a = 'bar' let @a = 'bar'
call feedkeys("S\<*C-R>a", 'tx') call assert_nobeep('call feedkeys("S\<*C-R>a", "tx")')
call assert_equal('bar', getline('.')) call assert_equal('bar', getline('.'))
endfor endfor
endfor endfor

View File

@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
644,
/**/ /**/
643, 643,
/**/ /**/