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