mirror of
https://github.com/neovim/neovim.git
synced 2024-12-23 20:55:18 -07:00
refactor: use ml_get_buf_len() in API code (#27825)
This commit is contained in:
parent
b02a4d8ac3
commit
ac8cd5368d
@ -529,18 +529,18 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
|
||||
|
||||
// Another call to ml_get_buf() may free the lines, so we make copies
|
||||
char *str_at_start = ml_get_buf(buf, (linenr_T)start_row);
|
||||
size_t len_at_start = strlen(str_at_start);
|
||||
str_at_start = arena_memdupz(arena, str_at_start, len_at_start);
|
||||
start_col = start_col < 0 ? (int64_t)len_at_start + start_col + 1 : start_col;
|
||||
VALIDATE_RANGE((start_col >= 0 && (size_t)start_col <= len_at_start), "start_col", {
|
||||
colnr_T len_at_start = ml_get_buf_len(buf, (linenr_T)start_row);
|
||||
str_at_start = arena_memdupz(arena, str_at_start, (size_t)len_at_start);
|
||||
start_col = start_col < 0 ? len_at_start + start_col + 1 : start_col;
|
||||
VALIDATE_RANGE((start_col >= 0 && start_col <= len_at_start), "start_col", {
|
||||
return;
|
||||
});
|
||||
|
||||
char *str_at_end = ml_get_buf(buf, (linenr_T)end_row);
|
||||
size_t len_at_end = strlen(str_at_end);
|
||||
str_at_end = arena_memdupz(arena, str_at_end, len_at_end);
|
||||
end_col = end_col < 0 ? (int64_t)len_at_end + end_col + 1 : end_col;
|
||||
VALIDATE_RANGE((end_col >= 0 && (size_t)end_col <= len_at_end), "end_col", {
|
||||
colnr_T len_at_end = ml_get_buf_len(buf, (linenr_T)end_row);
|
||||
str_at_end = arena_memdupz(arena, str_at_end, (size_t)len_at_end);
|
||||
end_col = end_col < 0 ? len_at_end + end_col + 1 : end_col;
|
||||
VALIDATE_RANGE((end_col >= 0 && end_col <= len_at_end), "end_col", {
|
||||
return;
|
||||
});
|
||||
|
||||
@ -563,12 +563,10 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
|
||||
if (start_row == end_row) {
|
||||
old_byte = (bcount_t)end_col - start_col;
|
||||
} else {
|
||||
old_byte += (bcount_t)len_at_start - start_col;
|
||||
old_byte += len_at_start - start_col;
|
||||
for (int64_t i = 1; i < end_row - start_row; i++) {
|
||||
int64_t lnum = start_row + i;
|
||||
|
||||
const char *bufline = ml_get_buf(buf, (linenr_T)lnum);
|
||||
old_byte += (bcount_t)(strlen(bufline)) + 1;
|
||||
old_byte += ml_get_buf_len(buf, (linenr_T)lnum) + 1;
|
||||
}
|
||||
old_byte += (bcount_t)end_col + 1;
|
||||
}
|
||||
@ -577,7 +575,7 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
|
||||
String last_item = replacement.items[replacement.size - 1].data.string;
|
||||
|
||||
size_t firstlen = (size_t)start_col + first_item.size;
|
||||
size_t last_part_len = len_at_end - (size_t)end_col;
|
||||
size_t last_part_len = (size_t)len_at_end - (size_t)end_col;
|
||||
if (replacement.size == 1) {
|
||||
firstlen += last_part_len;
|
||||
}
|
||||
@ -1324,7 +1322,7 @@ static void fix_cursor_cols(win_T *win, linenr_T start_row, colnr_T start_col, l
|
||||
// it already (in case virtualedit is active)
|
||||
// column might be additionally adjusted below
|
||||
// to keep it inside col range if needed
|
||||
colnr_T len = (colnr_T)strlen(ml_get_buf(win->w_buffer, new_end_row));
|
||||
colnr_T len = ml_get_buf_len(win->w_buffer, new_end_row);
|
||||
if (win->w_cursor.col < len) {
|
||||
win->w_cursor.col = len;
|
||||
}
|
||||
@ -1424,6 +1422,7 @@ void buf_collect_lines(buf_T *buf, size_t n, linenr_T start, int start_idx, bool
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
linenr_T lnum = start + (linenr_T)i;
|
||||
char *bufstr = ml_get_buf(buf, lnum);
|
||||
push_linestr(lstate, l, bufstr, strlen(bufstr), start_idx + (int)i, replace_nl, arena);
|
||||
size_t bufstrlen = (size_t)ml_get_buf_len(buf, lnum);
|
||||
push_linestr(lstate, l, bufstr, bufstrlen, start_idx + (int)i, replace_nl, arena);
|
||||
}
|
||||
}
|
||||
|
@ -682,7 +682,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
goto error;
|
||||
});
|
||||
|
||||
size_t len = 0;
|
||||
colnr_T len = 0;
|
||||
|
||||
if (HAS_KEY(opts, set_extmark, spell)) {
|
||||
hl.flags |= (opts->spell) ? kSHSpellOn : kSHSpellOff;
|
||||
@ -712,16 +712,16 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
});
|
||||
line = buf->b_ml.ml_line_count;
|
||||
} else if (line < buf->b_ml.ml_line_count) {
|
||||
len = opts->ephemeral ? MAXCOL : strlen(ml_get_buf(buf, (linenr_T)line + 1));
|
||||
len = opts->ephemeral ? MAXCOL : ml_get_buf_len(buf, (linenr_T)line + 1);
|
||||
}
|
||||
|
||||
if (col == -1) {
|
||||
col = (Integer)len;
|
||||
} else if (col > (Integer)len) {
|
||||
col = len;
|
||||
} else if (col > len) {
|
||||
VALIDATE_RANGE(!strict, "col", {
|
||||
goto error;
|
||||
});
|
||||
col = (Integer)len;
|
||||
col = len;
|
||||
} else if (col < -1) {
|
||||
VALIDATE_RANGE(false, "col", {
|
||||
goto error;
|
||||
@ -730,7 +730,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
|
||||
if (col2 >= 0) {
|
||||
if (line2 >= 0 && line2 < buf->b_ml.ml_line_count) {
|
||||
len = opts->ephemeral ? MAXCOL : strlen(ml_get_buf(buf, (linenr_T)line2 + 1));
|
||||
len = opts->ephemeral ? MAXCOL : ml_get_buf_len(buf, (linenr_T)line2 + 1);
|
||||
} else if (line2 == buf->b_ml.ml_line_count) {
|
||||
// We are trying to add an extmark past final newline
|
||||
len = 0;
|
||||
@ -738,11 +738,11 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
// reuse len from before
|
||||
line2 = (int)line;
|
||||
}
|
||||
if (col2 > (Integer)len) {
|
||||
if (col2 > len) {
|
||||
VALIDATE_RANGE(!strict, "end_col", {
|
||||
goto error;
|
||||
});
|
||||
col2 = (int)len;
|
||||
col2 = len;
|
||||
}
|
||||
} else if (line2 >= 0) {
|
||||
col2 = 0;
|
||||
|
@ -524,10 +524,10 @@ String buf_get_text(buf_T *buf, int64_t lnum, int64_t start_col, int64_t end_col
|
||||
}
|
||||
|
||||
char *bufstr = ml_get_buf(buf, (linenr_T)lnum);
|
||||
size_t line_length = strlen(bufstr);
|
||||
colnr_T line_length = ml_get_buf_len(buf, (linenr_T)lnum);
|
||||
|
||||
start_col = start_col < 0 ? (int64_t)line_length + start_col + 1 : start_col;
|
||||
end_col = end_col < 0 ? (int64_t)line_length + end_col + 1 : end_col;
|
||||
start_col = start_col < 0 ? line_length + start_col + 1 : start_col;
|
||||
end_col = end_col < 0 ? line_length + end_col + 1 : end_col;
|
||||
|
||||
if (start_col >= MAXCOL || end_col >= MAXCOL) {
|
||||
api_set_error(err, kErrorTypeValidation, "Column index is too high");
|
||||
@ -539,7 +539,7 @@ String buf_get_text(buf_T *buf, int64_t lnum, int64_t start_col, int64_t end_col
|
||||
return rv;
|
||||
}
|
||||
|
||||
if ((size_t)start_col >= line_length) {
|
||||
if (start_col >= line_length) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
@ -1767,7 +1767,7 @@ void ex_luado(exarg_T *const eap)
|
||||
lua_pushvalue(lstate, -1);
|
||||
const char *const old_line = ml_get_buf(curbuf, l);
|
||||
// Get length of old_line here as calling Lua code may free it.
|
||||
const size_t old_line_len = strlen(old_line);
|
||||
const colnr_T old_line_len = ml_get_buf_len(curbuf, l);
|
||||
lua_pushstring(lstate, old_line);
|
||||
lua_pushnumber(lstate, (lua_Number)l);
|
||||
if (nlua_pcall(lstate, 2, 1)) {
|
||||
@ -1791,7 +1791,7 @@ void ex_luado(exarg_T *const eap)
|
||||
}
|
||||
}
|
||||
ml_replace(l, new_line_transformed, false);
|
||||
inserted_bytes(l, 0, (int)old_line_len, (int)new_line_len);
|
||||
inserted_bytes(l, 0, old_line_len, (int)new_line_len);
|
||||
}
|
||||
lua_pop(lstate, 1);
|
||||
}
|
||||
|
@ -107,15 +107,15 @@ static int regex_match_line(lua_State *lstate)
|
||||
}
|
||||
|
||||
char *line = ml_get_buf(buf, rownr + 1);
|
||||
size_t len = strlen(line);
|
||||
colnr_T len = ml_get_buf_len(buf, rownr + 1);
|
||||
|
||||
if (start < 0 || (size_t)start > len) {
|
||||
if (start < 0 || start > len) {
|
||||
return luaL_error(lstate, "invalid start");
|
||||
}
|
||||
|
||||
char save = NUL;
|
||||
if (end >= 0) {
|
||||
if ((size_t)end > len || end < start) {
|
||||
if (end > len || end < start) {
|
||||
return luaL_error(lstate, "invalid end");
|
||||
}
|
||||
save = line[end];
|
||||
|
@ -371,7 +371,7 @@ static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position
|
||||
return "";
|
||||
}
|
||||
char *line = ml_get_buf(bp, (linenr_T)position.row + 1);
|
||||
size_t len = strlen(line);
|
||||
size_t len = (size_t)ml_get_buf_len(bp, (linenr_T)position.row + 1);
|
||||
if (position.column > len) {
|
||||
*bytes_read = 0;
|
||||
return "";
|
||||
|
Loading…
Reference in New Issue
Block a user