Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qregularexpression.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count |
---|---|---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2015 Giuseppe D'Angelo <dangelog@gmail.com>. | - |
4 | ** Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | - |
5 | ** Copyright (C) 2016 The Qt Company Ltd. | - |
6 | ** Contact: https://www.qt.io/licensing/ | - |
7 | ** | - |
8 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
9 | ** | - |
10 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
11 | ** Commercial License Usage | - |
12 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
13 | ** accordance with the commercial license agreement provided with the | - |
14 | ** Software or, alternatively, in accordance with the terms contained in | - |
15 | ** a written agreement between you and The Qt Company. For licensing terms | - |
16 | ** and conditions see https://www.qt.io/terms-conditions. For further | - |
17 | ** information use the contact form at https://www.qt.io/contact-us. | - |
18 | ** | - |
19 | ** GNU Lesser General Public License Usage | - |
20 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
21 | ** General Public License version 3 as published by the Free Software | - |
22 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - |
23 | ** packaging of this file. Please review the following information to | - |
24 | ** ensure the GNU Lesser General Public License version 3 requirements | - |
25 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - |
26 | ** | - |
27 | ** GNU General Public License Usage | - |
28 | ** Alternatively, this file may be used under the terms of the GNU | - |
29 | ** General Public License version 2.0 or (at your option) the GNU General | - |
30 | ** Public license version 3 or any later version approved by the KDE Free | - |
31 | ** Qt Foundation. The licenses are as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - |
33 | ** included in the packaging of this file. Please review the following | - |
34 | ** information to ensure the GNU General Public License requirements will | - |
35 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - |
36 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | - | |
42 | #include "qregularexpression.h" | - |
43 | - | |
44 | #ifndef QT_NO_REGULAREXPRESSION | - |
45 | - | |
46 | #include <QtCore/qcoreapplication.h> | - |
47 | #include <QtCore/qhashfunctions.h> | - |
48 | #include <QtCore/qmutex.h> | - |
49 | #include <QtCore/qvector.h> | - |
50 | #include <QtCore/qstringlist.h> | - |
51 | #include <QtCore/qdebug.h> | - |
52 | #include <QtCore/qthreadstorage.h> | - |
53 | #include <QtCore/qglobal.h> | - |
54 | #include <QtCore/qatomic.h> | - |
55 | #include <QtCore/qdatastream.h> | - |
56 | - | |
57 | #include <pcre.h> | - |
58 | - | |
59 | QT_BEGIN_NAMESPACE | - |
60 | - | |
61 | /*! | - |
62 | \class QRegularExpression | - |
63 | \inmodule QtCore | - |
64 | \reentrant | - |
65 | - | |
66 | \brief The QRegularExpression class provides pattern matching using regular | - |
67 | expressions. | - |
68 | - | |
69 | \since 5.0 | - |
70 | - | |
71 | \ingroup tools | - |
72 | \ingroup shared | - |
73 | - | |
74 | \keyword regular expression | - |
75 | - | |
76 | Regular expressions, or \e{regexps}, are a very powerful tool to handle | - |
77 | strings and texts. This is useful in many contexts, e.g., | - |
78 | - | |
79 | \table | - |
80 | \row \li Validation | - |
81 | \li A regexp can test whether a substring meets some criteria, | - |
82 | e.g. is an integer or contains no whitespace. | - |
83 | \row \li Searching | - |
84 | \li A regexp provides more powerful pattern matching than | - |
85 | simple substring matching, e.g., match one of the words | - |
86 | \e{mail}, \e{letter} or \e{correspondence}, but none of the | - |
87 | words \e{email}, \e{mailman}, \e{mailer}, \e{letterbox}, etc. | - |
88 | \row \li Search and Replace | - |
89 | \li A regexp can replace all occurrences of a substring with a | - |
90 | different substring, e.g., replace all occurrences of \e{&} | - |
91 | with \e{\&} except where the \e{&} is already followed by | - |
92 | an \e{amp;}. | - |
93 | \row \li String Splitting | - |
94 | \li A regexp can be used to identify where a string should be | - |
95 | split apart, e.g. splitting tab-delimited strings. | - |
96 | \endtable | - |
97 | - | |
98 | This document is by no means a complete reference to pattern matching using | - |
99 | regular expressions, and the following parts will require the reader to | - |
100 | have some basic knowledge about Perl-like regular expressions and their | - |
101 | pattern syntax. | - |
102 | - | |
103 | Good references about regular expressions include: | - |
104 | - | |
105 | \list | - |
106 | \li \e {Mastering Regular Expressions} (Third Edition) by Jeffrey E. F. | - |
107 | Friedl, ISBN 0-596-52812-4; | - |
108 | \li the \l{http://pcre.org/pcre.txt} {pcrepattern(3)} man page, describing | - |
109 | the pattern syntax supported by PCRE (the reference implementation of | - |
110 | Perl-compatible regular expressions); | - |
111 | \li the \l{http://perldoc.perl.org/perlre.html} {Perl's regular expression | - |
112 | documentation} and the \l{http://perldoc.perl.org/perlretut.html} {Perl's | - |
113 | regular expression tutorial}. | - |
114 | \endlist | - |
115 | - | |
116 | \tableofcontents | - |
117 | - | |
118 | \section1 Introduction | - |
119 | - | |
120 | QRegularExpression implements Perl-compatible regular expressions. It fully | - |
121 | supports Unicode. For an overview of the regular expression syntax | - |
122 | supported by QRegularExpression, please refer to the aforementioned | - |
123 | pcrepattern(3) man page. A regular expression is made up of two things: a | - |
124 | \b{pattern string} and a set of \b{pattern options} that change the | - |
125 | meaning of the pattern string. | - |
126 | - | |
127 | You can set the pattern string by passing a string to the QRegularExpression | - |
128 | constructor: | - |
129 | - | |
130 | \snippet code/src_corelib_tools_qregularexpression.cpp 0 | - |
131 | - | |
132 | This sets the pattern string to \c{a pattern}. You can also use the | - |
133 | setPattern() function to set a pattern on an existing QRegularExpression | - |
134 | object: | - |
135 | - | |
136 | \snippet code/src_corelib_tools_qregularexpression.cpp 1 | - |
137 | - | |
138 | Note that due to C++ literal strings rules, you must escape all backslashes | - |
139 | inside the pattern string with another backslash: | - |
140 | - | |
141 | \snippet code/src_corelib_tools_qregularexpression.cpp 2 | - |
142 | - | |
143 | The pattern() function returns the pattern that is currently set for a | - |
144 | QRegularExpression object: | - |
145 | - | |
146 | \snippet code/src_corelib_tools_qregularexpression.cpp 3 | - |
147 | - | |
148 | \section1 Pattern Options | - |
149 | - | |
150 | The meaning of the pattern string can be modified by setting one or more | - |
151 | \e{pattern options}. For instance, it is possible to set a pattern to match | - |
152 | case insensitively by setting the QRegularExpression::CaseInsensitiveOption. | - |
153 | - | |
154 | You can set the options by passing them to the QRegularExpression | - |
155 | constructor, as in: | - |
156 | - | |
157 | \snippet code/src_corelib_tools_qregularexpression.cpp 4 | - |
158 | - | |
159 | Alternatively, you can use the setPatternOptions() function on an existing | - |
160 | QRegularExpressionObject: | - |
161 | - | |
162 | \snippet code/src_corelib_tools_qregularexpression.cpp 5 | - |
163 | - | |
164 | It is possible to get the pattern options currently set on a | - |
165 | QRegularExpression object by using the patternOptions() function: | - |
166 | - | |
167 | \snippet code/src_corelib_tools_qregularexpression.cpp 6 | - |
168 | - | |
169 | Please refer to the QRegularExpression::PatternOption enum documentation for | - |
170 | more information about each pattern option. | - |
171 | - | |
172 | \section1 Match Type and Match Options | - |
173 | - | |
174 | The last two arguments of the match() and the globalMatch() functions set | - |
175 | the match type and the match options. The match type is a value of the | - |
176 | QRegularExpression::MatchType enum; the "traditional" matching algorithm is | - |
177 | chosen by using the NormalMatch match type (the default). It is also | - |
178 | possible to enable partial matching of the regular expression against a | - |
179 | subject string: see the \l{partial matching} section for more details. | - |
180 | - | |
181 | The match options are a set of one or more QRegularExpression::MatchOption | - |
182 | values. They change the way a specific match of a regular expression | - |
183 | against a subject string is done. Please refer to the | - |
184 | QRegularExpression::MatchOption enum documentation for more details. | - |
185 | - | |
186 | \target normal matching | - |
187 | \section1 Normal Matching | - |
188 | - | |
189 | In order to perform a match you can simply invoke the match() function | - |
190 | passing a string to match against. We refer to this string as the | - |
191 | \e{subject string}. The result of the match() function is a | - |
192 | QRegularExpressionMatch object that can be used to inspect the results of | - |
193 | the match. For instance: | - |
194 | - | |
195 | \snippet code/src_corelib_tools_qregularexpression.cpp 7 | - |
196 | - | |
197 | If a match is successful, the (implicit) capturing group number 0 can be | - |
198 | used to retrieve the substring matched by the entire pattern (see also the | - |
199 | section about \l{extracting captured substrings}): | - |
200 | - | |
201 | \snippet code/src_corelib_tools_qregularexpression.cpp 8 | - |
202 | - | |
203 | It's also possible to start a match at an arbitrary offset inside the | - |
204 | subject string by passing the offset as an argument of the | - |
205 | match() function. In the following example \c{"12 abc"} | - |
206 | is not matched because the match is started at offset 1: | - |
207 | - | |
208 | \snippet code/src_corelib_tools_qregularexpression.cpp 9 | - |
209 | - | |
210 | \target extracting captured substrings | - |
211 | \section2 Extracting captured substrings | - |
212 | - | |
213 | The QRegularExpressionMatch object contains also information about the | - |
214 | substrings captured by the capturing groups in the pattern string. The | - |
215 | \l{QRegularExpressionMatch::}{captured()} function will return the string | - |
216 | captured by the n-th capturing group: | - |
217 | - | |
218 | \snippet code/src_corelib_tools_qregularexpression.cpp 10 | - |
219 | - | |
220 | Capturing groups in the pattern are numbered starting from 1, and the | - |
221 | implicit capturing group 0 is used to capture the substring that matched | - |
222 | the entire pattern. | - |
223 | - | |
224 | It's also possible to retrieve the starting and the ending offsets (inside | - |
225 | the subject string) of each captured substring, by using the | - |
226 | \l{QRegularExpressionMatch::}{capturedStart()} and the | - |
227 | \l{QRegularExpressionMatch::}{capturedEnd()} functions: | - |
228 | - | |
229 | \snippet code/src_corelib_tools_qregularexpression.cpp 11 | - |
230 | - | |
231 | All of these functions have an overload taking a QString as a parameter | - |
232 | in order to extract \e{named} captured substrings. For instance: | - |
233 | - | |
234 | \snippet code/src_corelib_tools_qregularexpression.cpp 12 | - |
235 | - | |
236 | \target global matching | - |
237 | \section1 Global Matching | - |
238 | - | |
239 | \e{Global matching} is useful to find all the occurrences of a given | - |
240 | regular expression inside a subject string. Suppose that we want to extract | - |
241 | all the words from a given string, where a word is a substring matching | - |
242 | the pattern \c{\w+}. | - |
243 | - | |
244 | QRegularExpression::globalMatch returns a QRegularExpressionMatchIterator, | - |
245 | which is a Java-like forward iterator that can be used to iterate over the | - |
246 | results. For instance: | - |
247 | - | |
248 | \snippet code/src_corelib_tools_qregularexpression.cpp 13 | - |
249 | - | |
250 | Since it's a Java-like iterator, the QRegularExpressionMatchIterator will | - |
251 | point immediately before the first result. Every result is returned as a | - |
252 | QRegularExpressionMatch object. The | - |
253 | \l{QRegularExpressionMatchIterator::}{hasNext()} function will return true | - |
254 | if there's at least one more result, and | - |
255 | \l{QRegularExpressionMatchIterator::}{next()} will return the next result | - |
256 | and advance the iterator. Continuing from the previous example: | - |
257 | - | |
258 | \snippet code/src_corelib_tools_qregularexpression.cpp 14 | - |
259 | - | |
260 | You can also use \l{QRegularExpressionMatchIterator::}{peekNext()} to get | - |
261 | the next result without advancing the iterator. | - |
262 | - | |
263 | It is possible to pass a starting offset and one or more match options to | - |
264 | the globalMatch() function, exactly like normal matching with match(). | - |
265 | - | |
266 | \target partial matching | - |
267 | \section1 Partial Matching | - |
268 | - | |
269 | A \e{partial match} is obtained when the end of the subject string is | - |
270 | reached, but more characters are needed to successfully complete the match. | - |
271 | Note that a partial match is usually much more inefficient than a normal | - |
272 | match because many optimizations of the matching algorithm cannot be | - |
273 | employed. | - |
274 | - | |
275 | A partial match must be explicitly requested by specifying a match type of | - |
276 | PartialPreferCompleteMatch or PartialPreferFirstMatch when calling | - |
277 | QRegularExpression::match or QRegularExpression::globalMatch. If a partial | - |
278 | match is found, then calling the \l{QRegularExpressionMatch::}{hasMatch()} | - |
279 | function on the QRegularExpressionMatch object returned by match() will | - |
280 | return \c{false}, but \l{QRegularExpressionMatch::}{hasPartialMatch()} will return | - |
281 | \c{true}. | - |
282 | - | |
283 | When a partial match is found, no captured substrings are returned, and the | - |
284 | (implicit) capturing group 0 corresponding to the whole match captures the | - |
285 | partially matched substring of the subject string. | - |
286 | - | |
287 | Note that asking for a partial match can still lead to a complete match, if | - |
288 | one is found; in this case, \l{QRegularExpressionMatch::}{hasMatch()} will | - |
289 | return \c{true} and \l{QRegularExpressionMatch::}{hasPartialMatch()} | - |
290 | \c{false}. It never happens that a QRegularExpressionMatch reports both a | - |
291 | partial and a complete match. | - |
292 | - | |
293 | Partial matching is mainly useful in two scenarios: validating user input | - |
294 | in real time and incremental/multi-segment matching. | - |
295 | - | |
296 | \target validating user input | - |
297 | \section2 Validating user input | - |
298 | - | |
299 | Suppose that we would like the user to input a date in a specific | - |
300 | format, for instance "MMM dd, yyyy". We can check the input validity with | - |
301 | a pattern like: | - |
302 | - | |
303 | \c{^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d\d?, \d\d\d\d$} | - |
304 | - | |
305 | (This pattern doesn't catch invalid days, but let's keep it for the | - |
306 | example's purposes). | - |
307 | - | |
308 | We would like to validate the input with this regular expression \e{while} | - |
309 | the user is typing it, so that we can report an error in the input as soon | - |
310 | as it is committed (for instance, the user typed the wrong key). In order | - |
311 | to do so we must distinguish three cases: | - |
312 | - | |
313 | \list | - |
314 | \li the input cannot possibly match the regular expression; | - |
315 | \li the input does match the regular expression; | - |
316 | \li the input does not match the regular expression right now, | - |
317 | but it will if more characters will be added to it. | - |
318 | \endlist | - |
319 | - | |
320 | Note that these three cases represent exactly the possible states of a | - |
321 | QValidator (see the QValidator::State enum). | - |
322 | - | |
323 | In particular, in the last case we want the regular expression engine to | - |
324 | report a partial match: we are successfully matching the pattern against | - |
325 | the subject string but the matching cannot continue because the end of the | - |
326 | subject is encountered. Notice, however, that the matching algorithm should | - |
327 | continue and try all possibilities, and in case a complete (non-partial) | - |
328 | match is found, then this one should be reported, and the input string | - |
329 | accepted as fully valid. | - |
330 | - | |
331 | This behaviour is implemented by the PartialPreferCompleteMatch match type. | - |
332 | For instance: | - |
333 | - | |
334 | \snippet code/src_corelib_tools_qregularexpression.cpp 15 | - |
335 | - | |
336 | If matching the same regular expression against the subject string leads to | - |
337 | a complete match, it is reported as usual: | - |
338 | - | |
339 | \snippet code/src_corelib_tools_qregularexpression.cpp 16 | - |
340 | - | |
341 | Another example with a different pattern, showing the behaviour of | - |
342 | preferring a complete match over a partial one: | - |
343 | - | |
344 | \snippet code/src_corelib_tools_qregularexpression.cpp 17 | - |
345 | - | |
346 | In this case, the subpattern \c{abc\\w+X} partially matches the subject | - |
347 | string; however, the subpattern \c{def} matches the subject string | - |
348 | completely, and therefore a complete match is reported. | - |
349 | - | |
350 | If multiple partial matches are found when matching (but no complete | - |
351 | match), then the QRegularExpressionMatch object will report the first one | - |
352 | that is found. For instance: | - |
353 | - | |
354 | \snippet code/src_corelib_tools_qregularexpression.cpp 18 | - |
355 | - | |
356 | \section2 Incremental/multi-segment matching | - |
357 | - | |
358 | Incremental matching is another use case of partial matching. Suppose that | - |
359 | we want to find the occurrences of a regular expression inside a large text | - |
360 | (that is, substrings matching the regular expression). In order to do so we | - |
361 | would like to "feed" the large text to the regular expression engines in | - |
362 | smaller chunks. The obvious problem is what happens if the substring that | - |
363 | matches the regular expression spans across two or more chunks. | - |
364 | - | |
365 | In this case, the regular expression engine should report a partial match, | - |
366 | so that we can match again adding new data and (eventually) get a complete | - |
367 | match. This implies that the regular expression engine may assume that | - |
368 | there are other characters \e{beyond the end} of the subject string. This | - |
369 | is not to be taken literally -- the engine will never try to access | - |
370 | any character after the last one in the subject. | - |
371 | - | |
372 | QRegularExpression implements this behaviour when using the | - |
373 | PartialPreferFirstMatch match type. This match type reports a partial match | - |
374 | as soon as it is found, and other match alternatives are not tried | - |
375 | (even if they could lead to a complete match). For instance: | - |
376 | - | |
377 | \snippet code/src_corelib_tools_qregularexpression.cpp 19 | - |
378 | - | |
379 | This happens because when matching the first branch of the alternation | - |
380 | operator a partial match is found, and therefore matching stops, without | - |
381 | trying the second branch. Another example: | - |
382 | - | |
383 | \snippet code/src_corelib_tools_qregularexpression.cpp 20 | - |
384 | - | |
385 | This shows what could seem a counterintuitve behaviour of quantifiers: | - |
386 | since \c{?} is greedy, then the engine tries first to continue the match | - |
387 | after having matched \c{"abc"}; but then the matching reaches the end of the | - |
388 | subject string, and therefore a partial match is reported. This is | - |
389 | even more surprising in the following example: | - |
390 | - | |
391 | \snippet code/src_corelib_tools_qregularexpression.cpp 21 | - |
392 | - | |
393 | It's easy to understand this behaviour if we remember that the engine | - |
394 | expects the subject string to be only a substring of the whole text we're | - |
395 | looking for a match into (that is, how we said before, that the engine | - |
396 | assumes that there are other characters beyond the end of the subject | - |
397 | string). | - |
398 | - | |
399 | Since the \c{*} quantifier is greedy, then reporting a complete match could | - |
400 | be an error, because after the current subject \c{"abc"} there may be other | - |
401 | occurrences of \c{"abc"}. For instance, the complete text could have been | - |
402 | "abcabcX", and therefore the \e{right} match to report (in the complete | - |
403 | text) would have been \c{"abcabc"}; by matching only against the leading | - |
404 | \c{"abc"} we instead get a partial match. | - |
405 | - | |
406 | \section1 Error Handling | - |
407 | - | |
408 | It is possible for a QRegularExpression object to be invalid because of | - |
409 | syntax errors in the pattern string. The isValid() function will return | - |
410 | true if the regular expression is valid, or false otherwise: | - |
411 | - | |
412 | \snippet code/src_corelib_tools_qregularexpression.cpp 22 | - |
413 | - | |
414 | You can get more information about the specific error by calling the | - |
415 | errorString() function; moreover, the patternErrorOffset() function | - |
416 | will return the offset inside the pattern string | - |
417 | - | |
418 | \snippet code/src_corelib_tools_qregularexpression.cpp 23 | - |
419 | - | |
420 | If a match is attempted with an invalid QRegularExpression, then the | - |
421 | returned QRegularExpressionMatch object will be invalid as well (that is, | - |
422 | its \l{QRegularExpressionMatch::}{isValid()} function will return false). | - |
423 | The same applies for attempting a global match. | - |
424 | - | |
425 | \section1 Unsupported Perl-compatible Regular Expressions Features | - |
426 | - | |
427 | QRegularExpression does not support all the features available in | - |
428 | Perl-compatible regular expressions. The most notable one is the fact that | - |
429 | duplicated names for capturing groups are not supported, and using them can | - |
430 | lead to undefined behaviour. | - |
431 | - | |
432 | This may change in a future version of Qt. | - |
433 | - | |
434 | \section1 Notes for QRegExp Users | - |
435 | - | |
436 | The QRegularExpression class introduced in Qt 5 is a big improvement upon | - |
437 | QRegExp, in terms of APIs offered, supported pattern syntax and speed of | - |
438 | execution. The biggest difference is that QRegularExpression simply holds a | - |
439 | regular expression, and it's \e{not} modified when a match is requested. | - |
440 | Instead, a QRegularExpressionMatch object is returned, in order to check | - |
441 | the result of a match and extract the captured substring. The same applies | - |
442 | with global matching and QRegularExpressionMatchIterator. | - |
443 | - | |
444 | Other differences are outlined below. | - |
445 | - | |
446 | \section2 Exact matching | - |
447 | - | |
448 | QRegExp::exactMatch() in Qt 4 served two purposes: it exactly matched | - |
449 | a regular expression against a subject string, and it implemented partial | - |
450 | matching. In fact, if an exact match was not found, one could still find | - |
451 | out how much of the subject string was matched by the regular expression | - |
452 | by calling QRegExp::matchedLength(). If the returned length was equal | - |
453 | to the subject string's length, then one could desume that a partial match | - |
454 | was found. | - |
455 | - | |
456 | QRegularExpression supports partial matching explicitly by means of the | - |
457 | appropriate MatchType. If instead you simply want to be sure that the | - |
458 | subject string matches the regular expression exactly, you can wrap the | - |
459 | pattern between a couple of anchoring expressions. Simply | - |
460 | putting the pattern between the \c{^} and the \c{$} anchors is enough | - |
461 | in most cases: | - |
462 | - | |
463 | \snippet code/src_corelib_tools_qregularexpression.cpp 24 | - |
464 | - | |
465 | However, remember that the \c{$} anchor not only matches at the end of the | - |
466 | string, but also at a newline character right before the end of the string; | - |
467 | that is, the previous pattern matches against the string "this pattern must | - |
468 | match exactly\\n". Also, the behaviour of both the \c{^} and the \c{$} | - |
469 | anchors changes if the MultiLineOption is set either explicitly (as a | - |
470 | pattern option) or implicitly (as a directive inside the pattern string). | - |
471 | - | |
472 | Therefore, in the most general case, you should wrap the pattern between | - |
473 | the \c{\A} and the \c{\z} anchors: | - |
474 | - | |
475 | \snippet code/src_corelib_tools_qregularexpression.cpp 25 | - |
476 | - | |
477 | Note the usage of the non-capturing group in order to preserve the meaning | - |
478 | of the branch operator inside the pattern. | - |
479 | - | |
480 | \section2 Global matching | - |
481 | - | |
482 | Due to limitations of the QRegExp API it was impossible to implement global | - |
483 | matching correctly (that is, like Perl does). In particular, patterns that | - |
484 | can match 0 characters (like \c{"a*"}) are problematic. | - |
485 | - | |
486 | QRegularExpression::globalMatch() implements Perl global match correctly, and | - |
487 | the returned iterator can be used to examine each result. | - |
488 | - | |
489 | \section2 Unicode properties support | - |
490 | - | |
491 | When using QRegExp, character classes such as \c{\w}, \c{\d}, etc. match | - |
492 | characters with the corresponding Unicode property: for instance, \c{\d} | - |
493 | matches any character with the Unicode Nd (decimal digit) property. | - |
494 | - | |
495 | Those character classes only match ASCII characters by default when using | - |
496 | QRegularExpression: for instance, \c{\d} matches exactly a character in the | - |
497 | \c{0-9} ASCII range. It is possible to change this behaviour by using the | - |
498 | UseUnicodePropertiesOption pattern option. | - |
499 | - | |
500 | \section2 Wildcard matching | - |
501 | - | |
502 | There is no equivalent of wildcard matching in QRegularExpression. | - |
503 | Nevertheless, rewriting a regular expression in wildcard syntax to a | - |
504 | Perl-compatible regular expression is a very easy task, given the fact | - |
505 | that wildcard syntax supported by QRegExp is very simple. | - |
506 | - | |
507 | \section2 Other pattern syntaxes | - |
508 | - | |
509 | QRegularExpression supports only Perl-compatible regular expressions. | - |
510 | - | |
511 | \section2 Minimal matching | - |
512 | - | |
513 | QRegExp::setMinimal() implemented minimal matching by simply reversing the | - |
514 | greediness of the quantifiers (QRegExp did not support lazy quantifiers, | - |
515 | like \c{*?}, \c{+?}, etc.). QRegularExpression instead does support greedy, | - |
516 | lazy and possessive quantifiers. The InvertedGreedinessOption | - |
517 | pattern option can be useful to emulate the effects of QRegExp::setMinimal(): | - |
518 | if enabled, it inverts the greediness of quantifiers (greedy ones become | - |
519 | lazy and vice versa). | - |
520 | - | |
521 | \section2 Caret modes | - |
522 | - | |
523 | The AnchoredMatchOption match option can be used to emulate the | - |
524 | QRegExp::CaretAtOffset behaviour. There is no equivalent for the other | - |
525 | QRegExp::CaretMode modes. | - |
526 | - | |
527 | \section1 Debugging Code that Uses QRegularExpression | - |
528 | - | |
529 | QRegularExpression internally uses a just in time compiler (JIT) to | - |
530 | optimize the execution of the matching algorithm. The JIT makes extensive | - |
531 | usage of self-modifying code, which can lead debugging tools such as | - |
532 | Valgrind to crash. You must enable all checks for self-modifying code if | - |
533 | you want to debug programs using QRegularExpression (f.i., see Valgrind's | - |
534 | \c{--smc-check} command line option). The downside of enabling such checks | - |
535 | is that your program will run considerably slower. | - |
536 | - | |
537 | To avoid that, the JIT is disabled by default if you compile Qt in debug | - |
538 | mode. It is possible to override the default and enable or disable the JIT | - |
539 | usage (both in debug or release mode) by setting the | - |
540 | \c{QT_ENABLE_REGEXP_JIT} environment variable to a non-zero or zero value | - |
541 | respectively. | - |
542 | - | |
543 | \sa QRegularExpressionMatch, QRegularExpressionMatchIterator | - |
544 | */ | - |
545 | - | |
546 | /*! | - |
547 | \class QRegularExpressionMatch | - |
548 | \inmodule QtCore | - |
549 | \reentrant | - |
550 | - | |
551 | \brief The QRegularExpressionMatch class provides the results of matching | - |
552 | a QRegularExpression against a string. | - |
553 | - | |
554 | \since 5.0 | - |
555 | - | |
556 | \ingroup tools | - |
557 | \ingroup shared | - |
558 | - | |
559 | \keyword regular expression match | - |
560 | - | |
561 | A QRegularExpressionMatch object can be obtained by calling the | - |
562 | QRegularExpression::match() function, or as a single result of a global | - |
563 | match from a QRegularExpressionMatchIterator. | - |
564 | - | |
565 | The success or the failure of a match attempt can be inspected by calling | - |
566 | the hasMatch() function. QRegularExpressionMatch also reports a successful | - |
567 | partial match through the hasPartialMatch() function. | - |
568 | - | |
569 | In addition, QRegularExpressionMatch returns the substrings captured by the | - |
570 | capturing groups in the pattern string. The implicit capturing group with | - |
571 | index 0 captures the result of the whole match. The captured() function | - |
572 | returns each substring captured, either by the capturing group's index or | - |
573 | by its name: | - |
574 | - | |
575 | \snippet code/src_corelib_tools_qregularexpression.cpp 29 | - |
576 | - | |
577 | For each captured substring it is possible to query its starting and ending | - |
578 | offsets in the subject string by calling the capturedStart() and the | - |
579 | capturedEnd() function, respectively. The length of each captured | - |
580 | substring is available using the capturedLength() function. | - |
581 | - | |
582 | The convenience function capturedTexts() will return \e{all} the captured | - |
583 | substrings at once (including the substring matched by the entire pattern) | - |
584 | in the order they have been captured by captring groups; that is, | - |
585 | \c{captured(i) == capturedTexts().at(i)}. | - |
586 | - | |
587 | You can retrieve the QRegularExpression object the subject string was | - |
588 | matched against by calling the regularExpression() function; the | - |
589 | match type and the match options are available as well by calling | - |
590 | the matchType() and the matchOptions() respectively. | - |
591 | - | |
592 | Please refer to the QRegularExpression documentation for more information | - |
593 | about the Qt regular expression classes. | - |
594 | - | |
595 | \sa QRegularExpression | - |
596 | */ | - |
597 | - | |
598 | /*! | - |
599 | \class QRegularExpressionMatchIterator | - |
600 | \inmodule QtCore | - |
601 | \reentrant | - |
602 | - | |
603 | \brief The QRegularExpressionMatchIterator class provides an iterator on | - |
604 | the results of a global match of a QRegularExpression object against a string. | - |
605 | - | |
606 | \since 5.0 | - |
607 | - | |
608 | \ingroup tools | - |
609 | \ingroup shared | - |
610 | - | |
611 | \keyword regular expression iterator | - |
612 | - | |
613 | A QRegularExpressionMatchIterator object is a forward only Java-like | - |
614 | iterator; it can be obtained by calling the | - |
615 | QRegularExpression::globalMatch() function. A new | - |
616 | QRegularExpressionMatchIterator will be positioned before the first result. | - |
617 | You can then call the hasNext() function to check if there are more | - |
618 | results available; if so, the next() function will return the next | - |
619 | result and advance the iterator. | - |
620 | - | |
621 | Each result is a QRegularExpressionMatch object holding all the information | - |
622 | for that result (including captured substrings). | - |
623 | - | |
624 | For instance: | - |
625 | - | |
626 | \snippet code/src_corelib_tools_qregularexpression.cpp 30 | - |
627 | - | |
628 | Moreover, QRegularExpressionMatchIterator offers a peekNext() function | - |
629 | to get the next result \e{without} advancing the iterator. | - |
630 | - | |
631 | You can retrieve the QRegularExpression object the subject string was | - |
632 | matched against by calling the regularExpression() function; the | - |
633 | match type and the match options are available as well by calling | - |
634 | the matchType() and the matchOptions() respectively. | - |
635 | - | |
636 | Please refer to the QRegularExpression documentation for more information | - |
637 | about the Qt regular expression classes. | - |
638 | - | |
639 | \sa QRegularExpression, QRegularExpressionMatch | - |
640 | */ | - |
641 | - | |
642 | - | |
643 | /*! | - |
644 | \enum QRegularExpression::PatternOption | - |
645 | - | |
646 | The PatternOption enum defines modifiers to the way the pattern string | - |
647 | should be interpreted, and therefore the way the pattern matches against a | - |
648 | subject string. | - |
649 | - | |
650 | \value NoPatternOption | - |
651 | No pattern options are set. | - |
652 | - | |
653 | \value CaseInsensitiveOption | - |
654 | The pattern should match against the subject string in a case | - |
655 | insensitive way. This option corresponds to the /i modifier in Perl | - |
656 | regular expressions. | - |
657 | - | |
658 | \value DotMatchesEverythingOption | - |
659 | The dot metacharacter (\c{.}) in the pattern string is allowed to match | - |
660 | any character in the subject string, including newlines (normally, the | - |
661 | dot does not match newlines). This option corresponds to the \c{/s} | - |
662 | modifier in Perl regular expressions. | - |
663 | - | |
664 | \value MultilineOption | - |
665 | The caret (\c{^}) and the dollar (\c{$}) metacharacters in the pattern | - |
666 | string are allowed to match, respectively, immediately after and | - |
667 | immediately before any newline in the subject string, as well as at the | - |
668 | very beginning and at the very end of the subject string. This option | - |
669 | corresponds to the \c{/m} modifier in Perl regular expressions. | - |
670 | - | |
671 | \value ExtendedPatternSyntaxOption | - |
672 | Any whitespace in the pattern string which is not escaped and outside a | - |
673 | character class is ignored. Moreover, an unescaped sharp (\b{#}) | - |
674 | outside a character class causes all the following characters, until | - |
675 | the first newline (included), to be ignored. This can be used to | - |
676 | increase the readability of a pattern string as well as put comments | - |
677 | inside regular expressions; this is particulary useful if the pattern | - |
678 | string is loaded from a file or written by the user, because in C++ | - |
679 | code it is always possible to use the rules for string literals to put | - |
680 | comments outside the pattern string. This option corresponds to the \c{/x} | - |
681 | modifier in Perl regular expressions. | - |
682 | - | |
683 | \value InvertedGreedinessOption | - |
684 | The greediness of the quantifiers is inverted: \c{*}, \c{+}, \c{?}, | - |
685 | \c{{m,n}}, etc. become lazy, while their lazy versions (\c{*?}, | - |
686 | \c{+?}, \c{??}, \c{{m,n}?}, etc.) become greedy. There is no equivalent | - |
687 | for this option in Perl regular expressions. | - |
688 | - | |
689 | \value DontCaptureOption | - |
690 | The non-named capturing groups do not capture substrings; named | - |
691 | capturing groups still work as intended, as well as the implicit | - |
692 | capturing group number 0 corresponding to the entire match. There is no | - |
693 | equivalent for this option in Perl regular expressions. | - |
694 | - | |
695 | \value UseUnicodePropertiesOption | - |
696 | The meaning of the \c{\w}, \c{\d}, etc., character classes, as well as | - |
697 | the meaning of their counterparts (\c{\W}, \c{\D}, etc.), is changed | - |
698 | from matching ASCII characters only to matching any character with the | - |
699 | corresponding Unicode property. For instance, \c{\d} is changed to | - |
700 | match any character with the Unicode Nd (decimal digit) property; | - |
701 | \c{\w} to match any character with either the Unicode L (letter) or N | - |
702 | (digit) property, plus underscore, and so on. This option corresponds | - |
703 | to the \c{/u} modifier in Perl regular expressions. | - |
704 | - | |
705 | \value OptimizeOnFirstUsageOption | - |
706 | The regular expression will be optimized (and possibly | - |
707 | JIT-compiled) on its first usage, instead of after a certain (undefined) | - |
708 | number of usages. See also \l{QRegularExpression::}{optimize()}. | - |
709 | This enum value has been introduced in Qt 5.4. | - |
710 | - | |
711 | \value DontAutomaticallyOptimizeOption | - |
712 | Regular expressions are automatically optimized after a | - |
713 | certain number of usages; setting this option prevents such | - |
714 | optimizations, therefore avoiding possible unpredictable spikes in | - |
715 | CPU and memory usage. If both this option and the | - |
716 | \c{OptimizeOnFirstUsageOption} option are set, then this option takes | - |
717 | precedence. Note: this option will still let the regular expression | - |
718 | to be optimized by manually calling | - |
719 | \l{QRegularExpression::}{optimize()}. This enum value has been | - |
720 | introduced in Qt 5.4. | - |
721 | */ | - |
722 | - | |
723 | /*! | - |
724 | \enum QRegularExpression::MatchType | - |
725 | - | |
726 | The MatchType enum defines the type of the match that should be attempted | - |
727 | against the subject string. | - |
728 | - | |
729 | \value NormalMatch | - |
730 | A normal match is done. | - |
731 | - | |
732 | \value PartialPreferCompleteMatch | - |
733 | The pattern string is matched partially against the subject string. If | - |
734 | a partial match is found, then it is recorded, and other matching | - |
735 | alternatives are tried as usual. If a complete match is then found, | - |
736 | then it's preferred to the partial match; in this case only the | - |
737 | complete match is reported. If instead no complete match is found (but | - |
738 | only the partial one), then the partial one is reported. | - |
739 | - | |
740 | \value PartialPreferFirstMatch | - |
741 | The pattern string is matched partially against the subject string. If | - |
742 | a partial match is found, then matching stops and the partial match is | - |
743 | reported. In this case, other matching alternatives (potentially | - |
744 | leading to a complete match) are not tried. Moreover, this match type | - |
745 | assumes that the subject string only a substring of a larger text, and | - |
746 | that (in this text) there are other characters beyond the end of the | - |
747 | subject string. This can lead to surprising results; see the discussion | - |
748 | in the \l{partial matching} section for more details. | - |
749 | - | |
750 | \value NoMatch | - |
751 | No matching is done. This value is returned as the match type by a | - |
752 | default constructed QRegularExpressionMatch or | - |
753 | QRegularExpressionMatchIterator. Using this match type is not very | - |
754 | useful for the user, as no matching ever happens. This enum value | - |
755 | has been introduced in Qt 5.1. | - |
756 | */ | - |
757 | - | |
758 | /*! | - |
759 | \enum QRegularExpression::MatchOption | - |
760 | - | |
761 | \value NoMatchOption | - |
762 | No match options are set. | - |
763 | - | |
764 | \value AnchoredMatchOption | - |
765 | The match is constrained to start exactly at the offset passed to | - |
766 | match() in order to be successful, even if the pattern string does not | - |
767 | contain any metacharacter that anchors the match at that point. | - |
768 | - | |
769 | \value DontCheckSubjectStringMatchOption | - |
770 | The subject string is not checked for UTF-16 validity before | - |
771 | attempting a match. Use this option with extreme caution, as | - |
772 | attempting to match an invalid string may crash the program and/or | - |
773 | constitute a security issue. This enum value has been introduced in | - |
774 | Qt 5.4. | - |
775 | */ | - |
776 | - | |
777 | // after how many usages we optimize the regexp | - |
778 | #ifdef QT_BUILD_INTERNAL | - |
779 | Q_AUTOTEST_EXPORT unsigned int qt_qregularexpression_optimize_after_use_count = 10; | - |
780 | #else | - |
781 | static const unsigned int qt_qregularexpression_optimize_after_use_count = 10; | - |
782 | #endif // QT_BUILD_INTERNAL | - |
783 | - | |
784 | /*! | - |
785 | \internal | - |
786 | */ | - |
787 | static int convertToPcreOptions(QRegularExpression::PatternOptions patternOptions) | - |
788 | { | - |
789 | int options = 0; | - |
790 | - | |
791 | if (patternOptions & QRegularExpression::CaseInsensitiveOption) | - |
792 | options |= PCRE_CASELESS; | - |
793 | if (patternOptions & QRegularExpression::DotMatchesEverythingOption) | - |
794 | options |= PCRE_DOTALL; | - |
795 | if (patternOptions & QRegularExpression::MultilineOption) | - |
796 | options |= PCRE_MULTILINE; | - |
797 | if (patternOptions & QRegularExpression::ExtendedPatternSyntaxOption) | - |
798 | options |= PCRE_EXTENDED; | - |
799 | if (patternOptions & QRegularExpression::InvertedGreedinessOption) | - |
800 | options |= PCRE_UNGREEDY; | - |
801 | if (patternOptions & QRegularExpression::DontCaptureOption) | - |
802 | options |= PCRE_NO_AUTO_CAPTURE; | - |
803 | if (patternOptions & QRegularExpression::UseUnicodePropertiesOption) | - |
804 | options |= PCRE_UCP; | - |
805 | - | |
806 | return options; | - |
807 | } | - |
808 | - | |
809 | /*! | - |
810 | \internal | - |
811 | */ | - |
812 | static int convertToPcreOptions(QRegularExpression::MatchOptions matchOptions) | - |
813 | { | - |
814 | int options = 0; | - |
815 | - | |
816 | if (matchOptions & QRegularExpression::AnchoredMatchOption) | - |
817 | options |= PCRE_ANCHORED; | - |
818 | - | |
819 | return options; | - |
820 | } | - |
821 | - | |
822 | struct QRegularExpressionPrivate : QSharedData | - |
823 | { | - |
824 | QRegularExpressionPrivate(); | - |
825 | ~QRegularExpressionPrivate(); | - |
826 | QRegularExpressionPrivate(const QRegularExpressionPrivate &other); | - |
827 | - | |
828 | void cleanCompiledPattern(); | - |
829 | void compilePattern(); | - |
830 | void getPatternInfo(); | - |
831 | - | |
832 | enum OptimizePatternOption { | - |
833 | LazyOptimizeOption, | - |
834 | ImmediateOptimizeOption | - |
835 | }; | - |
836 | - | |
837 | void optimizePattern(OptimizePatternOption option); | - |
838 | - | |
839 | enum CheckSubjectStringOption { | - |
840 | CheckSubjectString, | - |
841 | DontCheckSubjectString | - |
842 | }; | - |
843 | - | |
844 | QRegularExpressionMatchPrivate *doMatch(const QString &subject, | - |
845 | int subjectStartPos, | - |
846 | int subjectLength, | - |
847 | int offset, | - |
848 | QRegularExpression::MatchType matchType, | - |
849 | QRegularExpression::MatchOptions matchOptions, | - |
850 | CheckSubjectStringOption checkSubjectStringOption = CheckSubjectString, | - |
851 | const QRegularExpressionMatchPrivate *previous = 0) const; | - |
852 | - | |
853 | int captureIndexForName(const QString &name) const; | - |
854 | - | |
855 | // sizeof(QSharedData) == 4, so start our members with an enum | - |
856 | QRegularExpression::PatternOptions patternOptions; | - |
857 | QString pattern; | - |
858 | - | |
859 | // *All* of the following members are set managed while holding this mutex, | - |
860 | // except for isDirty which is set to true by QRegularExpression setters | - |
861 | // (right after a detach happened). | - |
862 | // On the other hand, after the compilation and studying, | - |
863 | // it's safe to *use* (i.e. read) them from multiple threads at the same time. | - |
864 | // Therefore, doMatch doesn't need to lock this mutex. | - |
865 | QMutex mutex; | - |
866 | - | |
867 | // The PCRE pointers are reference-counted by the QRegularExpressionPrivate | - |
868 | // objects themselves; when the private is copied (i.e. a detach happened) | - |
869 | // they are set to 0 | - |
870 | pcre16 *compiledPattern; | - |
871 | QAtomicPointer<pcre16_extra> studyData; | - |
872 | const char *errorString; | - |
873 | int errorOffset; | - |
874 | int capturingCount; | - |
875 | unsigned int usedCount; | - |
876 | bool usingCrLfNewlines; | - |
877 | bool isDirty; | - |
878 | }; | - |
879 | - | |
880 | struct QRegularExpressionMatchPrivate : QSharedData | - |
881 | { | - |
882 | QRegularExpressionMatchPrivate(const QRegularExpression &re, | - |
883 | const QString &subject, | - |
884 | int subjectStart, | - |
885 | int subjectLength, | - |
886 | QRegularExpression::MatchType matchType, | - |
887 | QRegularExpression::MatchOptions matchOptions, | - |
888 | int capturingCount = 0); | - |
889 | - | |
890 | QRegularExpressionMatch nextMatch() const; | - |
891 | - | |
892 | const QRegularExpression regularExpression; | - |
893 | const QString subject; | - |
894 | // the capturedOffsets vector contains pairs of (start, end) positions | - |
895 | // for each captured substring | - |
896 | QVector<int> capturedOffsets; | - |
897 | - | |
898 | const int subjectStart; | - |
899 | const int subjectLength; | - |
900 | - | |
901 | const QRegularExpression::MatchType matchType; | - |
902 | const QRegularExpression::MatchOptions matchOptions; | - |
903 | - | |
904 | int capturedCount; | - |
905 | - | |
906 | bool hasMatch; | - |
907 | bool hasPartialMatch; | - |
908 | bool isValid; | - |
909 | }; | - |
910 | - | |
911 | struct QRegularExpressionMatchIteratorPrivate : QSharedData | - |
912 | { | - |
913 | QRegularExpressionMatchIteratorPrivate(const QRegularExpression &re, | - |
914 | QRegularExpression::MatchType matchType, | - |
915 | QRegularExpression::MatchOptions matchOptions, | - |
916 | const QRegularExpressionMatch &next); | - |
917 | - | |
918 | bool hasNext() const; | - |
919 | QRegularExpressionMatch next; | - |
920 | const QRegularExpression regularExpression; | - |
921 | const QRegularExpression::MatchType matchType; | - |
922 | const QRegularExpression::MatchOptions matchOptions; | - |
923 | }; | - |
924 | - | |
925 | /*! | - |
926 | \internal | - |
927 | */ | - |
928 | QRegularExpression::QRegularExpression(QRegularExpressionPrivate &dd) | - |
929 | : d(&dd) | - |
930 | { | - |
931 | } | - |
932 | - | |
933 | /*! | - |
934 | \internal | - |
935 | */ | - |
936 | QRegularExpressionPrivate::QRegularExpressionPrivate() | - |
937 | : patternOptions(0), pattern(), | - |
938 | mutex(), | - |
939 | compiledPattern(0), studyData(0), | - |
940 | errorString(0), errorOffset(-1), | - |
941 | capturingCount(0), | - |
942 | usedCount(0), | - |
943 | usingCrLfNewlines(false), | - |
944 | isDirty(true) | - |
945 | { | - |
946 | } | - |
947 | - | |
948 | /*! | - |
949 | \internal | - |
950 | */ | - |
951 | QRegularExpressionPrivate::~QRegularExpressionPrivate() | - |
952 | { | - |
953 | cleanCompiledPattern(); | - |
954 | } | - |
955 | - | |
956 | /*! | - |
957 | \internal | - |
958 | - | |
959 | Copies the private, which means copying only the pattern and the pattern | - |
960 | options. The compiledPattern and the studyData pointers are NOT copied (we | - |
961 | do not own them any more), and in general all the members set when | - |
962 | compiling a pattern are set to default values. isDirty is set back to true | - |
963 | so that the pattern has to be recompiled again. | - |
964 | */ | - |
965 | QRegularExpressionPrivate::QRegularExpressionPrivate(const QRegularExpressionPrivate &other) | - |
966 | : QSharedData(other), | - |
967 | patternOptions(other.patternOptions), pattern(other.pattern), | - |
968 | mutex(), | - |
969 | compiledPattern(0), studyData(0), | - |
970 | errorString(0), | - |
971 | errorOffset(-1), capturingCount(0), | - |
972 | usedCount(0), | - |
973 | usingCrLfNewlines(false), isDirty(true) | - |
974 | { | - |
975 | } | - |
976 | - | |
977 | /*! | - |
978 | \internal | - |
979 | */ | - |
980 | void QRegularExpressionPrivate::cleanCompiledPattern() | - |
981 | { | - |
982 | pcre16_free(compiledPattern); | - |
983 | pcre16_free_study(studyData.load()); | - |
984 | usedCount = 0; | - |
985 | compiledPattern = 0; | - |
986 | studyData.store(0); | - |
987 | usingCrLfNewlines = false; | - |
988 | errorOffset = -1; | - |
989 | capturingCount = 0; | - |
990 | } | - |
991 | - | |
992 | /*! | - |
993 | \internal | - |
994 | */ | - |
995 | void QRegularExpressionPrivate::compilePattern() | - |
996 | { | - |
997 | QMutexLocker lock(&mutex); | - |
998 | - | |
999 | if (!isDirty) | - |
1000 | return; | - |
1001 | - | |
1002 | isDirty = false; | - |
1003 | cleanCompiledPattern(); | - |
1004 | - | |
1005 | int options = convertToPcreOptions(patternOptions); | - |
1006 | options |= PCRE_UTF16; | - |
1007 | - | |
1008 | int errorCode; | - |
1009 | compiledPattern = pcre16_compile2(pattern.utf16(), options, | - |
1010 | &errorCode, &errorString, &errorOffset, 0); | - |
1011 | - | |
1012 | if (!compiledPattern) | - |
1013 | return; | - |
1014 | - | |
1015 | Q_ASSERT(errorCode == 0); | - |
1016 | Q_ASSERT(studyData.load() == 0); // studying (=>optimizing) is always done later | - |
1017 | errorOffset = -1; | - |
1018 | - | |
1019 | getPatternInfo(); | - |
1020 | } | - |
1021 | - | |
1022 | /*! | - |
1023 | \internal | - |
1024 | */ | - |
1025 | void QRegularExpressionPrivate::getPatternInfo() | - |
1026 | { | - |
1027 | Q_ASSERT(compiledPattern); | - |
1028 | Q_ASSERT(studyData.load() == 0); | - |
1029 | - | |
1030 | pcre16_fullinfo(compiledPattern, 0, PCRE_INFO_CAPTURECOUNT, &capturingCount); | - |
1031 | - | |
1032 | // detect the settings for the newline | - |
1033 | unsigned long int patternNewlineSetting; | - |
1034 | pcre16_fullinfo(compiledPattern, 0, PCRE_INFO_OPTIONS, &patternNewlineSetting); | - |
1035 | patternNewlineSetting &= PCRE_NEWLINE_CR | PCRE_NEWLINE_LF | PCRE_NEWLINE_CRLF | - |
1036 | | PCRE_NEWLINE_ANY | PCRE_NEWLINE_ANYCRLF; | - |
1037 | if (patternNewlineSetting == 0) { | - |
1038 | // no option was specified in the regexp, grab PCRE build defaults | - |
1039 | int pcreNewlineSetting; | - |
1040 | pcre16_config(PCRE_CONFIG_NEWLINE, &pcreNewlineSetting); | - |
1041 | switch (pcreNewlineSetting) { | - |
1042 | case 13: | - |
1043 | patternNewlineSetting = PCRE_NEWLINE_CR; break; | - |
1044 | case 10: | - |
1045 | patternNewlineSetting = PCRE_NEWLINE_LF; break; | - |
1046 | case 3338: // (13<<8 | 10) | - |
1047 | patternNewlineSetting = PCRE_NEWLINE_CRLF; break; | - |
1048 | case -2: | - |
1049 | patternNewlineSetting = PCRE_NEWLINE_ANYCRLF; break; | - |
1050 | case -1: | - |
1051 | patternNewlineSetting = PCRE_NEWLINE_ANY; break; | - |
1052 | default: | - |
1053 | qWarning("QRegularExpressionPrivate::compilePattern(): " | - |
1054 | "PCRE_CONFIG_NEWLINE returned an unknown newline"); | - |
1055 | break; | - |
1056 | } | - |
1057 | } | - |
1058 | - | |
1059 | usingCrLfNewlines = (patternNewlineSetting == PCRE_NEWLINE_CRLF) || | - |
1060 | (patternNewlineSetting == PCRE_NEWLINE_ANY) || | - |
1061 | (patternNewlineSetting == PCRE_NEWLINE_ANYCRLF); | - |
1062 | - | |
1063 | int hasJOptionChanged; | - |
1064 | pcre16_fullinfo(compiledPattern, 0, PCRE_INFO_JCHANGED, &hasJOptionChanged); | - |
1065 | if (hasJOptionChanged) { | - |
1066 | qWarning("QRegularExpressionPrivate::getPatternInfo(): the pattern '%s'\n" | - |
1067 | " is using the (?J) option; duplicate capturing group names are not supported by Qt", | - |
1068 | qPrintable(pattern)); | - |
1069 | } | - |
1070 | } | - |
1071 | - | |
1072 | - | |
1073 | /* | - |
1074 | Simple "smartpointer" wrapper around a pcre_jit_stack, to be used with | - |
1075 | QThreadStorage. | - |
1076 | */ | - |
1077 | class QPcreJitStackPointer | - |
1078 | { | - |
1079 | Q_DISABLE_COPY(QPcreJitStackPointer) | - |
1080 | - | |
1081 | public: | - |
1082 | /*! | - |
1083 | \internal | - |
1084 | */ | - |
1085 | QPcreJitStackPointer() | - |
1086 | { | - |
1087 | // The default JIT stack size in PCRE is 32K, | - |
1088 | // we allocate from 32K up to 512K. | - |
1089 | stack = pcre16_jit_stack_alloc(32*1024, 512*1024); | - |
1090 | } | - |
1091 | /*! | - |
1092 | \internal | - |
1093 | */ | - |
1094 | ~QPcreJitStackPointer() | - |
1095 | { | - |
1096 | if (stack) | - |
1097 | pcre16_jit_stack_free(stack); | - |
1098 | } | - |
1099 | - | |
1100 | pcre16_jit_stack *stack; | - |
1101 | }; | - |
1102 | - | |
1103 | Q_GLOBAL_STATIC(QThreadStorage<QPcreJitStackPointer *>, jitStacks) | - |
1104 | - | |
1105 | /*! | - |
1106 | \internal | - |
1107 | */ | - |
1108 | static pcre16_jit_stack *qtPcreCallback(void *) | - |
1109 | { | - |
1110 | if (jitStacks()->hasLocalData()) | - |
1111 | return jitStacks()->localData()->stack; | - |
1112 | - | |
1113 | return 0; | - |
1114 | } | - |
1115 | - | |
1116 | /*! | - |
1117 | \internal | - |
1118 | */ | - |
1119 | static bool isJitEnabled() | - |
1120 | { | - |
1121 | QByteArray jitEnvironment = qgetenv("QT_ENABLE_REGEXP_JIT"); | - |
1122 | if (!jitEnvironment.isEmpty()) { | - |
1123 | bool ok; | - |
1124 | int enableJit = jitEnvironment.toInt(&ok); | - |
1125 | return ok ? (enableJit != 0) : true; | - |
1126 | } | - |
1127 | - | |
1128 | #ifdef QT_DEBUG | - |
1129 | return false; | - |
1130 | #else | - |
1131 | return true; | - |
1132 | #endif | - |
1133 | } | - |
1134 | - | |
1135 | /*! | - |
1136 | \internal | - |
1137 | - | |
1138 | The purpose of the function is to call pcre16_study (which allows some | - |
1139 | optimizations to be performed, including JIT-compiling the pattern), and | - |
1140 | setting the studyData member variable to the result of the study. It gets | - |
1141 | called by doMatch() every time a match is performed. As of now, the | - |
1142 | optimizations on the pattern are performed after a certain number of usages | - |
1143 | (i.e. the qt_qregularexpression_optimize_after_use_count constant) unless | - |
1144 | the DontAutomaticallyOptimizeOption option is set on the QRegularExpression | - |
1145 | object, or anyhow by calling optimize() (which will pass | - |
1146 | ImmediateOptimizeOption). | - |
1147 | - | |
1148 | Notice that although the method is protected by a mutex, one thread may | - |
1149 | invoke this function and return immediately (i.e. not study the pattern, | - |
1150 | leaving studyData to NULL); but before calling pcre16_exec to perform the | - |
1151 | match, another thread performs the studying and sets studyData to something | - |
1152 | else. Although the assignment to studyData is itself atomic, the release of | - |
1153 | the memory pointed by studyData isn't. Therefore, we work on a local copy | - |
1154 | (localStudyData) before using storeRelease on studyData. In doMatch there's | - |
1155 | the corresponding loadAcquire. | - |
1156 | */ | - |
1157 | void QRegularExpressionPrivate::optimizePattern(OptimizePatternOption option) | - |
1158 | { | - |
1159 | Q_ASSERT(compiledPattern); | - |
1160 | - | |
1161 | QMutexLocker lock(&mutex); | - |
1162 | - | |
1163 | if (studyData.load()) // already optimized | - |
1164 | return; | - |
1165 | - | |
1166 | if ((option == LazyOptimizeOption) && (++usedCount != qt_qregularexpression_optimize_after_use_count)) | - |
1167 | return; | - |
1168 | - | |
1169 | static const bool enableJit = isJitEnabled(); | - |
1170 | - | |
1171 | int studyOptions = 0; | - |
1172 | if (enableJit) | - |
1173 | studyOptions |= (PCRE_STUDY_JIT_COMPILE | PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE | PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE); | - |
1174 | - | |
1175 | const char *err; | - |
1176 | pcre16_extra * const localStudyData = pcre16_study(compiledPattern, studyOptions, &err); | - |
1177 | - | |
1178 | if (localStudyData && localStudyData->flags & PCRE_EXTRA_EXECUTABLE_JIT) | - |
1179 | pcre16_assign_jit_stack(localStudyData, qtPcreCallback, 0); | - |
1180 | - | |
1181 | if (!localStudyData && err) | - |
1182 | qWarning("QRegularExpressionPrivate::optimizePattern(): pcre_study failed: %s", err); | - |
1183 | - | |
1184 | studyData.storeRelease(localStudyData); | - |
1185 | } | - |
1186 | - | |
1187 | /*! | - |
1188 | \internal | - |
1189 | - | |
1190 | Returns the capturing group number for the given name. Duplicated names for | - |
1191 | capturing groups are not supported. | - |
1192 | */ | - |
1193 | int QRegularExpressionPrivate::captureIndexForName(const QString &name) const | - |
1194 | { | - |
1195 | Q_ASSERT(!name.isEmpty()); | - |
1196 | - | |
1197 | if (!compiledPattern) | - |
1198 | return -1; | - |
1199 | - | |
1200 | int index = pcre16_get_stringnumber(compiledPattern, name.utf16()); | - |
1201 | if (index >= 0) | - |
1202 | return index; | - |
1203 | - | |
1204 | return -1; | - |
1205 | } | - |
1206 | - | |
1207 | /*! | - |
1208 | \internal | - |
1209 | - | |
1210 | This is a simple wrapper for pcre16_exec for handling the case in which the | - |
1211 | JIT runs out of memory. In that case, we allocate a thread-local JIT stack | - |
1212 | and re-run pcre16_exec. | - |
1213 | */ | - |
1214 | static int pcre16SafeExec(const pcre16 *code, const pcre16_extra *extra, | - |
1215 | const unsigned short *subject, int length, | - |
1216 | int startOffset, int options, | - |
1217 | int *ovector, int ovecsize) | - |
1218 | { | - |
1219 | int result = pcre16_exec(code, extra, subject, length, | - |
1220 | startOffset, options, ovector, ovecsize); | - |
1221 | - | |
1222 | if (result == PCRE_ERROR_JIT_STACKLIMIT && !jitStacks()->hasLocalData()) { | - |
1223 | QPcreJitStackPointer *p = new QPcreJitStackPointer; | - |
1224 | jitStacks()->setLocalData(p); | - |
1225 | - | |
1226 | result = pcre16_exec(code, extra, subject, length, | - |
1227 | startOffset, options, ovector, ovecsize); | - |
1228 | } | - |
1229 | - | |
1230 | return result; | - |
1231 | } | - |
1232 | - | |
1233 | /*! | - |
1234 | \internal | - |
1235 | - | |
1236 | Performs a match on the substring of the given \a subject string, | - |
1237 | substring which starts from \a subjectStart and up to | - |
1238 | (but not including) \a subjectStart + \a subjectLength. The match | - |
1239 | will be of type \a matchType and using the options \a matchOptions; | - |
1240 | the matching \a offset is relative the substring, | - |
1241 | and if negative, it's taken as an offset from the end of the substring. | - |
1242 | - | |
1243 | It also advances a match if a previous result is given as \a | - |
1244 | previous. The \a subject string goes a Unicode validity check if | - |
1245 | \a checkSubjectString is CheckSubjectString and the match options don't | - |
1246 | include DontCheckSubjectStringMatchOption (PCRE doesn't like illegal | - |
1247 | UTF-16 sequences). | - |
1248 | - | |
1249 | Returns the QRegularExpressionMatchPrivate of the result. | - |
1250 | - | |
1251 | Advancing a match is a tricky algorithm. If the previous match matched a | - |
1252 | non-empty string, we just do an ordinary match at the offset position. | - |
1253 | - | |
1254 | If the previous match matched an empty string, then an anchored, non-empty | - |
1255 | match is attempted at the offset position. If that succeeds, then we got | - |
1256 | the next match and we can return it. Otherwise, we advance by 1 position | - |
1257 | (which can be one or two code units in UTF-16!) and reattempt a "normal" | - |
1258 | match. We also have the problem of detecting the current newline format: if | - |
1259 | the new advanced offset is pointing to the beginning of a CRLF sequence, we | - |
1260 | must advance over it. | - |
1261 | */ | - |
1262 | QRegularExpressionMatchPrivate *QRegularExpressionPrivate::doMatch(const QString &subject, | - |
1263 | int subjectStart, | - |
1264 | int subjectLength, | - |
1265 | int offset, | - |
1266 | QRegularExpression::MatchType matchType, | - |
1267 | QRegularExpression::MatchOptions matchOptions, | - |
1268 | CheckSubjectStringOption checkSubjectStringOption, | - |
1269 | const QRegularExpressionMatchPrivate *previous) const | - |
1270 | { | - |
1271 | if (offset < 0) | - |
1272 | offset += subjectLength; | - |
1273 | - | |
1274 | QRegularExpression re(*const_cast<QRegularExpressionPrivate *>(this)); | - |
1275 | - | |
1276 | if (offset < 0 || offset > subjectLength) | - |
1277 | return new QRegularExpressionMatchPrivate(re, subject, subjectStart, subjectLength, matchType, matchOptions); | - |
1278 | - | |
1279 | if (!compiledPattern) { | - |
1280 | qWarning("QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object"); | - |
1281 | return new QRegularExpressionMatchPrivate(re, subject, subjectStart, subjectLength, matchType, matchOptions); | - |
1282 | } | - |
1283 | - | |
1284 | // skip optimizing and doing the actual matching if NoMatch type was requested | - |
1285 | if (matchType == QRegularExpression::NoMatch) { | - |
1286 | QRegularExpressionMatchPrivate *priv = new QRegularExpressionMatchPrivate(re, subject, | - |
1287 | subjectStart, subjectLength, | - |
1288 | matchType, matchOptions); | - |
1289 | priv->isValid = true; | - |
1290 | return priv; | - |
1291 | } | - |
1292 | - | |
1293 | // capturingCount doesn't include the implicit "0" capturing group | - |
1294 | QRegularExpressionMatchPrivate *priv = new QRegularExpressionMatchPrivate(re, subject, | - |
1295 | subjectStart, subjectLength, | - |
1296 | matchType, matchOptions, | - |
1297 | capturingCount + 1); | - |
1298 | - | |
1299 | if (!(patternOptions & QRegularExpression::DontAutomaticallyOptimizeOption)) { | - |
1300 | const OptimizePatternOption optimizePatternOption = | - |
1301 | (patternOptions & QRegularExpression::OptimizeOnFirstUsageOption) | - |
1302 | ? ImmediateOptimizeOption | - |
1303 | : LazyOptimizeOption; | - |
1304 | - | |
1305 | // this is mutex protected | - |
1306 | const_cast<QRegularExpressionPrivate *>(this)->optimizePattern(optimizePatternOption); | - |
1307 | } | - |
1308 | - | |
1309 | // work with a local copy of the study data, as we are running pcre_exec | - |
1310 | // potentially more than once, and we don't want to run call it | - |
1311 | // with different study data | - |
1312 | const pcre16_extra * const currentStudyData = studyData.loadAcquire(); | - |
1313 | - | |
1314 | int pcreOptions = convertToPcreOptions(matchOptions); | - |
1315 | - | |
1316 | if (matchType == QRegularExpression::PartialPreferCompleteMatch) | - |
1317 | pcreOptions |= PCRE_PARTIAL_SOFT; | - |
1318 | else if (matchType == QRegularExpression::PartialPreferFirstMatch) | - |
1319 | pcreOptions |= PCRE_PARTIAL_HARD; | - |
1320 | - | |
1321 | if (checkSubjectStringOption == DontCheckSubjectString | - |
1322 | || matchOptions & QRegularExpression::DontCheckSubjectStringMatchOption) { | - |
1323 | pcreOptions |= PCRE_NO_UTF16_CHECK; | - |
1324 | } | - |
1325 | - | |
1326 | bool previousMatchWasEmpty = false; | - |
1327 | if (previous && previous->hasMatch && | - |
1328 | (previous->capturedOffsets.at(0) == previous->capturedOffsets.at(1))) { | - |
1329 | previousMatchWasEmpty = true; | - |
1330 | } | - |
1331 | - | |
1332 | int * const captureOffsets = priv->capturedOffsets.data(); | - |
1333 | const int captureOffsetsCount = priv->capturedOffsets.size(); | - |
1334 | - | |
1335 | const unsigned short * const subjectUtf16 = subject.utf16() + subjectStart; | - |
1336 | - | |
1337 | int result; | - |
1338 | - | |
1339 | if (!previousMatchWasEmpty) { | - |
1340 | result = pcre16SafeExec(compiledPattern, currentStudyData, | - |
1341 | subjectUtf16, subjectLength, | - |
1342 | offset, pcreOptions, | - |
1343 | captureOffsets, captureOffsetsCount); | - |
1344 | } else { | - |
1345 | result = pcre16SafeExec(compiledPattern, currentStudyData, | - |
1346 | subjectUtf16, subjectLength, | - |
1347 | offset, pcreOptions | PCRE_NOTEMPTY_ATSTART | PCRE_ANCHORED, | - |
1348 | captureOffsets, captureOffsetsCount); | - |
1349 | - | |
1350 | if (result == PCRE_ERROR_NOMATCH) { | - |
1351 | ++offset; | - |
1352 | - | |
1353 | if (usingCrLfNewlines | - |
1354 | && offset < subjectLength | - |
1355 | && subjectUtf16[offset - 1] == QLatin1Char('\r') | - |
1356 | && subjectUtf16[offset] == QLatin1Char('\n')) { | - |
1357 | ++offset; | - |
1358 | } else if (offset < subjectLength | - |
1359 | && QChar::isLowSurrogate(subjectUtf16[offset])) { | - |
1360 | ++offset; | - |
1361 | } | - |
1362 | - | |
1363 | result = pcre16SafeExec(compiledPattern, currentStudyData, | - |
1364 | subjectUtf16, subjectLength, | - |
1365 | offset, pcreOptions, | - |
1366 | captureOffsets, captureOffsetsCount); | - |
1367 | } | - |
1368 | } | - |
1369 | - | |
1370 | #ifdef QREGULAREXPRESSION_DEBUG | - |
1371 | qDebug() << "Matching" << pattern << "against" << subject | - |
1372 | << "starting at" << subjectStart << "len" << subjectLength | - |
1373 | << "offset" << offset | - |
1374 | << matchType << matchOptions << previousMatchWasEmpty | - |
1375 | << "result" << result; | - |
1376 | #endif | - |
1377 | - | |
1378 | // result == 0 means not enough space in captureOffsets; should never happen | - |
1379 | Q_ASSERT(result != 0); | - |
1380 | - | |
1381 | if (result > 0) { | - |
1382 | // full match | - |
1383 | priv->isValid = true; | - |
1384 | priv->hasMatch = true; | - |
1385 | priv->capturedCount = result; | - |
1386 | priv->capturedOffsets.resize(result * 2); | - |
1387 | } else { | - |
1388 | // no match, partial match or error | - |
1389 | priv->hasPartialMatch = (result == PCRE_ERROR_PARTIAL); | - |
1390 | priv->isValid = (result == PCRE_ERROR_NOMATCH || result == PCRE_ERROR_PARTIAL); | - |
1391 | - | |
1392 | if (result == PCRE_ERROR_PARTIAL) { | - |
1393 | // partial match: | - |
1394 | // leave the start and end capture offsets (i.e. cap(0)) | - |
1395 | priv->capturedCount = 1; | - |
1396 | priv->capturedOffsets.resize(2); | - |
1397 | } else { | - |
1398 | // no match or error | - |
1399 | priv->capturedCount = 0; | - |
1400 | priv->capturedOffsets.clear(); | - |
1401 | } | - |
1402 | } | - |
1403 | - | |
1404 | return priv; | - |
1405 | } | - |
1406 | - | |
1407 | /*! | - |
1408 | \internal | - |
1409 | */ | - |
1410 | QRegularExpressionMatchPrivate::QRegularExpressionMatchPrivate(const QRegularExpression &re, | - |
1411 | const QString &subject, | - |
1412 | int subjectStart, | - |
1413 | int subjectLength, | - |
1414 | QRegularExpression::MatchType matchType, | - |
1415 | QRegularExpression::MatchOptions matchOptions, | - |
1416 | int capturingCount) | - |
1417 | : regularExpression(re), subject(subject), | - |
1418 | subjectStart(subjectStart), subjectLength(subjectLength), | - |
1419 | matchType(matchType), matchOptions(matchOptions), | - |
1420 | capturedCount(0), | - |
1421 | hasMatch(false), hasPartialMatch(false), isValid(false) | - |
1422 | { | - |
1423 | Q_ASSERT(capturingCount >= 0); | - |
1424 | if (capturingCount > 0) { | - |
1425 | const int captureOffsetsCount = capturingCount * 3; | - |
1426 | capturedOffsets.resize(captureOffsetsCount); | - |
1427 | } | - |
1428 | } | - |
1429 | - | |
1430 | - | |
1431 | /*! | - |
1432 | \internal | - |
1433 | */ | - |
1434 | QRegularExpressionMatch QRegularExpressionMatchPrivate::nextMatch() const | - |
1435 | { | - |
1436 | Q_ASSERT(isValid); | - |
1437 | Q_ASSERT(hasMatch || hasPartialMatch); | - |
1438 | - | |
1439 | // Note the DontCheckSubjectString passed for the check of the subject string: | - |
1440 | // if we're advancing a match on the same subject, | - |
1441 | // then that subject was already checked at least once (when this object | - |
1442 | // was created, or when the object that created this one was created, etc.) | - |
1443 | QRegularExpressionMatchPrivate *nextPrivate = regularExpression.d->doMatch(subject, | - |
1444 | subjectStart, | - |
1445 | subjectLength, | - |
1446 | capturedOffsets.at(1), | - |
1447 | matchType, | - |
1448 | matchOptions, | - |
1449 | QRegularExpressionPrivate::DontCheckSubjectString, | - |
1450 | this); | - |
1451 | return QRegularExpressionMatch(*nextPrivate); | - |
1452 | } | - |
1453 | - | |
1454 | /*! | - |
1455 | \internal | - |
1456 | */ | - |
1457 | QRegularExpressionMatchIteratorPrivate::QRegularExpressionMatchIteratorPrivate(const QRegularExpression &re, | - |
1458 | QRegularExpression::MatchType matchType, | - |
1459 | QRegularExpression::MatchOptions matchOptions, | - |
1460 | const QRegularExpressionMatch &next) | - |
1461 | : next(next), | - |
1462 | regularExpression(re), | - |
1463 | matchType(matchType), matchOptions(matchOptions) | - |
1464 | { | - |
1465 | } | - |
1466 | - | |
1467 | /*! | - |
1468 | \internal | - |
1469 | */ | - |
1470 | bool QRegularExpressionMatchIteratorPrivate::hasNext() const | - |
1471 | { | - |
1472 | return next.isValid() && (next.hasMatch() || next.hasPartialMatch()); | - |
1473 | } | - |
1474 | - | |
1475 | // PUBLIC API | - |
1476 | - | |
1477 | /*! | - |
1478 | Constructs a QRegularExpression object with an empty pattern and no pattern | - |
1479 | options. | - |
1480 | - | |
1481 | \sa setPattern(), setPatternOptions() | - |
1482 | */ | - |
1483 | QRegularExpression::QRegularExpression() | - |
1484 | : d(new QRegularExpressionPrivate) | - |
1485 | { | - |
1486 | } | - |
1487 | - | |
1488 | /*! | - |
1489 | Constructs a QRegularExpression object using the given \a pattern as | - |
1490 | pattern and the \a options as the pattern options. | - |
1491 | - | |
1492 | \sa setPattern(), setPatternOptions() | - |
1493 | */ | - |
1494 | QRegularExpression::QRegularExpression(const QString &pattern, PatternOptions options) | - |
1495 | : d(new QRegularExpressionPrivate) | - |
1496 | { | - |
1497 | d->pattern = pattern; | - |
1498 | d->patternOptions = options; | - |
1499 | } | - |
1500 | - | |
1501 | /*! | - |
1502 | Constructs a QRegularExpression object as a copy of \a re. | - |
1503 | - | |
1504 | \sa operator=() | - |
1505 | */ | - |
1506 | QRegularExpression::QRegularExpression(const QRegularExpression &re) | - |
1507 | : d(re.d) | - |
1508 | { | - |
1509 | } | - |
1510 | - | |
1511 | /*! | - |
1512 | Destroys the QRegularExpression object. | - |
1513 | */ | - |
1514 | QRegularExpression::~QRegularExpression() | - |
1515 | { | - |
1516 | } | - |
1517 | - | |
1518 | /*! | - |
1519 | Assigns the regular expression \a re to this object, and returns a reference | - |
1520 | to the copy. Both the pattern and the pattern options are copied. | - |
1521 | */ | - |
1522 | QRegularExpression &QRegularExpression::operator=(const QRegularExpression &re) | - |
1523 | { | - |
1524 | d = re.d; | - |
1525 | return *this; | - |
1526 | } | - |
1527 | - | |
1528 | /*! | - |
1529 | \fn void QRegularExpression::swap(QRegularExpression &other) | - |
1530 | - | |
1531 | Swaps the regular expression \a other with this regular expression. This | - |
1532 | operation is very fast and never fails. | - |
1533 | */ | - |
1534 | - | |
1535 | /*! | - |
1536 | Returns the pattern string of the regular expression. | - |
1537 | - | |
1538 | \sa setPattern(), patternOptions() | - |
1539 | */ | - |
1540 | QString QRegularExpression::pattern() const | - |
1541 | { | - |
1542 | return d->pattern; | - |
1543 | } | - |
1544 | - | |
1545 | /*! | - |
1546 | Sets the pattern string of the regular expression to \a pattern. The | - |
1547 | pattern options are left unchanged. | - |
1548 | - | |
1549 | \sa pattern(), setPatternOptions() | - |
1550 | */ | - |
1551 | void QRegularExpression::setPattern(const QString &pattern) | - |
1552 | { | - |
1553 | d.detach(); | - |
1554 | d->isDirty = true; | - |
1555 | d->pattern = pattern; | - |
1556 | } | - |
1557 | - | |
1558 | /*! | - |
1559 | Returns the pattern options for the regular expression. | - |
1560 | - | |
1561 | \sa setPatternOptions(), pattern() | - |
1562 | */ | - |
1563 | QRegularExpression::PatternOptions QRegularExpression::patternOptions() const | - |
1564 | { | - |
1565 | return d->patternOptions; | - |
1566 | } | - |
1567 | - | |
1568 | /*! | - |
1569 | Sets the given \a options as the pattern options of the regular expression. | - |
1570 | The pattern string is left unchanged. | - |
1571 | - | |
1572 | \sa patternOptions(), setPattern() | - |
1573 | */ | - |
1574 | void QRegularExpression::setPatternOptions(PatternOptions options) | - |
1575 | { | - |
1576 | d.detach(); | - |
1577 | d->isDirty = true; | - |
1578 | d->patternOptions = options; | - |
1579 | } | - |
1580 | - | |
1581 | /*! | - |
1582 | Returns the number of capturing groups inside the pattern string, | - |
1583 | or -1 if the regular expression is not valid. | - |
1584 | - | |
1585 | \note The implicit capturing group 0 is \e{not} included in the returned number. | - |
1586 | - | |
1587 | \sa isValid() | - |
1588 | */ | - |
1589 | int QRegularExpression::captureCount() const | - |
1590 | { | - |
1591 | if (!isValid()) // will compile the pattern | - |
1592 | return -1; | - |
1593 | return d->capturingCount; | - |
1594 | } | - |
1595 | - | |
1596 | /*! | - |
1597 | \since 5.1 | - |
1598 | - | |
1599 | Returns a list of captureCount() + 1 elements, containing the names of the | - |
1600 | named capturing groups in the pattern string. The list is sorted such that | - |
1601 | the element of the list at position \c{i} is the name of the \c{i}-th | - |
1602 | capturing group, if it has a name, or an empty string if that capturing | - |
1603 | group is unnamed. | - |
1604 | - | |
1605 | For instance, given the regular expression | - |
1606 | - | |
1607 | \code | - |
1608 | (?<day>\d\d)-(?<month>\d\d)-(?<year>\d\d\d\d) (\w+) (?<name>\w+) | - |
1609 | \endcode | - |
1610 | - | |
1611 | namedCaptureGroups() will return the following list: | - |
1612 | - | |
1613 | \code | - |
1614 | ("", "day", "month", "year", "", "name") | - |
1615 | \endcode | - |
1616 | - | |
1617 | which corresponds to the fact that the capturing group #0 (corresponding to | - |
1618 | the whole match) has no name, the capturing group #1 has name "day", the | - |
1619 | capturing group #2 has name "month", etc. | - |
1620 | - | |
1621 | If the regular expression is not valid, returns an empty list. | - |
1622 | - | |
1623 | \sa isValid(), QRegularExpressionMatch::captured(), QString::isEmpty() | - |
1624 | */ | - |
1625 | QStringList QRegularExpression::namedCaptureGroups() const | - |
1626 | { | - |
1627 | if (!isValid()) // isValid() will compile the pattern | - |
1628 | return QStringList(); | - |
1629 | - | |
1630 | // namedCapturingTable will point to a table of | - |
1631 | // namedCapturingTableEntryCount entries, each one of which | - |
1632 | // contains one ushort followed by the name, NUL terminated. | - |
1633 | // The ushort is the numerical index of the name in the pattern. | - |
1634 | // The length of each entry is namedCapturingTableEntrySize. | - |
1635 | ushort *namedCapturingTable; | - |
1636 | int namedCapturingTableEntryCount; | - |
1637 | int namedCapturingTableEntrySize; | - |
1638 | - | |
1639 | pcre16_fullinfo(d->compiledPattern, 0, PCRE_INFO_NAMETABLE, &namedCapturingTable); | - |
1640 | pcre16_fullinfo(d->compiledPattern, 0, PCRE_INFO_NAMECOUNT, &namedCapturingTableEntryCount); | - |
1641 | pcre16_fullinfo(d->compiledPattern, 0, PCRE_INFO_NAMEENTRYSIZE, &namedCapturingTableEntrySize); | - |
1642 | - | |
1643 | QStringList result; | - |
1644 | - | |
1645 | // no QList::resize nor fill is available. The +1 is for the implicit group #0 | - |
1646 | result.reserve(d->capturingCount + 1); | - |
1647 | for (int i = 0; i < d->capturingCount + 1; ++i) | - |
1648 | result.append(QString()); | - |
1649 | - | |
1650 | for (int i = 0; i < namedCapturingTableEntryCount; ++i) { | - |
1651 | const ushort * const currentNamedCapturingTableRow = namedCapturingTable + | - |
1652 | namedCapturingTableEntrySize * i; | - |
1653 | - | |
1654 | const int index = *currentNamedCapturingTableRow; | - |
1655 | result[index] = QString::fromUtf16(currentNamedCapturingTableRow + 1); | - |
1656 | } | - |
1657 | - | |
1658 | return result; | - |
1659 | } | - |
1660 | - | |
1661 | /*! | - |
1662 | Returns \c true if the regular expression is a valid regular expression (that | - |
1663 | is, it contains no syntax errors, etc.), or false otherwise. Use | - |
1664 | errorString() to obtain a textual description of the error. | - |
1665 | - | |
1666 | \sa errorString(), patternErrorOffset() | - |
1667 | */ | - |
1668 | bool QRegularExpression::isValid() const | - |
1669 | { | - |
1670 | d.data()->compilePattern(); | - |
1671 | return d->compiledPattern; | - |
1672 | } | - |
1673 | - | |
1674 | /*! | - |
1675 | Returns a textual description of the error found when checking the validity | - |
1676 | of the regular expression, or "no error" if no error was found. | - |
1677 | - | |
1678 | \sa isValid(), patternErrorOffset() | - |
1679 | */ | - |
1680 | QString QRegularExpression::errorString() const | - |
1681 | { | - |
1682 | d.data()->compilePattern(); | - |
1683 | if (d->errorString) | - |
1684 | return QCoreApplication::translate("QRegularExpression", d->errorString); | - |
1685 | return QCoreApplication::translate("QRegularExpression", "no error"); | - |
1686 | } | - |
1687 | - | |
1688 | /*! | - |
1689 | Returns the offset, inside the pattern string, at which an error was found | - |
1690 | when checking the validity of the regular expression. If no error was | - |
1691 | found, then -1 is returned. | - |
1692 | - | |
1693 | \sa pattern(), isValid(), errorString() | - |
1694 | */ | - |
1695 | int QRegularExpression::patternErrorOffset() const | - |
1696 | { | - |
1697 | d.data()->compilePattern(); | - |
1698 | return d->errorOffset; | - |
1699 | } | - |
1700 | - | |
1701 | /*! | - |
1702 | Attempts to match the regular expression against the given \a subject | - |
1703 | string, starting at the position \a offset inside the subject, using a | - |
1704 | match of type \a matchType and honoring the given \a matchOptions. | - |
1705 | - | |
1706 | The returned QRegularExpressionMatch object contains the results of the | - |
1707 | match. | - |
1708 | - | |
1709 | \sa QRegularExpressionMatch, {normal matching} | - |
1710 | */ | - |
1711 | QRegularExpressionMatch QRegularExpression::match(const QString &subject, | - |
1712 | int offset, | - |
1713 | MatchType matchType, | - |
1714 | MatchOptions matchOptions) const | - |
1715 | { | - |
1716 | d.data()->compilePattern(); | - |
1717 | - | |
1718 | QRegularExpressionMatchPrivate *priv = d->doMatch(subject, 0, subject.length(), offset, matchType, matchOptions); | - |
1719 | return QRegularExpressionMatch(*priv); | - |
1720 | } | - |
1721 | - | |
1722 | /*! | - |
1723 | \since 5.5 | - |
1724 | \overload | - |
1725 | - | |
1726 | Attempts to match the regular expression against the given \a subjectRef | - |
1727 | string reference, starting at the position \a offset inside the subject, using a | - |
1728 | match of type \a matchType and honoring the given \a matchOptions. | - |
1729 | - | |
1730 | The returned QRegularExpressionMatch object contains the results of the | - |
1731 | match. | - |
1732 | - | |
1733 | \sa QRegularExpressionMatch, {normal matching} | - |
1734 | */ | - |
1735 | QRegularExpressionMatch QRegularExpression::match(const QStringRef &subjectRef, | - |
1736 | int offset, | - |
1737 | MatchType matchType, | - |
1738 | MatchOptions matchOptions) const | - |
1739 | { | - |
1740 | d.data()->compilePattern(); | - |
1741 | - | |
1742 | const QString subject = subjectRef.string() ? *subjectRef.string() : QString(); | - |
1743 | - | |
1744 | QRegularExpressionMatchPrivate *priv = d->doMatch(subject, subjectRef.position(), subjectRef.length(), offset, matchType, matchOptions); | - |
1745 | return QRegularExpressionMatch(*priv); | - |
1746 | } | - |
1747 | - | |
1748 | /*! | - |
1749 | Attempts to perform a global match of the regular expression against the | - |
1750 | given \a subject string, starting at the position \a offset inside the | - |
1751 | subject, using a match of type \a matchType and honoring the given \a | - |
1752 | matchOptions. | - |
1753 | - | |
1754 | The returned QRegularExpressionMatchIterator is positioned before the | - |
1755 | first match result (if any). | - |
1756 | - | |
1757 | \sa QRegularExpressionMatchIterator, {global matching} | - |
1758 | */ | - |
1759 | QRegularExpressionMatchIterator QRegularExpression::globalMatch(const QString &subject, | - |
1760 | int offset, | - |
1761 | MatchType matchType, | - |
1762 | MatchOptions matchOptions) const | - |
1763 | { | - |
1764 | QRegularExpressionMatchIteratorPrivate *priv = | - |
1765 | new QRegularExpressionMatchIteratorPrivate(*this, | - |
1766 | matchType, | - |
1767 | matchOptions, | - |
1768 | match(subject, offset, matchType, matchOptions)); | - |
1769 | - | |
1770 | return QRegularExpressionMatchIterator(*priv); | - |
1771 | } | - |
1772 | - | |
1773 | /*! | - |
1774 | \since 5.5 | - |
1775 | \overload | - |
1776 | - | |
1777 | Attempts to perform a global match of the regular expression against the | - |
1778 | given \a subjectRef string reference, starting at the position \a offset inside the | - |
1779 | subject, using a match of type \a matchType and honoring the given \a | - |
1780 | matchOptions. | - |
1781 | - | |
1782 | The returned QRegularExpressionMatchIterator is positioned before the | - |
1783 | first match result (if any). | - |
1784 | - | |
1785 | \sa QRegularExpressionMatchIterator, {global matching} | - |
1786 | */ | - |
1787 | QRegularExpressionMatchIterator QRegularExpression::globalMatch(const QStringRef &subjectRef, | - |
1788 | int offset, | - |
1789 | MatchType matchType, | - |
1790 | MatchOptions matchOptions) const | - |
1791 | { | - |
1792 | QRegularExpressionMatchIteratorPrivate *priv = | - |
1793 | new QRegularExpressionMatchIteratorPrivate(*this, | - |
1794 | matchType, | - |
1795 | matchOptions, | - |
1796 | match(subjectRef, offset, matchType, matchOptions)); | - |
1797 | - | |
1798 | return QRegularExpressionMatchIterator(*priv); | - |
1799 | } | - |
1800 | - | |
1801 | /*! | - |
1802 | \since 5.4 | - |
1803 | - | |
1804 | Forces an immediate optimization of the pattern, including | - |
1805 | JIT-compiling it (if the JIT compiler is enabled). | - |
1806 | - | |
1807 | Patterns are normally optimized only after a certain number of usages. | - |
1808 | If you can predict that this QRegularExpression object is going to be | - |
1809 | used for several matches, it may be convenient to optimize it in | - |
1810 | advance by calling this function. | - |
1811 | - | |
1812 | \sa QRegularExpression::OptimizeOnFirstUsageOption | - |
1813 | */ | - |
1814 | void QRegularExpression::optimize() const | - |
1815 | { | - |
1816 | if (!isValid()) // will compile the pattern | - |
1817 | return; | - |
1818 | - | |
1819 | d->optimizePattern(QRegularExpressionPrivate::ImmediateOptimizeOption); | - |
1820 | } | - |
1821 | - | |
1822 | /*! | - |
1823 | Returns \c true if the regular expression is equal to \a re, or false | - |
1824 | otherwise. Two QRegularExpression objects are equal if they have | - |
1825 | the same pattern string and the same pattern options. | - |
1826 | - | |
1827 | \sa operator!=() | - |
1828 | */ | - |
1829 | bool QRegularExpression::operator==(const QRegularExpression &re) const | - |
1830 | { | - |
1831 | return (d == re.d) || | - |
1832 | (d->pattern == re.d->pattern && d->patternOptions == re.d->patternOptions); | - |
1833 | } | - |
1834 | - | |
1835 | /*! | - |
1836 | \fn QRegularExpression & QRegularExpression::operator=(QRegularExpression && re) | - |
1837 | - | |
1838 | Move-assigns the regular expression \a re to this object, and returns a reference | - |
1839 | to the copy. Both the pattern and the pattern options are copied. | - |
1840 | */ | - |
1841 | - | |
1842 | /*! | - |
1843 | \fn bool QRegularExpression::operator!=(const QRegularExpression &re) const | - |
1844 | - | |
1845 | Returns \c true if the regular expression is different from \a re, or | - |
1846 | false otherwise. | - |
1847 | - | |
1848 | \sa operator==() | - |
1849 | */ | - |
1850 | - | |
1851 | /*! | - |
1852 | \since 5.6 | - |
1853 | \relates QRegularExpression | - |
1854 | - | |
1855 | Returns the hash value for \a key, using | - |
1856 | \a seed to seed the calculation. | - |
1857 | */ | - |
1858 | uint qHash(const QRegularExpression &key, uint seed) Q_DECL_NOTHROW | - |
1859 | { | - |
1860 | QtPrivate::QHashCombine hash; | - |
1861 | seed = hash(seed, key.d->pattern); | - |
1862 | seed = hash(seed, key.d->patternOptions); | - |
1863 | return seed; executed 1350 times by 3 tests: return seed; Executed by:
| 1350 |
1864 | } | - |
1865 | - | |
1866 | /*! | - |
1867 | Escapes all characters of \a str so that they no longer have any special | - |
1868 | meaning when used as a regular expression pattern string, and returns | - |
1869 | the escaped string. For instance: | - |
1870 | - | |
1871 | \snippet code/src_corelib_tools_qregularexpression.cpp 26 | - |
1872 | - | |
1873 | This is very convenient in order to build patterns from arbitrary strings: | - |
1874 | - | |
1875 | \snippet code/src_corelib_tools_qregularexpression.cpp 27 | - |
1876 | - | |
1877 | \note This function implements Perl's quotemeta algorithm and escapes with | - |
1878 | a backslash all characters in \a str, except for the characters in the | - |
1879 | \c{[A-Z]}, \c{[a-z]} and \c{[0-9]} ranges, as well as the underscore | - |
1880 | (\c{_}) character. The only difference with Perl is that a literal NUL | - |
1881 | inside \a str is escaped with the sequence \c{"\\0"} (backslash + | - |
1882 | \c{'0'}), instead of \c{"\\\0"} (backslash + \c{NUL}). | - |
1883 | */ | - |
1884 | QString QRegularExpression::escape(const QString &str) | - |
1885 | { | - |
1886 | QString result; | - |
1887 | const int count = str.size(); | - |
1888 | result.reserve(count * 2); | - |
1889 | - | |
1890 | // everything but [a-zA-Z0-9_] gets escaped, | - |
1891 | // cf. perldoc -f quotemeta | - |
1892 | for (int i = 0; i < count; ++i) { | - |
1893 | const QChar current = str.at(i); | - |
1894 | - | |
1895 | if (current == QChar::Null) { | - |
1896 | // unlike Perl, a literal NUL must be escaped with | - |
1897 | // "\\0" (backslash + 0) and not "\\\0" (backslash + NUL), | - |
1898 | // because pcre16_compile uses a NUL-terminated string | - |
1899 | result.append(QLatin1Char('\\')); | - |
1900 | result.append(QLatin1Char('0')); | - |
1901 | } else if ( (current < QLatin1Char('a') || current > QLatin1Char('z')) && | - |
1902 | (current < QLatin1Char('A') || current > QLatin1Char('Z')) && | - |
1903 | (current < QLatin1Char('0') || current > QLatin1Char('9')) && | - |
1904 | current != QLatin1Char('_') ) | - |
1905 | { | - |
1906 | result.append(QLatin1Char('\\')); | - |
1907 | result.append(current); | - |
1908 | if (current.isHighSurrogate() && i < (count - 1)) | - |
1909 | result.append(str.at(++i)); | - |
1910 | } else { | - |
1911 | result.append(current); | - |
1912 | } | - |
1913 | } | - |
1914 | - | |
1915 | result.squeeze(); | - |
1916 | return result; | - |
1917 | } | - |
1918 | - | |
1919 | /*! | - |
1920 | \since 5.1 | - |
1921 | - | |
1922 | Constructs a valid, empty QRegularExpressionMatch object. The regular | - |
1923 | expression is set to a default-constructed one; the match type to | - |
1924 | QRegularExpression::NoMatch and the match options to | - |
1925 | QRegularExpression::NoMatchOption. | - |
1926 | - | |
1927 | The object will report no match through the hasMatch() and the | - |
1928 | hasPartialMatch() member functions. | - |
1929 | */ | - |
1930 | QRegularExpressionMatch::QRegularExpressionMatch() | - |
1931 | : d(new QRegularExpressionMatchPrivate(QRegularExpression(), | - |
1932 | QString(), | - |
1933 | 0, | - |
1934 | 0, | - |
1935 | QRegularExpression::NoMatch, | - |
1936 | QRegularExpression::NoMatchOption)) | - |
1937 | { | - |
1938 | d->isValid = true; | - |
1939 | } | - |
1940 | - | |
1941 | /*! | - |
1942 | Destroys the match result. | - |
1943 | */ | - |
1944 | QRegularExpressionMatch::~QRegularExpressionMatch() | - |
1945 | { | - |
1946 | } | - |
1947 | - | |
1948 | /*! | - |
1949 | Constructs a match result by copying the result of the given \a match. | - |
1950 | - | |
1951 | \sa operator=() | - |
1952 | */ | - |
1953 | QRegularExpressionMatch::QRegularExpressionMatch(const QRegularExpressionMatch &match) | - |
1954 | : d(match.d) | - |
1955 | { | - |
1956 | } | - |
1957 | - | |
1958 | /*! | - |
1959 | Assigns the match result \a match to this object, and returns a reference | - |
1960 | to the copy. | - |
1961 | */ | - |
1962 | QRegularExpressionMatch &QRegularExpressionMatch::operator=(const QRegularExpressionMatch &match) | - |
1963 | { | - |
1964 | d = match.d; | - |
1965 | return *this; | - |
1966 | } | - |
1967 | - | |
1968 | /*! | - |
1969 | \fn QRegularExpressionMatch &QRegularExpressionMatch::operator=(QRegularExpressionMatch &&match) | - |
1970 | - | |
1971 | Move-assigns the match result \a match to this object, and returns a reference | - |
1972 | to the copy. | - |
1973 | */ | - |
1974 | - | |
1975 | /*! | - |
1976 | \fn void QRegularExpressionMatch::swap(QRegularExpressionMatch &other) | - |
1977 | - | |
1978 | Swaps the match result \a other with this match result. This | - |
1979 | operation is very fast and never fails. | - |
1980 | */ | - |
1981 | - | |
1982 | /*! | - |
1983 | \internal | - |
1984 | */ | - |
1985 | QRegularExpressionMatch::QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd) | - |
1986 | : d(&dd) | - |
1987 | { | - |
1988 | } | - |
1989 | - | |
1990 | /*! | - |
1991 | Returns the QRegularExpression object whose match() function returned this | - |
1992 | object. | - |
1993 | - | |
1994 | \sa QRegularExpression::match(), matchType(), matchOptions() | - |
1995 | */ | - |
1996 | QRegularExpression QRegularExpressionMatch::regularExpression() const | - |
1997 | { | - |
1998 | return d->regularExpression; | - |
1999 | } | - |
2000 | - | |
2001 | - | |
2002 | /*! | - |
2003 | Returns the match type that was used to get this QRegularExpressionMatch | - |
2004 | object, that is, the match type that was passed to | - |
2005 | QRegularExpression::match() or QRegularExpression::globalMatch(). | - |
2006 | - | |
2007 | \sa QRegularExpression::match(), regularExpression(), matchOptions() | - |
2008 | */ | - |
2009 | QRegularExpression::MatchType QRegularExpressionMatch::matchType() const | - |
2010 | { | - |
2011 | return d->matchType; | - |
2012 | } | - |
2013 | - | |
2014 | /*! | - |
2015 | Returns the match options that were used to get this | - |
2016 | QRegularExpressionMatch object, that is, the match options that were passed | - |
2017 | to QRegularExpression::match() or QRegularExpression::globalMatch(). | - |
2018 | - | |
2019 | \sa QRegularExpression::match(), regularExpression(), matchType() | - |
2020 | */ | - |
2021 | QRegularExpression::MatchOptions QRegularExpressionMatch::matchOptions() const | - |
2022 | { | - |
2023 | return d->matchOptions; | - |
2024 | } | - |
2025 | - | |
2026 | /*! | - |
2027 | Returns the index of the last capturing group that captured something, | - |
2028 | including the implicit capturing group 0. This can be used to extract all | - |
2029 | the substrings that were captured: | - |
2030 | - | |
2031 | \snippet code/src_corelib_tools_qregularexpression.cpp 28 | - |
2032 | - | |
2033 | Note that some of the capturing groups with an index less than | - |
2034 | lastCapturedIndex() could have not matched, and therefore captured nothing. | - |
2035 | - | |
2036 | If the regular expression did not match, this function returns -1. | - |
2037 | - | |
2038 | \sa captured(), capturedStart(), capturedEnd(), capturedLength() | - |
2039 | */ | - |
2040 | int QRegularExpressionMatch::lastCapturedIndex() const | - |
2041 | { | - |
2042 | return d->capturedCount - 1; | - |
2043 | } | - |
2044 | - | |
2045 | /*! | - |
2046 | Returns the substring captured by the \a nth capturing group. If the \a nth | - |
2047 | capturing group did not capture a string or doesn't exist, returns a null | - |
2048 | QString. | - |
2049 | - | |
2050 | \sa capturedRef(), lastCapturedIndex(), capturedStart(), capturedEnd(), | - |
2051 | capturedLength(), QString::isNull() | - |
2052 | */ | - |
2053 | QString QRegularExpressionMatch::captured(int nth) const | - |
2054 | { | - |
2055 | if (nth < 0 || nth > lastCapturedIndex()) | - |
2056 | return QString(); | - |
2057 | - | |
2058 | int start = capturedStart(nth); | - |
2059 | - | |
2060 | if (start == -1) // didn't capture | - |
2061 | return QString(); | - |
2062 | - | |
2063 | return d->subject.mid(start + d->subjectStart, capturedLength(nth)); | - |
2064 | } | - |
2065 | - | |
2066 | /*! | - |
2067 | Returns a reference to the substring captured by the \a nth capturing group. | - |
2068 | If the \a nth capturing group did not capture a string or doesn't exist, | - |
2069 | returns a null QStringRef. | - |
2070 | - | |
2071 | \sa captured(), lastCapturedIndex(), capturedStart(), capturedEnd(), | - |
2072 | capturedLength(), QStringRef::isNull() | - |
2073 | */ | - |
2074 | QStringRef QRegularExpressionMatch::capturedRef(int nth) const | - |
2075 | { | - |
2076 | if (nth < 0 || nth > lastCapturedIndex()) | - |
2077 | return QStringRef(); | - |
2078 | - | |
2079 | int start = capturedStart(nth); | - |
2080 | - | |
2081 | if (start == -1) // didn't capture | - |
2082 | return QStringRef(); | - |
2083 | - | |
2084 | return d->subject.midRef(start + d->subjectStart, capturedLength(nth)); | - |
2085 | } | - |
2086 | - | |
2087 | /*! | - |
2088 | Returns the substring captured by the capturing group named \a name. If the | - |
2089 | capturing group named \a name did not capture a string or doesn't exist, | - |
2090 | returns a null QString. | - |
2091 | - | |
2092 | \sa capturedRef(), capturedStart(), capturedEnd(), capturedLength(), | - |
2093 | QString::isNull() | - |
2094 | */ | - |
2095 | QString QRegularExpressionMatch::captured(const QString &name) const | - |
2096 | { | - |
2097 | if (name.isEmpty()) { | - |
2098 | qWarning("QRegularExpressionMatch::captured: empty capturing group name passed"); | - |
2099 | return QString(); | - |
2100 | } | - |
2101 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
2102 | if (nth == -1) | - |
2103 | return QString(); | - |
2104 | return captured(nth); | - |
2105 | } | - |
2106 | - | |
2107 | /*! | - |
2108 | Returns a reference to the string captured by the capturing group named \a | - |
2109 | name. If the capturing group named \a name did not capture a string or | - |
2110 | doesn't exist, returns a null QStringRef. | - |
2111 | - | |
2112 | \sa captured(), capturedStart(), capturedEnd(), capturedLength(), | - |
2113 | QStringRef::isNull() | - |
2114 | */ | - |
2115 | QStringRef QRegularExpressionMatch::capturedRef(const QString &name) const | - |
2116 | { | - |
2117 | if (name.isEmpty()) { | - |
2118 | qWarning("QRegularExpressionMatch::capturedRef: empty capturing group name passed"); | - |
2119 | return QStringRef(); | - |
2120 | } | - |
2121 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
2122 | if (nth == -1) | - |
2123 | return QStringRef(); | - |
2124 | return capturedRef(nth); | - |
2125 | } | - |
2126 | - | |
2127 | /*! | - |
2128 | Returns a list of all strings captured by capturing groups, in the order | - |
2129 | the groups themselves appear in the pattern string. | - |
2130 | */ | - |
2131 | QStringList QRegularExpressionMatch::capturedTexts() const | - |
2132 | { | - |
2133 | QStringList texts; | - |
2134 | texts.reserve(d->capturedCount); | - |
2135 | for (int i = 0; i < d->capturedCount; ++i) | - |
2136 | texts << captured(i); | - |
2137 | return texts; | - |
2138 | } | - |
2139 | - | |
2140 | /*! | - |
2141 | Returns the offset inside the subject string corresponding to the | - |
2142 | starting position of the substring captured by the \a nth capturing group. | - |
2143 | If the \a nth capturing group did not capture a string or doesn't exist, | - |
2144 | returns -1. | - |
2145 | - | |
2146 | \sa capturedEnd(), capturedLength(), captured() | - |
2147 | */ | - |
2148 | int QRegularExpressionMatch::capturedStart(int nth) const | - |
2149 | { | - |
2150 | if (nth < 0 || nth > lastCapturedIndex()) | - |
2151 | return -1; | - |
2152 | - | |
2153 | return d->capturedOffsets.at(nth * 2); | - |
2154 | } | - |
2155 | - | |
2156 | /*! | - |
2157 | Returns the length of the substring captured by the \a nth capturing group. | - |
2158 | - | |
2159 | \note This function returns 0 if the \a nth capturing group did not capture | - |
2160 | a string or doesn't exist. | - |
2161 | - | |
2162 | \sa capturedStart(), capturedEnd(), captured() | - |
2163 | */ | - |
2164 | int QRegularExpressionMatch::capturedLength(int nth) const | - |
2165 | { | - |
2166 | // bound checking performed by these two functions | - |
2167 | return capturedEnd(nth) - capturedStart(nth); | - |
2168 | } | - |
2169 | - | |
2170 | /*! | - |
2171 | Returns the offset inside the subject string immediately after the ending | - |
2172 | position of the substring captured by the \a nth capturing group. If the \a | - |
2173 | nth capturing group did not capture a string or doesn't exist, returns -1. | - |
2174 | - | |
2175 | \sa capturedStart(), capturedLength(), captured() | - |
2176 | */ | - |
2177 | int QRegularExpressionMatch::capturedEnd(int nth) const | - |
2178 | { | - |
2179 | if (nth < 0 || nth > lastCapturedIndex()) | - |
2180 | return -1; | - |
2181 | - | |
2182 | return d->capturedOffsets.at(nth * 2 + 1); | - |
2183 | } | - |
2184 | - | |
2185 | /*! | - |
2186 | Returns the offset inside the subject string corresponding to the starting | - |
2187 | position of the substring captured by the capturing group named \a name. | - |
2188 | If the capturing group named \a name did not capture a string or doesn't | - |
2189 | exist, returns -1. | - |
2190 | - | |
2191 | \sa capturedEnd(), capturedLength(), captured() | - |
2192 | */ | - |
2193 | int QRegularExpressionMatch::capturedStart(const QString &name) const | - |
2194 | { | - |
2195 | if (name.isEmpty()) { | - |
2196 | qWarning("QRegularExpressionMatch::capturedStart: empty capturing group name passed"); | - |
2197 | return -1; | - |
2198 | } | - |
2199 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
2200 | if (nth == -1) | - |
2201 | return -1; | - |
2202 | return capturedStart(nth); | - |
2203 | } | - |
2204 | - | |
2205 | /*! | - |
2206 | Returns the offset inside the subject string corresponding to the starting | - |
2207 | position of the substring captured by the capturing group named \a name. | - |
2208 | - | |
2209 | \note This function returns 0 if the capturing group named \a name did not | - |
2210 | capture a string or doesn't exist. | - |
2211 | - | |
2212 | \sa capturedStart(), capturedEnd(), captured() | - |
2213 | */ | - |
2214 | int QRegularExpressionMatch::capturedLength(const QString &name) const | - |
2215 | { | - |
2216 | if (name.isEmpty()) { | - |
2217 | qWarning("QRegularExpressionMatch::capturedLength: empty capturing group name passed"); | - |
2218 | return 0; | - |
2219 | } | - |
2220 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
2221 | if (nth == -1) | - |
2222 | return 0; | - |
2223 | return capturedLength(nth); | - |
2224 | } | - |
2225 | - | |
2226 | /*! | - |
2227 | Returns the offset inside the subject string immediately after the ending | - |
2228 | position of the substring captured by the capturing group named \a name. If | - |
2229 | the capturing group named \a name did not capture a string or doesn't | - |
2230 | exist, returns -1. | - |
2231 | - | |
2232 | \sa capturedStart(), capturedLength(), captured() | - |
2233 | */ | - |
2234 | int QRegularExpressionMatch::capturedEnd(const QString &name) const | - |
2235 | { | - |
2236 | if (name.isEmpty()) { | - |
2237 | qWarning("QRegularExpressionMatch::capturedEnd: empty capturing group name passed"); | - |
2238 | return -1; | - |
2239 | } | - |
2240 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
2241 | if (nth == -1) | - |
2242 | return -1; | - |
2243 | return capturedEnd(nth); | - |
2244 | } | - |
2245 | - | |
2246 | /*! | - |
2247 | Returns \c true if the regular expression matched against the subject string, | - |
2248 | or false otherwise. | - |
2249 | - | |
2250 | \sa QRegularExpression::match(), hasPartialMatch() | - |
2251 | */ | - |
2252 | bool QRegularExpressionMatch::hasMatch() const | - |
2253 | { | - |
2254 | return d->hasMatch; | - |
2255 | } | - |
2256 | - | |
2257 | /*! | - |
2258 | Returns \c true if the regular expression partially matched against the | - |
2259 | subject string, or false otherwise. | - |
2260 | - | |
2261 | \note Only a match that explicitly used the one of the partial match types | - |
2262 | can yield a partial match. Still, if such a match succeeds totally, this | - |
2263 | function will return false, while hasMatch() will return true. | - |
2264 | - | |
2265 | \sa QRegularExpression::match(), QRegularExpression::MatchType, hasMatch() | - |
2266 | */ | - |
2267 | bool QRegularExpressionMatch::hasPartialMatch() const | - |
2268 | { | - |
2269 | return d->hasPartialMatch; | - |
2270 | } | - |
2271 | - | |
2272 | /*! | - |
2273 | Returns \c true if the match object was obtained as a result from the | - |
2274 | QRegularExpression::match() function invoked on a valid QRegularExpression | - |
2275 | object; returns \c false if the QRegularExpression was invalid. | - |
2276 | - | |
2277 | \sa QRegularExpression::match(), QRegularExpression::isValid() | - |
2278 | */ | - |
2279 | bool QRegularExpressionMatch::isValid() const | - |
2280 | { | - |
2281 | return d->isValid; | - |
2282 | } | - |
2283 | - | |
2284 | /*! | - |
2285 | \internal | - |
2286 | */ | - |
2287 | QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd) | - |
2288 | : d(&dd) | - |
2289 | { | - |
2290 | } | - |
2291 | - | |
2292 | /*! | - |
2293 | \since 5.1 | - |
2294 | - | |
2295 | Constructs an empty, valid QRegularExpressionMatchIterator object. The | - |
2296 | regular expression is set to a default-constructed one; the match type to | - |
2297 | QRegularExpression::NoMatch and the match options to | - |
2298 | QRegularExpression::NoMatchOption. | - |
2299 | - | |
2300 | Invoking the hasNext() member function on the constructed object will | - |
2301 | return false, as the iterator is not iterating on a valid sequence of | - |
2302 | matches. | - |
2303 | */ | - |
2304 | QRegularExpressionMatchIterator::QRegularExpressionMatchIterator() | - |
2305 | : d(new QRegularExpressionMatchIteratorPrivate(QRegularExpression(), | - |
2306 | QRegularExpression::NoMatch, | - |
2307 | QRegularExpression::NoMatchOption, | - |
2308 | QRegularExpressionMatch())) | - |
2309 | { | - |
2310 | } | - |
2311 | - | |
2312 | /*! | - |
2313 | Destroys the QRegularExpressionMatchIterator object. | - |
2314 | */ | - |
2315 | QRegularExpressionMatchIterator::~QRegularExpressionMatchIterator() | - |
2316 | { | - |
2317 | } | - |
2318 | - | |
2319 | /*! | - |
2320 | Constructs a QRegularExpressionMatchIterator object as a copy of \a | - |
2321 | iterator. | - |
2322 | - | |
2323 | \sa operator=() | - |
2324 | */ | - |
2325 | QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator) | - |
2326 | : d(iterator.d) | - |
2327 | { | - |
2328 | } | - |
2329 | - | |
2330 | /*! | - |
2331 | Assigns the iterator \a iterator to this object, and returns a reference to | - |
2332 | the copy. | - |
2333 | */ | - |
2334 | QRegularExpressionMatchIterator &QRegularExpressionMatchIterator::operator=(const QRegularExpressionMatchIterator &iterator) | - |
2335 | { | - |
2336 | d = iterator.d; | - |
2337 | return *this; | - |
2338 | } | - |
2339 | - | |
2340 | /*! | - |
2341 | \fn QRegularExpressionMatchIterator &QRegularExpressionMatchIterator::operator=(QRegularExpressionMatchIterator &&iterator) | - |
2342 | - | |
2343 | Move-assigns the \a iterator to this object. | - |
2344 | */ | - |
2345 | - | |
2346 | /*! | - |
2347 | \fn void QRegularExpressionMatchIterator::swap(QRegularExpressionMatchIterator &other) | - |
2348 | - | |
2349 | Swaps the iterator \a other with this iterator object. This operation is | - |
2350 | very fast and never fails. | - |
2351 | */ | - |
2352 | - | |
2353 | /*! | - |
2354 | Returns \c true if the iterator object was obtained as a result from the | - |
2355 | QRegularExpression::globalMatch() function invoked on a valid | - |
2356 | QRegularExpression object; returns \c false if the QRegularExpression was | - |
2357 | invalid. | - |
2358 | - | |
2359 | \sa QRegularExpression::globalMatch(), QRegularExpression::isValid() | - |
2360 | */ | - |
2361 | bool QRegularExpressionMatchIterator::isValid() const | - |
2362 | { | - |
2363 | return d->next.isValid(); | - |
2364 | } | - |
2365 | - | |
2366 | /*! | - |
2367 | Returns \c true if there is at least one match result ahead of the iterator; | - |
2368 | otherwise it returns \c false. | - |
2369 | - | |
2370 | \sa next() | - |
2371 | */ | - |
2372 | bool QRegularExpressionMatchIterator::hasNext() const | - |
2373 | { | - |
2374 | return d->hasNext(); | - |
2375 | } | - |
2376 | - | |
2377 | /*! | - |
2378 | Returns the next match result without moving the iterator. | - |
2379 | - | |
2380 | \note Calling this function when the iterator is at the end of the result | - |
2381 | set leads to undefined results. | - |
2382 | */ | - |
2383 | QRegularExpressionMatch QRegularExpressionMatchIterator::peekNext() const | - |
2384 | { | - |
2385 | if (!hasNext()) | - |
2386 | qWarning("QRegularExpressionMatchIterator::peekNext() called on an iterator already at end"); | - |
2387 | - | |
2388 | return d->next; | - |
2389 | } | - |
2390 | - | |
2391 | /*! | - |
2392 | Returns the next match result and advances the iterator by one position. | - |
2393 | - | |
2394 | \note Calling this function when the iterator is at the end of the result | - |
2395 | set leads to undefined results. | - |
2396 | */ | - |
2397 | QRegularExpressionMatch QRegularExpressionMatchIterator::next() | - |
2398 | { | - |
2399 | if (!hasNext()) { | - |
2400 | qWarning("QRegularExpressionMatchIterator::next() called on an iterator already at end"); | - |
2401 | return d->next; | - |
2402 | } | - |
2403 | - | |
2404 | QRegularExpressionMatch current = d->next; | - |
2405 | d->next = d->next.d.constData()->nextMatch(); | - |
2406 | return current; | - |
2407 | } | - |
2408 | - | |
2409 | /*! | - |
2410 | Returns the QRegularExpression object whose globalMatch() function returned | - |
2411 | this object. | - |
2412 | - | |
2413 | \sa QRegularExpression::globalMatch(), matchType(), matchOptions() | - |
2414 | */ | - |
2415 | QRegularExpression QRegularExpressionMatchIterator::regularExpression() const | - |
2416 | { | - |
2417 | return d->regularExpression; | - |
2418 | } | - |
2419 | - | |
2420 | /*! | - |
2421 | Returns the match type that was used to get this | - |
2422 | QRegularExpressionMatchIterator object, that is, the match type that was | - |
2423 | passed to QRegularExpression::globalMatch(). | - |
2424 | - | |
2425 | \sa QRegularExpression::globalMatch(), regularExpression(), matchOptions() | - |
2426 | */ | - |
2427 | QRegularExpression::MatchType QRegularExpressionMatchIterator::matchType() const | - |
2428 | { | - |
2429 | return d->matchType; | - |
2430 | } | - |
2431 | - | |
2432 | /*! | - |
2433 | Returns the match options that were used to get this | - |
2434 | QRegularExpressionMatchIterator object, that is, the match options that | - |
2435 | were passed to QRegularExpression::globalMatch(). | - |
2436 | - | |
2437 | \sa QRegularExpression::globalMatch(), regularExpression(), matchType() | - |
2438 | */ | - |
2439 | QRegularExpression::MatchOptions QRegularExpressionMatchIterator::matchOptions() const | - |
2440 | { | - |
2441 | return d->matchOptions; | - |
2442 | } | - |
2443 | - | |
2444 | #ifndef QT_NO_DATASTREAM | - |
2445 | /*! | - |
2446 | \relates QRegularExpression | - |
2447 | - | |
2448 | Writes the regular expression \a re to stream \a out. | - |
2449 | - | |
2450 | \sa {Serializing Qt Data Types} | - |
2451 | */ | - |
2452 | QDataStream &operator<<(QDataStream &out, const QRegularExpression &re) | - |
2453 | { | - |
2454 | out << re.pattern() << quint32(re.patternOptions()); | - |
2455 | return out; | - |
2456 | } | - |
2457 | - | |
2458 | /*! | - |
2459 | \relates QRegularExpression | - |
2460 | - | |
2461 | Reads a regular expression from stream \a in into \a re. | - |
2462 | - | |
2463 | \sa {Serializing Qt Data Types} | - |
2464 | */ | - |
2465 | QDataStream &operator>>(QDataStream &in, QRegularExpression &re) | - |
2466 | { | - |
2467 | QString pattern; | - |
2468 | quint32 patternOptions; | - |
2469 | in >> pattern >> patternOptions; | - |
2470 | re.setPattern(pattern); | - |
2471 | re.setPatternOptions(QRegularExpression::PatternOptions(patternOptions)); | - |
2472 | return in; | - |
2473 | } | - |
2474 | #endif | - |
2475 | - | |
2476 | #ifndef QT_NO_DEBUG_STREAM | - |
2477 | /*! | - |
2478 | \relates QRegularExpression | - |
2479 | - | |
2480 | Writes the regular expression \a re into the debug object \a debug for | - |
2481 | debugging purposes. | - |
2482 | - | |
2483 | \sa {Debugging Techniques} | - |
2484 | */ | - |
2485 | QDebug operator<<(QDebug debug, const QRegularExpression &re) | - |
2486 | { | - |
2487 | QDebugStateSaver saver(debug); | - |
2488 | debug.nospace() << "QRegularExpression(" << re.pattern() << ", " << re.patternOptions() << ')'; | - |
2489 | return debug; | - |
2490 | } | - |
2491 | - | |
2492 | /*! | - |
2493 | \relates QRegularExpression | - |
2494 | - | |
2495 | Writes the pattern options \a patternOptions into the debug object \a debug | - |
2496 | for debugging purposes. | - |
2497 | - | |
2498 | \sa {Debugging Techniques} | - |
2499 | */ | - |
2500 | QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions) | - |
2501 | { | - |
2502 | QDebugStateSaver saver(debug); | - |
2503 | QByteArray flags; | - |
2504 | - | |
2505 | if (patternOptions == QRegularExpression::NoPatternOption) { | - |
2506 | flags = "NoPatternOption"; | - |
2507 | } else { | - |
2508 | flags.reserve(200); // worst case... | - |
2509 | if (patternOptions & QRegularExpression::CaseInsensitiveOption) | - |
2510 | flags.append("CaseInsensitiveOption|"); | - |
2511 | if (patternOptions & QRegularExpression::DotMatchesEverythingOption) | - |
2512 | flags.append("DotMatchesEverythingOption|"); | - |
2513 | if (patternOptions & QRegularExpression::MultilineOption) | - |
2514 | flags.append("MultilineOption|"); | - |
2515 | if (patternOptions & QRegularExpression::ExtendedPatternSyntaxOption) | - |
2516 | flags.append("ExtendedPatternSyntaxOption|"); | - |
2517 | if (patternOptions & QRegularExpression::InvertedGreedinessOption) | - |
2518 | flags.append("InvertedGreedinessOption|"); | - |
2519 | if (patternOptions & QRegularExpression::DontCaptureOption) | - |
2520 | flags.append("DontCaptureOption|"); | - |
2521 | if (patternOptions & QRegularExpression::UseUnicodePropertiesOption) | - |
2522 | flags.append("UseUnicodePropertiesOption|"); | - |
2523 | if (patternOptions & QRegularExpression::OptimizeOnFirstUsageOption) | - |
2524 | flags.append("OptimizeOnFirstUsageOption|"); | - |
2525 | if (patternOptions & QRegularExpression::DontAutomaticallyOptimizeOption) | - |
2526 | flags.append("DontAutomaticallyOptimizeOption|"); | - |
2527 | flags.chop(1); | - |
2528 | } | - |
2529 | - | |
2530 | debug.nospace() << "QRegularExpression::PatternOptions(" << flags << ')'; | - |
2531 | - | |
2532 | return debug; | - |
2533 | } | - |
2534 | /*! | - |
2535 | \relates QRegularExpressionMatch | - |
2536 | - | |
2537 | Writes the match object \a match into the debug object \a debug for | - |
2538 | debugging purposes. | - |
2539 | - | |
2540 | \sa {Debugging Techniques} | - |
2541 | */ | - |
2542 | QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match) | - |
2543 | { | - |
2544 | QDebugStateSaver saver(debug); | - |
2545 | debug.nospace() << "QRegularExpressionMatch("; | - |
2546 | - | |
2547 | if (!match.isValid()) { | - |
2548 | debug << "Invalid)"; | - |
2549 | return debug; | - |
2550 | } | - |
2551 | - | |
2552 | debug << "Valid"; | - |
2553 | - | |
2554 | if (match.hasMatch()) { | - |
2555 | debug << ", has match: "; | - |
2556 | for (int i = 0; i <= match.lastCapturedIndex(); ++i) { | - |
2557 | debug << i | - |
2558 | << ":(" << match.capturedStart(i) << ", " << match.capturedEnd(i) | - |
2559 | << ", " << match.captured(i) << ')'; | - |
2560 | if (i < match.lastCapturedIndex()) | - |
2561 | debug << ", "; | - |
2562 | } | - |
2563 | } else if (match.hasPartialMatch()) { | - |
2564 | debug << ", has partial match: (" | - |
2565 | << match.capturedStart(0) << ", " | - |
2566 | << match.capturedEnd(0) << ", " | - |
2567 | << match.captured(0) << ')'; | - |
2568 | } else { | - |
2569 | debug << ", no match"; | - |
2570 | } | - |
2571 | - | |
2572 | debug << ')'; | - |
2573 | - | |
2574 | return debug; | - |
2575 | } | - |
2576 | #endif | - |
2577 | - | |
2578 | // fool lupdate: make it extract those strings for translation, but don't put them | - |
2579 | // inside Qt -- they're already inside libpcre (cf. man 3 pcreapi, pcre_compile.c). | - |
2580 | #if 0 | - |
2581 | - | |
2582 | /* PCRE is a library of functions to support regular expressions whose syntax | - |
2583 | and semantics are as close as possible to those of the Perl 5 language. | - |
2584 | - | |
2585 | Written by Philip Hazel | - |
2586 | Copyright (c) 1997-2012 University of Cambridge | - |
2587 | - | |
2588 | ----------------------------------------------------------------------------- | - |
2589 | Redistribution and use in source and binary forms, with or without | - |
2590 | modification, are permitted provided that the following conditions are met: | - |
2591 | - | |
2592 | * Redistributions of source code must retain the above copyright notice, | - |
2593 | this list of conditions and the following disclaimer. | - |
2594 | - | |
2595 | * Redistributions in binary form must reproduce the above copyright | - |
2596 | notice, this list of conditions and the following disclaimer in the | - |
2597 | documentation and/or other materials provided with the distribution. | - |
2598 | - | |
2599 | * Neither the name of the University of Cambridge nor the names of its | - |
2600 | contributors may be used to endorse or promote products derived from | - |
2601 | this software without specific prior written permission. | - |
2602 | - | |
2603 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | - |
2604 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | - |
2605 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | - |
2606 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | - |
2607 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | - |
2608 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | - |
2609 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | - |
2610 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | - |
2611 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | - |
2612 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | - |
2613 | POSSIBILITY OF SUCH DAMAGE. | - |
2614 | ----------------------------------------------------------------------------- | - |
2615 | */ | - |
2616 | - | |
2617 | static const char *pcreCompileErrorCodes[] = | - |
2618 | { | - |
2619 | QT_TRANSLATE_NOOP("QRegularExpression", "no error"), | - |
2620 | QT_TRANSLATE_NOOP("QRegularExpression", "\\ at end of pattern"), | - |
2621 | QT_TRANSLATE_NOOP("QRegularExpression", "\\c at end of pattern"), | - |
2622 | QT_TRANSLATE_NOOP("QRegularExpression", "unrecognized character follows \\"), | - |
2623 | QT_TRANSLATE_NOOP("QRegularExpression", "numbers out of order in {} quantifier"), | - |
2624 | QT_TRANSLATE_NOOP("QRegularExpression", "number too big in {} quantifier"), | - |
2625 | QT_TRANSLATE_NOOP("QRegularExpression", "missing terminating ] for character class"), | - |
2626 | QT_TRANSLATE_NOOP("QRegularExpression", "invalid escape sequence in character class"), | - |
2627 | QT_TRANSLATE_NOOP("QRegularExpression", "range out of order in character class"), | - |
2628 | QT_TRANSLATE_NOOP("QRegularExpression", "nothing to repeat"), | - |
2629 | QT_TRANSLATE_NOOP("QRegularExpression", "internal error: unexpected repeat"), | - |
2630 | QT_TRANSLATE_NOOP("QRegularExpression", "unrecognized character after (? or (?-"), | - |
2631 | QT_TRANSLATE_NOOP("QRegularExpression", "POSIX named classes are supported only within a class"), | - |
2632 | QT_TRANSLATE_NOOP("QRegularExpression", "missing )"), | - |
2633 | QT_TRANSLATE_NOOP("QRegularExpression", "reference to non-existent subpattern"), | - |
2634 | QT_TRANSLATE_NOOP("QRegularExpression", "erroffset passed as NULL"), | - |
2635 | QT_TRANSLATE_NOOP("QRegularExpression", "unknown option bit(s) set"), | - |
2636 | QT_TRANSLATE_NOOP("QRegularExpression", "missing ) after comment"), | - |
2637 | QT_TRANSLATE_NOOP("QRegularExpression", "regular expression is too large"), | - |
2638 | QT_TRANSLATE_NOOP("QRegularExpression", "failed to get memory"), | - |
2639 | QT_TRANSLATE_NOOP("QRegularExpression", "unmatched parentheses"), | - |
2640 | QT_TRANSLATE_NOOP("QRegularExpression", "internal error: code overflow"), | - |
2641 | QT_TRANSLATE_NOOP("QRegularExpression", "unrecognized character after (?<"), | - |
2642 | QT_TRANSLATE_NOOP("QRegularExpression", "lookbehind assertion is not fixed length"), | - |
2643 | QT_TRANSLATE_NOOP("QRegularExpression", "malformed number or name after (?("), | - |
2644 | QT_TRANSLATE_NOOP("QRegularExpression", "conditional group contains more than two branches"), | - |
2645 | QT_TRANSLATE_NOOP("QRegularExpression", "assertion expected after (?("), | - |
2646 | QT_TRANSLATE_NOOP("QRegularExpression", "(?R or (?[+-]digits must be followed by )"), | - |
2647 | QT_TRANSLATE_NOOP("QRegularExpression", "unknown POSIX class name"), | - |
2648 | QT_TRANSLATE_NOOP("QRegularExpression", "POSIX collating elements are not supported"), | - |
2649 | QT_TRANSLATE_NOOP("QRegularExpression", "this version of PCRE is not compiled with PCRE_UTF8 support"), | - |
2650 | QT_TRANSLATE_NOOP("QRegularExpression", "character value in \\x{...} sequence is too large"), | - |
2651 | QT_TRANSLATE_NOOP("QRegularExpression", "invalid condition (?(0)"), | - |
2652 | QT_TRANSLATE_NOOP("QRegularExpression", "\\C not allowed in lookbehind assertion"), | - |
2653 | QT_TRANSLATE_NOOP("QRegularExpression", "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u"), | - |
2654 | QT_TRANSLATE_NOOP("QRegularExpression", "number after (?C is > 255"), | - |
2655 | QT_TRANSLATE_NOOP("QRegularExpression", "closing ) for (?C expected"), | - |
2656 | QT_TRANSLATE_NOOP("QRegularExpression", "recursive call could loop indefinitely"), | - |
2657 | QT_TRANSLATE_NOOP("QRegularExpression", "unrecognized character after (?P"), | - |
2658 | QT_TRANSLATE_NOOP("QRegularExpression", "syntax error in subpattern name (missing terminator)"), | - |
2659 | QT_TRANSLATE_NOOP("QRegularExpression", "two named subpatterns have the same name"), | - |
2660 | QT_TRANSLATE_NOOP("QRegularExpression", "invalid UTF-8 string"), | - |
2661 | QT_TRANSLATE_NOOP("QRegularExpression", "support for \\P, \\p, and \\X has not been compiled"), | - |
2662 | QT_TRANSLATE_NOOP("QRegularExpression", "malformed \\P or \\p sequence"), | - |
2663 | QT_TRANSLATE_NOOP("QRegularExpression", "unknown property name after \\P or \\p"), | - |
2664 | QT_TRANSLATE_NOOP("QRegularExpression", "subpattern name is too long (maximum 32 characters)"), | - |
2665 | QT_TRANSLATE_NOOP("QRegularExpression", "too many named subpatterns (maximum 10000)"), | - |
2666 | QT_TRANSLATE_NOOP("QRegularExpression", "octal value is greater than \\377 (not in UTF-8 mode)"), | - |
2667 | QT_TRANSLATE_NOOP("QRegularExpression", "internal error: overran compiling workspace"), | - |
2668 | QT_TRANSLATE_NOOP("QRegularExpression", "internal error: previously-checked referenced subpattern not found"), | - |
2669 | QT_TRANSLATE_NOOP("QRegularExpression", "DEFINE group contains more than one branch"), | - |
2670 | QT_TRANSLATE_NOOP("QRegularExpression", "repeating a DEFINE group is not allowed"), | - |
2671 | QT_TRANSLATE_NOOP("QRegularExpression", "inconsistent NEWLINE options"), | - |
2672 | QT_TRANSLATE_NOOP("QRegularExpression", "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number"), | - |
2673 | QT_TRANSLATE_NOOP("QRegularExpression", "a numbered reference must not be zero"), | - |
2674 | QT_TRANSLATE_NOOP("QRegularExpression", "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)"), | - |
2675 | QT_TRANSLATE_NOOP("QRegularExpression", "(*VERB) not recognized"), | - |
2676 | QT_TRANSLATE_NOOP("QRegularExpression", "number is too big"), | - |
2677 | QT_TRANSLATE_NOOP("QRegularExpression", "subpattern name expected"), | - |
2678 | QT_TRANSLATE_NOOP("QRegularExpression", "digit expected after (?+"), | - |
2679 | QT_TRANSLATE_NOOP("QRegularExpression", "] is an invalid data character in JavaScript compatibility mode"), | - |
2680 | QT_TRANSLATE_NOOP("QRegularExpression", "different names for subpatterns of the same number are not allowed"), | - |
2681 | QT_TRANSLATE_NOOP("QRegularExpression", "(*MARK) must have an argument"), | - |
2682 | QT_TRANSLATE_NOOP("QRegularExpression", "this version of PCRE is not compiled with PCRE_UCP support"), | - |
2683 | QT_TRANSLATE_NOOP("QRegularExpression", "\\c must be followed by an ASCII character"), | - |
2684 | QT_TRANSLATE_NOOP("QRegularExpression", "\\k is not followed by a braced, angle-bracketed, or quoted name"), | - |
2685 | QT_TRANSLATE_NOOP("QRegularExpression", "internal error: unknown opcode in find_fixedlength()"), | - |
2686 | QT_TRANSLATE_NOOP("QRegularExpression", "\\N is not supported in a class"), | - |
2687 | QT_TRANSLATE_NOOP("QRegularExpression", "too many forward references"), | - |
2688 | QT_TRANSLATE_NOOP("QRegularExpression", "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)"), | - |
2689 | QT_TRANSLATE_NOOP("QRegularExpression", "invalid UTF-16 string"), | - |
2690 | QT_TRANSLATE_NOOP("QRegularExpression", "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)"), | - |
2691 | QT_TRANSLATE_NOOP("QRegularExpression", "character value in \\u.... sequence is too large"), | - |
2692 | QT_TRANSLATE_NOOP("QRegularExpression", "invalid UTF-32 string"), | - |
2693 | QT_TRANSLATE_NOOP("QRegularExpression", "setting UTF is disabled by the application"), | - |
2694 | QT_TRANSLATE_NOOP("QRegularExpression", "non-hex character in \\x{} (closing brace missing?)"), | - |
2695 | QT_TRANSLATE_NOOP("QRegularExpression", "non-octal character in \\o{} (closing brace missing?)"), | - |
2696 | QT_TRANSLATE_NOOP("QRegularExpression", "missing opening brace after \\o"), | - |
2697 | QT_TRANSLATE_NOOP("QRegularExpression", "parentheses are too deeply nested"), | - |
2698 | QT_TRANSLATE_NOOP("QRegularExpression", "invalid range in character class"), | - |
2699 | QT_TRANSLATE_NOOP("QRegularExpression", "group name must start with a non-digit"), | - |
2700 | QT_TRANSLATE_NOOP("QRegularExpression", "parentheses are too deeply nested (stack check)"), | - |
2701 | QT_TRANSLATE_NOOP("QRegularExpression", "digits missing in \\x{} or \\o{}") | - |
2702 | }; | - |
2703 | #endif // #if 0 | - |
2704 | - | |
2705 | QT_END_NAMESPACE | - |
2706 | - | |
2707 | #endif // QT_NO_REGULAREXPRESSION | - |
Source code | Switch to Preprocessed file |