author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
#include <QDebug> |
|
42 |
#include <QRegExp> |
|
43 |
#include <QString> |
|
44 |
||
45 |
#include <qtest.h> |
|
46 |
||
47 |
||
48 |
class tst_qregexp : public QObject |
|
49 |
{ |
|
50 |
Q_OBJECT |
|
51 |
private slots: |
|
52 |
void escape_old(); |
|
53 |
void escape_old_data() { escape_data(); } |
|
54 |
void escape_new1(); |
|
55 |
void escape_new1_data() { escape_data(); } |
|
56 |
void escape_new2(); |
|
57 |
void escape_new2_data() { escape_data(); } |
|
58 |
void escape_new3(); |
|
59 |
void escape_new3_data() { escape_data(); } |
|
60 |
void escape_new4(); |
|
61 |
void escape_new4_data() { escape_data(); } |
|
62 |
private: |
|
63 |
void escape_data(); |
|
64 |
}; |
|
65 |
||
66 |
||
67 |
static void verify(const QString "ed, const QString &expected) |
|
68 |
{ |
|
69 |
if (quoted != expected) |
|
70 |
qDebug() << "ERROR:" << quoted << expected; |
|
71 |
} |
|
72 |
||
73 |
void tst_qregexp::escape_data() |
|
74 |
{ |
|
75 |
QTest::addColumn<QString>("pattern"); |
|
76 |
QTest::addColumn<QString>("expected"); |
|
77 |
||
78 |
QTest::newRow("escape 0") << "Hello world" << "Hello world"; |
|
79 |
QTest::newRow("escape 1") << "(Hello world)" << "\\(Hello world\\)"; |
|
80 |
{ |
|
81 |
QString s; |
|
82 |
for (int i = 0; i < 10; ++i) |
|
83 |
s += "(escape)"; |
|
84 |
QTest::newRow("escape 10") << s << QRegExp::escape(s); |
|
85 |
} |
|
86 |
{ |
|
87 |
QString s; |
|
88 |
for (int i = 0; i < 100; ++i) |
|
89 |
s += "(escape)"; |
|
90 |
QTest::newRow("escape 100") << s << QRegExp::escape(s); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
void tst_qregexp::escape_old() |
|
95 |
{ |
|
96 |
QFETCH(QString, pattern); |
|
97 |
QFETCH(QString, expected); |
|
98 |
||
99 |
QBENCHMARK { |
|
100 |
static const char meta[] = "$()*+.?[\\]^{|}"; |
|
101 |
QString quoted = pattern; |
|
102 |
int i = 0; |
|
103 |
||
104 |
while (i < quoted.length()) { |
|
105 |
if (strchr(meta, quoted.at(i).toLatin1()) != 0) |
|
106 |
quoted.insert(i++, QLatin1Char('\\')); |
|
107 |
++i; |
|
108 |
} |
|
109 |
||
110 |
verify(quoted, expected); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
void tst_qregexp::escape_new1() |
|
115 |
{ |
|
116 |
QFETCH(QString, pattern); |
|
117 |
QFETCH(QString, expected); |
|
118 |
||
119 |
QBENCHMARK { |
|
120 |
QString quoted; |
|
121 |
const int count = pattern.count(); |
|
122 |
quoted.reserve(count * 2); |
|
123 |
const QLatin1Char backslash('\\'); |
|
124 |
for (int i = 0; i < count; i++) { |
|
125 |
switch (pattern.at(i).toLatin1()) { |
|
126 |
case '$': |
|
127 |
case '(': |
|
128 |
case ')': |
|
129 |
case '*': |
|
130 |
case '+': |
|
131 |
case '.': |
|
132 |
case '?': |
|
133 |
case '[': |
|
134 |
case '\\': |
|
135 |
case ']': |
|
136 |
case '^': |
|
137 |
case '{': |
|
138 |
case '|': |
|
139 |
case '}': |
|
140 |
quoted.append(backslash); |
|
141 |
} |
|
142 |
quoted.append(pattern.at(i)); |
|
143 |
} |
|
144 |
verify(quoted, expected); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
void tst_qregexp::escape_new2() |
|
149 |
{ |
|
150 |
QFETCH(QString, pattern); |
|
151 |
QFETCH(QString, expected); |
|
152 |
||
153 |
QBENCHMARK { |
|
154 |
int count = pattern.count(); |
|
155 |
const QLatin1Char backslash('\\'); |
|
156 |
QString quoted(count * 2, backslash); |
|
157 |
const QChar *patternData = pattern.data(); |
|
158 |
QChar *quotedData = quoted.data(); |
|
159 |
int escaped = 0; |
|
160 |
for ( ; --count >= 0; ++patternData) { |
|
161 |
const QChar c = *patternData; |
|
162 |
switch (c.unicode()) { |
|
163 |
case '$': |
|
164 |
case '(': |
|
165 |
case ')': |
|
166 |
case '*': |
|
167 |
case '+': |
|
168 |
case '.': |
|
169 |
case '?': |
|
170 |
case '[': |
|
171 |
case '\\': |
|
172 |
case ']': |
|
173 |
case '^': |
|
174 |
case '{': |
|
175 |
case '|': |
|
176 |
case '}': |
|
177 |
++escaped; |
|
178 |
++quotedData; |
|
179 |
} |
|
180 |
*quotedData = c; |
|
181 |
++quotedData; |
|
182 |
} |
|
183 |
quoted.resize(pattern.size() + escaped); |
|
184 |
||
185 |
verify(quoted, expected); |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
void tst_qregexp::escape_new3() |
|
190 |
{ |
|
191 |
QFETCH(QString, pattern); |
|
192 |
QFETCH(QString, expected); |
|
193 |
||
194 |
QBENCHMARK { |
|
195 |
QString quoted; |
|
196 |
const int count = pattern.count(); |
|
197 |
quoted.reserve(count * 2); |
|
198 |
const QLatin1Char backslash('\\'); |
|
199 |
for (int i = 0; i < count; i++) { |
|
200 |
switch (pattern.at(i).toLatin1()) { |
|
201 |
case '$': |
|
202 |
case '(': |
|
203 |
case ')': |
|
204 |
case '*': |
|
205 |
case '+': |
|
206 |
case '.': |
|
207 |
case '?': |
|
208 |
case '[': |
|
209 |
case '\\': |
|
210 |
case ']': |
|
211 |
case '^': |
|
212 |
case '{': |
|
213 |
case '|': |
|
214 |
case '}': |
|
215 |
quoted += backslash; |
|
216 |
} |
|
217 |
quoted += pattern.at(i); |
|
218 |
} |
|
219 |
||
220 |
verify(quoted, expected); |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
||
225 |
static inline bool needsEscaping(int c) |
|
226 |
{ |
|
227 |
switch (c) { |
|
228 |
case '$': |
|
229 |
case '(': |
|
230 |
case ')': |
|
231 |
case '*': |
|
232 |
case '+': |
|
233 |
case '.': |
|
234 |
case '?': |
|
235 |
case '[': |
|
236 |
case '\\': |
|
237 |
case ']': |
|
238 |
case '^': |
|
239 |
case '{': |
|
240 |
case '|': |
|
241 |
case '}': |
|
242 |
return true; |
|
243 |
} |
|
244 |
return false; |
|
245 |
} |
|
246 |
||
247 |
void tst_qregexp::escape_new4() |
|
248 |
{ |
|
249 |
QFETCH(QString, pattern); |
|
250 |
QFETCH(QString, expected); |
|
251 |
||
252 |
QBENCHMARK { |
|
253 |
const int n = pattern.size(); |
|
254 |
const QChar *patternData = pattern.data(); |
|
255 |
// try to prevent copy if no escape is needed |
|
256 |
int i = 0; |
|
257 |
for (int i = 0; i != n; ++i) { |
|
258 |
const QChar c = patternData[i]; |
|
259 |
if (needsEscaping(c.unicode())) |
|
260 |
break; |
|
261 |
} |
|
262 |
if (i == n) { |
|
263 |
verify(pattern, expected); |
|
264 |
// no escaping needed, "return pattern" should be done here. |
|
265 |
return; |
|
266 |
} |
|
267 |
const QLatin1Char backslash('\\'); |
|
268 |
QString quoted(n * 2, backslash); |
|
269 |
QChar *quotedData = quoted.data(); |
|
270 |
for (int j = 0; j != i; ++j) |
|
271 |
*quotedData++ = *patternData++; |
|
272 |
int escaped = 0; |
|
273 |
for (; i != n; ++i) { |
|
274 |
const QChar c = *patternData; |
|
275 |
if (needsEscaping(c.unicode())) { |
|
276 |
++escaped; |
|
277 |
++quotedData; |
|
278 |
} |
|
279 |
*quotedData = c; |
|
280 |
++quotedData; |
|
281 |
++patternData; |
|
282 |
} |
|
283 |
quoted.resize(n + escaped); |
|
284 |
verify(quoted, expected); |
|
285 |
// "return quoted" |
|
286 |
} |
|
287 |
} |
|
288 |
QTEST_MAIN(tst_qregexp) |
|
289 |
||
290 |
#include "main.moc" |