Simplify parse_util_get_parameter_info

We no longer use any part of the "parameter info" except its quote type.
Just return the quote type directly.
This commit is contained in:
ridiculousfish 2021-06-25 22:40:43 -07:00
parent 083d8c5d23
commit a2dfd87928
3 changed files with 10 additions and 47 deletions

View File

@ -489,7 +489,7 @@ wcstring parse_util_unescape_wildcards(const wcstring &str) {
static wchar_t get_quote(const wcstring &cmd_str, size_t len) {
size_t i = 0;
wchar_t res = 0;
const wchar_t *const cmd = cmd_str.c_str();
const wchar_t *cmd = cmd_str.c_str();
while (true) {
if (!cmd[i]) break;
@ -501,7 +501,6 @@ static wchar_t get_quote(const wcstring &cmd_str, size_t len) {
} else {
if (cmd[i] == L'\'' || cmd[i] == L'\"') {
const wchar_t *end = quote_end(&cmd[i], cmd[i]);
// std::fwprintf( stderr, L"Jump %d\n", end-cmd );
if ((end == nullptr) || (!*end) || (end > cmd + len)) {
res = cmd[i];
break;
@ -515,47 +514,15 @@ static wchar_t get_quote(const wcstring &cmd_str, size_t len) {
return res;
}
void parse_util_get_parameter_info(const wcstring &cmd, const size_t pos, wchar_t *quote,
size_t *offset, token_type_t *out_type) {
size_t prev_pos = 0;
wchar_t last_quote = L'\0';
wchar_t parse_util_get_quote_type(const wcstring &cmd, size_t pos) {
tokenizer_t tok(cmd.c_str(), TOK_ACCEPT_UNFINISHED);
while (auto token = tok.next()) {
if (token->offset > pos) break;
if (token->type == token_type_t::string)
last_quote = get_quote(tok.text_of(*token), pos - token->offset);
if (out_type != nullptr) *out_type = token->type;
prev_pos = token->offset;
}
wchar_t *cmd_tmp = wcsdup(cmd.c_str());
cmd_tmp[pos] = 0;
size_t cmdlen = pos;
bool finished = cmdlen != 0;
if (finished) {
finished = (quote == nullptr);
if (finished && std::wcschr(L" \t\n\r", cmd_tmp[cmdlen - 1])) {
finished = cmdlen > 1 && cmd_tmp[cmdlen - 2] == L'\\';
if (token->type == token_type_t::string &&
token->location_in_or_at_end_of_source_range(pos)) {
return get_quote(tok.text_of(*token), pos - token->offset);
}
}
if (quote) *quote = last_quote;
if (offset != nullptr) {
if (finished) {
while ((cmd_tmp[prev_pos] != 0) && (std::wcschr(L";|", cmd_tmp[prev_pos]) != nullptr))
prev_pos++;
*offset = prev_pos;
} else {
*offset = pos;
}
}
free(cmd_tmp);
return L'\0';
}
wcstring parse_util_escape_string_with_quote(const wcstring &cmd, wchar_t quote, bool no_tilde) {

View File

@ -100,12 +100,8 @@ bool parse_util_argument_is_help(const wcstring &s);
///
/// \param cmd The command to be analyzed
/// \param pos An index in the string which is inside the parameter
/// \param quote If not NULL, store the type of quote this parameter has, can be either ', " or \\0,
/// meaning the string is not quoted.
/// \param offset If not NULL, get_param will store the offset to the beginning of the parameter.
/// \param out_type If not NULL, get_param will store the token type.
void parse_util_get_parameter_info(const wcstring &cmd, const size_t pos, wchar_t *quote,
size_t *offset, token_type_t *out_type);
/// \return the type of quote used by the parameter: either ' or " or \0.
wchar_t parse_util_get_quote_type(const wcstring &cmd, size_t pos);
/// Attempts to escape the string 'cmd' using the given quote type, as determined by the quote
/// character. The quote can be a single quote or double quote, or L'\0' to indicate no quoting (and

View File

@ -1592,9 +1592,9 @@ wcstring completion_apply_to_command_line(const wcstring &val, complete_flags_t
// Find the last quote in the token to complete. By parsing only the string inside any
// command substitution, we prevent the tokenizer from treating the entire command
// substitution as one token.
parse_util_get_parameter_info(
quote = parse_util_get_quote_type(
command_line.substr(cmdsub_offset, (cmdsub_end - cmdsub_begin)),
cursor_pos - cmdsub_offset, &quote, nullptr, nullptr);
cursor_pos - cmdsub_offset);
// If the token is reported as unquoted, but ends with a (unescaped) quote, and we can
// modify the command line, then delete the trailing quote so that we can insert within