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