From 98ec3fdf744ffce5e685da76a5ef6f2b000649f6 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 21 Apr 2025 19:16:29 +0800 Subject: [PATCH] vim-patch:9.1.1328: too many strlen() calls in indent.c (#33563) Problem: too many strlen() calls in indent.c Solution: refactor indent.c slightly and remove strlen() calls (John Marriott) closes: vim/vim#17156 https://github.com/vim/vim/commit/eac45c558e3585ada79bb24a86e8cd343e2807cf Co-authored-by: John Marriott --- src/nvim/edit.c | 4 ++-- src/nvim/indent.c | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/nvim/edit.c b/src/nvim/edit.c index d176c98ed1..e6ed80bba8 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -1638,7 +1638,7 @@ void change_indent(int type, int amount, int round, bool call_changed_bytes) // MODE_VREPLACE state needs to know what the line was like before changing if (State & VREPLACE_FLAG) { - orig_line = xstrdup(get_cursor_line_ptr()); // Deal with NULL below + orig_line = xstrnsave(get_cursor_line_ptr(), (size_t)get_cursor_line_len()); orig_col = curwin->w_cursor.col; } @@ -1788,7 +1788,7 @@ void change_indent(int type, int amount, int round, bool call_changed_bytes) // then put it back again the way we wanted it. if (State & VREPLACE_FLAG) { // Save new line - char *new_line = xstrdup(get_cursor_line_ptr()); + char *new_line = xstrnsave(get_cursor_line_ptr(), (size_t)get_cursor_line_len()); // We only put back the new line up to the cursor new_line[curwin->w_cursor.col] = NUL; diff --git a/src/nvim/indent.c b/src/nvim/indent.c index 0d06199bde..96c746df43 100644 --- a/src/nvim/indent.c +++ b/src/nvim/indent.c @@ -488,6 +488,7 @@ bool set_indent(int size, int flags) int todo = size; int ind_len = 0; // Measured in characters. char *p = oldline = get_cursor_line_ptr(); + int line_len = get_cursor_line_len() + 1; // size of the line (including the NUL) // Calculate the buffer size for the new indent, and check to see if it // isn't already set. @@ -584,8 +585,8 @@ bool set_indent(int size, int flags) p = oldline; } else { p = skipwhite(p); + line_len -= (int)(p - oldline); } - int line_len = (int)strlen(p) + 1; // If 'preserveindent' and 'expandtab' are both set keep the original // characters and allocate accordingly. We will fill the rest with spaces @@ -1028,6 +1029,7 @@ void ex_retab(exarg_T *eap) } for (linenr_T lnum = eap->line1; !got_int && lnum <= eap->line2; lnum++) { char *ptr = ml_get(lnum); + int old_len = ml_get_len(lnum); int col = 0; int64_t vcol = 0; bool did_undo = false; // called u_save for current line @@ -1071,7 +1073,6 @@ void ex_retab(exarg_T *eap) // len is actual number of white characters used len = num_spaces + num_tabs; - int old_len = (int)strlen(ptr); const int new_len = old_len - col + start_col + len + 1; if (new_len <= 0 || new_len >= MAXCOL) { emsg_text_too_long(); @@ -1099,6 +1100,7 @@ void ex_retab(exarg_T *eap) } last_line = lnum; ptr = new_line; + old_len = new_len - 1; col = start_col + len; } } @@ -1410,10 +1412,8 @@ static int lisp_match(char *p) char *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords; while (*word != NUL) { - copy_option_part(&word, buf, sizeof(buf), ","); - int len = (int)strlen(buf); - - if ((strncmp(buf, p, (size_t)len) == 0) && ascii_iswhite_or_nul(p[len])) { + size_t len = copy_option_part(&word, buf, sizeof(buf), ","); + if ((strncmp(buf, p, len) == 0) && ascii_iswhite_or_nul(p[len])) { return true; } }