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