1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker ********************************************************************
5*0e209d39SAndroid Build Coastguard Worker *
6*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 1997-2011, International Business Machines
7*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved.
8*0e209d39SAndroid Build Coastguard Worker *
9*0e209d39SAndroid Build Coastguard Worker ********************************************************************
10*0e209d39SAndroid Build Coastguard Worker */
11*0e209d39SAndroid Build Coastguard Worker
12*0e209d39SAndroid Build Coastguard Worker #ifndef CHARITER_H
13*0e209d39SAndroid Build Coastguard Worker #define CHARITER_H
14*0e209d39SAndroid Build Coastguard Worker
15*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
16*0e209d39SAndroid Build Coastguard Worker
17*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API
18*0e209d39SAndroid Build Coastguard Worker
19*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h"
20*0e209d39SAndroid Build Coastguard Worker #include "unicode/unistr.h"
21*0e209d39SAndroid Build Coastguard Worker /**
22*0e209d39SAndroid Build Coastguard Worker * \file
23*0e209d39SAndroid Build Coastguard Worker * \brief C++ API: Character Iterator
24*0e209d39SAndroid Build Coastguard Worker */
25*0e209d39SAndroid Build Coastguard Worker
26*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
27*0e209d39SAndroid Build Coastguard Worker /**
28*0e209d39SAndroid Build Coastguard Worker * Abstract class that defines an API for forward-only iteration
29*0e209d39SAndroid Build Coastguard Worker * on text objects.
30*0e209d39SAndroid Build Coastguard Worker * This is a minimal interface for iteration without random access
31*0e209d39SAndroid Build Coastguard Worker * or backwards iteration. It is especially useful for wrapping
32*0e209d39SAndroid Build Coastguard Worker * streams with converters into an object for collation or
33*0e209d39SAndroid Build Coastguard Worker * normalization.
34*0e209d39SAndroid Build Coastguard Worker *
35*0e209d39SAndroid Build Coastguard Worker * <p>Characters can be accessed in two ways: as code units or as
36*0e209d39SAndroid Build Coastguard Worker * code points.
37*0e209d39SAndroid Build Coastguard Worker * Unicode code points are 21-bit integers and are the scalar values
38*0e209d39SAndroid Build Coastguard Worker * of Unicode characters. ICU uses the type UChar32 for them.
39*0e209d39SAndroid Build Coastguard Worker * Unicode code units are the storage units of a given
40*0e209d39SAndroid Build Coastguard Worker * Unicode/UCS Transformation Format (a character encoding scheme).
41*0e209d39SAndroid Build Coastguard Worker * With UTF-16, all code points can be represented with either one
42*0e209d39SAndroid Build Coastguard Worker * or two code units ("surrogates").
43*0e209d39SAndroid Build Coastguard Worker * String storage is typically based on code units, while properties
44*0e209d39SAndroid Build Coastguard Worker * of characters are typically determined using code point values.
45*0e209d39SAndroid Build Coastguard Worker * Some processes may be designed to work with sequences of code units,
46*0e209d39SAndroid Build Coastguard Worker * or it may be known that all characters that are important to an
47*0e209d39SAndroid Build Coastguard Worker * algorithm can be represented with single code units.
48*0e209d39SAndroid Build Coastguard Worker * Other processes will need to use the code point access functions.</p>
49*0e209d39SAndroid Build Coastguard Worker *
50*0e209d39SAndroid Build Coastguard Worker * <p>ForwardCharacterIterator provides nextPostInc() to access
51*0e209d39SAndroid Build Coastguard Worker * a code unit and advance an internal position into the text object,
52*0e209d39SAndroid Build Coastguard Worker * similar to a <code>return text[position++]</code>.<br>
53*0e209d39SAndroid Build Coastguard Worker * It provides next32PostInc() to access a code point and advance an internal
54*0e209d39SAndroid Build Coastguard Worker * position.</p>
55*0e209d39SAndroid Build Coastguard Worker *
56*0e209d39SAndroid Build Coastguard Worker * <p>next32PostInc() assumes that the current position is that of
57*0e209d39SAndroid Build Coastguard Worker * the beginning of a code point, i.e., of its first code unit.
58*0e209d39SAndroid Build Coastguard Worker * After next32PostInc(), this will be true again.
59*0e209d39SAndroid Build Coastguard Worker * In general, access to code units and code points in the same
60*0e209d39SAndroid Build Coastguard Worker * iteration loop should not be mixed. In UTF-16, if the current position
61*0e209d39SAndroid Build Coastguard Worker * is on a second code unit (Low Surrogate), then only that code unit
62*0e209d39SAndroid Build Coastguard Worker * is returned even by next32PostInc().</p>
63*0e209d39SAndroid Build Coastguard Worker *
64*0e209d39SAndroid Build Coastguard Worker * <p>For iteration with either function, there are two ways to
65*0e209d39SAndroid Build Coastguard Worker * check for the end of the iteration. When there are no more
66*0e209d39SAndroid Build Coastguard Worker * characters in the text object:
67*0e209d39SAndroid Build Coastguard Worker * <ul>
68*0e209d39SAndroid Build Coastguard Worker * <li>The hasNext() function returns false.</li>
69*0e209d39SAndroid Build Coastguard Worker * <li>nextPostInc() and next32PostInc() return DONE
70*0e209d39SAndroid Build Coastguard Worker * when one attempts to read beyond the end of the text object.</li>
71*0e209d39SAndroid Build Coastguard Worker * </ul>
72*0e209d39SAndroid Build Coastguard Worker *
73*0e209d39SAndroid Build Coastguard Worker * Example:
74*0e209d39SAndroid Build Coastguard Worker * \code
75*0e209d39SAndroid Build Coastguard Worker * void function1(ForwardCharacterIterator &it) {
76*0e209d39SAndroid Build Coastguard Worker * UChar32 c;
77*0e209d39SAndroid Build Coastguard Worker * while(it.hasNext()) {
78*0e209d39SAndroid Build Coastguard Worker * c=it.next32PostInc();
79*0e209d39SAndroid Build Coastguard Worker * // use c
80*0e209d39SAndroid Build Coastguard Worker * }
81*0e209d39SAndroid Build Coastguard Worker * }
82*0e209d39SAndroid Build Coastguard Worker *
83*0e209d39SAndroid Build Coastguard Worker * void function1(ForwardCharacterIterator &it) {
84*0e209d39SAndroid Build Coastguard Worker * char16_t c;
85*0e209d39SAndroid Build Coastguard Worker * while((c=it.nextPostInc())!=ForwardCharacterIterator::DONE) {
86*0e209d39SAndroid Build Coastguard Worker * // use c
87*0e209d39SAndroid Build Coastguard Worker * }
88*0e209d39SAndroid Build Coastguard Worker * }
89*0e209d39SAndroid Build Coastguard Worker * \endcode
90*0e209d39SAndroid Build Coastguard Worker * </p>
91*0e209d39SAndroid Build Coastguard Worker *
92*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
93*0e209d39SAndroid Build Coastguard Worker */
94*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API ForwardCharacterIterator : public UObject {
95*0e209d39SAndroid Build Coastguard Worker public:
96*0e209d39SAndroid Build Coastguard Worker /**
97*0e209d39SAndroid Build Coastguard Worker * Value returned by most of ForwardCharacterIterator's functions
98*0e209d39SAndroid Build Coastguard Worker * when the iterator has reached the limits of its iteration.
99*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
100*0e209d39SAndroid Build Coastguard Worker */
101*0e209d39SAndroid Build Coastguard Worker enum { DONE = 0xffff };
102*0e209d39SAndroid Build Coastguard Worker
103*0e209d39SAndroid Build Coastguard Worker /**
104*0e209d39SAndroid Build Coastguard Worker * Destructor.
105*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
106*0e209d39SAndroid Build Coastguard Worker */
107*0e209d39SAndroid Build Coastguard Worker virtual ~ForwardCharacterIterator();
108*0e209d39SAndroid Build Coastguard Worker
109*0e209d39SAndroid Build Coastguard Worker /**
110*0e209d39SAndroid Build Coastguard Worker * Returns true when both iterators refer to the same
111*0e209d39SAndroid Build Coastguard Worker * character in the same character-storage object.
112*0e209d39SAndroid Build Coastguard Worker * @param that The ForwardCharacterIterator to be compared for equality
113*0e209d39SAndroid Build Coastguard Worker * @return true when both iterators refer to the same
114*0e209d39SAndroid Build Coastguard Worker * character in the same character-storage object
115*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
116*0e209d39SAndroid Build Coastguard Worker */
117*0e209d39SAndroid Build Coastguard Worker virtual bool operator==(const ForwardCharacterIterator& that) const = 0;
118*0e209d39SAndroid Build Coastguard Worker
119*0e209d39SAndroid Build Coastguard Worker /**
120*0e209d39SAndroid Build Coastguard Worker * Returns true when the iterators refer to different
121*0e209d39SAndroid Build Coastguard Worker * text-storage objects, or to different characters in the
122*0e209d39SAndroid Build Coastguard Worker * same text-storage object.
123*0e209d39SAndroid Build Coastguard Worker * @param that The ForwardCharacterIterator to be compared for inequality
124*0e209d39SAndroid Build Coastguard Worker * @return true when the iterators refer to different
125*0e209d39SAndroid Build Coastguard Worker * text-storage objects, or to different characters in the
126*0e209d39SAndroid Build Coastguard Worker * same text-storage object
127*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
128*0e209d39SAndroid Build Coastguard Worker */
129*0e209d39SAndroid Build Coastguard Worker inline bool operator!=(const ForwardCharacterIterator& that) const;
130*0e209d39SAndroid Build Coastguard Worker
131*0e209d39SAndroid Build Coastguard Worker /**
132*0e209d39SAndroid Build Coastguard Worker * Generates a hash code for this iterator.
133*0e209d39SAndroid Build Coastguard Worker * @return the hash code.
134*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
135*0e209d39SAndroid Build Coastguard Worker */
136*0e209d39SAndroid Build Coastguard Worker virtual int32_t hashCode() const = 0;
137*0e209d39SAndroid Build Coastguard Worker
138*0e209d39SAndroid Build Coastguard Worker /**
139*0e209d39SAndroid Build Coastguard Worker * Returns a UClassID for this ForwardCharacterIterator ("poor man's
140*0e209d39SAndroid Build Coastguard Worker * RTTI").<P> Despite the fact that this function is public,
141*0e209d39SAndroid Build Coastguard Worker * DO NOT CONSIDER IT PART OF CHARACTERITERATOR'S API!
142*0e209d39SAndroid Build Coastguard Worker * @return a UClassID for this ForwardCharacterIterator
143*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
144*0e209d39SAndroid Build Coastguard Worker */
145*0e209d39SAndroid Build Coastguard Worker virtual UClassID getDynamicClassID() const override = 0;
146*0e209d39SAndroid Build Coastguard Worker
147*0e209d39SAndroid Build Coastguard Worker /**
148*0e209d39SAndroid Build Coastguard Worker * Gets the current code unit for returning and advances to the next code unit
149*0e209d39SAndroid Build Coastguard Worker * in the iteration range
150*0e209d39SAndroid Build Coastguard Worker * (toward endIndex()). If there are
151*0e209d39SAndroid Build Coastguard Worker * no more code units to return, returns DONE.
152*0e209d39SAndroid Build Coastguard Worker * @return the current code unit.
153*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
154*0e209d39SAndroid Build Coastguard Worker */
155*0e209d39SAndroid Build Coastguard Worker virtual char16_t nextPostInc() = 0;
156*0e209d39SAndroid Build Coastguard Worker
157*0e209d39SAndroid Build Coastguard Worker /**
158*0e209d39SAndroid Build Coastguard Worker * Gets the current code point for returning and advances to the next code point
159*0e209d39SAndroid Build Coastguard Worker * in the iteration range
160*0e209d39SAndroid Build Coastguard Worker * (toward endIndex()). If there are
161*0e209d39SAndroid Build Coastguard Worker * no more code points to return, returns DONE.
162*0e209d39SAndroid Build Coastguard Worker * @return the current code point.
163*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
164*0e209d39SAndroid Build Coastguard Worker */
165*0e209d39SAndroid Build Coastguard Worker virtual UChar32 next32PostInc() = 0;
166*0e209d39SAndroid Build Coastguard Worker
167*0e209d39SAndroid Build Coastguard Worker /**
168*0e209d39SAndroid Build Coastguard Worker * Returns false if there are no more code units or code points
169*0e209d39SAndroid Build Coastguard Worker * at or after the current position in the iteration range.
170*0e209d39SAndroid Build Coastguard Worker * This is used with nextPostInc() or next32PostInc() in forward
171*0e209d39SAndroid Build Coastguard Worker * iteration.
172*0e209d39SAndroid Build Coastguard Worker * @returns false if there are no more code units or code points
173*0e209d39SAndroid Build Coastguard Worker * at or after the current position in the iteration range.
174*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
175*0e209d39SAndroid Build Coastguard Worker */
176*0e209d39SAndroid Build Coastguard Worker virtual UBool hasNext() = 0;
177*0e209d39SAndroid Build Coastguard Worker
178*0e209d39SAndroid Build Coastguard Worker protected:
179*0e209d39SAndroid Build Coastguard Worker /** Default constructor to be overridden in the implementing class. @stable ICU 2.0*/
180*0e209d39SAndroid Build Coastguard Worker ForwardCharacterIterator();
181*0e209d39SAndroid Build Coastguard Worker
182*0e209d39SAndroid Build Coastguard Worker /** Copy constructor to be overridden in the implementing class. @stable ICU 2.0*/
183*0e209d39SAndroid Build Coastguard Worker ForwardCharacterIterator(const ForwardCharacterIterator &other);
184*0e209d39SAndroid Build Coastguard Worker
185*0e209d39SAndroid Build Coastguard Worker /**
186*0e209d39SAndroid Build Coastguard Worker * Assignment operator to be overridden in the implementing class.
187*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
188*0e209d39SAndroid Build Coastguard Worker */
189*0e209d39SAndroid Build Coastguard Worker ForwardCharacterIterator &operator=(const ForwardCharacterIterator&) { return *this; }
190*0e209d39SAndroid Build Coastguard Worker };
191*0e209d39SAndroid Build Coastguard Worker
192*0e209d39SAndroid Build Coastguard Worker /**
193*0e209d39SAndroid Build Coastguard Worker * Abstract class that defines an API for iteration
194*0e209d39SAndroid Build Coastguard Worker * on text objects.
195*0e209d39SAndroid Build Coastguard Worker * This is an interface for forward and backward iteration
196*0e209d39SAndroid Build Coastguard Worker * and random access into a text object.
197*0e209d39SAndroid Build Coastguard Worker *
198*0e209d39SAndroid Build Coastguard Worker * <p>The API provides backward compatibility to the Java and older ICU
199*0e209d39SAndroid Build Coastguard Worker * CharacterIterator classes but extends them significantly:
200*0e209d39SAndroid Build Coastguard Worker * <ol>
201*0e209d39SAndroid Build Coastguard Worker * <li>CharacterIterator is now a subclass of ForwardCharacterIterator.</li>
202*0e209d39SAndroid Build Coastguard Worker * <li>While the old API functions provided forward iteration with
203*0e209d39SAndroid Build Coastguard Worker * "pre-increment" semantics, the new one also provides functions
204*0e209d39SAndroid Build Coastguard Worker * with "post-increment" semantics. They are more efficient and should
205*0e209d39SAndroid Build Coastguard Worker * be the preferred iterator functions for new implementations.
206*0e209d39SAndroid Build Coastguard Worker * The backward iteration always had "pre-decrement" semantics, which
207*0e209d39SAndroid Build Coastguard Worker * are efficient.</li>
208*0e209d39SAndroid Build Coastguard Worker * <li>Just like ForwardCharacterIterator, it provides access to
209*0e209d39SAndroid Build Coastguard Worker * both code units and code points. Code point access versions are available
210*0e209d39SAndroid Build Coastguard Worker * for the old and the new iteration semantics.</li>
211*0e209d39SAndroid Build Coastguard Worker * <li>There are new functions for setting and moving the current position
212*0e209d39SAndroid Build Coastguard Worker * without returning a character, for efficiency.</li>
213*0e209d39SAndroid Build Coastguard Worker * </ol>
214*0e209d39SAndroid Build Coastguard Worker *
215*0e209d39SAndroid Build Coastguard Worker * See ForwardCharacterIterator for examples for using the new forward iteration
216*0e209d39SAndroid Build Coastguard Worker * functions. For backward iteration, there is also a hasPrevious() function
217*0e209d39SAndroid Build Coastguard Worker * that can be used analogously to hasNext().
218*0e209d39SAndroid Build Coastguard Worker * The old functions work as before and are shown below.</p>
219*0e209d39SAndroid Build Coastguard Worker *
220*0e209d39SAndroid Build Coastguard Worker * <p>Examples for some of the new functions:</p>
221*0e209d39SAndroid Build Coastguard Worker *
222*0e209d39SAndroid Build Coastguard Worker * Forward iteration with hasNext():
223*0e209d39SAndroid Build Coastguard Worker * \code
224*0e209d39SAndroid Build Coastguard Worker * void forward1(CharacterIterator &it) {
225*0e209d39SAndroid Build Coastguard Worker * UChar32 c;
226*0e209d39SAndroid Build Coastguard Worker * for(it.setToStart(); it.hasNext();) {
227*0e209d39SAndroid Build Coastguard Worker * c=it.next32PostInc();
228*0e209d39SAndroid Build Coastguard Worker * // use c
229*0e209d39SAndroid Build Coastguard Worker * }
230*0e209d39SAndroid Build Coastguard Worker * }
231*0e209d39SAndroid Build Coastguard Worker * \endcode
232*0e209d39SAndroid Build Coastguard Worker * Forward iteration more similar to loops with the old forward iteration,
233*0e209d39SAndroid Build Coastguard Worker * showing a way to convert simple for() loops:
234*0e209d39SAndroid Build Coastguard Worker * \code
235*0e209d39SAndroid Build Coastguard Worker * void forward2(CharacterIterator &it) {
236*0e209d39SAndroid Build Coastguard Worker * char16_t c;
237*0e209d39SAndroid Build Coastguard Worker * for(c=it.firstPostInc(); c!=CharacterIterator::DONE; c=it.nextPostInc()) {
238*0e209d39SAndroid Build Coastguard Worker * // use c
239*0e209d39SAndroid Build Coastguard Worker * }
240*0e209d39SAndroid Build Coastguard Worker * }
241*0e209d39SAndroid Build Coastguard Worker * \endcode
242*0e209d39SAndroid Build Coastguard Worker * Backward iteration with setToEnd() and hasPrevious():
243*0e209d39SAndroid Build Coastguard Worker * \code
244*0e209d39SAndroid Build Coastguard Worker * void backward1(CharacterIterator &it) {
245*0e209d39SAndroid Build Coastguard Worker * UChar32 c;
246*0e209d39SAndroid Build Coastguard Worker * for(it.setToEnd(); it.hasPrevious();) {
247*0e209d39SAndroid Build Coastguard Worker * c=it.previous32();
248*0e209d39SAndroid Build Coastguard Worker * // use c
249*0e209d39SAndroid Build Coastguard Worker * }
250*0e209d39SAndroid Build Coastguard Worker * }
251*0e209d39SAndroid Build Coastguard Worker * \endcode
252*0e209d39SAndroid Build Coastguard Worker * Backward iteration with a more traditional for() loop:
253*0e209d39SAndroid Build Coastguard Worker * \code
254*0e209d39SAndroid Build Coastguard Worker * void backward2(CharacterIterator &it) {
255*0e209d39SAndroid Build Coastguard Worker * char16_t c;
256*0e209d39SAndroid Build Coastguard Worker * for(c=it.last(); c!=CharacterIterator::DONE; c=it.previous()) {
257*0e209d39SAndroid Build Coastguard Worker * // use c
258*0e209d39SAndroid Build Coastguard Worker * }
259*0e209d39SAndroid Build Coastguard Worker * }
260*0e209d39SAndroid Build Coastguard Worker * \endcode
261*0e209d39SAndroid Build Coastguard Worker *
262*0e209d39SAndroid Build Coastguard Worker * Example for random access:
263*0e209d39SAndroid Build Coastguard Worker * \code
264*0e209d39SAndroid Build Coastguard Worker * void random(CharacterIterator &it) {
265*0e209d39SAndroid Build Coastguard Worker * // set to the third code point from the beginning
266*0e209d39SAndroid Build Coastguard Worker * it.move32(3, CharacterIterator::kStart);
267*0e209d39SAndroid Build Coastguard Worker * // get a code point from here without moving the position
268*0e209d39SAndroid Build Coastguard Worker * UChar32 c=it.current32();
269*0e209d39SAndroid Build Coastguard Worker * // get the position
270*0e209d39SAndroid Build Coastguard Worker * int32_t pos=it.getIndex();
271*0e209d39SAndroid Build Coastguard Worker * // get the previous code unit
272*0e209d39SAndroid Build Coastguard Worker * char16_t u=it.previous();
273*0e209d39SAndroid Build Coastguard Worker * // move back one more code unit
274*0e209d39SAndroid Build Coastguard Worker * it.move(-1, CharacterIterator::kCurrent);
275*0e209d39SAndroid Build Coastguard Worker * // set the position back to where it was
276*0e209d39SAndroid Build Coastguard Worker * // and read the same code point c and move beyond it
277*0e209d39SAndroid Build Coastguard Worker * it.setIndex(pos);
278*0e209d39SAndroid Build Coastguard Worker * if(c!=it.next32PostInc()) {
279*0e209d39SAndroid Build Coastguard Worker * exit(1); // CharacterIterator inconsistent
280*0e209d39SAndroid Build Coastguard Worker * }
281*0e209d39SAndroid Build Coastguard Worker * }
282*0e209d39SAndroid Build Coastguard Worker * \endcode
283*0e209d39SAndroid Build Coastguard Worker *
284*0e209d39SAndroid Build Coastguard Worker * <p>Examples, especially for the old API:</p>
285*0e209d39SAndroid Build Coastguard Worker *
286*0e209d39SAndroid Build Coastguard Worker * Function processing characters, in this example simple output
287*0e209d39SAndroid Build Coastguard Worker * <pre>
288*0e209d39SAndroid Build Coastguard Worker * \code
289*0e209d39SAndroid Build Coastguard Worker * void processChar( char16_t c )
290*0e209d39SAndroid Build Coastguard Worker * {
291*0e209d39SAndroid Build Coastguard Worker * cout << " " << c;
292*0e209d39SAndroid Build Coastguard Worker * }
293*0e209d39SAndroid Build Coastguard Worker * \endcode
294*0e209d39SAndroid Build Coastguard Worker * </pre>
295*0e209d39SAndroid Build Coastguard Worker * Traverse the text from start to finish
296*0e209d39SAndroid Build Coastguard Worker * <pre>
297*0e209d39SAndroid Build Coastguard Worker * \code
298*0e209d39SAndroid Build Coastguard Worker * void traverseForward(CharacterIterator& iter)
299*0e209d39SAndroid Build Coastguard Worker * {
300*0e209d39SAndroid Build Coastguard Worker * for(char16_t c = iter.first(); c != CharacterIterator::DONE; c = iter.next()) {
301*0e209d39SAndroid Build Coastguard Worker * processChar(c);
302*0e209d39SAndroid Build Coastguard Worker * }
303*0e209d39SAndroid Build Coastguard Worker * }
304*0e209d39SAndroid Build Coastguard Worker * \endcode
305*0e209d39SAndroid Build Coastguard Worker * </pre>
306*0e209d39SAndroid Build Coastguard Worker * Traverse the text backwards, from end to start
307*0e209d39SAndroid Build Coastguard Worker * <pre>
308*0e209d39SAndroid Build Coastguard Worker * \code
309*0e209d39SAndroid Build Coastguard Worker * void traverseBackward(CharacterIterator& iter)
310*0e209d39SAndroid Build Coastguard Worker * {
311*0e209d39SAndroid Build Coastguard Worker * for(char16_t c = iter.last(); c != CharacterIterator::DONE; c = iter.previous()) {
312*0e209d39SAndroid Build Coastguard Worker * processChar(c);
313*0e209d39SAndroid Build Coastguard Worker * }
314*0e209d39SAndroid Build Coastguard Worker * }
315*0e209d39SAndroid Build Coastguard Worker * \endcode
316*0e209d39SAndroid Build Coastguard Worker * </pre>
317*0e209d39SAndroid Build Coastguard Worker * Traverse both forward and backward from a given position in the text.
318*0e209d39SAndroid Build Coastguard Worker * Calls to notBoundary() in this example represents some additional stopping criteria.
319*0e209d39SAndroid Build Coastguard Worker * <pre>
320*0e209d39SAndroid Build Coastguard Worker * \code
321*0e209d39SAndroid Build Coastguard Worker * void traverseOut(CharacterIterator& iter, int32_t pos)
322*0e209d39SAndroid Build Coastguard Worker * {
323*0e209d39SAndroid Build Coastguard Worker * char16_t c;
324*0e209d39SAndroid Build Coastguard Worker * for (c = iter.setIndex(pos);
325*0e209d39SAndroid Build Coastguard Worker * c != CharacterIterator::DONE && (Unicode::isLetter(c) || Unicode::isDigit(c));
326*0e209d39SAndroid Build Coastguard Worker * c = iter.next()) {}
327*0e209d39SAndroid Build Coastguard Worker * int32_t end = iter.getIndex();
328*0e209d39SAndroid Build Coastguard Worker * for (c = iter.setIndex(pos);
329*0e209d39SAndroid Build Coastguard Worker * c != CharacterIterator::DONE && (Unicode::isLetter(c) || Unicode::isDigit(c));
330*0e209d39SAndroid Build Coastguard Worker * c = iter.previous()) {}
331*0e209d39SAndroid Build Coastguard Worker * int32_t start = iter.getIndex() + 1;
332*0e209d39SAndroid Build Coastguard Worker *
333*0e209d39SAndroid Build Coastguard Worker * cout << "start: " << start << " end: " << end << endl;
334*0e209d39SAndroid Build Coastguard Worker * for (c = iter.setIndex(start); iter.getIndex() < end; c = iter.next() ) {
335*0e209d39SAndroid Build Coastguard Worker * processChar(c);
336*0e209d39SAndroid Build Coastguard Worker * }
337*0e209d39SAndroid Build Coastguard Worker * }
338*0e209d39SAndroid Build Coastguard Worker * \endcode
339*0e209d39SAndroid Build Coastguard Worker * </pre>
340*0e209d39SAndroid Build Coastguard Worker * Creating a StringCharacterIterator and calling the test functions
341*0e209d39SAndroid Build Coastguard Worker * <pre>
342*0e209d39SAndroid Build Coastguard Worker * \code
343*0e209d39SAndroid Build Coastguard Worker * void CharacterIterator_Example( void )
344*0e209d39SAndroid Build Coastguard Worker * {
345*0e209d39SAndroid Build Coastguard Worker * cout << endl << "===== CharacterIterator_Example: =====" << endl;
346*0e209d39SAndroid Build Coastguard Worker * UnicodeString text("Ein kleiner Satz.");
347*0e209d39SAndroid Build Coastguard Worker * StringCharacterIterator iterator(text);
348*0e209d39SAndroid Build Coastguard Worker * cout << "----- traverseForward: -----------" << endl;
349*0e209d39SAndroid Build Coastguard Worker * traverseForward( iterator );
350*0e209d39SAndroid Build Coastguard Worker * cout << endl << endl << "----- traverseBackward: ----------" << endl;
351*0e209d39SAndroid Build Coastguard Worker * traverseBackward( iterator );
352*0e209d39SAndroid Build Coastguard Worker * cout << endl << endl << "----- traverseOut: ---------------" << endl;
353*0e209d39SAndroid Build Coastguard Worker * traverseOut( iterator, 7 );
354*0e209d39SAndroid Build Coastguard Worker * cout << endl << endl << "-----" << endl;
355*0e209d39SAndroid Build Coastguard Worker * }
356*0e209d39SAndroid Build Coastguard Worker * \endcode
357*0e209d39SAndroid Build Coastguard Worker * </pre>
358*0e209d39SAndroid Build Coastguard Worker *
359*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
360*0e209d39SAndroid Build Coastguard Worker */
361*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API CharacterIterator : public ForwardCharacterIterator {
362*0e209d39SAndroid Build Coastguard Worker public:
363*0e209d39SAndroid Build Coastguard Worker /**
364*0e209d39SAndroid Build Coastguard Worker * Origin enumeration for the move() and move32() functions.
365*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
366*0e209d39SAndroid Build Coastguard Worker */
367*0e209d39SAndroid Build Coastguard Worker enum EOrigin { kStart, kCurrent, kEnd };
368*0e209d39SAndroid Build Coastguard Worker
369*0e209d39SAndroid Build Coastguard Worker /**
370*0e209d39SAndroid Build Coastguard Worker * Destructor.
371*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
372*0e209d39SAndroid Build Coastguard Worker */
373*0e209d39SAndroid Build Coastguard Worker virtual ~CharacterIterator();
374*0e209d39SAndroid Build Coastguard Worker
375*0e209d39SAndroid Build Coastguard Worker /**
376*0e209d39SAndroid Build Coastguard Worker * Returns a pointer to a new CharacterIterator of the same
377*0e209d39SAndroid Build Coastguard Worker * concrete class as this one, and referring to the same
378*0e209d39SAndroid Build Coastguard Worker * character in the same text-storage object as this one. The
379*0e209d39SAndroid Build Coastguard Worker * caller is responsible for deleting the new clone.
380*0e209d39SAndroid Build Coastguard Worker * @return a pointer to a new CharacterIterator
381*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
382*0e209d39SAndroid Build Coastguard Worker */
383*0e209d39SAndroid Build Coastguard Worker virtual CharacterIterator* clone() const = 0;
384*0e209d39SAndroid Build Coastguard Worker
385*0e209d39SAndroid Build Coastguard Worker /**
386*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the first code unit in its
387*0e209d39SAndroid Build Coastguard Worker * iteration range, and returns that code unit.
388*0e209d39SAndroid Build Coastguard Worker * This can be used to begin an iteration with next().
389*0e209d39SAndroid Build Coastguard Worker * @return the first code unit in its iteration range.
390*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
391*0e209d39SAndroid Build Coastguard Worker */
392*0e209d39SAndroid Build Coastguard Worker virtual char16_t first() = 0;
393*0e209d39SAndroid Build Coastguard Worker
394*0e209d39SAndroid Build Coastguard Worker /**
395*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the first code unit in its
396*0e209d39SAndroid Build Coastguard Worker * iteration range, returns that code unit, and moves the position
397*0e209d39SAndroid Build Coastguard Worker * to the second code unit. This is an alternative to setToStart()
398*0e209d39SAndroid Build Coastguard Worker * for forward iteration with nextPostInc().
399*0e209d39SAndroid Build Coastguard Worker * @return the first code unit in its iteration range.
400*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
401*0e209d39SAndroid Build Coastguard Worker */
402*0e209d39SAndroid Build Coastguard Worker virtual char16_t firstPostInc();
403*0e209d39SAndroid Build Coastguard Worker
404*0e209d39SAndroid Build Coastguard Worker /**
405*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the first code point in its
406*0e209d39SAndroid Build Coastguard Worker * iteration range, and returns that code unit,
407*0e209d39SAndroid Build Coastguard Worker * This can be used to begin an iteration with next32().
408*0e209d39SAndroid Build Coastguard Worker * Note that an iteration with next32PostInc(), beginning with,
409*0e209d39SAndroid Build Coastguard Worker * e.g., setToStart() or firstPostInc(), is more efficient.
410*0e209d39SAndroid Build Coastguard Worker * @return the first code point in its iteration range.
411*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
412*0e209d39SAndroid Build Coastguard Worker */
413*0e209d39SAndroid Build Coastguard Worker virtual UChar32 first32() = 0;
414*0e209d39SAndroid Build Coastguard Worker
415*0e209d39SAndroid Build Coastguard Worker /**
416*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the first code point in its
417*0e209d39SAndroid Build Coastguard Worker * iteration range, returns that code point, and moves the position
418*0e209d39SAndroid Build Coastguard Worker * to the second code point. This is an alternative to setToStart()
419*0e209d39SAndroid Build Coastguard Worker * for forward iteration with next32PostInc().
420*0e209d39SAndroid Build Coastguard Worker * @return the first code point in its iteration range.
421*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
422*0e209d39SAndroid Build Coastguard Worker */
423*0e209d39SAndroid Build Coastguard Worker virtual UChar32 first32PostInc();
424*0e209d39SAndroid Build Coastguard Worker
425*0e209d39SAndroid Build Coastguard Worker /**
426*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the first code unit or code point in its
427*0e209d39SAndroid Build Coastguard Worker * iteration range. This can be used to begin a forward
428*0e209d39SAndroid Build Coastguard Worker * iteration with nextPostInc() or next32PostInc().
429*0e209d39SAndroid Build Coastguard Worker * @return the start position of the iteration range
430*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
431*0e209d39SAndroid Build Coastguard Worker */
432*0e209d39SAndroid Build Coastguard Worker inline int32_t setToStart();
433*0e209d39SAndroid Build Coastguard Worker
434*0e209d39SAndroid Build Coastguard Worker /**
435*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the last code unit in its
436*0e209d39SAndroid Build Coastguard Worker * iteration range, and returns that code unit.
437*0e209d39SAndroid Build Coastguard Worker * This can be used to begin an iteration with previous().
438*0e209d39SAndroid Build Coastguard Worker * @return the last code unit.
439*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
440*0e209d39SAndroid Build Coastguard Worker */
441*0e209d39SAndroid Build Coastguard Worker virtual char16_t last() = 0;
442*0e209d39SAndroid Build Coastguard Worker
443*0e209d39SAndroid Build Coastguard Worker /**
444*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the last code point in its
445*0e209d39SAndroid Build Coastguard Worker * iteration range, and returns that code unit.
446*0e209d39SAndroid Build Coastguard Worker * This can be used to begin an iteration with previous32().
447*0e209d39SAndroid Build Coastguard Worker * @return the last code point.
448*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
449*0e209d39SAndroid Build Coastguard Worker */
450*0e209d39SAndroid Build Coastguard Worker virtual UChar32 last32() = 0;
451*0e209d39SAndroid Build Coastguard Worker
452*0e209d39SAndroid Build Coastguard Worker /**
453*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to the end of its iteration range, just behind
454*0e209d39SAndroid Build Coastguard Worker * the last code unit or code point. This can be used to begin a backward
455*0e209d39SAndroid Build Coastguard Worker * iteration with previous() or previous32().
456*0e209d39SAndroid Build Coastguard Worker * @return the end position of the iteration range
457*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
458*0e209d39SAndroid Build Coastguard Worker */
459*0e209d39SAndroid Build Coastguard Worker inline int32_t setToEnd();
460*0e209d39SAndroid Build Coastguard Worker
461*0e209d39SAndroid Build Coastguard Worker /**
462*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the "position"-th code unit
463*0e209d39SAndroid Build Coastguard Worker * in the text-storage object the iterator refers to, and
464*0e209d39SAndroid Build Coastguard Worker * returns that code unit.
465*0e209d39SAndroid Build Coastguard Worker * @param position the "position"-th code unit in the text-storage object
466*0e209d39SAndroid Build Coastguard Worker * @return the "position"-th code unit.
467*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
468*0e209d39SAndroid Build Coastguard Worker */
469*0e209d39SAndroid Build Coastguard Worker virtual char16_t setIndex(int32_t position) = 0;
470*0e209d39SAndroid Build Coastguard Worker
471*0e209d39SAndroid Build Coastguard Worker /**
472*0e209d39SAndroid Build Coastguard Worker * Sets the iterator to refer to the beginning of the code point
473*0e209d39SAndroid Build Coastguard Worker * that contains the "position"-th code unit
474*0e209d39SAndroid Build Coastguard Worker * in the text-storage object the iterator refers to, and
475*0e209d39SAndroid Build Coastguard Worker * returns that code point.
476*0e209d39SAndroid Build Coastguard Worker * The current position is adjusted to the beginning of the code point
477*0e209d39SAndroid Build Coastguard Worker * (its first code unit).
478*0e209d39SAndroid Build Coastguard Worker * @param position the "position"-th code unit in the text-storage object
479*0e209d39SAndroid Build Coastguard Worker * @return the "position"-th code point.
480*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
481*0e209d39SAndroid Build Coastguard Worker */
482*0e209d39SAndroid Build Coastguard Worker virtual UChar32 setIndex32(int32_t position) = 0;
483*0e209d39SAndroid Build Coastguard Worker
484*0e209d39SAndroid Build Coastguard Worker /**
485*0e209d39SAndroid Build Coastguard Worker * Returns the code unit the iterator currently refers to.
486*0e209d39SAndroid Build Coastguard Worker * @return the current code unit.
487*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
488*0e209d39SAndroid Build Coastguard Worker */
489*0e209d39SAndroid Build Coastguard Worker virtual char16_t current() const = 0;
490*0e209d39SAndroid Build Coastguard Worker
491*0e209d39SAndroid Build Coastguard Worker /**
492*0e209d39SAndroid Build Coastguard Worker * Returns the code point the iterator currently refers to.
493*0e209d39SAndroid Build Coastguard Worker * @return the current code point.
494*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
495*0e209d39SAndroid Build Coastguard Worker */
496*0e209d39SAndroid Build Coastguard Worker virtual UChar32 current32() const = 0;
497*0e209d39SAndroid Build Coastguard Worker
498*0e209d39SAndroid Build Coastguard Worker /**
499*0e209d39SAndroid Build Coastguard Worker * Advances to the next code unit in the iteration range
500*0e209d39SAndroid Build Coastguard Worker * (toward endIndex()), and returns that code unit. If there are
501*0e209d39SAndroid Build Coastguard Worker * no more code units to return, returns DONE.
502*0e209d39SAndroid Build Coastguard Worker * @return the next code unit.
503*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
504*0e209d39SAndroid Build Coastguard Worker */
505*0e209d39SAndroid Build Coastguard Worker virtual char16_t next() = 0;
506*0e209d39SAndroid Build Coastguard Worker
507*0e209d39SAndroid Build Coastguard Worker /**
508*0e209d39SAndroid Build Coastguard Worker * Advances to the next code point in the iteration range
509*0e209d39SAndroid Build Coastguard Worker * (toward endIndex()), and returns that code point. If there are
510*0e209d39SAndroid Build Coastguard Worker * no more code points to return, returns DONE.
511*0e209d39SAndroid Build Coastguard Worker * Note that iteration with "pre-increment" semantics is less
512*0e209d39SAndroid Build Coastguard Worker * efficient than iteration with "post-increment" semantics
513*0e209d39SAndroid Build Coastguard Worker * that is provided by next32PostInc().
514*0e209d39SAndroid Build Coastguard Worker * @return the next code point.
515*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
516*0e209d39SAndroid Build Coastguard Worker */
517*0e209d39SAndroid Build Coastguard Worker virtual UChar32 next32() = 0;
518*0e209d39SAndroid Build Coastguard Worker
519*0e209d39SAndroid Build Coastguard Worker /**
520*0e209d39SAndroid Build Coastguard Worker * Advances to the previous code unit in the iteration range
521*0e209d39SAndroid Build Coastguard Worker * (toward startIndex()), and returns that code unit. If there are
522*0e209d39SAndroid Build Coastguard Worker * no more code units to return, returns DONE.
523*0e209d39SAndroid Build Coastguard Worker * @return the previous code unit.
524*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
525*0e209d39SAndroid Build Coastguard Worker */
526*0e209d39SAndroid Build Coastguard Worker virtual char16_t previous() = 0;
527*0e209d39SAndroid Build Coastguard Worker
528*0e209d39SAndroid Build Coastguard Worker /**
529*0e209d39SAndroid Build Coastguard Worker * Advances to the previous code point in the iteration range
530*0e209d39SAndroid Build Coastguard Worker * (toward startIndex()), and returns that code point. If there are
531*0e209d39SAndroid Build Coastguard Worker * no more code points to return, returns DONE.
532*0e209d39SAndroid Build Coastguard Worker * @return the previous code point.
533*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
534*0e209d39SAndroid Build Coastguard Worker */
535*0e209d39SAndroid Build Coastguard Worker virtual UChar32 previous32() = 0;
536*0e209d39SAndroid Build Coastguard Worker
537*0e209d39SAndroid Build Coastguard Worker /**
538*0e209d39SAndroid Build Coastguard Worker * Returns false if there are no more code units or code points
539*0e209d39SAndroid Build Coastguard Worker * before the current position in the iteration range.
540*0e209d39SAndroid Build Coastguard Worker * This is used with previous() or previous32() in backward
541*0e209d39SAndroid Build Coastguard Worker * iteration.
542*0e209d39SAndroid Build Coastguard Worker * @return false if there are no more code units or code points
543*0e209d39SAndroid Build Coastguard Worker * before the current position in the iteration range, return true otherwise.
544*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
545*0e209d39SAndroid Build Coastguard Worker */
546*0e209d39SAndroid Build Coastguard Worker virtual UBool hasPrevious() = 0;
547*0e209d39SAndroid Build Coastguard Worker
548*0e209d39SAndroid Build Coastguard Worker /**
549*0e209d39SAndroid Build Coastguard Worker * Returns the numeric index in the underlying text-storage
550*0e209d39SAndroid Build Coastguard Worker * object of the character returned by first(). Since it's
551*0e209d39SAndroid Build Coastguard Worker * possible to create an iterator that iterates across only
552*0e209d39SAndroid Build Coastguard Worker * part of a text-storage object, this number isn't
553*0e209d39SAndroid Build Coastguard Worker * necessarily 0.
554*0e209d39SAndroid Build Coastguard Worker * @returns the numeric index in the underlying text-storage
555*0e209d39SAndroid Build Coastguard Worker * object of the character returned by first().
556*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
557*0e209d39SAndroid Build Coastguard Worker */
558*0e209d39SAndroid Build Coastguard Worker inline int32_t startIndex() const;
559*0e209d39SAndroid Build Coastguard Worker
560*0e209d39SAndroid Build Coastguard Worker /**
561*0e209d39SAndroid Build Coastguard Worker * Returns the numeric index in the underlying text-storage
562*0e209d39SAndroid Build Coastguard Worker * object of the position immediately BEYOND the character
563*0e209d39SAndroid Build Coastguard Worker * returned by last().
564*0e209d39SAndroid Build Coastguard Worker * @return the numeric index in the underlying text-storage
565*0e209d39SAndroid Build Coastguard Worker * object of the position immediately BEYOND the character
566*0e209d39SAndroid Build Coastguard Worker * returned by last().
567*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
568*0e209d39SAndroid Build Coastguard Worker */
569*0e209d39SAndroid Build Coastguard Worker inline int32_t endIndex() const;
570*0e209d39SAndroid Build Coastguard Worker
571*0e209d39SAndroid Build Coastguard Worker /**
572*0e209d39SAndroid Build Coastguard Worker * Returns the numeric index in the underlying text-storage
573*0e209d39SAndroid Build Coastguard Worker * object of the character the iterator currently refers to
574*0e209d39SAndroid Build Coastguard Worker * (i.e., the character returned by current()).
575*0e209d39SAndroid Build Coastguard Worker * @return the numeric index in the text-storage object of
576*0e209d39SAndroid Build Coastguard Worker * the character the iterator currently refers to
577*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
578*0e209d39SAndroid Build Coastguard Worker */
579*0e209d39SAndroid Build Coastguard Worker inline int32_t getIndex() const;
580*0e209d39SAndroid Build Coastguard Worker
581*0e209d39SAndroid Build Coastguard Worker /**
582*0e209d39SAndroid Build Coastguard Worker * Returns the length of the entire text in the underlying
583*0e209d39SAndroid Build Coastguard Worker * text-storage object.
584*0e209d39SAndroid Build Coastguard Worker * @return the length of the entire text in the text-storage object
585*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
586*0e209d39SAndroid Build Coastguard Worker */
587*0e209d39SAndroid Build Coastguard Worker inline int32_t getLength() const;
588*0e209d39SAndroid Build Coastguard Worker
589*0e209d39SAndroid Build Coastguard Worker /**
590*0e209d39SAndroid Build Coastguard Worker * Moves the current position relative to the start or end of the
591*0e209d39SAndroid Build Coastguard Worker * iteration range, or relative to the current position itself.
592*0e209d39SAndroid Build Coastguard Worker * The movement is expressed in numbers of code units forward
593*0e209d39SAndroid Build Coastguard Worker * or backward by specifying a positive or negative delta.
594*0e209d39SAndroid Build Coastguard Worker * @param delta the position relative to origin. A positive delta means forward;
595*0e209d39SAndroid Build Coastguard Worker * a negative delta means backward.
596*0e209d39SAndroid Build Coastguard Worker * @param origin Origin enumeration {kStart, kCurrent, kEnd}
597*0e209d39SAndroid Build Coastguard Worker * @return the new position
598*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
599*0e209d39SAndroid Build Coastguard Worker */
600*0e209d39SAndroid Build Coastguard Worker virtual int32_t move(int32_t delta, EOrigin origin) = 0;
601*0e209d39SAndroid Build Coastguard Worker
602*0e209d39SAndroid Build Coastguard Worker /**
603*0e209d39SAndroid Build Coastguard Worker * Moves the current position relative to the start or end of the
604*0e209d39SAndroid Build Coastguard Worker * iteration range, or relative to the current position itself.
605*0e209d39SAndroid Build Coastguard Worker * The movement is expressed in numbers of code points forward
606*0e209d39SAndroid Build Coastguard Worker * or backward by specifying a positive or negative delta.
607*0e209d39SAndroid Build Coastguard Worker * @param delta the position relative to origin. A positive delta means forward;
608*0e209d39SAndroid Build Coastguard Worker * a negative delta means backward.
609*0e209d39SAndroid Build Coastguard Worker * @param origin Origin enumeration {kStart, kCurrent, kEnd}
610*0e209d39SAndroid Build Coastguard Worker * @return the new position
611*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
612*0e209d39SAndroid Build Coastguard Worker */
613*0e209d39SAndroid Build Coastguard Worker #ifdef move32
614*0e209d39SAndroid Build Coastguard Worker // One of the system headers right now is sometimes defining a conflicting macro we don't use
615*0e209d39SAndroid Build Coastguard Worker #undef move32
616*0e209d39SAndroid Build Coastguard Worker #endif
617*0e209d39SAndroid Build Coastguard Worker virtual int32_t move32(int32_t delta, EOrigin origin) = 0;
618*0e209d39SAndroid Build Coastguard Worker
619*0e209d39SAndroid Build Coastguard Worker /**
620*0e209d39SAndroid Build Coastguard Worker * Copies the text under iteration into the UnicodeString
621*0e209d39SAndroid Build Coastguard Worker * referred to by "result".
622*0e209d39SAndroid Build Coastguard Worker * @param result Receives a copy of the text under iteration.
623*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
624*0e209d39SAndroid Build Coastguard Worker */
625*0e209d39SAndroid Build Coastguard Worker virtual void getText(UnicodeString& result) = 0;
626*0e209d39SAndroid Build Coastguard Worker
627*0e209d39SAndroid Build Coastguard Worker protected:
628*0e209d39SAndroid Build Coastguard Worker /**
629*0e209d39SAndroid Build Coastguard Worker * Empty constructor.
630*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
631*0e209d39SAndroid Build Coastguard Worker */
632*0e209d39SAndroid Build Coastguard Worker CharacterIterator();
633*0e209d39SAndroid Build Coastguard Worker
634*0e209d39SAndroid Build Coastguard Worker /**
635*0e209d39SAndroid Build Coastguard Worker * Constructor, just setting the length field in this base class.
636*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
637*0e209d39SAndroid Build Coastguard Worker */
638*0e209d39SAndroid Build Coastguard Worker CharacterIterator(int32_t length);
639*0e209d39SAndroid Build Coastguard Worker
640*0e209d39SAndroid Build Coastguard Worker /**
641*0e209d39SAndroid Build Coastguard Worker * Constructor, just setting the length and position fields in this base class.
642*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
643*0e209d39SAndroid Build Coastguard Worker */
644*0e209d39SAndroid Build Coastguard Worker CharacterIterator(int32_t length, int32_t position);
645*0e209d39SAndroid Build Coastguard Worker
646*0e209d39SAndroid Build Coastguard Worker /**
647*0e209d39SAndroid Build Coastguard Worker * Constructor, just setting the length, start, end, and position fields in this base class.
648*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
649*0e209d39SAndroid Build Coastguard Worker */
650*0e209d39SAndroid Build Coastguard Worker CharacterIterator(int32_t length, int32_t textBegin, int32_t textEnd, int32_t position);
651*0e209d39SAndroid Build Coastguard Worker
652*0e209d39SAndroid Build Coastguard Worker /**
653*0e209d39SAndroid Build Coastguard Worker * Copy constructor.
654*0e209d39SAndroid Build Coastguard Worker *
655*0e209d39SAndroid Build Coastguard Worker * @param that The CharacterIterator to be copied
656*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
657*0e209d39SAndroid Build Coastguard Worker */
658*0e209d39SAndroid Build Coastguard Worker CharacterIterator(const CharacterIterator &that);
659*0e209d39SAndroid Build Coastguard Worker
660*0e209d39SAndroid Build Coastguard Worker /**
661*0e209d39SAndroid Build Coastguard Worker * Assignment operator. Sets this CharacterIterator to have the same behavior,
662*0e209d39SAndroid Build Coastguard Worker * as the one passed in.
663*0e209d39SAndroid Build Coastguard Worker * @param that The CharacterIterator passed in.
664*0e209d39SAndroid Build Coastguard Worker * @return the newly set CharacterIterator.
665*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
666*0e209d39SAndroid Build Coastguard Worker */
667*0e209d39SAndroid Build Coastguard Worker CharacterIterator &operator=(const CharacterIterator &that);
668*0e209d39SAndroid Build Coastguard Worker
669*0e209d39SAndroid Build Coastguard Worker /**
670*0e209d39SAndroid Build Coastguard Worker * Base class text length field.
671*0e209d39SAndroid Build Coastguard Worker * Necessary this for correct getText() and hashCode().
672*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
673*0e209d39SAndroid Build Coastguard Worker */
674*0e209d39SAndroid Build Coastguard Worker int32_t textLength;
675*0e209d39SAndroid Build Coastguard Worker
676*0e209d39SAndroid Build Coastguard Worker /**
677*0e209d39SAndroid Build Coastguard Worker * Base class field for the current position.
678*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
679*0e209d39SAndroid Build Coastguard Worker */
680*0e209d39SAndroid Build Coastguard Worker int32_t pos;
681*0e209d39SAndroid Build Coastguard Worker
682*0e209d39SAndroid Build Coastguard Worker /**
683*0e209d39SAndroid Build Coastguard Worker * Base class field for the start of the iteration range.
684*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
685*0e209d39SAndroid Build Coastguard Worker */
686*0e209d39SAndroid Build Coastguard Worker int32_t begin;
687*0e209d39SAndroid Build Coastguard Worker
688*0e209d39SAndroid Build Coastguard Worker /**
689*0e209d39SAndroid Build Coastguard Worker * Base class field for the end of the iteration range.
690*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0
691*0e209d39SAndroid Build Coastguard Worker */
692*0e209d39SAndroid Build Coastguard Worker int32_t end;
693*0e209d39SAndroid Build Coastguard Worker };
694*0e209d39SAndroid Build Coastguard Worker
695*0e209d39SAndroid Build Coastguard Worker inline bool
696*0e209d39SAndroid Build Coastguard Worker ForwardCharacterIterator::operator!=(const ForwardCharacterIterator& that) const {
697*0e209d39SAndroid Build Coastguard Worker return !operator==(that);
698*0e209d39SAndroid Build Coastguard Worker }
699*0e209d39SAndroid Build Coastguard Worker
700*0e209d39SAndroid Build Coastguard Worker inline int32_t
setToStart()701*0e209d39SAndroid Build Coastguard Worker CharacterIterator::setToStart() {
702*0e209d39SAndroid Build Coastguard Worker return move(0, kStart);
703*0e209d39SAndroid Build Coastguard Worker }
704*0e209d39SAndroid Build Coastguard Worker
705*0e209d39SAndroid Build Coastguard Worker inline int32_t
setToEnd()706*0e209d39SAndroid Build Coastguard Worker CharacterIterator::setToEnd() {
707*0e209d39SAndroid Build Coastguard Worker return move(0, kEnd);
708*0e209d39SAndroid Build Coastguard Worker }
709*0e209d39SAndroid Build Coastguard Worker
710*0e209d39SAndroid Build Coastguard Worker inline int32_t
startIndex()711*0e209d39SAndroid Build Coastguard Worker CharacterIterator::startIndex() const {
712*0e209d39SAndroid Build Coastguard Worker return begin;
713*0e209d39SAndroid Build Coastguard Worker }
714*0e209d39SAndroid Build Coastguard Worker
715*0e209d39SAndroid Build Coastguard Worker inline int32_t
endIndex()716*0e209d39SAndroid Build Coastguard Worker CharacterIterator::endIndex() const {
717*0e209d39SAndroid Build Coastguard Worker return end;
718*0e209d39SAndroid Build Coastguard Worker }
719*0e209d39SAndroid Build Coastguard Worker
720*0e209d39SAndroid Build Coastguard Worker inline int32_t
getIndex()721*0e209d39SAndroid Build Coastguard Worker CharacterIterator::getIndex() const {
722*0e209d39SAndroid Build Coastguard Worker return pos;
723*0e209d39SAndroid Build Coastguard Worker }
724*0e209d39SAndroid Build Coastguard Worker
725*0e209d39SAndroid Build Coastguard Worker inline int32_t
getLength()726*0e209d39SAndroid Build Coastguard Worker CharacterIterator::getLength() const {
727*0e209d39SAndroid Build Coastguard Worker return textLength;
728*0e209d39SAndroid Build Coastguard Worker }
729*0e209d39SAndroid Build Coastguard Worker
730*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
731*0e209d39SAndroid Build Coastguard Worker
732*0e209d39SAndroid Build Coastguard Worker #endif /* U_SHOW_CPLUSPLUS_API */
733*0e209d39SAndroid Build Coastguard Worker
734*0e209d39SAndroid Build Coastguard Worker #endif
735