Simplify some parse_util functions

Don't just reflexively drop down to wchar_t.
This commit is contained in:
Fabian Homborg 2021-07-27 17:59:52 +02:00
parent a6fa1c3b10
commit 35ca42413d
4 changed files with 17 additions and 37 deletions

View File

@ -348,7 +348,7 @@ maybe_t<int> builtin_commandline(parser_t &parser, io_streams_t &streams, const
if (line_mode) {
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;
}

View File

@ -42,51 +42,31 @@
/// Maximum length of a variable name to show in error reports before truncation
static constexpr int var_err_len = 16;
int parse_util_lineno(const wchar_t *str, size_t offset) {
if (!str) return 0;
int parse_util_lineno(const wcstring &str, size_t offset) {
// Return the line number of position offset, starting with 1.
if (str.empty()) return 0;
int res = 1;
for (size_t i = 0; i < offset && str[i] != L'\0'; i++) {
if (str[i] == L'\n') {
res++;
}
}
return res;
auto end = offset > str.length() ? str.end() : str.begin() + offset;
return std::count(str.begin(), end, L'\n') + 1;
}
int parse_util_get_line_from_offset(const wcstring &str, size_t pos) {
const wchar_t *buff = str.c_str();
int count = 0;
for (size_t i = 0; i < pos; i++) {
if (!buff[i]) {
return -1;
}
if (buff[i] == L'\n') {
count++;
}
}
return count;
// Return the line pos is on, or -1 if it's after the end.
if (str.length() > pos) return -1;
return std::count(str.begin() + pos, str.end(), L'\n');
}
size_t parse_util_get_offset_from_line(const wcstring &str, int line) {
const wchar_t *buff = str.c_str();
size_t i;
int count = 0;
// Return the first position on line X, counting from 0.
if (line < 0) return static_cast<size_t>(-1);
if (line == 0) return 0;
for (i = 0;; i++) {
if (!buff[i]) return static_cast<size_t>(-1);
if (buff[i] == L'\n') {
count++;
if (count == line) {
return i + 1;
}
}
ssize_t i = 0;
while (auto pos = str.find(L'\n')) {
i++;
if (i == line) return pos + 1;
}
return static_cast<size_t>(-1);
}
size_t parse_util_get_offset(const wcstring &str, int line, long line_offset) {

View File

@ -79,7 +79,7 @@ void parse_util_token_extent(const wchar_t *buff, size_t cursor_pos, const wchar
const wchar_t **prev_end);
/// 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.
int parse_util_get_line_from_offset(const wcstring &str, size_t pos);

View File

@ -3573,7 +3573,7 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat
else
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) {
auto indents = parse_util_compute_indents(el->text());