db/proxy: simplify csvToStrings()

This commit is contained in:
Max Kellermann 2014-01-22 23:17:42 +01:00
parent 546e7cafa4
commit 0ec1808956

View File

@ -143,56 +143,26 @@ csvToStrings(const char *s, T &tokens)
{ {
std::string current; std::string current;
tokens.clear(); tokens.clear();
enum states {TOKEN, ESCAPE};
states state = TOKEN;
while (*s != 0) { while (true) {
const char ch = *s++; char ch = *s++;
if (ch == 0) {
switch (ch) { tokens.push_back(current);
case ',': return true;
switch(state) {
case TOKEN:
tokens.push_back(current);
current.clear();
continue;
case ESCAPE:
current += ',';
state = TOKEN;
continue;
}
break;
case '\\':
switch(state) {
case TOKEN:
state=ESCAPE;
continue;
case ESCAPE:
current += '\\';
state = TOKEN;
continue;
}
break;
default:
switch(state) {
case ESCAPE:
state = TOKEN;
break;
case TOKEN:
break;
}
current += ch;
} }
if (ch == '\\') {
ch = *s++;
if (ch == 0)
return false;
} else if (ch == ',') {
tokens.push_back(current);
current.clear();
continue;
}
current.push_back(ch);
} }
switch(state) {
case TOKEN:
tokens.push_back(current);
break;
case ESCAPE:
return false;
}
return true;
} }
template bool csvToStrings<std::list<std::string>>(const char *, std::list<std::string> &); template bool csvToStrings<std::list<std::string>>(const char *, std::list<std::string> &);