avoid having to use external test runner script by parsing environment variables directly

This commit is contained in:
Axel Kohlmeyer
2020-06-24 09:32:59 -04:00
parent 8cec13a038
commit ee5be42026
6 changed files with 137 additions and 28 deletions

View File

@ -430,6 +430,86 @@ size_t utils::trim_and_count_words(const std::string & text, const std::string &
return utils::count_words(utils::trim_comment(text), separators);
}
/* ----------------------------------------------------------------------
Convert string into words on whitespace while handling single and
double quotes.
------------------------------------------------------------------------- */
std::vector<std::string> utils::split_words(const std::string &text)
{
std::vector<std::string> list;
const char *buf = text.c_str();
std::size_t beg = 0;
std::size_t len = 0;
char c = *buf;
while (c) {
// leading whitespace
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') {
c = *++buf;
++beg;
continue;
};
len = 0;
// handle escaped/quoted text.
quoted:
// handle single quote
if (c == '\'') {
c = *++buf;
++len;
while (((c != '\'') && (c != '\0'))
|| ((c == '\\') && (buf[1] == '\''))) {
if ((c == '\\') && (buf[1] == '\'')) {
++buf;
++len;
}
c = *++buf;
++len;
}
c = *++buf;
++len;
// handle double quote
} else if (c == '"') {
c = *++buf;
++len;
while (((c != '"') && (c != '\0'))
|| ((c == '\\') && (buf[1] == '"'))) {
if ((c == '\\') && (buf[1] == '"')) {
++buf;
++len;
}
c = *++buf;
++len;
}
c = *++buf;
++len;
}
// unquoted
while (1) {
if ((c == '\'') || (c == '"')) goto quoted;
// skip escaped quote
if ((c == '\\') && ((buf[1] == '\'') || (buf[1] == '"'))) {
++buf;
++len;
c = *++buf;
++len;
}
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n')
|| (c == '\f') || (c == '\0')) {
list.push_back(text.substr(beg,len));
beg += len;
break;
}
c = *++buf;
++len;
}
}
return list;
}
/* ----------------------------------------------------------------------
Return whether string is a valid integer number
------------------------------------------------------------------------- */