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