2 **********************************************************************
3 * Copyright (c) 2002-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 #include "unicode/utypes.h"
11 #include "unicode/uobject.h"
12 #include "unicode/unistr.h"
20 * UnicodeSetIterator iterates over the contents of a UnicodeSet. It
21 * iterates over either code points or code point ranges. After all
22 * code points or ranges have been returned, it returns the
23 * multicharacter strings of the UnicodSet, if any.
25 * <p>To iterate over code points, use a loop like this:
27 * UnicodeSetIterator it(set);
28 * while (set.next()) {
29 * if (set.isString()) {
30 * processString(set.getString());
32 * processCodepoint(set.getCodepoint());
37 * <p>To iterate over code point ranges, use a loop like this:
39 * UnicodeSetIterator it(set);
40 * while (it.nextRange()) {
41 * if (it.isString()) {
42 * processString(it.getString());
44 * processCodepointRange(it.getCodepoint(), it.getCodepointEnd());
51 class U_COMMON_API UnicodeSetIterator
: public UObject
{
56 * Value of <tt>codepoint</tt> if the iterator points to a string.
57 * If <tt>codepoint == IS_STRING</tt>, then examine
58 * <tt>string</tt> for the current iteration result.
61 enum { IS_STRING
= -1 };
64 * Current code point, or the special value <tt>IS_STRING</tt>, if
65 * the iterator points to a string.
71 * When iterating over ranges using <tt>nextRange()</tt>,
72 * <tt>codepointEnd</tt> contains the inclusive end of the
73 * iteration range, if <tt>codepoint != IS_STRING</tt>. If
74 * iterating over code points using <tt>next()</tt>, or if
75 * <tt>codepoint == IS_STRING</tt>, then the value of
76 * <tt>codepointEnd</tt> is undefined.
82 * If <tt>codepoint == IS_STRING</tt>, then <tt>string</tt> points
83 * to the current string. If <tt>codepoint != IS_STRING</tt>, the
84 * value of <tt>string</tt> is undefined.
87 const UnicodeString
* string
;
92 * Create an iterator over the given set. The iterator is valid
93 * only so long as <tt>set</tt> is valid.
94 * @param set set to iterate over
97 UnicodeSetIterator(const UnicodeSet
& set
);
100 * Create an iterator over nothing. <tt>next()</tt> and
101 * <tt>nextRange()</tt> return false. This is a convenience
102 * constructor allowing the target to be set later.
105 UnicodeSetIterator();
111 virtual ~UnicodeSetIterator();
114 * Returns true if the current element is a string. If so, the
115 * caller can retrieve it with <tt>getString()</tt>. If this
116 * method returns false, the current element is a code point or
117 * code point range, depending on whether <tt>next()</tt> or
118 * <tt>nextRange()</tt> was called, and the caller can retrieve it
119 * with <tt>getCodepoint()</tt> and, for a range,
120 * <tt>getCodepointEnd()</tt>.
123 inline UBool
isString() const;
126 * Returns the current code point, if <tt>isString()</tt> returned
127 * false. Otherwise returns an undefined result.
130 inline UChar32
getCodepoint() const;
133 * Returns the end of the current code point range, if
134 * <tt>isString()</tt> returned false and <tt>nextRange()</tt> was
135 * called. Otherwise returns an undefined result.
138 inline UChar32
getCodepointEnd() const;
141 * Returns the current string, if <tt>isString()</tt> returned
142 * true. Otherwise returns an undefined result.
145 inline const UnicodeString
& getString() const;
148 * Returns the next element in the set, either a single code point
149 * or a string. If there are no more elements in the set, return
150 * false. If <tt>codepoint == IS_STRING</tt>, the value is a
151 * string in the <tt>string</tt> field. Otherwise the value is a
152 * single code point in the <tt>codepoint</tt> field.
154 * <p>The order of iteration is all code points in sorted order,
155 * followed by all strings sorted order. <tt>codepointEnd</tt> is
156 * undefined after calling this method. <tt>string</tt> is
157 * undefined unless <tt>codepoint == IS_STRING</tt>. Do not mix
158 * calls to <tt>next()</tt> and <tt>nextRange()</tt> without
159 * calling <tt>reset()</tt> between them. The results of doing so
162 * @return true if there was another element in the set and this
163 * object contains the element.
169 * Returns the next element in the set, either a code point range
170 * or a string. If there are no more elements in the set, return
171 * false. If <tt>codepoint == IS_STRING</tt>, the value is a
172 * string in the <tt>string</tt> field. Otherwise the value is a
173 * range of one or more code points from <tt>codepoint</tt> to
174 * <tt>codepointeEnd</tt> inclusive.
176 * <p>The order of iteration is all code points ranges in sorted
177 * order, followed by all strings sorted order. Ranges are
178 * disjoint and non-contiguous. <tt>string</tt> is undefined
179 * unless <tt>codepoint == IS_STRING</tt>. Do not mix calls to
180 * <tt>next()</tt> and <tt>nextRange()</tt> without calling
181 * <tt>reset()</tt> between them. The results of doing so are
184 * @return true if there was another element in the set and this
185 * object contains the element.
191 * Sets this iterator to visit the elements of the given set and
192 * resets it to the start of that set. The iterator is valid only
193 * so long as <tt>set</tt> is valid.
194 * @param set the set to iterate over.
197 void reset(const UnicodeSet
& set
);
200 * Resets this iterator to the start of the set.
206 * ICU "poor man's RTTI", returns a UClassID for this class.
210 static UClassID U_EXPORT2
getStaticClassID();
213 * ICU "poor man's RTTI", returns a UClassID for the actual class.
217 virtual UClassID
getDynamicClassID() const;
219 // ======================= PRIVATES ===========================
223 // endElement and nextElements are really UChar32's, but we keep
224 // them as signed int32_t's so we can do comparisons with
225 // endElement set to -1. Leave them as int32_t's.
229 const UnicodeSet
* set
;
256 /** Copy constructor. Disallowed.
259 UnicodeSetIterator(const UnicodeSetIterator
&); // disallow
261 /** Assignment operator. Disallowed.
264 UnicodeSetIterator
& operator=(const UnicodeSetIterator
&); // disallow
269 virtual void loadRange(int32_t range
);
273 inline UBool
UnicodeSetIterator::isString() const {
274 return codepoint
== (UChar32
)IS_STRING
;
277 inline UChar32
UnicodeSetIterator::getCodepoint() const {
281 inline UChar32
UnicodeSetIterator::getCodepointEnd() const {
285 inline const UnicodeString
& UnicodeSetIterator::getString() const {