1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker **************************************************************************
5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 1999-2012, International Business Machines Corporation and
6*0e209d39SAndroid Build Coastguard Worker * others. All Rights Reserved.
7*0e209d39SAndroid Build Coastguard Worker **************************************************************************
8*0e209d39SAndroid Build Coastguard Worker * Date Name Description
9*0e209d39SAndroid Build Coastguard Worker * 11/17/99 aliu Creation. Ported from java. Modified to
10*0e209d39SAndroid Build Coastguard Worker * match current UnicodeString API. Forced
11*0e209d39SAndroid Build Coastguard Worker * to use name "handleReplaceBetween" because
12*0e209d39SAndroid Build Coastguard Worker * of existing methods in UnicodeString.
13*0e209d39SAndroid Build Coastguard Worker **************************************************************************
14*0e209d39SAndroid Build Coastguard Worker */
15*0e209d39SAndroid Build Coastguard Worker
16*0e209d39SAndroid Build Coastguard Worker #ifndef REP_H
17*0e209d39SAndroid Build Coastguard Worker #define REP_H
18*0e209d39SAndroid Build Coastguard Worker
19*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
20*0e209d39SAndroid Build Coastguard Worker
21*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API
22*0e209d39SAndroid Build Coastguard Worker
23*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h"
24*0e209d39SAndroid Build Coastguard Worker
25*0e209d39SAndroid Build Coastguard Worker /**
26*0e209d39SAndroid Build Coastguard Worker * \file
27*0e209d39SAndroid Build Coastguard Worker * \brief C++ API: Replaceable String
28*0e209d39SAndroid Build Coastguard Worker */
29*0e209d39SAndroid Build Coastguard Worker
30*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
31*0e209d39SAndroid Build Coastguard Worker
32*0e209d39SAndroid Build Coastguard Worker class UnicodeString;
33*0e209d39SAndroid Build Coastguard Worker
34*0e209d39SAndroid Build Coastguard Worker /**
35*0e209d39SAndroid Build Coastguard Worker * <code>Replaceable</code> is an abstract base class representing a
36*0e209d39SAndroid Build Coastguard Worker * string of characters that supports the replacement of a range of
37*0e209d39SAndroid Build Coastguard Worker * itself with a new string of characters. It is used by APIs that
38*0e209d39SAndroid Build Coastguard Worker * change a piece of text while retaining metadata. Metadata is data
39*0e209d39SAndroid Build Coastguard Worker * other than the Unicode characters returned by char32At(). One
40*0e209d39SAndroid Build Coastguard Worker * example of metadata is style attributes; another is an edit
41*0e209d39SAndroid Build Coastguard Worker * history, marking each character with an author and revision number.
42*0e209d39SAndroid Build Coastguard Worker *
43*0e209d39SAndroid Build Coastguard Worker * <p>An implicit aspect of the <code>Replaceable</code> API is that
44*0e209d39SAndroid Build Coastguard Worker * during a replace operation, new characters take on the metadata of
45*0e209d39SAndroid Build Coastguard Worker * the old characters. For example, if the string "the <b>bold</b>
46*0e209d39SAndroid Build Coastguard Worker * font" has range (4, 8) replaced with "strong", then it becomes "the
47*0e209d39SAndroid Build Coastguard Worker * <b>strong</b> font".
48*0e209d39SAndroid Build Coastguard Worker *
49*0e209d39SAndroid Build Coastguard Worker * <p><code>Replaceable</code> specifies ranges using a start
50*0e209d39SAndroid Build Coastguard Worker * offset and a limit offset. The range of characters thus specified
51*0e209d39SAndroid Build Coastguard Worker * includes the characters at offset start..limit-1. That is, the
52*0e209d39SAndroid Build Coastguard Worker * start offset is inclusive, and the limit offset is exclusive.
53*0e209d39SAndroid Build Coastguard Worker *
54*0e209d39SAndroid Build Coastguard Worker * <p><code>Replaceable</code> also includes API to access characters
55*0e209d39SAndroid Build Coastguard Worker * in the string: <code>length()</code>, <code>charAt()</code>,
56*0e209d39SAndroid Build Coastguard Worker * <code>char32At()</code>, and <code>extractBetween()</code>.
57*0e209d39SAndroid Build Coastguard Worker *
58*0e209d39SAndroid Build Coastguard Worker * <p>For a subclass to support metadata, typical behavior of
59*0e209d39SAndroid Build Coastguard Worker * <code>replace()</code> is the following:
60*0e209d39SAndroid Build Coastguard Worker * <ul>
61*0e209d39SAndroid Build Coastguard Worker * <li>Set the metadata of the new text to the metadata of the first
62*0e209d39SAndroid Build Coastguard Worker * character replaced</li>
63*0e209d39SAndroid Build Coastguard Worker * <li>If no characters are replaced, use the metadata of the
64*0e209d39SAndroid Build Coastguard Worker * previous character</li>
65*0e209d39SAndroid Build Coastguard Worker * <li>If there is no previous character (i.e. start == 0), use the
66*0e209d39SAndroid Build Coastguard Worker * following character</li>
67*0e209d39SAndroid Build Coastguard Worker * <li>If there is no following character (i.e. the replaceable was
68*0e209d39SAndroid Build Coastguard Worker * empty), use default metadata.<br>
69*0e209d39SAndroid Build Coastguard Worker * <li>If the code point U+FFFF is seen, it should be interpreted as
70*0e209d39SAndroid Build Coastguard Worker * a special marker having no metadata<li>
71*0e209d39SAndroid Build Coastguard Worker * </li>
72*0e209d39SAndroid Build Coastguard Worker * </ul>
73*0e209d39SAndroid Build Coastguard Worker * If this is not the behavior, the subclass should document any differences.
74*0e209d39SAndroid Build Coastguard Worker * @author Alan Liu
75*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
76*0e209d39SAndroid Build Coastguard Worker */
77*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API Replaceable : public UObject {
78*0e209d39SAndroid Build Coastguard Worker
79*0e209d39SAndroid Build Coastguard Worker public:
80*0e209d39SAndroid Build Coastguard Worker /**
81*0e209d39SAndroid Build Coastguard Worker * Destructor.
82*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
83*0e209d39SAndroid Build Coastguard Worker */
84*0e209d39SAndroid Build Coastguard Worker virtual ~Replaceable();
85*0e209d39SAndroid Build Coastguard Worker
86*0e209d39SAndroid Build Coastguard Worker /**
87*0e209d39SAndroid Build Coastguard Worker * Returns the number of 16-bit code units in the text.
88*0e209d39SAndroid Build Coastguard Worker * @return number of 16-bit code units in text
89*0e209d39SAndroid Build Coastguard Worker * @stable ICU 1.8
90*0e209d39SAndroid Build Coastguard Worker */
91*0e209d39SAndroid Build Coastguard Worker inline int32_t length() const;
92*0e209d39SAndroid Build Coastguard Worker
93*0e209d39SAndroid Build Coastguard Worker /**
94*0e209d39SAndroid Build Coastguard Worker * Returns the 16-bit code unit at the given offset into the text.
95*0e209d39SAndroid Build Coastguard Worker * @param offset an integer between 0 and <code>length()</code>-1
96*0e209d39SAndroid Build Coastguard Worker * inclusive
97*0e209d39SAndroid Build Coastguard Worker * @return 16-bit code unit of text at given offset
98*0e209d39SAndroid Build Coastguard Worker * @stable ICU 1.8
99*0e209d39SAndroid Build Coastguard Worker */
100*0e209d39SAndroid Build Coastguard Worker inline char16_t charAt(int32_t offset) const;
101*0e209d39SAndroid Build Coastguard Worker
102*0e209d39SAndroid Build Coastguard Worker /**
103*0e209d39SAndroid Build Coastguard Worker * Returns the 32-bit code point at the given 16-bit offset into
104*0e209d39SAndroid Build Coastguard Worker * the text. This assumes the text is stored as 16-bit code units
105*0e209d39SAndroid Build Coastguard Worker * with surrogate pairs intermixed. If the offset of a leading or
106*0e209d39SAndroid Build Coastguard Worker * trailing code unit of a surrogate pair is given, return the
107*0e209d39SAndroid Build Coastguard Worker * code point of the surrogate pair.
108*0e209d39SAndroid Build Coastguard Worker *
109*0e209d39SAndroid Build Coastguard Worker * @param offset an integer between 0 and <code>length()</code>-1
110*0e209d39SAndroid Build Coastguard Worker * inclusive
111*0e209d39SAndroid Build Coastguard Worker * @return 32-bit code point of text at given offset
112*0e209d39SAndroid Build Coastguard Worker * @stable ICU 1.8
113*0e209d39SAndroid Build Coastguard Worker */
114*0e209d39SAndroid Build Coastguard Worker inline UChar32 char32At(int32_t offset) const;
115*0e209d39SAndroid Build Coastguard Worker
116*0e209d39SAndroid Build Coastguard Worker /**
117*0e209d39SAndroid Build Coastguard Worker * Copies characters in the range [<tt>start</tt>, <tt>limit</tt>)
118*0e209d39SAndroid Build Coastguard Worker * into the UnicodeString <tt>target</tt>.
119*0e209d39SAndroid Build Coastguard Worker * @param start offset of first character which will be copied
120*0e209d39SAndroid Build Coastguard Worker * @param limit offset immediately following the last character to
121*0e209d39SAndroid Build Coastguard Worker * be copied
122*0e209d39SAndroid Build Coastguard Worker * @param target UnicodeString into which to copy characters.
123*0e209d39SAndroid Build Coastguard Worker * @return A reference to <TT>target</TT>
124*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.1
125*0e209d39SAndroid Build Coastguard Worker */
126*0e209d39SAndroid Build Coastguard Worker virtual void extractBetween(int32_t start,
127*0e209d39SAndroid Build Coastguard Worker int32_t limit,
128*0e209d39SAndroid Build Coastguard Worker UnicodeString& target) const = 0;
129*0e209d39SAndroid Build Coastguard Worker
130*0e209d39SAndroid Build Coastguard Worker /**
131*0e209d39SAndroid Build Coastguard Worker * Replaces a substring of this object with the given text. If the
132*0e209d39SAndroid Build Coastguard Worker * characters being replaced have metadata, the new characters
133*0e209d39SAndroid Build Coastguard Worker * that replace them should be given the same metadata.
134*0e209d39SAndroid Build Coastguard Worker *
135*0e209d39SAndroid Build Coastguard Worker * <p>Subclasses must ensure that if the text between start and
136*0e209d39SAndroid Build Coastguard Worker * limit is equal to the replacement text, that replace has no
137*0e209d39SAndroid Build Coastguard Worker * effect. That is, any metadata
138*0e209d39SAndroid Build Coastguard Worker * should be unaffected. In addition, subclasses are encouraged to
139*0e209d39SAndroid Build Coastguard Worker * check for initial and trailing identical characters, and make a
140*0e209d39SAndroid Build Coastguard Worker * smaller replacement if possible. This will preserve as much
141*0e209d39SAndroid Build Coastguard Worker * metadata as possible.
142*0e209d39SAndroid Build Coastguard Worker * @param start the beginning index, inclusive; <code>0 <= start
143*0e209d39SAndroid Build Coastguard Worker * <= limit</code>.
144*0e209d39SAndroid Build Coastguard Worker * @param limit the ending index, exclusive; <code>start <= limit
145*0e209d39SAndroid Build Coastguard Worker * <= length()</code>.
146*0e209d39SAndroid Build Coastguard Worker * @param text the text to replace characters <code>start</code>
147*0e209d39SAndroid Build Coastguard Worker * to <code>limit - 1</code>
148*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
149*0e209d39SAndroid Build Coastguard Worker */
150*0e209d39SAndroid Build Coastguard Worker virtual void handleReplaceBetween(int32_t start,
151*0e209d39SAndroid Build Coastguard Worker int32_t limit,
152*0e209d39SAndroid Build Coastguard Worker const UnicodeString& text) = 0;
153*0e209d39SAndroid Build Coastguard Worker // Note: All other methods in this class take the names of
154*0e209d39SAndroid Build Coastguard Worker // existing UnicodeString methods. This method is the exception.
155*0e209d39SAndroid Build Coastguard Worker // It is named differently because all replace methods of
156*0e209d39SAndroid Build Coastguard Worker // UnicodeString return a UnicodeString&. The 'between' is
157*0e209d39SAndroid Build Coastguard Worker // required in order to conform to the UnicodeString naming
158*0e209d39SAndroid Build Coastguard Worker // convention; API taking start/length are named <operation>, and
159*0e209d39SAndroid Build Coastguard Worker // those taking start/limit are named <operationBetween>. The
160*0e209d39SAndroid Build Coastguard Worker // 'handle' is added because 'replaceBetween' and
161*0e209d39SAndroid Build Coastguard Worker // 'doReplaceBetween' are already taken.
162*0e209d39SAndroid Build Coastguard Worker
163*0e209d39SAndroid Build Coastguard Worker /**
164*0e209d39SAndroid Build Coastguard Worker * Copies a substring of this object, retaining metadata.
165*0e209d39SAndroid Build Coastguard Worker * This method is used to duplicate or reorder substrings.
166*0e209d39SAndroid Build Coastguard Worker * The destination index must not overlap the source range.
167*0e209d39SAndroid Build Coastguard Worker *
168*0e209d39SAndroid Build Coastguard Worker * @param start the beginning index, inclusive; <code>0 <= start <=
169*0e209d39SAndroid Build Coastguard Worker * limit</code>.
170*0e209d39SAndroid Build Coastguard Worker * @param limit the ending index, exclusive; <code>start <= limit <=
171*0e209d39SAndroid Build Coastguard Worker * length()</code>.
172*0e209d39SAndroid Build Coastguard Worker * @param dest the destination index. The characters from
173*0e209d39SAndroid Build Coastguard Worker * <code>start..limit-1</code> will be copied to <code>dest</code>.
174*0e209d39SAndroid Build Coastguard Worker * Implementations of this method may assume that <code>dest <= start ||
175*0e209d39SAndroid Build Coastguard Worker * dest >= limit</code>.
176*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
177*0e209d39SAndroid Build Coastguard Worker */
178*0e209d39SAndroid Build Coastguard Worker virtual void copy(int32_t start, int32_t limit, int32_t dest) = 0;
179*0e209d39SAndroid Build Coastguard Worker
180*0e209d39SAndroid Build Coastguard Worker /**
181*0e209d39SAndroid Build Coastguard Worker * Returns true if this object contains metadata. If a
182*0e209d39SAndroid Build Coastguard Worker * Replaceable object has metadata, calls to the Replaceable API
183*0e209d39SAndroid Build Coastguard Worker * must be made so as to preserve metadata. If it does not, calls
184*0e209d39SAndroid Build Coastguard Worker * to the Replaceable API may be optimized to improve performance.
185*0e209d39SAndroid Build Coastguard Worker * The default implementation returns true.
186*0e209d39SAndroid Build Coastguard Worker * @return true if this object contains metadata
187*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.2
188*0e209d39SAndroid Build Coastguard Worker */
189*0e209d39SAndroid Build Coastguard Worker virtual UBool hasMetaData() const;
190*0e209d39SAndroid Build Coastguard Worker
191*0e209d39SAndroid Build Coastguard Worker /**
192*0e209d39SAndroid Build Coastguard Worker * Clone this object, an instance of a subclass of Replaceable.
193*0e209d39SAndroid Build Coastguard Worker * Clones can be used concurrently in multiple threads.
194*0e209d39SAndroid Build Coastguard Worker * If a subclass does not implement clone(), or if an error occurs,
195*0e209d39SAndroid Build Coastguard Worker * then nullptr is returned.
196*0e209d39SAndroid Build Coastguard Worker * The caller must delete the clone.
197*0e209d39SAndroid Build Coastguard Worker *
198*0e209d39SAndroid Build Coastguard Worker * @return a clone of this object
199*0e209d39SAndroid Build Coastguard Worker *
200*0e209d39SAndroid Build Coastguard Worker * @see getDynamicClassID
201*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.6
202*0e209d39SAndroid Build Coastguard Worker */
203*0e209d39SAndroid Build Coastguard Worker virtual Replaceable *clone() const;
204*0e209d39SAndroid Build Coastguard Worker
205*0e209d39SAndroid Build Coastguard Worker protected:
206*0e209d39SAndroid Build Coastguard Worker
207*0e209d39SAndroid Build Coastguard Worker /**
208*0e209d39SAndroid Build Coastguard Worker * Default constructor.
209*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4
210*0e209d39SAndroid Build Coastguard Worker */
211*0e209d39SAndroid Build Coastguard Worker inline Replaceable();
212*0e209d39SAndroid Build Coastguard Worker
213*0e209d39SAndroid Build Coastguard Worker /*
214*0e209d39SAndroid Build Coastguard Worker * Assignment operator not declared. The compiler will provide one
215*0e209d39SAndroid Build Coastguard Worker * which does nothing since this class does not contain any data members.
216*0e209d39SAndroid Build Coastguard Worker * API/code coverage may show the assignment operator as present and
217*0e209d39SAndroid Build Coastguard Worker * untested - ignore.
218*0e209d39SAndroid Build Coastguard Worker * Subclasses need this assignment operator if they use compiler-provided
219*0e209d39SAndroid Build Coastguard Worker * assignment operators of their own. An alternative to not declaring one
220*0e209d39SAndroid Build Coastguard Worker * here would be to declare and empty-implement a protected or public one.
221*0e209d39SAndroid Build Coastguard Worker Replaceable &Replaceable::operator=(const Replaceable &);
222*0e209d39SAndroid Build Coastguard Worker */
223*0e209d39SAndroid Build Coastguard Worker
224*0e209d39SAndroid Build Coastguard Worker /**
225*0e209d39SAndroid Build Coastguard Worker * Virtual version of length().
226*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4
227*0e209d39SAndroid Build Coastguard Worker */
228*0e209d39SAndroid Build Coastguard Worker virtual int32_t getLength() const = 0;
229*0e209d39SAndroid Build Coastguard Worker
230*0e209d39SAndroid Build Coastguard Worker /**
231*0e209d39SAndroid Build Coastguard Worker * Virtual version of charAt().
232*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4
233*0e209d39SAndroid Build Coastguard Worker */
234*0e209d39SAndroid Build Coastguard Worker virtual char16_t getCharAt(int32_t offset) const = 0;
235*0e209d39SAndroid Build Coastguard Worker
236*0e209d39SAndroid Build Coastguard Worker /**
237*0e209d39SAndroid Build Coastguard Worker * Virtual version of char32At().
238*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4
239*0e209d39SAndroid Build Coastguard Worker */
240*0e209d39SAndroid Build Coastguard Worker virtual UChar32 getChar32At(int32_t offset) const = 0;
241*0e209d39SAndroid Build Coastguard Worker };
242*0e209d39SAndroid Build Coastguard Worker
Replaceable()243*0e209d39SAndroid Build Coastguard Worker inline Replaceable::Replaceable() {}
244*0e209d39SAndroid Build Coastguard Worker
245*0e209d39SAndroid Build Coastguard Worker inline int32_t
length()246*0e209d39SAndroid Build Coastguard Worker Replaceable::length() const {
247*0e209d39SAndroid Build Coastguard Worker return getLength();
248*0e209d39SAndroid Build Coastguard Worker }
249*0e209d39SAndroid Build Coastguard Worker
250*0e209d39SAndroid Build Coastguard Worker inline char16_t
charAt(int32_t offset)251*0e209d39SAndroid Build Coastguard Worker Replaceable::charAt(int32_t offset) const {
252*0e209d39SAndroid Build Coastguard Worker return getCharAt(offset);
253*0e209d39SAndroid Build Coastguard Worker }
254*0e209d39SAndroid Build Coastguard Worker
255*0e209d39SAndroid Build Coastguard Worker inline UChar32
char32At(int32_t offset)256*0e209d39SAndroid Build Coastguard Worker Replaceable::char32At(int32_t offset) const {
257*0e209d39SAndroid Build Coastguard Worker return getChar32At(offset);
258*0e209d39SAndroid Build Coastguard Worker }
259*0e209d39SAndroid Build Coastguard Worker
260*0e209d39SAndroid Build Coastguard Worker // There is no rep.cpp, see unistr.cpp for Replaceable function implementations.
261*0e209d39SAndroid Build Coastguard Worker
262*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
263*0e209d39SAndroid Build Coastguard Worker
264*0e209d39SAndroid Build Coastguard Worker #endif /* U_SHOW_CPLUSPLUS_API */
265*0e209d39SAndroid Build Coastguard Worker
266*0e209d39SAndroid Build Coastguard Worker #endif
267