Hier mal ein Versuch ein hübscheren tokenizer zu bauen:
/// \todo TH use string_view
std::list<std::string> tokenize(const std::string& str) {
std::list<std::string> tokens;
std::string token;
auto add = [&tokens](std::string& token) {
if(not token.empty()) {
tokens.emplace_back(std::move(token));
}
};
for(const char c : str) {
if(c == ' ') {
add(token);
} else if(c == '(' or c == ')') {
add (token);
tokens.emplace_back(std::string{c});
} else {
token.push_back(c);
}
}
add(token);
return tokens;
}
Ist auf alle Fälle Millionen mal besser als Dieses hier:
https://gist.github.com/ofan/721464
std::list<std::string> tokenize(const std::string & str)
{
std::list<std::string> tokens;
const char * s = str.c_str();
while (*s) {
while (*s == ' ')
++s;
if (*s == '(' || *s == ')')
tokens.push_back(*s++ == '(' ? "(" : ")");
else {
const char * t = s;
while (*t && *t != ' ' && *t != '(' && *t != ')')
++t;
tokens.push_back(std::string(s, t));
s = t;
}
}
return tokens;
}
hf