]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/regex.h
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / regex.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (C) 2002-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * file name: regex.h
9 * encoding: UTF-8
10 * indentation:4
11 *
12 * created on: 2002oct22
13 * created by: Andy Heninger
14 *
15 * ICU Regular Expressions, API for C++
16 */
17
18 #ifndef REGEX_H
19 #define REGEX_H
20
21 //#define REGEX_DEBUG
22
23 /**
24 * \file
25 * \brief C++ API: Regular Expressions
26 *
27 * The ICU API for processing regular expressions consists of two classes,
28 * `RegexPattern` and `RegexMatcher`.
29 * `RegexPattern` objects represent a pre-processed, or compiled
30 * regular expression. They are created from a regular expression pattern string,
31 * and can be used to create `RegexMatcher` objects for the pattern.
32 *
33 * Class `RegexMatcher` bundles together a regular expression
34 * pattern and a target string to which the search pattern will be applied.
35 * `RegexMatcher` includes API for doing plain find or search
36 * operations, for search and replace operations, and for obtaining detailed
37 * information about bounds of a match.
38 *
39 * Note that by constructing `RegexMatcher` objects directly from regular
40 * expression pattern strings application code can be simplified and the explicit
41 * need for `RegexPattern` objects can usually be eliminated.
42 *
43 */
44
45 #include "unicode/utypes.h"
46
47 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
48
49 #include "unicode/uobject.h"
50 #include "unicode/unistr.h"
51 #include "unicode/utext.h"
52 #include "unicode/parseerr.h"
53
54 #include "unicode/uregex.h"
55
56 // Forward Declarations
57
58 struct UHashtable;
59
60 #if U_SHOW_CPLUSPLUS_API
61 U_NAMESPACE_BEGIN
62
63 struct Regex8BitSet;
64 class RegexCImpl;
65 class RegexMatcher;
66 class RegexPattern;
67 struct REStackFrame;
68 class RuleBasedBreakIterator;
69 class UnicodeSet;
70 class UVector;
71 class UVector32;
72 class UVector64;
73
74
75 /**
76 * Class `RegexPattern` represents a compiled regular expression. It includes
77 * factory methods for creating a RegexPattern object from the source (string) form
78 * of a regular expression, methods for creating RegexMatchers that allow the pattern
79 * to be applied to input text, and a few convenience methods for simple common
80 * uses of regular expressions.
81 *
82 * Class RegexPattern is not intended to be subclassed.
83 *
84 * @stable ICU 2.4
85 */
86 class U_I18N_API RegexPattern U_FINAL : public UObject {
87 public:
88
89 /**
90 * default constructor. Create a RegexPattern object that refers to no actual
91 * pattern. Not normally needed; RegexPattern objects are usually
92 * created using the factory method `compile()`.
93 *
94 * @stable ICU 2.4
95 */
96 RegexPattern();
97
98 /**
99 * Copy Constructor. Create a new RegexPattern object that is equivalent
100 * to the source object.
101 * @param source the pattern object to be copied.
102 * @stable ICU 2.4
103 */
104 RegexPattern(const RegexPattern &source);
105
106 /**
107 * Destructor. Note that a RegexPattern object must persist so long as any
108 * RegexMatcher objects that were created from the RegexPattern are active.
109 * @stable ICU 2.4
110 */
111 virtual ~RegexPattern();
112
113 /**
114 * Comparison operator. Two RegexPattern objects are considered equal if they
115 * were constructed from identical source patterns using the same #URegexpFlag
116 * settings.
117 * @param that a RegexPattern object to compare with "this".
118 * @return TRUE if the objects are equivalent.
119 * @stable ICU 2.4
120 */
121 UBool operator==(const RegexPattern& that) const;
122
123 /**
124 * Comparison operator. Two RegexPattern objects are considered equal if they
125 * were constructed from identical source patterns using the same #URegexpFlag
126 * settings.
127 * @param that a RegexPattern object to compare with "this".
128 * @return TRUE if the objects are different.
129 * @stable ICU 2.4
130 */
131 inline UBool operator!=(const RegexPattern& that) const {return ! operator ==(that);}
132
133 /**
134 * Assignment operator. After assignment, this RegexPattern will behave identically
135 * to the source object.
136 * @stable ICU 2.4
137 */
138 RegexPattern &operator =(const RegexPattern &source);
139
140 /**
141 * Create an exact copy of this RegexPattern object. Since RegexPattern is not
142 * intended to be subclassed, <code>clone()</code> and the copy construction are
143 * equivalent operations.
144 * @return the copy of this RegexPattern
145 * @stable ICU 2.4
146 */
147 virtual RegexPattern *clone() const;
148
149
150 /**
151 * Compiles the regular expression in string form into a RegexPattern
152 * object. These compile methods, rather than the constructors, are the usual
153 * way that RegexPattern objects are created.
154 *
155 * Note that RegexPattern objects must not be deleted while RegexMatcher
156 * objects created from the pattern are active. RegexMatchers keep a pointer
157 * back to their pattern, so premature deletion of the pattern is a
158 * catastrophic error.
159 *
160 * All #URegexpFlag pattern match mode flags are set to their default values.
161 *
162 * Note that it is often more convenient to construct a RegexMatcher directly
163 * from a pattern string rather than separately compiling the pattern and
164 * then creating a RegexMatcher object from the pattern.
165 *
166 * @param regex The regular expression to be compiled.
167 * @param pe Receives the position (line and column nubers) of any error
168 * within the regular expression.)
169 * @param status A reference to a UErrorCode to receive any errors.
170 * @return A regexPattern object for the compiled pattern.
171 *
172 * @stable ICU 2.4
173 */
174 static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
175 UParseError &pe,
176 UErrorCode &status);
177
178 /**
179 * Compiles the regular expression in string form into a RegexPattern
180 * object. These compile methods, rather than the constructors, are the usual
181 * way that RegexPattern objects are created.
182 *
183 * Note that RegexPattern objects must not be deleted while RegexMatcher
184 * objects created from the pattern are active. RegexMatchers keep a pointer
185 * back to their pattern, so premature deletion of the pattern is a
186 * catastrophic error.
187 *
188 * All #URegexpFlag pattern match mode flags are set to their default values.
189 *
190 * Note that it is often more convenient to construct a RegexMatcher directly
191 * from a pattern string rather than separately compiling the pattern and
192 * then creating a RegexMatcher object from the pattern.
193 *
194 * @param regex The regular expression to be compiled. Note, the text referred
195 * to by this UText must not be deleted during the lifetime of the
196 * RegexPattern object or any RegexMatcher object created from it.
197 * @param pe Receives the position (line and column nubers) of any error
198 * within the regular expression.)
199 * @param status A reference to a UErrorCode to receive any errors.
200 * @return A regexPattern object for the compiled pattern.
201 *
202 * @stable ICU 4.6
203 */
204 static RegexPattern * U_EXPORT2 compile( UText *regex,
205 UParseError &pe,
206 UErrorCode &status);
207
208 /**
209 * Compiles the regular expression in string form into a RegexPattern
210 * object using the specified #URegexpFlag match mode flags. These compile methods,
211 * rather than the constructors, are the usual way that RegexPattern objects
212 * are created.
213 *
214 * Note that RegexPattern objects must not be deleted while RegexMatcher
215 * objects created from the pattern are active. RegexMatchers keep a pointer
216 * back to their pattern, so premature deletion of the pattern is a
217 * catastrophic error.
218 *
219 * Note that it is often more convenient to construct a RegexMatcher directly
220 * from a pattern string instead of than separately compiling the pattern and
221 * then creating a RegexMatcher object from the pattern.
222 *
223 * @param regex The regular expression to be compiled.
224 * @param flags The #URegexpFlag match mode flags to be used, e.g. #UREGEX_CASE_INSENSITIVE.
225 * @param pe Receives the position (line and column numbers) of any error
226 * within the regular expression.)
227 * @param status A reference to a UErrorCode to receive any errors.
228 * @return A regexPattern object for the compiled pattern.
229 *
230 * @stable ICU 2.4
231 */
232 static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
233 uint32_t flags,
234 UParseError &pe,
235 UErrorCode &status);
236
237 /**
238 * Compiles the regular expression in string form into a RegexPattern
239 * object using the specified #URegexpFlag match mode flags. These compile methods,
240 * rather than the constructors, are the usual way that RegexPattern objects
241 * are created.
242 *
243 * Note that RegexPattern objects must not be deleted while RegexMatcher
244 * objects created from the pattern are active. RegexMatchers keep a pointer
245 * back to their pattern, so premature deletion of the pattern is a
246 * catastrophic error.
247 *
248 * Note that it is often more convenient to construct a RegexMatcher directly
249 * from a pattern string instead of than separately compiling the pattern and
250 * then creating a RegexMatcher object from the pattern.
251 *
252 * @param regex The regular expression to be compiled. Note, the text referred
253 * to by this UText must not be deleted during the lifetime of the
254 * RegexPattern object or any RegexMatcher object created from it.
255 * @param flags The #URegexpFlag match mode flags to be used, e.g. #UREGEX_CASE_INSENSITIVE.
256 * @param pe Receives the position (line and column numbers) of any error
257 * within the regular expression.)
258 * @param status A reference to a UErrorCode to receive any errors.
259 * @return A regexPattern object for the compiled pattern.
260 *
261 * @stable ICU 4.6
262 */
263 static RegexPattern * U_EXPORT2 compile( UText *regex,
264 uint32_t flags,
265 UParseError &pe,
266 UErrorCode &status);
267
268 /**
269 * Compiles the regular expression in string form into a RegexPattern
270 * object using the specified #URegexpFlag match mode flags. These compile methods,
271 * rather than the constructors, are the usual way that RegexPattern objects
272 * are created.
273 *
274 * Note that RegexPattern objects must not be deleted while RegexMatcher
275 * objects created from the pattern are active. RegexMatchers keep a pointer
276 * back to their pattern, so premature deletion of the pattern is a
277 * catastrophic error.
278 *
279 * Note that it is often more convenient to construct a RegexMatcher directly
280 * from a pattern string instead of than separately compiling the pattern and
281 * then creating a RegexMatcher object from the pattern.
282 *
283 * @param regex The regular expression to be compiled.
284 * @param flags The #URegexpFlag match mode flags to be used, e.g. #UREGEX_CASE_INSENSITIVE.
285 * @param status A reference to a UErrorCode to receive any errors.
286 * @return A regexPattern object for the compiled pattern.
287 *
288 * @stable ICU 2.6
289 */
290 static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
291 uint32_t flags,
292 UErrorCode &status);
293
294 /**
295 * Compiles the regular expression in string form into a RegexPattern
296 * object using the specified #URegexpFlag match mode flags. These compile methods,
297 * rather than the constructors, are the usual way that RegexPattern objects
298 * are created.
299 *
300 * Note that RegexPattern objects must not be deleted while RegexMatcher
301 * objects created from the pattern are active. RegexMatchers keep a pointer
302 * back to their pattern, so premature deletion of the pattern is a
303 * catastrophic error.
304 *
305 * Note that it is often more convenient to construct a RegexMatcher directly
306 * from a pattern string instead of than separately compiling the pattern and
307 * then creating a RegexMatcher object from the pattern.
308 *
309 * @param regex The regular expression to be compiled. Note, the text referred
310 * to by this UText must not be deleted during the lifetime of the
311 * RegexPattern object or any RegexMatcher object created from it.
312 * @param flags The #URegexpFlag match mode flags to be used, e.g. #UREGEX_CASE_INSENSITIVE.
313 * @param status A reference to a UErrorCode to receive any errors.
314 * @return A regexPattern object for the compiled pattern.
315 *
316 * @stable ICU 4.6
317 */
318 static RegexPattern * U_EXPORT2 compile( UText *regex,
319 uint32_t flags,
320 UErrorCode &status);
321
322 /**
323 * Get the #URegexpFlag match mode flags that were used when compiling this pattern.
324 * @return the #URegexpFlag match mode flags
325 * @stable ICU 2.4
326 */
327 virtual uint32_t flags() const;
328
329 /**
330 * Creates a RegexMatcher that will match the given input against this pattern. The
331 * RegexMatcher can then be used to perform match, find or replace operations
332 * on the input. Note that a RegexPattern object must not be deleted while
333 * RegexMatchers created from it still exist and might possibly be used again.
334 *
335 * The matcher will retain a reference to the supplied input string, and all regexp
336 * pattern matching operations happen directly on this original string. It is
337 * critical that the string not be altered or deleted before use by the regular
338 * expression operations is complete.
339 *
340 * @param input The input string to which the regular expression will be applied.
341 * @param status A reference to a UErrorCode to receive any errors.
342 * @return A RegexMatcher object for this pattern and input.
343 *
344 * @stable ICU 2.4
345 */
346 virtual RegexMatcher *matcher(const UnicodeString &input,
347 UErrorCode &status) const;
348
349 private:
350 /**
351 * Cause a compilation error if an application accidentally attempts to
352 * create a matcher with a (char16_t *) string as input rather than
353 * a UnicodeString. Avoids a dangling reference to a temporary string.
354 *
355 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
356 * using one of the aliasing constructors, such as
357 * `UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);`
358 * or in a UText, using
359 * `utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);`
360 *
361 */
362 RegexMatcher *matcher(const char16_t *input,
363 UErrorCode &status) const;
364 public:
365
366
367 /**
368 * Creates a RegexMatcher that will match against this pattern. The
369 * RegexMatcher can be used to perform match, find or replace operations.
370 * Note that a RegexPattern object must not be deleted while
371 * RegexMatchers created from it still exist and might possibly be used again.
372 *
373 * @param status A reference to a UErrorCode to receive any errors.
374 * @return A RegexMatcher object for this pattern and input.
375 *
376 * @stable ICU 2.6
377 */
378 virtual RegexMatcher *matcher(UErrorCode &status) const;
379
380
381 /**
382 * Test whether a string matches a regular expression. This convenience function
383 * both compiles the regular expression and applies it in a single operation.
384 * Note that if the same pattern needs to be applied repeatedly, this method will be
385 * less efficient than creating and reusing a RegexMatcher object.
386 *
387 * @param regex The regular expression
388 * @param input The string data to be matched
389 * @param pe Receives the position of any syntax errors within the regular expression
390 * @param status A reference to a UErrorCode to receive any errors.
391 * @return True if the regular expression exactly matches the full input string.
392 *
393 * @stable ICU 2.4
394 */
395 static UBool U_EXPORT2 matches(const UnicodeString &regex,
396 const UnicodeString &input,
397 UParseError &pe,
398 UErrorCode &status);
399
400 /**
401 * Test whether a string matches a regular expression. This convenience function
402 * both compiles the regular expression and applies it in a single operation.
403 * Note that if the same pattern needs to be applied repeatedly, this method will be
404 * less efficient than creating and reusing a RegexMatcher object.
405 *
406 * @param regex The regular expression
407 * @param input The string data to be matched
408 * @param pe Receives the position of any syntax errors within the regular expression
409 * @param status A reference to a UErrorCode to receive any errors.
410 * @return True if the regular expression exactly matches the full input string.
411 *
412 * @stable ICU 4.6
413 */
414 static UBool U_EXPORT2 matches(UText *regex,
415 UText *input,
416 UParseError &pe,
417 UErrorCode &status);
418
419 /**
420 * Returns the regular expression from which this pattern was compiled. This method will work
421 * even if the pattern was compiled from a UText.
422 *
423 * Note: If the pattern was originally compiled from a UText, and that UText was modified,
424 * the returned string may no longer reflect the RegexPattern object.
425 * @stable ICU 2.4
426 */
427 virtual UnicodeString pattern() const;
428
429
430 /**
431 * Returns the regular expression from which this pattern was compiled. This method will work
432 * even if the pattern was compiled from a UnicodeString.
433 *
434 * Note: This is the original input, not a clone. If the pattern was originally compiled from a
435 * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern
436 * object.
437 *
438 * @stable ICU 4.6
439 */
440 virtual UText *patternText(UErrorCode &status) const;
441
442
443 /**
444 * Get the group number corresponding to a named capture group.
445 * The returned number can be used with any function that access
446 * capture groups by number.
447 *
448 * The function returns an error status if the specified name does not
449 * appear in the pattern.
450 *
451 * @param groupName The capture group name.
452 * @param status A UErrorCode to receive any errors.
453 *
454 * @stable ICU 55
455 */
456 virtual int32_t groupNumberFromName(const UnicodeString &groupName, UErrorCode &status) const;
457
458
459 /**
460 * Get the group number corresponding to a named capture group.
461 * The returned number can be used with any function that access
462 * capture groups by number.
463 *
464 * The function returns an error status if the specified name does not
465 * appear in the pattern.
466 *
467 * @param groupName The capture group name,
468 * platform invariant characters only.
469 * @param nameLength The length of the name, or -1 if the name is
470 * nul-terminated.
471 * @param status A UErrorCode to receive any errors.
472 *
473 * @stable ICU 55
474 */
475 virtual int32_t groupNumberFromName(const char *groupName, int32_t nameLength, UErrorCode &status) const;
476
477
478 /**
479 * Split a string into fields. Somewhat like split() from Perl or Java.
480 * Pattern matches identify delimiters that separate the input
481 * into fields. The input data between the delimiters becomes the
482 * fields themselves.
483 *
484 * If the delimiter pattern includes capture groups, the captured text will
485 * also appear in the destination array of output strings, interspersed
486 * with the fields. This is similar to Perl, but differs from Java,
487 * which ignores the presence of capture groups in the pattern.
488 *
489 * Trailing empty fields will always be returned, assuming sufficient
490 * destination capacity. This differs from the default behavior for Java
491 * and Perl where trailing empty fields are not returned.
492 *
493 * The number of strings produced by the split operation is returned.
494 * This count includes the strings from capture groups in the delimiter pattern.
495 * This behavior differs from Java, which ignores capture groups.
496 *
497 * For the best performance on split() operations,
498 * <code>RegexMatcher::split</code> is preferable to this function
499 *
500 * @param input The string to be split into fields. The field delimiters
501 * match the pattern (in the "this" object)
502 * @param dest An array of UnicodeStrings to receive the results of the split.
503 * This is an array of actual UnicodeString objects, not an
504 * array of pointers to strings. Local (stack based) arrays can
505 * work well here.
506 * @param destCapacity The number of elements in the destination array.
507 * If the number of fields found is less than destCapacity, the
508 * extra strings in the destination array are not altered.
509 * If the number of destination strings is less than the number
510 * of fields, the trailing part of the input string, including any
511 * field delimiters, is placed in the last destination string.
512 * @param status A reference to a UErrorCode to receive any errors.
513 * @return The number of fields into which the input string was split.
514 * @stable ICU 2.4
515 */
516 virtual int32_t split(const UnicodeString &input,
517 UnicodeString dest[],
518 int32_t destCapacity,
519 UErrorCode &status) const;
520
521
522 /**
523 * Split a string into fields. Somewhat like %split() from Perl or Java.
524 * Pattern matches identify delimiters that separate the input
525 * into fields. The input data between the delimiters becomes the
526 * fields themselves.
527 *
528 * If the delimiter pattern includes capture groups, the captured text will
529 * also appear in the destination array of output strings, interspersed
530 * with the fields. This is similar to Perl, but differs from Java,
531 * which ignores the presence of capture groups in the pattern.
532 *
533 * Trailing empty fields will always be returned, assuming sufficient
534 * destination capacity. This differs from the default behavior for Java
535 * and Perl where trailing empty fields are not returned.
536 *
537 * The number of strings produced by the split operation is returned.
538 * This count includes the strings from capture groups in the delimiter pattern.
539 * This behavior differs from Java, which ignores capture groups.
540 *
541 * For the best performance on split() operations,
542 * `RegexMatcher::split()` is preferable to this function
543 *
544 * @param input The string to be split into fields. The field delimiters
545 * match the pattern (in the "this" object)
546 * @param dest An array of mutable UText structs to receive the results of the split.
547 * If a field is NULL, a new UText is allocated to contain the results for
548 * that field. This new UText is not guaranteed to be mutable.
549 * @param destCapacity The number of elements in the destination array.
550 * If the number of fields found is less than destCapacity, the
551 * extra strings in the destination array are not altered.
552 * If the number of destination strings is less than the number
553 * of fields, the trailing part of the input string, including any
554 * field delimiters, is placed in the last destination string.
555 * @param status A reference to a UErrorCode to receive any errors.
556 * @return The number of destination strings used.
557 *
558 * @stable ICU 4.6
559 */
560 virtual int32_t split(UText *input,
561 UText *dest[],
562 int32_t destCapacity,
563 UErrorCode &status) const;
564
565
566 /**
567 * ICU "poor man's RTTI", returns a UClassID for the actual class.
568 *
569 * @stable ICU 2.4
570 */
571 virtual UClassID getDynamicClassID() const;
572
573 /**
574 * ICU "poor man's RTTI", returns a UClassID for this class.
575 *
576 * @stable ICU 2.4
577 */
578 static UClassID U_EXPORT2 getStaticClassID();
579
580 private:
581 //
582 // Implementation Data
583 //
584 UText *fPattern; // The original pattern string.
585 UnicodeString *fPatternString; // The original pattern UncodeString if relevant
586 uint32_t fFlags; // The flags used when compiling the pattern.
587 //
588 UVector64 *fCompiledPat; // The compiled pattern p-code.
589 UnicodeString fLiteralText; // Any literal string data from the pattern,
590 // after un-escaping, for use during the match.
591
592 UVector *fSets; // Any UnicodeSets referenced from the pattern.
593 Regex8BitSet *fSets8; // (and fast sets for latin-1 range.)
594
595
596 UErrorCode fDeferredStatus; // status if some prior error has left this
597 // RegexPattern in an unusable state.
598
599 int32_t fMinMatchLen; // Minimum Match Length. All matches will have length
600 // >= this value. For some patterns, this calculated
601 // value may be less than the true shortest
602 // possible match.
603
604 int32_t fFrameSize; // Size of a state stack frame in the
605 // execution engine.
606
607 int32_t fDataSize; // The size of the data needed by the pattern that
608 // does not go on the state stack, but has just
609 // a single copy per matcher.
610
611 UVector32 *fGroupMap; // Map from capture group number to position of
612 // the group's variables in the matcher stack frame.
613
614 UnicodeSet **fStaticSets; // Ptr to static (shared) sets for predefined
615 // regex character classes, e.g. Word.
616
617 Regex8BitSet *fStaticSets8; // Ptr to the static (shared) latin-1 only
618 // sets for predefined regex classes.
619
620 int32_t fStartType; // Info on how a match must start.
621 int32_t fInitialStringIdx; //
622 int32_t fInitialStringLen;
623 UnicodeSet *fInitialChars;
624 UChar32 fInitialChar;
625 Regex8BitSet *fInitialChars8;
626 UBool fNeedsAltInput;
627
628 UHashtable *fNamedCaptureMap; // Map from capture group names to numbers.
629
630 friend class RegexCompile;
631 friend class RegexMatcher;
632 friend class RegexCImpl;
633
634 //
635 // Implementation Methods
636 //
637 void init(); // Common initialization, for use by constructors.
638 void zap(); // Common cleanup
639
640 void dumpOp(int32_t index) const;
641
642 public:
643 #ifndef U_HIDE_INTERNAL_API
644 /**
645 * Dump a compiled pattern. Internal debug function.
646 * @internal
647 */
648 void dumpPattern() const;
649 #endif /* U_HIDE_INTERNAL_API */
650 };
651
652
653
654 /**
655 * class RegexMatcher bundles together a regular expression pattern and
656 * input text to which the expression can be applied. It includes methods
657 * for testing for matches, and for find and replace operations.
658 *
659 * <p>Class RegexMatcher is not intended to be subclassed.</p>
660 *
661 * @stable ICU 2.4
662 */
663 class U_I18N_API RegexMatcher U_FINAL : public UObject {
664 public:
665
666 /**
667 * Construct a RegexMatcher for a regular expression.
668 * This is a convenience method that avoids the need to explicitly create
669 * a RegexPattern object. Note that if several RegexMatchers need to be
670 * created for the same expression, it will be more efficient to
671 * separately create and cache a RegexPattern object, and use
672 * its matcher() method to create the RegexMatcher objects.
673 *
674 * @param regexp The Regular Expression to be compiled.
675 * @param flags #URegexpFlag options, such as #UREGEX_CASE_INSENSITIVE.
676 * @param status Any errors are reported by setting this UErrorCode variable.
677 * @stable ICU 2.6
678 */
679 RegexMatcher(const UnicodeString &regexp, uint32_t flags, UErrorCode &status);
680
681 /**
682 * Construct a RegexMatcher for a regular expression.
683 * This is a convenience method that avoids the need to explicitly create
684 * a RegexPattern object. Note that if several RegexMatchers need to be
685 * created for the same expression, it will be more efficient to
686 * separately create and cache a RegexPattern object, and use
687 * its matcher() method to create the RegexMatcher objects.
688 *
689 * @param regexp The regular expression to be compiled.
690 * @param flags #URegexpFlag options, such as #UREGEX_CASE_INSENSITIVE.
691 * @param status Any errors are reported by setting this UErrorCode variable.
692 *
693 * @stable ICU 4.6
694 */
695 RegexMatcher(UText *regexp, uint32_t flags, UErrorCode &status);
696
697 /**
698 * Construct a RegexMatcher for a regular expression.
699 * This is a convenience method that avoids the need to explicitly create
700 * a RegexPattern object. Note that if several RegexMatchers need to be
701 * created for the same expression, it will be more efficient to
702 * separately create and cache a RegexPattern object, and use
703 * its matcher() method to create the RegexMatcher objects.
704 *
705 * The matcher will retain a reference to the supplied input string, and all regexp
706 * pattern matching operations happen directly on the original string. It is
707 * critical that the string not be altered or deleted before use by the regular
708 * expression operations is complete.
709 *
710 * @param regexp The Regular Expression to be compiled.
711 * @param input The string to match. The matcher retains a reference to the
712 * caller's string; mo copy is made.
713 * @param flags #URegexpFlag options, such as #UREGEX_CASE_INSENSITIVE.
714 * @param status Any errors are reported by setting this UErrorCode variable.
715 * @stable ICU 2.6
716 */
717 RegexMatcher(const UnicodeString &regexp, const UnicodeString &input,
718 uint32_t flags, UErrorCode &status);
719
720 /**
721 * Construct a RegexMatcher for a regular expression.
722 * This is a convenience method that avoids the need to explicitly create
723 * a RegexPattern object. Note that if several RegexMatchers need to be
724 * created for the same expression, it will be more efficient to
725 * separately create and cache a RegexPattern object, and use
726 * its matcher() method to create the RegexMatcher objects.
727 *
728 * The matcher will make a shallow clone of the supplied input text, and all regexp
729 * pattern matching operations happen on this clone. While read-only operations on
730 * the supplied text are permitted, it is critical that the underlying string not be
731 * altered or deleted before use by the regular expression operations is complete.
732 *
733 * @param regexp The Regular Expression to be compiled.
734 * @param input The string to match. The matcher retains a shallow clone of the text.
735 * @param flags #URegexpFlag options, such as #UREGEX_CASE_INSENSITIVE.
736 * @param status Any errors are reported by setting this UErrorCode variable.
737 *
738 * @stable ICU 4.6
739 */
740 RegexMatcher(UText *regexp, UText *input,
741 uint32_t flags, UErrorCode &status);
742
743 private:
744 /**
745 * Cause a compilation error if an application accidentally attempts to
746 * create a matcher with a (char16_t *) string as input rather than
747 * a UnicodeString. Avoids a dangling reference to a temporary string.
748 *
749 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
750 * using one of the aliasing constructors, such as
751 * `UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);`
752 * or in a UText, using
753 * `utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);`
754 */
755 RegexMatcher(const UnicodeString &regexp, const char16_t *input,
756 uint32_t flags, UErrorCode &status);
757 public:
758
759
760 /**
761 * Destructor.
762 *
763 * @stable ICU 2.4
764 */
765 virtual ~RegexMatcher();
766
767
768 /**
769 * Attempts to match the entire input region against the pattern.
770 * @param status A reference to a UErrorCode to receive any errors.
771 * @return TRUE if there is a match
772 * @stable ICU 2.4
773 */
774 virtual UBool matches(UErrorCode &status);
775
776
777 /**
778 * Resets the matcher, then attempts to match the input beginning
779 * at the specified startIndex, and extending to the end of the input.
780 * The input region is reset to include the entire input string.
781 * A successful match must extend to the end of the input.
782 * @param startIndex The input string (native) index at which to begin matching.
783 * @param status A reference to a UErrorCode to receive any errors.
784 * @return TRUE if there is a match
785 * @stable ICU 2.8
786 */
787 virtual UBool matches(int64_t startIndex, UErrorCode &status);
788
789
790 /**
791 * Attempts to match the input string, starting from the beginning of the region,
792 * against the pattern. Like the matches() method, this function
793 * always starts at the beginning of the input region;
794 * unlike that function, it does not require that the entire region be matched.
795 *
796 * If the match succeeds then more information can be obtained via the start(),
797 * end(), and group() functions.
798 *
799 * @param status A reference to a UErrorCode to receive any errors.
800 * @return TRUE if there is a match at the start of the input string.
801 * @stable ICU 2.4
802 */
803 virtual UBool lookingAt(UErrorCode &status);
804
805
806 /**
807 * Attempts to match the input string, starting from the specified index, against the pattern.
808 * The match may be of any length, and is not required to extend to the end
809 * of the input string. Contrast with match().
810 *
811 * If the match succeeds then more information can be obtained via the start(),
812 * end(), and group() functions.
813 *
814 * @param startIndex The input string (native) index at which to begin matching.
815 * @param status A reference to a UErrorCode to receive any errors.
816 * @return TRUE if there is a match.
817 * @stable ICU 2.8
818 */
819 virtual UBool lookingAt(int64_t startIndex, UErrorCode &status);
820
821
822 /**
823 * Find the next pattern match in the input string.
824 * The find begins searching the input at the location following the end of
825 * the previous match, or at the start of the string if there is no previous match.
826 * If a match is found, `start()`, `end()` and `group()`
827 * will provide more information regarding the match.
828 * Note that if the input string is changed by the application,
829 * use find(startPos, status) instead of find(), because the saved starting
830 * position may not be valid with the altered input string.
831 * @return TRUE if a match is found.
832 * @stable ICU 2.4
833 */
834 virtual UBool find();
835
836
837 /**
838 * Find the next pattern match in the input string.
839 * The find begins searching the input at the location following the end of
840 * the previous match, or at the start of the string if there is no previous match.
841 * If a match is found, `start()`, `end()` and `group()`
842 * will provide more information regarding the match.
843 *
844 * Note that if the input string is changed by the application,
845 * use find(startPos, status) instead of find(), because the saved starting
846 * position may not be valid with the altered input string.
847 * @param status A reference to a UErrorCode to receive any errors.
848 * @return TRUE if a match is found.
849 * @stable ICU 55
850 */
851 virtual UBool find(UErrorCode &status);
852
853 /**
854 * Resets this RegexMatcher and then attempts to find the next substring of the
855 * input string that matches the pattern, starting at the specified index.
856 *
857 * @param start The (native) index in the input string to begin the search.
858 * @param status A reference to a UErrorCode to receive any errors.
859 * @return TRUE if a match is found.
860 * @stable ICU 2.4
861 */
862 virtual UBool find(int64_t start, UErrorCode &status);
863
864
865 /**
866 * Returns a string containing the text matched by the previous match.
867 * If the pattern can match an empty string, an empty string may be returned.
868 * @param status A reference to a UErrorCode to receive any errors.
869 * Possible errors are U_REGEX_INVALID_STATE if no match
870 * has been attempted or the last match failed.
871 * @return a string containing the matched input text.
872 * @stable ICU 2.4
873 */
874 virtual UnicodeString group(UErrorCode &status) const;
875
876
877 /**
878 * Returns a string containing the text captured by the given group
879 * during the previous match operation. Group(0) is the entire match.
880 *
881 * A zero length string is returned both for capture groups that did not
882 * participate in the match and for actual zero length matches.
883 * To distinguish between these two cases use the function start(),
884 * which returns -1 for non-participating groups.
885 *
886 * @param groupNum the capture group number
887 * @param status A reference to a UErrorCode to receive any errors.
888 * Possible errors are U_REGEX_INVALID_STATE if no match
889 * has been attempted or the last match failed and
890 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
891 * @return the captured text
892 * @stable ICU 2.4
893 */
894 virtual UnicodeString group(int32_t groupNum, UErrorCode &status) const;
895
896 /**
897 * Returns the number of capturing groups in this matcher's pattern.
898 * @return the number of capture groups
899 * @stable ICU 2.4
900 */
901 virtual int32_t groupCount() const;
902
903
904 /**
905 * Returns a shallow clone of the entire live input string with the UText current native index
906 * set to the beginning of the requested group.
907 *
908 * @param dest The UText into which the input should be cloned, or NULL to create a new UText
909 * @param group_len A reference to receive the length of the desired capture group
910 * @param status A reference to a UErrorCode to receive any errors.
911 * Possible errors are U_REGEX_INVALID_STATE if no match
912 * has been attempted or the last match failed and
913 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
914 * @return dest if non-NULL, a shallow copy of the input text otherwise
915 *
916 * @stable ICU 4.6
917 */
918 virtual UText *group(UText *dest, int64_t &group_len, UErrorCode &status) const;
919
920 /**
921 * Returns a shallow clone of the entire live input string with the UText current native index
922 * set to the beginning of the requested group.
923 *
924 * A group length of zero is returned both for capture groups that did not
925 * participate in the match and for actual zero length matches.
926 * To distinguish between these two cases use the function start(),
927 * which returns -1 for non-participating groups.
928 *
929 * @param groupNum The capture group number.
930 * @param dest The UText into which the input should be cloned, or NULL to create a new UText.
931 * @param group_len A reference to receive the length of the desired capture group
932 * @param status A reference to a UErrorCode to receive any errors.
933 * Possible errors are U_REGEX_INVALID_STATE if no match
934 * has been attempted or the last match failed and
935 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
936 * @return dest if non-NULL, a shallow copy of the input text otherwise
937 *
938 * @stable ICU 4.6
939 */
940 virtual UText *group(int32_t groupNum, UText *dest, int64_t &group_len, UErrorCode &status) const;
941
942 /**
943 * Returns the index in the input string of the start of the text matched
944 * during the previous match operation.
945 * @param status a reference to a UErrorCode to receive any errors.
946 * @return The (native) position in the input string of the start of the last match.
947 * @stable ICU 2.4
948 */
949 virtual int32_t start(UErrorCode &status) const;
950
951 /**
952 * Returns the index in the input string of the start of the text matched
953 * during the previous match operation.
954 * @param status a reference to a UErrorCode to receive any errors.
955 * @return The (native) position in the input string of the start of the last match.
956 * @stable ICU 4.6
957 */
958 virtual int64_t start64(UErrorCode &status) const;
959
960
961 /**
962 * Returns the index in the input string of the start of the text matched by the
963 * specified capture group during the previous match operation. Return -1 if
964 * the capture group exists in the pattern, but was not part of the last match.
965 *
966 * @param group the capture group number
967 * @param status A reference to a UErrorCode to receive any errors. Possible
968 * errors are U_REGEX_INVALID_STATE if no match has been
969 * attempted or the last match failed, and
970 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
971 * @return the (native) start position of substring matched by the specified group.
972 * @stable ICU 2.4
973 */
974 virtual int32_t start(int32_t group, UErrorCode &status) const;
975
976 /**
977 * Returns the index in the input string of the start of the text matched by the
978 * specified capture group during the previous match operation. Return -1 if
979 * the capture group exists in the pattern, but was not part of the last match.
980 *
981 * @param group the capture group number.
982 * @param status A reference to a UErrorCode to receive any errors. Possible
983 * errors are U_REGEX_INVALID_STATE if no match has been
984 * attempted or the last match failed, and
985 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
986 * @return the (native) start position of substring matched by the specified group.
987 * @stable ICU 4.6
988 */
989 virtual int64_t start64(int32_t group, UErrorCode &status) const;
990
991 /**
992 * Returns the index in the input string of the first character following the
993 * text matched during the previous match operation.
994 *
995 * @param status A reference to a UErrorCode to receive any errors. Possible
996 * errors are U_REGEX_INVALID_STATE if no match has been
997 * attempted or the last match failed.
998 * @return the index of the last character matched, plus one.
999 * The index value returned is a native index, corresponding to
1000 * code units for the underlying encoding type, for example,
1001 * a byte index for UTF-8.
1002 * @stable ICU 2.4
1003 */
1004 virtual int32_t end(UErrorCode &status) const;
1005
1006 /**
1007 * Returns the index in the input string of the first character following the
1008 * text matched during the previous match operation.
1009 *
1010 * @param status A reference to a UErrorCode to receive any errors. Possible
1011 * errors are U_REGEX_INVALID_STATE if no match has been
1012 * attempted or the last match failed.
1013 * @return the index of the last character matched, plus one.
1014 * The index value returned is a native index, corresponding to
1015 * code units for the underlying encoding type, for example,
1016 * a byte index for UTF-8.
1017 * @stable ICU 4.6
1018 */
1019 virtual int64_t end64(UErrorCode &status) const;
1020
1021
1022 /**
1023 * Returns the index in the input string of the character following the
1024 * text matched by the specified capture group during the previous match operation.
1025 *
1026 * @param group the capture group number
1027 * @param status A reference to a UErrorCode to receive any errors. Possible
1028 * errors are U_REGEX_INVALID_STATE if no match has been
1029 * attempted or the last match failed and
1030 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1031 * @return the index of the first character following the text
1032 * captured by the specified group during the previous match operation.
1033 * Return -1 if the capture group exists in the pattern but was not part of the match.
1034 * The index value returned is a native index, corresponding to
1035 * code units for the underlying encoding type, for example,
1036 * a byte index for UTF8.
1037 * @stable ICU 2.4
1038 */
1039 virtual int32_t end(int32_t group, UErrorCode &status) const;
1040
1041 /**
1042 * Returns the index in the input string of the character following the
1043 * text matched by the specified capture group during the previous match operation.
1044 *
1045 * @param group the capture group number
1046 * @param status A reference to a UErrorCode to receive any errors. Possible
1047 * errors are U_REGEX_INVALID_STATE if no match has been
1048 * attempted or the last match failed and
1049 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1050 * @return the index of the first character following the text
1051 * captured by the specified group during the previous match operation.
1052 * Return -1 if the capture group exists in the pattern but was not part of the match.
1053 * The index value returned is a native index, corresponding to
1054 * code units for the underlying encoding type, for example,
1055 * a byte index for UTF8.
1056 * @stable ICU 4.6
1057 */
1058 virtual int64_t end64(int32_t group, UErrorCode &status) const;
1059
1060 /**
1061 * Resets this matcher. The effect is to remove any memory of previous matches,
1062 * and to cause subsequent find() operations to begin at the beginning of
1063 * the input string.
1064 *
1065 * @return this RegexMatcher.
1066 * @stable ICU 2.4
1067 */
1068 virtual RegexMatcher &reset();
1069
1070
1071 /**
1072 * Resets this matcher, and set the current input position.
1073 * The effect is to remove any memory of previous matches,
1074 * and to cause subsequent find() operations to begin at
1075 * the specified (native) position in the input string.
1076 *
1077 * The matcher's region is reset to its default, which is the entire
1078 * input string.
1079 *
1080 * An alternative to this function is to set a match region
1081 * beginning at the desired index.
1082 *
1083 * @return this RegexMatcher.
1084 * @stable ICU 2.8
1085 */
1086 virtual RegexMatcher &reset(int64_t index, UErrorCode &status);
1087
1088
1089 /**
1090 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1091 * to be reused, which is more efficient than creating a new RegexMatcher for
1092 * each input string to be processed.
1093 * @param input The new string on which subsequent pattern matches will operate.
1094 * The matcher retains a reference to the callers string, and operates
1095 * directly on that. Ownership of the string remains with the caller.
1096 * Because no copy of the string is made, it is essential that the
1097 * caller not delete the string until after regexp operations on it
1098 * are done.
1099 * Note that while a reset on the matcher with an input string that is then
1100 * modified across/during matcher operations may be supported currently for UnicodeString,
1101 * this was not originally intended behavior, and support for this is not guaranteed
1102 * in upcoming versions of ICU.
1103 * @return this RegexMatcher.
1104 * @stable ICU 2.4
1105 */
1106 virtual RegexMatcher &reset(const UnicodeString &input);
1107
1108
1109 /**
1110 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1111 * to be reused, which is more efficient than creating a new RegexMatcher for
1112 * each input string to be processed.
1113 * @param input The new string on which subsequent pattern matches will operate.
1114 * The matcher makes a shallow clone of the given text; ownership of the
1115 * original string remains with the caller. Because no deep copy of the
1116 * text is made, it is essential that the caller not modify the string
1117 * until after regexp operations on it are done.
1118 * @return this RegexMatcher.
1119 *
1120 * @stable ICU 4.6
1121 */
1122 virtual RegexMatcher &reset(UText *input);
1123
1124
1125 /**
1126 * Set the subject text string upon which the regular expression is looking for matches
1127 * without changing any other aspect of the matching state.
1128 * The new and previous text strings must have the same content.
1129 *
1130 * This function is intended for use in environments where ICU is operating on
1131 * strings that may move around in memory. It provides a mechanism for notifying
1132 * ICU that the string has been relocated, and providing a new UText to access the
1133 * string in its new position.
1134 *
1135 * Note that the regular expression implementation never copies the underlying text
1136 * of a string being matched, but always operates directly on the original text
1137 * provided by the user. Refreshing simply drops the references to the old text
1138 * and replaces them with references to the new.
1139 *
1140 * Caution: this function is normally used only by very specialized,
1141 * system-level code. One example use case is with garbage collection that moves
1142 * the text in memory.
1143 *
1144 * @param input The new (moved) text string.
1145 * @param status Receives errors detected by this function.
1146 *
1147 * @stable ICU 4.8
1148 */
1149 virtual RegexMatcher &refreshInputText(UText *input, UErrorCode &status);
1150
1151 private:
1152 /**
1153 * Cause a compilation error if an application accidentally attempts to
1154 * reset a matcher with a (char16_t *) string as input rather than
1155 * a UnicodeString. Avoids a dangling reference to a temporary string.
1156 *
1157 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
1158 * using one of the aliasing constructors, such as
1159 * `UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);`
1160 * or in a UText, using
1161 * `utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);`
1162 *
1163 */
1164 RegexMatcher &reset(const char16_t *input);
1165 public:
1166
1167 /**
1168 * Returns the input string being matched. Ownership of the string belongs to
1169 * the matcher; it should not be altered or deleted. This method will work even if the input
1170 * was originally supplied as a UText.
1171 * @return the input string
1172 * @stable ICU 2.4
1173 */
1174 virtual const UnicodeString &input() const;
1175
1176 /**
1177 * Returns the input string being matched. This is the live input text; it should not be
1178 * altered or deleted. This method will work even if the input was originally supplied as
1179 * a UnicodeString.
1180 * @return the input text
1181 *
1182 * @stable ICU 4.6
1183 */
1184 virtual UText *inputText() const;
1185
1186 /**
1187 * Returns the input string being matched, either by copying it into the provided
1188 * UText parameter or by returning a shallow clone of the live input. Note that copying
1189 * the entire input may cause significant performance and memory issues.
1190 * @param dest The UText into which the input should be copied, or NULL to create a new UText
1191 * @param status error code
1192 * @return dest if non-NULL, a shallow copy of the input text otherwise
1193 *
1194 * @stable ICU 4.6
1195 */
1196 virtual UText *getInput(UText *dest, UErrorCode &status) const;
1197
1198
1199 /** Sets the limits of this matcher's region.
1200 * The region is the part of the input string that will be searched to find a match.
1201 * Invoking this method resets the matcher, and then sets the region to start
1202 * at the index specified by the start parameter and end at the index specified
1203 * by the end parameter.
1204 *
1205 * Depending on the transparency and anchoring being used (see useTransparentBounds
1206 * and useAnchoringBounds), certain constructs such as anchors may behave differently
1207 * at or around the boundaries of the region
1208 *
1209 * The function will fail if start is greater than limit, or if either index
1210 * is less than zero or greater than the length of the string being matched.
1211 *
1212 * @param start The (native) index to begin searches at.
1213 * @param limit The index to end searches at (exclusive).
1214 * @param status A reference to a UErrorCode to receive any errors.
1215 * @stable ICU 4.0
1216 */
1217 virtual RegexMatcher &region(int64_t start, int64_t limit, UErrorCode &status);
1218
1219 /**
1220 * Identical to region(start, limit, status) but also allows a start position without
1221 * resetting the region state.
1222 * @param regionStart The region start
1223 * @param regionLimit the limit of the region
1224 * @param startIndex The (native) index within the region bounds at which to begin searches.
1225 * @param status A reference to a UErrorCode to receive any errors.
1226 * If startIndex is not within the specified region bounds,
1227 * U_INDEX_OUTOFBOUNDS_ERROR is returned.
1228 * @stable ICU 4.6
1229 */
1230 virtual RegexMatcher &region(int64_t regionStart, int64_t regionLimit, int64_t startIndex, UErrorCode &status);
1231
1232 /**
1233 * Reports the start index of this matcher's region. The searches this matcher
1234 * conducts are limited to finding matches within regionStart (inclusive) and
1235 * regionEnd (exclusive).
1236 *
1237 * @return The starting (native) index of this matcher's region.
1238 * @stable ICU 4.0
1239 */
1240 virtual int32_t regionStart() const;
1241
1242 /**
1243 * Reports the start index of this matcher's region. The searches this matcher
1244 * conducts are limited to finding matches within regionStart (inclusive) and
1245 * regionEnd (exclusive).
1246 *
1247 * @return The starting (native) index of this matcher's region.
1248 * @stable ICU 4.6
1249 */
1250 virtual int64_t regionStart64() const;
1251
1252
1253 /**
1254 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1255 * this matcher conducts are limited to finding matches within regionStart
1256 * (inclusive) and regionEnd (exclusive).
1257 *
1258 * @return The ending point (native) of this matcher's region.
1259 * @stable ICU 4.0
1260 */
1261 virtual int32_t regionEnd() const;
1262
1263 /**
1264 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1265 * this matcher conducts are limited to finding matches within regionStart
1266 * (inclusive) and regionEnd (exclusive).
1267 *
1268 * @return The ending point (native) of this matcher's region.
1269 * @stable ICU 4.6
1270 */
1271 virtual int64_t regionEnd64() const;
1272
1273 /**
1274 * Queries the transparency of region bounds for this matcher.
1275 * See useTransparentBounds for a description of transparent and opaque bounds.
1276 * By default, a matcher uses opaque region boundaries.
1277 *
1278 * @return TRUE if this matcher is using opaque bounds, false if it is not.
1279 * @stable ICU 4.0
1280 */
1281 virtual UBool hasTransparentBounds() const;
1282
1283 /**
1284 * Sets the transparency of region bounds for this matcher.
1285 * Invoking this function with an argument of true will set this matcher to use transparent bounds.
1286 * If the boolean argument is false, then opaque bounds will be used.
1287 *
1288 * Using transparent bounds, the boundaries of this matcher's region are transparent
1289 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
1290 * see text beyond the boundaries of the region while checking for a match.
1291 *
1292 * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
1293 * lookbehind, and boundary matching constructs.
1294 *
1295 * By default, a matcher uses opaque bounds.
1296 *
1297 * @param b TRUE for transparent bounds; FALSE for opaque bounds
1298 * @return This Matcher;
1299 * @stable ICU 4.0
1300 **/
1301 virtual RegexMatcher &useTransparentBounds(UBool b);
1302
1303
1304 /**
1305 * Return true if this matcher is using anchoring bounds.
1306 * By default, matchers use anchoring region bounds.
1307 *
1308 * @return TRUE if this matcher is using anchoring bounds.
1309 * @stable ICU 4.0
1310 */
1311 virtual UBool hasAnchoringBounds() const;
1312
1313
1314 /**
1315 * Set whether this matcher is using Anchoring Bounds for its region.
1316 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
1317 * and end of the region. Without Anchoring Bounds, anchors will only match at
1318 * the positions they would in the complete text.
1319 *
1320 * Anchoring Bounds are the default for regions.
1321 *
1322 * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
1323 * @return This Matcher
1324 * @stable ICU 4.0
1325 */
1326 virtual RegexMatcher &useAnchoringBounds(UBool b);
1327
1328
1329 /**
1330 * Return TRUE if the most recent matching operation attempted to access
1331 * additional input beyond the available input text.
1332 * In this case, additional input text could change the results of the match.
1333 *
1334 * hitEnd() is defined for both successful and unsuccessful matches.
1335 * In either case hitEnd() will return TRUE if if the end of the text was
1336 * reached at any point during the matching process.
1337 *
1338 * @return TRUE if the most recent match hit the end of input
1339 * @stable ICU 4.0
1340 */
1341 virtual UBool hitEnd() const;
1342
1343 /**
1344 * Return TRUE the most recent match succeeded and additional input could cause
1345 * it to fail. If this method returns false and a match was found, then more input
1346 * might change the match but the match won't be lost. If a match was not found,
1347 * then requireEnd has no meaning.
1348 *
1349 * @return TRUE if more input could cause the most recent match to no longer match.
1350 * @stable ICU 4.0
1351 */
1352 virtual UBool requireEnd() const;
1353
1354
1355 /**
1356 * Returns the pattern that is interpreted by this matcher.
1357 * @return the RegexPattern for this RegexMatcher
1358 * @stable ICU 2.4
1359 */
1360 virtual const RegexPattern &pattern() const;
1361
1362
1363 /**
1364 * Replaces every substring of the input that matches the pattern
1365 * with the given replacement string. This is a convenience function that
1366 * provides a complete find-and-replace-all operation.
1367 *
1368 * This method first resets this matcher. It then scans the input string
1369 * looking for matches of the pattern. Input that is not part of any
1370 * match is left unchanged; each match is replaced in the result by the
1371 * replacement string. The replacement string may contain references to
1372 * capture groups.
1373 *
1374 * @param replacement a string containing the replacement text.
1375 * @param status a reference to a UErrorCode to receive any errors.
1376 * @return a string containing the results of the find and replace.
1377 * @stable ICU 2.4
1378 */
1379 virtual UnicodeString replaceAll(const UnicodeString &replacement, UErrorCode &status);
1380
1381
1382 /**
1383 * Replaces every substring of the input that matches the pattern
1384 * with the given replacement string. This is a convenience function that
1385 * provides a complete find-and-replace-all operation.
1386 *
1387 * This method first resets this matcher. It then scans the input string
1388 * looking for matches of the pattern. Input that is not part of any
1389 * match is left unchanged; each match is replaced in the result by the
1390 * replacement string. The replacement string may contain references to
1391 * capture groups.
1392 *
1393 * @param replacement a string containing the replacement text.
1394 * @param dest a mutable UText in which the results are placed.
1395 * If NULL, a new UText will be created (which may not be mutable).
1396 * @param status a reference to a UErrorCode to receive any errors.
1397 * @return a string containing the results of the find and replace.
1398 * If a pre-allocated UText was provided, it will always be used and returned.
1399 *
1400 * @stable ICU 4.6
1401 */
1402 virtual UText *replaceAll(UText *replacement, UText *dest, UErrorCode &status);
1403
1404
1405 /**
1406 * Replaces the first substring of the input that matches
1407 * the pattern with the replacement string. This is a convenience
1408 * function that provides a complete find-and-replace operation.
1409 *
1410 * This function first resets this RegexMatcher. It then scans the input string
1411 * looking for a match of the pattern. Input that is not part
1412 * of the match is appended directly to the result string; the match is replaced
1413 * in the result by the replacement string. The replacement string may contain
1414 * references to captured groups.
1415 *
1416 * The state of the matcher (the position at which a subsequent find()
1417 * would begin) after completing a replaceFirst() is not specified. The
1418 * RegexMatcher should be reset before doing additional find() operations.
1419 *
1420 * @param replacement a string containing the replacement text.
1421 * @param status a reference to a UErrorCode to receive any errors.
1422 * @return a string containing the results of the find and replace.
1423 * @stable ICU 2.4
1424 */
1425 virtual UnicodeString replaceFirst(const UnicodeString &replacement, UErrorCode &status);
1426
1427
1428 /**
1429 * Replaces the first substring of the input that matches
1430 * the pattern with the replacement string. This is a convenience
1431 * function that provides a complete find-and-replace operation.
1432 *
1433 * This function first resets this RegexMatcher. It then scans the input string
1434 * looking for a match of the pattern. Input that is not part
1435 * of the match is appended directly to the result string; the match is replaced
1436 * in the result by the replacement string. The replacement string may contain
1437 * references to captured groups.
1438 *
1439 * The state of the matcher (the position at which a subsequent find()
1440 * would begin) after completing a replaceFirst() is not specified. The
1441 * RegexMatcher should be reset before doing additional find() operations.
1442 *
1443 * @param replacement a string containing the replacement text.
1444 * @param dest a mutable UText in which the results are placed.
1445 * If NULL, a new UText will be created (which may not be mutable).
1446 * @param status a reference to a UErrorCode to receive any errors.
1447 * @return a string containing the results of the find and replace.
1448 * If a pre-allocated UText was provided, it will always be used and returned.
1449 *
1450 * @stable ICU 4.6
1451 */
1452 virtual UText *replaceFirst(UText *replacement, UText *dest, UErrorCode &status);
1453
1454
1455 /**
1456 * Implements a replace operation intended to be used as part of an
1457 * incremental find-and-replace.
1458 *
1459 * The input string, starting from the end of the previous replacement and ending at
1460 * the start of the current match, is appended to the destination string. Then the
1461 * replacement string is appended to the output string,
1462 * including handling any substitutions of captured text.
1463 *
1464 * For simple, prepackaged, non-incremental find-and-replace
1465 * operations, see replaceFirst() or replaceAll().
1466 *
1467 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1468 * @param replacement A UnicodeString that provides the text to be substituted for
1469 * the input text that matched the regexp pattern. The replacement
1470 * text may contain references to captured text from the
1471 * input.
1472 * @param status A reference to a UErrorCode to receive any errors. Possible
1473 * errors are U_REGEX_INVALID_STATE if no match has been
1474 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1475 * if the replacement text specifies a capture group that
1476 * does not exist in the pattern.
1477 *
1478 * @return this RegexMatcher
1479 * @stable ICU 2.4
1480 *
1481 */
1482 virtual RegexMatcher &appendReplacement(UnicodeString &dest,
1483 const UnicodeString &replacement, UErrorCode &status);
1484
1485
1486 /**
1487 * Implements a replace operation intended to be used as part of an
1488 * incremental find-and-replace.
1489 *
1490 * The input string, starting from the end of the previous replacement and ending at
1491 * the start of the current match, is appended to the destination string. Then the
1492 * replacement string is appended to the output string,
1493 * including handling any substitutions of captured text.
1494 *
1495 * For simple, prepackaged, non-incremental find-and-replace
1496 * operations, see replaceFirst() or replaceAll().
1497 *
1498 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1499 * Must not be NULL.
1500 * @param replacement A UText that provides the text to be substituted for
1501 * the input text that matched the regexp pattern. The replacement
1502 * text may contain references to captured text from the input.
1503 * @param status A reference to a UErrorCode to receive any errors. Possible
1504 * errors are U_REGEX_INVALID_STATE if no match has been
1505 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1506 * if the replacement text specifies a capture group that
1507 * does not exist in the pattern.
1508 *
1509 * @return this RegexMatcher
1510 *
1511 * @stable ICU 4.6
1512 */
1513 virtual RegexMatcher &appendReplacement(UText *dest,
1514 UText *replacement, UErrorCode &status);
1515
1516
1517 /**
1518 * As the final step in a find-and-replace operation, append the remainder
1519 * of the input string, starting at the position following the last appendReplacement(),
1520 * to the destination string. `appendTail()` is intended to be invoked after one
1521 * or more invocations of the `RegexMatcher::appendReplacement()`.
1522 *
1523 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1524 * @return the destination string.
1525 * @stable ICU 2.4
1526 */
1527 virtual UnicodeString &appendTail(UnicodeString &dest);
1528
1529
1530 /**
1531 * As the final step in a find-and-replace operation, append the remainder
1532 * of the input string, starting at the position following the last appendReplacement(),
1533 * to the destination string. `appendTail()` is intended to be invoked after one
1534 * or more invocations of the `RegexMatcher::appendReplacement()`.
1535 *
1536 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1537 * Must not be NULL.
1538 * @param status error cod
1539 * @return the destination string.
1540 *
1541 * @stable ICU 4.6
1542 */
1543 virtual UText *appendTail(UText *dest, UErrorCode &status);
1544
1545
1546 /**
1547 * Split a string into fields. Somewhat like %split() from Perl.
1548 * The pattern matches identify delimiters that separate the input
1549 * into fields. The input data between the matches becomes the
1550 * fields themselves.
1551 *
1552 * @param input The string to be split into fields. The field delimiters
1553 * match the pattern (in the "this" object). This matcher
1554 * will be reset to this input string.
1555 * @param dest An array of UnicodeStrings to receive the results of the split.
1556 * This is an array of actual UnicodeString objects, not an
1557 * array of pointers to strings. Local (stack based) arrays can
1558 * work well here.
1559 * @param destCapacity The number of elements in the destination array.
1560 * If the number of fields found is less than destCapacity, the
1561 * extra strings in the destination array are not altered.
1562 * If the number of destination strings is less than the number
1563 * of fields, the trailing part of the input string, including any
1564 * field delimiters, is placed in the last destination string.
1565 * @param status A reference to a UErrorCode to receive any errors.
1566 * @return The number of fields into which the input string was split.
1567 * @stable ICU 2.6
1568 */
1569 virtual int32_t split(const UnicodeString &input,
1570 UnicodeString dest[],
1571 int32_t destCapacity,
1572 UErrorCode &status);
1573
1574
1575 /**
1576 * Split a string into fields. Somewhat like %split() from Perl.
1577 * The pattern matches identify delimiters that separate the input
1578 * into fields. The input data between the matches becomes the
1579 * fields themselves.
1580 *
1581 * @param input The string to be split into fields. The field delimiters
1582 * match the pattern (in the "this" object). This matcher
1583 * will be reset to this input string.
1584 * @param dest An array of mutable UText structs to receive the results of the split.
1585 * If a field is NULL, a new UText is allocated to contain the results for
1586 * that field. This new UText is not guaranteed to be mutable.
1587 * @param destCapacity The number of elements in the destination array.
1588 * If the number of fields found is less than destCapacity, the
1589 * extra strings in the destination array are not altered.
1590 * If the number of destination strings is less than the number
1591 * of fields, the trailing part of the input string, including any
1592 * field delimiters, is placed in the last destination string.
1593 * @param status A reference to a UErrorCode to receive any errors.
1594 * @return The number of fields into which the input string was split.
1595 *
1596 * @stable ICU 4.6
1597 */
1598 virtual int32_t split(UText *input,
1599 UText *dest[],
1600 int32_t destCapacity,
1601 UErrorCode &status);
1602
1603 /**
1604 * Set a processing time limit for match operations with this Matcher.
1605 *
1606 * Some patterns, when matching certain strings, can run in exponential time.
1607 * For practical purposes, the match operation may appear to be in an
1608 * infinite loop.
1609 * When a limit is set a match operation will fail with an error if the
1610 * limit is exceeded.
1611 *
1612 * The units of the limit are steps of the match engine.
1613 * Correspondence with actual processor time will depend on the speed
1614 * of the processor and the details of the specific pattern, but will
1615 * typically be on the order of milliseconds.
1616 *
1617 * By default, the matching time is not limited.
1618 *
1619 *
1620 * @param limit The limit value, or 0 for no limit.
1621 * @param status A reference to a UErrorCode to receive any errors.
1622 * @stable ICU 4.0
1623 */
1624 virtual void setTimeLimit(int32_t limit, UErrorCode &status);
1625
1626 /**
1627 * Get the time limit, if any, for match operations made with this Matcher.
1628 *
1629 * @return the maximum allowed time for a match, in units of processing steps.
1630 * @stable ICU 4.0
1631 */
1632 virtual int32_t getTimeLimit() const;
1633
1634 /**
1635 * Set the amount of heap storage available for use by the match backtracking stack.
1636 * The matcher is also reset, discarding any results from previous matches.
1637 *
1638 * ICU uses a backtracking regular expression engine, with the backtrack stack
1639 * maintained on the heap. This function sets the limit to the amount of memory
1640 * that can be used for this purpose. A backtracking stack overflow will
1641 * result in an error from the match operation that caused it.
1642 *
1643 * A limit is desirable because a malicious or poorly designed pattern can use
1644 * excessive memory, potentially crashing the process. A limit is enabled
1645 * by default.
1646 *
1647 * @param limit The maximum size, in bytes, of the matching backtrack stack.
1648 * A value of zero means no limit.
1649 * The limit must be greater or equal to zero.
1650 *
1651 * @param status A reference to a UErrorCode to receive any errors.
1652 *
1653 * @stable ICU 4.0
1654 */
1655 virtual void setStackLimit(int32_t limit, UErrorCode &status);
1656
1657 /**
1658 * Get the size of the heap storage available for use by the back tracking stack.
1659 *
1660 * @return the maximum backtracking stack size, in bytes, or zero if the
1661 * stack size is unlimited.
1662 * @stable ICU 4.0
1663 */
1664 virtual int32_t getStackLimit() const;
1665
1666
1667 /**
1668 * Set a callback function for use with this Matcher.
1669 * During matching operations the function will be called periodically,
1670 * giving the application the opportunity to terminate a long-running
1671 * match.
1672 *
1673 * @param callback A pointer to the user-supplied callback function.
1674 * @param context User context pointer. The value supplied at the
1675 * time the callback function is set will be saved
1676 * and passed to the callback each time that it is called.
1677 * @param status A reference to a UErrorCode to receive any errors.
1678 * @stable ICU 4.0
1679 */
1680 virtual void setMatchCallback(URegexMatchCallback *callback,
1681 const void *context,
1682 UErrorCode &status);
1683
1684
1685 /**
1686 * Get the callback function for this URegularExpression.
1687 *
1688 * @param callback Out parameter, receives a pointer to the user-supplied
1689 * callback function.
1690 * @param context Out parameter, receives the user context pointer that
1691 * was set when uregex_setMatchCallback() was called.
1692 * @param status A reference to a UErrorCode to receive any errors.
1693 * @stable ICU 4.0
1694 */
1695 virtual void getMatchCallback(URegexMatchCallback *&callback,
1696 const void *&context,
1697 UErrorCode &status);
1698
1699
1700 /**
1701 * Set a progress callback function for use with find operations on this Matcher.
1702 * During find operations, the callback will be invoked after each return from a
1703 * match attempt, giving the application the opportunity to terminate a long-running
1704 * find operation.
1705 *
1706 * @param callback A pointer to the user-supplied callback function.
1707 * @param context User context pointer. The value supplied at the
1708 * time the callback function is set will be saved
1709 * and passed to the callback each time that it is called.
1710 * @param status A reference to a UErrorCode to receive any errors.
1711 * @stable ICU 4.6
1712 */
1713 virtual void setFindProgressCallback(URegexFindProgressCallback *callback,
1714 const void *context,
1715 UErrorCode &status);
1716
1717
1718 /**
1719 * Get the find progress callback function for this URegularExpression.
1720 *
1721 * @param callback Out parameter, receives a pointer to the user-supplied
1722 * callback function.
1723 * @param context Out parameter, receives the user context pointer that
1724 * was set when uregex_setFindProgressCallback() was called.
1725 * @param status A reference to a UErrorCode to receive any errors.
1726 * @stable ICU 4.6
1727 */
1728 virtual void getFindProgressCallback(URegexFindProgressCallback *&callback,
1729 const void *&context,
1730 UErrorCode &status);
1731
1732 #ifndef U_HIDE_INTERNAL_API
1733 /**
1734 * setTrace Debug function, enable/disable tracing of the matching engine.
1735 * For internal ICU development use only. DO NO USE!!!!
1736 * @internal
1737 */
1738 void setTrace(UBool state);
1739 #endif /* U_HIDE_INTERNAL_API */
1740
1741 /**
1742 * ICU "poor man's RTTI", returns a UClassID for this class.
1743 *
1744 * @stable ICU 2.2
1745 */
1746 static UClassID U_EXPORT2 getStaticClassID();
1747
1748 /**
1749 * ICU "poor man's RTTI", returns a UClassID for the actual class.
1750 *
1751 * @stable ICU 2.2
1752 */
1753 virtual UClassID getDynamicClassID() const;
1754
1755 private:
1756 // Constructors and other object boilerplate are private.
1757 // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
1758 RegexMatcher(); // default constructor not implemented
1759 RegexMatcher(const RegexPattern *pat);
1760 RegexMatcher(const RegexMatcher &other);
1761 RegexMatcher &operator =(const RegexMatcher &rhs);
1762 void init(UErrorCode &status); // Common initialization
1763 void init2(UText *t, UErrorCode &e); // Common initialization, part 2.
1764
1765 friend class RegexPattern;
1766 friend class RegexCImpl;
1767 public:
1768 #ifndef U_HIDE_INTERNAL_API
1769 /** @internal */
1770 void resetPreserveRegion(); // Reset matcher state, but preserve any region.
1771 #endif /* U_HIDE_INTERNAL_API */
1772 private:
1773
1774 //
1775 // MatchAt This is the internal interface to the match engine itself.
1776 // Match status comes back in matcher member variables.
1777 //
1778 void MatchAt(int64_t startIdx, UBool toEnd, UErrorCode &status);
1779 inline void backTrack(int64_t &inputIdx, int32_t &patIdx);
1780 UBool isWordBoundary(int64_t pos); // perform Perl-like \b test
1781 UBool isUWordBoundary(int64_t pos); // perform RBBI based \b test
1782 REStackFrame *resetStack();
1783 inline REStackFrame *StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status);
1784 void IncrementTime(UErrorCode &status);
1785
1786 // Call user find callback function, if set. Return TRUE if operation should be interrupted.
1787 inline UBool findProgressInterrupt(int64_t matchIndex, UErrorCode &status);
1788
1789 int64_t appendGroup(int32_t groupNum, UText *dest, UErrorCode &status) const;
1790
1791 UBool findUsingChunk(UErrorCode &status);
1792 void MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status);
1793 UBool isChunkWordBoundary(int32_t pos);
1794
1795 const RegexPattern *fPattern;
1796 RegexPattern *fPatternOwned; // Non-NULL if this matcher owns the pattern, and
1797 // should delete it when through.
1798
1799 const UnicodeString *fInput; // The string being matched. Only used for input()
1800 UText *fInputText; // The text being matched. Is never NULL.
1801 UText *fAltInputText; // A shallow copy of the text being matched.
1802 // Only created if the pattern contains backreferences.
1803 int64_t fInputLength; // Full length of the input text.
1804 int32_t fFrameSize; // The size of a frame in the backtrack stack.
1805
1806 int64_t fRegionStart; // Start of the input region, default = 0.
1807 int64_t fRegionLimit; // End of input region, default to input.length.
1808
1809 int64_t fAnchorStart; // Region bounds for anchoring operations (^ or $).
1810 int64_t fAnchorLimit; // See useAnchoringBounds
1811
1812 int64_t fLookStart; // Region bounds for look-ahead/behind and
1813 int64_t fLookLimit; // and other boundary tests. See
1814 // useTransparentBounds
1815
1816 int64_t fActiveStart; // Currently active bounds for matching.
1817 int64_t fActiveLimit; // Usually is the same as region, but
1818 // is changed to fLookStart/Limit when
1819 // entering look around regions.
1820
1821 UBool fTransparentBounds; // True if using transparent bounds.
1822 UBool fAnchoringBounds; // True if using anchoring bounds.
1823
1824 UBool fMatch; // True if the last attempted match was successful.
1825 int64_t fMatchStart; // Position of the start of the most recent match
1826 int64_t fMatchEnd; // First position after the end of the most recent match
1827 // Zero if no previous match, even when a region
1828 // is active.
1829 int64_t fLastMatchEnd; // First position after the end of the previous match,
1830 // or -1 if there was no previous match.
1831 int64_t fAppendPosition; // First position after the end of the previous
1832 // appendReplacement(). As described by the
1833 // JavaDoc for Java Matcher, where it is called
1834 // "append position"
1835 UBool fHitEnd; // True if the last match touched the end of input.
1836 UBool fRequireEnd; // True if the last match required end-of-input
1837 // (matched $ or Z)
1838
1839 UVector64 *fStack;
1840 REStackFrame *fFrame; // After finding a match, the last active stack frame,
1841 // which will contain the capture group results.
1842 // NOT valid while match engine is running.
1843
1844 int64_t *fData; // Data area for use by the compiled pattern.
1845 int64_t fSmallData[8]; // Use this for data if it's enough.
1846
1847 int32_t fTimeLimit; // Max time (in arbitrary steps) to let the
1848 // match engine run. Zero for unlimited.
1849
1850 int32_t fTime; // Match time, accumulates while matching.
1851 int32_t fTickCounter; // Low bits counter for time. Counts down StateSaves.
1852 // Kept separately from fTime to keep as much
1853 // code as possible out of the inline
1854 // StateSave function.
1855
1856 int32_t fStackLimit; // Maximum memory size to use for the backtrack
1857 // stack, in bytes. Zero for unlimited.
1858
1859 URegexMatchCallback *fCallbackFn; // Pointer to match progress callback funct.
1860 // NULL if there is no callback.
1861 const void *fCallbackContext; // User Context ptr for callback function.
1862
1863 URegexFindProgressCallback *fFindProgressCallbackFn; // Pointer to match progress callback funct.
1864 // NULL if there is no callback.
1865 const void *fFindProgressCallbackContext; // User Context ptr for callback function.
1866
1867
1868 UBool fInputUniStrMaybeMutable; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
1869
1870 UBool fTraceDebug; // Set true for debug tracing of match engine.
1871
1872 UErrorCode fDeferredStatus; // Save error state that cannot be immediately
1873 // reported, or that permanently disables this matcher.
1874
1875 RuleBasedBreakIterator *fWordBreakItr;
1876 };
1877
1878 U_NAMESPACE_END
1879 #endif // U_SHOW_CPLUSPLUS_API
1880
1881 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS
1882 #endif