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