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