mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-09 12:11:20 -03:00
Simplify some parse_util functions
Don't just reflexively drop down to wchar_t.
This commit is contained in:
@@ -348,7 +348,7 @@ maybe_t<int> builtin_commandline(parser_t &parser, io_streams_t &streams, const
|
|||||||
|
|
||||||
if (line_mode) {
|
if (line_mode) {
|
||||||
streams.out.append_format(L"%d\n",
|
streams.out.append_format(L"%d\n",
|
||||||
parse_util_lineno(rstate.text.c_str(), rstate.cursor_pos));
|
parse_util_lineno(rstate.text, rstate.cursor_pos));
|
||||||
return STATUS_CMD_OK;
|
return STATUS_CMD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,51 +42,31 @@
|
|||||||
/// Maximum length of a variable name to show in error reports before truncation
|
/// Maximum length of a variable name to show in error reports before truncation
|
||||||
static constexpr int var_err_len = 16;
|
static constexpr int var_err_len = 16;
|
||||||
|
|
||||||
int parse_util_lineno(const wchar_t *str, size_t offset) {
|
int parse_util_lineno(const wcstring &str, size_t offset) {
|
||||||
if (!str) return 0;
|
// Return the line number of position offset, starting with 1.
|
||||||
|
if (str.empty()) return 0;
|
||||||
|
|
||||||
int res = 1;
|
auto end = offset > str.length() ? str.end() : str.begin() + offset;
|
||||||
for (size_t i = 0; i < offset && str[i] != L'\0'; i++) {
|
return std::count(str.begin(), end, L'\n') + 1;
|
||||||
if (str[i] == L'\n') {
|
|
||||||
res++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_util_get_line_from_offset(const wcstring &str, size_t pos) {
|
int parse_util_get_line_from_offset(const wcstring &str, size_t pos) {
|
||||||
const wchar_t *buff = str.c_str();
|
// Return the line pos is on, or -1 if it's after the end.
|
||||||
int count = 0;
|
if (str.length() > pos) return -1;
|
||||||
for (size_t i = 0; i < pos; i++) {
|
return std::count(str.begin() + pos, str.end(), L'\n');
|
||||||
if (!buff[i]) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buff[i] == L'\n') {
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t parse_util_get_offset_from_line(const wcstring &str, int line) {
|
size_t parse_util_get_offset_from_line(const wcstring &str, int line) {
|
||||||
const wchar_t *buff = str.c_str();
|
// Return the first position on line X, counting from 0.
|
||||||
size_t i;
|
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
if (line < 0) return static_cast<size_t>(-1);
|
if (line < 0) return static_cast<size_t>(-1);
|
||||||
if (line == 0) return 0;
|
if (line == 0) return 0;
|
||||||
|
|
||||||
for (i = 0;; i++) {
|
ssize_t i = 0;
|
||||||
if (!buff[i]) return static_cast<size_t>(-1);
|
while (auto pos = str.find(L'\n')) {
|
||||||
|
i++;
|
||||||
if (buff[i] == L'\n') {
|
if (i == line) return pos + 1;
|
||||||
count++;
|
|
||||||
if (count == line) {
|
|
||||||
return i + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return static_cast<size_t>(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t parse_util_get_offset(const wcstring &str, int line, long line_offset) {
|
size_t parse_util_get_offset(const wcstring &str, int line, long line_offset) {
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ void parse_util_token_extent(const wchar_t *buff, size_t cursor_pos, const wchar
|
|||||||
const wchar_t **prev_end);
|
const wchar_t **prev_end);
|
||||||
|
|
||||||
/// Get the linenumber at the specified character offset.
|
/// Get the linenumber at the specified character offset.
|
||||||
int parse_util_lineno(const wchar_t *str, size_t offset);
|
int parse_util_lineno(const wcstring &str, size_t offset);
|
||||||
|
|
||||||
/// Calculate the line number of the specified cursor position.
|
/// Calculate the line number of the specified cursor position.
|
||||||
int parse_util_get_line_from_offset(const wcstring &str, size_t pos);
|
int parse_util_get_line_from_offset(const wcstring &str, size_t pos);
|
||||||
|
|||||||
@@ -3573,7 +3573,7 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat
|
|||||||
else
|
else
|
||||||
line_new = line_old + 1;
|
line_new = line_old + 1;
|
||||||
|
|
||||||
int line_count = parse_util_lineno(el->text().c_str(), el->size()) - 1;
|
int line_count = parse_util_lineno(el->text(), el->size()) - 1;
|
||||||
|
|
||||||
if (line_new >= 0 && line_new <= line_count) {
|
if (line_new >= 0 && line_new <= line_count) {
|
||||||
auto indents = parse_util_compute_indents(el->text());
|
auto indents = parse_util_compute_indents(el->text());
|
||||||
|
|||||||
Reference in New Issue
Block a user