vim-patch:9.1.0168: too many STRLEN() calls (#27823)

Problem:  too many STRLEN() calls
Solution: Make use of ml_get_len() calls instead
          (John Marriott)

closes: vim/vim#14123

bfcc895482

Co-authored-by: John Marriott <basilisk@internode.on.net>
This commit is contained in:
zeertzjq 2024-03-12 07:20:22 +08:00 committed by GitHub
parent 59e3bcfb00
commit b02a4d8ac3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 21 deletions

View File

@ -714,7 +714,7 @@ void ins_char_bytes(char *buf, size_t charlen)
size_t col = (size_t)curwin->w_cursor.col;
linenr_T lnum = curwin->w_cursor.lnum;
char *oldp = ml_get(lnum);
size_t linelen = strlen(oldp) + 1; // length of old line including NUL
size_t linelen = (size_t)ml_get_len(lnum) + 1; // length of old line including NUL
// The lengths default to the values for when not replacing.
size_t oldlen = 0; // nr of bytes inserted
@ -821,7 +821,7 @@ void ins_str(char *s)
colnr_T col = curwin->w_cursor.col;
char *oldp = ml_get(lnum);
int oldlen = (int)strlen(oldp);
int oldlen = ml_get_len(lnum);
char *newp = xmalloc((size_t)oldlen + (size_t)newlen + 1);
if (col > 0) {
@ -879,7 +879,7 @@ int del_bytes(colnr_T count, bool fixpos_arg, bool use_delcombine)
colnr_T col = curwin->w_cursor.col;
bool fixpos = fixpos_arg;
char *oldp = ml_get(lnum);
colnr_T oldlen = (colnr_T)strlen(oldp);
colnr_T oldlen = ml_get_len(lnum);
// Can't do anything when the cursor is on the NUL after the line.
if (col >= oldlen) {
@ -1117,7 +1117,7 @@ bool open_line(int dir, int flags, int second_line_indent, bool *did_do_comment)
colnr_T mincol = curwin->w_cursor.col + 1;
// make a copy of the current line so we can mess with it
char *saved_line = xstrdup(get_cursor_line_ptr());
char *saved_line = xstrnsave(get_cursor_line_ptr(), (size_t)get_cursor_line_len());
if (State & VREPLACE_FLAG) {
// With MODE_VREPLACE we make a copy of the next line, which we will be
@ -1128,7 +1128,8 @@ bool open_line(int dir, int flags, int second_line_indent, bool *did_do_comment)
// the line, replacing what was there before and pushing the right
// stuff onto the replace stack. -- webb.
if (curwin->w_cursor.lnum < orig_line_count) {
next_line = xstrdup(ml_get(curwin->w_cursor.lnum + 1));
next_line = xstrnsave(ml_get(curwin->w_cursor.lnum + 1),
(size_t)ml_get_len(curwin->w_cursor.lnum + 1));
} else {
next_line = xstrdup("");
}
@ -1908,7 +1909,7 @@ bool open_line(int dir, int flags, int second_line_indent, bool *did_do_comment)
// stuff onto the replace stack (via ins_char()).
if (State & VREPLACE_FLAG) {
// Put new line in p_extra
p_extra = xstrdup(get_cursor_line_ptr());
p_extra = xstrnsave(get_cursor_line_ptr(), (size_t)get_cursor_line_len());
// Put back original line
ml_replace(curwin->w_cursor.lnum, next_line, false);
@ -1939,7 +1940,7 @@ void truncate_line(int fixpos)
colnr_T col = curwin->w_cursor.col;
char *old_line = ml_get(lnum);
char *newp = col == 0 ? xstrdup("") : xstrnsave(old_line, (size_t)col);
int deleted = (int)strlen(old_line) - col;
int deleted = ml_get_len(lnum) - col;
ml_replace(lnum, newp, false);

View File

@ -4331,7 +4331,7 @@ static bool ins_tab(void)
if (State & VREPLACE_FLAG) {
pos = curwin->w_cursor;
cursor = &pos;
saved_line = xstrdup(get_cursor_line_ptr());
saved_line = xstrnsave(get_cursor_line_ptr(), (size_t)get_cursor_line_len());
ptr = saved_line + pos.col;
} else {
ptr = get_cursor_pos_ptr();

View File

@ -6699,7 +6699,7 @@ pos_T *var2fpos(const typval_T *const tv, const bool dollar_lnum, int *const ret
if (charcol) {
len = mb_charlen(ml_get(pos.lnum));
} else {
len = (int)strlen(ml_get(pos.lnum));
len = ml_get_len(pos.lnum);
}
// We accept "$" for the column number: last column.
@ -6789,7 +6789,7 @@ pos_T *var2fpos(const typval_T *const tv, const bool dollar_lnum, int *const ret
if (charcol) {
pos.col = (colnr_T)mb_charlen(get_cursor_line_ptr());
} else {
pos.col = (colnr_T)strlen(get_cursor_line_ptr());
pos.col = get_cursor_line_len();
}
}
return &pos;

View File

@ -738,7 +738,7 @@ static void get_col(typval_T *argvars, typval_T *rettv, bool charcol)
if (fp->col == MAXCOL) {
// '> can be MAXCOL, get the length of the line then
if (fp->lnum <= curbuf->b_ml.ml_line_count) {
col = (colnr_T)strlen(ml_get(fp->lnum)) + 1;
col = ml_get_len(fp->lnum) + 1;
} else {
col = MAXCOL;
}
@ -8688,7 +8688,7 @@ static void f_synID(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
int id = 0;
if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
&& col >= 0 && (size_t)col < strlen(ml_get(lnum))) {
&& col >= 0 && col < ml_get_len(lnum)) {
id = syn_get_id(curwin, lnum, col, trans, NULL, false);
}
@ -8811,8 +8811,8 @@ static void f_synconcealed(typval_T *argvars, typval_T *rettv, EvalFuncData fptr
CLEAR_FIELD(str);
if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count && col >= 0
&& (size_t)col <= strlen(ml_get(lnum)) && curwin->w_p_cole > 0) {
if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
&& col >= 0 && col <= ml_get_len(lnum) && curwin->w_p_cole > 0) {
syn_get_id(curwin, lnum, col, false, NULL, false);
syntax_flags = get_syntax_info(&matchid);
@ -8845,10 +8845,8 @@ static void f_synstack(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
const linenr_T lnum = tv_get_lnum(argvars);
const colnr_T col = (colnr_T)tv_get_number(&argvars[1]) - 1;
if (lnum >= 1
&& lnum <= curbuf->b_ml.ml_line_count
&& col >= 0
&& (size_t)col <= strlen(ml_get(lnum))) {
if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
&& col >= 0 && col <= ml_get_len(lnum)) {
tv_list_alloc_ret(rettv, kListLenMayKnow);
syn_get_id(curwin, lnum, col, false, NULL, true);
@ -9218,9 +9216,9 @@ static void f_virtcol(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
if (fp->col < 0) {
fp->col = 0;
} else {
const size_t len = strlen(ml_get(fp->lnum));
if (fp->col > (colnr_T)len) {
fp->col = (colnr_T)len;
const colnr_T len = ml_get_len(fp->lnum);
if (fp->col > len) {
fp->col = len;
}
}
getvvcol(curwin, fp, &vcol_start, NULL, &vcol_end);