comparison 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
comparison
equal deleted inserted replaced
1021:1888ca033a84 1022:eecf544bed92
17 Rosegarden MIDI and audio sequencer and notation editor. 17 Rosegarden MIDI and audio sequencer and notation editor.
18 This file copyright 2000-2010 Chris Cannam. 18 This file copyright 2000-2010 Chris Cannam.
19 */ 19 */
20 20
21 #include "StringBits.h" 21 #include "StringBits.h"
22
23 #include "Debug.h"
24
25 using namespace std;
22 26
23 double 27 double
24 StringBits::stringToDoubleLocaleFree(QString s, bool *ok) 28 StringBits::stringToDoubleLocaleFree(QString s, bool *ok)
25 { 29 {
26 int dp = 0; 30 int dp = 0;
71 StringBits::splitQuoted(QString s, QChar separator) 75 StringBits::splitQuoted(QString s, QChar separator)
72 { 76 {
73 QStringList tokens; 77 QStringList tokens;
74 QString tok; 78 QString tok;
75 79
80 // sep -> just seen a field separator (or start of line)
81 // unq -> in an unquoted field
82 // q1 -> in a single-quoted field
83 // q2 -> in a double-quoted field
84
76 enum { sep, unq, q1, q2 } mode = sep; 85 enum { sep, unq, q1, q2 } mode = sep;
77 86
78 for (int i = 0; i < s.length(); ++i) { 87 for (int i = 0; i < s.length(); ++i) {
79 88
80 QChar c = s[i]; 89 QChar c = s[i];
115 case unq: case q1: case q2: tok += c; break; 124 case unq: case q1: case q2: tok += c; break;
116 } 125 }
117 } 126 }
118 } 127 }
119 128
120 if (tok != "" || mode != sep) tokens << tok; 129 if (tok != "" || mode != sep) {
130 if (mode == q1) {
131 tokens << ("'" + tok); // turns out it wasn't quoted after all
132 } else if (mode == q2) {
133 tokens << ("\"" + tok);
134 } else {
135 tokens << tok;
136 }
137 }
138
121 return tokens; 139 return tokens;
122 } 140 }
123
124 /*
125
126 void testSplit()
127 {
128 QStringList tests;
129 tests << "a b c d";
130 tests << "a \"b c\" d";
131 tests << "a 'b c' d";
132 tests << "a \"b c\\\" d\"";
133 tests << "a 'b c\\' d'";
134 tests << "a \"b c' d\"";
135 tests << "a 'b c\" d'";
136 tests << "aa 'bb cc\" dd'";
137 tests << "a'a 'bb' \\\"cc\" dd\\\"";
138 tests << " a'a \\\' 'bb' \' \\\"cc\" ' dd\\\" '";
139
140 for (int j = 0; j < tests.size(); ++j) {
141 cout << endl;
142 cout << tests[j] << endl;
143 cout << "->" << endl << "(";
144 QStringList l = splitQuoted(tests[j], ' ');
145 for (int i = 0; i < l.size(); ++i) {
146 if (i > 0) cout << ";";
147 cout << l[i];
148 }
149 cout << ")" << endl;
150 }
151 }
152
153 */
154
155 /*
156 Results:
157
158 a b c d
159 ->
160 (a;b;c;d)
161
162 a "b c" d
163 ->
164 (a;b c;d)
165
166 a 'b c' d
167 ->
168 (a;b c;d)
169
170 a "b c\" d"
171 ->
172 (a;b c" d)
173
174 a 'b c\' d'
175 ->
176 (a;b c' d)
177
178 a "b c' d"
179 ->
180 (a;b c' d)
181
182 a 'b c" d'
183 ->
184 (a;b c" d)
185
186 aa 'bb cc" dd'
187 ->
188 (aa;bb cc" dd)
189
190 a'a 'bb' \"cc" dd\"
191 ->
192 (a'a;bb;"cc";dd")
193
194 a'a \' 'bb' ' \"cc" ' dd\" '
195 ->
196 (a'a;';bb; "cc" ;dd";)
197
198 */
199 141
200 QStringList 142 QStringList
201 StringBits::split(QString line, QChar separator, bool quoted) 143 StringBits::split(QString line, QChar separator, bool quoted)
202 { 144 {
203 if (quoted) { 145 if (quoted) {