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