2 **********************************************************************
3 * Copyright (C) 1998-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
11 #include "unicode/utypes.h"
12 #include "unicode/chariter.h"
17 * A concrete subclass of CharacterIterator that iterates over the
18 * characters (code units or code points) in a UChar array.
19 * It's possible not only to create an
20 * iterator that iterates over an entire UChar array, but also to
21 * create one that iterates over only a subrange of a UChar array
22 * (iterators over different subranges of the same UChar array don't
24 * @see CharacterIterator
25 * @see ForwardCharacterIterator
28 class U_COMMON_API UCharCharacterIterator
: public CharacterIterator
{
31 * Create an iterator over the UChar array referred to by "textPtr".
32 * The iteration range is 0 to <code>length-1</code>.
33 * text is only aliased, not adopted (the
34 * destructor will not delete it).
35 * @param textPtr The UChar array to be iterated over
36 * @param length The length of the UChar array
39 UCharCharacterIterator(const UChar
* textPtr
, int32_t length
);
42 * Create an iterator over the UChar array referred to by "textPtr".
43 * The iteration range is 0 to <code>length-1</code>.
44 * text is only aliased, not adopted (the
45 * destructor will not delete it).
47 * position is specified by "position". If "position" is outside the valid
48 * iteration range, the behavior of this object is undefined.
49 * @param textPtr The UChar array to be iteratd over
50 * @param length The length of the UChar array
51 * @param position The starting position of the iteration
54 UCharCharacterIterator(const UChar
* textPtr
, int32_t length
,
58 * Create an iterator over the UChar array referred to by "textPtr".
59 * The iteration range is 0 to <code>end-1</code>.
60 * text is only aliased, not adopted (the
61 * destructor will not delete it).
63 * position is specified by "position". If begin and end do not
64 * form a valid iteration range or "position" is outside the valid
65 * iteration range, the behavior of this object is undefined.
66 * @param textPtr The UChar array to be iterated over
67 * @param length The length of the UChar array
68 * @param textBegin The begin position of the iteration range
69 * @param textEnd The end position of the iteration range
70 * @param position The starting position of the iteration
73 UCharCharacterIterator(const UChar
* textPtr
, int32_t length
,
79 * Copy constructor. The new iterator iterates over the same range
80 * of the same string as "that", and its initial position is the
81 * same as "that"'s current position.
82 * @param that The UCharCharacterIterator to be copied
85 UCharCharacterIterator(const UCharCharacterIterator
& that
);
91 virtual ~UCharCharacterIterator();
94 * Assignment operator. *this is altered to iterate over the sane
95 * range of the same string as "that", and refers to the same
96 * character within that string as "that" does.
97 * @param that The object to be copied
98 * @return the newly created object
101 UCharCharacterIterator
&
102 operator=(const UCharCharacterIterator
& that
);
105 * Returns true if the iterators iterate over the same range of the
106 * same string and are pointing at the same character.
107 * @param that The ForwardCharacterIterator used to be compared for equality
108 * @return true if the iterators iterate over the same range of the
109 * same string and are pointing at the same character.
112 virtual UBool
operator==(const ForwardCharacterIterator
& that
) const;
115 * Generates a hash code for this iterator.
116 * @return the hash code.
119 virtual int32_t hashCode(void) const;
122 * Returns a new UCharCharacterIterator referring to the same
123 * character in the same range of the same string as this one. The
124 * caller must delete the new iterator.
125 * @return the CharacterIterator newly created
128 virtual CharacterIterator
* clone(void) const;
131 * Sets the iterator to refer to the first code unit in its
132 * iteration range, and returns that code unit.
133 * This can be used to begin an iteration with next().
134 * @return the first code unit in its iteration range.
137 virtual UChar
first(void);
140 * Sets the iterator to refer to the first code unit in its
141 * iteration range, returns that code unit, and moves the position
142 * to the second code unit. This is an alternative to setToStart()
143 * for forward iteration with nextPostInc().
144 * @return the first code unit in its iteration range
147 virtual UChar
firstPostInc(void);
150 * Sets the iterator to refer to the first code point in its
151 * iteration range, and returns that code unit,
152 * This can be used to begin an iteration with next32().
153 * Note that an iteration with next32PostInc(), beginning with,
154 * e.g., setToStart() or firstPostInc(), is more efficient.
155 * @return the first code point in its iteration range
158 virtual UChar32
first32(void);
161 * Sets the iterator to refer to the first code point in its
162 * iteration range, returns that code point, and moves the position
163 * to the second code point. This is an alternative to setToStart()
164 * for forward iteration with next32PostInc().
165 * @return the first code point in its iteration range.
168 virtual UChar32
first32PostInc(void);
171 * Sets the iterator to refer to the last code unit in its
172 * iteration range, and returns that code unit.
173 * This can be used to begin an iteration with previous().
174 * @return the last code unit in its iteration range.
177 virtual UChar
last(void);
180 * Sets the iterator to refer to the last code point in its
181 * iteration range, and returns that code unit.
182 * This can be used to begin an iteration with previous32().
183 * @return the last code point in its iteration range.
186 virtual UChar32
last32(void);
189 * Sets the iterator to refer to the "position"-th code unit
190 * in the text-storage object the iterator refers to, and
191 * returns that code unit.
192 * @param position the position within the text-storage object
193 * @return the code unit
196 virtual UChar
setIndex(int32_t position
);
199 * Sets the iterator to refer to the beginning of the code point
200 * that contains the "position"-th code unit
201 * in the text-storage object the iterator refers to, and
202 * returns that code point.
203 * The current position is adjusted to the beginning of the code point
204 * (its first code unit).
205 * @param position the position within the text-storage object
206 * @return the code unit
209 virtual UChar32
setIndex32(int32_t position
);
212 * Returns the code unit the iterator currently refers to.
213 * @return the code unit the iterator currently refers to.
216 virtual UChar
current(void) const;
219 * Returns the code point the iterator currently refers to.
220 * @return the code point the iterator currently refers to.
223 virtual UChar32
current32(void) const;
226 * Advances to the next code unit in the iteration range (toward
227 * endIndex()), and returns that code unit. If there are no more
228 * code units to return, returns DONE.
229 * @return the next code unit in the iteration range.
232 virtual UChar
next(void);
235 * Gets the current code unit for returning and advances to the next code unit
236 * in the iteration range
237 * (toward endIndex()). If there are
238 * no more code units to return, returns DONE.
239 * @return the current code unit.
242 virtual UChar
nextPostInc(void);
245 * Advances to the next code point in the iteration range (toward
246 * endIndex()), and returns that code point. If there are no more
247 * code points to return, returns DONE.
248 * Note that iteration with "pre-increment" semantics is less
249 * efficient than iteration with "post-increment" semantics
250 * that is provided by next32PostInc().
251 * @return the next code point in the iteration range.
254 virtual UChar32
next32(void);
257 * Gets the current code point for returning and advances to the next code point
258 * in the iteration range
259 * (toward endIndex()). If there are
260 * no more code points to return, returns DONE.
261 * @return the current point.
264 virtual UChar32
next32PostInc(void);
267 * Returns FALSE if there are no more code units or code points
268 * at or after the current position in the iteration range.
269 * This is used with nextPostInc() or next32PostInc() in forward
271 * @return FALSE if there are no more code units or code points
272 * at or after the current position in the iteration range.
275 virtual UBool
hasNext();
278 * Advances to the previous code unit in the iteration range (toward
279 * startIndex()), and returns that code unit. If there are no more
280 * code units to return, returns DONE.
281 * @return the previous code unit in the iteration range.
284 virtual UChar
previous(void);
287 * Advances to the previous code point in the iteration range (toward
288 * startIndex()), and returns that code point. If there are no more
289 * code points to return, returns DONE.
290 * @return the previous code point in the iteration range.
293 virtual UChar32
previous32(void);
296 * Returns FALSE if there are no more code units or code points
297 * before the current position in the iteration range.
298 * This is used with previous() or previous32() in backward
300 * @return FALSE if there are no more code units or code points
301 * before the current position in the iteration range.
304 virtual UBool
hasPrevious();
307 * Moves the current position relative to the start or end of the
308 * iteration range, or relative to the current position itself.
309 * The movement is expressed in numbers of code units forward
310 * or backward by specifying a positive or negative delta.
311 * @param delta the position relative to origin. A positive delta means forward;
312 * a negative delta means backward.
313 * @param origin Origin enumeration {kStart, kCurrent, kEnd}
314 * @return the new position
317 virtual int32_t move(int32_t delta
, EOrigin origin
);
320 * Moves the current position relative to the start or end of the
321 * iteration range, or relative to the current position itself.
322 * The movement is expressed in numbers of code points forward
323 * or backward by specifying a positive or negative delta.
324 * @param delta the position relative to origin. A positive delta means forward;
325 * a negative delta means backward.
326 * @param origin Origin enumeration {kStart, kCurrent, kEnd}
327 * @return the new position
330 virtual int32_t move32(int32_t delta
, EOrigin origin
);
333 * Sets the iterator to iterate over a new range of text
336 void setText(const UChar
* newText
, int32_t newTextLength
);
339 * Copies the UChar array under iteration into the UnicodeString
340 * referred to by "result". Even if this iterator iterates across
341 * only a part of this string, the whole string is copied.
342 * @param result Receives a copy of the text under iteration.
345 virtual void getText(UnicodeString
& result
);
348 * Return a class ID for this class (not really public)
349 * @return a class ID for this class
352 static UClassID U_EXPORT2
getStaticClassID(void);
355 * Return a class ID for this object (not really public)
356 * @return a class ID for this object.
359 virtual UClassID
getDynamicClassID(void) const;
363 * Protected constructor
366 UCharCharacterIterator();
368 * Protected member text