Home | History | Annotate | Download | only in i18n
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html
      3 /*
      4 **********************************************************************
      5 *   Copyright (c) 2002-2011, International Business Machines Corporation
      6 *   and others.  All Rights Reserved.
      7 **********************************************************************
      8 *   Date        Name        Description
      9 *   01/21/2002  aliu        Creation.
     10 **********************************************************************
     11 */
     12 
     13 #ifndef STRREPL_H
     14 #define STRREPL_H
     15 
     16 #include "unicode/utypes.h"
     17 
     18 #if !UCONFIG_NO_TRANSLITERATION
     19 
     20 #include "unicode/unifunct.h"
     21 #include "unicode/unirepl.h"
     22 #include "unicode/unistr.h"
     23 
     24 U_NAMESPACE_BEGIN
     25 
     26 class TransliterationRuleData;
     27 
     28 /**
     29  * A replacer that produces static text as its output.  The text may
     30  * contain transliterator stand-in characters that represent nested
     31  * UnicodeReplacer objects, making it possible to encode a tree of
     32  * replacers in a StringReplacer.  A StringReplacer that contains such
     33  * stand-ins is called a <em>complex</em> StringReplacer.  A complex
     34  * StringReplacer has a slower processing loop than a non-complex one.
     35  * @author Alan Liu
     36  */
     37 class StringReplacer : public UnicodeFunctor, public UnicodeReplacer {
     38 
     39  private:
     40 
     41     /**
     42      * Output text, possibly containing stand-in characters that
     43      * represent nested UnicodeReplacers.
     44      */
     45     UnicodeString output;
     46 
     47     /**
     48      * Cursor position.  Value is ignored if hasCursor is false.
     49      */
     50     int32_t cursorPos;
     51 
     52     /**
     53      * True if this object outputs a cursor position.
     54      */
     55     UBool hasCursor;
     56 
     57     /**
     58      * A complex object contains nested replacers and requires more
     59      * complex processing.  StringReplacers are initially assumed to
     60      * be complex.  If no nested replacers are seen during processing,
     61      * then isComplex is set to false, and future replacements are
     62      * short circuited for better performance.
     63      */
     64     UBool isComplex;
     65 
     66     /**
     67      * Object that translates stand-in characters in 'output' to
     68      * UnicodeReplacer objects.
     69      */
     70     const TransliterationRuleData* data;
     71 
     72  public:
     73 
     74     /**
     75      * Construct a StringReplacer that sets the emits the given output
     76      * text and sets the cursor to the given position.
     77      * @param theOutput text that will replace input text when the
     78      * replace() method is called.  May contain stand-in characters
     79      * that represent nested replacers.
     80      * @param theCursorPos cursor position that will be returned by
     81      * the replace() method
     82      * @param theData transliterator context object that translates
     83      * stand-in characters to UnicodeReplacer objects
     84      */
     85     StringReplacer(const UnicodeString& theOutput,
     86                    int32_t theCursorPos,
     87                    const TransliterationRuleData* theData);
     88 
     89     /**
     90      * Construct a StringReplacer that sets the emits the given output
     91      * text and does not modify the cursor.
     92      * @param theOutput text that will replace input text when the
     93      * replace() method is called.  May contain stand-in characters
     94      * that represent nested replacers.
     95      * @param theData transliterator context object that translates
     96      * stand-in characters to UnicodeReplacer objects
     97      */
     98     StringReplacer(const UnicodeString& theOutput,
     99                    const TransliterationRuleData* theData);
    100 
    101     /**
    102      * Copy constructor.
    103      */
    104     StringReplacer(const StringReplacer& other);
    105 
    106     /**
    107      * Destructor
    108      */
    109     virtual ~StringReplacer();
    110 
    111     /**
    112      * Implement UnicodeFunctor
    113      */
    114     virtual UnicodeFunctor* clone() const;
    115 
    116     /**
    117      * UnicodeFunctor API.  Cast 'this' to a UnicodeReplacer* pointer
    118      * and return the pointer.
    119      */
    120     virtual UnicodeReplacer* toReplacer() const;
    121 
    122     /**
    123      * UnicodeReplacer API
    124      */
    125     virtual int32_t replace(Replaceable& text,
    126                             int32_t start,
    127                             int32_t limit,
    128                             int32_t& cursor);
    129 
    130     /**
    131      * UnicodeReplacer API
    132      */
    133     virtual UnicodeString& toReplacerPattern(UnicodeString& result,
    134                                              UBool escapeUnprintable) const;
    135 
    136     /**
    137      * Implement UnicodeReplacer
    138      */
    139     virtual void addReplacementSetTo(UnicodeSet& toUnionTo) const;
    140 
    141     /**
    142      * UnicodeFunctor API
    143      */
    144     virtual void setData(const TransliterationRuleData*);
    145 
    146     /**
    147      * ICU "poor man's RTTI", returns a UClassID for this class.
    148      */
    149     static UClassID U_EXPORT2 getStaticClassID();
    150 
    151     /**
    152      * ICU "poor man's RTTI", returns a UClassID for the actual class.
    153      */
    154     virtual UClassID getDynamicClassID() const;
    155 };
    156 
    157 U_NAMESPACE_END
    158 
    159 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
    160 
    161 #endif
    162 
    163 //eof
    164