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