0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

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
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
5 changed files with 48 additions and 18 deletions

View File

@ -421,7 +421,7 @@ edit(
new_insert_skip = 0; new_insert_skip = 0;
else else
{ {
new_insert_skip = (int)STRLEN(ptr); new_insert_skip = (int)get_inserted_len();
vim_free(ptr); vim_free(ptr);
} }
@ -2462,7 +2462,7 @@ stop_insert(
* otherwise CTRL-O w and then <Left> will clear "last_insert". * otherwise CTRL-O w and then <Left> will clear "last_insert".
*/ */
ptr = get_inserted(); ptr = get_inserted();
int added = ptr == NULL ? 0 : (int)STRLEN(ptr) - new_insert_skip; int added = ptr == NULL ? 0 : (int)get_inserted_len() - new_insert_skip;
if (did_restart_edit == 0 || added > 0) if (did_restart_edit == 0 || added > 0)
{ {
vim_free(last_insert); vim_free(last_insert);
@ -2993,11 +2993,11 @@ get_last_insert_save(void)
if (last_insert == NULL) if (last_insert == NULL)
return NULL; return NULL;
s = vim_strsave(last_insert + last_insert_skip); len = (int)STRLEN(last_insert + last_insert_skip);
s = vim_strnsave(last_insert + last_insert_skip, len);
if (s == NULL) if (s == NULL)
return NULL; return NULL;
len = (int)STRLEN(s);
if (len > 0 && s[len - 1] == ESC) // remove trailing ESC if (len > 0 && s[len - 1] == ESC) // remove trailing ESC
s[len - 1] = NUL; s[len - 1] = NUL;
return s; return s;

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_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 #ifdef FEAT_EVAL
mapblock_T *last_used_map = NULL; mapblock_T *last_used_map = NULL;
int last_used_sid = -1; int last_used_sid = -1;
@ -168,31 +173,46 @@ get_buffcont(
get_recorded(void) get_recorded(void)
{ {
char_u *p; 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); free_buff(&recordbuff);
/* /*
* Remove the characters that were added the last time, these must be the * Remove the characters that were added the last time, these must be the
* (possibly mapped) characters that stopped the recording. * (possibly mapped) characters that stopped the recording.
*/ */
if (len >= last_recorded_len) if (last_get_recorded_len >= last_recorded_len)
{ {
len -= last_recorded_len; last_get_recorded_len -= last_recorded_len;
p[len] = NUL; p[last_get_recorded_len] = NUL;
} }
/* /*
* When stopping recording from Insert mode with CTRL-O q, also remove the * When stopping recording from Insert mode with CTRL-O q, also remove the
* CTRL-O. * CTRL-O.
*/ */
if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O) if (last_get_recorded_len > 0 && restart_edit != 0
p[len - 1] = NUL; && p[last_get_recorded_len - 1] == Ctrl_O)
{
--last_get_recorded_len;
p[last_get_recorded_len] = NUL;
}
return (p); 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. * Return the contents of the redo buffer as a single string.
* K_SPECIAL and CSI in the returned string are escaped. * K_SPECIAL and CSI in the returned string are escaped.
@ -200,7 +220,16 @@ get_recorded(void)
char_u * char_u *
get_inserted(void) 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;
} }
/* /*

View File

@ -1,6 +1,8 @@
/* getchar.c */ /* getchar.c */
char_u *get_recorded(void); char_u *get_recorded(void);
size_t get_recorded_len(void);
char_u *get_inserted(void); char_u *get_inserted(void);
size_t get_inserted_len(void);
int stuff_empty(void); int stuff_empty(void);
int readbuf1_empty(void); int readbuf1_empty(void);
void typeahead_noflush(int c); void typeahead_noflush(int c);

View File

@ -28,7 +28,7 @@ static yankreg_T *y_current; // ptr to current yankreg
static int y_append; // TRUE when appending static int y_append; // TRUE when appending
static yankreg_T *y_previous = NULL; // ptr to last written yankreg static yankreg_T *y_previous = NULL; // ptr to last written yankreg
static int stuff_yank(int, char_u *); static int stuff_yank(int, char_u *, size_t);
static void put_reedit_in_typebuf(int silent); static void put_reedit_in_typebuf(int silent);
static int put_in_typebuf(char_u *s, int esc, int colon, int silent); static int put_in_typebuf(char_u *s, int esc, int colon, int silent);
static int yank_copy_line(struct block_def *bd, long y_idx, int exclude_trailing_space); static int yank_copy_line(struct block_def *bd, long y_idx, int exclude_trailing_space);
@ -419,7 +419,7 @@ do_record(int c)
old_y_previous = y_previous; old_y_previous = y_previous;
old_y_current = y_current; old_y_current = y_current;
retval = stuff_yank(regname, p); retval = stuff_yank(regname, p, get_recorded_len());
y_previous = old_y_previous; y_previous = old_y_previous;
y_current = old_y_current; y_current = old_y_current;
@ -435,10 +435,8 @@ do_record(int c)
* return FAIL for failure, OK otherwise * return FAIL for failure, OK otherwise
*/ */
static int static int
stuff_yank(int regname, char_u *p) stuff_yank(int regname, char_u *p, size_t plen)
{ {
size_t plen;
// check for read-only register // check for read-only register
if (regname != 0 && !valid_yank_reg(regname, TRUE)) if (regname != 0 && !valid_yank_reg(regname, TRUE))
{ {
@ -451,7 +449,6 @@ stuff_yank(int regname, char_u *p)
return OK; return OK;
} }
plen = STRLEN(p);
get_yank_register(regname, TRUE); get_yank_register(regname, TRUE);
if (y_append && y_current->y_array != NULL) if (y_append && y_current->y_array != NULL)
{ {

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 */
/**/
1151,
/**/ /**/
1150, 1150,
/**/ /**/