]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/i18n/unicode/regex.h
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / regex.h
... / ...
CommitLineData
1/*
2**********************************************************************
3* Copyright (C) 2002-2006, 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/parseerr.h"
52
53#include "unicode/uregex.h"
54
55U_NAMESPACE_BEGIN
56
57
58// Forward Declarations...
59
60class RegexMatcher;
61class RegexPattern;
62class UVector;
63class UVector32;
64class UnicodeSet;
65struct REStackFrame;
66struct Regex8BitSet;
67class RuleBasedBreakIterator;
68class RegexCImpl;
69
70
71
72
73/**
74 * RBBIPatternDump Debug function, displays the compiled form of a pattern.
75 * @internal
76 */
77#ifdef REGEX_DEBUG
78U_INTERNAL void U_EXPORT2
79 RegexPatternDump(const RegexPattern *pat);
80#else
81 #define RegexPatternDump(pat)
82#endif
83
84
85
86/**
87 * Class <code>RegexPattern</code> represents a compiled regular expression. It includes
88 * factory methods for creating a RegexPattern object from the source (string) form
89 * of a regular expression, methods for creating RegexMatchers that allow the pattern
90 * to be applied to input text, and a few convenience methods for simple common
91 * uses of regular expressions.
92 *
93 * <p>Class RegexPattern is not intended to be subclassed.</p>
94 *
95 * @stable ICU 2.4
96 */
97class U_I18N_API RegexPattern: public UObject {
98public:
99
100 /**
101 * default constructor. Create a RegexPattern object that refers to no actual
102 * pattern. Not normally needed; RegexPattern objects are usually
103 * created using the factory method <code>compile()</code>.
104 *
105 * @stable ICU 2.4
106 */
107 RegexPattern();
108
109 /**
110 * Copy Constructor. Create a new RegexPattern object that is equivalent
111 * to the source object.
112 * @param source the pattern object to be copied.
113 * @stable ICU 2.4
114 */
115 RegexPattern(const RegexPattern &source);
116
117 /**
118 * Destructor. Note that a RegexPattern object must persist so long as any
119 * RegexMatcher objects that were created from the RegexPattern are active.
120 * @stable ICU 2.4
121 */
122 virtual ~RegexPattern();
123
124 /**
125 * Comparison operator. Two RegexPattern objects are considered equal if they
126 * were constructed from identical source patterns using the same match flag
127 * settings.
128 * @param that a RegexPattern object to compare with "this".
129 * @return TRUE if the objects are equivalent.
130 * @stable ICU 2.4
131 */
132 UBool operator==(const RegexPattern& that) const;
133
134 /**
135 * Comparison operator. Two RegexPattern objects are considered equal if they
136 * were constructed from identical source patterns using the same match flag
137 * settings.
138 * @param that a RegexPattern object to compare with "this".
139 * @return TRUE if the objects are different.
140 * @stable ICU 2.4
141 */
142 inline UBool operator!=(const RegexPattern& that) const {return ! operator ==(that);};
143
144 /**
145 * Assignment operator. After assignment, this RegexPattern will behave identically
146 * to the source object.
147 * @stable ICU 2.4
148 */
149 RegexPattern &operator =(const RegexPattern &source);
150
151 /**
152 * Create an exact copy of this RegexPattern object. Since RegexPattern is not
153 * intended to be subclasses, <code>clone()</code> and the copy construction are
154 * equivalent operations.
155 * @return the copy of this RegexPattern
156 * @stable ICU 2.4
157 */
158 virtual RegexPattern *clone() const;
159
160
161 /**
162 * Compiles the regular expression in string form into a RegexPattern
163 * object. These compile methods, rather than the constructors, are the usual
164 * way that RegexPattern objects are created.
165 *
166 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
167 * objects created from the pattern are active. RegexMatchers keep a pointer
168 * back to their pattern, so premature deletion of the pattern is a
169 * catastrophic error.</p>
170 *
171 * <p>All pattern match mode flags are set to their default values.</p>
172 *
173 * <p>Note that it is often more convenient to construct a RegexMatcher directly
174 * from a pattern string rather than separately compiling the pattern and
175 * then creating a RegexMatcher object from the pattern.</p>
176 *
177 * @param regex The regular expression to be compiled.
178 * @param pe Receives the position (line and column nubers) of any error
179 * within the regular expression.)
180 * @param status A reference to a UErrorCode to receive any errors.
181 * @return A regexPattern object for the compiled pattern.
182 *
183 * @stable ICU 2.4
184 */
185 static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
186 UParseError &pe,
187 UErrorCode &status);
188
189 /**
190 * Compiles the regular expression in string form into a RegexPattern
191 * object using the specified match mode flags. These compile methods,
192 * rather than the constructors, are the usual way that RegexPattern objects
193 * 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>Note that it is often more convenient to construct a RegexMatcher directly
201 * from a pattern string instead of than separately compiling the pattern and
202 * then creating a RegexMatcher object from the pattern.</p>
203 *
204 * @param regex The regular expression to be compiled.
205 * @param flags The match mode flags to be used.
206 * @param pe Receives the position (line and column nubers) of any error
207 * within the regular expression.)
208 * @param status A reference to a UErrorCode to receive any errors.
209 * @return A regexPattern object for the compiled pattern.
210 *
211 * @stable ICU 2.4
212 */
213 static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
214 uint32_t flags,
215 UParseError &pe,
216 UErrorCode &status);
217
218
219 /**
220 * Compiles the regular expression in string form into a RegexPattern
221 * object using the specified match mode flags. These compile methods,
222 * rather than the constructors, are the usual way that RegexPattern objects
223 * are created.
224 *
225 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
226 * objects created from the pattern are active. RegexMatchers keep a pointer
227 * back to their pattern, so premature deletion of the pattern is a
228 * catastrophic error.</p>
229 *
230 * <p>Note that it is often more convenient to construct a RegexMatcher directly
231 * from a pattern string instead of than separately compiling the pattern and
232 * then creating a RegexMatcher object from the pattern.</p>
233 *
234 * @param regex The regular expression to be compiled.
235 * @param flags The match mode flags to be used.
236 * @param status A reference to a UErrorCode to receive any errors.
237 * @return A regexPattern object for the compiled pattern.
238 *
239 * @stable ICU 2.6
240 */
241 static RegexPattern * U_EXPORT2 compile( const UnicodeString &regex,
242 uint32_t flags,
243 UErrorCode &status);
244
245
246 /**
247 * Get the match mode flags that were used when compiling this pattern.
248 * @return the match mode flags
249 * @stable ICU 2.4
250 */
251 virtual uint32_t flags() const;
252
253 /**
254 * Creates a RegexMatcher that will match the given input against this pattern. The
255 * RegexMatcher can then be used to perform match, find or replace operations
256 * on the input. Note that a RegexPattern object must not be deleted while
257 * RegexMatchers created from it still exist and might possibly be used again.
258 * <p>
259 * The matcher will retain a reference to the supplied input string, and all regexp
260 * pattern matching operations happen directly on this original string. It is
261 * critical that the string not be altered or deleted before use by the regular
262 * expression operations is complete.
263 *
264 * @param input The input string to which the regular expression will be applied.
265 * @param status A reference to a UErrorCode to receive any errors.
266 * @return A RegexMatcher object for this pattern and input.
267 *
268 * @stable ICU 2.4
269 */
270 virtual RegexMatcher *matcher(const UnicodeString &input,
271 UErrorCode &status) const;
272
273private:
274 /**
275 * Cause a compilation error if an application accidently attempts to
276 * create a matcher with a (UChar *) string as input rather than
277 * a UnicodeString. Avoids a dangling reference to a temporary string.
278 * <p>
279 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
280 * using one of the aliasing constructors, such as
281 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
282 *
283 * @internal
284 */
285 RegexMatcher *matcher(const UChar *input,
286 UErrorCode &status) const;
287public:
288
289
290 /**
291 * Creates a RegexMatcher that will match against this pattern. The
292 * RegexMatcher can be used to perform match, find or replace operations.
293 * Note that a RegexPattern object must not be deleted while
294 * RegexMatchers created from it still exist and might possibly be used again.
295 *
296 * @param status A reference to a UErrorCode to receive any errors.
297 * @return A RegexMatcher object for this pattern and input.
298 *
299 * @stable ICU 2.6
300 */
301 virtual RegexMatcher *matcher(UErrorCode &status) const;
302
303
304 /**
305 * Test whether a string matches a regular expression. This convenience function
306 * both compiles the reguluar expression and applies it in a single operation.
307 * Note that if the same pattern needs to be applied repeatedly, this method will be
308 * less efficient than creating and reusing a RegexMatcher object.
309 *
310 * @param regex The regular expression
311 * @param input The string data to be matched
312 * @param pe Receives the position of any syntax errors within the regular expression
313 * @param status A reference to a UErrorCode to receive any errors.
314 * @return True if the regular expression exactly matches the full input string.
315 *
316 * @stable ICU 2.4
317 */
318 static UBool U_EXPORT2 matches(const UnicodeString &regex,
319 const UnicodeString &input,
320 UParseError &pe,
321 UErrorCode &status);
322
323
324 /**
325 * Returns the regular expression from which this pattern was compiled.
326 * @stable ICU 2.4
327 */
328 virtual UnicodeString pattern() const;
329
330
331 /**
332 * Split a string into fields. Somewhat like split() from Perl.
333 * The pattern matches identify delimiters that separate the input
334 * into fields. The input data between the matches becomes the
335 * fields themselves.
336 * <p>
337 * For the best performance on split() operations,
338 * <code>RegexMatcher::split</code> is perferable to this function
339 *
340 * @param input The string to be split into fields. The field delimiters
341 * match the pattern (in the "this" object)
342 * @param dest An array of UnicodeStrings to receive the results of the split.
343 * This is an array of actual UnicodeString objects, not an
344 * array of pointers to strings. Local (stack based) arrays can
345 * work well here.
346 * @param destCapacity The number of elements in the destination array.
347 * If the number of fields found is less than destCapacity, the
348 * extra strings in the destination array are not altered.
349 * If the number of destination strings is less than the number
350 * of fields, the trailing part of the input string, including any
351 * field delimiters, is placed in the last destination string.
352 * @param status A reference to a UErrorCode to receive any errors.
353 * @return The number of fields into which the input string was split.
354 * @stable ICU 2.4
355 */
356 virtual int32_t split(const UnicodeString &input,
357 UnicodeString dest[],
358 int32_t destCapacity,
359 UErrorCode &status) const;
360
361
362 /**
363 * ICU "poor man's RTTI", returns a UClassID for the actual class.
364 *
365 * @stable ICU 2.4
366 */
367 virtual UClassID getDynamicClassID() const;
368
369 /**
370 * ICU "poor man's RTTI", returns a UClassID for this class.
371 *
372 * @stable ICU 2.4
373 */
374 static UClassID U_EXPORT2 getStaticClassID();
375
376private:
377 //
378 // Implementation Data
379 //
380 UnicodeString fPattern; // The original pattern string.
381 uint32_t fFlags; // The flags used when compiling the pattern.
382 //
383 UVector32 *fCompiledPat; // The compiled pattern p-code.
384 UnicodeString fLiteralText; // Any literal string data from the pattern,
385 // after un-escaping, for use during the match.
386
387 UVector *fSets; // Any UnicodeSets referenced from the pattern.
388 Regex8BitSet *fSets8; // (and fast sets for latin-1 range.)
389
390
391 UErrorCode fDeferredStatus; // status if some prior error has left this
392 // RegexPattern in an unusable state.
393
394 int32_t fMinMatchLen; // Minimum Match Length. All matches will have length
395 // >= this value. For some patterns, this calculated
396 // value may be less than the true shortest
397 // possible match.
398
399 int32_t fFrameSize; // Size of a state stack frame in the
400 // execution engine.
401
402 int32_t fDataSize; // The size of the data needed by the pattern that
403 // does not go on the state stack, but has just
404 // a single copy per matcher.
405
406 UVector32 *fGroupMap; // Map from capture group number to position of
407 // the group's variables in the matcher stack frame.
408
409 int32_t fMaxCaptureDigits;
410
411 UnicodeSet **fStaticSets; // Ptr to static (shared) sets for predefined
412 // regex character classes, e.g. Word.
413
414 Regex8BitSet *fStaticSets8; // Ptr to the static (shared) latin-1 only
415 // sets for predefined regex classes.
416
417 int32_t fStartType; // Info on how a match must start.
418 int32_t fInitialStringIdx; //
419 int32_t fInitialStringLen;
420 UnicodeSet *fInitialChars;
421 UChar32 fInitialChar;
422 Regex8BitSet *fInitialChars8;
423
424 friend class RegexCompile;
425 friend class RegexMatcher;
426 friend class RegexCImpl;
427
428 //
429 // Implementation Methods
430 //
431 void init(); // Common initialization, for use by constructors.
432 void zap(); // Common cleanup
433#ifdef REGEX_DEBUG
434 void dumpOp(int32_t index) const;
435 friend void U_EXPORT2 RegexPatternDump(const RegexPattern *);
436#endif
437
438};
439
440
441
442/**
443 * class RegexMatcher bundles together a reular expression pattern and
444 * input text to which the expression can be applied. It includes methods
445 * for testing for matches, and for find and replace operations.
446 *
447 * <p>Class RegexMatcher is not intended to be subclassed.</p>
448 *
449 * @stable ICU 2.4
450 */
451class U_I18N_API RegexMatcher: public UObject {
452public:
453
454 /**
455 * Construct a RegexMatcher for a regular expression.
456 * This is a convenience method that avoids the need to explicitly create
457 * a RegexPattern object. Note that if several RegexMatchers need to be
458 * created for the same expression, it will be more efficient to
459 * separately create and cache a RegexPattern object, and use
460 * its matcher() method to create the RegexMatcher objects.
461 *
462 * @param regexp The Regular Expression to be compiled.
463 * @param flags Regular expression options, such as case insensitive matching.
464 * @see UREGEX_CASE_INSENSITIVE
465 * @param status Any errors are reported by setting this UErrorCode variable.
466 * @stable ICU 2.6
467 */
468 RegexMatcher(const UnicodeString &regexp, uint32_t flags, UErrorCode &status);
469
470 /**
471 * Construct a RegexMatcher for a regular expression.
472 * This is a convenience method that avoids the need to explicitly create
473 * a RegexPattern object. Note that if several RegexMatchers need to be
474 * created for the same expression, it will be more efficient to
475 * separately create and cache a RegexPattern object, and use
476 * its matcher() method to create the RegexMatcher objects.
477 * <p>
478 * The matcher will retain a reference to the supplied input string, and all regexp
479 * pattern matching operations happen directly on the original string. It is
480 * critical that the string not be altered or deleted before use by the regular
481 * expression operations is complete.
482 *
483 * @param regexp The Regular Expression to be compiled.
484 * @param input The string to match. The matcher retains a reference to the
485 * caller's string; mo copy is made.
486 * @param flags Regular expression options, such as case insensitive matching.
487 * @see UREGEX_CASE_INSENSITIVE
488 * @param status Any errors are reported by setting this UErrorCode variable.
489 * @stable ICU 2.6
490 */
491 RegexMatcher(const UnicodeString &regexp, const UnicodeString &input,
492 uint32_t flags, UErrorCode &status);
493
494private:
495 /**
496 * Cause a compilation error if an application accidently attempts to
497 * create a matcher with a (UChar *) string as input rather than
498 * a UnicodeString. Avoids a dangling reference to a temporary string.
499 * <p>
500 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
501 * using one of the aliasing constructors, such as
502 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
503 *
504 * @internal
505 */
506 RegexMatcher(const UnicodeString &regexp, const UChar *input,
507 uint32_t flags, UErrorCode &status);
508public:
509
510
511 /**
512 * Destructor.
513 *
514 * @stable ICU 2.4
515 */
516 virtual ~RegexMatcher();
517
518
519 /**
520 * Attempts to match the entire input string against the pattern.
521 * @param status A reference to a UErrorCode to receive any errors.
522 * @return TRUE if there is a match
523 * @stable ICU 2.4
524 */
525 virtual UBool matches(UErrorCode &status);
526
527 /**
528 * Attempts to match the input string, beginning at startIndex, against the pattern.
529 * The match must extend to the end of the input string.
530 * @param startIndex The input string index at which to begin matching.
531 * @param status A reference to a UErrorCode to receive any errors.
532 * @return TRUE if there is a match
533 * @stable ICU 2.8
534 */
535 virtual UBool matches(int32_t startIndex, UErrorCode &status);
536
537
538
539
540 /**
541 * Attempts to match the input string, starting from the beginning, against the pattern.
542 * Like the matches() method, this function always starts at the beginning of the input string;
543 * unlike that function, it does not require that the entire input string be matched.
544 *
545 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
546 * <code>end()</code>, and <code>group()</code> functions.</p>
547 *
548 * @param status A reference to a UErrorCode to receive any errors.
549 * @return TRUE if there is a match at the start of the input string.
550 * @stable ICU 2.4
551 */
552 virtual UBool lookingAt(UErrorCode &status);
553
554
555 /**
556 * Attempts to match the input string, starting from the specified index, against the pattern.
557 * The match may be of any length, and is not required to extend to the end
558 * of the input string. Contrast with match().
559 *
560 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
561 * <code>end()</code>, and <code>group()</code> functions.</p>
562 *
563 * @param startIndex The input string index at which to begin matching.
564 * @param status A reference to a UErrorCode to receive any errors.
565 * @return TRUE if there is a match.
566 * @stable ICU 2.8
567 */
568 virtual UBool lookingAt(int32_t startIndex, UErrorCode &status);
569
570 /**
571 * Find the next pattern match in the input string.
572 * The find begins searching the input at the location following the end of
573 * the previous match, or at the start of the string if there is no previous match.
574 * If a match is found, <code>start(), end()</code> and <code>group()</code>
575 * will provide more information regarding the match.
576 * <p>Note that if the input string is changed by the application,
577 * use find(startPos, status) instead of find(), because the saved starting
578 * position may not be valid with the altered input string.</p>
579 * @return TRUE if a match is found.
580 * @stable ICU 2.4
581 */
582 virtual UBool find();
583
584
585 /**
586 * Resets this RegexMatcher and then attempts to find the next substring of the
587 * input string that matches the pattern, starting at the specified index.
588 *
589 * @param start the position in the input string to begin the search
590 * @param status A reference to a UErrorCode to receive any errors.
591 * @return TRUE if a match is found.
592 * @stable ICU 2.4
593 */
594 virtual UBool find(int32_t start, UErrorCode &status);
595
596
597 /**
598 * Returns a string containing the text matched by the previous match.
599 * If the pattern can match an empty string, an empty string may be returned.
600 * @param status A reference to a UErrorCode to receive any errors.
601 * Possible errors are U_REGEX_INVALID_STATE if no match
602 * has been attempted or the last match failed.
603 * @return a string containing the matched input text.
604 * @stable ICU 2.4
605 */
606 virtual UnicodeString group(UErrorCode &status) const;
607
608
609 /**
610 * Returns a string containing the text captured by the given group
611 * during the previous match operation. Group(0) is the entire match.
612 *
613 * @param groupNum the capture group number
614 * @param status A reference to a UErrorCode to receive any errors.
615 * Possible errors are U_REGEX_INVALID_STATE if no match
616 * has been attempted or the last match failed and
617 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
618 * @return the captured text
619 * @stable ICU 2.4
620 */
621 virtual UnicodeString group(int32_t groupNum, UErrorCode &status) const;
622
623
624 /**
625 * Returns the number of capturing groups in this matcher's pattern.
626 * @return the number of capture groups
627 * @stable ICU 2.4
628 */
629 virtual int32_t groupCount() const;
630
631
632 /**
633 * Returns the index in the input string of the start of the text matched
634 * during the previous match operation.
635 * @param status a reference to a UErrorCode to receive any errors.
636 * @return The position in the input string of the start of the last match.
637 * @stable ICU 2.4
638 */
639 virtual int32_t start(UErrorCode &status) const;
640
641
642 /**
643 * Returns the index in the input string of the start of the text matched by the
644 * specified capture group during the previous match operation. Return -1 if
645 * the capture group exists in the pattern, but was not part of the last match.
646 *
647 * @param group the capture group number
648 * @param status A reference to a UErrorCode to receive any errors. Possible
649 * errors are U_REGEX_INVALID_STATE if no match has been
650 * attempted or the last match failed, and
651 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
652 * @return the start position of substring matched by the specified group.
653 * @stable ICU 2.4
654 */
655 virtual int32_t start(int32_t group, UErrorCode &status) const;
656
657
658 /**
659 * Returns the index in the input string of the first character following the
660 * text matched during the previous match operation.
661 * @param status A reference to a UErrorCode to receive any errors. Possible
662 * errors are U_REGEX_INVALID_STATE if no match has been
663 * attempted or the last match failed.
664 * @return the index of the last character matched, plus one.
665 * @stable ICU 2.4
666 */
667 virtual int32_t end(UErrorCode &status) const;
668
669
670 /**
671 * Returns the index in the input string of the character following the
672 * text matched by the specified capture group during the previous match operation.
673 * @param group the capture group number
674 * @param status A reference to a UErrorCode to receive any errors. Possible
675 * errors are U_REGEX_INVALID_STATE if no match has been
676 * attempted or the last match failed and
677 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
678 * @return the index of the first character following the text
679 * captured by the specifed group during the previous match operation.
680 * Return -1 if the capture group exists in the pattern but was not part of the match.
681 * @stable ICU 2.4
682 */
683 virtual int32_t end(int32_t group, UErrorCode &status) const;
684
685
686 /**
687 * Resets this matcher. The effect is to remove any memory of previous matches,
688 * and to cause subsequent find() operations to begin at the beginning of
689 * the input string.
690 *
691 * @return this RegexMatcher.
692 * @stable ICU 2.4
693 */
694 virtual RegexMatcher &reset();
695
696
697 /**
698 * Resets this matcher, and set the current input position.
699 * The effect is to remove any memory of previous matches,
700 * and to cause subsequent find() operations to begin at
701 * the specified position in the input string.
702 *
703 * @return this RegexMatcher.
704 * @stable ICU 2.8
705 */
706 virtual RegexMatcher &reset(int32_t index, UErrorCode &status);
707
708
709 /**
710 * Resets this matcher with a new input string. This allows instances of RegexMatcher
711 * to be reused, which is more efficient than creating a new RegexMatcher for
712 * each input string to be processed.
713 * @param input The new string on which subsequent pattern matches will operate.
714 * The matcher retains a reference to the callers string, and operates
715 * directly on that. Ownership of the string remains with the caller.
716 * Because no copy of the string is made, it is essential that the
717 * caller not delete the string until after regexp operations on it
718 * are done.
719 * @return this RegexMatcher.
720 * @stable ICU 2.4
721 */
722 virtual RegexMatcher &reset(const UnicodeString &input);
723
724private:
725 /**
726 * Cause a compilation error if an application accidently attempts to
727 * reset a matcher with a (UChar *) string as input rather than
728 * a UnicodeString. Avoids a dangling reference to a temporary string.
729 * <p>
730 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
731 * using one of the aliasing constructors, such as
732 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
733 *
734 * @internal
735 */
736 virtual RegexMatcher &reset(const UChar *input);
737public:
738
739 /**
740 * Returns the input string being matched. The returned string is not a copy,
741 * but the live input string. It should not be altered or deleted.
742 * @return the input string
743 * @stable ICU 2.4
744 */
745 virtual const UnicodeString &input() const;
746
747
748 /**
749 * Returns the pattern that is interpreted by this matcher.
750 * @return the RegexPattern for this RegexMatcher
751 * @stable ICU 2.4
752 */
753 virtual const RegexPattern &pattern() const;
754
755
756 /**
757 * Replaces every substring of the input that matches the pattern
758 * with the given replacement string. This is a convenience function that
759 * provides a complete find-and-replace-all operation.
760 *
761 * This method first resets this matcher. It then scans the input string
762 * looking for matches of the pattern. Input that is not part of any
763 * match is left unchanged; each match is replaced in the result by the
764 * replacement string. The replacement string may contain references to
765 * capture groups.
766 *
767 * @param replacement a string containing the replacement text.
768 * @param status a reference to a UErrorCode to receive any errors.
769 * @return a string containing the results of the find and replace.
770 * @stable ICU 2.4
771 */
772 virtual UnicodeString replaceAll(const UnicodeString &replacement, UErrorCode &status);
773
774
775 /**
776 * Replaces the first substring of the input that matches
777 * the pattern with the replacement string. This is a convenience
778 * function that provides a complete find-and-replace operation.
779 *
780 * <p>This function first resets this RegexMatcher. It then scans the input string
781 * looking for a match of the pattern. Input that is not part
782 * of the match is appended directly to the result string; the match is replaced
783 * in the result by the replacement string. The replacement string may contain
784 * references to captured groups.</p>
785 *
786 * <p>The state of the matcher (the position at which a subsequent find()
787 * would begin) after completing a replaceFirst() is not specified. The
788 * RegexMatcher should be reset before doing additional find() operations.</p>
789 *
790 * @param replacement a string containing the replacement text.
791 * @param status a reference to a UErrorCode to receive any errors.
792 * @return a string containing the results of the find and replace.
793 * @stable ICU 2.4
794 */
795 virtual UnicodeString replaceFirst(const UnicodeString &replacement, UErrorCode &status);
796
797 /**
798 * Implements a replace operation intended to be used as part of an
799 * incremental find-and-replace.
800 *
801 * <p>The input string, starting from the end of the previous replacement and ending at
802 * the start of the current match, is appended to the destination string. Then the
803 * replacement string is appended to the output string,
804 * including handling any substitutions of captured text.</p>
805 *
806 * <p>For simple, prepackaged, non-incremental find-and-replace
807 * operations, see replaceFirst() or replaceAll().</p>
808 *
809 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
810 * @param replacement A UnicodeString that provides the text to be substituted for
811 * the input text that matched the regexp pattern. The replacement
812 * text may contain references to captured text from the
813 * input.
814 * @param status A reference to a UErrorCode to receive any errors. Possible
815 * errors are U_REGEX_INVALID_STATE if no match has been
816 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
817 * if the replacement text specifies a capture group that
818 * does not exist in the pattern.
819 *
820 * @return this RegexMatcher
821 * @stable ICU 2.4
822 *
823 */
824 virtual RegexMatcher &appendReplacement(UnicodeString &dest,
825 const UnicodeString &replacement, UErrorCode &status);
826
827
828 /**
829 * As the final step in a find-and-replace operation, append the remainder
830 * of the input string, starting at the position following the last appendReplacement(),
831 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
832 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
833 *
834 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
835 * @return the destination string.
836 * @stable ICU 2.4
837 */
838 virtual UnicodeString &appendTail(UnicodeString &dest);
839
840
841
842 /**
843 * Split a string into fields. Somewhat like split() from Perl.
844 * The pattern matches identify delimiters that separate the input
845 * into fields. The input data between the matches becomes the
846 * fields themselves.
847 * <p>
848 *
849 * @param input The string to be split into fields. The field delimiters
850 * match the pattern (in the "this" object). This matcher
851 * will be reset to this input string.
852 * @param dest An array of UnicodeStrings to receive the results of the split.
853 * This is an array of actual UnicodeString objects, not an
854 * array of pointers to strings. Local (stack based) arrays can
855 * work well here.
856 * @param destCapacity The number of elements in the destination array.
857 * If the number of fields found is less than destCapacity, the
858 * extra strings in the destination array are not altered.
859 * If the number of destination strings is less than the number
860 * of fields, the trailing part of the input string, including any
861 * field delimiters, is placed in the last destination string.
862 * @param status A reference to a UErrorCode to receive any errors.
863 * @return The number of fields into which the input string was split.
864 * @stable ICU 2.6
865 */
866 virtual int32_t split(const UnicodeString &input,
867 UnicodeString dest[],
868 int32_t destCapacity,
869 UErrorCode &status);
870
871
872
873 /**
874 * setTrace Debug function, enable/disable tracing of the matching engine.
875 * For internal ICU development use only. DO NO USE!!!!
876 * @internal
877 */
878 void setTrace(UBool state);
879
880
881 /**
882 * ICU "poor man's RTTI", returns a UClassID for this class.
883 *
884 * @stable ICU 2.2
885 */
886 static UClassID U_EXPORT2 getStaticClassID();
887
888 /**
889 * ICU "poor man's RTTI", returns a UClassID for the actual class.
890 *
891 * @stable ICU 2.2
892 */
893 virtual UClassID getDynamicClassID() const;
894
895private:
896 // Constructors and other object boilerplate are private.
897 // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
898 RegexMatcher(); // default constructor not implemented
899 RegexMatcher(const RegexPattern *pat);
900 RegexMatcher(const RegexMatcher &other);
901 RegexMatcher &operator =(const RegexMatcher &rhs);
902 friend class RegexPattern;
903 friend class RegexCImpl;
904
905 //
906 // MatchAt This is the internal interface to the match engine itself.
907 // Match status comes back in matcher member variables.
908 //
909 void MatchAt(int32_t startIdx, UErrorCode &status);
910 inline void backTrack(int32_t &inputIdx, int32_t &patIdx);
911 UBool isWordBoundary(int32_t pos); // perform Perl-like \b test
912 UBool isUWordBoundary(int32_t pos); // perform RBBI based \b test
913 REStackFrame *resetStack();
914 inline REStackFrame *StateSave(REStackFrame *fp, int32_t savePatIdx,
915 int32_t frameSize, UErrorCode &status);
916
917
918 const RegexPattern *fPattern;
919 RegexPattern *fPatternOwned; // Non-NULL if this matcher owns the pattern, and
920 // should delete it when through.
921 const UnicodeString *fInput;
922
923 UBool fMatch; // True if the last match was successful.
924 int32_t fMatchStart; // Position of the start of the most recent match
925 int32_t fMatchEnd; // First position after the end of the most recent match
926 int32_t fLastMatchEnd; // First position after the end of the previous match,
927 // or -1 if there was no previous match.
928 int32_t fLastReplaceEnd; // First position after the end of the previous appendReplacement();
929
930 UVector32 *fStack;
931 REStackFrame *fFrame; // After finding a match, the last active stack
932 // frame, which will contain the capture group results.
933 // NOT valid while match engine is running.
934
935 int32_t *fData; // Data area for use by the compiled pattern.
936 int32_t fSmallData[8]; // Use this for data if it's enough.
937
938 UBool fTraceDebug; // Set true for debug tracing of match engine.
939
940 UErrorCode fDeferredStatus; // Save error state if that cannot be immediately
941 // reported, or that permanently disables this matcher.
942
943 RuleBasedBreakIterator *fWordBreakItr;
944
945
946};
947
948U_NAMESPACE_END
949#endif // UCONFIG_NO_REGULAR_EXPRESSIONS
950#endif