1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2002-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
12 #include "unicode/utypes.h"
14 #if U_SHOW_CPLUSPLUS_API
16 #include "unicode/uobject.h"
17 #include "unicode/unistr.h"
21 * \brief C++ API: UnicodeSetIterator iterates over the contents of a UnicodeSet.
31 * UnicodeSetIterator iterates over the contents of a UnicodeSet. It
32 * iterates over either code points or code point ranges. After all
33 * code points or ranges have been returned, it returns the
34 * multicharacter strings of the UnicodeSet, if any.
36 * This class is not intended to be subclassed. Consider any fields
37 * or methods declared as "protected" to be private. The use of
38 * protected in this class is an artifact of history.
40 * <p>To iterate over code points and strings, use a loop like this:
42 * UnicodeSetIterator it(set);
44 * processItem(it.getString());
47 * <p>Each item in the set is accessed as a string. Set elements
48 * consisting of single code points are returned as strings containing
49 * just the one code point.
51 * <p>To iterate over code point ranges, instead of individual code points,
52 * use a loop like this:
54 * UnicodeSetIterator it(set);
55 * while (it.nextRange()) {
56 * if (it.isString()) {
57 * processString(it.getString());
59 * processCodepointRange(it.getCodepoint(), it.getCodepointEnd());
66 class U_COMMON_API UnicodeSetIterator
: public UObject
{
71 * Value of <tt>codepoint</tt> if the iterator points to a string.
72 * If <tt>codepoint == IS_STRING</tt>, then examine
73 * <tt>string</tt> for the current iteration result.
76 enum { IS_STRING
= -1 };
79 * Current code point, or the special value <tt>IS_STRING</tt>, if
80 * the iterator points to a string.
86 * When iterating over ranges using <tt>nextRange()</tt>,
87 * <tt>codepointEnd</tt> contains the inclusive end of the
88 * iteration range, if <tt>codepoint != IS_STRING</tt>. If
89 * iterating over code points using <tt>next()</tt>, or if
90 * <tt>codepoint == IS_STRING</tt>, then the value of
91 * <tt>codepointEnd</tt> is undefined.
97 * If <tt>codepoint == IS_STRING</tt>, then <tt>string</tt> points
98 * to the current string. If <tt>codepoint != IS_STRING</tt>, the
99 * value of <tt>string</tt> is undefined.
102 const UnicodeString
* string
;
107 * Create an iterator over the given set. The iterator is valid
108 * only so long as <tt>set</tt> is valid.
109 * @param set set to iterate over
112 UnicodeSetIterator(const UnicodeSet
& set
);
115 * Create an iterator over nothing. <tt>next()</tt> and
116 * <tt>nextRange()</tt> return false. This is a convenience
117 * constructor allowing the target to be set later.
120 UnicodeSetIterator();
126 virtual ~UnicodeSetIterator();
129 * Returns true if the current element is a string. If so, the
130 * caller can retrieve it with <tt>getString()</tt>. If this
131 * method returns false, the current element is a code point or
132 * code point range, depending on whether <tt>next()</tt> or
133 * <tt>nextRange()</tt> was called.
134 * Elements of types string and codepoint can both be retrieved
135 * with the function <tt>getString()</tt>.
136 * Elements of type codepoint can also be retrieved with
137 * <tt>getCodepoint()</tt>.
138 * For ranges, <tt>getCodepoint()</tt> returns the starting codepoint
139 * of the range, and <tt>getCodepointEnd()</tt> returns the end
143 inline UBool
isString() const;
146 * Returns the current code point, if <tt>isString()</tt> returned
147 * false. Otherwise returns an undefined result.
150 inline UChar32
getCodepoint() const;
153 * Returns the end of the current code point range, if
154 * <tt>isString()</tt> returned false and <tt>nextRange()</tt> was
155 * called. Otherwise returns an undefined result.
158 inline UChar32
getCodepointEnd() const;
161 * Returns the current string, if <tt>isString()</tt> returned
162 * true. If the current iteration item is a code point, a UnicodeString
163 * containing that single code point is returned.
165 * Ownership of the returned string remains with the iterator.
166 * The string is guaranteed to remain valid only until the iterator is
167 * advanced to the next item, or until the iterator is deleted.
171 const UnicodeString
& getString();
174 * Advances the iteration position to the next element in the set,
175 * which can be either a single code point or a string.
176 * If there are no more elements in the set, return false.
179 * If <tt>isString() == TRUE</tt>, the value is a
180 * string, otherwise the value is a
181 * single code point. Elements of either type can be retrieved
182 * with the function <tt>getString()</tt>, while elements of
183 * consisting of a single code point can be retrieved with
184 * <tt>getCodepoint()</tt>
186 * <p>The order of iteration is all code points in sorted order,
187 * followed by all strings sorted order. Do not mix
188 * calls to <tt>next()</tt> and <tt>nextRange()</tt> without
189 * calling <tt>reset()</tt> between them. The results of doing so
192 * @return true if there was another element in the set.
198 * Returns the next element in the set, either a code point range
199 * or a string. If there are no more elements in the set, return
200 * false. If <tt>isString() == TRUE</tt>, the value is a
201 * string and can be accessed with <tt>getString()</tt>. Otherwise the value is a
202 * range of one or more code points from <tt>getCodepoint()</tt> to
203 * <tt>getCodepointeEnd()</tt> inclusive.
205 * <p>The order of iteration is all code points ranges in sorted
206 * order, followed by all strings sorted order. Ranges are
207 * disjoint and non-contiguous. The value returned from <tt>getString()</tt>
208 * is undefined unless <tt>isString() == TRUE</tt>. Do not mix calls to
209 * <tt>next()</tt> and <tt>nextRange()</tt> without calling
210 * <tt>reset()</tt> between them. The results of doing so are
213 * @return true if there was another element in the set.
219 * Sets this iterator to visit the elements of the given set and
220 * resets it to the start of that set. The iterator is valid only
221 * so long as <tt>set</tt> is valid.
222 * @param set the set to iterate over.
225 void reset(const UnicodeSet
& set
);
228 * Resets this iterator to the start of the set.
234 * ICU "poor man's RTTI", returns a UClassID for this class.
238 static UClassID U_EXPORT2
getStaticClassID();
241 * ICU "poor man's RTTI", returns a UClassID for the actual class.
245 virtual UClassID
getDynamicClassID() const;
247 // ======================= PRIVATES ===========================
251 // endElement and nextElements are really UChar32's, but we keep
252 // them as signed int32_t's so we can do comparisons with
253 // endElement set to -1. Leave them as int32_t's.
257 const UnicodeSet
* set
;
285 * Points to the string to use when the caller asks for a
286 * string and the current iteration item is a code point, not a string.
289 UnicodeString
*cpString
;
291 /** Copy constructor. Disallowed.
294 UnicodeSetIterator(const UnicodeSetIterator
&); // disallow
296 /** Assignment operator. Disallowed.
299 UnicodeSetIterator
& operator=(const UnicodeSetIterator
&); // disallow
304 virtual void loadRange(int32_t range
);
308 inline UBool
UnicodeSetIterator::isString() const {
309 return codepoint
== (UChar32
)IS_STRING
;
312 inline UChar32
UnicodeSetIterator::getCodepoint() const {
316 inline UChar32
UnicodeSetIterator::getCodepointEnd() const {
323 #endif /* U_SHOW_CPLUSPLUS_API */