1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1998-2005, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
13 #include "unicode/utypes.h"
14 #include "unicode/chariter.h"
18 * \brief C++ API: char16_t Character Iterator
21 #if U_SHOW_CPLUSPLUS_API
25 * A concrete subclass of CharacterIterator that iterates over the
26 * characters (code units or code points) in a char16_t array.
27 * It's possible not only to create an
28 * iterator that iterates over an entire char16_t array, but also to
29 * create one that iterates over only a subrange of a char16_t array
30 * (iterators over different subranges of the same char16_t array don't
32 * @see CharacterIterator
33 * @see ForwardCharacterIterator
36 class U_COMMON_API UCharCharacterIterator
: public CharacterIterator
{
39 * Create an iterator over the char16_t array referred to by "textPtr".
40 * The iteration range is 0 to <code>length-1</code>.
41 * text is only aliased, not adopted (the
42 * destructor will not delete it).
43 * @param textPtr The char16_t array to be iterated over
44 * @param length The length of the char16_t array
47 UCharCharacterIterator(ConstChar16Ptr textPtr
, int32_t length
);
50 * Create an iterator over the char16_t array referred to by "textPtr".
51 * The iteration range is 0 to <code>length-1</code>.
52 * text is only aliased, not adopted (the
53 * destructor will not delete it).
55 * position is specified by "position". If "position" is outside the valid
56 * iteration range, the behavior of this object is undefined.
57 * @param textPtr The char16_t array to be iteratd over
58 * @param length The length of the char16_t array
59 * @param position The starting position of the iteration
62 UCharCharacterIterator(ConstChar16Ptr textPtr
, int32_t length
,
66 * Create an iterator over the char16_t array referred to by "textPtr".
67 * The iteration range is 0 to <code>end-1</code>.
68 * text is only aliased, not adopted (the
69 * destructor will not delete it).
71 * position is specified by "position". If begin and end do not
72 * form a valid iteration range or "position" is outside the valid
73 * iteration range, the behavior of this object is undefined.
74 * @param textPtr The char16_t array to be iterated over
75 * @param length The length of the char16_t array
76 * @param textBegin The begin position of the iteration range
77 * @param textEnd The end position of the iteration range
78 * @param position The starting position of the iteration
81 UCharCharacterIterator(ConstChar16Ptr textPtr
, int32_t length
,
87 * Copy constructor. The new iterator iterates over the same range
88 * of the same string as "that", and its initial position is the
89 * same as "that"'s current position.
90 * @param that The UCharCharacterIterator to be copied
93 UCharCharacterIterator(const UCharCharacterIterator
& that
);
99 virtual ~UCharCharacterIterator();
102 * Assignment operator. *this is altered to iterate over the sane
103 * range of the same string as "that", and refers to the same
104 * character within that string as "that" does.
105 * @param that The object to be copied
106 * @return the newly created object
109 UCharCharacterIterator
&
110 operator=(const UCharCharacterIterator
& that
);
113 * Returns true if the iterators iterate over the same range of the
114 * same string and are pointing at the same character.
115 * @param that The ForwardCharacterIterator used to be compared for equality
116 * @return true if the iterators iterate over the same range of the
117 * same string and are pointing at the same character.
120 virtual UBool
operator==(const ForwardCharacterIterator
& that
) const;
123 * Generates a hash code for this iterator.
124 * @return the hash code.
127 virtual int32_t hashCode(void) const;
130 * Returns a new UCharCharacterIterator referring to the same
131 * character in the same range of the same string as this one. The
132 * caller must delete the new iterator.
133 * @return the CharacterIterator newly created
136 virtual CharacterIterator
* clone(void) const;
139 * Sets the iterator to refer to the first code unit in its
140 * iteration range, and returns that code unit.
141 * This can be used to begin an iteration with next().
142 * @return the first code unit in its iteration range.
145 virtual char16_t first(void);
148 * Sets the iterator to refer to the first code unit in its
149 * iteration range, returns that code unit, and moves the position
150 * to the second code unit. This is an alternative to setToStart()
151 * for forward iteration with nextPostInc().
152 * @return the first code unit in its iteration range
155 virtual char16_t firstPostInc(void);
158 * Sets the iterator to refer to the first code point in its
159 * iteration range, and returns that code unit,
160 * This can be used to begin an iteration with next32().
161 * Note that an iteration with next32PostInc(), beginning with,
162 * e.g., setToStart() or firstPostInc(), is more efficient.
163 * @return the first code point in its iteration range
166 virtual UChar32
first32(void);
169 * Sets the iterator to refer to the first code point in its
170 * iteration range, returns that code point, and moves the position
171 * to the second code point. This is an alternative to setToStart()
172 * for forward iteration with next32PostInc().
173 * @return the first code point in its iteration range.
176 virtual UChar32
first32PostInc(void);
179 * Sets the iterator to refer to the last code unit in its
180 * iteration range, and returns that code unit.
181 * This can be used to begin an iteration with previous().
182 * @return the last code unit in its iteration range.
185 virtual char16_t last(void);
188 * Sets the iterator to refer to the last code point in its
189 * iteration range, and returns that code unit.
190 * This can be used to begin an iteration with previous32().
191 * @return the last code point in its iteration range.
194 virtual UChar32
last32(void);
197 * Sets the iterator to refer to the "position"-th code unit
198 * in the text-storage object the iterator refers to, and
199 * returns that code unit.
200 * @param position the position within the text-storage object
201 * @return the code unit
204 virtual char16_t setIndex(int32_t position
);
207 * Sets the iterator to refer to the beginning of the code point
208 * that contains the "position"-th code unit
209 * in the text-storage object the iterator refers to, and
210 * returns that code point.
211 * The current position is adjusted to the beginning of the code point
212 * (its first code unit).
213 * @param position the position within the text-storage object
214 * @return the code unit
217 virtual UChar32
setIndex32(int32_t position
);
220 * Returns the code unit the iterator currently refers to.
221 * @return the code unit the iterator currently refers to.
224 virtual char16_t current(void) const;
227 * Returns the code point the iterator currently refers to.
228 * @return the code point the iterator currently refers to.
231 virtual UChar32
current32(void) const;
234 * Advances to the next code unit in the iteration range (toward
235 * endIndex()), and returns that code unit. If there are no more
236 * code units to return, returns DONE.
237 * @return the next code unit in the iteration range.
240 virtual char16_t next(void);
243 * Gets the current code unit for returning and advances to the next code unit
244 * in the iteration range
245 * (toward endIndex()). If there are
246 * no more code units to return, returns DONE.
247 * @return the current code unit.
250 virtual char16_t nextPostInc(void);
253 * Advances to the next code point in the iteration range (toward
254 * endIndex()), and returns that code point. If there are no more
255 * code points to return, returns DONE.
256 * Note that iteration with "pre-increment" semantics is less
257 * efficient than iteration with "post-increment" semantics
258 * that is provided by next32PostInc().
259 * @return the next code point in the iteration range.
262 virtual UChar32
next32(void);
265 * Gets the current code point for returning and advances to the next code point
266 * in the iteration range
267 * (toward endIndex()). If there are
268 * no more code points to return, returns DONE.
269 * @return the current point.
272 virtual UChar32
next32PostInc(void);
275 * Returns FALSE if there are no more code units or code points
276 * at or after the current position in the iteration range.
277 * This is used with nextPostInc() or next32PostInc() in forward
279 * @return FALSE if there are no more code units or code points
280 * at or after the current position in the iteration range.
283 virtual UBool
hasNext();
286 * Advances to the previous code unit in the iteration range (toward
287 * startIndex()), and returns that code unit. If there are no more
288 * code units to return, returns DONE.
289 * @return the previous code unit in the iteration range.
292 virtual char16_t previous(void);
295 * Advances to the previous code point in the iteration range (toward
296 * startIndex()), and returns that code point. If there are no more
297 * code points to return, returns DONE.
298 * @return the previous code point in the iteration range.
301 virtual UChar32
previous32(void);
304 * Returns FALSE if there are no more code units or code points
305 * before the current position in the iteration range.
306 * This is used with previous() or previous32() in backward
308 * @return FALSE if there are no more code units or code points
309 * before the current position in the iteration range.
312 virtual UBool
hasPrevious();
315 * Moves the current position relative to the start or end of the
316 * iteration range, or relative to the current position itself.
317 * The movement is expressed in numbers of code units forward
318 * or backward by specifying a positive or negative delta.
319 * @param delta the position relative to origin. A positive delta means forward;
320 * a negative delta means backward.
321 * @param origin Origin enumeration {kStart, kCurrent, kEnd}
322 * @return the new position
325 virtual int32_t move(int32_t delta
, EOrigin origin
);
328 * Moves the current position relative to the start or end of the
329 * iteration range, or relative to the current position itself.
330 * The movement is expressed in numbers of code points forward
331 * or backward by specifying a positive or negative delta.
332 * @param delta the position relative to origin. A positive delta means forward;
333 * a negative delta means backward.
334 * @param origin Origin enumeration {kStart, kCurrent, kEnd}
335 * @return the new position
339 // One of the system headers right now is sometimes defining a conflicting macro we don't use
342 virtual int32_t move32(int32_t delta
, EOrigin origin
);
345 * Sets the iterator to iterate over a new range of text
348 void setText(ConstChar16Ptr newText
, int32_t newTextLength
);
351 * Copies the char16_t array under iteration into the UnicodeString
352 * referred to by "result". Even if this iterator iterates across
353 * only a part of this string, the whole string is copied.
354 * @param result Receives a copy of the text under iteration.
357 virtual void getText(UnicodeString
& result
);
360 * Return a class ID for this class (not really public)
361 * @return a class ID for this class
364 static UClassID U_EXPORT2
getStaticClassID(void);
367 * Return a class ID for this object (not really public)
368 * @return a class ID for this object.
371 virtual UClassID
getDynamicClassID(void) const;
375 * Protected constructor
378 UCharCharacterIterator();
380 * Protected member text
383 const char16_t* text
;
388 #endif // U_SHOW_CPLUSPLUS_API