terminal: construct the commandline only once

This commit is contained in:
Maxim Prokhorov
2021-04-07 03:38:35 +03:00
parent 273d19f3c6
commit ae63e5fc0b
2 changed files with 11 additions and 9 deletions

View File

@@ -92,8 +92,8 @@ static char hex_digit_to_int(char c) {
CommandLine parse_commandline(const char *line) {
const char *p = line;
CommandLine result {{}, 0};
result.argv.reserve(4);
Argv argv;
argv.reserve(4);
String current;
@@ -186,18 +186,18 @@ CommandLine parse_commandline(const char *line) {
if (*p) p++;
}
/* add the token to the vector */
result.argv.emplace_back(std::move(current));
++result.argc;
argv.emplace_back(std::move(current));
} else {
/* Even on empty input string return something not NULL. */
return result;
goto out;
}
}
err:
result.argc = 0;
result.argv.clear();
return result;
argv.clear();
out:
size_t argc = argv.size();
return CommandLine{std::move(argv), argc};
}
// FowlerNollVo hash function to hash command strings that treats input as lowercase

View File

@@ -18,8 +18,10 @@ namespace parsing {
// Generic command line parser
// - split each arg from the input line and put them into the argv array
// - argc is expected to be equal to the argv
using Argv = std::vector<String>;
struct CommandLine {
std::vector<String> argv;
Argv argv;
size_t argc;
};