mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 03:18:16 +00:00
refactor: use ml_get_buf_len() in API code (#27825)
This commit is contained in:
@@ -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
|
// 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);
|
char *str_at_start = ml_get_buf(buf, (linenr_T)start_row);
|
||||||
size_t len_at_start = strlen(str_at_start);
|
colnr_T len_at_start = ml_get_buf_len(buf, (linenr_T)start_row);
|
||||||
str_at_start = arena_memdupz(arena, str_at_start, len_at_start);
|
str_at_start = arena_memdupz(arena, str_at_start, (size_t)len_at_start);
|
||||||
start_col = start_col < 0 ? (int64_t)len_at_start + start_col + 1 : start_col;
|
start_col = start_col < 0 ? len_at_start + start_col + 1 : start_col;
|
||||||
VALIDATE_RANGE((start_col >= 0 && (size_t)start_col <= len_at_start), "start_col", {
|
VALIDATE_RANGE((start_col >= 0 && start_col <= len_at_start), "start_col", {
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
|
|
||||||
char *str_at_end = ml_get_buf(buf, (linenr_T)end_row);
|
char *str_at_end = ml_get_buf(buf, (linenr_T)end_row);
|
||||||
size_t len_at_end = strlen(str_at_end);
|
colnr_T len_at_end = ml_get_buf_len(buf, (linenr_T)end_row);
|
||||||
str_at_end = arena_memdupz(arena, str_at_end, len_at_end);
|
str_at_end = arena_memdupz(arena, str_at_end, (size_t)len_at_end);
|
||||||
end_col = end_col < 0 ? (int64_t)len_at_end + end_col + 1 : end_col;
|
end_col = end_col < 0 ? len_at_end + end_col + 1 : end_col;
|
||||||
VALIDATE_RANGE((end_col >= 0 && (size_t)end_col <= len_at_end), "end_col", {
|
VALIDATE_RANGE((end_col >= 0 && end_col <= len_at_end), "end_col", {
|
||||||
return;
|
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) {
|
if (start_row == end_row) {
|
||||||
old_byte = (bcount_t)end_col - start_col;
|
old_byte = (bcount_t)end_col - start_col;
|
||||||
} else {
|
} 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++) {
|
for (int64_t i = 1; i < end_row - start_row; i++) {
|
||||||
int64_t lnum = start_row + i;
|
int64_t lnum = start_row + i;
|
||||||
|
old_byte += ml_get_buf_len(buf, (linenr_T)lnum) + 1;
|
||||||
const char *bufline = ml_get_buf(buf, (linenr_T)lnum);
|
|
||||||
old_byte += (bcount_t)(strlen(bufline)) + 1;
|
|
||||||
}
|
}
|
||||||
old_byte += (bcount_t)end_col + 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;
|
String last_item = replacement.items[replacement.size - 1].data.string;
|
||||||
|
|
||||||
size_t firstlen = (size_t)start_col + first_item.size;
|
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) {
|
if (replacement.size == 1) {
|
||||||
firstlen += last_part_len;
|
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)
|
// it already (in case virtualedit is active)
|
||||||
// column might be additionally adjusted below
|
// column might be additionally adjusted below
|
||||||
// to keep it inside col range if needed
|
// 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) {
|
if (win->w_cursor.col < len) {
|
||||||
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++) {
|
for (size_t i = 0; i < n; i++) {
|
||||||
linenr_T lnum = start + (linenr_T)i;
|
linenr_T lnum = start + (linenr_T)i;
|
||||||
char *bufstr = ml_get_buf(buf, lnum);
|
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;
|
goto error;
|
||||||
});
|
});
|
||||||
|
|
||||||
size_t len = 0;
|
colnr_T len = 0;
|
||||||
|
|
||||||
if (HAS_KEY(opts, set_extmark, spell)) {
|
if (HAS_KEY(opts, set_extmark, spell)) {
|
||||||
hl.flags |= (opts->spell) ? kSHSpellOn : kSHSpellOff;
|
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;
|
line = buf->b_ml.ml_line_count;
|
||||||
} else if (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) {
|
if (col == -1) {
|
||||||
col = (Integer)len;
|
col = len;
|
||||||
} else if (col > (Integer)len) {
|
} else if (col > len) {
|
||||||
VALIDATE_RANGE(!strict, "col", {
|
VALIDATE_RANGE(!strict, "col", {
|
||||||
goto error;
|
goto error;
|
||||||
});
|
});
|
||||||
col = (Integer)len;
|
col = len;
|
||||||
} else if (col < -1) {
|
} else if (col < -1) {
|
||||||
VALIDATE_RANGE(false, "col", {
|
VALIDATE_RANGE(false, "col", {
|
||||||
goto error;
|
goto error;
|
||||||
@@ -730,7 +730,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
|||||||
|
|
||||||
if (col2 >= 0) {
|
if (col2 >= 0) {
|
||||||
if (line2 >= 0 && line2 < buf->b_ml.ml_line_count) {
|
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) {
|
} else if (line2 == buf->b_ml.ml_line_count) {
|
||||||
// We are trying to add an extmark past final newline
|
// We are trying to add an extmark past final newline
|
||||||
len = 0;
|
len = 0;
|
||||||
@@ -738,11 +738,11 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
|||||||
// reuse len from before
|
// reuse len from before
|
||||||
line2 = (int)line;
|
line2 = (int)line;
|
||||||
}
|
}
|
||||||
if (col2 > (Integer)len) {
|
if (col2 > len) {
|
||||||
VALIDATE_RANGE(!strict, "end_col", {
|
VALIDATE_RANGE(!strict, "end_col", {
|
||||||
goto error;
|
goto error;
|
||||||
});
|
});
|
||||||
col2 = (int)len;
|
col2 = len;
|
||||||
}
|
}
|
||||||
} else if (line2 >= 0) {
|
} else if (line2 >= 0) {
|
||||||
col2 = 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);
|
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;
|
start_col = start_col < 0 ? line_length + start_col + 1 : start_col;
|
||||||
end_col = end_col < 0 ? (int64_t)line_length + end_col + 1 : end_col;
|
end_col = end_col < 0 ? line_length + end_col + 1 : end_col;
|
||||||
|
|
||||||
if (start_col >= MAXCOL || end_col >= MAXCOL) {
|
if (start_col >= MAXCOL || end_col >= MAXCOL) {
|
||||||
api_set_error(err, kErrorTypeValidation, "Column index is too high");
|
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;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((size_t)start_col >= line_length) {
|
if (start_col >= line_length) {
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1767,7 +1767,7 @@ void ex_luado(exarg_T *const eap)
|
|||||||
lua_pushvalue(lstate, -1);
|
lua_pushvalue(lstate, -1);
|
||||||
const char *const old_line = ml_get_buf(curbuf, l);
|
const char *const old_line = ml_get_buf(curbuf, l);
|
||||||
// Get length of old_line here as calling Lua code may free it.
|
// 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_pushstring(lstate, old_line);
|
||||||
lua_pushnumber(lstate, (lua_Number)l);
|
lua_pushnumber(lstate, (lua_Number)l);
|
||||||
if (nlua_pcall(lstate, 2, 1)) {
|
if (nlua_pcall(lstate, 2, 1)) {
|
||||||
@@ -1791,7 +1791,7 @@ void ex_luado(exarg_T *const eap)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ml_replace(l, new_line_transformed, false);
|
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);
|
lua_pop(lstate, 1);
|
||||||
}
|
}
|
||||||
|
@@ -107,15 +107,15 @@ static int regex_match_line(lua_State *lstate)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *line = ml_get_buf(buf, rownr + 1);
|
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");
|
return luaL_error(lstate, "invalid start");
|
||||||
}
|
}
|
||||||
|
|
||||||
char save = NUL;
|
char save = NUL;
|
||||||
if (end >= 0) {
|
if (end >= 0) {
|
||||||
if ((size_t)end > len || end < start) {
|
if (end > len || end < start) {
|
||||||
return luaL_error(lstate, "invalid end");
|
return luaL_error(lstate, "invalid end");
|
||||||
}
|
}
|
||||||
save = line[end];
|
save = line[end];
|
||||||
|
@@ -371,7 +371,7 @@ static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
char *line = ml_get_buf(bp, (linenr_T)position.row + 1);
|
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) {
|
if (position.column > len) {
|
||||||
*bytes_read = 0;
|
*bytes_read = 0;
|
||||||
return "";
|
return "";
|
||||||
|
Reference in New Issue
Block a user