patch 9.1.1151: too many strlen() calls in getchar.c

Problem:  too many strlen() calls in getchar.c
Solution: store last inserted and recorded lengths,
          add functions to retrieve those and use those
          functions (John Marriott)

closes: #16720

Signed-off-by: John Marriott <basilisk@internode.on.net>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
John Marriott
2025-02-25 20:56:38 +01:00
committed by Christian Brabandt
parent 580e457a2a
commit d3c4b7e946
5 changed files with 48 additions and 18 deletions

View File

@ -88,6 +88,11 @@ static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
static size_t last_recorded_len = 0; // number of last recorded chars
static size_t last_get_recorded_len = 0; // length of the string returned from the
// last call to get_recorded()
static size_t last_get_inserted_len = 0; // length of the string returned from the
// last call to get_inserted()
#ifdef FEAT_EVAL
mapblock_T *last_used_map = NULL;
int last_used_sid = -1;
@ -168,31 +173,46 @@ get_buffcont(
get_recorded(void)
{
char_u *p;
size_t len;
p = get_buffcont(&recordbuff, TRUE, &len);
p = get_buffcont(&recordbuff, TRUE, &last_get_recorded_len);
if (p == NULL)
return NULL;
free_buff(&recordbuff);
/*
* Remove the characters that were added the last time, these must be the
* (possibly mapped) characters that stopped the recording.
*/
if (len >= last_recorded_len)
if (last_get_recorded_len >= last_recorded_len)
{
len -= last_recorded_len;
p[len] = NUL;
last_get_recorded_len -= last_recorded_len;
p[last_get_recorded_len] = NUL;
}
/*
* When stopping recording from Insert mode with CTRL-O q, also remove the
* CTRL-O.
*/
if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
p[len - 1] = NUL;
if (last_get_recorded_len > 0 && restart_edit != 0
&& p[last_get_recorded_len - 1] == Ctrl_O)
{
--last_get_recorded_len;
p[last_get_recorded_len] = NUL;
}
return (p);
}
/*
* Return the length of string returned from the last call of get_recorded().
*/
size_t
get_recorded_len(void)
{
return last_get_recorded_len;
}
/*
* Return the contents of the redo buffer as a single string.
* K_SPECIAL and CSI in the returned string are escaped.
@ -200,7 +220,16 @@ get_recorded(void)
char_u *
get_inserted(void)
{
return get_buffcont(&redobuff, FALSE, NULL);
return get_buffcont(&redobuff, FALSE, &last_get_inserted_len);
}
/*
* Return the length of string returned from the last call of get_inserted().
*/
size_t
get_inserted_len(void)
{
return last_get_inserted_len;
}
/*