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