Home | History | Annotate | Download | only in unicode
      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 &regex,
    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     * @internal ICU 4.4 technology preview
    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 &regex,
    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     * @internal ICU 4.4 technology preview
    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 &regex,
    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     * @internal ICU 4.4 technology preview
    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     * @internal ICU 4.4 technology preview
    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     * @internal ICU 4.4 technology preview
    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   &regex,
    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     * @internal ICU 4.4 technology preview
    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     * @internal ICU 4.4 technology preview
    491     */
    492     virtual UText *patternText() 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      * @internal ICU 4.4 technology preview
    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 &regexp, 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       *  @internal ICU 4.4 technology preview
    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 &regexp, 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       *  @internal ICU 4.4 technology preview
    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 &regexp, 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 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(int32_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 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(int32_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 position 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(int32_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     * Flag to disambiguate RegexMatcher::group signature
    854     * @internal ICU 4.4 technology preview
    855     */
    856     enum MatcherDestIsUTextFlag { MATCHER_DEST_IS_UTEXT };
    857 
    858    /**
    859     *   Returns a string containing the text matched by the previous match.
    860     *   If the pattern can match an empty string, an empty string may be returned.
    861     *   @param   dest        A mutable UText in which the matching text is placed.
    862     *                        If NULL, a new UText will be created (which may not be mutable).
    863     *   @param   flag        Must be RegexMatcher::MATCHER_DEST_IS_UTEXT; used to
    864     *                        disambiguate method signature.
    865     *   @param   status      A reference to a UErrorCode to receive any errors.
    866     *                        Possible errors are  U_REGEX_INVALID_STATE if no match
    867     *                        has been attempted or the last match failed.
    868     *   @return  A string containing the matched input text. If a pre-allocated UText
    869     *            was provided, it will always be used and returned.
    870     *
    871     *   @internal ICU 4.4 technology preview
    872     */
    873     virtual UText *group(UText *dest, MatcherDestIsUTextFlag flag, UErrorCode &status) const;
    874 
    875 
    876    /**
    877     *    Returns a string containing the text captured by the given group
    878     *    during the previous match operation.  Group(0) is the entire match.
    879     *
    880     *    @param groupNum the capture group number
    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 the captured text
    886     *    @stable ICU 2.4
    887     */
    888     virtual UnicodeString group(int32_t groupNum, UErrorCode &status) const;
    889 
    890 
    891    /**
    892     *   Returns a string containing the text captured by the given group
    893     *   during the previous match operation.  Group(0) is the entire match.
    894     *
    895     *   @param   groupNum    the capture group number
    896     *   @param   dest        A mutable UText in which the matching text is placed.
    897     *                        If NULL, a new UText will be created (which may not be mutable).
    898     *   @param   status      A reference to a UErrorCode to receive any errors.
    899     *                        Possible errors are  U_REGEX_INVALID_STATE if no match
    900     *                        has been attempted or the last match failed.
    901     *   @return  A string containing the matched input text. If a pre-allocated UText
    902     *            was provided, it will always be used and returned.
    903     *
    904     *   @internal ICU 4.4 technology preview
    905     */
    906     virtual UText *group(int32_t groupNum, UText *dest, UErrorCode &status) const;
    907 
    908 
    909    /**
    910     *   Returns the number of capturing groups in this matcher's pattern.
    911     *   @return the number of capture groups
    912     *   @stable ICU 2.4
    913     */
    914     virtual int32_t groupCount() const;
    915 
    916 
    917    /**
    918     *   Returns the index in the input string of the start of the text matched
    919     *   during the previous match operation.
    920     *    @param   status      a reference to a UErrorCode to receive any errors.
    921     *    @return              The position in the input string of the start of the last match.
    922     *    @stable ICU 2.4
    923     */
    924     virtual int32_t start(UErrorCode &status) const;
    925 
    926 
    927    /**
    928     *   Returns the index in the input string of the start of the text matched by the
    929     *    specified capture group during the previous match operation.  Return -1 if
    930     *    the capture group exists in the pattern, but was not part of the last match.
    931     *
    932     *    @param  group       the capture group number
    933     *    @param  status      A reference to a UErrorCode to receive any errors.  Possible
    934     *                        errors are  U_REGEX_INVALID_STATE if no match has been
    935     *                        attempted or the last match failed, and
    936     *                        U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
    937     *    @return the start position of substring matched by the specified group.
    938     *    @stable ICU 2.4
    939     */
    940     virtual int32_t start(int32_t group, UErrorCode &status) const;
    941 
    942 
    943    /**
    944     *    Returns the index in the input string of the first character following the
    945     *    text matched during the previous match operation.
    946     *   @param   status      A reference to a UErrorCode to receive any errors.  Possible
    947     *                        errors are  U_REGEX_INVALID_STATE if no match has been
    948     *                        attempted or the last match failed.
    949     *    @return the index of the last character matched, plus one.
    950     *   @stable ICU 2.4
    951     */
    952     virtual int32_t end(UErrorCode &status) const;
    953 
    954 
    955    /**
    956     *    Returns the index in the input string of the character following the
    957     *    text matched by the specified capture group during the previous match operation.
    958     *    @param group  the capture group number
    959     *    @param   status      A reference to a UErrorCode to receive any errors.  Possible
    960     *                        errors are  U_REGEX_INVALID_STATE if no match has been
    961     *                        attempted or the last match failed and
    962     *                        U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
    963     *    @return  the index of the first character following the text
    964     *              captured by the specifed group during the previous match operation.
    965     *              Return -1 if the capture group exists in the pattern but was not part of the match.
    966     *    @stable ICU 2.4
    967     */
    968     virtual int32_t end(int32_t group, UErrorCode &status) const;
    969 
    970 
    971    /**
    972     *   Resets this matcher.  The effect is to remove any memory of previous matches,
    973     *       and to cause subsequent find() operations to begin at the beginning of
    974     *       the input string.
    975     *
    976     *   @return this RegexMatcher.
    977     *   @stable ICU 2.4
    978     */
    979     virtual RegexMatcher &reset();
    980 
    981 
    982    /**
    983     *   Resets this matcher, and set the current input position.
    984     *   The effect is to remove any memory of previous matches,
    985     *       and to cause subsequent find() operations to begin at
    986     *       the specified position in the input string.
    987     * <p>
    988     *   The matcher's region is reset to its default, which is the entire
    989     *   input string.
    990     * <p>
    991     *   An alternative to this function is to set a match region
    992     *   beginning at the desired index.
    993     *
    994     *   @return this RegexMatcher.
    995     *   @stable ICU 2.8
    996     */
    997     virtual RegexMatcher &reset(int32_t index, UErrorCode &status);
    998 
    999 
   1000    /**
   1001     *   Resets this matcher with a new input string.  This allows instances of RegexMatcher
   1002     *     to be reused, which is more efficient than creating a new RegexMatcher for
   1003     *     each input string to be processed.
   1004     *   @param input The new string on which subsequent pattern matches will operate.
   1005     *                The matcher retains a reference to the callers string, and operates
   1006     *                directly on that.  Ownership of the string remains with the caller.
   1007     *                Because no copy of the string is made, it is essential that the
   1008     *                caller not delete the string until after regexp operations on it
   1009     *                are done.
   1010     *                Note that while a reset on the matcher with an input string that is then
   1011     *                modified across/during matcher operations may be supported currently for UnicodeString,
   1012     *                this was not originally intended behavior, and support for this is not guaranteed
   1013     *                in upcoming versions of ICU.
   1014     *   @return this RegexMatcher.
   1015     *   @stable ICU 2.4
   1016     */
   1017     virtual RegexMatcher &reset(const UnicodeString &input);
   1018 
   1019 
   1020    /**
   1021     *   Resets this matcher with a new input string.  This allows instances of RegexMatcher
   1022     *     to be reused, which is more efficient than creating a new RegexMatcher for
   1023     *     each input string to be processed.
   1024     *   @param input The new string on which subsequent pattern matches will operate.
   1025     *                The matcher makes a shallow clone of the given text; ownership of the
   1026     *                original string remains with the caller. Because no deep copy of the
   1027     *                text is made, it is essential that the caller not modify the string
   1028     *                until after regexp operations on it are done.
   1029     *   @return this RegexMatcher.
   1030     *
   1031     *   @internal ICU 4.4 technology preview
   1032     */
   1033     virtual RegexMatcher &reset(UText *input);
   1034 
   1035   // BEGIN android-added
   1036   // Removed this API after Android upgrade to ICU4.6.
   1037   /**
   1038     *  Set the subject text string upon which the regular expression is looking for matches
   1039     *  without changing any other aspect of the matching state.
   1040     *  The new and previous text strings must have the same content.
   1041     *
   1042     *  This function is intended for use in environments where ICU is operating on
   1043     *  strings that may move around in memory.  It provides a mechanism for notifying
   1044     *  ICU that the string has been relocated, and providing a new UText to access the
   1045     *  string in its new position.
   1046     *
   1047     *  Caution:  this function is normally used only by very specialized,
   1048     *  system-level code.
   1049     *
   1050     * @param input      The new (moved) text string.
   1051     * @param status     Receives errors detected by this function.
   1052     *
   1053     * @internal ICU 4.6
   1054     */
   1055     virtual RegexMatcher &refreshInputText(UText *input, UErrorCode &status);
   1056    // END android-added
   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     *   @internal ICU 4.4 technology preview
   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     *   @internal ICU 4.4 technology preview
   1102     */
   1103     virtual UText *getInput(UText *dest) 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 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 &region(int32_t start, int32_t limit, UErrorCode &status);
   1125 
   1126 
   1127    /**
   1128      * Reports the start index of this matcher's region. The searches this matcher
   1129      * conducts are limited to finding matches within regionStart (inclusive) and
   1130      * regionEnd (exclusive).
   1131      *
   1132      * @return The starting index of this matcher's region.
   1133      * @stable ICU 4.0
   1134      */
   1135      virtual int32_t regionStart() const;
   1136 
   1137 
   1138     /**
   1139       * Reports the end (limit) index (exclusive) of this matcher's region. The searches
   1140       * this matcher conducts are limited to finding matches within regionStart
   1141       * (inclusive) and regionEnd (exclusive).
   1142       *
   1143       * @return The ending point of this matcher's region.
   1144       * @stable ICU 4.0
   1145       */
   1146       virtual int32_t regionEnd() const;
   1147 
   1148     /**
   1149       * Queries the transparency of region bounds for this matcher.
   1150       * See useTransparentBounds for a description of transparent and opaque bounds.
   1151       * By default, a matcher uses opaque region boundaries.
   1152       *
   1153       * @return TRUE if this matcher is using opaque bounds, false if it is not.
   1154       * @stable ICU 4.0
   1155       */
   1156       virtual UBool hasTransparentBounds() const;
   1157 
   1158     /**
   1159       * Sets the transparency of region bounds for this matcher.
   1160       * Invoking this function with an argument of true will set this matcher to use transparent bounds.
   1161       * If the boolean argument is false, then opaque bounds will be used.
   1162       *
   1163       * Using transparent bounds, the boundaries of this matcher's region are transparent
   1164       * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
   1165       * see text beyond the boundaries of the region while checking for a match.
   1166       *
   1167       * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
   1168       * lookbehind, and boundary matching constructs.
   1169       *
   1170       * By default, a matcher uses opaque bounds.
   1171       *
   1172       * @param   b TRUE for transparent bounds; FALSE for opaque bounds
   1173       * @return  This Matcher;
   1174       * @stable ICU 4.0
   1175       **/
   1176       virtual RegexMatcher &useTransparentBounds(UBool b);
   1177 
   1178 
   1179     /**
   1180       * Return true if this matcher is using anchoring bounds.
   1181       * By default, matchers use anchoring region boounds.
   1182       *
   1183       * @return TRUE if this matcher is using anchoring bounds.
   1184       * @stable ICU 4.0
   1185       */
   1186       virtual UBool hasAnchoringBounds() const;
   1187 
   1188 
   1189     /**
   1190       * Set whether this matcher is using Anchoring Bounds for its region.
   1191       * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
   1192       * and end of the region.  Without Anchoring Bounds, anchors will only match at
   1193       * the positions they would in the complete text.
   1194       *
   1195       * Anchoring Bounds are the default for regions.
   1196       *
   1197       * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
   1198       * @return  This Matcher
   1199       * @stable ICU 4.0
   1200       */
   1201       virtual RegexMatcher &useAnchoringBounds(UBool b);
   1202 
   1203 
   1204     /**
   1205       * Return TRUE if the most recent matching operation touched the
   1206       *  end of the text being processed.  In this case, additional input text could
   1207       *  change the results of that match.
   1208       *
   1209       *  hitEnd() is defined for both successful and unsuccessful matches.
   1210       *  In either case hitEnd() will return TRUE if if the end of the text was
   1211       *  reached at any point during the matching process.
   1212       *
   1213       *  @return  TRUE if the most recent match hit the end of input
   1214       *  @stable ICU 4.0
   1215       */
   1216       virtual UBool hitEnd() const;
   1217 
   1218     /**
   1219       * Return TRUE the most recent match succeeded and additional input could cause
   1220       * it to fail. If this method returns false and a match was found, then more input
   1221       * might change the match but the match won't be lost. If a match was not found,
   1222       * then requireEnd has no meaning.
   1223       *
   1224       * @return TRUE if more input could cause the most recent match to no longer match.
   1225       * @stable ICU 4.0
   1226       */
   1227       virtual UBool requireEnd() const;
   1228 
   1229 
   1230    /**
   1231     *    Returns the pattern that is interpreted by this matcher.
   1232     *    @return  the RegexPattern for this RegexMatcher
   1233     *    @stable ICU 2.4
   1234     */
   1235     virtual const RegexPattern &pattern() const;
   1236 
   1237 
   1238    /**
   1239     *    Replaces every substring of the input that matches the pattern
   1240     *    with the given replacement string.  This is a convenience function that
   1241     *    provides a complete find-and-replace-all operation.
   1242     *
   1243     *    This method first resets this matcher. It then scans the input string
   1244     *    looking for matches of the pattern. Input that is not part of any
   1245     *    match is left unchanged; each match is replaced in the result by the
   1246     *    replacement string. The replacement string may contain references to
   1247     *    capture groups.
   1248     *
   1249     *    @param   replacement a string containing the replacement text.
   1250     *    @param   status      a reference to a UErrorCode to receive any errors.
   1251     *    @return              a string containing the results of the find and replace.
   1252     *    @stable ICU 2.4
   1253     */
   1254     virtual UnicodeString replaceAll(const UnicodeString &replacement, UErrorCode &status);
   1255 
   1256 
   1257    /**
   1258     *    Replaces every substring of the input that matches the pattern
   1259     *    with the given replacement string.  This is a convenience function that
   1260     *    provides a complete find-and-replace-all operation.
   1261     *
   1262     *    This method first resets this matcher. It then scans the input string
   1263     *    looking for matches of the pattern. Input that is not part of any
   1264     *    match is left unchanged; each match is replaced in the result by the
   1265     *    replacement string. The replacement string may contain references to
   1266     *    capture groups.
   1267     *
   1268     *    @param   replacement a string containing the replacement text.
   1269     *    @param   dest        a mutable UText in which the results are placed.
   1270     *                          If NULL, a new UText will be created (which may not be mutable).
   1271     *    @param   status      a reference to a UErrorCode to receive any errors.
   1272     *    @return              a string containing the results of the find and replace.
   1273     *                          If a pre-allocated UText was provided, it will always be used and returned.
   1274     *
   1275     *    @internal ICU 4.4 technology preview
   1276     */
   1277     virtual UText *replaceAll(UText *replacement, UText *dest, UErrorCode &status);
   1278 
   1279 
   1280    /**
   1281     * Replaces the first substring of the input that matches
   1282     * the pattern with the replacement string.   This is a convenience
   1283     * function that provides a complete find-and-replace operation.
   1284     *
   1285     * <p>This function first resets this RegexMatcher. It then scans the input string
   1286     * looking for a match of the pattern. Input that is not part
   1287     * of the match is appended directly to the result string; the match is replaced
   1288     * in the result by the replacement string. The replacement string may contain
   1289     * references to captured groups.</p>
   1290     *
   1291     * <p>The state of the matcher (the position at which a subsequent find()
   1292     *    would begin) after completing a replaceFirst() is not specified.  The
   1293     *    RegexMatcher should be reset before doing additional find() operations.</p>
   1294     *
   1295     *    @param   replacement a string containing the replacement text.
   1296     *    @param   status      a reference to a UErrorCode to receive any errors.
   1297     *    @return              a string containing the results of the find and replace.
   1298     *    @stable ICU 2.4
   1299     */
   1300     virtual UnicodeString replaceFirst(const UnicodeString &replacement, UErrorCode &status);
   1301 
   1302 
   1303    /**
   1304     * Replaces the first substring of the input that matches
   1305     * the pattern with the replacement string.   This is a convenience
   1306     * function that provides a complete find-and-replace operation.
   1307     *
   1308     * <p>This function first resets this RegexMatcher. It then scans the input string
   1309     * looking for a match of the pattern. Input that is not part
   1310     * of the match is appended directly to the result string; the match is replaced
   1311     * in the result by the replacement string. The replacement string may contain
   1312     * references to captured groups.</p>
   1313     *
   1314     * <p>The state of the matcher (the position at which a subsequent find()
   1315     *    would begin) after completing a replaceFirst() is not specified.  The
   1316     *    RegexMatcher should be reset before doing additional find() operations.</p>
   1317     *
   1318     *    @param   replacement a string containing the replacement text.
   1319     *    @param   dest        a mutable UText in which the results are placed.
   1320     *                          If NULL, a new UText will be created (which may not be mutable).
   1321     *    @param   status      a reference to a UErrorCode to receive any errors.
   1322     *    @return              a string containing the results of the find and replace.
   1323     *                          If a pre-allocated UText was provided, it will always be used and returned.
   1324     *
   1325     *    @internal ICU 4.4 technology preview
   1326     */
   1327     virtual UText *replaceFirst(UText *replacement, UText *dest, UErrorCode &status);
   1328 
   1329 
   1330    /**
   1331     *   Implements a replace operation intended to be used as part of an
   1332     *   incremental find-and-replace.
   1333     *
   1334     *   <p>The input string, starting from the end of the previous replacement and ending at
   1335     *   the start of the current match, is appended to the destination string.  Then the
   1336     *   replacement string is appended to the output string,
   1337     *   including handling any substitutions of captured text.</p>
   1338     *
   1339     *   <p>For simple, prepackaged, non-incremental find-and-replace
   1340     *   operations, see replaceFirst() or replaceAll().</p>
   1341     *
   1342     *   @param   dest        A UnicodeString to which the results of the find-and-replace are appended.
   1343     *   @param   replacement A UnicodeString that provides the text to be substituted for
   1344     *                        the input text that matched the regexp pattern.  The replacement
   1345     *                        text may contain references to captured text from the
   1346     *                        input.
   1347     *   @param   status      A reference to a UErrorCode to receive any errors.  Possible
   1348     *                        errors are  U_REGEX_INVALID_STATE if no match has been
   1349     *                        attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
   1350     *                        if the replacement text specifies a capture group that
   1351     *                        does not exist in the pattern.
   1352     *
   1353     *   @return  this  RegexMatcher
   1354     *   @stable ICU 2.4
   1355     *
   1356     */
   1357     virtual RegexMatcher &appendReplacement(UnicodeString &dest,
   1358         const UnicodeString &replacement, UErrorCode &status);
   1359 
   1360 
   1361    /**
   1362     *   Implements a replace operation intended to be used as part of an
   1363     *   incremental find-and-replace.
   1364     *
   1365     *   <p>The input string, starting from the end of the previous replacement and ending at
   1366     *   the start of the current match, is appended to the destination string.  Then the
   1367     *   replacement string is appended to the output string,
   1368     *   including handling any substitutions of captured text.</p>
   1369     *
   1370     *   <p>For simple, prepackaged, non-incremental find-and-replace
   1371     *   operations, see replaceFirst() or replaceAll().</p>
   1372     *
   1373     *   @param   dest        A mutable UText to which the results of the find-and-replace are appended.
   1374     *                         Must not be NULL.
   1375     *   @param   replacement A UText that provides the text to be substituted for
   1376     *                        the input text that matched the regexp pattern.  The replacement
   1377     *                        text may contain references to captured text from the input.
   1378     *   @param   status      A reference to a UErrorCode to receive any errors.  Possible
   1379     *                        errors are  U_REGEX_INVALID_STATE if no match has been
   1380     *                        attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
   1381     *                        if the replacement text specifies a capture group that
   1382     *                        does not exist in the pattern.
   1383     *
   1384     *   @return  this  RegexMatcher
   1385     *
   1386     *   @internal ICU 4.4 technology preview
   1387     */
   1388     virtual RegexMatcher &appendReplacement(UText *dest,
   1389         UText *replacement, UErrorCode &status);
   1390 
   1391 
   1392    /**
   1393     * As the final step in a find-and-replace operation, append the remainder
   1394     * of the input string, starting at the position following the last appendReplacement(),
   1395     * to the destination string. <code>appendTail()</code> is intended to be invoked after one
   1396     * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
   1397     *
   1398     *  @param dest A UnicodeString to which the results of the find-and-replace are appended.
   1399     *  @return  the destination string.
   1400     *  @stable ICU 2.4
   1401     */
   1402     virtual UnicodeString &appendTail(UnicodeString &dest);
   1403 
   1404 
   1405    /**
   1406     * As the final step in a find-and-replace operation, append the remainder
   1407     * of the input string, starting at the position following the last appendReplacement(),
   1408     * to the destination string. <code>appendTail()</code> is intended to be invoked after one
   1409     * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
   1410     *
   1411     *  @param dest A mutable UText to which the results of the find-and-replace are appended.
   1412     *               Must not be NULL.
   1413     *  @return  the destination string.
   1414     *
   1415     *  @internal ICU 4.4 technology preview
   1416     */
   1417     virtual UText *appendTail(UText *dest);
   1418 
   1419 
   1420     /**
   1421      * Split a string into fields.  Somewhat like split() from Perl.
   1422      * The pattern matches identify delimiters that separate the input
   1423      *  into fields.  The input data between the matches becomes the
   1424      *  fields themselves.
   1425      *
   1426      * @param input   The string to be split into fields.  The field delimiters
   1427      *                match the pattern (in the "this" object).  This matcher
   1428      *                will be reset to this input string.
   1429      * @param dest    An array of UnicodeStrings to receive the results of the split.
   1430      *                This is an array of actual UnicodeString objects, not an
   1431      *                array of pointers to strings.  Local (stack based) arrays can
   1432      *                work well here.
   1433      * @param destCapacity  The number of elements in the destination array.
   1434      *                If the number of fields found is less than destCapacity, the
   1435      *                extra strings in the destination array are not altered.
   1436      *                If the number of destination strings is less than the number
   1437      *                of fields, the trailing part of the input string, including any
   1438      *                field delimiters, is placed in the last destination string.
   1439      * @param status  A reference to a UErrorCode to receive any errors.
   1440      * @return        The number of fields into which the input string was split.
   1441      * @stable ICU 2.6
   1442      */
   1443     virtual int32_t  split(const UnicodeString &input,
   1444         UnicodeString    dest[],
   1445         int32_t          destCapacity,
   1446         UErrorCode       &status);
   1447 
   1448 
   1449     /**
   1450      * Split a string into fields.  Somewhat like split() from Perl.
   1451      * The pattern matches identify delimiters that separate the input
   1452      *  into fields.  The input data between the matches becomes the
   1453      *  fields themselves.
   1454      *
   1455      * @param input   The string to be split into fields.  The field delimiters
   1456      *                match the pattern (in the "this" object).  This matcher
   1457      *                will be reset to this input string.
   1458      * @param dest    An array of mutable UText structs to receive the results of the split.
   1459      *                If a field is NULL, a new UText is allocated to contain the results for
   1460      *                that field. This new UText is not guaranteed to be mutable.
   1461      * @param destCapacity  The number of elements in the destination array.
   1462      *                If the number of fields found is less than destCapacity, the
   1463      *                extra strings in the destination array are not altered.
   1464      *                If the number of destination strings is less than the number
   1465      *                of fields, the trailing part of the input string, including any
   1466      *                field delimiters, is placed in the last destination string.
   1467      * @param status  A reference to a UErrorCode to receive any errors.
   1468      * @return        The number of fields into which the input string was split.
   1469      *
   1470      * @internal ICU 4.4 technology preview
   1471      */
   1472     virtual int32_t  split(UText *input,
   1473         UText           *dest[],
   1474         int32_t          destCapacity,
   1475         UErrorCode       &status);
   1476 
   1477   /**
   1478     *   Set a processing time limit for match operations with this Matcher.
   1479     *
   1480     *   Some patterns, when matching certain strings, can run in exponential time.
   1481     *   For practical purposes, the match operation may appear to be in an
   1482     *   infinite loop.
   1483     *   When a limit is set a match operation will fail with an error if the
   1484     *   limit is exceeded.
   1485     *   <p>
   1486     *   The units of the limit are steps of the match engine.
   1487     *   Correspondence with actual processor time will depend on the speed
   1488     *   of the processor and the details of the specific pattern, but will
   1489     *   typically be on the order of milliseconds.
   1490     *   <p>
   1491     *   By default, the matching time is not limited.
   1492     *   <p>
   1493     *
   1494     *   @param   limit       The limit value, or 0 for no limit.
   1495     *   @param   status      A reference to a UErrorCode to receive any errors.
   1496     *   @stable ICU 4.0
   1497     */
   1498     virtual void setTimeLimit(int32_t limit, UErrorCode &status);
   1499 
   1500   /**
   1501     * Get the time limit, if any, for match operations made with this Matcher.
   1502     *
   1503     *   @return the maximum allowed time for a match, in units of processing steps.
   1504     *   @stable ICU 4.0
   1505     */
   1506     virtual int32_t getTimeLimit() const;
   1507 
   1508   /**
   1509     *  Set the amount of heap storage avaliable for use by the match backtracking stack.
   1510     *  The matcher is also reset, discarding any results from previous matches.
   1511     *  <p>
   1512     *  ICU uses a backtracking regular expression engine, with the backtrack stack
   1513     *  maintained on the heap.  This function sets the limit to the amount of memory
   1514     *  that can be used  for this purpose.  A backtracking stack overflow will
   1515     *  result in an error from the match operation that caused it.
   1516     *  <p>
   1517     *  A limit is desirable because a malicious or poorly designed pattern can use
   1518     *  excessive memory, potentially crashing the process.  A limit is enabled
   1519     *  by default.
   1520     *  <p>
   1521     *  @param limit  The maximum size, in bytes, of the matching backtrack stack.
   1522     *                A value of zero means no limit.
   1523     *                The limit must be greater or equal to zero.
   1524     *
   1525     *  @param status   A reference to a UErrorCode to receive any errors.
   1526     *
   1527     *  @stable ICU 4.0
   1528     */
   1529     virtual void setStackLimit(int32_t  limit, UErrorCode &status);
   1530 
   1531   /**
   1532     *  Get the size of the heap storage available for use by the back tracking stack.
   1533     *
   1534     *  @return  the maximum backtracking stack size, in bytes, or zero if the
   1535     *           stack size is unlimited.
   1536     *  @stable ICU 4.0
   1537     */
   1538     virtual int32_t  getStackLimit() const;
   1539 
   1540 
   1541   /**
   1542     * Set a callback function for use with this Matcher.
   1543     * During matching operations the function will be called periodically,
   1544     * giving the application the opportunity to terminate a long-running
   1545     * match.
   1546     *
   1547     *    @param   callback    A pointer to the user-supplied callback function.
   1548     *    @param   context     User context pointer.  The value supplied at the
   1549     *                         time the callback function is set will be saved
   1550     *                         and passed to the callback each time that it is called.
   1551     *    @param   status      A reference to a UErrorCode to receive any errors.
   1552     *  @stable ICU 4.0
   1553     */
   1554     virtual void setMatchCallback(URegexMatchCallback     *callback,
   1555                                   const void              *context,
   1556                                   UErrorCode              &status);
   1557 
   1558 
   1559   /**
   1560     *  Get the callback function for this URegularExpression.
   1561     *
   1562     *    @param   callback    Out paramater, receives a pointer to the user-supplied
   1563     *                         callback function.
   1564     *    @param   context     Out parameter, receives the user context pointer that
   1565     *                         was set when uregex_setMatchCallback() was called.
   1566     *    @param   status      A reference to a UErrorCode to receive any errors.
   1567     *    @stable ICU 4.0
   1568     */
   1569     virtual void getMatchCallback(URegexMatchCallback     *&callback,
   1570                                   const void              *&context,
   1571                                   UErrorCode              &status);
   1572 
   1573 
   1574    /**
   1575      *   setTrace   Debug function, enable/disable tracing of the matching engine.
   1576      *              For internal ICU development use only.  DO NO USE!!!!
   1577      *   @internal
   1578      */
   1579     void setTrace(UBool state);
   1580 
   1581 
   1582     /**
   1583     * ICU "poor man's RTTI", returns a UClassID for this class.
   1584     *
   1585     * @stable ICU 2.2
   1586     */
   1587     static UClassID U_EXPORT2 getStaticClassID();
   1588 
   1589     /**
   1590      * ICU "poor man's RTTI", returns a UClassID for the actual class.
   1591      *
   1592      * @stable ICU 2.2
   1593      */
   1594     virtual UClassID getDynamicClassID() const;
   1595 
   1596 private:
   1597     // Constructors and other object boilerplate are private.
   1598     // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
   1599     RegexMatcher();                  // default constructor not implemented
   1600     RegexMatcher(const RegexPattern *pat);
   1601     RegexMatcher(const RegexMatcher &other);
   1602     RegexMatcher &operator =(const RegexMatcher &rhs);
   1603     void init(UErrorCode &status);                      // Common initialization
   1604     void init2(UText *t, UErrorCode &e);  // Common initialization, part 2.
   1605 
   1606     friend class RegexPattern;
   1607     friend class RegexCImpl;
   1608 public:
   1609     /** @internal  */
   1610     void resetPreserveRegion();  // Reset matcher state, but preserve any region.
   1611 private:
   1612 
   1613     //
   1614     //  MatchAt   This is the internal interface to the match engine itself.
   1615     //            Match status comes back in matcher member variables.
   1616     //
   1617     void                 MatchAt(int64_t startIdx, UBool toEnd, UErrorCode &status);
   1618     inline void          backTrack(int64_t &inputIdx, int32_t &patIdx);
   1619     UBool                isWordBoundary(int64_t pos);         // perform Perl-like  \b test
   1620     UBool                isUWordBoundary(int64_t pos);        // perform RBBI based \b test
   1621     REStackFrame        *resetStack();
   1622     inline REStackFrame *StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status);
   1623     void                 IncrementTime(UErrorCode &status);
   1624 
   1625     int64_t              appendGroup(int32_t groupNum, UText *dest, UErrorCode &status) const;
   1626 
   1627     UBool                findUsingChunk();
   1628     void                 MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status);
   1629     UBool                isChunkWordBoundary(int32_t pos);
   1630 
   1631     const RegexPattern  *fPattern;
   1632     RegexPattern        *fPatternOwned;    // Non-NULL if this matcher owns the pattern, and
   1633                                            //   should delete it when through.
   1634 
   1635     const UnicodeString *fInput;           // The string being matched. Only used for input()
   1636     UText               *fInputText;       // The text being matched. Is never NULL.
   1637     UText               *fAltInputText;    // A shallow copy of the text being matched.
   1638                                            //   Only created if the pattern contains backreferences.
   1639     int64_t              fInputLength;     // Full length of the input text.
   1640     int32_t              fFrameSize;       // The size of a frame in the backtrack stack.
   1641 
   1642     int64_t              fRegionStart;     // Start of the input region, default = 0.
   1643     int64_t              fRegionLimit;     // End of input region, default to input.length.
   1644 
   1645     int64_t              fAnchorStart;     // Region bounds for anchoring operations (^ or $).
   1646     int64_t              fAnchorLimit;     //   See useAnchoringBounds
   1647 
   1648     int64_t              fLookStart;       // Region bounds for look-ahead/behind and
   1649     int64_t              fLookLimit;       //   and other boundary tests.  See
   1650                                            //   useTransparentBounds
   1651 
   1652     int64_t              fActiveStart;     // Currently active bounds for matching.
   1653     int64_t              fActiveLimit;     //   Usually is the same as region, but
   1654                                            //   is changed to fLookStart/Limit when
   1655                                            //   entering look around regions.
   1656 
   1657     UBool                fTransparentBounds;  // True if using transparent bounds.
   1658     UBool                fAnchoringBounds; // True if using anchoring bounds.
   1659 
   1660     UBool                fMatch;           // True if the last attempted match was successful.
   1661     int64_t              fMatchStart;      // Position of the start of the most recent match
   1662     int64_t              fMatchEnd;        // First position after the end of the most recent match
   1663                                            //   Zero if no previous match, even when a region
   1664                                            //   is active.
   1665     int64_t              fLastMatchEnd;    // First position after the end of the previous match,
   1666                                            //   or -1 if there was no previous match.
   1667     int64_t              fAppendPosition;  // First position after the end of the previous
   1668                                            //   appendReplacement().  As described by the
   1669                                            //   JavaDoc for Java Matcher, where it is called
   1670                                            //   "append position"
   1671     UBool                fHitEnd;          // True if the last match touched the end of input.
   1672     UBool                fRequireEnd;      // True if the last match required end-of-input
   1673                                            //    (matched $ or Z)
   1674 
   1675     UVector64           *fStack;
   1676     REStackFrame        *fFrame;           // After finding a match, the last active stack frame,
   1677                                            //   which will contain the capture group results.
   1678                                            //   NOT valid while match engine is running.
   1679 
   1680     int64_t             *fData;            // Data area for use by the compiled pattern.
   1681     int64_t             fSmallData[8];     //   Use this for data if it's enough.
   1682 
   1683     int32_t             fTimeLimit;        // Max time (in arbitrary steps) to let the
   1684                                            //   match engine run.  Zero for unlimited.
   1685 
   1686     int32_t             fTime;             // Match time, accumulates while matching.
   1687     int32_t             fTickCounter;      // Low bits counter for time.  Counts down StateSaves.
   1688                                            //   Kept separately from fTime to keep as much
   1689                                            //   code as possible out of the inline
   1690                                            //   StateSave function.
   1691 
   1692     int32_t             fStackLimit;       // Maximum memory size to use for the backtrack
   1693                                            //   stack, in bytes.  Zero for unlimited.
   1694 
   1695     URegexMatchCallback *fCallbackFn;       // Pointer to match progress callback funct.
   1696                                            //   NULL if there is no callback.
   1697     const void         *fCallbackContext;  // User Context ptr for callback function.
   1698 
   1699     UBool               fInputUniStrMaybeMutable;  // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
   1700 
   1701     UBool               fTraceDebug;       // Set true for debug tracing of match engine.
   1702 
   1703     UErrorCode          fDeferredStatus;   // Save error state that cannot be immediately
   1704                                            //   reported, or that permanently disables this matcher.
   1705 
   1706     RuleBasedBreakIterator  *fWordBreakItr;
   1707 
   1708 
   1709 };
   1710 
   1711 U_NAMESPACE_END
   1712 #endif  // UCONFIG_NO_REGULAR_EXPRESSIONS
   1713 #endif
   1714