Line | Source Code | Coverage |
---|
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | static const unsigned int qt_qregularexpression_optimize_after_use_count = 10; | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | static int convertToPcreOptions(QRegularExpression::PatternOptions patternOptions) | - |
12 | { | - |
13 | int options = 0; | - |
14 | | - |
15 | if (patternOptions & QRegularExpression::CaseInsensitiveOption) | - |
16 | options |= 0x00000001; | - |
17 | if (patternOptions & QRegularExpression::DotMatchesEverythingOption) | - |
18 | options |= 0x00000004; | - |
19 | if (patternOptions & QRegularExpression::MultilineOption) | - |
20 | options |= 0x00000002; | - |
21 | if (patternOptions & QRegularExpression::ExtendedPatternSyntaxOption) | - |
22 | options |= 0x00000008; | - |
23 | if (patternOptions & QRegularExpression::InvertedGreedinessOption) | - |
24 | options |= 0x00000200; | - |
25 | if (patternOptions & QRegularExpression::DontCaptureOption) | - |
26 | options |= 0x00001000; | - |
27 | if (patternOptions & QRegularExpression::UseUnicodePropertiesOption) | - |
28 | options |= 0x20000000; | - |
29 | | - |
30 | return options; | - |
31 | } | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | static int convertToPcreOptions(QRegularExpression::MatchOptions matchOptions) | - |
37 | { | - |
38 | int options = 0; | - |
39 | | - |
40 | if (matchOptions & QRegularExpression::AnchoredMatchOption) | - |
41 | options |= 0x00000010; | - |
42 | | - |
43 | return options; | - |
44 | } | - |
45 | | - |
46 | struct QRegularExpressionPrivate : QSharedData | - |
47 | { | - |
48 | QRegularExpressionPrivate(); | - |
49 | ~QRegularExpressionPrivate(); | - |
50 | QRegularExpressionPrivate(const QRegularExpressionPrivate &other); | - |
51 | | - |
52 | void cleanCompiledPattern(); | - |
53 | void compilePattern(); | - |
54 | void getPatternInfo(); | - |
55 | pcre16_extra *optimizePattern(); | - |
56 | | - |
57 | QRegularExpressionMatchPrivate *doMatch(const QString &subject, | - |
58 | int offset, | - |
59 | QRegularExpression::MatchType matchType, | - |
60 | QRegularExpression::MatchOptions matchOptions, | - |
61 | bool checkSubjectString = true, | - |
62 | const QRegularExpressionMatchPrivate *previous = 0) const; | - |
63 | | - |
64 | int captureIndexForName(const QString &name) const; | - |
65 | | - |
66 | QString pattern; | - |
67 | QRegularExpression::PatternOptions patternOptions; | - |
68 | | - |
69 | | - |
70 | | - |
71 | | - |
72 | | - |
73 | | - |
74 | | - |
75 | QMutex mutex; | - |
76 | | - |
77 | | - |
78 | | - |
79 | | - |
80 | pcre16 *compiledPattern; | - |
81 | pcre16_extra *studyData; | - |
82 | const char *errorString; | - |
83 | int errorOffset; | - |
84 | int capturingCount; | - |
85 | unsigned int usedCount; | - |
86 | bool usingCrLfNewlines; | - |
87 | bool isDirty; | - |
88 | }; | - |
89 | | - |
90 | struct QRegularExpressionMatchPrivate : QSharedData | - |
91 | { | - |
92 | QRegularExpressionMatchPrivate(const QRegularExpression &re, | - |
93 | const QString &subject, | - |
94 | QRegularExpression::MatchType matchType, | - |
95 | QRegularExpression::MatchOptions matchOptions, | - |
96 | int capturingCount); | - |
97 | | - |
98 | QRegularExpressionMatch nextMatch() const; | - |
99 | | - |
100 | const QRegularExpression regularExpression; | - |
101 | const QString subject; | - |
102 | | - |
103 | | - |
104 | QVector<int> capturedOffsets; | - |
105 | | - |
106 | const QRegularExpression::MatchType matchType; | - |
107 | const QRegularExpression::MatchOptions matchOptions; | - |
108 | | - |
109 | int capturedCount; | - |
110 | | - |
111 | bool hasMatch; | - |
112 | bool hasPartialMatch; | - |
113 | bool isValid; | - |
114 | }; | - |
115 | | - |
116 | struct QRegularExpressionMatchIteratorPrivate : QSharedData | - |
117 | { | - |
118 | QRegularExpressionMatchIteratorPrivate(const QRegularExpression &re, | - |
119 | QRegularExpression::MatchType matchType, | - |
120 | QRegularExpression::MatchOptions matchOptions, | - |
121 | const QRegularExpressionMatch &next); | - |
122 | | - |
123 | bool hasNext() const; | - |
124 | QRegularExpressionMatch next; | - |
125 | const QRegularExpression regularExpression; | - |
126 | const QRegularExpression::MatchType matchType; | - |
127 | const QRegularExpression::MatchOptions matchOptions; | - |
128 | }; | - |
129 | | - |
130 | | - |
131 | | - |
132 | | - |
133 | QRegularExpression::QRegularExpression(QRegularExpressionPrivate &dd) | - |
134 | : d(&dd) | - |
135 | { | - |
136 | } | - |
137 | | - |
138 | | - |
139 | | - |
140 | | - |
141 | QRegularExpressionPrivate::QRegularExpressionPrivate() | - |
142 | : pattern(), patternOptions(0), | - |
143 | mutex(), | - |
144 | compiledPattern(0), studyData(0), | - |
145 | errorString(0), errorOffset(-1), | - |
146 | capturingCount(0), | - |
147 | usedCount(0), | - |
148 | usingCrLfNewlines(false), | - |
149 | isDirty(true) | - |
150 | { | - |
151 | } | - |
152 | | - |
153 | | - |
154 | | - |
155 | | - |
156 | QRegularExpressionPrivate::~QRegularExpressionPrivate() | - |
157 | { | - |
158 | cleanCompiledPattern(); | - |
159 | } | - |
160 | QRegularExpressionPrivate::QRegularExpressionPrivate(const QRegularExpressionPrivate &other) | - |
161 | : QSharedData(other), | - |
162 | pattern(other.pattern), patternOptions(other.patternOptions), | - |
163 | mutex(), | - |
164 | compiledPattern(0), studyData(0), | - |
165 | errorString(0), | - |
166 | errorOffset(-1), capturingCount(0), | - |
167 | usedCount(0), | - |
168 | usingCrLfNewlines(false), isDirty(true) | - |
169 | { | - |
170 | } | - |
171 | | - |
172 | | - |
173 | | - |
174 | | - |
175 | void QRegularExpressionPrivate::cleanCompiledPattern() | - |
176 | { | - |
177 | pcre16_free(compiledPattern); | - |
178 | pcre16_free_study(studyData); | - |
179 | usedCount = 0; | - |
180 | compiledPattern = 0; | - |
181 | studyData = 0; | - |
182 | usingCrLfNewlines = false; | - |
183 | errorOffset = -1; | - |
184 | capturingCount = 0; | - |
185 | } | - |
186 | | - |
187 | | - |
188 | | - |
189 | | - |
190 | void QRegularExpressionPrivate::compilePattern() | - |
191 | { | - |
192 | QMutexLocker lock(&mutex); | - |
193 | | - |
194 | if (!isDirty) | - |
195 | return; | - |
196 | | - |
197 | isDirty = false; | - |
198 | cleanCompiledPattern(); | - |
199 | | - |
200 | int options = convertToPcreOptions(patternOptions); | - |
201 | options |= 0x00000800; | - |
202 | | - |
203 | int errorCode; | - |
204 | compiledPattern = pcre16_compile2(pattern.utf16(), options, | - |
205 | &errorCode, &errorString, &errorOffset, 0); | - |
206 | | - |
207 | if (!compiledPattern) | - |
208 | return; | - |
209 | | - |
210 | qt_noop(); | - |
211 | qt_noop(); | - |
212 | errorOffset = -1; | - |
213 | | - |
214 | getPatternInfo(); | - |
215 | } | - |
216 | | - |
217 | | - |
218 | | - |
219 | | - |
220 | void QRegularExpressionPrivate::getPatternInfo() | - |
221 | { | - |
222 | qt_noop(); | - |
223 | | - |
224 | pcre16_fullinfo(compiledPattern, 0, 2, &capturingCount); | - |
225 | | - |
226 | | - |
227 | unsigned long int patternNewlineSetting; | - |
228 | pcre16_fullinfo(compiledPattern, studyData, 0, &patternNewlineSetting); | - |
229 | patternNewlineSetting &= 0x00100000 | 0x00200000 | 0x00300000 | - |
230 | | 0x00400000 | 0x00500000; | - |
231 | if (patternNewlineSetting == 0) { evaluated: patternNewlineSetting == 0 yes Evaluation Count:354 | yes Evaluation Count:3 |
| 3-354 |
232 | | - |
233 | int pcreNewlineSetting; | - |
234 | pcre16_config(1, &pcreNewlineSetting); | - |
235 | switch (pcreNewlineSetting) { | - |
236 | case 13: | - |
237 | patternNewlineSetting = 0x00100000; break; | 0 |
238 | case 10: | - |
239 | patternNewlineSetting = 0x00200000; break; executed: break; Execution Count:354 | 354 |
240 | case 3338: | - |
241 | patternNewlineSetting = 0x00300000; break; | 0 |
242 | case -2: | - |
243 | patternNewlineSetting = 0x00500000; break; | 0 |
244 | case -1: | - |
245 | patternNewlineSetting = 0x00400000; break; | 0 |
246 | default: | - |
247 | QMessageLogger("tools/qregularexpression.cpp", 998, __PRETTY_FUNCTION__).warning("QRegularExpressionPrivate::compilePattern(): " | - |
248 | "PCRE_CONFIG_NEWLINE returned an unknown newline"); | - |
249 | break; | 0 |
250 | } | - |
251 | } executed: } Execution Count:354 | 354 |
252 | | - |
253 | usingCrLfNewlines = (patternNewlineSetting == 0x00300000) || evaluated: (patternNewlineSetting == 0x00300000) yes Evaluation Count:2 | yes Evaluation Count:355 |
| 2-355 |
254 | (patternNewlineSetting == 0x00400000) || partially evaluated: (patternNewlineSetting == 0x00400000) no Evaluation Count:0 | yes Evaluation Count:355 |
| 0-355 |
255 | (patternNewlineSetting == 0x00500000); evaluated: (patternNewlineSetting == 0x00500000) yes Evaluation Count:1 | yes Evaluation Count:354 |
| 1-354 |
256 | } executed: } Execution Count:357 | 357 |
257 | | - |
258 | | - |
259 | | - |
260 | | - |
261 | | - |
262 | | - |
263 | class QPcreJitStackPointer | - |
264 | { | - |
265 | QPcreJitStackPointer(const QPcreJitStackPointer &) = delete; QPcreJitStackPointer &operator=(const QPcreJitStackPointer &) = delete;; | - |
266 | | - |
267 | public: | - |
268 | | - |
269 | | - |
270 | | - |
271 | QPcreJitStackPointer() | - |
272 | { | - |
273 | | - |
274 | | - |
275 | stack = pcre16_jit_stack_alloc(32*1024, 512*1024); | - |
276 | } | - |
277 | | - |
278 | | - |
279 | | - |
280 | ~QPcreJitStackPointer() | - |
281 | { | - |
282 | if (stack) | - |
283 | pcre16_jit_stack_free(stack); | - |
284 | } | - |
285 | | - |
286 | pcre16_jit_stack *stack; | - |
287 | }; | - |
288 | | - |
289 | static QThreadStorage<QPcreJitStackPointer *> *jitStacks() { static QGlobalStatic<QThreadStorage<QPcreJitStackPointer *> > thisGlobalStatic = { { (0) }, false }; if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) { QThreadStorage<QPcreJitStackPointer *> *x = new QThreadStorage<QPcreJitStackPointer *>; if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) delete x; else static QGlobalStaticDeleter<QThreadStorage<QPcreJitStackPointer *> > cleanup(thisGlobalStatic); } return thisGlobalStatic.pointer.load(); } | - |
290 | | - |
291 | | - |
292 | | - |
293 | | - |
294 | static pcre16_jit_stack *qtPcreCallback(void *) | - |
295 | { | - |
296 | if (jitStacks()->hasLocalData()) | - |
297 | return jitStacks()->localData()->stack; | - |
298 | | - |
299 | return 0; | - |
300 | } | - |
301 | | - |
302 | | - |
303 | | - |
304 | | - |
305 | static bool isJitEnabled() | - |
306 | { | - |
307 | QByteArray jitEnvironment = qgetenv("QT_ENABLE_REGEXP_JIT"); | - |
308 | if (!jitEnvironment.isEmpty()) { | - |
309 | bool ok; | - |
310 | int enableJit = jitEnvironment.toInt(&ok); | - |
311 | return ok ? (enableJit != 0) : true; | - |
312 | } | - |
313 | | - |
314 | | - |
315 | | - |
316 | | - |
317 | return true; | - |
318 | | - |
319 | } | - |
320 | pcre16_extra *QRegularExpressionPrivate::optimizePattern() | - |
321 | { | - |
322 | qt_noop(); | - |
323 | | - |
324 | QMutexLocker lock(&mutex); | - |
325 | | - |
326 | if (studyData || (++usedCount != qt_qregularexpression_optimize_after_use_count)) | - |
327 | return studyData; | - |
328 | | - |
329 | static const bool enableJit = isJitEnabled(); | - |
330 | | - |
331 | int studyOptions = 0; | - |
332 | if (enableJit) | - |
333 | studyOptions |= 0x0001; | - |
334 | | - |
335 | const char *err; | - |
336 | studyData = pcre16_study(compiledPattern, studyOptions, &err); | - |
337 | | - |
338 | if (studyData && studyData->flags & 0x0040) | - |
339 | pcre16_assign_jit_stack(studyData, qtPcreCallback, 0); | - |
340 | | - |
341 | if (!studyData && err) | - |
342 | QMessageLogger("tools/qregularexpression.cpp", 1112, __PRETTY_FUNCTION__).warning("QRegularExpressionPrivate::optimizePattern(): pcre_study failed: %s", err); | - |
343 | | - |
344 | return studyData; | - |
345 | } | - |
346 | | - |
347 | | - |
348 | | - |
349 | | - |
350 | | - |
351 | | - |
352 | | - |
353 | int QRegularExpressionPrivate::captureIndexForName(const QString &name) const | - |
354 | { | - |
355 | qt_noop(); | - |
356 | | - |
357 | if (!compiledPattern) | - |
358 | return -1; | - |
359 | | - |
360 | int index = pcre16_get_stringnumber(compiledPattern, name.utf16()); | - |
361 | if (index >= 0) | - |
362 | return index; | - |
363 | | - |
364 | return -1; | - |
365 | } | - |
366 | static int pcre16SafeExec(const pcre16 *code, const pcre16_extra *extra, | - |
367 | const unsigned short *subject, int length, | - |
368 | int startOffset, int options, | - |
369 | int *ovector, int ovecsize) | - |
370 | { | - |
371 | int result = pcre16_exec(code, extra, subject, length, | - |
372 | startOffset, options, ovector, ovecsize); | - |
373 | | - |
374 | if (result == (-27) && !jitStacks()->hasLocalData()) { | - |
375 | QPcreJitStackPointer *p = new QPcreJitStackPointer; | - |
376 | jitStacks()->setLocalData(p); | - |
377 | | - |
378 | result = pcre16_exec(code, extra, subject, length, | - |
379 | startOffset, options, ovector, ovecsize); | - |
380 | } | - |
381 | | - |
382 | return result; | - |
383 | } | - |
384 | QRegularExpressionMatchPrivate *QRegularExpressionPrivate::doMatch(const QString &subject, | - |
385 | int offset, | - |
386 | QRegularExpression::MatchType matchType, | - |
387 | QRegularExpression::MatchOptions matchOptions, | - |
388 | bool checkSubjectString, | - |
389 | const QRegularExpressionMatchPrivate *previous) const | - |
390 | { | - |
391 | if (offset < 0) | - |
392 | offset += subject.length(); | - |
393 | | - |
394 | QRegularExpression re(*const_cast<QRegularExpressionPrivate *>(this)); | - |
395 | | - |
396 | if (offset < 0 || offset > subject.length()) | - |
397 | return new QRegularExpressionMatchPrivate(re, subject, matchType, matchOptions, 0); | - |
398 | | - |
399 | if (!compiledPattern) { | - |
400 | QMessageLogger("tools/qregularexpression.cpp", 1199, __PRETTY_FUNCTION__).warning("QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object"); | - |
401 | return new QRegularExpressionMatchPrivate(re, subject, matchType, matchOptions, 0); | - |
402 | } | - |
403 | | - |
404 | QRegularExpressionMatchPrivate *priv = new QRegularExpressionMatchPrivate(re, subject, | - |
405 | matchType, matchOptions, | - |
406 | capturingCount); | - |
407 | | - |
408 | | - |
409 | const pcre16_extra *currentStudyData = const_cast<QRegularExpressionPrivate *>(this)->optimizePattern(); | - |
410 | | - |
411 | int pcreOptions = convertToPcreOptions(matchOptions); | - |
412 | | - |
413 | if (matchType == QRegularExpression::PartialPreferCompleteMatch) | - |
414 | pcreOptions |= 0x00008000; | - |
415 | else if (matchType == QRegularExpression::PartialPreferFirstMatch) | - |
416 | pcreOptions |= 0x08000000; | - |
417 | | - |
418 | if (!checkSubjectString) | - |
419 | pcreOptions |= 0x00002000; | - |
420 | | - |
421 | bool previousMatchWasEmpty = false; | - |
422 | if (previous && previous->hasMatch && | - |
423 | (previous->capturedOffsets.at(0) == previous->capturedOffsets.at(1))) { | - |
424 | previousMatchWasEmpty = true; | - |
425 | } | - |
426 | | - |
427 | int * const captureOffsets = priv->capturedOffsets.data(); | - |
428 | const int captureOffsetsCount = priv->capturedOffsets.size(); | - |
429 | | - |
430 | const unsigned short * const subjectUtf16 = subject.utf16(); | - |
431 | const int subjectLength = subject.length(); | - |
432 | | - |
433 | int result; | - |
434 | | - |
435 | if (!previousMatchWasEmpty) { | - |
436 | result = pcre16SafeExec(compiledPattern, currentStudyData, | - |
437 | subjectUtf16, subjectLength, | - |
438 | offset, pcreOptions, | - |
439 | captureOffsets, captureOffsetsCount); | - |
440 | } else { | - |
441 | result = pcre16SafeExec(compiledPattern, currentStudyData, | - |
442 | subjectUtf16, subjectLength, | - |
443 | offset, pcreOptions | 0x10000000 | 0x00000010, | - |
444 | captureOffsets, captureOffsetsCount); | - |
445 | | - |
446 | if (result == (-1)) { | - |
447 | ++offset; | - |
448 | | - |
449 | if (usingCrLfNewlines | - |
450 | && offset < subjectLength | - |
451 | && subjectUtf16[offset - 1] == QLatin1Char('\r') | - |
452 | && subjectUtf16[offset] == QLatin1Char('\n')) { | - |
453 | ++offset; | - |
454 | } else if (offset < subjectLength | - |
455 | && QChar::isLowSurrogate(subjectUtf16[offset])) { | - |
456 | ++offset; | - |
457 | } | - |
458 | | - |
459 | result = pcre16SafeExec(compiledPattern, currentStudyData, | - |
460 | subjectUtf16, subjectLength, | - |
461 | offset, pcreOptions, | - |
462 | captureOffsets, captureOffsetsCount); | - |
463 | } | - |
464 | } | - |
465 | qt_noop(); | - |
466 | | - |
467 | if (result > 0) { | - |
468 | | - |
469 | priv->isValid = true; | - |
470 | priv->hasMatch = true; | - |
471 | priv->capturedCount = result; | - |
472 | priv->capturedOffsets.resize(result * 2); | - |
473 | } else { | - |
474 | | - |
475 | priv->hasPartialMatch = (result == (-12)); | - |
476 | priv->isValid = (result == (-1) || result == (-12)); | - |
477 | | - |
478 | if (result == (-12)) { | - |
479 | | - |
480 | | - |
481 | priv->capturedCount = 1; | - |
482 | priv->capturedOffsets.resize(2); | - |
483 | } else { | - |
484 | | - |
485 | priv->capturedCount = 0; | - |
486 | priv->capturedOffsets.clear(); | - |
487 | } | - |
488 | } | - |
489 | | - |
490 | return priv; | - |
491 | } | - |
492 | | - |
493 | | - |
494 | | - |
495 | | - |
496 | QRegularExpressionMatchPrivate::QRegularExpressionMatchPrivate(const QRegularExpression &re, | - |
497 | const QString &subject, | - |
498 | QRegularExpression::MatchType matchType, | - |
499 | QRegularExpression::MatchOptions matchOptions, | - |
500 | int capturingCount) | - |
501 | : regularExpression(re), subject(subject), | - |
502 | matchType(matchType), matchOptions(matchOptions), | - |
503 | capturedCount(0), | - |
504 | hasMatch(false), hasPartialMatch(false), isValid(false) | - |
505 | { | - |
506 | qt_noop(); | - |
507 | const int captureOffsetsCount = (capturingCount + 1) * 3; | - |
508 | capturedOffsets.resize(captureOffsetsCount); | - |
509 | } | - |
510 | | - |
511 | | - |
512 | | - |
513 | | - |
514 | | - |
515 | QRegularExpressionMatch QRegularExpressionMatchPrivate::nextMatch() const | - |
516 | { | - |
517 | qt_noop(); | - |
518 | qt_noop(); | - |
519 | | - |
520 | | - |
521 | | - |
522 | | - |
523 | | - |
524 | QRegularExpressionMatchPrivate *nextPrivate = regularExpression.d->doMatch(subject, | - |
525 | capturedOffsets.at(1), | - |
526 | matchType, | - |
527 | matchOptions, | - |
528 | false, | - |
529 | this); | - |
530 | return QRegularExpressionMatch(*nextPrivate); | - |
531 | } | - |
532 | | - |
533 | | - |
534 | | - |
535 | | - |
536 | QRegularExpressionMatchIteratorPrivate::QRegularExpressionMatchIteratorPrivate(const QRegularExpression &re, | - |
537 | QRegularExpression::MatchType matchType, | - |
538 | QRegularExpression::MatchOptions matchOptions, | - |
539 | const QRegularExpressionMatch &next) | - |
540 | : next(next), | - |
541 | regularExpression(re), | - |
542 | matchType(matchType), matchOptions(matchOptions) | - |
543 | { | - |
544 | } | - |
545 | | - |
546 | | - |
547 | | - |
548 | | - |
549 | bool QRegularExpressionMatchIteratorPrivate::hasNext() const | - |
550 | { | - |
551 | return next.isValid() && (next.hasMatch() || next.hasPartialMatch()); | - |
552 | } | - |
553 | QRegularExpression::QRegularExpression() | - |
554 | : d(new QRegularExpressionPrivate) | - |
555 | { | - |
556 | } | - |
557 | | - |
558 | | - |
559 | | - |
560 | | - |
561 | | - |
562 | | - |
563 | | - |
564 | QRegularExpression::QRegularExpression(const QString &pattern, PatternOptions options) | - |
565 | : d(new QRegularExpressionPrivate) | - |
566 | { | - |
567 | d->pattern = pattern; | - |
568 | d->patternOptions = options; | - |
569 | } | - |
570 | | - |
571 | | - |
572 | | - |
573 | | - |
574 | | - |
575 | | - |
576 | QRegularExpression::QRegularExpression(const QRegularExpression &re) | - |
577 | : d(re.d) | - |
578 | { | - |
579 | } | - |
580 | | - |
581 | | - |
582 | | - |
583 | | - |
584 | QRegularExpression::~QRegularExpression() | - |
585 | { | - |
586 | } | - |
587 | | - |
588 | | - |
589 | | - |
590 | | - |
591 | | - |
592 | QRegularExpression &QRegularExpression::operator=(const QRegularExpression &re) | - |
593 | { | - |
594 | d = re.d; | - |
595 | return *this; | - |
596 | } | - |
597 | QString QRegularExpression::pattern() const | - |
598 | { | - |
599 | return d->pattern; | - |
600 | } | - |
601 | | - |
602 | | - |
603 | | - |
604 | | - |
605 | | - |
606 | | - |
607 | | - |
608 | void QRegularExpression::setPattern(const QString &pattern) | - |
609 | { | - |
610 | d.detach(); | - |
611 | d->isDirty = true; | - |
612 | d->pattern = pattern; | - |
613 | } | - |
614 | | - |
615 | | - |
616 | | - |
617 | | - |
618 | | - |
619 | | - |
620 | QRegularExpression::PatternOptions QRegularExpression::patternOptions() const | - |
621 | { | - |
622 | return d->patternOptions; | - |
623 | } | - |
624 | | - |
625 | | - |
626 | | - |
627 | | - |
628 | | - |
629 | | - |
630 | | - |
631 | void QRegularExpression::setPatternOptions(PatternOptions options) | - |
632 | { | - |
633 | d.detach(); | - |
634 | d->isDirty = true; | - |
635 | d->patternOptions = options; | - |
636 | } | - |
637 | | - |
638 | | - |
639 | | - |
640 | | - |
641 | | - |
642 | | - |
643 | | - |
644 | int QRegularExpression::captureCount() const | - |
645 | { | - |
646 | if (!isValid()) | - |
647 | return -1; | - |
648 | return d->capturingCount; | - |
649 | } | - |
650 | bool QRegularExpression::isValid() const | - |
651 | { | - |
652 | d.data()->compilePattern(); | - |
653 | return d->compiledPattern; | - |
654 | } | - |
655 | | - |
656 | | - |
657 | | - |
658 | | - |
659 | | - |
660 | | - |
661 | | - |
662 | QString QRegularExpression::errorString() const | - |
663 | { | - |
664 | d.data()->compilePattern(); | - |
665 | if (d->errorString) | - |
666 | return QCoreApplication::translate("QRegularExpression", d->errorString); | - |
667 | return QCoreApplication::translate("QRegularExpression", "no error"); | - |
668 | } | - |
669 | int QRegularExpression::patternErrorOffset() const | - |
670 | { | - |
671 | d.data()->compilePattern(); | - |
672 | return d->errorOffset; | - |
673 | } | - |
674 | QRegularExpressionMatch QRegularExpression::match(const QString &subject, | - |
675 | int offset, | - |
676 | MatchType matchType, | - |
677 | MatchOptions matchOptions) const | - |
678 | { | - |
679 | d.data()->compilePattern(); | - |
680 | | - |
681 | QRegularExpressionMatchPrivate *priv = d->doMatch(subject, offset, matchType, matchOptions); | - |
682 | return QRegularExpressionMatch(*priv); | - |
683 | } | - |
684 | QRegularExpressionMatchIterator QRegularExpression::globalMatch(const QString &subject, | - |
685 | int offset, | - |
686 | MatchType matchType, | - |
687 | MatchOptions matchOptions) const | - |
688 | { | - |
689 | QRegularExpressionMatchIteratorPrivate *priv = | - |
690 | new QRegularExpressionMatchIteratorPrivate(*this, | - |
691 | matchType, | - |
692 | matchOptions, | - |
693 | match(subject, offset, matchType, matchOptions)); | - |
694 | | - |
695 | return QRegularExpressionMatchIterator(*priv); | - |
696 | } | - |
697 | bool QRegularExpression::operator==(const QRegularExpression &re) const | - |
698 | { | - |
699 | return (d == re.d) || | - |
700 | (d->pattern == re.d->pattern && d->patternOptions == re.d->patternOptions); | - |
701 | } | - |
702 | QString QRegularExpression::escape(const QString &str) | - |
703 | { | - |
704 | QString result; | - |
705 | const int count = str.size(); | - |
706 | result.reserve(count * 2); | - |
707 | | - |
708 | | - |
709 | | - |
710 | for (int i = 0; i < count; ++i) { | - |
711 | const QChar current = str.at(i); | - |
712 | | - |
713 | if (current == QChar::Null) { | - |
714 | | - |
715 | | - |
716 | | - |
717 | result.append(QLatin1Char('\\')); | - |
718 | result.append(QLatin1Char('0')); | - |
719 | } else if ( (current < QLatin1Char('a') || current > QLatin1Char('z')) && | - |
720 | (current < QLatin1Char('A') || current > QLatin1Char('Z')) && | - |
721 | (current < QLatin1Char('0') || current > QLatin1Char('9')) && | - |
722 | current != QLatin1Char('_') ) | - |
723 | { | - |
724 | result.append(QLatin1Char('\\')); | - |
725 | result.append(current); | - |
726 | if (current.isHighSurrogate() && i < (count - 1)) | - |
727 | result.append(str.at(++i)); | - |
728 | } else { | - |
729 | result.append(current); | - |
730 | } | - |
731 | } | - |
732 | | - |
733 | result.squeeze(); | - |
734 | return result; | - |
735 | } | - |
736 | | - |
737 | | - |
738 | | - |
739 | | - |
740 | QRegularExpressionMatch::~QRegularExpressionMatch() | - |
741 | { | - |
742 | } | - |
743 | | - |
744 | | - |
745 | | - |
746 | | - |
747 | | - |
748 | | - |
749 | QRegularExpressionMatch::QRegularExpressionMatch(const QRegularExpressionMatch &match) | - |
750 | : d(match.d) | - |
751 | { | - |
752 | } | - |
753 | | - |
754 | | - |
755 | | - |
756 | | - |
757 | | - |
758 | QRegularExpressionMatch &QRegularExpressionMatch::operator=(const QRegularExpressionMatch &match) | - |
759 | { | - |
760 | d = match.d; | - |
761 | return *this; | - |
762 | } | - |
763 | QRegularExpressionMatch::QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd) | - |
764 | : d(&dd) | - |
765 | { | - |
766 | } | - |
767 | | - |
768 | | - |
769 | | - |
770 | | - |
771 | | - |
772 | | - |
773 | | - |
774 | QRegularExpression QRegularExpressionMatch::regularExpression() const | - |
775 | { | - |
776 | return d->regularExpression; | - |
777 | } | - |
778 | QRegularExpression::MatchType QRegularExpressionMatch::matchType() const | - |
779 | { | - |
780 | return d->matchType; | - |
781 | } | - |
782 | QRegularExpression::MatchOptions QRegularExpressionMatch::matchOptions() const | - |
783 | { | - |
784 | return d->matchOptions; | - |
785 | } | - |
786 | int QRegularExpressionMatch::lastCapturedIndex() const | - |
787 | { | - |
788 | return d->capturedCount - 1; | - |
789 | } | - |
790 | QString QRegularExpressionMatch::captured(int nth) const | - |
791 | { | - |
792 | if (nth < 0 || nth > lastCapturedIndex()) | - |
793 | return QString(); | - |
794 | | - |
795 | int start = capturedStart(nth); | - |
796 | | - |
797 | if (start == -1) | - |
798 | return QString(); | - |
799 | | - |
800 | return d->subject.mid(start, capturedLength(nth)); | - |
801 | } | - |
802 | QStringRef QRegularExpressionMatch::capturedRef(int nth) const | - |
803 | { | - |
804 | if (nth < 0 || nth > lastCapturedIndex()) | - |
805 | return QStringRef(); | - |
806 | | - |
807 | int start = capturedStart(nth); | - |
808 | | - |
809 | if (start == -1) | - |
810 | return QStringRef(); | - |
811 | | - |
812 | return d->subject.midRef(start, capturedLength(nth)); | - |
813 | } | - |
814 | QString QRegularExpressionMatch::captured(const QString &name) const | - |
815 | { | - |
816 | if (name.isEmpty()) { | - |
817 | QMessageLogger("tools/qregularexpression.cpp", 1791, __PRETTY_FUNCTION__).warning("QRegularExpressionMatch::captured: empty capturing group name passed"); | - |
818 | return QString(); | - |
819 | } | - |
820 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
821 | if (nth == -1) | - |
822 | return QString(); | - |
823 | return captured(nth); | - |
824 | } | - |
825 | QStringRef QRegularExpressionMatch::capturedRef(const QString &name) const | - |
826 | { | - |
827 | if (name.isEmpty()) { | - |
828 | QMessageLogger("tools/qregularexpression.cpp", 1811, __PRETTY_FUNCTION__).warning("QRegularExpressionMatch::capturedRef: empty capturing group name passed"); | - |
829 | return QStringRef(); | - |
830 | } | - |
831 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
832 | if (nth == -1) | - |
833 | return QStringRef(); | - |
834 | return capturedRef(nth); | - |
835 | } | - |
836 | | - |
837 | | - |
838 | | - |
839 | | - |
840 | | - |
841 | QStringList QRegularExpressionMatch::capturedTexts() const | - |
842 | { | - |
843 | QStringList texts; | - |
844 | for (int i = 0; i <= lastCapturedIndex(); ++i) | - |
845 | texts << captured(i); | - |
846 | return texts; | - |
847 | } | - |
848 | int QRegularExpressionMatch::capturedStart(int nth) const | - |
849 | { | - |
850 | if (nth < 0 || nth > lastCapturedIndex()) | - |
851 | return -1; | - |
852 | | - |
853 | return d->capturedOffsets.at(nth * 2); | - |
854 | } | - |
855 | int QRegularExpressionMatch::capturedLength(int nth) const | - |
856 | { | - |
857 | | - |
858 | return capturedEnd(nth) - capturedStart(nth); | - |
859 | } | - |
860 | int QRegularExpressionMatch::capturedEnd(int nth) const | - |
861 | { | - |
862 | if (nth < 0 || nth > lastCapturedIndex()) | - |
863 | return -1; | - |
864 | | - |
865 | return d->capturedOffsets.at(nth * 2 + 1); | - |
866 | } | - |
867 | int QRegularExpressionMatch::capturedStart(const QString &name) const | - |
868 | { | - |
869 | if (name.isEmpty()) { | - |
870 | QMessageLogger("tools/qregularexpression.cpp", 1888, __PRETTY_FUNCTION__).warning("QRegularExpressionMatch::capturedStart: empty capturing group name passed"); | - |
871 | return -1; | - |
872 | } | - |
873 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
874 | if (nth == -1) | - |
875 | return -1; | - |
876 | return capturedStart(nth); | - |
877 | } | - |
878 | int QRegularExpressionMatch::capturedLength(const QString &name) const | - |
879 | { | - |
880 | if (name.isEmpty()) { | - |
881 | QMessageLogger("tools/qregularexpression.cpp", 1909, __PRETTY_FUNCTION__).warning("QRegularExpressionMatch::capturedLength: empty capturing group name passed"); | - |
882 | return 0; | - |
883 | } | - |
884 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
885 | if (nth == -1) | - |
886 | return 0; | - |
887 | return capturedLength(nth); | - |
888 | } | - |
889 | int QRegularExpressionMatch::capturedEnd(const QString &name) const | - |
890 | { | - |
891 | if (name.isEmpty()) { | - |
892 | QMessageLogger("tools/qregularexpression.cpp", 1929, __PRETTY_FUNCTION__).warning("QRegularExpressionMatch::capturedEnd: empty capturing group name passed"); | - |
893 | return -1; | - |
894 | } | - |
895 | int nth = d->regularExpression.d->captureIndexForName(name); | - |
896 | if (nth == -1) | - |
897 | return -1; | - |
898 | return capturedEnd(nth); | - |
899 | } | - |
900 | | - |
901 | | - |
902 | | - |
903 | | - |
904 | | - |
905 | | - |
906 | | - |
907 | bool QRegularExpressionMatch::hasMatch() const | - |
908 | { | - |
909 | return d->hasMatch; | - |
910 | } | - |
911 | bool QRegularExpressionMatch::hasPartialMatch() const | - |
912 | { | - |
913 | return d->hasPartialMatch; | - |
914 | } | - |
915 | bool QRegularExpressionMatch::isValid() const | - |
916 | { | - |
917 | return d->isValid; | - |
918 | } | - |
919 | | - |
920 | | - |
921 | | - |
922 | | - |
923 | QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd) | - |
924 | : d(&dd) | - |
925 | { | - |
926 | } | - |
927 | | - |
928 | | - |
929 | | - |
930 | | - |
931 | QRegularExpressionMatchIterator::~QRegularExpressionMatchIterator() | - |
932 | { | - |
933 | } | - |
934 | | - |
935 | | - |
936 | | - |
937 | | - |
938 | | - |
939 | | - |
940 | | - |
941 | QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator) | - |
942 | : d(iterator.d) | - |
943 | { | - |
944 | } | - |
945 | | - |
946 | | - |
947 | | - |
948 | | - |
949 | | - |
950 | QRegularExpressionMatchIterator &QRegularExpressionMatchIterator::operator=(const QRegularExpressionMatchIterator &iterator) | - |
951 | { | - |
952 | d = iterator.d; | - |
953 | return *this; | - |
954 | } | - |
955 | bool QRegularExpressionMatchIterator::isValid() const | - |
956 | { | - |
957 | return d->next.isValid(); | - |
958 | } | - |
959 | | - |
960 | | - |
961 | | - |
962 | | - |
963 | | - |
964 | | - |
965 | | - |
966 | bool QRegularExpressionMatchIterator::hasNext() const | - |
967 | { | - |
968 | return d->hasNext(); | - |
969 | } | - |
970 | | - |
971 | | - |
972 | | - |
973 | | - |
974 | | - |
975 | | - |
976 | | - |
977 | QRegularExpressionMatch QRegularExpressionMatchIterator::peekNext() const | - |
978 | { | - |
979 | if (!hasNext()) | - |
980 | QMessageLogger("tools/qregularexpression.cpp", 2052, __PRETTY_FUNCTION__).warning("QRegularExpressionMatchIterator::peekNext() called on an iterator already at end"); | - |
981 | | - |
982 | return d->next; | - |
983 | } | - |
984 | | - |
985 | | - |
986 | | - |
987 | | - |
988 | | - |
989 | | - |
990 | | - |
991 | QRegularExpressionMatch QRegularExpressionMatchIterator::next() | - |
992 | { | - |
993 | if (!hasNext()) { | - |
994 | QMessageLogger("tools/qregularexpression.cpp", 2066, __PRETTY_FUNCTION__).warning("QRegularExpressionMatchIterator::next() called on an iterator already at end"); | - |
995 | return d->next; | - |
996 | } | - |
997 | | - |
998 | QRegularExpressionMatch current = d->next; | - |
999 | d->next = d->next.d.constData()->nextMatch(); | - |
1000 | return current; | - |
1001 | } | - |
1002 | | - |
1003 | | - |
1004 | | - |
1005 | | - |
1006 | | - |
1007 | | - |
1008 | | - |
1009 | QRegularExpression QRegularExpressionMatchIterator::regularExpression() const | - |
1010 | { | - |
1011 | return d->regularExpression; | - |
1012 | } | - |
1013 | QRegularExpression::MatchType QRegularExpressionMatchIterator::matchType() const | - |
1014 | { | - |
1015 | return d->matchType; | - |
1016 | } | - |
1017 | QRegularExpression::MatchOptions QRegularExpressionMatchIterator::matchOptions() const | - |
1018 | { | - |
1019 | return d->matchOptions; | - |
1020 | } | - |
1021 | QDataStream &operator<<(QDataStream &out, const QRegularExpression &re) | - |
1022 | { | - |
1023 | out << re.pattern() << quint32(re.patternOptions()); | - |
1024 | return out; | - |
1025 | } | - |
1026 | QDataStream &operator>>(QDataStream &in, QRegularExpression &re) | - |
1027 | { | - |
1028 | QString pattern; | - |
1029 | quint32 patternOptions; | - |
1030 | in >> pattern >> patternOptions; | - |
1031 | re.setPattern(pattern); | - |
1032 | re.setPatternOptions(QRegularExpression::PatternOptions(patternOptions)); | - |
1033 | return in; | - |
1034 | } | - |
1035 | QDebug operator<<(QDebug debug, const QRegularExpression &re) | - |
1036 | { | - |
1037 | debug.nospace() << "QRegularExpression(" << re.pattern() << ", " << re.patternOptions() << ")"; | - |
1038 | return debug.space(); | - |
1039 | } | - |
1040 | QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions) | - |
1041 | { | - |
1042 | QByteArray flags; | - |
1043 | | - |
1044 | if (patternOptions == QRegularExpression::NoPatternOption) { | - |
1045 | flags = "NoPatternOption"; | - |
1046 | } else { | - |
1047 | flags.reserve(200); | - |
1048 | if (patternOptions & QRegularExpression::CaseInsensitiveOption) | - |
1049 | flags.append("CaseInsensitiveOption|"); | - |
1050 | if (patternOptions & QRegularExpression::DotMatchesEverythingOption) | - |
1051 | flags.append("DotMatchesEverythingOption|"); | - |
1052 | if (patternOptions & QRegularExpression::MultilineOption) | - |
1053 | flags.append("MultilineOption|"); | - |
1054 | if (patternOptions & QRegularExpression::ExtendedPatternSyntaxOption) | - |
1055 | flags.append("ExtendedPatternSyntaxOption|"); | - |
1056 | if (patternOptions & QRegularExpression::InvertedGreedinessOption) | - |
1057 | flags.append("InvertedGreedinessOption|"); | - |
1058 | if (patternOptions & QRegularExpression::DontCaptureOption) | - |
1059 | flags.append("DontCaptureOption|"); | - |
1060 | if (patternOptions & QRegularExpression::UseUnicodePropertiesOption) | - |
1061 | flags.append("UseUnicodePropertiesOption|"); | - |
1062 | flags.chop(1); | - |
1063 | } | - |
1064 | | - |
1065 | debug.nospace() << "QRegularExpression::PatternOptions(" << flags << ")"; | - |
1066 | | - |
1067 | return debug.space(); | - |
1068 | } | - |
1069 | QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match) | - |
1070 | { | - |
1071 | debug.nospace() << "QRegularExpressionMatch("; | - |
1072 | | - |
1073 | if (!match.isValid()) { | - |
1074 | debug << "Invalid)"; | - |
1075 | return debug.space(); | - |
1076 | } | - |
1077 | | - |
1078 | debug << "Valid"; | - |
1079 | | - |
1080 | if (match.hasMatch()) { | - |
1081 | debug << ", has match: "; | - |
1082 | for (int i = 0; i <= match.lastCapturedIndex(); ++i) { | - |
1083 | debug << i | - |
1084 | << ":(" << match.capturedStart(i) << ", " << match.capturedEnd(i) | - |
1085 | << ", " << match.captured(i) << ")"; | - |
1086 | if (i < match.lastCapturedIndex()) | - |
1087 | debug << ", "; | - |
1088 | } | - |
1089 | } else if (match.hasPartialMatch()) { | - |
1090 | debug << ", has partial match: (" | - |
1091 | << match.capturedStart(0) << ", " | - |
1092 | << match.capturedEnd(0) << ", " | - |
1093 | << match.captured(0) << ")"; | - |
1094 | } else { | - |
1095 | debug << ", no match"; | - |
1096 | } | - |
1097 | | - |
1098 | debug << ")"; | - |
1099 | | - |
1100 | return debug.space(); | - |
1101 | } | - |
1102 | | - |
1103 | | - |
| | |