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