Update contrib.
2 **********************************************************************
3 * Copyright (c) 2002-2005, 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"
16 * \brief C++ API: UnicodeSetIterator iterates over the contents of a UnicodeSet.
26 * UnicodeSetIterator iterates over the contents of a UnicodeSet. It
27 * iterates over either code points or code point ranges. After all
28 * code points or ranges have been returned, it returns the
29 * multicharacter strings of the UnicodSet, if any.
31 * <p>To iterate over code points, use a loop like this:
33 * UnicodeSetIterator it(set);
34 * while (set.next()) {
35 * if (set.isString()) {
36 * processString(set.getString());
38 * processCodepoint(set.getCodepoint());
43 * <p>To iterate over code point ranges, use a loop like this:
45 * UnicodeSetIterator it(set);
46 * while (it.nextRange()) {
47 * if (it.isString()) {
48 * processString(it.getString());
50 * processCodepointRange(it.getCodepoint(), it.getCodepointEnd());
57 class U_COMMON_API UnicodeSetIterator : public UObject {
62 * Value of <tt>codepoint</tt> if the iterator points to a string.
63 * If <tt>codepoint == IS_STRING</tt>, then examine
64 * <tt>string</tt> for the current iteration result.
67 enum { IS_STRING = -1 };
70 * Current code point, or the special value <tt>IS_STRING</tt>, if
71 * the iterator points to a string.
77 * When iterating over ranges using <tt>nextRange()</tt>,
78 * <tt>codepointEnd</tt> contains the inclusive end of the
79 * iteration range, if <tt>codepoint != IS_STRING</tt>. If
80 * iterating over code points using <tt>next()</tt>, or if
81 * <tt>codepoint == IS_STRING</tt>, then the value of
82 * <tt>codepointEnd</tt> is undefined.
88 * If <tt>codepoint == IS_STRING</tt>, then <tt>string</tt> points
89 * to the current string. If <tt>codepoint != IS_STRING</tt>, the
90 * value of <tt>string</tt> is undefined.
93 const UnicodeString* string;
98 * Create an iterator over the given set. The iterator is valid
99 * only so long as <tt>set</tt> is valid.
100 * @param set set to iterate over
103 UnicodeSetIterator(const UnicodeSet& set);
106 * Create an iterator over nothing. <tt>next()</tt> and
107 * <tt>nextRange()</tt> return false. This is a convenience
108 * constructor allowing the target to be set later.
111 UnicodeSetIterator();
117 virtual ~UnicodeSetIterator();
120 * Returns true if the current element is a string. If so, the
121 * caller can retrieve it with <tt>getString()</tt>. If this
122 * method returns false, the current element is a code point or
123 * code point range, depending on whether <tt>next()</tt> or
124 * <tt>nextRange()</tt> was called, and the caller can retrieve it
125 * with <tt>getCodepoint()</tt> and, for a range,
126 * <tt>getCodepointEnd()</tt>.
129 inline UBool isString() const;
132 * Returns the current code point, if <tt>isString()</tt> returned
133 * false. Otherwise returns an undefined result.
136 inline UChar32 getCodepoint() const;
139 * Returns the end of the current code point range, if
140 * <tt>isString()</tt> returned false and <tt>nextRange()</tt> was
141 * called. Otherwise returns an undefined result.
144 inline UChar32 getCodepointEnd() const;
147 * Returns the current string, if <tt>isString()</tt> returned
148 * true. Otherwise returns an undefined result.
151 inline const UnicodeString& getString() const;
154 * Returns the next element in the set, either a single code point
155 * or a string. If there are no more elements in the set, return
156 * false. If <tt>codepoint == IS_STRING</tt>, the value is a
157 * string in the <tt>string</tt> field. Otherwise the value is a
158 * single code point in the <tt>codepoint</tt> field.
160 * <p>The order of iteration is all code points in sorted order,
161 * followed by all strings sorted order. <tt>codepointEnd</tt> is
162 * undefined after calling this method. <tt>string</tt> is
163 * undefined unless <tt>codepoint == IS_STRING</tt>. Do not mix
164 * calls to <tt>next()</tt> and <tt>nextRange()</tt> without
165 * calling <tt>reset()</tt> between them. The results of doing so
168 * @return true if there was another element in the set and this
169 * object contains the element.
175 * Returns the next element in the set, either a code point range
176 * or a string. If there are no more elements in the set, return
177 * false. If <tt>codepoint == IS_STRING</tt>, the value is a
178 * string in the <tt>string</tt> field. Otherwise the value is a
179 * range of one or more code points from <tt>codepoint</tt> to
180 * <tt>codepointeEnd</tt> inclusive.
182 * <p>The order of iteration is all code points ranges in sorted
183 * order, followed by all strings sorted order. Ranges are
184 * disjoint and non-contiguous. <tt>string</tt> is undefined
185 * unless <tt>codepoint == IS_STRING</tt>. Do not mix calls to
186 * <tt>next()</tt> and <tt>nextRange()</tt> without calling
187 * <tt>reset()</tt> between them. The results of doing so are
190 * @return true if there was another element in the set and this
191 * object contains the element.
197 * Sets this iterator to visit the elements of the given set and
198 * resets it to the start of that set. The iterator is valid only
199 * so long as <tt>set</tt> is valid.
200 * @param set the set to iterate over.
203 void reset(const UnicodeSet& set);
206 * Resets this iterator to the start of the set.
212 * ICU "poor man's RTTI", returns a UClassID for this class.
216 static UClassID U_EXPORT2 getStaticClassID();
219 * ICU "poor man's RTTI", returns a UClassID for the actual class.
223 virtual UClassID getDynamicClassID() const;
225 // ======================= PRIVATES ===========================
229 // endElement and nextElements are really UChar32's, but we keep
230 // them as signed int32_t's so we can do comparisons with
231 // endElement set to -1. Leave them as int32_t's.
235 const UnicodeSet* set;
262 /** Copy constructor. Disallowed.
265 UnicodeSetIterator(const UnicodeSetIterator&); // disallow
267 /** Assignment operator. Disallowed.
270 UnicodeSetIterator& operator=(const UnicodeSetIterator&); // disallow
275 virtual void loadRange(int32_t range);
279 inline UBool UnicodeSetIterator::isString() const {
280 return codepoint == (UChar32)IS_STRING;
283 inline UChar32 UnicodeSetIterator::getCodepoint() const {
287 inline UChar32 UnicodeSetIterator::getCodepointEnd() const {
291 inline const UnicodeString& UnicodeSetIterator::getString() const {