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"
15 #if U_SHOW_CPLUSPLUS_API
17 #include "unicode/chariter.h"
21 * \brief C++ API: char16_t Character Iterator
27 * A concrete subclass of CharacterIterator that iterates over the
28 * characters (code units or code points) in a char16_t array.
29 * It's possible not only to create an
30 * iterator that iterates over an entire char16_t array, but also to
31 * create one that iterates over only a subrange of a char16_t array
32 * (iterators over different subranges of the same char16_t array don't
34 * @see CharacterIterator
35 * @see ForwardCharacterIterator
38 class U_COMMON_API UCharCharacterIterator
: public CharacterIterator
{
41 * Create an iterator over the char16_t array referred to by "textPtr".
42 * The iteration range is 0 to <code>length-1</code>.
43 * text is only aliased, not adopted (the
44 * destructor will not delete it).
45 * @param textPtr The char16_t array to be iterated over
46 * @param length The length of the char16_t array
49 UCharCharacterIterator(ConstChar16Ptr textPtr
, int32_t length
);
52 * Create an iterator over the char16_t array referred to by "textPtr".
53 * The iteration range is 0 to <code>length-1</code>.
54 * text is only aliased, not adopted (the
55 * destructor will not delete it).
57 * position is specified by "position". If "position" is outside the valid
58 * iteration range, the behavior of this object is undefined.
59 * @param textPtr The char16_t array to be iteratd over
60 * @param length The length of the char16_t array
61 * @param position The starting position of the iteration
64 UCharCharacterIterator(ConstChar16Ptr textPtr
, int32_t length
,
68 * Create an iterator over the char16_t array referred to by "textPtr".
69 * The iteration range is 0 to <code>end-1</code>.
70 * text is only aliased, not adopted (the
71 * destructor will not delete it).
73 * position is specified by "position". If begin and end do not
74 * form a valid iteration range or "position" is outside the valid
75 * iteration range, the behavior of this object is undefined.
76 * @param textPtr The char16_t array to be iterated over
77 * @param length The length of the char16_t array
78 * @param textBegin The begin position of the iteration range
79 * @param textEnd The end position of the iteration range
80 * @param position The starting position of the iteration
83 UCharCharacterIterator(ConstChar16Ptr textPtr
, int32_t length
,
89 * Copy constructor. The new iterator iterates over the same range
90 * of the same string as "that", and its initial position is the
91 * same as "that"'s current position.
92 * @param that The UCharCharacterIterator to be copied
95 UCharCharacterIterator(const UCharCharacterIterator
& that
);
101 virtual ~UCharCharacterIterator();
104 * Assignment operator. *this is altered to iterate over the sane
105 * range of the same string as "that", and refers to the same
106 * character within that string as "that" does.
107 * @param that The object to be copied
108 * @return the newly created object
111 UCharCharacterIterator
&
112 operator=(const UCharCharacterIterator
& that
);
115 * Returns true if the iterators iterate over the same range of the
116 * same string and are pointing at the same character.
117 * @param that The ForwardCharacterIterator used to be compared for equality
118 * @return true if the iterators iterate over the same range of the
119 * same string and are pointing at the same character.
122 virtual UBool
operator==(const ForwardCharacterIterator
& that
) const;
125 * Generates a hash code for this iterator.
126 * @return the hash code.
129 virtual int32_t hashCode(void) const;
132 * Returns a new UCharCharacterIterator referring to the same
133 * character in the same range of the same string as this one. The
134 * caller must delete the new iterator.
135 * @return the CharacterIterator newly created
138 virtual UCharCharacterIterator
* clone() const;
141 * Sets the iterator to refer to the first code unit in its
142 * iteration range, and returns that code unit.
143 * This can be used to begin an iteration with next().
144 * @return the first code unit in its iteration range.
147 virtual char16_t first(void);
150 * Sets the iterator to refer to the first code unit in its
151 * iteration range, returns that code unit, and moves the position
152 * to the second code unit. This is an alternative to setToStart()
153 * for forward iteration with nextPostInc().
154 * @return the first code unit in its iteration range
157 virtual char16_t firstPostInc(void);
160 * Sets the iterator to refer to the first code point in its
161 * iteration range, and returns that code unit,
162 * This can be used to begin an iteration with next32().
163 * Note that an iteration with next32PostInc(), beginning with,
164 * e.g., setToStart() or firstPostInc(), is more efficient.
165 * @return the first code point in its iteration range
168 virtual UChar32
first32(void);
171 * Sets the iterator to refer to the first code point in its
172 * iteration range, returns that code point, and moves the position
173 * to the second code point. This is an alternative to setToStart()
174 * for forward iteration with next32PostInc().
175 * @return the first code point in its iteration range.
178 virtual UChar32
first32PostInc(void);
181 * Sets the iterator to refer to the last code unit in its
182 * iteration range, and returns that code unit.
183 * This can be used to begin an iteration with previous().
184 * @return the last code unit in its iteration range.
187 virtual char16_t last(void);
190 * Sets the iterator to refer to the last code point in its
191 * iteration range, and returns that code unit.
192 * This can be used to begin an iteration with previous32().
193 * @return the last code point in its iteration range.
196 virtual UChar32
last32(void);
199 * Sets the iterator to refer to the "position"-th code unit
200 * in the text-storage object the iterator refers to, and
201 * returns that code unit.
202 * @param position the position within the text-storage object
203 * @return the code unit
206 virtual char16_t setIndex(int32_t position
);
209 * Sets the iterator to refer to the beginning of the code point
210 * that contains the "position"-th code unit
211 * in the text-storage object the iterator refers to, and
212 * returns that code point.
213 * The current position is adjusted to the beginning of the code point
214 * (its first code unit).
215 * @param position the position within the text-storage object
216 * @return the code unit
219 virtual UChar32
setIndex32(int32_t position
);
222 * Returns the code unit the iterator currently refers to.
223 * @return the code unit the iterator currently refers to.
226 virtual char16_t current(void) const;
229 * Returns the code point the iterator currently refers to.
230 * @return the code point the iterator currently refers to.
233 virtual UChar32
current32(void) const;
236 * Advances to the next code unit in the iteration range (toward
237 * endIndex()), and returns that code unit. If there are no more
238 * code units to return, returns DONE.
239 * @return the next code unit in the iteration range.
242 virtual char16_t next(void);
245 * Gets the current code unit for returning and advances to the next code unit
246 * in the iteration range
247 * (toward endIndex()). If there are
248 * no more code units to return, returns DONE.
249 * @return the current code unit.
252 virtual char16_t nextPostInc(void);
255 * Advances to the next code point in the iteration range (toward
256 * endIndex()), and returns that code point. If there are no more
257 * code points to return, returns DONE.
258 * Note that iteration with "pre-increment" semantics is less
259 * efficient than iteration with "post-increment" semantics
260 * that is provided by next32PostInc().
261 * @return the next code point in the iteration range.
264 virtual UChar32
next32(void);
267 * Gets the current code point for returning and advances to the next code point
268 * in the iteration range
269 * (toward endIndex()). If there are
270 * no more code points to return, returns DONE.
271 * @return the current point.
274 virtual UChar32
next32PostInc(void);
277 * Returns FALSE if there are no more code units or code points
278 * at or after the current position in the iteration range.
279 * This is used with nextPostInc() or next32PostInc() in forward
281 * @return FALSE if there are no more code units or code points
282 * at or after the current position in the iteration range.
285 virtual UBool
hasNext();
288 * Advances to the previous code unit in the iteration range (toward
289 * startIndex()), and returns that code unit. If there are no more
290 * code units to return, returns DONE.
291 * @return the previous code unit in the iteration range.
294 virtual char16_t previous(void);
297 * Advances to the previous code point in the iteration range (toward
298 * startIndex()), and returns that code point. If there are no more
299 * code points to return, returns DONE.
300 * @return the previous code point in the iteration range.
303 virtual UChar32
previous32(void);
306 * Returns FALSE if there are no more code units or code points
307 * before the current position in the iteration range.
308 * This is used with previous() or previous32() in backward
310 * @return FALSE if there are no more code units or code points
311 * before the current position in the iteration range.
314 virtual UBool
hasPrevious();
317 * Moves the current position relative to the start or end of the
318 * iteration range, or relative to the current position itself.
319 * The movement is expressed in numbers of code units forward
320 * or backward by specifying a positive or negative delta.
321 * @param delta the position relative to origin. A positive delta means forward;
322 * a negative delta means backward.
323 * @param origin Origin enumeration {kStart, kCurrent, kEnd}
324 * @return the new position
327 virtual int32_t move(int32_t delta
, EOrigin origin
);
330 * Moves the current position relative to the start or end of the
331 * iteration range, or relative to the current position itself.
332 * The movement is expressed in numbers of code points forward
333 * or backward by specifying a positive or negative delta.
334 * @param delta the position relative to origin. A positive delta means forward;
335 * a negative delta means backward.
336 * @param origin Origin enumeration {kStart, kCurrent, kEnd}
337 * @return the new position
341 // One of the system headers right now is sometimes defining a conflicting macro we don't use
344 virtual int32_t move32(int32_t delta
, EOrigin origin
);
347 * Sets the iterator to iterate over a new range of text
350 void setText(ConstChar16Ptr newText
, int32_t newTextLength
);
353 * Copies the char16_t array under iteration into the UnicodeString
354 * referred to by "result". Even if this iterator iterates across
355 * only a part of this string, the whole string is copied.
356 * @param result Receives a copy of the text under iteration.
359 virtual void getText(UnicodeString
& result
);
362 * Return a class ID for this class (not really public)
363 * @return a class ID for this class
366 static UClassID U_EXPORT2
getStaticClassID(void);
369 * Return a class ID for this object (not really public)
370 * @return a class ID for this object.
373 virtual UClassID
getDynamicClassID(void) const;
377 * Protected constructor
380 UCharCharacterIterator();
382 * Protected member text
385 const char16_t* text
;
391 #endif /* U_SHOW_CPLUSPLUS_API */