Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qregexp.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
24 | ** | - | ||||||||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
35 | ** | - | ||||||||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
37 | ** | - | ||||||||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | #include "qregexp.h" | - | ||||||||||||||||||
41 | - | |||||||||||||||||||
42 | #include "qalgorithms.h" | - | ||||||||||||||||||
43 | #include "qbitarray.h" | - | ||||||||||||||||||
44 | #include "qcache.h" | - | ||||||||||||||||||
45 | #include "qdatastream.h" | - | ||||||||||||||||||
46 | #include "qdebug.h" | - | ||||||||||||||||||
47 | #include "qhashfunctions.h" | - | ||||||||||||||||||
48 | #include "qlist.h" | - | ||||||||||||||||||
49 | #include "qmap.h" | - | ||||||||||||||||||
50 | #include "qmutex.h" | - | ||||||||||||||||||
51 | #include "qstring.h" | - | ||||||||||||||||||
52 | #include "qstringlist.h" | - | ||||||||||||||||||
53 | #include "qstringmatcher.h" | - | ||||||||||||||||||
54 | #include "qvector.h" | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | #include <limits.h> | - | ||||||||||||||||||
57 | #include <algorithm> | - | ||||||||||||||||||
58 | - | |||||||||||||||||||
59 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
60 | - | |||||||||||||||||||
61 | int qFindString(const QChar *haystack, int haystackLen, int from, | - | ||||||||||||||||||
62 | const QChar *needle, int needleLen, Qt::CaseSensitivity cs); | - | ||||||||||||||||||
63 | - | |||||||||||||||||||
64 | // error strings for the regexp parser | - | ||||||||||||||||||
65 | #define RXERR_OK QT_TRANSLATE_NOOP("QRegExp", "no error occurred") | - | ||||||||||||||||||
66 | #define RXERR_DISABLED QT_TRANSLATE_NOOP("QRegExp", "disabled feature used") | - | ||||||||||||||||||
67 | #define RXERR_CHARCLASS QT_TRANSLATE_NOOP("QRegExp", "bad char class syntax") | - | ||||||||||||||||||
68 | #define RXERR_LOOKAHEAD QT_TRANSLATE_NOOP("QRegExp", "bad lookahead syntax") | - | ||||||||||||||||||
69 | #define RXERR_LOOKBEHIND QT_TRANSLATE_NOOP("QRegExp", "lookbehinds not supported, see QTBUG-2371") | - | ||||||||||||||||||
70 | #define RXERR_REPETITION QT_TRANSLATE_NOOP("QRegExp", "bad repetition syntax") | - | ||||||||||||||||||
71 | #define RXERR_OCTAL QT_TRANSLATE_NOOP("QRegExp", "invalid octal value") | - | ||||||||||||||||||
72 | #define RXERR_LEFTDELIM QT_TRANSLATE_NOOP("QRegExp", "missing left delim") | - | ||||||||||||||||||
73 | #define RXERR_END QT_TRANSLATE_NOOP("QRegExp", "unexpected end") | - | ||||||||||||||||||
74 | #define RXERR_LIMIT QT_TRANSLATE_NOOP("QRegExp", "met internal limit") | - | ||||||||||||||||||
75 | #define RXERR_INTERVAL QT_TRANSLATE_NOOP("QRegExp", "invalid interval") | - | ||||||||||||||||||
76 | #define RXERR_CATEGORY QT_TRANSLATE_NOOP("QRegExp", "invalid category") | - | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | /*! | - | ||||||||||||||||||
79 | \class QRegExp | - | ||||||||||||||||||
80 | \inmodule QtCore | - | ||||||||||||||||||
81 | \reentrant | - | ||||||||||||||||||
82 | \brief The QRegExp class provides pattern matching using regular expressions. | - | ||||||||||||||||||
83 | - | |||||||||||||||||||
84 | \ingroup tools | - | ||||||||||||||||||
85 | \ingroup shared | - | ||||||||||||||||||
86 | - | |||||||||||||||||||
87 | \keyword regular expression | - | ||||||||||||||||||
88 | - | |||||||||||||||||||
89 | A regular expression, or "regexp", is a pattern for matching | - | ||||||||||||||||||
90 | substrings in a text. This is useful in many contexts, e.g., | - | ||||||||||||||||||
91 | - | |||||||||||||||||||
92 | \table | - | ||||||||||||||||||
93 | \row \li Validation | - | ||||||||||||||||||
94 | \li A regexp can test whether a substring meets some criteria, | - | ||||||||||||||||||
95 | e.g. is an integer or contains no whitespace. | - | ||||||||||||||||||
96 | \row \li Searching | - | ||||||||||||||||||
97 | \li A regexp provides more powerful pattern matching than | - | ||||||||||||||||||
98 | simple substring matching, e.g., match one of the words | - | ||||||||||||||||||
99 | \e{mail}, \e{letter} or \e{correspondence}, but none of the | - | ||||||||||||||||||
100 | words \e{email}, \e{mailman}, \e{mailer}, \e{letterbox}, etc. | - | ||||||||||||||||||
101 | \row \li Search and Replace | - | ||||||||||||||||||
102 | \li A regexp can replace all occurrences of a substring with a | - | ||||||||||||||||||
103 | different substring, e.g., replace all occurrences of \e{&} | - | ||||||||||||||||||
104 | with \e{\&} except where the \e{&} is already followed by | - | ||||||||||||||||||
105 | an \e{amp;}. | - | ||||||||||||||||||
106 | \row \li String Splitting | - | ||||||||||||||||||
107 | \li A regexp can be used to identify where a string should be | - | ||||||||||||||||||
108 | split apart, e.g. splitting tab-delimited strings. | - | ||||||||||||||||||
109 | \endtable | - | ||||||||||||||||||
110 | - | |||||||||||||||||||
111 | A brief introduction to regexps is presented, a description of | - | ||||||||||||||||||
112 | Qt's regexp language, some examples, and the function | - | ||||||||||||||||||
113 | documentation itself. QRegExp is modeled on Perl's regexp | - | ||||||||||||||||||
114 | language. It fully supports Unicode. QRegExp can also be used in a | - | ||||||||||||||||||
115 | simpler, \e{wildcard mode} that is similar to the functionality | - | ||||||||||||||||||
116 | found in command shells. The syntax rules used by QRegExp can be | - | ||||||||||||||||||
117 | changed with setPatternSyntax(). In particular, the pattern syntax | - | ||||||||||||||||||
118 | can be set to QRegExp::FixedString, which means the pattern to be | - | ||||||||||||||||||
119 | matched is interpreted as a plain string, i.e., special characters | - | ||||||||||||||||||
120 | (e.g., backslash) are not escaped. | - | ||||||||||||||||||
121 | - | |||||||||||||||||||
122 | A good text on regexps is \e {Mastering Regular Expressions} | - | ||||||||||||||||||
123 | (Third Edition) by Jeffrey E. F. Friedl, ISBN 0-596-52812-4. | - | ||||||||||||||||||
124 | - | |||||||||||||||||||
125 | \note In Qt 5, the new QRegularExpression class provides a Perl | - | ||||||||||||||||||
126 | compatible implementation of regular expressions and is recommended | - | ||||||||||||||||||
127 | in place of QRegExp. | - | ||||||||||||||||||
128 | - | |||||||||||||||||||
129 | \tableofcontents | - | ||||||||||||||||||
130 | - | |||||||||||||||||||
131 | \section1 Introduction | - | ||||||||||||||||||
132 | - | |||||||||||||||||||
133 | Regexps are built up from expressions, quantifiers, and | - | ||||||||||||||||||
134 | assertions. The simplest expression is a character, e.g. \b{x} | - | ||||||||||||||||||
135 | or \b{5}. An expression can also be a set of characters | - | ||||||||||||||||||
136 | enclosed in square brackets. \b{[ABCD]} will match an \b{A} | - | ||||||||||||||||||
137 | or a \b{B} or a \b{C} or a \b{D}. We can write this same | - | ||||||||||||||||||
138 | expression as \b{[A-D]}, and an expression to match any | - | ||||||||||||||||||
139 | capital letter in the English alphabet is written as | - | ||||||||||||||||||
140 | \b{[A-Z]}. | - | ||||||||||||||||||
141 | - | |||||||||||||||||||
142 | A quantifier specifies the number of occurrences of an expression | - | ||||||||||||||||||
143 | that must be matched. \b{x{1,1}} means match one and only one | - | ||||||||||||||||||
144 | \b{x}. \b{x{1,5}} means match a sequence of \b{x} | - | ||||||||||||||||||
145 | characters that contains at least one \b{x} but no more than | - | ||||||||||||||||||
146 | five. | - | ||||||||||||||||||
147 | - | |||||||||||||||||||
148 | Note that in general regexps cannot be used to check for balanced | - | ||||||||||||||||||
149 | brackets or tags. For example, a regexp can be written to match an | - | ||||||||||||||||||
150 | opening html \c{<b>} and its closing \c{</b>}, if the \c{<b>} tags | - | ||||||||||||||||||
151 | are not nested, but if the \c{<b>} tags are nested, that same | - | ||||||||||||||||||
152 | regexp will match an opening \c{<b>} tag with the wrong closing | - | ||||||||||||||||||
153 | \c{</b>}. For the fragment \c{<b>bold <b>bolder</b></b>}, the | - | ||||||||||||||||||
154 | first \c{<b>} would be matched with the first \c{</b>}, which is | - | ||||||||||||||||||
155 | not correct. However, it is possible to write a regexp that will | - | ||||||||||||||||||
156 | match nested brackets or tags correctly, but only if the number of | - | ||||||||||||||||||
157 | nesting levels is fixed and known. If the number of nesting levels | - | ||||||||||||||||||
158 | is not fixed and known, it is impossible to write a regexp that | - | ||||||||||||||||||
159 | will not fail. | - | ||||||||||||||||||
160 | - | |||||||||||||||||||
161 | Suppose we want a regexp to match integers in the range 0 to 99. | - | ||||||||||||||||||
162 | At least one digit is required, so we start with the expression | - | ||||||||||||||||||
163 | \b{[0-9]{1,1}}, which matches a single digit exactly once. This | - | ||||||||||||||||||
164 | regexp matches integers in the range 0 to 9. To match integers up | - | ||||||||||||||||||
165 | to 99, increase the maximum number of occurrences to 2, so the | - | ||||||||||||||||||
166 | regexp becomes \b{[0-9]{1,2}}. This regexp satisfies the | - | ||||||||||||||||||
167 | original requirement to match integers from 0 to 99, but it will | - | ||||||||||||||||||
168 | also match integers that occur in the middle of strings. If we | - | ||||||||||||||||||
169 | want the matched integer to be the whole string, we must use the | - | ||||||||||||||||||
170 | anchor assertions, \b{^} (caret) and \b{$} (dollar). When | - | ||||||||||||||||||
171 | \b{^} is the first character in a regexp, it means the regexp | - | ||||||||||||||||||
172 | must match from the beginning of the string. When \b{$} is the | - | ||||||||||||||||||
173 | last character of the regexp, it means the regexp must match to | - | ||||||||||||||||||
174 | the end of the string. The regexp becomes \b{^[0-9]{1,2}$}. | - | ||||||||||||||||||
175 | Note that assertions, e.g. \b{^} and \b{$}, do not match | - | ||||||||||||||||||
176 | characters but locations in the string. | - | ||||||||||||||||||
177 | - | |||||||||||||||||||
178 | If you have seen regexps described elsewhere, they may have looked | - | ||||||||||||||||||
179 | different from the ones shown here. This is because some sets of | - | ||||||||||||||||||
180 | characters and some quantifiers are so common that they have been | - | ||||||||||||||||||
181 | given special symbols to represent them. \b{[0-9]} can be | - | ||||||||||||||||||
182 | replaced with the symbol \b{\\d}. The quantifier to match | - | ||||||||||||||||||
183 | exactly one occurrence, \b{{1,1}}, can be replaced with the | - | ||||||||||||||||||
184 | expression itself, i.e. \b{x{1,1}} is the same as \b{x}. So | - | ||||||||||||||||||
185 | our 0 to 99 matcher could be written as \b{^\\d{1,2}$}. It can | - | ||||||||||||||||||
186 | also be written \b{^\\d\\d{0,1}$}, i.e. \e{From the start of | - | ||||||||||||||||||
187 | the string, match a digit, followed immediately by 0 or 1 digits}. | - | ||||||||||||||||||
188 | In practice, it would be written as \b{^\\d\\d?$}. The \b{?} | - | ||||||||||||||||||
189 | is shorthand for the quantifier \b{{0,1}}, i.e. 0 or 1 | - | ||||||||||||||||||
190 | occurrences. \b{?} makes an expression optional. The regexp | - | ||||||||||||||||||
191 | \b{^\\d\\d?$} means \e{From the beginning of the string, match | - | ||||||||||||||||||
192 | one digit, followed immediately by 0 or 1 more digit, followed | - | ||||||||||||||||||
193 | immediately by end of string}. | - | ||||||||||||||||||
194 | - | |||||||||||||||||||
195 | To write a regexp that matches one of the words 'mail' \e or | - | ||||||||||||||||||
196 | 'letter' \e or 'correspondence' but does not match words that | - | ||||||||||||||||||
197 | contain these words, e.g., 'email', 'mailman', 'mailer', and | - | ||||||||||||||||||
198 | 'letterbox', start with a regexp that matches 'mail'. Expressed | - | ||||||||||||||||||
199 | fully, the regexp is \b{m{1,1}a{1,1}i{1,1}l{1,1}}, but because | - | ||||||||||||||||||
200 | a character expression is automatically quantified by | - | ||||||||||||||||||
201 | \b{{1,1}}, we can simplify the regexp to \b{mail}, i.e., an | - | ||||||||||||||||||
202 | 'm' followed by an 'a' followed by an 'i' followed by an 'l'. Now | - | ||||||||||||||||||
203 | we can use the vertical bar \b{|}, which means \b{or}, to | - | ||||||||||||||||||
204 | include the other two words, so our regexp for matching any of the | - | ||||||||||||||||||
205 | three words becomes \b{mail|letter|correspondence}. Match | - | ||||||||||||||||||
206 | 'mail' \b{or} 'letter' \b{or} 'correspondence'. While this | - | ||||||||||||||||||
207 | regexp will match one of the three words we want to match, it will | - | ||||||||||||||||||
208 | also match words we don't want to match, e.g., 'email'. To | - | ||||||||||||||||||
209 | prevent the regexp from matching unwanted words, we must tell it | - | ||||||||||||||||||
210 | to begin and end the match at word boundaries. First we enclose | - | ||||||||||||||||||
211 | our regexp in parentheses, \b{(mail|letter|correspondence)}. | - | ||||||||||||||||||
212 | Parentheses group expressions together, and they identify a part | - | ||||||||||||||||||
213 | of the regexp that we wish to \l{capturing text}{capture}. | - | ||||||||||||||||||
214 | Enclosing the expression in parentheses allows us to use it as a | - | ||||||||||||||||||
215 | component in more complex regexps. It also allows us to examine | - | ||||||||||||||||||
216 | which of the three words was actually matched. To force the match | - | ||||||||||||||||||
217 | to begin and end on word boundaries, we enclose the regexp in | - | ||||||||||||||||||
218 | \b{\\b} \e{word boundary} assertions: | - | ||||||||||||||||||
219 | \b{\\b(mail|letter|correspondence)\\b}. Now the regexp means: | - | ||||||||||||||||||
220 | \e{Match a word boundary, followed by the regexp in parentheses, | - | ||||||||||||||||||
221 | followed by a word boundary}. The \b{\\b} assertion matches a | - | ||||||||||||||||||
222 | \e position in the regexp, not a \e character. A word boundary is | - | ||||||||||||||||||
223 | any non-word character, e.g., a space, newline, or the beginning | - | ||||||||||||||||||
224 | or ending of a string. | - | ||||||||||||||||||
225 | - | |||||||||||||||||||
226 | If we want to replace ampersand characters with the HTML entity | - | ||||||||||||||||||
227 | \b{\&}, the regexp to match is simply \b{\&}. But this | - | ||||||||||||||||||
228 | regexp will also match ampersands that have already been converted | - | ||||||||||||||||||
229 | to HTML entities. We want to replace only ampersands that are not | - | ||||||||||||||||||
230 | already followed by \b{amp;}. For this, we need the negative | - | ||||||||||||||||||
231 | lookahead assertion, \b{(?!}__\b{)}. The regexp can then be | - | ||||||||||||||||||
232 | written as \b{\&(?!amp;)}, i.e. \e{Match an ampersand that is} | - | ||||||||||||||||||
233 | \b{not} \e{followed by} \b{amp;}. | - | ||||||||||||||||||
234 | - | |||||||||||||||||||
235 | If we want to count all the occurrences of 'Eric' and 'Eirik' in a | - | ||||||||||||||||||
236 | string, two valid solutions are \b{\\b(Eric|Eirik)\\b} and | - | ||||||||||||||||||
237 | \b{\\bEi?ri[ck]\\b}. The word boundary assertion '\\b' is | - | ||||||||||||||||||
238 | required to avoid matching words that contain either name, | - | ||||||||||||||||||
239 | e.g. 'Ericsson'. Note that the second regexp matches more | - | ||||||||||||||||||
240 | spellings than we want: 'Eric', 'Erik', 'Eiric' and 'Eirik'. | - | ||||||||||||||||||
241 | - | |||||||||||||||||||
242 | Some of the examples discussed above are implemented in the | - | ||||||||||||||||||
243 | \l{#code-examples}{code examples} section. | - | ||||||||||||||||||
244 | - | |||||||||||||||||||
245 | \target characters-and-abbreviations-for-sets-of-characters | - | ||||||||||||||||||
246 | \section1 Characters and Abbreviations for Sets of Characters | - | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | \table | - | ||||||||||||||||||
249 | \header \li Element \li Meaning | - | ||||||||||||||||||
250 | \row \li \b{c} | - | ||||||||||||||||||
251 | \li A character represents itself unless it has a special | - | ||||||||||||||||||
252 | regexp meaning. e.g. \b{c} matches the character \e c. | - | ||||||||||||||||||
253 | \row \li \b{\\c} | - | ||||||||||||||||||
254 | \li A character that follows a backslash matches the character | - | ||||||||||||||||||
255 | itself, except as specified below. e.g., To match a literal | - | ||||||||||||||||||
256 | caret at the beginning of a string, write \b{\\^}. | - | ||||||||||||||||||
257 | \row \li \b{\\a} | - | ||||||||||||||||||
258 | \li Matches the ASCII bell (BEL, 0x07). | - | ||||||||||||||||||
259 | \row \li \b{\\f} | - | ||||||||||||||||||
260 | \li Matches the ASCII form feed (FF, 0x0C). | - | ||||||||||||||||||
261 | \row \li \b{\\n} | - | ||||||||||||||||||
262 | \li Matches the ASCII line feed (LF, 0x0A, Unix newline). | - | ||||||||||||||||||
263 | \row \li \b{\\r} | - | ||||||||||||||||||
264 | \li Matches the ASCII carriage return (CR, 0x0D). | - | ||||||||||||||||||
265 | \row \li \b{\\t} | - | ||||||||||||||||||
266 | \li Matches the ASCII horizontal tab (HT, 0x09). | - | ||||||||||||||||||
267 | \row \li \b{\\v} | - | ||||||||||||||||||
268 | \li Matches the ASCII vertical tab (VT, 0x0B). | - | ||||||||||||||||||
269 | \row \li \b{\\x\e{hhhh}} | - | ||||||||||||||||||
270 | \li Matches the Unicode character corresponding to the | - | ||||||||||||||||||
271 | hexadecimal number \e{hhhh} (between 0x0000 and 0xFFFF). | - | ||||||||||||||||||
272 | \row \li \b{\\0\e{ooo}} (i.e., \\zero \e{ooo}) | - | ||||||||||||||||||
273 | \li matches the ASCII/Latin1 character for the octal number | - | ||||||||||||||||||
274 | \e{ooo} (between 0 and 0377). | - | ||||||||||||||||||
275 | \row \li \b{. (dot)} | - | ||||||||||||||||||
276 | \li Matches any character (including newline). | - | ||||||||||||||||||
277 | \row \li \b{\\d} | - | ||||||||||||||||||
278 | \li Matches a digit (QChar::isDigit()). | - | ||||||||||||||||||
279 | \row \li \b{\\D} | - | ||||||||||||||||||
280 | \li Matches a non-digit. | - | ||||||||||||||||||
281 | \row \li \b{\\s} | - | ||||||||||||||||||
282 | \li Matches a whitespace character (QChar::isSpace()). | - | ||||||||||||||||||
283 | \row \li \b{\\S} | - | ||||||||||||||||||
284 | \li Matches a non-whitespace character. | - | ||||||||||||||||||
285 | \row \li \b{\\w} | - | ||||||||||||||||||
286 | \li Matches a word character (QChar::isLetterOrNumber(), QChar::isMark(), or '_'). | - | ||||||||||||||||||
287 | \row \li \b{\\W} | - | ||||||||||||||||||
288 | \li Matches a non-word character. | - | ||||||||||||||||||
289 | \row \li \b{\\\e{n}} | - | ||||||||||||||||||
290 | \li The \e{n}-th backreference, e.g. \\1, \\2, etc. | - | ||||||||||||||||||
291 | \endtable | - | ||||||||||||||||||
292 | - | |||||||||||||||||||
293 | \b{Note:} The C++ compiler transforms backslashes in strings. | - | ||||||||||||||||||
294 | To include a \b{\\} in a regexp, enter it twice, i.e. \c{\\}. | - | ||||||||||||||||||
295 | To match the backslash character itself, enter it four times, i.e. | - | ||||||||||||||||||
296 | \c{\\\\}. | - | ||||||||||||||||||
297 | - | |||||||||||||||||||
298 | \target sets-of-characters | - | ||||||||||||||||||
299 | \section1 Sets of Characters | - | ||||||||||||||||||
300 | - | |||||||||||||||||||
301 | Square brackets mean match any character contained in the square | - | ||||||||||||||||||
302 | brackets. The character set abbreviations described above can | - | ||||||||||||||||||
303 | appear in a character set in square brackets. Except for the | - | ||||||||||||||||||
304 | character set abbreviations and the following two exceptions, | - | ||||||||||||||||||
305 | characters do not have special meanings in square brackets. | - | ||||||||||||||||||
306 | - | |||||||||||||||||||
307 | \table | - | ||||||||||||||||||
308 | \row \li \b{^} | - | ||||||||||||||||||
309 | - | |||||||||||||||||||
310 | \li The caret negates the character set if it occurs as the | - | ||||||||||||||||||
311 | first character (i.e. immediately after the opening square | - | ||||||||||||||||||
312 | bracket). \b{[abc]} matches 'a' or 'b' or 'c', but | - | ||||||||||||||||||
313 | \b{[^abc]} matches anything \e but 'a' or 'b' or 'c'. | - | ||||||||||||||||||
314 | - | |||||||||||||||||||
315 | \row \li \b{-} | - | ||||||||||||||||||
316 | - | |||||||||||||||||||
317 | \li The dash indicates a range of characters. \b{[W-Z]} | - | ||||||||||||||||||
318 | matches 'W' or 'X' or 'Y' or 'Z'. | - | ||||||||||||||||||
319 | - | |||||||||||||||||||
320 | \endtable | - | ||||||||||||||||||
321 | - | |||||||||||||||||||
322 | Using the predefined character set abbreviations is more portable | - | ||||||||||||||||||
323 | than using character ranges across platforms and languages. For | - | ||||||||||||||||||
324 | example, \b{[0-9]} matches a digit in Western alphabets but | - | ||||||||||||||||||
325 | \b{\\d} matches a digit in \e any alphabet. | - | ||||||||||||||||||
326 | - | |||||||||||||||||||
327 | Note: In other regexp documentation, sets of characters are often | - | ||||||||||||||||||
328 | called "character classes". | - | ||||||||||||||||||
329 | - | |||||||||||||||||||
330 | \target quantifiers | - | ||||||||||||||||||
331 | \section1 Quantifiers | - | ||||||||||||||||||
332 | - | |||||||||||||||||||
333 | By default, an expression is automatically quantified by | - | ||||||||||||||||||
334 | \b{{1,1}}, i.e. it should occur exactly once. In the following | - | ||||||||||||||||||
335 | list, \b{\e {E}} stands for expression. An expression is a | - | ||||||||||||||||||
336 | character, or an abbreviation for a set of characters, or a set of | - | ||||||||||||||||||
337 | characters in square brackets, or an expression in parentheses. | - | ||||||||||||||||||
338 | - | |||||||||||||||||||
339 | \table | - | ||||||||||||||||||
340 | \row \li \b{\e {E}?} | - | ||||||||||||||||||
341 | - | |||||||||||||||||||
342 | \li Matches zero or one occurrences of \e E. This quantifier | - | ||||||||||||||||||
343 | means \e{The previous expression is optional}, because it | - | ||||||||||||||||||
344 | will match whether or not the expression is found. \b{\e | - | ||||||||||||||||||
345 | {E}?} is the same as \b{\e {E}{0,1}}. e.g., \b{dents?} | - | ||||||||||||||||||
346 | matches 'dent' or 'dents'. | - | ||||||||||||||||||
347 | - | |||||||||||||||||||
348 | \row \li \b{\e {E}+} | - | ||||||||||||||||||
349 | - | |||||||||||||||||||
350 | \li Matches one or more occurrences of \e E. \b{\e {E}+} is | - | ||||||||||||||||||
351 | the same as \b{\e {E}{1,}}. e.g., \b{0+} matches '0', | - | ||||||||||||||||||
352 | '00', '000', etc. | - | ||||||||||||||||||
353 | - | |||||||||||||||||||
354 | \row \li \b{\e {E}*} | - | ||||||||||||||||||
355 | - | |||||||||||||||||||
356 | \li Matches zero or more occurrences of \e E. It is the same | - | ||||||||||||||||||
357 | as \b{\e {E}{0,}}. The \b{*} quantifier is often used | - | ||||||||||||||||||
358 | in error where \b{+} should be used. For example, if | - | ||||||||||||||||||
359 | \b{\\s*$} is used in an expression to match strings that | - | ||||||||||||||||||
360 | end in whitespace, it will match every string because | - | ||||||||||||||||||
361 | \b{\\s*$} means \e{Match zero or more whitespaces followed | - | ||||||||||||||||||
362 | by end of string}. The correct regexp to match strings that | - | ||||||||||||||||||
363 | have at least one trailing whitespace character is | - | ||||||||||||||||||
364 | \b{\\s+$}. | - | ||||||||||||||||||
365 | - | |||||||||||||||||||
366 | \row \li \b{\e {E}{n}} | - | ||||||||||||||||||
367 | - | |||||||||||||||||||
368 | \li Matches exactly \e n occurrences of \e E. \b{\e {E}{n}} | - | ||||||||||||||||||
369 | is the same as repeating \e E \e n times. For example, | - | ||||||||||||||||||
370 | \b{x{5}} is the same as \b{xxxxx}. It is also the same | - | ||||||||||||||||||
371 | as \b{\e {E}{n,n}}, e.g. \b{x{5,5}}. | - | ||||||||||||||||||
372 | - | |||||||||||||||||||
373 | \row \li \b{\e {E}{n,}} | - | ||||||||||||||||||
374 | \li Matches at least \e n occurrences of \e E. | - | ||||||||||||||||||
375 | - | |||||||||||||||||||
376 | \row \li \b{\e {E}{,m}} | - | ||||||||||||||||||
377 | \li Matches at most \e m occurrences of \e E. \b{\e {E}{,m}} | - | ||||||||||||||||||
378 | is the same as \b{\e {E}{0,m}}. | - | ||||||||||||||||||
379 | - | |||||||||||||||||||
380 | \row \li \b{\e {E}{n,m}} | - | ||||||||||||||||||
381 | \li Matches at least \e n and at most \e m occurrences of \e E. | - | ||||||||||||||||||
382 | \endtable | - | ||||||||||||||||||
383 | - | |||||||||||||||||||
384 | To apply a quantifier to more than just the preceding character, | - | ||||||||||||||||||
385 | use parentheses to group characters together in an expression. For | - | ||||||||||||||||||
386 | example, \b{tag+} matches a 't' followed by an 'a' followed by | - | ||||||||||||||||||
387 | at least one 'g', whereas \b{(tag)+} matches at least one | - | ||||||||||||||||||
388 | occurrence of 'tag'. | - | ||||||||||||||||||
389 | - | |||||||||||||||||||
390 | Note: Quantifiers are normally "greedy". They always match as much | - | ||||||||||||||||||
391 | text as they can. For example, \b{0+} matches the first zero it | - | ||||||||||||||||||
392 | finds and all the consecutive zeros after the first zero. Applied | - | ||||||||||||||||||
393 | to '20005', it matches '2\underline{000}5'. Quantifiers can be made | - | ||||||||||||||||||
394 | non-greedy, see setMinimal(). | - | ||||||||||||||||||
395 | - | |||||||||||||||||||
396 | \target capturing parentheses | - | ||||||||||||||||||
397 | \target backreferences | - | ||||||||||||||||||
398 | \section1 Capturing Text | - | ||||||||||||||||||
399 | - | |||||||||||||||||||
400 | Parentheses allow us to group elements together so that we can | - | ||||||||||||||||||
401 | quantify and capture them. For example if we have the expression | - | ||||||||||||||||||
402 | \b{mail|letter|correspondence} that matches a string we know | - | ||||||||||||||||||
403 | that \e one of the words matched but not which one. Using | - | ||||||||||||||||||
404 | parentheses allows us to "capture" whatever is matched within | - | ||||||||||||||||||
405 | their bounds, so if we used \b{(mail|letter|correspondence)} | - | ||||||||||||||||||
406 | and matched this regexp against the string "I sent you some email" | - | ||||||||||||||||||
407 | we can use the cap() or capturedTexts() functions to extract the | - | ||||||||||||||||||
408 | matched characters, in this case 'mail'. | - | ||||||||||||||||||
409 | - | |||||||||||||||||||
410 | We can use captured text within the regexp itself. To refer to the | - | ||||||||||||||||||
411 | captured text we use \e backreferences which are indexed from 1, | - | ||||||||||||||||||
412 | the same as for cap(). For example we could search for duplicate | - | ||||||||||||||||||
413 | words in a string using \b{\\b(\\w+)\\W+\\1\\b} which means match a | - | ||||||||||||||||||
414 | word boundary followed by one or more word characters followed by | - | ||||||||||||||||||
415 | one or more non-word characters followed by the same text as the | - | ||||||||||||||||||
416 | first parenthesized expression followed by a word boundary. | - | ||||||||||||||||||
417 | - | |||||||||||||||||||
418 | If we want to use parentheses purely for grouping and not for | - | ||||||||||||||||||
419 | capturing we can use the non-capturing syntax, e.g. | - | ||||||||||||||||||
420 | \b{(?:green|blue)}. Non-capturing parentheses begin '(?:' and | - | ||||||||||||||||||
421 | end ')'. In this example we match either 'green' or 'blue' but we | - | ||||||||||||||||||
422 | do not capture the match so we only know whether or not we matched | - | ||||||||||||||||||
423 | but not which color we actually found. Using non-capturing | - | ||||||||||||||||||
424 | parentheses is more efficient than using capturing parentheses | - | ||||||||||||||||||
425 | since the regexp engine has to do less book-keeping. | - | ||||||||||||||||||
426 | - | |||||||||||||||||||
427 | Both capturing and non-capturing parentheses may be nested. | - | ||||||||||||||||||
428 | - | |||||||||||||||||||
429 | \target greedy quantifiers | - | ||||||||||||||||||
430 | - | |||||||||||||||||||
431 | For historical reasons, quantifiers (e.g. \b{*}) that apply to | - | ||||||||||||||||||
432 | capturing parentheses are more "greedy" than other quantifiers. | - | ||||||||||||||||||
433 | For example, \b{a*(a*)} will match "aaa" with cap(1) == "aaa". | - | ||||||||||||||||||
434 | This behavior is different from what other regexp engines do | - | ||||||||||||||||||
435 | (notably, Perl). To obtain a more intuitive capturing behavior, | - | ||||||||||||||||||
436 | specify QRegExp::RegExp2 to the QRegExp constructor or call | - | ||||||||||||||||||
437 | setPatternSyntax(QRegExp::RegExp2). | - | ||||||||||||||||||
438 | - | |||||||||||||||||||
439 | \target cap_in_a_loop | - | ||||||||||||||||||
440 | - | |||||||||||||||||||
441 | When the number of matches cannot be determined in advance, a | - | ||||||||||||||||||
442 | common idiom is to use cap() in a loop. For example: | - | ||||||||||||||||||
443 | - | |||||||||||||||||||
444 | \snippet code/src_corelib_tools_qregexp.cpp 0 | - | ||||||||||||||||||
445 | - | |||||||||||||||||||
446 | \target assertions | - | ||||||||||||||||||
447 | \section1 Assertions | - | ||||||||||||||||||
448 | - | |||||||||||||||||||
449 | Assertions make some statement about the text at the point where | - | ||||||||||||||||||
450 | they occur in the regexp but they do not match any characters. In | - | ||||||||||||||||||
451 | the following list \b{\e {E}} stands for any expression. | - | ||||||||||||||||||
452 | - | |||||||||||||||||||
453 | \table | - | ||||||||||||||||||
454 | \row \li \b{^} | - | ||||||||||||||||||
455 | \li The caret signifies the beginning of the string. If you | - | ||||||||||||||||||
456 | wish to match a literal \c{^} you must escape it by | - | ||||||||||||||||||
457 | writing \c{\\^}. For example, \b{^#include} will only | - | ||||||||||||||||||
458 | match strings which \e begin with the characters '#include'. | - | ||||||||||||||||||
459 | (When the caret is the first character of a character set it | - | ||||||||||||||||||
460 | has a special meaning, see \l{#sets-of-characters}{Sets of Characters}.) | - | ||||||||||||||||||
461 | - | |||||||||||||||||||
462 | \row \li \b{$} | - | ||||||||||||||||||
463 | \li The dollar signifies the end of the string. For example | - | ||||||||||||||||||
464 | \b{\\d\\s*$} will match strings which end with a digit | - | ||||||||||||||||||
465 | optionally followed by whitespace. If you wish to match a | - | ||||||||||||||||||
466 | literal \c{$} you must escape it by writing | - | ||||||||||||||||||
467 | \c{\\$}. | - | ||||||||||||||||||
468 | - | |||||||||||||||||||
469 | \row \li \b{\\b} | - | ||||||||||||||||||
470 | \li A word boundary. For example the regexp | - | ||||||||||||||||||
471 | \b{\\bOK\\b} means match immediately after a word | - | ||||||||||||||||||
472 | boundary (e.g. start of string or whitespace) the letter 'O' | - | ||||||||||||||||||
473 | then the letter 'K' immediately before another word boundary | - | ||||||||||||||||||
474 | (e.g. end of string or whitespace). But note that the | - | ||||||||||||||||||
475 | assertion does not actually match any whitespace so if we | - | ||||||||||||||||||
476 | write \b{(\\bOK\\b)} and we have a match it will only | - | ||||||||||||||||||
477 | contain 'OK' even if the string is "It's \underline{OK} now". | - | ||||||||||||||||||
478 | - | |||||||||||||||||||
479 | \row \li \b{\\B} | - | ||||||||||||||||||
480 | \li A non-word boundary. This assertion is true wherever | - | ||||||||||||||||||
481 | \b{\\b} is false. For example if we searched for | - | ||||||||||||||||||
482 | \b{\\Bon\\B} in "Left on" the match would fail (space | - | ||||||||||||||||||
483 | and end of string aren't non-word boundaries), but it would | - | ||||||||||||||||||
484 | match in "t\underline{on}ne". | - | ||||||||||||||||||
485 | - | |||||||||||||||||||
486 | \row \li \b{(?=\e E)} | - | ||||||||||||||||||
487 | \li Positive lookahead. This assertion is true if the | - | ||||||||||||||||||
488 | expression matches at this point in the regexp. For example, | - | ||||||||||||||||||
489 | \b{const(?=\\s+char)} matches 'const' whenever it is | - | ||||||||||||||||||
490 | followed by 'char', as in 'static \underline{const} char *'. | - | ||||||||||||||||||
491 | (Compare with \b{const\\s+char}, which matches 'static | - | ||||||||||||||||||
492 | \underline{const char} *'.) | - | ||||||||||||||||||
493 | - | |||||||||||||||||||
494 | \row \li \b{(?!\e E)} | - | ||||||||||||||||||
495 | \li Negative lookahead. This assertion is true if the | - | ||||||||||||||||||
496 | expression does not match at this point in the regexp. For | - | ||||||||||||||||||
497 | example, \b{const(?!\\s+char)} matches 'const' \e except | - | ||||||||||||||||||
498 | when it is followed by 'char'. | - | ||||||||||||||||||
499 | \endtable | - | ||||||||||||||||||
500 | - | |||||||||||||||||||
501 | \target QRegExp wildcard matching | - | ||||||||||||||||||
502 | \section1 Wildcard Matching | - | ||||||||||||||||||
503 | - | |||||||||||||||||||
504 | Most command shells such as \e bash or \e cmd.exe support "file | - | ||||||||||||||||||
505 | globbing", the ability to identify a group of files by using | - | ||||||||||||||||||
506 | wildcards. The setPatternSyntax() function is used to switch | - | ||||||||||||||||||
507 | between regexp and wildcard mode. Wildcard matching is much | - | ||||||||||||||||||
508 | simpler than full regexps and has only four features: | - | ||||||||||||||||||
509 | - | |||||||||||||||||||
510 | \table | - | ||||||||||||||||||
511 | \row \li \b{c} | - | ||||||||||||||||||
512 | \li Any character represents itself apart from those mentioned | - | ||||||||||||||||||
513 | below. Thus \b{c} matches the character \e c. | - | ||||||||||||||||||
514 | \row \li \b{?} | - | ||||||||||||||||||
515 | \li Matches any single character. It is the same as | - | ||||||||||||||||||
516 | \b{.} in full regexps. | - | ||||||||||||||||||
517 | \row \li \b{*} | - | ||||||||||||||||||
518 | \li Matches zero or more of any characters. It is the | - | ||||||||||||||||||
519 | same as \b{.*} in full regexps. | - | ||||||||||||||||||
520 | \row \li \b{[...]} | - | ||||||||||||||||||
521 | \li Sets of characters can be represented in square brackets, | - | ||||||||||||||||||
522 | similar to full regexps. Within the character class, like | - | ||||||||||||||||||
523 | outside, backslash has no special meaning. | - | ||||||||||||||||||
524 | \endtable | - | ||||||||||||||||||
525 | - | |||||||||||||||||||
526 | In the mode Wildcard, the wildcard characters cannot be | - | ||||||||||||||||||
527 | escaped. In the mode WildcardUnix, the character '\\' escapes the | - | ||||||||||||||||||
528 | wildcard. | - | ||||||||||||||||||
529 | - | |||||||||||||||||||
530 | For example if we are in wildcard mode and have strings which | - | ||||||||||||||||||
531 | contain filenames we could identify HTML files with \b{*.html}. | - | ||||||||||||||||||
532 | This will match zero or more characters followed by a dot followed | - | ||||||||||||||||||
533 | by 'h', 't', 'm' and 'l'. | - | ||||||||||||||||||
534 | - | |||||||||||||||||||
535 | To test a string against a wildcard expression, use exactMatch(). | - | ||||||||||||||||||
536 | For example: | - | ||||||||||||||||||
537 | - | |||||||||||||||||||
538 | \snippet code/src_corelib_tools_qregexp.cpp 1 | - | ||||||||||||||||||
539 | - | |||||||||||||||||||
540 | \target perl-users | - | ||||||||||||||||||
541 | \section1 Notes for Perl Users | - | ||||||||||||||||||
542 | - | |||||||||||||||||||
543 | Most of the character class abbreviations supported by Perl are | - | ||||||||||||||||||
544 | supported by QRegExp, see \l{#characters-and-abbreviations-for-sets-of-characters} | - | ||||||||||||||||||
545 | {characters and abbreviations for sets of characters}. | - | ||||||||||||||||||
546 | - | |||||||||||||||||||
547 | In QRegExp, apart from within character classes, \c{^} always | - | ||||||||||||||||||
548 | signifies the start of the string, so carets must always be | - | ||||||||||||||||||
549 | escaped unless used for that purpose. In Perl the meaning of caret | - | ||||||||||||||||||
550 | varies automagically depending on where it occurs so escaping it | - | ||||||||||||||||||
551 | is rarely necessary. The same applies to \c{$} which in | - | ||||||||||||||||||
552 | QRegExp always signifies the end of the string. | - | ||||||||||||||||||
553 | - | |||||||||||||||||||
554 | QRegExp's quantifiers are the same as Perl's greedy quantifiers | - | ||||||||||||||||||
555 | (but see the \l{greedy quantifiers}{note above}). Non-greedy | - | ||||||||||||||||||
556 | matching cannot be applied to individual quantifiers, but can be | - | ||||||||||||||||||
557 | applied to all the quantifiers in the pattern. For example, to | - | ||||||||||||||||||
558 | match the Perl regexp \b{ro+?m} requires: | - | ||||||||||||||||||
559 | - | |||||||||||||||||||
560 | \snippet code/src_corelib_tools_qregexp.cpp 2 | - | ||||||||||||||||||
561 | - | |||||||||||||||||||
562 | The equivalent of Perl's \c{/i} option is | - | ||||||||||||||||||
563 | setCaseSensitivity(Qt::CaseInsensitive). | - | ||||||||||||||||||
564 | - | |||||||||||||||||||
565 | Perl's \c{/g} option can be emulated using a \l{#cap_in_a_loop}{loop}. | - | ||||||||||||||||||
566 | - | |||||||||||||||||||
567 | In QRegExp \b{.} matches any character, therefore all QRegExp | - | ||||||||||||||||||
568 | regexps have the equivalent of Perl's \c{/s} option. QRegExp | - | ||||||||||||||||||
569 | does not have an equivalent to Perl's \c{/m} option, but this | - | ||||||||||||||||||
570 | can be emulated in various ways for example by splitting the input | - | ||||||||||||||||||
571 | into lines or by looping with a regexp that searches for newlines. | - | ||||||||||||||||||
572 | - | |||||||||||||||||||
573 | Because QRegExp is string oriented, there are no \\A, \\Z, or \\z | - | ||||||||||||||||||
574 | assertions. The \\G assertion is not supported but can be emulated | - | ||||||||||||||||||
575 | in a loop. | - | ||||||||||||||||||
576 | - | |||||||||||||||||||
577 | Perl's $& is cap(0) or capturedTexts()[0]. There are no QRegExp | - | ||||||||||||||||||
578 | equivalents for $`, $' or $+. Perl's capturing variables, $1, $2, | - | ||||||||||||||||||
579 | ... correspond to cap(1) or capturedTexts()[1], cap(2) or | - | ||||||||||||||||||
580 | capturedTexts()[2], etc. | - | ||||||||||||||||||
581 | - | |||||||||||||||||||
582 | To substitute a pattern use QString::replace(). | - | ||||||||||||||||||
583 | - | |||||||||||||||||||
584 | Perl's extended \c{/x} syntax is not supported, nor are | - | ||||||||||||||||||
585 | directives, e.g. (?i), or regexp comments, e.g. (?#comment). On | - | ||||||||||||||||||
586 | the other hand, C++'s rules for literal strings can be used to | - | ||||||||||||||||||
587 | achieve the same: | - | ||||||||||||||||||
588 | - | |||||||||||||||||||
589 | \snippet code/src_corelib_tools_qregexp.cpp 3 | - | ||||||||||||||||||
590 | - | |||||||||||||||||||
591 | Both zero-width positive and zero-width negative lookahead | - | ||||||||||||||||||
592 | assertions (?=pattern) and (?!pattern) are supported with the same | - | ||||||||||||||||||
593 | syntax as Perl. Perl's lookbehind assertions, "independent" | - | ||||||||||||||||||
594 | subexpressions and conditional expressions are not supported. | - | ||||||||||||||||||
595 | - | |||||||||||||||||||
596 | Non-capturing parentheses are also supported, with the same | - | ||||||||||||||||||
597 | (?:pattern) syntax. | - | ||||||||||||||||||
598 | - | |||||||||||||||||||
599 | See QString::split() and QStringList::join() for equivalents | - | ||||||||||||||||||
600 | to Perl's split and join functions. | - | ||||||||||||||||||
601 | - | |||||||||||||||||||
602 | Note: because C++ transforms \\'s they must be written \e twice in | - | ||||||||||||||||||
603 | code, e.g. \b{\\b} must be written \b{\\\\b}. | - | ||||||||||||||||||
604 | - | |||||||||||||||||||
605 | \target code-examples | - | ||||||||||||||||||
606 | \section1 Code Examples | - | ||||||||||||||||||
607 | - | |||||||||||||||||||
608 | \snippet code/src_corelib_tools_qregexp.cpp 4 | - | ||||||||||||||||||
609 | - | |||||||||||||||||||
610 | The third string matches '\underline{6}'. This is a simple validation | - | ||||||||||||||||||
611 | regexp for integers in the range 0 to 99. | - | ||||||||||||||||||
612 | - | |||||||||||||||||||
613 | \snippet code/src_corelib_tools_qregexp.cpp 5 | - | ||||||||||||||||||
614 | - | |||||||||||||||||||
615 | The second string matches '\underline{This_is-OK}'. We've used the | - | ||||||||||||||||||
616 | character set abbreviation '\\S' (non-whitespace) and the anchors | - | ||||||||||||||||||
617 | to match strings which contain no whitespace. | - | ||||||||||||||||||
618 | - | |||||||||||||||||||
619 | In the following example we match strings containing 'mail' or | - | ||||||||||||||||||
620 | 'letter' or 'correspondence' but only match whole words i.e. not | - | ||||||||||||||||||
621 | 'email' | - | ||||||||||||||||||
622 | - | |||||||||||||||||||
623 | \snippet code/src_corelib_tools_qregexp.cpp 6 | - | ||||||||||||||||||
624 | - | |||||||||||||||||||
625 | The second string matches "Please write the \underline{letter}". The | - | ||||||||||||||||||
626 | word 'letter' is also captured (because of the parentheses). We | - | ||||||||||||||||||
627 | can see what text we've captured like this: | - | ||||||||||||||||||
628 | - | |||||||||||||||||||
629 | \snippet code/src_corelib_tools_qregexp.cpp 7 | - | ||||||||||||||||||
630 | - | |||||||||||||||||||
631 | This will capture the text from the first set of capturing | - | ||||||||||||||||||
632 | parentheses (counting capturing left parentheses from left to | - | ||||||||||||||||||
633 | right). The parentheses are counted from 1 since cap(0) is the | - | ||||||||||||||||||
634 | whole matched regexp (equivalent to '&' in most regexp engines). | - | ||||||||||||||||||
635 | - | |||||||||||||||||||
636 | \snippet code/src_corelib_tools_qregexp.cpp 8 | - | ||||||||||||||||||
637 | - | |||||||||||||||||||
638 | Here we've passed the QRegExp to QString's replace() function to | - | ||||||||||||||||||
639 | replace the matched text with new text. | - | ||||||||||||||||||
640 | - | |||||||||||||||||||
641 | \snippet code/src_corelib_tools_qregexp.cpp 9 | - | ||||||||||||||||||
642 | - | |||||||||||||||||||
643 | We've used the indexIn() function to repeatedly match the regexp in | - | ||||||||||||||||||
644 | the string. Note that instead of moving forward by one character | - | ||||||||||||||||||
645 | at a time \c pos++ we could have written \c {pos += | - | ||||||||||||||||||
646 | rx.matchedLength()} to skip over the already matched string. The | - | ||||||||||||||||||
647 | count will equal 3, matching 'One \underline{Eric} another | - | ||||||||||||||||||
648 | \underline{Eirik}, and an Ericsson. How many Eiriks, \underline{Eric}?'; it | - | ||||||||||||||||||
649 | doesn't match 'Ericsson' or 'Eiriks' because they are not bounded | - | ||||||||||||||||||
650 | by non-word boundaries. | - | ||||||||||||||||||
651 | - | |||||||||||||||||||
652 | One common use of regexps is to split lines of delimited data into | - | ||||||||||||||||||
653 | their component fields. | - | ||||||||||||||||||
654 | - | |||||||||||||||||||
655 | \snippet code/src_corelib_tools_qregexp.cpp 10 | - | ||||||||||||||||||
656 | - | |||||||||||||||||||
657 | In this example our input lines have the format company name, web | - | ||||||||||||||||||
658 | address and country. Unfortunately the regexp is rather long and | - | ||||||||||||||||||
659 | not very versatile -- the code will break if we add any more | - | ||||||||||||||||||
660 | fields. A simpler and better solution is to look for the | - | ||||||||||||||||||
661 | separator, '\\t' in this case, and take the surrounding text. The | - | ||||||||||||||||||
662 | QString::split() function can take a separator string or regexp | - | ||||||||||||||||||
663 | as an argument and split a string accordingly. | - | ||||||||||||||||||
664 | - | |||||||||||||||||||
665 | \snippet code/src_corelib_tools_qregexp.cpp 11 | - | ||||||||||||||||||
666 | - | |||||||||||||||||||
667 | Here field[0] is the company, field[1] the web address and so on. | - | ||||||||||||||||||
668 | - | |||||||||||||||||||
669 | To imitate the matching of a shell we can use wildcard mode. | - | ||||||||||||||||||
670 | - | |||||||||||||||||||
671 | \snippet code/src_corelib_tools_qregexp.cpp 12 | - | ||||||||||||||||||
672 | - | |||||||||||||||||||
673 | Wildcard matching can be convenient because of its simplicity, but | - | ||||||||||||||||||
674 | any wildcard regexp can be defined using full regexps, e.g. | - | ||||||||||||||||||
675 | \b{.*\\.html$}. Notice that we can't match both \c .html and \c | - | ||||||||||||||||||
676 | .htm files with a wildcard unless we use \b{*.htm*} which will | - | ||||||||||||||||||
677 | also match 'test.html.bak'. A full regexp gives us the precision | - | ||||||||||||||||||
678 | we need, \b{.*\\.html?$}. | - | ||||||||||||||||||
679 | - | |||||||||||||||||||
680 | QRegExp can match case insensitively using setCaseSensitivity(), | - | ||||||||||||||||||
681 | and can use non-greedy matching, see setMinimal(). By | - | ||||||||||||||||||
682 | default QRegExp uses full regexps but this can be changed with | - | ||||||||||||||||||
683 | setPatternSyntax(). Searching can be done forward with indexIn() or backward | - | ||||||||||||||||||
684 | with lastIndexIn(). Captured text can be accessed using | - | ||||||||||||||||||
685 | capturedTexts() which returns a string list of all captured | - | ||||||||||||||||||
686 | strings, or using cap() which returns the captured string for the | - | ||||||||||||||||||
687 | given index. The pos() function takes a match index and returns | - | ||||||||||||||||||
688 | the position in the string where the match was made (or -1 if | - | ||||||||||||||||||
689 | there was no match). | - | ||||||||||||||||||
690 | - | |||||||||||||||||||
691 | \sa QString, QStringList, QRegExpValidator, QSortFilterProxyModel, | - | ||||||||||||||||||
692 | {tools/regexp}{Regular Expression Example} | - | ||||||||||||||||||
693 | */ | - | ||||||||||||||||||
694 | - | |||||||||||||||||||
695 | #if defined(Q_OS_VXWORKS) && defined(EOS) | - | ||||||||||||||||||
696 | # undef EOS | - | ||||||||||||||||||
697 | #endif | - | ||||||||||||||||||
698 | - | |||||||||||||||||||
699 | const int NumBadChars = 64; | - | ||||||||||||||||||
700 | #define BadChar(ch) ((ch).unicode() % NumBadChars) | - | ||||||||||||||||||
701 | - | |||||||||||||||||||
702 | const int NoOccurrence = INT_MAX; | - | ||||||||||||||||||
703 | const int EmptyCapture = INT_MAX; | - | ||||||||||||||||||
704 | const int InftyLen = INT_MAX; | - | ||||||||||||||||||
705 | const int InftyRep = 1025; | - | ||||||||||||||||||
706 | const int EOS = -1; | - | ||||||||||||||||||
707 | - | |||||||||||||||||||
708 | static bool isWord(QChar ch) | - | ||||||||||||||||||
709 | { | - | ||||||||||||||||||
710 | return ch.isLetterOrNumber() || ch.isMark() || ch == QLatin1Char('_'); executed 287 times by 2 tests: return ch.isLetterOrNumber() || ch.isMark() || ch == QLatin1Char('_'); Executed by:
| 287 | ||||||||||||||||||
711 | } | - | ||||||||||||||||||
712 | - | |||||||||||||||||||
713 | /* | - | ||||||||||||||||||
714 | Merges two vectors of ints and puts the result into the first | - | ||||||||||||||||||
715 | one. | - | ||||||||||||||||||
716 | */ | - | ||||||||||||||||||
717 | static void mergeInto(QVector<int> *a, const QVector<int> &b) | - | ||||||||||||||||||
718 | { | - | ||||||||||||||||||
719 | int asize = a->size(); | - | ||||||||||||||||||
720 | int bsize = b.size(); | - | ||||||||||||||||||
721 | if (asize == 0) {
| 11023-32753 | ||||||||||||||||||
722 | *a = b; | - | ||||||||||||||||||
723 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
724 | } else if (bsize == 1 && a->at(asize - 1) < b.at(0)) { executed 32753 times by 116 tests: end of block Executed by:
| 1874-32753 | ||||||||||||||||||
725 | a->resize(asize + 1); | - | ||||||||||||||||||
726 | (*a)[asize] = b.at(0); | - | ||||||||||||||||||
727 | #endif | - | ||||||||||||||||||
728 | } else if (bsize >= 1) { executed 6250 times by 97 tests: end of block Executed by:
| 975-6250 | ||||||||||||||||||
729 | int csize = asize + bsize; | - | ||||||||||||||||||
730 | QVector<int> c(csize); | - | ||||||||||||||||||
731 | int i = 0, j = 0, k = 0; | - | ||||||||||||||||||
732 | while (i < asize) {
| 1500-8457 | ||||||||||||||||||
733 | if (j < bsize) {
| 2298-6159 | ||||||||||||||||||
734 | if (a->at(i) == b.at(j)) {
| 172-5987 | ||||||||||||||||||
735 | ++i; | - | ||||||||||||||||||
736 | --csize; | - | ||||||||||||||||||
737 | } else if (a->at(i) < b.at(j)) { executed 172 times by 1 test: end of block Executed by:
| 172-3005 | ||||||||||||||||||
738 | c[k++] = a->at(i++); | - | ||||||||||||||||||
739 | } else { executed 3005 times by 11 tests: end of block Executed by:
| 3005 | ||||||||||||||||||
740 | c[k++] = b.at(j++); | - | ||||||||||||||||||
741 | } executed 2982 times by 69 tests: end of block Executed by:
| 2982 | ||||||||||||||||||
742 | } else { | - | ||||||||||||||||||
743 | memcpy(c.data() + k, a->constData() + i, (asize - i) * sizeof(int)); | - | ||||||||||||||||||
744 | break; executed 2298 times by 69 tests: break; Executed by:
| 2298 | ||||||||||||||||||
745 | } | - | ||||||||||||||||||
746 | } | - | ||||||||||||||||||
747 | c.resize(csize); | - | ||||||||||||||||||
748 | if (j < bsize)
| 1500-2298 | ||||||||||||||||||
749 | memcpy(c.data() + k, b.constData() + j, (bsize - j) * sizeof(int)); executed 1500 times by 11 tests: memcpy(c.data() + k, b.constData() + j, (bsize - j) * sizeof(int)); Executed by:
| 1500 | ||||||||||||||||||
750 | *a = c; | - | ||||||||||||||||||
751 | } executed 3798 times by 69 tests: end of block Executed by:
| 3798 | ||||||||||||||||||
752 | } executed 43776 times by 116 tests: end of block Executed by:
| 43776 | ||||||||||||||||||
753 | - | |||||||||||||||||||
754 | #ifndef QT_NO_REGEXP_WILDCARD | - | ||||||||||||||||||
755 | /* | - | ||||||||||||||||||
756 | Translates a wildcard pattern to an equivalent regular expression | - | ||||||||||||||||||
757 | pattern (e.g., *.cpp to .*\.cpp). | - | ||||||||||||||||||
758 | - | |||||||||||||||||||
759 | If enableEscaping is true, it is possible to escape the wildcard | - | ||||||||||||||||||
760 | characters with \ | - | ||||||||||||||||||
761 | */ | - | ||||||||||||||||||
762 | static QString wc2rx(const QString &wc_str, const bool enableEscaping) | - | ||||||||||||||||||
763 | { | - | ||||||||||||||||||
764 | const int wclen = wc_str.length(); | - | ||||||||||||||||||
765 | QString rx; | - | ||||||||||||||||||
766 | int i = 0; | - | ||||||||||||||||||
767 | bool isEscaping = false; // the previous character is '\' | - | ||||||||||||||||||
768 | const QChar *wc = wc_str.unicode(); | - | ||||||||||||||||||
769 | - | |||||||||||||||||||
770 | while (i < wclen) {
| 290-1641 | ||||||||||||||||||
771 | const QChar c = wc[i++]; | - | ||||||||||||||||||
772 | switch (c.unicode()) { | - | ||||||||||||||||||
773 | case '\\': executed 12 times by 1 test: case '\\': Executed by:
| 12 | ||||||||||||||||||
774 | if (enableEscaping) {
| 0-12 | ||||||||||||||||||
775 | if (isEscaping) {
| 2-10 | ||||||||||||||||||
776 | rx += QLatin1String("\\\\"); | - | ||||||||||||||||||
777 | } // we insert the \\ later if necessary executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
778 | if (i == wclen) { // the end
| 1-11 | ||||||||||||||||||
779 | rx += QLatin1String("\\\\"); | - | ||||||||||||||||||
780 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
781 | } else { executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||
782 | rx += QLatin1String("\\\\"); | - | ||||||||||||||||||
783 | } never executed: end of block | 0 | ||||||||||||||||||
784 | isEscaping = true; | - | ||||||||||||||||||
785 | break; executed 12 times by 1 test: break; Executed by:
| 12 | ||||||||||||||||||
786 | case '*': executed 194 times by 71 tests: case '*': Executed by:
| 194 | ||||||||||||||||||
787 | if (isEscaping) {
| 0-194 | ||||||||||||||||||
788 | rx += QLatin1String("\\*"); | - | ||||||||||||||||||
789 | isEscaping = false; | - | ||||||||||||||||||
790 | } else { never executed: end of block | 0 | ||||||||||||||||||
791 | rx += QLatin1String(".*"); | - | ||||||||||||||||||
792 | } executed 194 times by 71 tests: end of block Executed by:
| 194 | ||||||||||||||||||
793 | break; executed 194 times by 71 tests: break; Executed by:
| 194 | ||||||||||||||||||
794 | case '?': executed 8 times by 1 test: case '?': Executed by:
| 8 | ||||||||||||||||||
795 | if (isEscaping) {
| 1-7 | ||||||||||||||||||
796 | rx += QLatin1String("\\?"); | - | ||||||||||||||||||
797 | isEscaping = false; | - | ||||||||||||||||||
798 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
799 | rx += QLatin1Char('.'); | - | ||||||||||||||||||
800 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||||||||
801 | - | |||||||||||||||||||
802 | break; executed 8 times by 1 test: break; Executed by:
| 8 | ||||||||||||||||||
803 | case '$': never executed: case '$': | 0 | ||||||||||||||||||
804 | case '(': executed 1 time by 1 test: case '(': Executed by:
| 1 | ||||||||||||||||||
805 | case ')': executed 1 time by 1 test: case ')': Executed by:
| 1 | ||||||||||||||||||
806 | case '+': never executed: case '+': | 0 | ||||||||||||||||||
807 | case '.': executed 120 times by 47 tests: case '.': Executed by:
| 120 | ||||||||||||||||||
808 | case '^': never executed: case '^': | 0 | ||||||||||||||||||
809 | case '{': never executed: case '{': | 0 | ||||||||||||||||||
810 | case '|': never executed: case '|': | 0 | ||||||||||||||||||
811 | case '}': never executed: case '}': | 0 | ||||||||||||||||||
812 | if (isEscaping) {
| 0-122 | ||||||||||||||||||
813 | isEscaping = false; | - | ||||||||||||||||||
814 | rx += QLatin1String("\\\\"); | - | ||||||||||||||||||
815 | } never executed: end of block | 0 | ||||||||||||||||||
816 | rx += QLatin1Char('\\'); | - | ||||||||||||||||||
817 | rx += c; | - | ||||||||||||||||||
818 | break; executed 122 times by 47 tests: break; Executed by:
| 122 | ||||||||||||||||||
819 | case '[': executed 249 times by 38 tests: case '[': Executed by:
| 249 | ||||||||||||||||||
820 | if (isEscaping) {
| 3-246 | ||||||||||||||||||
821 | isEscaping = false; | - | ||||||||||||||||||
822 | rx += QLatin1String("\\["); | - | ||||||||||||||||||
823 | } else { executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||
824 | rx += c; | - | ||||||||||||||||||
825 | if (wc[i] == QLatin1Char('^'))
| 2-244 | ||||||||||||||||||
826 | rx += wc[i++]; executed 2 times by 1 test: rx += wc[i++]; Executed by:
| 2 | ||||||||||||||||||
827 | if (i < wclen) {
| 4-242 | ||||||||||||||||||
828 | if (rx[i] == QLatin1Char(']'))
| 0-242 | ||||||||||||||||||
829 | rx += wc[i++]; never executed: rx += wc[i++]; | 0 | ||||||||||||||||||
830 | while (i < wclen && wc[i] != QLatin1Char(']')) {
| 4-1335 | ||||||||||||||||||
831 | if (wc[i] == QLatin1Char('\\'))
| 1-1096 | ||||||||||||||||||
832 | rx += QLatin1Char('\\'); executed 1 time by 1 test: rx += QLatin1Char('\\'); Executed by:
| 1 | ||||||||||||||||||
833 | rx += wc[i++]; | - | ||||||||||||||||||
834 | } executed 1097 times by 38 tests: end of block Executed by:
| 1097 | ||||||||||||||||||
835 | } executed 242 times by 38 tests: end of block Executed by:
| 242 | ||||||||||||||||||
836 | } executed 246 times by 38 tests: end of block Executed by:
| 246 | ||||||||||||||||||
837 | break; executed 249 times by 38 tests: break; Executed by:
| 249 | ||||||||||||||||||
838 | - | |||||||||||||||||||
839 | case ']': executed 246 times by 38 tests: case ']': Executed by:
| 246 | ||||||||||||||||||
840 | if(isEscaping){
| 2-244 | ||||||||||||||||||
841 | isEscaping = false; | - | ||||||||||||||||||
842 | rx += QLatin1String("\\"); | - | ||||||||||||||||||
843 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
844 | rx += c; | - | ||||||||||||||||||
845 | break; executed 246 times by 38 tests: break; Executed by:
| 246 | ||||||||||||||||||
846 | - | |||||||||||||||||||
847 | default: executed 810 times by 38 tests: default: Executed by:
| 810 | ||||||||||||||||||
848 | if(isEscaping){
| 3-807 | ||||||||||||||||||
849 | isEscaping = false; | - | ||||||||||||||||||
850 | rx += QLatin1String("\\\\"); | - | ||||||||||||||||||
851 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||
852 | rx += c; | - | ||||||||||||||||||
853 | } executed 810 times by 38 tests: end of block Executed by:
| 810 | ||||||||||||||||||
854 | } | - | ||||||||||||||||||
855 | return rx; executed 290 times by 78 tests: return rx; Executed by:
| 290 | ||||||||||||||||||
856 | } | - | ||||||||||||||||||
857 | #endif | - | ||||||||||||||||||
858 | - | |||||||||||||||||||
859 | static int caretIndex(int offset, QRegExp::CaretMode caretMode) | - | ||||||||||||||||||
860 | { | - | ||||||||||||||||||
861 | if (caretMode == QRegExp::CaretAtZero) {
| 25-423766 | ||||||||||||||||||
862 | return 0; executed 423766 times by 49 tests: return 0; Executed by:
| 423766 | ||||||||||||||||||
863 | } else if (caretMode == QRegExp::CaretAtOffset) {
| 0-25 | ||||||||||||||||||
864 | return offset; never executed: return offset; | 0 | ||||||||||||||||||
865 | } else { // QRegExp::CaretWontMatch | - | ||||||||||||||||||
866 | return -1; executed 25 times by 3 tests: return -1; Executed by:
| 25 | ||||||||||||||||||
867 | } | - | ||||||||||||||||||
868 | } | - | ||||||||||||||||||
869 | - | |||||||||||||||||||
870 | /* | - | ||||||||||||||||||
871 | The QRegExpEngineKey struct uniquely identifies an engine. | - | ||||||||||||||||||
872 | */ | - | ||||||||||||||||||
873 | struct QRegExpEngineKey | - | ||||||||||||||||||
874 | { | - | ||||||||||||||||||
875 | QString pattern; | - | ||||||||||||||||||
876 | QRegExp::PatternSyntax patternSyntax; | - | ||||||||||||||||||
877 | Qt::CaseSensitivity cs; | - | ||||||||||||||||||
878 | - | |||||||||||||||||||
879 | inline QRegExpEngineKey(const QString &pattern, QRegExp::PatternSyntax patternSyntax, | - | ||||||||||||||||||
880 | Qt::CaseSensitivity cs) | - | ||||||||||||||||||
881 | : pattern(pattern), patternSyntax(patternSyntax), cs(cs) {} executed 624287 times by 162 tests: end of block Executed by:
| 624287 | ||||||||||||||||||
882 | - | |||||||||||||||||||
883 | inline void clear() { | - | ||||||||||||||||||
884 | pattern.clear(); | - | ||||||||||||||||||
885 | patternSyntax = QRegExp::RegExp; | - | ||||||||||||||||||
886 | cs = Qt::CaseSensitive; | - | ||||||||||||||||||
887 | } never executed: end of block | 0 | ||||||||||||||||||
888 | }; | - | ||||||||||||||||||
889 | - | |||||||||||||||||||
890 | static bool operator==(const QRegExpEngineKey &key1, const QRegExpEngineKey &key2) | - | ||||||||||||||||||
891 | { | - | ||||||||||||||||||
892 | return key1.pattern == key2.pattern && key1.patternSyntax == key2.patternSyntax executed 315960 times by 135 tests: return key1.pattern == key2.pattern && key1.patternSyntax == key2.patternSyntax && key1.cs == key2.cs; Executed by:
| 315960 | ||||||||||||||||||
893 | && key1.cs == key2.cs; executed 315960 times by 135 tests: return key1.pattern == key2.pattern && key1.patternSyntax == key2.patternSyntax && key1.cs == key2.cs; Executed by:
| 315960 | ||||||||||||||||||
894 | } | - | ||||||||||||||||||
895 | - | |||||||||||||||||||
896 | static uint qHash(const QRegExpEngineKey &key, uint seed = 0) Q_DECL_NOTHROW | - | ||||||||||||||||||
897 | { | - | ||||||||||||||||||
898 | QtPrivate::QHashCombine hash; | - | ||||||||||||||||||
899 | seed = hash(seed, key.pattern); | - | ||||||||||||||||||
900 | seed = hash(seed, key.patternSyntax); | - | ||||||||||||||||||
901 | seed = hash(seed, key.cs); | - | ||||||||||||||||||
902 | return seed; executed 630297 times by 167 tests: return seed; Executed by:
| 630297 | ||||||||||||||||||
903 | } | - | ||||||||||||||||||
904 | - | |||||||||||||||||||
905 | class QRegExpEngine; | - | ||||||||||||||||||
906 | - | |||||||||||||||||||
907 | //Q_DECLARE_TYPEINFO(QVector<int>, Q_MOVABLE_TYPE); | - | ||||||||||||||||||
908 | - | |||||||||||||||||||
909 | /* | - | ||||||||||||||||||
910 | This is the engine state during matching. | - | ||||||||||||||||||
911 | */ | - | ||||||||||||||||||
912 | struct QRegExpMatchState | - | ||||||||||||||||||
913 | { | - | ||||||||||||||||||
914 | const QChar *in; // a pointer to the input string data | - | ||||||||||||||||||
915 | int pos; // the current position in the string | - | ||||||||||||||||||
916 | int caretPos; | - | ||||||||||||||||||
917 | int len; // the length of the input string | - | ||||||||||||||||||
918 | bool minimal; // minimal matching? | - | ||||||||||||||||||
919 | int *bigArray; // big array holding the data for the next pointers | - | ||||||||||||||||||
920 | int *inNextStack; // is state is nextStack? | - | ||||||||||||||||||
921 | int *curStack; // stack of current states | - | ||||||||||||||||||
922 | int *nextStack; // stack of next states | - | ||||||||||||||||||
923 | int *curCapBegin; // start of current states' captures | - | ||||||||||||||||||
924 | int *nextCapBegin; // start of next states' captures | - | ||||||||||||||||||
925 | int *curCapEnd; // end of current states' captures | - | ||||||||||||||||||
926 | int *nextCapEnd; // end of next states' captures | - | ||||||||||||||||||
927 | int *tempCapBegin; // start of temporary captures | - | ||||||||||||||||||
928 | int *tempCapEnd; // end of temporary captures | - | ||||||||||||||||||
929 | int *capBegin; // start of captures for a next state | - | ||||||||||||||||||
930 | int *capEnd; // end of captures for a next state | - | ||||||||||||||||||
931 | int *slideTab; // bump-along slide table for bad-character heuristic | - | ||||||||||||||||||
932 | int *captured; // what match() returned last | - | ||||||||||||||||||
933 | int slideTabSize; // size of slide table | - | ||||||||||||||||||
934 | int capturedSize; | - | ||||||||||||||||||
935 | #ifndef QT_NO_REGEXP_BACKREF | - | ||||||||||||||||||
936 | QList<QVector<int> > sleeping; // list of back-reference sleepers | - | ||||||||||||||||||
937 | #endif | - | ||||||||||||||||||
938 | int matchLen; // length of match | - | ||||||||||||||||||
939 | int oneTestMatchedLen; // length of partial match | - | ||||||||||||||||||
940 | - | |||||||||||||||||||
941 | const QRegExpEngine *eng; | - | ||||||||||||||||||
942 | - | |||||||||||||||||||
943 | inline QRegExpMatchState() : bigArray(0), captured(0) {} executed 1424895 times by 162 tests: end of block Executed by:
| 1424895 | ||||||||||||||||||
944 | inline ~QRegExpMatchState() { free(bigArray); } executed 1424892 times by 167 tests: end of block Executed by:
| 1424892 | ||||||||||||||||||
945 | - | |||||||||||||||||||
946 | void drain() { free(bigArray); bigArray = 0; captured = 0; } // to save memory executed 624921 times by 167 tests: end of block Executed by:
| 624921 | ||||||||||||||||||
947 | void prepareForMatch(QRegExpEngine *eng); | - | ||||||||||||||||||
948 | void match(const QChar *str, int len, int pos, bool minimal, | - | ||||||||||||||||||
949 | bool oneTest, int caretIndex); | - | ||||||||||||||||||
950 | bool matchHere(); | - | ||||||||||||||||||
951 | bool testAnchor(int i, int a, const int *capBegin); | - | ||||||||||||||||||
952 | }; | - | ||||||||||||||||||
953 | - | |||||||||||||||||||
954 | /* | - | ||||||||||||||||||
955 | The struct QRegExpAutomatonState represents one state in a modified NFA. The | - | ||||||||||||||||||
956 | input characters matched are stored in the state instead of on | - | ||||||||||||||||||
957 | the transitions, something possible for an automaton | - | ||||||||||||||||||
958 | constructed from a regular expression. | - | ||||||||||||||||||
959 | */ | - | ||||||||||||||||||
960 | struct QRegExpAutomatonState | - | ||||||||||||||||||
961 | { | - | ||||||||||||||||||
962 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
963 | int atom; // which atom does this state belong to? | - | ||||||||||||||||||
964 | #endif | - | ||||||||||||||||||
965 | int match; // what does it match? (see CharClassBit and BackRefBit) | - | ||||||||||||||||||
966 | QVector<int> outs; // out-transitions | - | ||||||||||||||||||
967 | QMap<int, int> reenter; // atoms reentered when transiting out | - | ||||||||||||||||||
968 | QMap<int, int> anchors; // anchors met when transiting out | - | ||||||||||||||||||
969 | - | |||||||||||||||||||
970 | inline QRegExpAutomatonState() { } | - | ||||||||||||||||||
971 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
972 | inline QRegExpAutomatonState(int a, int m) | - | ||||||||||||||||||
973 | : atom(a), match(m) { } executed 29320 times by 116 tests: end of block Executed by:
| 29320 | ||||||||||||||||||
974 | #else | - | ||||||||||||||||||
975 | inline QRegExpAutomatonState(int m) | - | ||||||||||||||||||
976 | : match(m) { } | - | ||||||||||||||||||
977 | #endif | - | ||||||||||||||||||
978 | }; | - | ||||||||||||||||||
979 | - | |||||||||||||||||||
980 | Q_DECLARE_TYPEINFO(QRegExpAutomatonState, Q_MOVABLE_TYPE); | - | ||||||||||||||||||
981 | - | |||||||||||||||||||
982 | /* | - | ||||||||||||||||||
983 | The struct QRegExpCharClassRange represents a range of characters (e.g., | - | ||||||||||||||||||
984 | [0-9] denotes range 48 to 57). | - | ||||||||||||||||||
985 | */ | - | ||||||||||||||||||
986 | struct QRegExpCharClassRange | - | ||||||||||||||||||
987 | { | - | ||||||||||||||||||
988 | ushort from; // 48 | - | ||||||||||||||||||
989 | ushort len; // 10 | - | ||||||||||||||||||
990 | }; | - | ||||||||||||||||||
991 | - | |||||||||||||||||||
992 | Q_DECLARE_TYPEINFO(QRegExpCharClassRange, Q_PRIMITIVE_TYPE); | - | ||||||||||||||||||
993 | - | |||||||||||||||||||
994 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
995 | /* | - | ||||||||||||||||||
996 | The struct QRegExpAtom represents one node in the hierarchy of regular | - | ||||||||||||||||||
997 | expression atoms. | - | ||||||||||||||||||
998 | */ | - | ||||||||||||||||||
999 | struct QRegExpAtom | - | ||||||||||||||||||
1000 | { | - | ||||||||||||||||||
1001 | enum { NoCapture = -1, OfficialCapture = -2, UnofficialCapture = -3 }; | - | ||||||||||||||||||
1002 | - | |||||||||||||||||||
1003 | int parent; // index of parent in array of atoms | - | ||||||||||||||||||
1004 | int capture; // index of capture, from 1 to ncap - 1 | - | ||||||||||||||||||
1005 | }; | - | ||||||||||||||||||
1006 | - | |||||||||||||||||||
1007 | Q_DECLARE_TYPEINFO(QRegExpAtom, Q_PRIMITIVE_TYPE); | - | ||||||||||||||||||
1008 | #endif | - | ||||||||||||||||||
1009 | - | |||||||||||||||||||
1010 | struct QRegExpLookahead; | - | ||||||||||||||||||
1011 | - | |||||||||||||||||||
1012 | #ifndef QT_NO_REGEXP_ANCHOR_ALT | - | ||||||||||||||||||
1013 | /* | - | ||||||||||||||||||
1014 | The struct QRegExpAnchorAlternation represents a pair of anchors with | - | ||||||||||||||||||
1015 | OR semantics. | - | ||||||||||||||||||
1016 | */ | - | ||||||||||||||||||
1017 | struct QRegExpAnchorAlternation | - | ||||||||||||||||||
1018 | { | - | ||||||||||||||||||
1019 | int a; // this anchor... | - | ||||||||||||||||||
1020 | int b; // ...or this one | - | ||||||||||||||||||
1021 | }; | - | ||||||||||||||||||
1022 | - | |||||||||||||||||||
1023 | Q_DECLARE_TYPEINFO(QRegExpAnchorAlternation, Q_PRIMITIVE_TYPE); | - | ||||||||||||||||||
1024 | #endif | - | ||||||||||||||||||
1025 | - | |||||||||||||||||||
1026 | #ifndef QT_NO_REGEXP_CCLASS | - | ||||||||||||||||||
1027 | - | |||||||||||||||||||
1028 | #define FLAG(x) (1 << (x)) | - | ||||||||||||||||||
1029 | /* | - | ||||||||||||||||||
1030 | The class QRegExpCharClass represents a set of characters, such as can | - | ||||||||||||||||||
1031 | be found in regular expressions (e.g., [a-z] denotes the set | - | ||||||||||||||||||
1032 | {a, b, ..., z}). | - | ||||||||||||||||||
1033 | */ | - | ||||||||||||||||||
1034 | class QRegExpCharClass | - | ||||||||||||||||||
1035 | { | - | ||||||||||||||||||
1036 | public: | - | ||||||||||||||||||
1037 | QRegExpCharClass(); | - | ||||||||||||||||||
1038 | - | |||||||||||||||||||
1039 | void clear(); | - | ||||||||||||||||||
1040 | bool negative() const { return n; } executed 1533679 times by 31 tests: return n; Executed by:
| 1533679 | ||||||||||||||||||
1041 | void setNegative(bool negative); | - | ||||||||||||||||||
1042 | void addCategories(uint cats); | - | ||||||||||||||||||
1043 | void addRange(ushort from, ushort to); | - | ||||||||||||||||||
1044 | void addSingleton(ushort ch) { addRange(ch, ch); } executed 6188 times by 47 tests: end of block Executed by:
| 6188 | ||||||||||||||||||
1045 | - | |||||||||||||||||||
1046 | bool in(QChar ch) const; | - | ||||||||||||||||||
1047 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1048 | const QVector<int> &firstOccurrence() const { return occ1; } executed 6409 times by 116 tests: return occ1; Executed by:
| 6409 | ||||||||||||||||||
1049 | #endif | - | ||||||||||||||||||
1050 | - | |||||||||||||||||||
1051 | #if defined(QT_DEBUG) | - | ||||||||||||||||||
1052 | void dump() const; | - | ||||||||||||||||||
1053 | #endif | - | ||||||||||||||||||
1054 | - | |||||||||||||||||||
1055 | private: | - | ||||||||||||||||||
1056 | QVector<QRegExpCharClassRange> r; // character ranges | - | ||||||||||||||||||
1057 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1058 | QVector<int> occ1; // first-occurrence array | - | ||||||||||||||||||
1059 | #endif | - | ||||||||||||||||||
1060 | uint c; // character classes | - | ||||||||||||||||||
1061 | bool n; // negative? | - | ||||||||||||||||||
1062 | }; | - | ||||||||||||||||||
1063 | #else | - | ||||||||||||||||||
1064 | struct QRegExpCharClass | - | ||||||||||||||||||
1065 | { | - | ||||||||||||||||||
1066 | int dummy; | - | ||||||||||||||||||
1067 | - | |||||||||||||||||||
1068 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1069 | QRegExpCharClass() { occ1.fill(0, NumBadChars); } | - | ||||||||||||||||||
1070 | - | |||||||||||||||||||
1071 | const QVector<int> &firstOccurrence() const { return occ1; } | - | ||||||||||||||||||
1072 | QVector<int> occ1; | - | ||||||||||||||||||
1073 | #endif | - | ||||||||||||||||||
1074 | }; | - | ||||||||||||||||||
1075 | #endif | - | ||||||||||||||||||
1076 | - | |||||||||||||||||||
1077 | Q_DECLARE_TYPEINFO(QRegExpCharClass, Q_MOVABLE_TYPE); | - | ||||||||||||||||||
1078 | - | |||||||||||||||||||
1079 | /* | - | ||||||||||||||||||
1080 | The QRegExpEngine class encapsulates a modified nondeterministic | - | ||||||||||||||||||
1081 | finite automaton (NFA). | - | ||||||||||||||||||
1082 | */ | - | ||||||||||||||||||
1083 | class QRegExpEngine | - | ||||||||||||||||||
1084 | { | - | ||||||||||||||||||
1085 | public: | - | ||||||||||||||||||
1086 | QRegExpEngine(Qt::CaseSensitivity cs, bool greedyQuantifiers) | - | ||||||||||||||||||
1087 | : cs(cs), greedyQuantifiers(greedyQuantifiers) { setup(); } executed 126 times by 2 tests: end of block Executed by:
| 126 | ||||||||||||||||||
1088 | - | |||||||||||||||||||
1089 | QRegExpEngine(const QRegExpEngineKey &key); | - | ||||||||||||||||||
1090 | ~QRegExpEngine(); | - | ||||||||||||||||||
1091 | - | |||||||||||||||||||
1092 | bool isValid() const { return valid; } executed 2263 times by 7 tests: return valid; Executed by:
| 2263 | ||||||||||||||||||
1093 | const QString &errorString() const { return yyError; } never executed: return yyError; | 0 | ||||||||||||||||||
1094 | int captureCount() const { return officialncap; } executed 2510822 times by 162 tests: return officialncap; Executed by:
| 2510822 | ||||||||||||||||||
1095 | - | |||||||||||||||||||
1096 | int createState(QChar ch); | - | ||||||||||||||||||
1097 | int createState(const QRegExpCharClass &cc); | - | ||||||||||||||||||
1098 | #ifndef QT_NO_REGEXP_BACKREF | - | ||||||||||||||||||
1099 | int createState(int bref); | - | ||||||||||||||||||
1100 | #endif | - | ||||||||||||||||||
1101 | - | |||||||||||||||||||
1102 | void addCatTransitions(const QVector<int> &from, const QVector<int> &to); | - | ||||||||||||||||||
1103 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1104 | void addPlusTransitions(const QVector<int> &from, const QVector<int> &to, int atom); | - | ||||||||||||||||||
1105 | #endif | - | ||||||||||||||||||
1106 | - | |||||||||||||||||||
1107 | #ifndef QT_NO_REGEXP_ANCHOR_ALT | - | ||||||||||||||||||
1108 | int anchorAlternation(int a, int b); | - | ||||||||||||||||||
1109 | int anchorConcatenation(int a, int b); | - | ||||||||||||||||||
1110 | #else | - | ||||||||||||||||||
1111 | int anchorAlternation(int a, int b) { return a & b; } | - | ||||||||||||||||||
1112 | int anchorConcatenation(int a, int b) { return a | b; } | - | ||||||||||||||||||
1113 | #endif | - | ||||||||||||||||||
1114 | void addAnchors(int from, int to, int a); | - | ||||||||||||||||||
1115 | - | |||||||||||||||||||
1116 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1117 | void heuristicallyChooseHeuristic(); | - | ||||||||||||||||||
1118 | #endif | - | ||||||||||||||||||
1119 | - | |||||||||||||||||||
1120 | #if defined(QT_DEBUG) | - | ||||||||||||||||||
1121 | void dump() const; | - | ||||||||||||||||||
1122 | #endif | - | ||||||||||||||||||
1123 | - | |||||||||||||||||||
1124 | QAtomicInt ref; | - | ||||||||||||||||||
1125 | - | |||||||||||||||||||
1126 | private: | - | ||||||||||||||||||
1127 | enum { CharClassBit = 0x10000, BackRefBit = 0x20000 }; | - | ||||||||||||||||||
1128 | enum { InitialState = 0, FinalState = 1 }; | - | ||||||||||||||||||
1129 | - | |||||||||||||||||||
1130 | void setup(); | - | ||||||||||||||||||
1131 | int setupState(int match); | - | ||||||||||||||||||
1132 | - | |||||||||||||||||||
1133 | /* | - | ||||||||||||||||||
1134 | Let's hope that 13 lookaheads and 14 back-references are | - | ||||||||||||||||||
1135 | enough. | - | ||||||||||||||||||
1136 | */ | - | ||||||||||||||||||
1137 | enum { MaxLookaheads = 13, MaxBackRefs = 14 }; | - | ||||||||||||||||||
1138 | enum { Anchor_Dollar = 0x00000001, Anchor_Caret = 0x00000002, Anchor_Word = 0x00000004, | - | ||||||||||||||||||
1139 | Anchor_NonWord = 0x00000008, Anchor_FirstLookahead = 0x00000010, | - | ||||||||||||||||||
1140 | Anchor_BackRef1Empty = Anchor_FirstLookahead << MaxLookaheads, | - | ||||||||||||||||||
1141 | Anchor_BackRef0Empty = Anchor_BackRef1Empty >> 1, | - | ||||||||||||||||||
1142 | Anchor_Alternation = unsigned(Anchor_BackRef1Empty) << MaxBackRefs, | - | ||||||||||||||||||
1143 | - | |||||||||||||||||||
1144 | Anchor_LookaheadMask = (Anchor_FirstLookahead - 1) ^ | - | ||||||||||||||||||
1145 | ((Anchor_FirstLookahead << MaxLookaheads) - 1) }; | - | ||||||||||||||||||
1146 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1147 | int startAtom(bool officialCapture); | - | ||||||||||||||||||
1148 | void finishAtom(int atom, bool needCapture); | - | ||||||||||||||||||
1149 | #endif | - | ||||||||||||||||||
1150 | - | |||||||||||||||||||
1151 | #ifndef QT_NO_REGEXP_LOOKAHEAD | - | ||||||||||||||||||
1152 | int addLookahead(QRegExpEngine *eng, bool negative); | - | ||||||||||||||||||
1153 | #endif | - | ||||||||||||||||||
1154 | - | |||||||||||||||||||
1155 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1156 | bool goodStringMatch(QRegExpMatchState &matchState) const; | - | ||||||||||||||||||
1157 | bool badCharMatch(QRegExpMatchState &matchState) const; | - | ||||||||||||||||||
1158 | #else | - | ||||||||||||||||||
1159 | bool bruteMatch(QRegExpMatchState &matchState) const; | - | ||||||||||||||||||
1160 | #endif | - | ||||||||||||||||||
1161 | - | |||||||||||||||||||
1162 | QVector<QRegExpAutomatonState> s; // array of states | - | ||||||||||||||||||
1163 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1164 | QVector<QRegExpAtom> f; // atom hierarchy | - | ||||||||||||||||||
1165 | int nf; // number of atoms | - | ||||||||||||||||||
1166 | int cf; // current atom | - | ||||||||||||||||||
1167 | QVector<int> captureForOfficialCapture; | - | ||||||||||||||||||
1168 | #endif | - | ||||||||||||||||||
1169 | int officialncap; // number of captures, seen from the outside | - | ||||||||||||||||||
1170 | int ncap; // number of captures, seen from the inside | - | ||||||||||||||||||
1171 | #ifndef QT_NO_REGEXP_CCLASS | - | ||||||||||||||||||
1172 | QVector<QRegExpCharClass> cl; // array of character classes | - | ||||||||||||||||||
1173 | #endif | - | ||||||||||||||||||
1174 | #ifndef QT_NO_REGEXP_LOOKAHEAD | - | ||||||||||||||||||
1175 | QVector<QRegExpLookahead *> ahead; // array of lookaheads | - | ||||||||||||||||||
1176 | #endif | - | ||||||||||||||||||
1177 | #ifndef QT_NO_REGEXP_ANCHOR_ALT | - | ||||||||||||||||||
1178 | QVector<QRegExpAnchorAlternation> aa; // array of (a, b) pairs of anchors | - | ||||||||||||||||||
1179 | #endif | - | ||||||||||||||||||
1180 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1181 | bool caretAnchored; // does the regexp start with ^? | - | ||||||||||||||||||
1182 | bool trivial; // is the good-string all that needs to match? | - | ||||||||||||||||||
1183 | #endif | - | ||||||||||||||||||
1184 | bool valid; // is the regular expression valid? | - | ||||||||||||||||||
1185 | Qt::CaseSensitivity cs; // case sensitive? | - | ||||||||||||||||||
1186 | bool greedyQuantifiers; // RegExp2? | - | ||||||||||||||||||
1187 | bool xmlSchemaExtensions; | - | ||||||||||||||||||
1188 | #ifndef QT_NO_REGEXP_BACKREF | - | ||||||||||||||||||
1189 | int nbrefs; // number of back-references | - | ||||||||||||||||||
1190 | #endif | - | ||||||||||||||||||
1191 | - | |||||||||||||||||||
1192 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1193 | bool useGoodStringHeuristic; // use goodStringMatch? otherwise badCharMatch | - | ||||||||||||||||||
1194 | - | |||||||||||||||||||
1195 | int goodEarlyStart; // the index where goodStr can first occur in a match | - | ||||||||||||||||||
1196 | int goodLateStart; // the index where goodStr can last occur in a match | - | ||||||||||||||||||
1197 | QString goodStr; // the string that any match has to contain | - | ||||||||||||||||||
1198 | - | |||||||||||||||||||
1199 | int minl; // the minimum length of a match | - | ||||||||||||||||||
1200 | QVector<int> occ1; // first-occurrence array | - | ||||||||||||||||||
1201 | #endif | - | ||||||||||||||||||
1202 | - | |||||||||||||||||||
1203 | /* | - | ||||||||||||||||||
1204 | The class Box is an abstraction for a regular expression | - | ||||||||||||||||||
1205 | fragment. It can also be seen as one node in the syntax tree of | - | ||||||||||||||||||
1206 | a regular expression with synthetized attributes. | - | ||||||||||||||||||
1207 | - | |||||||||||||||||||
1208 | Its interface is ugly for performance reasons. | - | ||||||||||||||||||
1209 | */ | - | ||||||||||||||||||
1210 | class Box | - | ||||||||||||||||||
1211 | { | - | ||||||||||||||||||
1212 | public: | - | ||||||||||||||||||
1213 | Box(QRegExpEngine *engine); | - | ||||||||||||||||||
1214 | Box(const Box &b) { operator=(b); } never executed: end of block | 0 | ||||||||||||||||||
1215 | - | |||||||||||||||||||
1216 | Box &operator=(const Box &b); | - | ||||||||||||||||||
1217 | - | |||||||||||||||||||
1218 | void clear() { operator=(Box(eng)); } executed 22 times by 2 tests: end of block Executed by:
| 22 | ||||||||||||||||||
1219 | void set(QChar ch); | - | ||||||||||||||||||
1220 | void set(const QRegExpCharClass &cc); | - | ||||||||||||||||||
1221 | #ifndef QT_NO_REGEXP_BACKREF | - | ||||||||||||||||||
1222 | void set(int bref); | - | ||||||||||||||||||
1223 | #endif | - | ||||||||||||||||||
1224 | - | |||||||||||||||||||
1225 | void cat(const Box &b); | - | ||||||||||||||||||
1226 | void orx(const Box &b); | - | ||||||||||||||||||
1227 | void plus(int atom); | - | ||||||||||||||||||
1228 | void opt(); | - | ||||||||||||||||||
1229 | void catAnchor(int a); | - | ||||||||||||||||||
1230 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1231 | void setupHeuristics(); | - | ||||||||||||||||||
1232 | #endif | - | ||||||||||||||||||
1233 | - | |||||||||||||||||||
1234 | #if defined(QT_DEBUG) | - | ||||||||||||||||||
1235 | void dump() const; | - | ||||||||||||||||||
1236 | #endif | - | ||||||||||||||||||
1237 | - | |||||||||||||||||||
1238 | private: | - | ||||||||||||||||||
1239 | void addAnchorsToEngine(const Box &to) const; | - | ||||||||||||||||||
1240 | - | |||||||||||||||||||
1241 | QRegExpEngine *eng; // the automaton under construction | - | ||||||||||||||||||
1242 | QVector<int> ls; // the left states (firstpos) | - | ||||||||||||||||||
1243 | QVector<int> rs; // the right states (lastpos) | - | ||||||||||||||||||
1244 | QMap<int, int> lanchors; // the left anchors | - | ||||||||||||||||||
1245 | QMap<int, int> ranchors; // the right anchors | - | ||||||||||||||||||
1246 | int skipanchors; // the anchors to match if the box is skipped | - | ||||||||||||||||||
1247 | - | |||||||||||||||||||
1248 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1249 | int earlyStart; // the index where str can first occur | - | ||||||||||||||||||
1250 | int lateStart; // the index where str can last occur | - | ||||||||||||||||||
1251 | QString str; // a string that has to occur in any match | - | ||||||||||||||||||
1252 | QString leftStr; // a string occurring at the left of this box | - | ||||||||||||||||||
1253 | QString rightStr; // a string occurring at the right of this box | - | ||||||||||||||||||
1254 | int maxl; // the maximum length of this box (possibly InftyLen) | - | ||||||||||||||||||
1255 | #endif | - | ||||||||||||||||||
1256 | - | |||||||||||||||||||
1257 | int minl; // the minimum length of this box | - | ||||||||||||||||||
1258 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1259 | QVector<int> occ1; // first-occurrence array | - | ||||||||||||||||||
1260 | #endif | - | ||||||||||||||||||
1261 | }; | - | ||||||||||||||||||
1262 | - | |||||||||||||||||||
1263 | friend class Box; | - | ||||||||||||||||||
1264 | - | |||||||||||||||||||
1265 | /* | - | ||||||||||||||||||
1266 | This is the lexical analyzer for regular expressions. | - | ||||||||||||||||||
1267 | */ | - | ||||||||||||||||||
1268 | enum { Tok_Eos, Tok_Dollar, Tok_LeftParen, Tok_MagicLeftParen, Tok_PosLookahead, | - | ||||||||||||||||||
1269 | Tok_NegLookahead, Tok_RightParen, Tok_CharClass, Tok_Caret, Tok_Quantifier, Tok_Bar, | - | ||||||||||||||||||
1270 | Tok_Word, Tok_NonWord, Tok_Char = 0x10000, Tok_BackRef = 0x20000 }; | - | ||||||||||||||||||
1271 | int getChar(); | - | ||||||||||||||||||
1272 | int getEscape(); | - | ||||||||||||||||||
1273 | #ifndef QT_NO_REGEXP_INTERVAL | - | ||||||||||||||||||
1274 | int getRep(int def); | - | ||||||||||||||||||
1275 | #endif | - | ||||||||||||||||||
1276 | #ifndef QT_NO_REGEXP_LOOKAHEAD | - | ||||||||||||||||||
1277 | void skipChars(int n); | - | ||||||||||||||||||
1278 | #endif | - | ||||||||||||||||||
1279 | void error(const char *msg); | - | ||||||||||||||||||
1280 | void startTokenizer(const QChar *rx, int len); | - | ||||||||||||||||||
1281 | int getToken(); | - | ||||||||||||||||||
1282 | - | |||||||||||||||||||
1283 | const QChar *yyIn; // a pointer to the input regular expression pattern | - | ||||||||||||||||||
1284 | int yyPos0; // the position of yyTok in the input pattern | - | ||||||||||||||||||
1285 | int yyPos; // the position of the next character to read | - | ||||||||||||||||||
1286 | int yyLen; // the length of yyIn | - | ||||||||||||||||||
1287 | int yyCh; // the last character read | - | ||||||||||||||||||
1288 | QScopedPointer<QRegExpCharClass> yyCharClass; // attribute for Tok_CharClass tokens | - | ||||||||||||||||||
1289 | int yyMinRep; // attribute for Tok_Quantifier | - | ||||||||||||||||||
1290 | int yyMaxRep; // ditto | - | ||||||||||||||||||
1291 | QString yyError; // syntax error or overflow during parsing? | - | ||||||||||||||||||
1292 | - | |||||||||||||||||||
1293 | /* | - | ||||||||||||||||||
1294 | This is the syntactic analyzer for regular expressions. | - | ||||||||||||||||||
1295 | */ | - | ||||||||||||||||||
1296 | int parse(const QChar *rx, int len); | - | ||||||||||||||||||
1297 | void parseAtom(Box *box); | - | ||||||||||||||||||
1298 | void parseFactor(Box *box); | - | ||||||||||||||||||
1299 | void parseTerm(Box *box); | - | ||||||||||||||||||
1300 | void parseExpression(Box *box); | - | ||||||||||||||||||
1301 | - | |||||||||||||||||||
1302 | int yyTok; // the last token read | - | ||||||||||||||||||
1303 | bool yyMayCapture; // set this to false to disable capturing | - | ||||||||||||||||||
1304 | - | |||||||||||||||||||
1305 | friend struct QRegExpMatchState; | - | ||||||||||||||||||
1306 | }; | - | ||||||||||||||||||
1307 | - | |||||||||||||||||||
1308 | #ifndef QT_NO_REGEXP_LOOKAHEAD | - | ||||||||||||||||||
1309 | /* | - | ||||||||||||||||||
1310 | The struct QRegExpLookahead represents a lookahead a la Perl (e.g., | - | ||||||||||||||||||
1311 | (?=foo) and (?!bar)). | - | ||||||||||||||||||
1312 | */ | - | ||||||||||||||||||
1313 | struct QRegExpLookahead | - | ||||||||||||||||||
1314 | { | - | ||||||||||||||||||
1315 | QRegExpEngine *eng; // NFA representing the embedded regular expression | - | ||||||||||||||||||
1316 | bool neg; // negative lookahead? | - | ||||||||||||||||||
1317 | - | |||||||||||||||||||
1318 | inline QRegExpLookahead(QRegExpEngine *eng0, bool neg0) | - | ||||||||||||||||||
1319 | : eng(eng0), neg(neg0) { } executed 126 times by 2 tests: end of block Executed by:
| 126 | ||||||||||||||||||
1320 | inline ~QRegExpLookahead() { delete eng; } executed 126 times by 2 tests: end of block Executed by:
| 126 | ||||||||||||||||||
1321 | }; | - | ||||||||||||||||||
1322 | #endif | - | ||||||||||||||||||
1323 | - | |||||||||||||||||||
1324 | /*! | - | ||||||||||||||||||
1325 | \internal | - | ||||||||||||||||||
1326 | convert the pattern string to the RegExp syntax. | - | ||||||||||||||||||
1327 | - | |||||||||||||||||||
1328 | This is also used by QScriptEngine::newRegExp to convert to a pattern that JavaScriptCore can understan | - | ||||||||||||||||||
1329 | */ | - | ||||||||||||||||||
1330 | Q_CORE_EXPORT QString qt_regexp_toCanonical(const QString &pattern, QRegExp::PatternSyntax patternSyntax) | - | ||||||||||||||||||
1331 | { | - | ||||||||||||||||||
1332 | switch (patternSyntax) { | - | ||||||||||||||||||
1333 | #ifndef QT_NO_REGEXP_WILDCARD | - | ||||||||||||||||||
1334 | case QRegExp::Wildcard: executed 214 times by 61 tests: case QRegExp::Wildcard: Executed by:
| 214 | ||||||||||||||||||
1335 | return wc2rx(pattern, false); executed 214 times by 61 tests: return wc2rx(pattern, false); Executed by:
| 214 | ||||||||||||||||||
1336 | case QRegExp::WildcardUnix: executed 76 times by 23 tests: case QRegExp::WildcardUnix: Executed by:
| 76 | ||||||||||||||||||
1337 | return wc2rx(pattern, true); executed 76 times by 23 tests: return wc2rx(pattern, true); Executed by:
| 76 | ||||||||||||||||||
1338 | #endif | - | ||||||||||||||||||
1339 | case QRegExp::FixedString: executed 97 times by 9 tests: case QRegExp::FixedString: Executed by:
| 97 | ||||||||||||||||||
1340 | return QRegExp::escape(pattern); executed 97 times by 9 tests: return QRegExp::escape(pattern); Executed by:
| 97 | ||||||||||||||||||
1341 | case QRegExp::W3CXmlSchema11: executed 52 times by 1 test: case QRegExp::W3CXmlSchema11: Executed by:
| 52 | ||||||||||||||||||
1342 | default: executed 1113 times by 61 tests: default: Executed by:
| 1113 | ||||||||||||||||||
1343 | return pattern; executed 1165 times by 61 tests: return pattern; Executed by:
| 1165 | ||||||||||||||||||
1344 | } | - | ||||||||||||||||||
1345 | } | - | ||||||||||||||||||
1346 | - | |||||||||||||||||||
1347 | QRegExpEngine::QRegExpEngine(const QRegExpEngineKey &key) | - | ||||||||||||||||||
1348 | : cs(key.cs), greedyQuantifiers(key.patternSyntax == QRegExp::RegExp2), | - | ||||||||||||||||||
1349 | xmlSchemaExtensions(key.patternSyntax == QRegExp::W3CXmlSchema11) | - | ||||||||||||||||||
1350 | { | - | ||||||||||||||||||
1351 | setup(); | - | ||||||||||||||||||
1352 | - | |||||||||||||||||||
1353 | QString rx = qt_regexp_toCanonical(key.pattern, key.patternSyntax); | - | ||||||||||||||||||
1354 | - | |||||||||||||||||||
1355 | valid = (parse(rx.unicode(), rx.length()) == rx.length()); | - | ||||||||||||||||||
1356 | if (!valid) {
| 52-1500 | ||||||||||||||||||
1357 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1358 | trivial = false; | - | ||||||||||||||||||
1359 | #endif | - | ||||||||||||||||||
1360 | error(RXERR_LEFTDELIM); | - | ||||||||||||||||||
1361 | } executed 52 times by 4 tests: end of block Executed by:
| 52 | ||||||||||||||||||
1362 | } executed 1552 times by 116 tests: end of block Executed by:
| 1552 | ||||||||||||||||||
1363 | - | |||||||||||||||||||
1364 | QRegExpEngine::~QRegExpEngine() | - | ||||||||||||||||||
1365 | { | - | ||||||||||||||||||
1366 | #ifndef QT_NO_REGEXP_LOOKAHEAD | - | ||||||||||||||||||
1367 | qDeleteAll(ahead); | - | ||||||||||||||||||
1368 | #endif | - | ||||||||||||||||||
1369 | } executed 1885 times by 299 tests: end of block Executed by:
| 1885 | ||||||||||||||||||
1370 | - | |||||||||||||||||||
1371 | void QRegExpMatchState::prepareForMatch(QRegExpEngine *eng) | - | ||||||||||||||||||
1372 | { | - | ||||||||||||||||||
1373 | /* | - | ||||||||||||||||||
1374 | We use one QVector<int> for all the big data used a lot in | - | ||||||||||||||||||
1375 | matchHere() and friends. | - | ||||||||||||||||||
1376 | */ | - | ||||||||||||||||||
1377 | int ns = eng->s.size(); // number of states | - | ||||||||||||||||||
1378 | int ncap = eng->ncap; | - | ||||||||||||||||||
1379 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1380 | int newSlideTabSize = qMax(eng->minl + 1, 16); | - | ||||||||||||||||||
1381 | #else | - | ||||||||||||||||||
1382 | int newSlideTabSize = 0; | - | ||||||||||||||||||
1383 | #endif | - | ||||||||||||||||||
1384 | int numCaptures = eng->captureCount(); | - | ||||||||||||||||||
1385 | int newCapturedSize = 2 + 2 * numCaptures; | - | ||||||||||||||||||
1386 | bigArray = q_check_ptr((int *)realloc(bigArray, ((3 + 4 * ncap) * ns + 4 * ncap + newSlideTabSize + newCapturedSize)*sizeof(int))); | - | ||||||||||||||||||
1387 | - | |||||||||||||||||||
1388 | // set all internal variables only _after_ bigArray is realloc'ed | - | ||||||||||||||||||
1389 | // to prevent a broken regexp in oom case | - | ||||||||||||||||||
1390 | - | |||||||||||||||||||
1391 | slideTabSize = newSlideTabSize; | - | ||||||||||||||||||
1392 | capturedSize = newCapturedSize; | - | ||||||||||||||||||
1393 | inNextStack = bigArray; | - | ||||||||||||||||||
1394 | memset(inNextStack, -1, ns * sizeof(int)); | - | ||||||||||||||||||
1395 | curStack = inNextStack + ns; | - | ||||||||||||||||||
1396 | nextStack = inNextStack + 2 * ns; | - | ||||||||||||||||||
1397 | - | |||||||||||||||||||
1398 | curCapBegin = inNextStack + 3 * ns; | - | ||||||||||||||||||
1399 | nextCapBegin = curCapBegin + ncap * ns; | - | ||||||||||||||||||
1400 | curCapEnd = curCapBegin + 2 * ncap * ns; | - | ||||||||||||||||||
1401 | nextCapEnd = curCapBegin + 3 * ncap * ns; | - | ||||||||||||||||||
1402 | - | |||||||||||||||||||
1403 | tempCapBegin = curCapBegin + 4 * ncap * ns; | - | ||||||||||||||||||
1404 | tempCapEnd = tempCapBegin + ncap; | - | ||||||||||||||||||
1405 | capBegin = tempCapBegin + 2 * ncap; | - | ||||||||||||||||||
1406 | capEnd = tempCapBegin + 3 * ncap; | - | ||||||||||||||||||
1407 | - | |||||||||||||||||||
1408 | slideTab = tempCapBegin + 4 * ncap; | - | ||||||||||||||||||
1409 | captured = slideTab + slideTabSize; | - | ||||||||||||||||||
1410 | memset(captured, -1, capturedSize*sizeof(int)); | - | ||||||||||||||||||
1411 | this->eng = eng; | - | ||||||||||||||||||
1412 | } executed 2420084 times by 162 tests: end of block Executed by:
| 2420084 | ||||||||||||||||||
1413 | - | |||||||||||||||||||
1414 | /* | - | ||||||||||||||||||
1415 | Tries to match in str and returns an array of (begin, length) pairs | - | ||||||||||||||||||
1416 | for captured text. If there is no match, all pairs are (-1, -1). | - | ||||||||||||||||||
1417 | */ | - | ||||||||||||||||||
1418 | void QRegExpMatchState::match(const QChar *str0, int len0, int pos0, | - | ||||||||||||||||||
1419 | bool minimal0, bool oneTest, int caretIndex) | - | ||||||||||||||||||
1420 | { | - | ||||||||||||||||||
1421 | bool matched = false; | - | ||||||||||||||||||
1422 | QChar char_null; | - | ||||||||||||||||||
1423 | - | |||||||||||||||||||
1424 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1425 | if (eng->trivial && !oneTest) {
| 1226-1704783 | ||||||||||||||||||
1426 | pos = qFindString(str0, len0, pos0, eng->goodStr.unicode(), eng->goodStr.length(), eng->cs); | - | ||||||||||||||||||
1427 | matchLen = eng->goodStr.length(); | - | ||||||||||||||||||
1428 | matched = (pos != -1); | - | ||||||||||||||||||
1429 | } else executed 120085 times by 11 tests: end of block Executed by:
| 120085 | ||||||||||||||||||
1430 | #endif | - | ||||||||||||||||||
1431 | { | - | ||||||||||||||||||
1432 | in = str0; | - | ||||||||||||||||||
1433 | if (in == 0)
| 0-1706009 | ||||||||||||||||||
1434 | in = &char_null; never executed: in = &char_null; | 0 | ||||||||||||||||||
1435 | pos = pos0; | - | ||||||||||||||||||
1436 | caretPos = caretIndex; | - | ||||||||||||||||||
1437 | len = len0; | - | ||||||||||||||||||
1438 | minimal = minimal0; | - | ||||||||||||||||||
1439 | matchLen = 0; | - | ||||||||||||||||||
1440 | oneTestMatchedLen = 0; | - | ||||||||||||||||||
1441 | - | |||||||||||||||||||
1442 | if (eng->valid && pos >= 0 && pos <= len) {
| 0-1679055 | ||||||||||||||||||
1443 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1444 | if (oneTest) {
| 244864-1434184 | ||||||||||||||||||
1445 | matched = matchHere(); | - | ||||||||||||||||||
1446 | } else { executed 1434184 times by 57 tests: end of block Executed by:
| 1434184 | ||||||||||||||||||
1447 | if (pos <= len - eng->minl) {
| 3151-241713 | ||||||||||||||||||
1448 | if (eng->caretAnchored) {
| 2595-239118 | ||||||||||||||||||
1449 | matched = matchHere(); | - | ||||||||||||||||||
1450 | } else if (eng->useGoodStringHeuristic) { executed 2595 times by 26 tests: end of block Executed by:
| 2595-201074 | ||||||||||||||||||
1451 | matched = eng->goodStringMatch(*this); | - | ||||||||||||||||||
1452 | } else { executed 201074 times by 16 tests: end of block Executed by:
| 201074 | ||||||||||||||||||
1453 | matched = eng->badCharMatch(*this); | - | ||||||||||||||||||
1454 | } executed 38044 times by 28 tests: end of block Executed by:
| 38044 | ||||||||||||||||||
1455 | } | - | ||||||||||||||||||
1456 | } executed 244864 times by 45 tests: end of block Executed by:
| 244864 | ||||||||||||||||||
1457 | #else | - | ||||||||||||||||||
1458 | matched = oneTest ? matchHere() : eng->bruteMatch(*this); | - | ||||||||||||||||||
1459 | #endif | - | ||||||||||||||||||
1460 | } | - | ||||||||||||||||||
1461 | } executed 1706009 times by 85 tests: end of block Executed by:
| 1706009 | ||||||||||||||||||
1462 | - | |||||||||||||||||||
1463 | if (matched) {
| 461049-1365045 | ||||||||||||||||||
1464 | int *c = captured; | - | ||||||||||||||||||
1465 | *c++ = pos; | - | ||||||||||||||||||
1466 | *c++ = matchLen; | - | ||||||||||||||||||
1467 | - | |||||||||||||||||||
1468 | int numCaptures = (capturedSize - 2) >> 1; | - | ||||||||||||||||||
1469 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1470 | for (int i = 0; i < numCaptures; ++i) {
| 220587-461049 | ||||||||||||||||||
1471 | int j = eng->captureForOfficialCapture.at(i); | - | ||||||||||||||||||
1472 | if (capBegin[j] != EmptyCapture) {
| 8679-211908 | ||||||||||||||||||
1473 | int len = capEnd[j] - capBegin[j]; | - | ||||||||||||||||||
1474 | *c++ = (len > 0) ? pos + capBegin[j] : 0;
| 0-211908 | ||||||||||||||||||
1475 | *c++ = len; | - | ||||||||||||||||||
1476 | } else { executed 211908 times by 22 tests: end of block Executed by:
| 211908 | ||||||||||||||||||
1477 | *c++ = -1; | - | ||||||||||||||||||
1478 | *c++ = -1; | - | ||||||||||||||||||
1479 | } executed 8679 times by 11 tests: end of block Executed by:
| 8679 | ||||||||||||||||||
1480 | } | - | ||||||||||||||||||
1481 | #endif | - | ||||||||||||||||||
1482 | } else { executed 461049 times by 67 tests: end of block Executed by:
| 461049 | ||||||||||||||||||
1483 | // we rely on 2's complement here | - | ||||||||||||||||||
1484 | memset(captured, -1, capturedSize * sizeof(int)); | - | ||||||||||||||||||
1485 | } executed 1365045 times by 78 tests: end of block Executed by:
| 1365045 | ||||||||||||||||||
1486 | } | - | ||||||||||||||||||
1487 | - | |||||||||||||||||||
1488 | /* | - | ||||||||||||||||||
1489 | The three following functions add one state to the automaton and | - | ||||||||||||||||||
1490 | return the number of the state. | - | ||||||||||||||||||
1491 | */ | - | ||||||||||||||||||
1492 | - | |||||||||||||||||||
1493 | int QRegExpEngine::createState(QChar ch) | - | ||||||||||||||||||
1494 | { | - | ||||||||||||||||||
1495 | return setupState(ch.unicode()); executed 22721 times by 82 tests: return setupState(ch.unicode()); Executed by:
| 22721 | ||||||||||||||||||
1496 | } | - | ||||||||||||||||||
1497 | - | |||||||||||||||||||
1498 | int QRegExpEngine::createState(const QRegExpCharClass &cc) | - | ||||||||||||||||||
1499 | { | - | ||||||||||||||||||
1500 | #ifndef QT_NO_REGEXP_CCLASS | - | ||||||||||||||||||
1501 | int n = cl.size(); | - | ||||||||||||||||||
1502 | cl += QRegExpCharClass(cc); | - | ||||||||||||||||||
1503 | return setupState(CharClassBit | n); executed 6409 times by 116 tests: return setupState(CharClassBit | n); Executed by:
| 6409 | ||||||||||||||||||
1504 | #else | - | ||||||||||||||||||
1505 | Q_UNUSED(cc); | - | ||||||||||||||||||
1506 | return setupState(CharClassBit); | - | ||||||||||||||||||
1507 | #endif | - | ||||||||||||||||||
1508 | } | - | ||||||||||||||||||
1509 | - | |||||||||||||||||||
1510 | #ifndef QT_NO_REGEXP_BACKREF | - | ||||||||||||||||||
1511 | int QRegExpEngine::createState(int bref) | - | ||||||||||||||||||
1512 | { | - | ||||||||||||||||||
1513 | if (bref > nbrefs) {
| 72-118 | ||||||||||||||||||
1514 | nbrefs = bref; | - | ||||||||||||||||||
1515 | if (nbrefs > MaxBackRefs) {
| 0-72 | ||||||||||||||||||
1516 | error(RXERR_LIMIT); | - | ||||||||||||||||||
1517 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
1518 | } | - | ||||||||||||||||||
1519 | } executed 72 times by 1 test: end of block Executed by:
| 72 | ||||||||||||||||||
1520 | return setupState(BackRefBit | bref); executed 190 times by 1 test: return setupState(BackRefBit | bref); Executed by:
| 190 | ||||||||||||||||||
1521 | } | - | ||||||||||||||||||
1522 | #endif | - | ||||||||||||||||||
1523 | - | |||||||||||||||||||
1524 | /* | - | ||||||||||||||||||
1525 | The two following functions add a transition between all pairs of | - | ||||||||||||||||||
1526 | states (i, j) where i is found in from, and j is found in to. | - | ||||||||||||||||||
1527 | - | |||||||||||||||||||
1528 | Cat-transitions are distinguished from plus-transitions for | - | ||||||||||||||||||
1529 | capturing. | - | ||||||||||||||||||
1530 | */ | - | ||||||||||||||||||
1531 | - | |||||||||||||||||||
1532 | void QRegExpEngine::addCatTransitions(const QVector<int> &from, const QVector<int> &to) | - | ||||||||||||||||||
1533 | { | - | ||||||||||||||||||
1534 | for (int i = 0; i < from.size(); i++)
| 30263-32083 | ||||||||||||||||||
1535 | mergeInto(&s[from.at(i)].outs, to); executed 32083 times by 116 tests: mergeInto(&s[from.at(i)].outs, to); Executed by:
| 32083 | ||||||||||||||||||
1536 | } executed 30263 times by 116 tests: end of block Executed by:
| 30263 | ||||||||||||||||||
1537 | - | |||||||||||||||||||
1538 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1539 | void QRegExpEngine::addPlusTransitions(const QVector<int> &from, const QVector<int> &to, int atom) | - | ||||||||||||||||||
1540 | { | - | ||||||||||||||||||
1541 | for (int i = 0; i < from.size(); i++) {
| 2194-3046 | ||||||||||||||||||
1542 | QRegExpAutomatonState &st = s[from.at(i)]; | - | ||||||||||||||||||
1543 | const QVector<int> oldOuts = st.outs; | - | ||||||||||||||||||
1544 | mergeInto(&st.outs, to); | - | ||||||||||||||||||
1545 | if (f.at(atom).capture != QRegExpAtom::NoCapture) {
| 626-2420 | ||||||||||||||||||
1546 | for (int j = 0; j < to.size(); j++) {
| 626-1032 | ||||||||||||||||||
1547 | // ### st.reenter.contains(to.at(j)) check looks suspicious | - | ||||||||||||||||||
1548 | if (!st.reenter.contains(to.at(j)) &&
| 8-1024 | ||||||||||||||||||
1549 | !std::binary_search(oldOuts.constBegin(), oldOuts.constEnd(), to.at(j)))
| 90-934 | ||||||||||||||||||
1550 | st.reenter.insert(to.at(j), atom); executed 934 times by 1 test: st.reenter.insert(to.at(j), atom); Executed by:
| 934 | ||||||||||||||||||
1551 | } executed 1032 times by 1 test: end of block Executed by:
| 1032 | ||||||||||||||||||
1552 | } executed 626 times by 1 test: end of block Executed by:
| 626 | ||||||||||||||||||
1553 | } executed 3046 times by 97 tests: end of block Executed by:
| 3046 | ||||||||||||||||||
1554 | } executed 2194 times by 97 tests: end of block Executed by:
| 2194 | ||||||||||||||||||
1555 | #endif | - | ||||||||||||||||||
1556 | - | |||||||||||||||||||
1557 | #ifndef QT_NO_REGEXP_ANCHOR_ALT | - | ||||||||||||||||||
1558 | /* | - | ||||||||||||||||||
1559 | Returns an anchor that means a OR b. | - | ||||||||||||||||||
1560 | */ | - | ||||||||||||||||||
1561 | int QRegExpEngine::anchorAlternation(int a, int b) | - | ||||||||||||||||||
1562 | { | - | ||||||||||||||||||
1563 | if (((a & b) == a || (a & b) == b) && ((a | b) & Anchor_Alternation) == 0)
| 12-422 | ||||||||||||||||||
1564 | return a & b; executed 270 times by 1 test: return a & b; Executed by:
| 270 | ||||||||||||||||||
1565 | - | |||||||||||||||||||
1566 | int n = aa.size(); | - | ||||||||||||||||||
1567 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1568 | if (n > 0 && aa.at(n - 1).a == a && aa.at(n - 1).b == b)
| 22-420 | ||||||||||||||||||
1569 | return Anchor_Alternation | (n - 1); executed 40 times by 1 test: return Anchor_Alternation | (n - 1); Executed by:
| 40 | ||||||||||||||||||
1570 | #endif | - | ||||||||||||||||||
1571 | - | |||||||||||||||||||
1572 | QRegExpAnchorAlternation element = {a, b}; | - | ||||||||||||||||||
1573 | aa.append(element); | - | ||||||||||||||||||
1574 | return Anchor_Alternation | n; executed 402 times by 1 test: return Anchor_Alternation | n; Executed by:
| 402 | ||||||||||||||||||
1575 | } | - | ||||||||||||||||||
1576 | - | |||||||||||||||||||
1577 | /* | - | ||||||||||||||||||
1578 | Returns an anchor that means a AND b. | - | ||||||||||||||||||
1579 | */ | - | ||||||||||||||||||
1580 | int QRegExpEngine::anchorConcatenation(int a, int b) | - | ||||||||||||||||||
1581 | { | - | ||||||||||||||||||
1582 | if (((a | b) & Anchor_Alternation) == 0)
| 500-41363 | ||||||||||||||||||
1583 | return a | b; executed 41363 times by 116 tests: return a | b; Executed by:
| 41363 | ||||||||||||||||||
1584 | if ((b & Anchor_Alternation) != 0)
| 170-330 | ||||||||||||||||||
1585 | qSwap(a, b); executed 170 times by 1 test: qSwap(a, b); Executed by:
| 170 | ||||||||||||||||||
1586 | - | |||||||||||||||||||
1587 | int aprime = anchorConcatenation(aa.at(a ^ Anchor_Alternation).a, b); | - | ||||||||||||||||||
1588 | int bprime = anchorConcatenation(aa.at(a ^ Anchor_Alternation).b, b); | - | ||||||||||||||||||
1589 | return anchorAlternation(aprime, bprime); executed 500 times by 1 test: return anchorAlternation(aprime, bprime); Executed by:
| 500 | ||||||||||||||||||
1590 | } | - | ||||||||||||||||||
1591 | #endif | - | ||||||||||||||||||
1592 | - | |||||||||||||||||||
1593 | /* | - | ||||||||||||||||||
1594 | Adds anchor a on a transition caracterised by its from state and | - | ||||||||||||||||||
1595 | its to state. | - | ||||||||||||||||||
1596 | */ | - | ||||||||||||||||||
1597 | void QRegExpEngine::addAnchors(int from, int to, int a) | - | ||||||||||||||||||
1598 | { | - | ||||||||||||||||||
1599 | QRegExpAutomatonState &st = s[from]; | - | ||||||||||||||||||
1600 | if (st.anchors.contains(to))
| 172-36566 | ||||||||||||||||||
1601 | a = anchorAlternation(st.anchors.value(to), a); executed 172 times by 1 test: a = anchorAlternation(st.anchors.value(to), a); Executed by:
| 172 | ||||||||||||||||||
1602 | st.anchors.insert(to, a); | - | ||||||||||||||||||
1603 | } executed 36738 times by 116 tests: end of block Executed by:
| 36738 | ||||||||||||||||||
1604 | - | |||||||||||||||||||
1605 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1606 | /* | - | ||||||||||||||||||
1607 | This function chooses between the good-string and the bad-character | - | ||||||||||||||||||
1608 | heuristics. It computes two scores and chooses the heuristic with | - | ||||||||||||||||||
1609 | the highest score. | - | ||||||||||||||||||
1610 | - | |||||||||||||||||||
1611 | Here are some common-sense constraints on the scores that should be | - | ||||||||||||||||||
1612 | respected if the formulas are ever modified: (1) If goodStr is | - | ||||||||||||||||||
1613 | empty, the good-string heuristic scores 0. (2) If the regular | - | ||||||||||||||||||
1614 | expression is trivial, the good-string heuristic should be used. | - | ||||||||||||||||||
1615 | (3) If the search is case insensitive, the good-string heuristic | - | ||||||||||||||||||
1616 | should be used, unless it scores 0. (Case insensitivity turns all | - | ||||||||||||||||||
1617 | entries of occ1 to 0.) (4) If (goodLateStart - goodEarlyStart) is | - | ||||||||||||||||||
1618 | big, the good-string heuristic should score less. | - | ||||||||||||||||||
1619 | */ | - | ||||||||||||||||||
1620 | void QRegExpEngine::heuristicallyChooseHeuristic() | - | ||||||||||||||||||
1621 | { | - | ||||||||||||||||||
1622 | if (minl == 0) {
| 421-1257 | ||||||||||||||||||
1623 | useGoodStringHeuristic = false; | - | ||||||||||||||||||
1624 | } else if (trivial) { executed 421 times by 61 tests: end of block Executed by:
| 421-775 | ||||||||||||||||||
1625 | useGoodStringHeuristic = true; | - | ||||||||||||||||||
1626 | } else { executed 482 times by 17 tests: end of block Executed by:
| 482 | ||||||||||||||||||
1627 | /* | - | ||||||||||||||||||
1628 | Magic formula: The good string has to constitute a good | - | ||||||||||||||||||
1629 | proportion of the minimum-length string, and appear at a | - | ||||||||||||||||||
1630 | more-or-less known index. | - | ||||||||||||||||||
1631 | */ | - | ||||||||||||||||||
1632 | int goodStringScore = (64 * goodStr.length() / minl) - | - | ||||||||||||||||||
1633 | (goodLateStart - goodEarlyStart); | - | ||||||||||||||||||
1634 | /* | - | ||||||||||||||||||
1635 | Less magic formula: We pick some characters at random, and | - | ||||||||||||||||||
1636 | check whether they are good or bad. | - | ||||||||||||||||||
1637 | */ | - | ||||||||||||||||||
1638 | int badCharScore = 0; | - | ||||||||||||||||||
1639 | int step = qMax(1, NumBadChars / 32); | - | ||||||||||||||||||
1640 | for (int i = 1; i < NumBadChars; i += step) {
| 775-24800 | ||||||||||||||||||
1641 | if (occ1.at(i) == NoOccurrence)
| 9241-15559 | ||||||||||||||||||
1642 | badCharScore += minl; executed 9241 times by 38 tests: badCharScore += minl; Executed by:
| 9241 | ||||||||||||||||||
1643 | else | - | ||||||||||||||||||
1644 | badCharScore += occ1.at(i); executed 15559 times by 86 tests: badCharScore += occ1.at(i); Executed by:
| 15559 | ||||||||||||||||||
1645 | } | - | ||||||||||||||||||
1646 | badCharScore /= minl; | - | ||||||||||||||||||
1647 | useGoodStringHeuristic = (goodStringScore > badCharScore); | - | ||||||||||||||||||
1648 | } executed 775 times by 86 tests: end of block Executed by:
| 775 | ||||||||||||||||||
1649 | } | - | ||||||||||||||||||
1650 | #endif | - | ||||||||||||||||||
1651 | - | |||||||||||||||||||
1652 | #if defined(QT_DEBUG) | - | ||||||||||||||||||
1653 | void QRegExpEngine::dump() const | - | ||||||||||||||||||
1654 | { | - | ||||||||||||||||||
1655 | int i, j; | - | ||||||||||||||||||
1656 | qDebug("Case %ssensitive engine", cs ? "" : "in"); | - | ||||||||||||||||||
1657 | qDebug(" States"); | - | ||||||||||||||||||
1658 | for (i = 0; i < s.size(); i++) {
| 0 | ||||||||||||||||||
1659 | qDebug(" %d%s", i, i == InitialState ? " (initial)" : i == FinalState ? " (final)" : ""); | - | ||||||||||||||||||
1660 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1661 | if (nf > 0)
| 0 | ||||||||||||||||||
1662 | qDebug(" in atom %d", s[i].atom); never executed: QMessageLogger(__FILE__, 1662, __PRETTY_FUNCTION__).debug(" in atom %d", s[i].atom); | 0 | ||||||||||||||||||
1663 | #endif | - | ||||||||||||||||||
1664 | int m = s[i].match; | - | ||||||||||||||||||
1665 | if ((m & CharClassBit) != 0) {
| 0 | ||||||||||||||||||
1666 | qDebug(" match character class %d", m ^ CharClassBit); | - | ||||||||||||||||||
1667 | #ifndef QT_NO_REGEXP_CCLASS | - | ||||||||||||||||||
1668 | cl[m ^ CharClassBit].dump(); | - | ||||||||||||||||||
1669 | #else | - | ||||||||||||||||||
1670 | qDebug(" negative character class"); | - | ||||||||||||||||||
1671 | #endif | - | ||||||||||||||||||
1672 | } else if ((m & BackRefBit) != 0) { never executed: end of block
| 0 | ||||||||||||||||||
1673 | qDebug(" match back-reference %d", m ^ BackRefBit); | - | ||||||||||||||||||
1674 | } else if (m >= 0x20 && m <= 0x7e) { never executed: end of block
| 0 | ||||||||||||||||||
1675 | qDebug(" match 0x%.4x (%c)", m, m); | - | ||||||||||||||||||
1676 | } else { never executed: end of block | 0 | ||||||||||||||||||
1677 | qDebug(" match 0x%.4x", m); | - | ||||||||||||||||||
1678 | } never executed: end of block | 0 | ||||||||||||||||||
1679 | for (j = 0; j < s[i].outs.size(); j++) {
| 0 | ||||||||||||||||||
1680 | int next = s[i].outs[j]; | - | ||||||||||||||||||
1681 | qDebug(" -> %d", next); | - | ||||||||||||||||||
1682 | if (s[i].reenter.contains(next))
| 0 | ||||||||||||||||||
1683 | qDebug(" [reenter %d]", s[i].reenter[next]); never executed: QMessageLogger(__FILE__, 1683, __PRETTY_FUNCTION__).debug(" [reenter %d]", s[i].reenter[next]); | 0 | ||||||||||||||||||
1684 | if (s[i].anchors.value(next) != 0)
| 0 | ||||||||||||||||||
1685 | qDebug(" [anchors 0x%.8x]", s[i].anchors[next]); never executed: QMessageLogger(__FILE__, 1685, __PRETTY_FUNCTION__).debug(" [anchors 0x%.8x]", s[i].anchors[next]); | 0 | ||||||||||||||||||
1686 | } never executed: end of block | 0 | ||||||||||||||||||
1687 | } never executed: end of block | 0 | ||||||||||||||||||
1688 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1689 | if (nf > 0) {
| 0 | ||||||||||||||||||
1690 | qDebug(" Atom Parent Capture"); | - | ||||||||||||||||||
1691 | for (i = 0; i < nf; i++) {
| 0 | ||||||||||||||||||
1692 | if (f[i].capture == QRegExpAtom::NoCapture) {
| 0 | ||||||||||||||||||
1693 | qDebug(" %6d %6d nil", i, f[i].parent); | - | ||||||||||||||||||
1694 | } else { never executed: end of block | 0 | ||||||||||||||||||
1695 | int cap = f[i].capture; | - | ||||||||||||||||||
1696 | bool official = captureForOfficialCapture.contains(cap); | - | ||||||||||||||||||
1697 | qDebug(" %6d %6d %6d %s", i, f[i].parent, f[i].capture, | - | ||||||||||||||||||
1698 | official ? "official" : ""); | - | ||||||||||||||||||
1699 | } never executed: end of block | 0 | ||||||||||||||||||
1700 | } | - | ||||||||||||||||||
1701 | } never executed: end of block | 0 | ||||||||||||||||||
1702 | #endif | - | ||||||||||||||||||
1703 | #ifndef QT_NO_REGEXP_ANCHOR_ALT | - | ||||||||||||||||||
1704 | for (i = 0; i < aa.size(); i++)
| 0 | ||||||||||||||||||
1705 | qDebug(" Anchor alternation 0x%.8x: 0x%.8x 0x%.9x", i, aa[i].a, aa[i].b); never executed: QMessageLogger(__FILE__, 1705, __PRETTY_FUNCTION__).debug(" Anchor alternation 0x%.8x: 0x%.8x 0x%.9x", i, aa[i].a, aa[i].b); | 0 | ||||||||||||||||||
1706 | #endif | - | ||||||||||||||||||
1707 | } never executed: end of block | 0 | ||||||||||||||||||
1708 | #endif | - | ||||||||||||||||||
1709 | - | |||||||||||||||||||
1710 | void QRegExpEngine::setup() | - | ||||||||||||||||||
1711 | { | - | ||||||||||||||||||
1712 | ref.store(1); | - | ||||||||||||||||||
1713 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1714 | f.resize(32); | - | ||||||||||||||||||
1715 | nf = 0; | - | ||||||||||||||||||
1716 | cf = -1; | - | ||||||||||||||||||
1717 | #endif | - | ||||||||||||||||||
1718 | officialncap = 0; | - | ||||||||||||||||||
1719 | ncap = 0; | - | ||||||||||||||||||
1720 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1721 | caretAnchored = true; | - | ||||||||||||||||||
1722 | trivial = true; | - | ||||||||||||||||||
1723 | #endif | - | ||||||||||||||||||
1724 | valid = false; | - | ||||||||||||||||||
1725 | #ifndef QT_NO_REGEXP_BACKREF | - | ||||||||||||||||||
1726 | nbrefs = 0; | - | ||||||||||||||||||
1727 | #endif | - | ||||||||||||||||||
1728 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1729 | useGoodStringHeuristic = true; | - | ||||||||||||||||||
1730 | minl = 0; | - | ||||||||||||||||||
1731 | occ1.fill(0, NumBadChars); | - | ||||||||||||||||||
1732 | #endif | - | ||||||||||||||||||
1733 | } executed 1678 times by 116 tests: end of block Executed by:
| 1678 | ||||||||||||||||||
1734 | - | |||||||||||||||||||
1735 | int QRegExpEngine::setupState(int match) | - | ||||||||||||||||||
1736 | { | - | ||||||||||||||||||
1737 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1738 | s += QRegExpAutomatonState(cf, match); | - | ||||||||||||||||||
1739 | #else | - | ||||||||||||||||||
1740 | s += QRegExpAutomatonState(match); | - | ||||||||||||||||||
1741 | #endif | - | ||||||||||||||||||
1742 | return s.size() - 1; executed 29320 times by 116 tests: return s.size() - 1; Executed by:
| 29320 | ||||||||||||||||||
1743 | } | - | ||||||||||||||||||
1744 | - | |||||||||||||||||||
1745 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1746 | /* | - | ||||||||||||||||||
1747 | Functions startAtom() and finishAtom() should be called to delimit | - | ||||||||||||||||||
1748 | atoms. When a state is created, it is assigned to the current atom. | - | ||||||||||||||||||
1749 | The information is later used for capturing. | - | ||||||||||||||||||
1750 | */ | - | ||||||||||||||||||
1751 | int QRegExpEngine::startAtom(bool officialCapture) | - | ||||||||||||||||||
1752 | { | - | ||||||||||||||||||
1753 | if ((nf & (nf + 1)) == 0 && nf + 1 >= f.size())
| 245-26924 | ||||||||||||||||||
1754 | f.resize((nf + 1) << 1); executed 245 times by 14 tests: f.resize((nf + 1) << 1); Executed by:
| 245 | ||||||||||||||||||
1755 | f[nf].parent = cf; | - | ||||||||||||||||||
1756 | cf = nf++; | - | ||||||||||||||||||
1757 | f[cf].capture = officialCapture ? QRegExpAtom::OfficialCapture : QRegExpAtom::NoCapture;
| 643-30996 | ||||||||||||||||||
1758 | return cf; executed 31639 times by 116 tests: return cf; Executed by:
| 31639 | ||||||||||||||||||
1759 | } | - | ||||||||||||||||||
1760 | - | |||||||||||||||||||
1761 | void QRegExpEngine::finishAtom(int atom, bool needCapture) | - | ||||||||||||||||||
1762 | { | - | ||||||||||||||||||
1763 | if (greedyQuantifiers && needCapture && f[atom].capture == QRegExpAtom::NoCapture)
| 0-26451 | ||||||||||||||||||
1764 | f[atom].capture = QRegExpAtom::UnofficialCapture; executed 1136 times by 1 test: f[atom].capture = QRegExpAtom::UnofficialCapture; Executed by:
| 1136 | ||||||||||||||||||
1765 | cf = f.at(atom).parent; | - | ||||||||||||||||||
1766 | } executed 31639 times by 116 tests: end of block Executed by:
| 31639 | ||||||||||||||||||
1767 | #endif | - | ||||||||||||||||||
1768 | - | |||||||||||||||||||
1769 | #ifndef QT_NO_REGEXP_LOOKAHEAD | - | ||||||||||||||||||
1770 | /* | - | ||||||||||||||||||
1771 | Creates a lookahead anchor. | - | ||||||||||||||||||
1772 | */ | - | ||||||||||||||||||
1773 | int QRegExpEngine::addLookahead(QRegExpEngine *eng, bool negative) | - | ||||||||||||||||||
1774 | { | - | ||||||||||||||||||
1775 | int n = ahead.size(); | - | ||||||||||||||||||
1776 | if (n == MaxLookaheads) {
| 0-126 | ||||||||||||||||||
1777 | error(RXERR_LIMIT); | - | ||||||||||||||||||
1778 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
1779 | } | - | ||||||||||||||||||
1780 | ahead += new QRegExpLookahead(eng, negative); | - | ||||||||||||||||||
1781 | return Anchor_FirstLookahead << n; executed 126 times by 2 tests: return Anchor_FirstLookahead << n; Executed by:
| 126 | ||||||||||||||||||
1782 | } | - | ||||||||||||||||||
1783 | #endif | - | ||||||||||||||||||
1784 | - | |||||||||||||||||||
1785 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1786 | /* | - | ||||||||||||||||||
1787 | We want the longest leftmost captures. | - | ||||||||||||||||||
1788 | */ | - | ||||||||||||||||||
1789 | static bool isBetterCapture(int ncap, const int *begin1, const int *end1, const int *begin2, | - | ||||||||||||||||||
1790 | const int *end2) | - | ||||||||||||||||||
1791 | { | - | ||||||||||||||||||
1792 | for (int i = 0; i < ncap; i++) {
| 2065-17888 | ||||||||||||||||||
1793 | int delta = begin2[i] - begin1[i]; // it has to start early... | - | ||||||||||||||||||
1794 | if (delta == 0)
| 789-17099 | ||||||||||||||||||
1795 | delta = end1[i] - end2[i]; // ...and end late executed 17099 times by 3 tests: delta = end1[i] - end2[i]; Executed by:
| 17099 | ||||||||||||||||||
1796 | - | |||||||||||||||||||
1797 | if (delta != 0)
| 950-16938 | ||||||||||||||||||
1798 | return delta > 0; executed 950 times by 3 tests: return delta > 0; Executed by:
| 950 | ||||||||||||||||||
1799 | } executed 16938 times by 3 tests: end of block Executed by:
| 16938 | ||||||||||||||||||
1800 | return false; executed 2065 times by 2 tests: return false; Executed by:
| 2065 | ||||||||||||||||||
1801 | } | - | ||||||||||||||||||
1802 | #endif | - | ||||||||||||||||||
1803 | - | |||||||||||||||||||
1804 | /* | - | ||||||||||||||||||
1805 | Returns \c true if anchor a matches at position pos + i in the input | - | ||||||||||||||||||
1806 | string, otherwise false. | - | ||||||||||||||||||
1807 | */ | - | ||||||||||||||||||
1808 | bool QRegExpMatchState::testAnchor(int i, int a, const int *capBegin) | - | ||||||||||||||||||
1809 | { | - | ||||||||||||||||||
1810 | int j; | - | ||||||||||||||||||
1811 | - | |||||||||||||||||||
1812 | #ifndef QT_NO_REGEXP_ANCHOR_ALT | - | ||||||||||||||||||
1813 | if ((a & QRegExpEngine::Anchor_Alternation) != 0)
| 336-1545780 | ||||||||||||||||||
1814 | return testAnchor(i, eng->aa.at(a ^ QRegExpEngine::Anchor_Alternation).a, capBegin) executed 336 times by 1 test: return testAnchor(i, eng->aa.at(a ^ QRegExpEngine::Anchor_Alternation).a, capBegin) || testAnchor(i, eng->aa.at(a ^ QRegExpEngine::Anchor_Alternation).b, capBegin); Executed by:
| 336 | ||||||||||||||||||
1815 | || testAnchor(i, eng->aa.at(a ^ QRegExpEngine::Anchor_Alternation).b, capBegin); executed 336 times by 1 test: return testAnchor(i, eng->aa.at(a ^ QRegExpEngine::Anchor_Alternation).a, capBegin) || testAnchor(i, eng->aa.at(a ^ QRegExpEngine::Anchor_Alternation).b, capBegin); Executed by:
| 336 | ||||||||||||||||||
1816 | #endif | - | ||||||||||||||||||
1817 | - | |||||||||||||||||||
1818 | if ((a & QRegExpEngine::Anchor_Caret) != 0) {
| 707353-838427 | ||||||||||||||||||
1819 | if (pos + i != caretPos)
| 90706-616647 | ||||||||||||||||||
1820 | return false; executed 90706 times by 4 tests: return false; Executed by:
| 90706 | ||||||||||||||||||
1821 | } executed 616647 times by 27 tests: end of block Executed by:
| 616647 | ||||||||||||||||||
1822 | if ((a & QRegExpEngine::Anchor_Dollar) != 0) {
| 24202-1430872 | ||||||||||||||||||
1823 | if (pos + i != len)
| 8027-16175 | ||||||||||||||||||
1824 | return false; executed 16175 times by 11 tests: return false; Executed by:
| 16175 | ||||||||||||||||||
1825 | } executed 8027 times by 15 tests: end of block Executed by:
| 8027 | ||||||||||||||||||
1826 | #ifndef QT_NO_REGEXP_ESCAPE | - | ||||||||||||||||||
1827 | if ((a & (QRegExpEngine::Anchor_Word | QRegExpEngine::Anchor_NonWord)) != 0) {
| 149-1438750 | ||||||||||||||||||
1828 | bool before = false; | - | ||||||||||||||||||
1829 | bool after = false; | - | ||||||||||||||||||
1830 | if (pos + i != 0)
| 6-143 | ||||||||||||||||||
1831 | before = isWord(in[pos + i - 1]); executed 143 times by 2 tests: before = isWord(in[pos + i - 1]); Executed by:
| 143 | ||||||||||||||||||
1832 | if (pos + i != len)
| 5-144 | ||||||||||||||||||
1833 | after = isWord(in[pos + i]); executed 144 times by 2 tests: after = isWord(in[pos + i]); Executed by:
| 144 | ||||||||||||||||||
1834 | if ((a & QRegExpEngine::Anchor_Word) != 0 && (before == after))
| 0-149 | ||||||||||||||||||
1835 | return false; executed 101 times by 1 test: return false; Executed by:
| 101 | ||||||||||||||||||
1836 | if ((a & QRegExpEngine::Anchor_NonWord) != 0 && (before != after))
| 0-48 | ||||||||||||||||||
1837 | return false; never executed: return false; | 0 | ||||||||||||||||||
1838 | } executed 48 times by 2 tests: end of block Executed by:
| 48 | ||||||||||||||||||
1839 | #endif | - | ||||||||||||||||||
1840 | #ifndef QT_NO_REGEXP_LOOKAHEAD | - | ||||||||||||||||||
1841 | if ((a & QRegExpEngine::Anchor_LookaheadMask) != 0) {
| 638190-800608 | ||||||||||||||||||
1842 | const QVector<QRegExpLookahead *> &ahead = eng->ahead; | - | ||||||||||||||||||
1843 | for (j = 0; j < ahead.size(); j++) {
| 800113-10402080 | ||||||||||||||||||
1844 | if ((a & (QRegExpEngine::Anchor_FirstLookahead << j)) != 0) {
| 800608-9601472 | ||||||||||||||||||
1845 | QRegExpMatchState matchState; | - | ||||||||||||||||||
1846 | matchState.prepareForMatch(ahead[j]->eng); | - | ||||||||||||||||||
1847 | matchState.match(in + pos + i, len - pos - i, 0, | - | ||||||||||||||||||
1848 | true, true, caretPos - pos - i); | - | ||||||||||||||||||
1849 | if ((matchState.captured[0] == 0) == ahead[j]->neg)
| 495-800113 | ||||||||||||||||||
1850 | return false; executed 495 times by 2 tests: return false; Executed by:
| 495 | ||||||||||||||||||
1851 | } executed 800113 times by 2 tests: end of block Executed by:
| 800113 | ||||||||||||||||||
1852 | } executed 10401585 times by 2 tests: end of block Executed by:
| 10401585 | ||||||||||||||||||
1853 | } executed 800113 times by 2 tests: end of block Executed by:
| 800113 | ||||||||||||||||||
1854 | #endif | - | ||||||||||||||||||
1855 | #ifndef QT_NO_REGEXP_CAPTURE | - | ||||||||||||||||||
1856 | #ifndef QT_NO_REGEXP_BACKREF | - | ||||||||||||||||||
1857 | for (j = 0; j < eng->nbrefs; j++) {
| 42778-1415439 | ||||||||||||||||||
1858 | if ((a & (QRegExpEngine::Anchor_BackRef1Empty << j)) != 0) {
| 16411-26367 | ||||||||||||||||||
1859 | int i = eng->captureForOfficialCapture.at(j); | - | ||||||||||||||||||
1860 | if (capBegin[i] != EmptyCapture)
| 3503-22864 | ||||||||||||||||||
1861 | return false; executed 22864 times by 1 test: return false; Executed by:
| 22864 | ||||||||||||||||||
1862 | } executed 3503 times by 1 test: end of block Executed by:
| 3503 | ||||||||||||||||||
1863 | } executed 19914 times by 1 test: end of block Executed by:
| 19914 | ||||||||||||||||||
1864 | #endif | - | ||||||||||||||||||
1865 | #endif | - | ||||||||||||||||||
1866 | return true; executed 1415439 times by 27 tests: return true; Executed by:
| 1415439 | ||||||||||||||||||
1867 | } | - | ||||||||||||||||||
1868 | - | |||||||||||||||||||
1869 | #ifndef QT_NO_REGEXP_OPTIM | - | ||||||||||||||||||
1870 | /* | - | ||||||||||||||||||
1871 | The three following functions are what Jeffrey Friedl would call | - | ||||||||||||||||||
1872 | transmissions (or bump-alongs). Using one or the other should make | - | ||||||||||||||||||
1873 | no difference except in performance. | - | ||||||||||||||||||
1874 | */ | - | ||||||||||||||||||
1875 | - | |||||||||||||||||||
1876 | bool QRegExpEngine::goodStringMatch(QRegExpMatchState &matchState) const | - | ||||||||||||||||||
1877 | { | - | ||||||||||||||||||
1878 | int k = matchState.pos + goodEarlyStart; | - | ||||||||||||||||||
1879 | QStringMatcher matcher(goodStr.unicode(), goodStr.length(), cs); | - | ||||||||||||||||||
1880 | while ((k = matcher.indexIn(matchState.in, matchState.len, k)) != -1) {
| 528-200568 | ||||||||||||||||||
1881 | int from = k - goodLateStart; | - | ||||||||||||||||||
1882 | int to = k - goodEarlyStart; | - | ||||||||||||||||||
1883 | if (from > matchState.pos)
| 317-200251 | ||||||||||||||||||
1884 | matchState.pos = from; executed 200251 times by 10 tests: matchState.pos = from; Executed by:
| 200251 | ||||||||||||||||||
1885 | - | |||||||||||||||||||
1886 | while (matchState.pos <= to) {
| 22-198808 | ||||||||||||||||||
1887 | if (matchState.matchHere())
| 22-200546 | ||||||||||||||||||
1888 | return true; executed 200546 times by 14 tests: return true; Executed by:
| 200546 | ||||||||||||||||||
1889 | ++matchState.pos; | - | ||||||||||||||||||
1890 | } executed 22 times by 4 tests: end of block Executed by:
| 22 | ||||||||||||||||||
1891 | ++k; | - | ||||||||||||||||||
1892 | } executed 22 times by 4 tests: end of block Executed by:
| 22 | ||||||||||||||||||
1893 | return false; executed 528 times by 10 tests: return false; Executed by:
| 528 | ||||||||||||||||||
1894 | } | - | ||||||||||||||||||
1895 | - | |||||||||||||||||||
1896 | bool QRegExpEngine::badCharMatch(QRegExpMatchState &matchState) const | - | ||||||||||||||||||
1897 | { | - | ||||||||||||||||||
1898 | int slideHead = 0; | - | ||||||||||||||||||
1899 | int slideNext = 0; | - | ||||||||||||||||||
1900 | int i; | - | ||||||||||||||||||
1901 | int lastPos = matchState.len - minl; | - | ||||||||||||||||||
1902 | memset(matchState.slideTab, 0, matchState.slideTabSize * sizeof(int)); | - | ||||||||||||||||||
1903 | - | |||||||||||||||||||
1904 | /* | - | ||||||||||||||||||
1905 | Set up the slide table, used for the bad-character heuristic, | - | ||||||||||||||||||
1906 | using the table of first occurrence of each character. | - | ||||||||||||||||||
1907 | */ | - | ||||||||||||||||||
1908 | for (i = 0; i < minl; i++) {
| 32203-38044 | ||||||||||||||||||
1909 | int sk = occ1[BadChar(matchState.in[matchState.pos + i])]; | - | ||||||||||||||||||
1910 | if (sk == NoOccurrence)
| 14097-18106 | ||||||||||||||||||
1911 | sk = i + 1; executed 14097 times by 14 tests: sk = i + 1; Executed by:
| 14097 | ||||||||||||||||||
1912 | if (sk > 0) {
| 14525-17678 | ||||||||||||||||||
1913 | int k = i + 1 - sk; | - | ||||||||||||||||||
1914 | if (k < 0) {
| 18-14507 | ||||||||||||||||||
1915 | sk = i + 1; | - | ||||||||||||||||||
1916 | k = 0; | - | ||||||||||||||||||
1917 | } executed 18 times by 1 test: end of block Executed by:
| 18 | ||||||||||||||||||
1918 | if (sk > matchState.slideTab[k])
| 0-14525 | ||||||||||||||||||
1919 | matchState.slideTab[k] = sk; executed 14525 times by 14 tests: matchState.slideTab[k] = sk; Executed by:
| 14525 | ||||||||||||||||||
1920 | } executed 14525 times by 14 tests: end of block Executed by:
| 14525 | ||||||||||||||||||
1921 | } executed 32203 times by 26 tests: end of block Executed by:
| 32203 | ||||||||||||||||||
1922 | - | |||||||||||||||||||
1923 | if (matchState.pos > lastPos)
| 0-38044 | ||||||||||||||||||
1924 | return false; never executed: return false; | 0 | ||||||||||||||||||
1925 | - | |||||||||||||||||||
1926 | for (;;) { | - | ||||||||||||||||||
1927 | if (++slideNext >= matchState.slideTabSize)
| 10662-345462 | ||||||||||||||||||
1928 | slideNext = 0; executed 10662 times by 10 tests: slideNext = 0; Executed by:
| 10662 | ||||||||||||||||||
1929 | if (matchState.slideTab[slideHead] > 0) {
| 113544-242580 | ||||||||||||||||||
1930 | if (matchState.slideTab[slideHead] - 1 > matchState.slideTab[slideNext])
| 167-242413 | ||||||||||||||||||
1931 | matchState.slideTab[slideNext] = matchState.slideTab[slideHead] - 1; executed 167 times by 3 tests: matchState.slideTab[slideNext] = matchState.slideTab[slideHead] - 1; Executed by:
| 167 | ||||||||||||||||||
1932 | matchState.slideTab[slideHead] = 0; | - | ||||||||||||||||||
1933 | } else { executed 242580 times by 14 tests: end of block Executed by:
| 242580 | ||||||||||||||||||
1934 | if (matchState.matchHere())
|