1 /* 2 ********************************************************************** 3 * Copyright (C) 2002-2010, 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 369 /** 370 * Flag to disambiguate RegexPattern::matcher signature 371 * @draft ICU 4.6 372 */ 373 enum PatternIsUTextFlag { PATTERN_IS_UTEXT }; 374 375 /** 376 * Creates a RegexMatcher that will match the given input against this pattern. The 377 * RegexMatcher can then be used to perform match, find or replace operations 378 * on the input. Note that a RegexPattern object must not be deleted while 379 * RegexMatchers created from it still exist and might possibly be used again. 380 * <p> 381 * The matcher will make a shallow clone of the supplied input text, and all regexp 382 * pattern matching operations happen on this clone. While read-only operations on 383 * the supplied text are permitted, it is critical that the underlying string not be 384 * altered or deleted before use by the regular expression operations is complete. 385 * 386 * @param input The input text to which the regular expression will be applied. 387 * @param flag Must be RegexPattern::PATTERN_IS_UTEXT; used to disambiguate 388 * method signature. 389 * @param status A reference to a UErrorCode to receive any errors. 390 * @return A RegexMatcher object for this pattern and input. 391 * 392 * @draft ICU 4.6 393 */ 394 virtual RegexMatcher *matcher(UText *input, 395 PatternIsUTextFlag flag, 396 UErrorCode &status) const; 397 398 private: 399 /** 400 * Cause a compilation error if an application accidently attempts to 401 * create a matcher with a (UChar *) string as input rather than 402 * a UnicodeString. Avoids a dangling reference to a temporary string. 403 * <p> 404 * To efficiently work with UChar *strings, wrap the data in a UnicodeString 405 * using one of the aliasing constructors, such as 406 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code> 407 * or in a UText, using 408 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code> 409 * 410 * @internal 411 */ 412 RegexMatcher *matcher(const UChar *input, 413 UErrorCode &status) const; 414 public: 415 416 417 /** 418 * Creates a RegexMatcher that will match against this pattern. The 419 * RegexMatcher can be used to perform match, find or replace operations. 420 * Note that a RegexPattern object must not be deleted while 421 * RegexMatchers created from it still exist and might possibly be used again. 422 * 423 * @param status A reference to a UErrorCode to receive any errors. 424 * @return A RegexMatcher object for this pattern and input. 425 * 426 * @stable ICU 2.6 427 */ 428 virtual RegexMatcher *matcher(UErrorCode &status) const; 429 430 431 /** 432 * Test whether a string matches a regular expression. This convenience function 433 * both compiles the reguluar expression and applies it in a single operation. 434 * Note that if the same pattern needs to be applied repeatedly, this method will be 435 * less efficient than creating and reusing a RegexMatcher object. 436 * 437 * @param regex The regular expression 438 * @param input The string data to be matched 439 * @param pe Receives the position of any syntax errors within the regular expression 440 * @param status A reference to a UErrorCode to receive any errors. 441 * @return True if the regular expression exactly matches the full input string. 442 * 443 * @stable ICU 2.4 444 */ 445 static UBool U_EXPORT2 matches(const UnicodeString ®ex, 446 const UnicodeString &input, 447 UParseError &pe, 448 UErrorCode &status); 449 450 451 /** 452 * Test whether a string matches a regular expression. This convenience function 453 * both compiles the reguluar expression and applies it in a single operation. 454 * Note that if the same pattern needs to be applied repeatedly, this method will be 455 * less efficient than creating and reusing a RegexMatcher object. 456 * 457 * @param regex The regular expression 458 * @param input The string data to be matched 459 * @param pe Receives the position of any syntax errors within the regular expression 460 * @param status A reference to a UErrorCode to receive any errors. 461 * @return True if the regular expression exactly matches the full input string. 462 * 463 * @draft ICU 4.6 464 */ 465 static UBool U_EXPORT2 matches(UText *regex, 466 UText *input, 467 UParseError &pe, 468 UErrorCode &status); 469 470 471 /** 472 * Returns the regular expression from which this pattern was compiled. This method will work 473 * even if the pattern was compiled from a UText. 474 * 475 * Note: If the pattern was originally compiled from a UText, and that UText was modified, 476 * the returned string may no longer reflect the RegexPattern object. 477 * @stable ICU 2.4 478 */ 479 virtual UnicodeString pattern() const; 480 481 482 /** 483 * Returns the regular expression from which this pattern was compiled. This method will work 484 * even if the pattern was compiled from a UnicodeString. 485 * 486 * Note: This is the original input, not a clone. If the pattern was originally compiled from a 487 * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern 488 * object. 489 * 490 * @draft ICU 4.6 491 */ 492 virtual UText *patternText(UErrorCode &status) const; 493 494 495 /** 496 * Split a string into fields. Somewhat like split() from Perl. 497 * The pattern matches identify delimiters that separate the input 498 * into fields. The input data between the matches becomes the 499 * fields themselves. 500 * <p> 501 * For the best performance on split() operations, 502 * <code>RegexMatcher::split</code> is perferable to this function 503 * 504 * @param input The string to be split into fields. The field delimiters 505 * match the pattern (in the "this" object) 506 * @param dest An array of UnicodeStrings to receive the results of the split. 507 * This is an array of actual UnicodeString objects, not an 508 * array of pointers to strings. Local (stack based) arrays can 509 * work well here. 510 * @param destCapacity The number of elements in the destination array. 511 * If the number of fields found is less than destCapacity, the 512 * extra strings in the destination array are not altered. 513 * If the number of destination strings is less than the number 514 * of fields, the trailing part of the input string, including any 515 * field delimiters, is placed in the last destination string. 516 * @param status A reference to a UErrorCode to receive any errors. 517 * @return The number of fields into which the input string was split. 518 * @stable ICU 2.4 519 */ 520 virtual int32_t split(const UnicodeString &input, 521 UnicodeString dest[], 522 int32_t destCapacity, 523 UErrorCode &status) const; 524 525 526 /** 527 * Split a string into fields. Somewhat like split() from Perl. 528 * The pattern matches identify delimiters that separate the input 529 * into fields. The input data between the matches becomes the 530 * fields themselves. 531 * <p> 532 * For the best performance on split() operations, 533 * <code>RegexMatcher::split</code> is perferable to this function 534 * 535 * @param input The string to be split into fields. The field delimiters 536 * match the pattern (in the "this" object) 537 * @param dest An array of mutable UText structs to receive the results of the split. 538 * If a field is NULL, a new UText is allocated to contain the results for 539 * that field. This new UText is not guaranteed to be mutable. 540 * @param destCapacity The number of elements in the destination array. 541 * If the number of fields found is less than destCapacity, the 542 * extra strings in the destination array are not altered. 543 * If the number of destination strings is less than the number 544 * of fields, the trailing part of the input string, including any 545 * field delimiters, is placed in the last destination string. 546 * @param status A reference to a UErrorCode to receive any errors. 547 * @return The number of fields into which the input string was split. 548 * 549 * @draft ICU 4.6 550 */ 551 virtual int32_t split(UText *input, 552 UText *dest[], 553 int32_t destCapacity, 554 UErrorCode &status) const; 555 556 557 /** 558 * ICU "poor man's RTTI", returns a UClassID for the actual class. 559 * 560 * @stable ICU 2.4 561 */ 562 virtual UClassID getDynamicClassID() const; 563 564 /** 565 * ICU "poor man's RTTI", returns a UClassID for this class. 566 * 567 * @stable ICU 2.4 568 */ 569 static UClassID U_EXPORT2 getStaticClassID(); 570 571 private: 572 // 573 // Implementation Data 574 // 575 UText *fPattern; // The original pattern string. 576 UnicodeString *fPatternString; // The original pattern UncodeString if relevant 577 uint32_t fFlags; // The flags used when compiling the pattern. 578 // 579 UVector64 *fCompiledPat; // The compiled pattern p-code. 580 UnicodeString fLiteralText; // Any literal string data from the pattern, 581 // after un-escaping, for use during the match. 582 583 UVector *fSets; // Any UnicodeSets referenced from the pattern. 584 Regex8BitSet *fSets8; // (and fast sets for latin-1 range.) 585 586 587 UErrorCode fDeferredStatus; // status if some prior error has left this 588 // RegexPattern in an unusable state. 589 590 int32_t fMinMatchLen; // Minimum Match Length. All matches will have length 591 // >= this value. For some patterns, this calculated 592 // value may be less than the true shortest 593 // possible match. 594 595 int32_t fFrameSize; // Size of a state stack frame in the 596 // execution engine. 597 598 int32_t fDataSize; // The size of the data needed by the pattern that 599 // does not go on the state stack, but has just 600 // a single copy per matcher. 601 602 UVector32 *fGroupMap; // Map from capture group number to position of 603 // the group's variables in the matcher stack frame. 604 605 int32_t fMaxCaptureDigits; 606 607 UnicodeSet **fStaticSets; // Ptr to static (shared) sets for predefined 608 // regex character classes, e.g. Word. 609 610 Regex8BitSet *fStaticSets8; // Ptr to the static (shared) latin-1 only 611 // sets for predefined regex classes. 612 613 int32_t fStartType; // Info on how a match must start. 614 int32_t fInitialStringIdx; // 615 int32_t fInitialStringLen; 616 UnicodeSet *fInitialChars; 617 UChar32 fInitialChar; 618 Regex8BitSet *fInitialChars8; 619 UBool fNeedsAltInput; 620 621 friend class RegexCompile; 622 friend class RegexMatcher; 623 friend class RegexCImpl; 624 625 // 626 // Implementation Methods 627 // 628 void init(); // Common initialization, for use by constructors. 629 void zap(); // Common cleanup 630 #ifdef REGEX_DEBUG 631 void dumpOp(int32_t index) const; 632 friend void U_EXPORT2 RegexPatternDump(const RegexPattern *); 633 #endif 634 635 }; 636 637 638 639 /** 640 * class RegexMatcher bundles together a reular expression pattern and 641 * input text to which the expression can be applied. It includes methods 642 * for testing for matches, and for find and replace operations. 643 * 644 * <p>Class RegexMatcher is not intended to be subclassed.</p> 645 * 646 * @stable ICU 2.4 647 */ 648 class U_I18N_API RegexMatcher: public UObject { 649 public: 650 651 /** 652 * Construct a RegexMatcher for a regular expression. 653 * This is a convenience method that avoids the need to explicitly create 654 * a RegexPattern object. Note that if several RegexMatchers need to be 655 * created for the same expression, it will be more efficient to 656 * separately create and cache a RegexPattern object, and use 657 * its matcher() method to create the RegexMatcher objects. 658 * 659 * @param regexp The Regular Expression to be compiled. 660 * @param flags Regular expression options, such as case insensitive matching. 661 * @see UREGEX_CASE_INSENSITIVE 662 * @param status Any errors are reported by setting this UErrorCode variable. 663 * @stable ICU 2.6 664 */ 665 RegexMatcher(const UnicodeString ®exp, uint32_t flags, UErrorCode &status); 666 667 /** 668 * Construct a RegexMatcher for a regular expression. 669 * This is a convenience method that avoids the need to explicitly create 670 * a RegexPattern object. Note that if several RegexMatchers need to be 671 * created for the same expression, it will be more efficient to 672 * separately create and cache a RegexPattern object, and use 673 * its matcher() method to create the RegexMatcher objects. 674 * 675 * @param regexp The regular expression to be compiled. 676 * @param flags Regular expression options, such as case insensitive matching. 677 * @see UREGEX_CASE_INSENSITIVE 678 * @param status Any errors are reported by setting this UErrorCode variable. 679 * 680 * @draft ICU 4.6 681 */ 682 RegexMatcher(UText *regexp, uint32_t flags, UErrorCode &status); 683 684 /** 685 * Construct a RegexMatcher for a regular expression. 686 * This is a convenience method that avoids the need to explicitly create 687 * a RegexPattern object. Note that if several RegexMatchers need to be 688 * created for the same expression, it will be more efficient to 689 * separately create and cache a RegexPattern object, and use 690 * its matcher() method to create the RegexMatcher objects. 691 * <p> 692 * The matcher will retain a reference to the supplied input string, and all regexp 693 * pattern matching operations happen directly on the original string. It is 694 * critical that the string not be altered or deleted before use by the regular 695 * expression operations is complete. 696 * 697 * @param regexp The Regular Expression to be compiled. 698 * @param input The string to match. The matcher retains a reference to the 699 * caller's string; mo copy is made. 700 * @param flags Regular expression options, such as case insensitive matching. 701 * @see UREGEX_CASE_INSENSITIVE 702 * @param status Any errors are reported by setting this UErrorCode variable. 703 * @stable ICU 2.6 704 */ 705 RegexMatcher(const UnicodeString ®exp, const UnicodeString &input, 706 uint32_t flags, UErrorCode &status); 707 708 /** 709 * Construct a RegexMatcher for a regular expression. 710 * This is a convenience method that avoids the need to explicitly create 711 * a RegexPattern object. Note that if several RegexMatchers need to be 712 * created for the same expression, it will be more efficient to 713 * separately create and cache a RegexPattern object, and use 714 * its matcher() method to create the RegexMatcher objects. 715 * <p> 716 * The matcher will make a shallow clone of the supplied input text, and all regexp 717 * pattern matching operations happen on this clone. While read-only operations on 718 * the supplied text are permitted, it is critical that the underlying string not be 719 * altered or deleted before use by the regular expression operations is complete. 720 * 721 * @param regexp The Regular Expression to be compiled. 722 * @param input The string to match. The matcher retains a shallow clone of the text. 723 * @param flags Regular expression options, such as case insensitive matching. 724 * @see UREGEX_CASE_INSENSITIVE 725 * @param status Any errors are reported by setting this UErrorCode variable. 726 * 727 * @draft ICU 4.6 728 */ 729 RegexMatcher(UText *regexp, UText *input, 730 uint32_t flags, UErrorCode &status); 731 732 private: 733 /** 734 * Cause a compilation error if an application accidently attempts to 735 * create a matcher with a (UChar *) string as input rather than 736 * a UnicodeString. Avoids a dangling reference to a temporary string. 737 * <p> 738 * To efficiently work with UChar *strings, wrap the data in a UnicodeString 739 * using one of the aliasing constructors, such as 740 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code> 741 * or in a UText, using 742 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code> 743 * 744 * @internal 745 */ 746 RegexMatcher(const UnicodeString ®exp, const UChar *input, 747 uint32_t flags, UErrorCode &status); 748 public: 749 750 751 /** 752 * Destructor. 753 * 754 * @stable ICU 2.4 755 */ 756 virtual ~RegexMatcher(); 757 758 759 /** 760 * Attempts to match the entire input region against the pattern. 761 * @param status A reference to a UErrorCode to receive any errors. 762 * @return TRUE if there is a match 763 * @stable ICU 2.4 764 */ 765 virtual UBool matches(UErrorCode &status); 766 767 768 /** 769 * Resets the matcher, then attempts to match the input beginning 770 * at the specified startIndex, and extending to the end of the input. 771 * The input region is reset to include the entire input string. 772 * A successful match must extend to the end of the input. 773 * @param startIndex The input string (native) index at which to begin matching. 774 * @param status A reference to a UErrorCode to receive any errors. 775 * @return TRUE if there is a match 776 * @stable ICU 2.8 777 */ 778 virtual UBool matches(int64_t startIndex, UErrorCode &status); 779 780 781 /** 782 * Attempts to match the input string, starting from the beginning of the region, 783 * against the pattern. Like the matches() method, this function 784 * always starts at the beginning of the input region; 785 * unlike that function, it does not require that the entire region be matched. 786 * 787 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>, 788 * <code>end()</code>, and <code>group()</code> functions.</p> 789 * 790 * @param status A reference to a UErrorCode to receive any errors. 791 * @return TRUE if there is a match at the start of the input string. 792 * @stable ICU 2.4 793 */ 794 virtual UBool lookingAt(UErrorCode &status); 795 796 797 /** 798 * Attempts to match the input string, starting from the specified index, against the pattern. 799 * The match may be of any length, and is not required to extend to the end 800 * of the input string. Contrast with match(). 801 * 802 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>, 803 * <code>end()</code>, and <code>group()</code> functions.</p> 804 * 805 * @param startIndex The input string (native) index at which to begin matching. 806 * @param status A reference to a UErrorCode to receive any errors. 807 * @return TRUE if there is a match. 808 * @stable ICU 2.8 809 */ 810 virtual UBool lookingAt(int64_t startIndex, UErrorCode &status); 811 812 813 /** 814 * Find the next pattern match in the input string. 815 * The find begins searching the input at the location following the end of 816 * the previous match, or at the start of the string if there is no previous match. 817 * If a match is found, <code>start(), end()</code> and <code>group()</code> 818 * will provide more information regarding the match. 819 * <p>Note that if the input string is changed by the application, 820 * use find(startPos, status) instead of find(), because the saved starting 821 * position may not be valid with the altered input string.</p> 822 * @return TRUE if a match is found. 823 * @stable ICU 2.4 824 */ 825 virtual UBool find(); 826 827 828 /** 829 * Resets this RegexMatcher and then attempts to find the next substring of the 830 * input string that matches the pattern, starting at the specified index. 831 * 832 * @param start The (native) index in the input string to begin the search. 833 * @param status A reference to a UErrorCode to receive any errors. 834 * @return TRUE if a match is found. 835 * @stable ICU 2.4 836 */ 837 virtual UBool find(int64_t start, UErrorCode &status); 838 839 840 /** 841 * Returns a string containing the text matched by the previous match. 842 * If the pattern can match an empty string, an empty string may be returned. 843 * @param status A reference to a UErrorCode to receive any errors. 844 * Possible errors are U_REGEX_INVALID_STATE if no match 845 * has been attempted or the last match failed. 846 * @return a string containing the matched input text. 847 * @stable ICU 2.4 848 */ 849 virtual UnicodeString group(UErrorCode &status) const; 850 851 852 /** 853 * Returns a string containing the text captured by the given group 854 * during the previous match operation. Group(0) is the entire match. 855 * 856 * @param groupNum the capture group number 857 * @param status A reference to a UErrorCode to receive any errors. 858 * Possible errors are U_REGEX_INVALID_STATE if no match 859 * has been attempted or the last match failed and 860 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 861 * @return the captured text 862 * @stable ICU 2.4 863 */ 864 virtual UnicodeString group(int32_t groupNum, UErrorCode &status) const; 865 866 867 /** 868 * Returns the number of capturing groups in this matcher's pattern. 869 * @return the number of capture groups 870 * @stable ICU 2.4 871 */ 872 virtual int32_t groupCount() const; 873 874 875 /** 876 * Returns a shallow clone of the entire live input string with the UText current native index 877 * set to the beginning of the requested group. 878 * Note that copying the entire input string may cause significant performance and memory issues. 879 * @param dest The UText into which the input should be copied, or NULL to create a new UText 880 * @param group_len A reference to receive the length of the desired capture group 881 * @param status A reference to a UErrorCode to receive any errors. 882 * Possible errors are U_REGEX_INVALID_STATE if no match 883 * has been attempted or the last match failed and 884 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 885 * @return dest if non-NULL, a shallow copy of the input text otherwise 886 * 887 * @draft ICU 4.6 888 */ 889 virtual UText *group(UText *dest, int64_t &group_len, UErrorCode &status) const; 890 891 /** 892 * @draft ICU 4.6 893 */ 894 virtual UText *group(int32_t groupNum, UText *dest, int64_t &group_len, UErrorCode &status) const; 895 896 /** 897 * Returns a string containing the text captured by the given group 898 * during the previous match operation. Group(0) is the entire match. 899 * 900 * @param groupNum the capture group number 901 * @param dest A mutable UText in which the matching text is placed. 902 * If NULL, a new UText will be created (which may not be mutable). 903 * @param status A reference to a UErrorCode to receive any errors. 904 * Possible errors are U_REGEX_INVALID_STATE if no match 905 * has been attempted or the last match failed. 906 * @return A string containing the matched input text. If a pre-allocated UText 907 * was provided, it will always be used and returned. 908 * 909 * @internal ICU 4.4 technology preview 910 */ 911 virtual UText *group(int32_t groupNum, UText *dest, UErrorCode &status) const; 912 913 914 /** 915 * Returns the index in the input string of the start of the text matched 916 * during the previous match operation. 917 * @param status a reference to a UErrorCode to receive any errors. 918 * @return The (native) position in the input string of the start of the last match. 919 * @stable ICU 2.4 920 */ 921 virtual int32_t start(UErrorCode &status) const; 922 923 /** 924 * @draft ICU 4.6 925 */ 926 virtual int64_t start64(UErrorCode &status) const; 927 928 929 /** 930 * Returns the index in the input string of the start of the text matched by the 931 * specified capture group during the previous match operation. Return -1 if 932 * the capture group exists in the pattern, but was not part of the last match. 933 * 934 * @param group the capture group number 935 * @param status A reference to a UErrorCode to receive any errors. Possible 936 * errors are U_REGEX_INVALID_STATE if no match has been 937 * attempted or the last match failed, and 938 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number 939 * @return the (native) start position of substring matched by the specified group. 940 * @stable ICU 2.4 941 */ 942 virtual int32_t start(int32_t group, UErrorCode &status) const; 943 944 /** 945 * @draft ICU 4.6 946 */ 947 virtual int64_t start64(int32_t group, UErrorCode &status) const; 948 949 950 /** 951 * Returns the index in the input string of the first character following the 952 * text matched during the previous match operation. 953 * @param status A reference to a UErrorCode to receive any errors. Possible 954 * errors are U_REGEX_INVALID_STATE if no match has been 955 * attempted or the last match failed. 956 * @return the index of the last character matched, plus one. 957 * The index value returned is a native index, corresponding to 958 * code units for the underlying encoding type, for example, 959 * a byte index for UTF8. 960 * @stable ICU 2.4 961 */ 962 virtual int32_t end(UErrorCode &status) const; 963 964 /** 965 * @draft ICU 4.6 966 */ 967 virtual int64_t end64(UErrorCode &status) const; 968 969 970 /** 971 * Returns the index in the input string of the character following the 972 * text matched by the specified capture group during the previous match operation. 973 * @param group the capture group number 974 * @param status A reference to a UErrorCode to receive any errors. Possible 975 * errors are U_REGEX_INVALID_STATE if no match has been 976 * attempted or the last match failed and 977 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number 978 * @return the index of the first character following the text 979 * captured by the specifed group during the previous match operation. 980 * Return -1 if the capture group exists in the pattern but was not part of the match. 981 * The index value returned is a native index, corresponding to 982 * code units for the underlying encoding type, for example, 983 * a byte index for UTF8. 984 * @stable ICU 2.4 985 */ 986 virtual int32_t end(int32_t group, UErrorCode &status) const; 987 988 /** 989 * @draft ICU 4.6 990 */ 991 virtual int64_t end64(int32_t group, UErrorCode &status) const; 992 993 994 /** 995 * Resets this matcher. The effect is to remove any memory of previous matches, 996 * and to cause subsequent find() operations to begin at the beginning of 997 * the input string. 998 * 999 * @return this RegexMatcher. 1000 * @stable ICU 2.4 1001 */ 1002 virtual RegexMatcher &reset(); 1003 1004 1005 /** 1006 * Resets this matcher, and set the current input position. 1007 * The effect is to remove any memory of previous matches, 1008 * and to cause subsequent find() operations to begin at 1009 * the specified (native) position in the input string. 1010 * <p> 1011 * The matcher's region is reset to its default, which is the entire 1012 * input string. 1013 * <p> 1014 * An alternative to this function is to set a match region 1015 * beginning at the desired index. 1016 * 1017 * @return this RegexMatcher. 1018 * @stable ICU 2.8 1019 */ 1020 virtual RegexMatcher &reset(int64_t index, UErrorCode &status); 1021 1022 1023 /** 1024 * Resets this matcher with a new input string. This allows instances of RegexMatcher 1025 * to be reused, which is more efficient than creating a new RegexMatcher for 1026 * each input string to be processed. 1027 * @param input The new string on which subsequent pattern matches will operate. 1028 * The matcher retains a reference to the callers string, and operates 1029 * directly on that. Ownership of the string remains with the caller. 1030 * Because no copy of the string is made, it is essential that the 1031 * caller not delete the string until after regexp operations on it 1032 * are done. 1033 * Note that while a reset on the matcher with an input string that is then 1034 * modified across/during matcher operations may be supported currently for UnicodeString, 1035 * this was not originally intended behavior, and support for this is not guaranteed 1036 * in upcoming versions of ICU. 1037 * @return this RegexMatcher. 1038 * @stable ICU 2.4 1039 */ 1040 virtual RegexMatcher &reset(const UnicodeString &input); 1041 1042 1043 /** 1044 * Resets this matcher with a new input string. This allows instances of RegexMatcher 1045 * to be reused, which is more efficient than creating a new RegexMatcher for 1046 * each input string to be processed. 1047 * @param input The new string on which subsequent pattern matches will operate. 1048 * The matcher makes a shallow clone of the given text; ownership of the 1049 * original string remains with the caller. Because no deep copy of the 1050 * text is made, it is essential that the caller not modify the string 1051 * until after regexp operations on it are done. 1052 * @return this RegexMatcher. 1053 * 1054 * @draft ICU 4.6 1055 */ 1056 virtual RegexMatcher &reset(UText *input); 1057 1058 private: 1059 /** 1060 * Cause a compilation error if an application accidently attempts to 1061 * reset a matcher with a (UChar *) string as input rather than 1062 * a UnicodeString. Avoids a dangling reference to a temporary string. 1063 * <p> 1064 * To efficiently work with UChar *strings, wrap the data in a UnicodeString 1065 * using one of the aliasing constructors, such as 1066 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code> 1067 * or in a UText, using 1068 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code> 1069 * 1070 * @internal 1071 */ 1072 RegexMatcher &reset(const UChar *input); 1073 public: 1074 1075 /** 1076 * Returns the input string being matched. Ownership of the string belongs to 1077 * the matcher; it should not be altered or deleted. This method will work even if the input 1078 * was originally supplied as a UText. 1079 * @return the input string 1080 * @stable ICU 2.4 1081 */ 1082 virtual const UnicodeString &input() const; 1083 1084 /** 1085 * Returns the input string being matched. This is the live input text; it should not be 1086 * altered or deleted. This method will work even if the input was originally supplied as 1087 * a UnicodeString. 1088 * @return the input text 1089 * 1090 * @draft ICU 4.6 1091 */ 1092 virtual UText *inputText() const; 1093 1094 /** 1095 * Returns the input string being matched, either by copying it into the provided 1096 * UText parameter or by returning a shallow clone of the live input. Note that copying 1097 * the entire input may cause significant performance and memory issues. 1098 * @param dest The UText into which the input should be copied, or NULL to create a new UText 1099 * @return dest if non-NULL, a shallow copy of the input text otherwise 1100 * 1101 * @draft ICU 4.6 1102 */ 1103 virtual UText *getInput(UText *dest, UErrorCode &status) const; 1104 1105 1106 /** Sets the limits of this matcher's region. 1107 * The region is the part of the input string that will be searched to find a match. 1108 * Invoking this method resets the matcher, and then sets the region to start 1109 * at the index specified by the start parameter and end at the index specified 1110 * by the end parameter. 1111 * 1112 * Depending on the transparency and anchoring being used (see useTransparentBounds 1113 * and useAnchoringBounds), certain constructs such as anchors may behave differently 1114 * at or around the boundaries of the region 1115 * 1116 * The function will fail if start is greater than limit, or if either index 1117 * is less than zero or greater than the length of the string being matched. 1118 * 1119 * @param start The (native) index to begin searches at. 1120 * @param limit The index to end searches at (exclusive). 1121 * @param status A reference to a UErrorCode to receive any errors. 1122 * @stable ICU 4.0 1123 */ 1124 virtual RegexMatcher ®ion(int64_t start, int64_t limit, UErrorCode &status); 1125 1126 /** 1127 * Identical to region(start, limit, status) but also allows a start position without 1128 * resetting the region state. 1129 * @param startIndex The (native) index within the region bounds at which to begin searches. 1130 * @param status A reference to a UErrorCode to receive any errors. 1131 * If startIndex is not within the specified region bounds, 1132 * U_INDEX_OUTOFBOUNDS_ERROR is returned. 1133 * @draft ICU 4.6 1134 */ 1135 virtual RegexMatcher ®ion(int64_t regionStart, int64_t regionLimit, int64_t startIndex, UErrorCode &status); 1136 1137 /** 1138 * Reports the start index of this matcher's region. The searches this matcher 1139 * conducts are limited to finding matches within regionStart (inclusive) and 1140 * regionEnd (exclusive). 1141 * 1142 * @return The starting (native) index of this matcher's region. 1143 * @stable ICU 4.0 1144 */ 1145 virtual int32_t regionStart() const; 1146 1147 /** 1148 * @draft ICU 4.6 1149 */ 1150 virtual int64_t regionStart64() const; 1151 1152 1153 /** 1154 * Reports the end (limit) index (exclusive) of this matcher's region. The searches 1155 * this matcher conducts are limited to finding matches within regionStart 1156 * (inclusive) and regionEnd (exclusive). 1157 * 1158 * @return The ending point (native) of this matcher's region. 1159 * @stable ICU 4.0 1160 */ 1161 virtual int32_t regionEnd() const; 1162 1163 /** 1164 * @draft ICU 4.6 1165 */ 1166 virtual int64_t regionEnd64() const; 1167 1168 /** 1169 * Queries the transparency of region bounds for this matcher. 1170 * See useTransparentBounds for a description of transparent and opaque bounds. 1171 * By default, a matcher uses opaque region boundaries. 1172 * 1173 * @return TRUE if this matcher is using opaque bounds, false if it is not. 1174 * @stable ICU 4.0 1175 */ 1176 virtual UBool hasTransparentBounds() const; 1177 1178 /** 1179 * Sets the transparency of region bounds for this matcher. 1180 * Invoking this function with an argument of true will set this matcher to use transparent bounds. 1181 * If the boolean argument is false, then opaque bounds will be used. 1182 * 1183 * Using transparent bounds, the boundaries of this matcher's region are transparent 1184 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can 1185 * see text beyond the boundaries of the region while checking for a match. 1186 * 1187 * With opaque bounds, no text outside of the matcher's region is visible to lookahead, 1188 * lookbehind, and boundary matching constructs. 1189 * 1190 * By default, a matcher uses opaque bounds. 1191 * 1192 * @param b TRUE for transparent bounds; FALSE for opaque bounds 1193 * @return This Matcher; 1194 * @stable ICU 4.0 1195 **/ 1196 virtual RegexMatcher &useTransparentBounds(UBool b); 1197 1198 1199 /** 1200 * Return true if this matcher is using anchoring bounds. 1201 * By default, matchers use anchoring region boounds. 1202 * 1203 * @return TRUE if this matcher is using anchoring bounds. 1204 * @stable ICU 4.0 1205 */ 1206 virtual UBool hasAnchoringBounds() const; 1207 1208 1209 /** 1210 * Set whether this matcher is using Anchoring Bounds for its region. 1211 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start 1212 * and end of the region. Without Anchoring Bounds, anchors will only match at 1213 * the positions they would in the complete text. 1214 * 1215 * Anchoring Bounds are the default for regions. 1216 * 1217 * @param b TRUE if to enable anchoring bounds; FALSE to disable them. 1218 * @return This Matcher 1219 * @stable ICU 4.0 1220 */ 1221 virtual RegexMatcher &useAnchoringBounds(UBool b); 1222 1223 1224 /** 1225 * Return TRUE if the most recent matching operation touched the 1226 * end of the text being processed. In this case, additional input text could 1227 * change the results of that match. 1228 * 1229 * hitEnd() is defined for both successful and unsuccessful matches. 1230 * In either case hitEnd() will return TRUE if if the end of the text was 1231 * reached at any point during the matching process. 1232 * 1233 * @return TRUE if the most recent match hit the end of input 1234 * @stable ICU 4.0 1235 */ 1236 virtual UBool hitEnd() const; 1237 1238 /** 1239 * Return TRUE the most recent match succeeded and additional input could cause 1240 * it to fail. If this method returns false and a match was found, then more input 1241 * might change the match but the match won't be lost. If a match was not found, 1242 * then requireEnd has no meaning. 1243 * 1244 * @return TRUE if more input could cause the most recent match to no longer match. 1245 * @stable ICU 4.0 1246 */ 1247 virtual UBool requireEnd() const; 1248 1249 1250 /** 1251 * Returns the pattern that is interpreted by this matcher. 1252 * @return the RegexPattern for this RegexMatcher 1253 * @stable ICU 2.4 1254 */ 1255 virtual const RegexPattern &pattern() const; 1256 1257 1258 /** 1259 * Replaces every substring of the input that matches the pattern 1260 * with the given replacement string. This is a convenience function that 1261 * provides a complete find-and-replace-all operation. 1262 * 1263 * This method first resets this matcher. It then scans the input string 1264 * looking for matches of the pattern. Input that is not part of any 1265 * match is left unchanged; each match is replaced in the result by the 1266 * replacement string. The replacement string may contain references to 1267 * capture groups. 1268 * 1269 * @param replacement a string containing the replacement text. 1270 * @param status a reference to a UErrorCode to receive any errors. 1271 * @return a string containing the results of the find and replace. 1272 * @stable ICU 2.4 1273 */ 1274 virtual UnicodeString replaceAll(const UnicodeString &replacement, UErrorCode &status); 1275 1276 1277 /** 1278 * Replaces every substring of the input that matches the pattern 1279 * with the given replacement string. This is a convenience function that 1280 * provides a complete find-and-replace-all operation. 1281 * 1282 * This method first resets this matcher. It then scans the input string 1283 * looking for matches of the pattern. Input that is not part of any 1284 * match is left unchanged; each match is replaced in the result by the 1285 * replacement string. The replacement string may contain references to 1286 * capture groups. 1287 * 1288 * @param replacement a string containing the replacement text. 1289 * @param dest a mutable UText in which the results are placed. 1290 * If NULL, a new UText will be created (which may not be mutable). 1291 * @param status a reference to a UErrorCode to receive any errors. 1292 * @return a string containing the results of the find and replace. 1293 * If a pre-allocated UText was provided, it will always be used and returned. 1294 * 1295 * @draft ICU 4.6 1296 */ 1297 virtual UText *replaceAll(UText *replacement, UText *dest, UErrorCode &status); 1298 1299 1300 /** 1301 * Replaces the first substring of the input that matches 1302 * the pattern with the replacement string. This is a convenience 1303 * function that provides a complete find-and-replace operation. 1304 * 1305 * <p>This function first resets this RegexMatcher. It then scans the input string 1306 * looking for a match of the pattern. Input that is not part 1307 * of the match is appended directly to the result string; the match is replaced 1308 * in the result by the replacement string. The replacement string may contain 1309 * references to captured groups.</p> 1310 * 1311 * <p>The state of the matcher (the position at which a subsequent find() 1312 * would begin) after completing a replaceFirst() is not specified. The 1313 * RegexMatcher should be reset before doing additional find() operations.</p> 1314 * 1315 * @param replacement a string containing the replacement text. 1316 * @param status a reference to a UErrorCode to receive any errors. 1317 * @return a string containing the results of the find and replace. 1318 * @stable ICU 2.4 1319 */ 1320 virtual UnicodeString replaceFirst(const UnicodeString &replacement, UErrorCode &status); 1321 1322 1323 /** 1324 * Replaces the first substring of the input that matches 1325 * the pattern with the replacement string. This is a convenience 1326 * function that provides a complete find-and-replace operation. 1327 * 1328 * <p>This function first resets this RegexMatcher. It then scans the input string 1329 * looking for a match of the pattern. Input that is not part 1330 * of the match is appended directly to the result string; the match is replaced 1331 * in the result by the replacement string. The replacement string may contain 1332 * references to captured groups.</p> 1333 * 1334 * <p>The state of the matcher (the position at which a subsequent find() 1335 * would begin) after completing a replaceFirst() is not specified. The 1336 * RegexMatcher should be reset before doing additional find() operations.</p> 1337 * 1338 * @param replacement a string containing the replacement text. 1339 * @param dest a mutable UText in which the results are placed. 1340 * If NULL, a new UText will be created (which may not be mutable). 1341 * @param status a reference to a UErrorCode to receive any errors. 1342 * @return a string containing the results of the find and replace. 1343 * If a pre-allocated UText was provided, it will always be used and returned. 1344 * 1345 * @draft ICU 4.6 1346 */ 1347 virtual UText *replaceFirst(UText *replacement, UText *dest, UErrorCode &status); 1348 1349 1350 /** 1351 * Implements a replace operation intended to be used as part of an 1352 * incremental find-and-replace. 1353 * 1354 * <p>The input string, starting from the end of the previous replacement and ending at 1355 * the start of the current match, is appended to the destination string. Then the 1356 * replacement string is appended to the output string, 1357 * including handling any substitutions of captured text.</p> 1358 * 1359 * <p>For simple, prepackaged, non-incremental find-and-replace 1360 * operations, see replaceFirst() or replaceAll().</p> 1361 * 1362 * @param dest A UnicodeString to which the results of the find-and-replace are appended. 1363 * @param replacement A UnicodeString that provides the text to be substituted for 1364 * the input text that matched the regexp pattern. The replacement 1365 * text may contain references to captured text from the 1366 * input. 1367 * @param status A reference to a UErrorCode to receive any errors. Possible 1368 * errors are U_REGEX_INVALID_STATE if no match has been 1369 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR 1370 * if the replacement text specifies a capture group that 1371 * does not exist in the pattern. 1372 * 1373 * @return this RegexMatcher 1374 * @stable ICU 2.4 1375 * 1376 */ 1377 virtual RegexMatcher &appendReplacement(UnicodeString &dest, 1378 const UnicodeString &replacement, UErrorCode &status); 1379 1380 1381 /** 1382 * Implements a replace operation intended to be used as part of an 1383 * incremental find-and-replace. 1384 * 1385 * <p>The input string, starting from the end of the previous replacement and ending at 1386 * the start of the current match, is appended to the destination string. Then the 1387 * replacement string is appended to the output string, 1388 * including handling any substitutions of captured text.</p> 1389 * 1390 * <p>For simple, prepackaged, non-incremental find-and-replace 1391 * operations, see replaceFirst() or replaceAll().</p> 1392 * 1393 * @param dest A mutable UText to which the results of the find-and-replace are appended. 1394 * Must not be NULL. 1395 * @param replacement A UText that provides the text to be substituted for 1396 * the input text that matched the regexp pattern. The replacement 1397 * text may contain references to captured text from the input. 1398 * @param status A reference to a UErrorCode to receive any errors. Possible 1399 * errors are U_REGEX_INVALID_STATE if no match has been 1400 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR 1401 * if the replacement text specifies a capture group that 1402 * does not exist in the pattern. 1403 * 1404 * @return this RegexMatcher 1405 * 1406 * @draft ICU 4.6 1407 */ 1408 virtual RegexMatcher &appendReplacement(UText *dest, 1409 UText *replacement, UErrorCode &status); 1410 1411 1412 /** 1413 * As the final step in a find-and-replace operation, append the remainder 1414 * of the input string, starting at the position following the last appendReplacement(), 1415 * to the destination string. <code>appendTail()</code> is intended to be invoked after one 1416 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>. 1417 * 1418 * @param dest A UnicodeString to which the results of the find-and-replace are appended. 1419 * @return the destination string. 1420 * @stable ICU 2.4 1421 */ 1422 virtual UnicodeString &appendTail(UnicodeString &dest); 1423 1424 1425 /** 1426 * As the final step in a find-and-replace operation, append the remainder 1427 * of the input string, starting at the position following the last appendReplacement(), 1428 * to the destination string. <code>appendTail()</code> is intended to be invoked after one 1429 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>. 1430 * 1431 * @param dest A mutable UText to which the results of the find-and-replace are appended. 1432 * Must not be NULL. 1433 * @return the destination string. 1434 * 1435 * @draft ICU 4.6 1436 */ 1437 virtual UText *appendTail(UText *dest, UErrorCode &status); 1438 1439 1440 /** 1441 * Split a string into fields. Somewhat like split() from Perl. 1442 * The pattern matches identify delimiters that separate the input 1443 * into fields. The input data between the matches becomes the 1444 * fields themselves. 1445 * 1446 * @param input The string to be split into fields. The field delimiters 1447 * match the pattern (in the "this" object). This matcher 1448 * will be reset to this input string. 1449 * @param dest An array of UnicodeStrings to receive the results of the split. 1450 * This is an array of actual UnicodeString objects, not an 1451 * array of pointers to strings. Local (stack based) arrays can 1452 * work well here. 1453 * @param destCapacity The number of elements in the destination array. 1454 * If the number of fields found is less than destCapacity, the 1455 * extra strings in the destination array are not altered. 1456 * If the number of destination strings is less than the number 1457 * of fields, the trailing part of the input string, including any 1458 * field delimiters, is placed in the last destination string. 1459 * @param status A reference to a UErrorCode to receive any errors. 1460 * @return The number of fields into which the input string was split. 1461 * @stable ICU 2.6 1462 */ 1463 virtual int32_t split(const UnicodeString &input, 1464 UnicodeString dest[], 1465 int32_t destCapacity, 1466 UErrorCode &status); 1467 1468 1469 /** 1470 * Split a string into fields. Somewhat like split() from Perl. 1471 * The pattern matches identify delimiters that separate the input 1472 * into fields. The input data between the matches becomes the 1473 * fields themselves. 1474 * 1475 * @param input The string to be split into fields. The field delimiters 1476 * match the pattern (in the "this" object). This matcher 1477 * will be reset to this input string. 1478 * @param dest An array of mutable UText structs to receive the results of the split. 1479 * If a field is NULL, a new UText is allocated to contain the results for 1480 * that field. This new UText is not guaranteed to be mutable. 1481 * @param destCapacity The number of elements in the destination array. 1482 * If the number of fields found is less than destCapacity, the 1483 * extra strings in the destination array are not altered. 1484 * If the number of destination strings is less than the number 1485 * of fields, the trailing part of the input string, including any 1486 * field delimiters, is placed in the last destination string. 1487 * @param status A reference to a UErrorCode to receive any errors. 1488 * @return The number of fields into which the input string was split. 1489 * 1490 * @draft ICU 4.6 1491 */ 1492 virtual int32_t split(UText *input, 1493 UText *dest[], 1494 int32_t destCapacity, 1495 UErrorCode &status); 1496 1497 /** 1498 * Set a processing time limit for match operations with this Matcher. 1499 * 1500 * Some patterns, when matching certain strings, can run in exponential time. 1501 * For practical purposes, the match operation may appear to be in an 1502 * infinite loop. 1503 * When a limit is set a match operation will fail with an error if the 1504 * limit is exceeded. 1505 * <p> 1506 * The units of the limit are steps of the match engine. 1507 * Correspondence with actual processor time will depend on the speed 1508 * of the processor and the details of the specific pattern, but will 1509 * typically be on the order of milliseconds. 1510 * <p> 1511 * By default, the matching time is not limited. 1512 * <p> 1513 * 1514 * @param limit The limit value, or 0 for no limit. 1515 * @param status A reference to a UErrorCode to receive any errors. 1516 * @stable ICU 4.0 1517 */ 1518 virtual void setTimeLimit(int32_t limit, UErrorCode &status); 1519 1520 /** 1521 * Get the time limit, if any, for match operations made with this Matcher. 1522 * 1523 * @return the maximum allowed time for a match, in units of processing steps. 1524 * @stable ICU 4.0 1525 */ 1526 virtual int32_t getTimeLimit() const; 1527 1528 /** 1529 * Set the amount of heap storage avaliable for use by the match backtracking stack. 1530 * The matcher is also reset, discarding any results from previous matches. 1531 * <p> 1532 * ICU uses a backtracking regular expression engine, with the backtrack stack 1533 * maintained on the heap. This function sets the limit to the amount of memory 1534 * that can be used for this purpose. A backtracking stack overflow will 1535 * result in an error from the match operation that caused it. 1536 * <p> 1537 * A limit is desirable because a malicious or poorly designed pattern can use 1538 * excessive memory, potentially crashing the process. A limit is enabled 1539 * by default. 1540 * <p> 1541 * @param limit The maximum size, in bytes, of the matching backtrack stack. 1542 * A value of zero means no limit. 1543 * The limit must be greater or equal to zero. 1544 * 1545 * @param status A reference to a UErrorCode to receive any errors. 1546 * 1547 * @stable ICU 4.0 1548 */ 1549 virtual void setStackLimit(int32_t limit, UErrorCode &status); 1550 1551 /** 1552 * Get the size of the heap storage available for use by the back tracking stack. 1553 * 1554 * @return the maximum backtracking stack size, in bytes, or zero if the 1555 * stack size is unlimited. 1556 * @stable ICU 4.0 1557 */ 1558 virtual int32_t getStackLimit() const; 1559 1560 1561 /** 1562 * Set a callback function for use with this Matcher. 1563 * During matching operations the function will be called periodically, 1564 * giving the application the opportunity to terminate a long-running 1565 * match. 1566 * 1567 * @param callback A pointer to the user-supplied callback function. 1568 * @param context User context pointer. The value supplied at the 1569 * time the callback function is set will be saved 1570 * and passed to the callback each time that it is called. 1571 * @param status A reference to a UErrorCode to receive any errors. 1572 * @stable ICU 4.0 1573 */ 1574 virtual void setMatchCallback(URegexMatchCallback *callback, 1575 const void *context, 1576 UErrorCode &status); 1577 1578 1579 /** 1580 * Get the callback function for this URegularExpression. 1581 * 1582 * @param callback Out paramater, receives a pointer to the user-supplied 1583 * callback function. 1584 * @param context Out parameter, receives the user context pointer that 1585 * was set when uregex_setMatchCallback() was called. 1586 * @param status A reference to a UErrorCode to receive any errors. 1587 * @stable ICU 4.0 1588 */ 1589 virtual void getMatchCallback(URegexMatchCallback *&callback, 1590 const void *&context, 1591 UErrorCode &status); 1592 1593 1594 /** 1595 * Set a progress callback function for use with find operations on this Matcher. 1596 * During find operations, the callback will be invoked after each return from a 1597 * match attempt, giving the application the opportunity to terminate a long-running 1598 * find operation. 1599 * 1600 * @param callback A pointer to the user-supplied callback function. 1601 * @param context User context pointer. The value supplied at the 1602 * time the callback function is set will be saved 1603 * and passed to the callback each time that it is called. 1604 * @param status A reference to a UErrorCode to receive any errors. 1605 * @draft ICU 4.6 1606 */ 1607 virtual void setFindProgressCallback(URegexFindProgressCallback *callback, 1608 const void *context, 1609 UErrorCode &status); 1610 1611 1612 /** 1613 * Get the find progress callback function for this URegularExpression. 1614 * 1615 * @param callback Out paramater, receives a pointer to the user-supplied 1616 * callback function. 1617 * @param context Out parameter, receives the user context pointer that 1618 * was set when uregex_setFindProgressCallback() was called. 1619 * @param status A reference to a UErrorCode to receive any errors. 1620 * @draft ICU 4.6 1621 */ 1622 virtual void getFindProgressCallback(URegexFindProgressCallback *&callback, 1623 const void *&context, 1624 UErrorCode &status); 1625 1626 1627 /** 1628 * setTrace Debug function, enable/disable tracing of the matching engine. 1629 * For internal ICU development use only. DO NO USE!!!! 1630 * @internal 1631 */ 1632 void setTrace(UBool state); 1633 1634 1635 /** 1636 * ICU "poor man's RTTI", returns a UClassID for this class. 1637 * 1638 * @stable ICU 2.2 1639 */ 1640 static UClassID U_EXPORT2 getStaticClassID(); 1641 1642 /** 1643 * ICU "poor man's RTTI", returns a UClassID for the actual class. 1644 * 1645 * @stable ICU 2.2 1646 */ 1647 virtual UClassID getDynamicClassID() const; 1648 1649 private: 1650 // Constructors and other object boilerplate are private. 1651 // Instances of RegexMatcher can not be assigned, copied, cloned, etc. 1652 RegexMatcher(); // default constructor not implemented 1653 RegexMatcher(const RegexPattern *pat); 1654 RegexMatcher(const RegexMatcher &other); 1655 RegexMatcher &operator =(const RegexMatcher &rhs); 1656 void init(UErrorCode &status); // Common initialization 1657 void init2(UText *t, UErrorCode &e); // Common initialization, part 2. 1658 1659 friend class RegexPattern; 1660 friend class RegexCImpl; 1661 public: 1662 /** @internal */ 1663 void resetPreserveRegion(); // Reset matcher state, but preserve any region. 1664 private: 1665 1666 // 1667 // MatchAt This is the internal interface to the match engine itself. 1668 // Match status comes back in matcher member variables. 1669 // 1670 void MatchAt(int64_t startIdx, UBool toEnd, UErrorCode &status); 1671 inline void backTrack(int64_t &inputIdx, int32_t &patIdx); 1672 UBool isWordBoundary(int64_t pos); // perform Perl-like \b test 1673 UBool isUWordBoundary(int64_t pos); // perform RBBI based \b test 1674 REStackFrame *resetStack(); 1675 inline REStackFrame *StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status); 1676 void IncrementTime(UErrorCode &status); 1677 UBool ReportFindProgress(int64_t matchIndex, UErrorCode &status); 1678 1679 int64_t appendGroup(int32_t groupNum, UText *dest, UErrorCode &status) const; 1680 1681 UBool findUsingChunk(); 1682 void MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status); 1683 UBool isChunkWordBoundary(int32_t pos); 1684 1685 const RegexPattern *fPattern; 1686 RegexPattern *fPatternOwned; // Non-NULL if this matcher owns the pattern, and 1687 // should delete it when through. 1688 1689 const UnicodeString *fInput; // The string being matched. Only used for input() 1690 UText *fInputText; // The text being matched. Is never NULL. 1691 UText *fAltInputText; // A shallow copy of the text being matched. 1692 // Only created if the pattern contains backreferences. 1693 int64_t fInputLength; // Full length of the input text. 1694 int32_t fFrameSize; // The size of a frame in the backtrack stack. 1695 1696 int64_t fRegionStart; // Start of the input region, default = 0. 1697 int64_t fRegionLimit; // End of input region, default to input.length. 1698 1699 int64_t fAnchorStart; // Region bounds for anchoring operations (^ or $). 1700 int64_t fAnchorLimit; // See useAnchoringBounds 1701 1702 int64_t fLookStart; // Region bounds for look-ahead/behind and 1703 int64_t fLookLimit; // and other boundary tests. See 1704 // useTransparentBounds 1705 1706 int64_t fActiveStart; // Currently active bounds for matching. 1707 int64_t fActiveLimit; // Usually is the same as region, but 1708 // is changed to fLookStart/Limit when 1709 // entering look around regions. 1710 1711 UBool fTransparentBounds; // True if using transparent bounds. 1712 UBool fAnchoringBounds; // True if using anchoring bounds. 1713 1714 UBool fMatch; // True if the last attempted match was successful. 1715 int64_t fMatchStart; // Position of the start of the most recent match 1716 int64_t fMatchEnd; // First position after the end of the most recent match 1717 // Zero if no previous match, even when a region 1718 // is active. 1719 int64_t fLastMatchEnd; // First position after the end of the previous match, 1720 // or -1 if there was no previous match. 1721 int64_t fAppendPosition; // First position after the end of the previous 1722 // appendReplacement(). As described by the 1723 // JavaDoc for Java Matcher, where it is called 1724 // "append position" 1725 UBool fHitEnd; // True if the last match touched the end of input. 1726 UBool fRequireEnd; // True if the last match required end-of-input 1727 // (matched $ or Z) 1728 1729 UVector64 *fStack; 1730 REStackFrame *fFrame; // After finding a match, the last active stack frame, 1731 // which will contain the capture group results. 1732 // NOT valid while match engine is running. 1733 1734 int64_t *fData; // Data area for use by the compiled pattern. 1735 int64_t fSmallData[8]; // Use this for data if it's enough. 1736 1737 int32_t fTimeLimit; // Max time (in arbitrary steps) to let the 1738 // match engine run. Zero for unlimited. 1739 1740 int32_t fTime; // Match time, accumulates while matching. 1741 int32_t fTickCounter; // Low bits counter for time. Counts down StateSaves. 1742 // Kept separately from fTime to keep as much 1743 // code as possible out of the inline 1744 // StateSave function. 1745 1746 int32_t fStackLimit; // Maximum memory size to use for the backtrack 1747 // stack, in bytes. Zero for unlimited. 1748 1749 URegexMatchCallback *fCallbackFn; // Pointer to match progress callback funct. 1750 // NULL if there is no callback. 1751 const void *fCallbackContext; // User Context ptr for callback function. 1752 1753 URegexFindProgressCallback *fFindProgressCallbackFn; // Pointer to match progress callback funct. 1754 // NULL if there is no callback. 1755 const void *fFindProgressCallbackContext; // User Context ptr for callback function. 1756 1757 1758 UBool fInputUniStrMaybeMutable; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility. 1759 1760 UBool fTraceDebug; // Set true for debug tracing of match engine. 1761 1762 UErrorCode fDeferredStatus; // Save error state that cannot be immediately 1763 // reported, or that permanently disables this matcher. 1764 1765 RuleBasedBreakIterator *fWordBreakItr; 1766 1767 1768 }; 1769 1770 U_NAMESPACE_END 1771 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS 1772 #endif 1773