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