Mercurial > hg > svcore
diff base/StringBits.cpp @ 1022:eecf544bed92
Unit tests for StringBits::splitQuoted
author | Chris Cannam |
---|---|
date | Mon, 01 Dec 2014 15:42:58 +0000 |
parents | a73c44ae5acb |
children | 48e9f538e6e9 |
line wrap: on
line diff
--- a/base/StringBits.cpp Mon Dec 01 10:18:55 2014 +0000 +++ b/base/StringBits.cpp Mon Dec 01 15:42:58 2014 +0000 @@ -20,6 +20,10 @@ #include "StringBits.h" +#include "Debug.h" + +using namespace std; + double StringBits::stringToDoubleLocaleFree(QString s, bool *ok) { @@ -73,6 +77,11 @@ QStringList tokens; QString tok; + // sep -> just seen a field separator (or start of line) + // unq -> in an unquoted field + // q1 -> in a single-quoted field + // q2 -> in a double-quoted field + enum { sep, unq, q1, q2 } mode = sep; for (int i = 0; i < s.length(); ++i) { @@ -117,86 +126,19 @@ } } - if (tok != "" || mode != sep) tokens << tok; + if (tok != "" || mode != sep) { + if (mode == q1) { + tokens << ("'" + tok); // turns out it wasn't quoted after all + } else if (mode == q2) { + tokens << ("\"" + tok); + } else { + tokens << tok; + } + } + return tokens; } -/* - -void testSplit() -{ - QStringList tests; - tests << "a b c d"; - tests << "a \"b c\" d"; - tests << "a 'b c' d"; - tests << "a \"b c\\\" d\""; - tests << "a 'b c\\' d'"; - tests << "a \"b c' d\""; - tests << "a 'b c\" d'"; - tests << "aa 'bb cc\" dd'"; - tests << "a'a 'bb' \\\"cc\" dd\\\""; - tests << " a'a \\\' 'bb' \' \\\"cc\" ' dd\\\" '"; - - for (int j = 0; j < tests.size(); ++j) { - cout << endl; - cout << tests[j] << endl; - cout << "->" << endl << "("; - QStringList l = splitQuoted(tests[j], ' '); - for (int i = 0; i < l.size(); ++i) { - if (i > 0) cout << ";"; - cout << l[i]; - } - cout << ")" << endl; - } -} - -*/ - -/* - Results: - -a b c d --> -(a;b;c;d) - -a "b c" d --> -(a;b c;d) - -a 'b c' d --> -(a;b c;d) - -a "b c\" d" --> -(a;b c" d) - -a 'b c\' d' --> -(a;b c' d) - -a "b c' d" --> -(a;b c' d) - -a 'b c" d' --> -(a;b c" d) - -aa 'bb cc" dd' --> -(aa;bb cc" dd) - -a'a 'bb' \"cc" dd\" --> -(a'a;bb;"cc";dd") - - a'a \' 'bb' ' \"cc" ' dd\" ' --> -(a'a;';bb; "cc" ;dd";) - -*/ - QStringList StringBits::split(QString line, QChar separator, bool quoted) {