Shell: String leading and trailing whitespace from commands

This fixes a bug of inserting empty argument if command had trailing
whitespace
This commit is contained in:
Bananymous 2023-09-28 10:28:49 +03:00
parent 27adb9486b
commit c84b66d078
1 changed files with 17 additions and 0 deletions

View File

@ -159,6 +159,21 @@ BAN::Optional<BAN::String> parse_dollar(BAN::StringView command, size_t& i)
return "$"sv; return "$"sv;
} }
BAN::StringView strip_whitespace(BAN::StringView sv)
{
size_t leading = 0;
while (leading < sv.size() && isspace(sv[leading]))
leading++;
sv = sv.substring(leading);
size_t trailing = 0;
while (trailing < sv.size() && isspace(sv[sv.size() - trailing - 1]))
trailing++;
sv = sv.substring(0, sv.size() - trailing);
return sv;
}
BAN::Vector<BAN::Vector<BAN::String>> parse_command(BAN::StringView command_view) BAN::Vector<BAN::Vector<BAN::String>> parse_command(BAN::StringView command_view)
{ {
enum class State enum class State
@ -168,6 +183,8 @@ BAN::Vector<BAN::Vector<BAN::String>> parse_command(BAN::StringView command_view
DoubleQuote, DoubleQuote,
}; };
command_view = strip_whitespace(command_view);
BAN::Vector<BAN::Vector<BAN::String>> result; BAN::Vector<BAN::Vector<BAN::String>> result;
BAN::Vector<BAN::String> command_args; BAN::Vector<BAN::String> command_args;