intl/icu/source/common/unicode/usetiter.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (c) 2002-2008, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 */
michael@0 7 #ifndef USETITER_H
michael@0 8 #define USETITER_H
michael@0 9
michael@0 10 #include "unicode/utypes.h"
michael@0 11 #include "unicode/uobject.h"
michael@0 12 #include "unicode/unistr.h"
michael@0 13
michael@0 14 /**
michael@0 15 * \file
michael@0 16 * \brief C++ API: UnicodeSetIterator iterates over the contents of a UnicodeSet.
michael@0 17 */
michael@0 18
michael@0 19 U_NAMESPACE_BEGIN
michael@0 20
michael@0 21 class UnicodeSet;
michael@0 22 class UnicodeString;
michael@0 23
michael@0 24 /**
michael@0 25 *
michael@0 26 * UnicodeSetIterator iterates over the contents of a UnicodeSet. It
michael@0 27 * iterates over either code points or code point ranges. After all
michael@0 28 * code points or ranges have been returned, it returns the
michael@0 29 * multicharacter strings of the UnicodeSet, if any.
michael@0 30 *
michael@0 31 * This class is not intended to be subclassed. Consider any fields
michael@0 32 * or methods declared as "protected" to be private. The use of
michael@0 33 * protected in this class is an artifact of history.
michael@0 34 *
michael@0 35 * <p>To iterate over code points and strings, use a loop like this:
michael@0 36 * <pre>
michael@0 37 * UnicodeSetIterator it(set);
michael@0 38 * while (it.next()) {
michael@0 39 * processItem(it.getString());
michael@0 40 * }
michael@0 41 * </pre>
michael@0 42 * <p>Each item in the set is accessed as a string. Set elements
michael@0 43 * consisting of single code points are returned as strings containing
michael@0 44 * just the one code point.
michael@0 45 *
michael@0 46 * <p>To iterate over code point ranges, instead of individual code points,
michael@0 47 * use a loop like this:
michael@0 48 * <pre>
michael@0 49 * UnicodeSetIterator it(set);
michael@0 50 * while (it.nextRange()) {
michael@0 51 * if (it.isString()) {
michael@0 52 * processString(it.getString());
michael@0 53 * } else {
michael@0 54 * processCodepointRange(it.getCodepoint(), it.getCodepointEnd());
michael@0 55 * }
michael@0 56 * }
michael@0 57 * </pre>
michael@0 58 * @author M. Davis
michael@0 59 * @stable ICU 2.4
michael@0 60 */
michael@0 61 class U_COMMON_API UnicodeSetIterator : public UObject {
michael@0 62
michael@0 63 protected:
michael@0 64
michael@0 65 /**
michael@0 66 * Value of <tt>codepoint</tt> if the iterator points to a string.
michael@0 67 * If <tt>codepoint == IS_STRING</tt>, then examine
michael@0 68 * <tt>string</tt> for the current iteration result.
michael@0 69 * @stable ICU 2.4
michael@0 70 */
michael@0 71 enum { IS_STRING = -1 };
michael@0 72
michael@0 73 /**
michael@0 74 * Current code point, or the special value <tt>IS_STRING</tt>, if
michael@0 75 * the iterator points to a string.
michael@0 76 * @stable ICU 2.4
michael@0 77 */
michael@0 78 UChar32 codepoint;
michael@0 79
michael@0 80 /**
michael@0 81 * When iterating over ranges using <tt>nextRange()</tt>,
michael@0 82 * <tt>codepointEnd</tt> contains the inclusive end of the
michael@0 83 * iteration range, if <tt>codepoint != IS_STRING</tt>. If
michael@0 84 * iterating over code points using <tt>next()</tt>, or if
michael@0 85 * <tt>codepoint == IS_STRING</tt>, then the value of
michael@0 86 * <tt>codepointEnd</tt> is undefined.
michael@0 87 * @stable ICU 2.4
michael@0 88 */
michael@0 89 UChar32 codepointEnd;
michael@0 90
michael@0 91 /**
michael@0 92 * If <tt>codepoint == IS_STRING</tt>, then <tt>string</tt> points
michael@0 93 * to the current string. If <tt>codepoint != IS_STRING</tt>, the
michael@0 94 * value of <tt>string</tt> is undefined.
michael@0 95 * @stable ICU 2.4
michael@0 96 */
michael@0 97 const UnicodeString* string;
michael@0 98
michael@0 99 public:
michael@0 100
michael@0 101 /**
michael@0 102 * Create an iterator over the given set. The iterator is valid
michael@0 103 * only so long as <tt>set</tt> is valid.
michael@0 104 * @param set set to iterate over
michael@0 105 * @stable ICU 2.4
michael@0 106 */
michael@0 107 UnicodeSetIterator(const UnicodeSet& set);
michael@0 108
michael@0 109 /**
michael@0 110 * Create an iterator over nothing. <tt>next()</tt> and
michael@0 111 * <tt>nextRange()</tt> return false. This is a convenience
michael@0 112 * constructor allowing the target to be set later.
michael@0 113 * @stable ICU 2.4
michael@0 114 */
michael@0 115 UnicodeSetIterator();
michael@0 116
michael@0 117 /**
michael@0 118 * Destructor.
michael@0 119 * @stable ICU 2.4
michael@0 120 */
michael@0 121 virtual ~UnicodeSetIterator();
michael@0 122
michael@0 123 /**
michael@0 124 * Returns true if the current element is a string. If so, the
michael@0 125 * caller can retrieve it with <tt>getString()</tt>. If this
michael@0 126 * method returns false, the current element is a code point or
michael@0 127 * code point range, depending on whether <tt>next()</tt> or
michael@0 128 * <tt>nextRange()</tt> was called.
michael@0 129 * Elements of types string and codepoint can both be retrieved
michael@0 130 * with the function <tt>getString()</tt>.
michael@0 131 * Elements of type codepoint can also be retrieved with
michael@0 132 * <tt>getCodepoint()</tt>.
michael@0 133 * For ranges, <tt>getCodepoint()</tt> returns the starting codepoint
michael@0 134 * of the range, and <tt>getCodepointEnd()</tt> returns the end
michael@0 135 * of the range.
michael@0 136 * @stable ICU 2.4
michael@0 137 */
michael@0 138 inline UBool isString() const;
michael@0 139
michael@0 140 /**
michael@0 141 * Returns the current code point, if <tt>isString()</tt> returned
michael@0 142 * false. Otherwise returns an undefined result.
michael@0 143 * @stable ICU 2.4
michael@0 144 */
michael@0 145 inline UChar32 getCodepoint() const;
michael@0 146
michael@0 147 /**
michael@0 148 * Returns the end of the current code point range, if
michael@0 149 * <tt>isString()</tt> returned false and <tt>nextRange()</tt> was
michael@0 150 * called. Otherwise returns an undefined result.
michael@0 151 * @stable ICU 2.4
michael@0 152 */
michael@0 153 inline UChar32 getCodepointEnd() const;
michael@0 154
michael@0 155 /**
michael@0 156 * Returns the current string, if <tt>isString()</tt> returned
michael@0 157 * true. If the current iteration item is a code point, a UnicodeString
michael@0 158 * containing that single code point is returned.
michael@0 159 *
michael@0 160 * Ownership of the returned string remains with the iterator.
michael@0 161 * The string is guaranteed to remain valid only until the iterator is
michael@0 162 * advanced to the next item, or until the iterator is deleted.
michael@0 163 *
michael@0 164 * @stable ICU 2.4
michael@0 165 */
michael@0 166 const UnicodeString& getString();
michael@0 167
michael@0 168 /**
michael@0 169 * Advances the iteration position to the next element in the set,
michael@0 170 * which can be either a single code point or a string.
michael@0 171 * If there are no more elements in the set, return false.
michael@0 172 *
michael@0 173 * <p>
michael@0 174 * If <tt>isString() == TRUE</tt>, the value is a
michael@0 175 * string, otherwise the value is a
michael@0 176 * single code point. Elements of either type can be retrieved
michael@0 177 * with the function <tt>getString()</tt>, while elements of
michael@0 178 * consisting of a single code point can be retrieved with
michael@0 179 * <tt>getCodepoint()</tt>
michael@0 180 *
michael@0 181 * <p>The order of iteration is all code points in sorted order,
michael@0 182 * followed by all strings sorted order. Do not mix
michael@0 183 * calls to <tt>next()</tt> and <tt>nextRange()</tt> without
michael@0 184 * calling <tt>reset()</tt> between them. The results of doing so
michael@0 185 * are undefined.
michael@0 186 *
michael@0 187 * @return true if there was another element in the set.
michael@0 188 * @stable ICU 2.4
michael@0 189 */
michael@0 190 UBool next();
michael@0 191
michael@0 192 /**
michael@0 193 * Returns the next element in the set, either a code point range
michael@0 194 * or a string. If there are no more elements in the set, return
michael@0 195 * false. If <tt>isString() == TRUE</tt>, the value is a
michael@0 196 * string and can be accessed with <tt>getString()</tt>. Otherwise the value is a
michael@0 197 * range of one or more code points from <tt>getCodepoint()</tt> to
michael@0 198 * <tt>getCodepointeEnd()</tt> inclusive.
michael@0 199 *
michael@0 200 * <p>The order of iteration is all code points ranges in sorted
michael@0 201 * order, followed by all strings sorted order. Ranges are
michael@0 202 * disjoint and non-contiguous. The value returned from <tt>getString()</tt>
michael@0 203 * is undefined unless <tt>isString() == TRUE</tt>. Do not mix calls to
michael@0 204 * <tt>next()</tt> and <tt>nextRange()</tt> without calling
michael@0 205 * <tt>reset()</tt> between them. The results of doing so are
michael@0 206 * undefined.
michael@0 207 *
michael@0 208 * @return true if there was another element in the set.
michael@0 209 * @stable ICU 2.4
michael@0 210 */
michael@0 211 UBool nextRange();
michael@0 212
michael@0 213 /**
michael@0 214 * Sets this iterator to visit the elements of the given set and
michael@0 215 * resets it to the start of that set. The iterator is valid only
michael@0 216 * so long as <tt>set</tt> is valid.
michael@0 217 * @param set the set to iterate over.
michael@0 218 * @stable ICU 2.4
michael@0 219 */
michael@0 220 void reset(const UnicodeSet& set);
michael@0 221
michael@0 222 /**
michael@0 223 * Resets this iterator to the start of the set.
michael@0 224 * @stable ICU 2.4
michael@0 225 */
michael@0 226 void reset();
michael@0 227
michael@0 228 /**
michael@0 229 * ICU "poor man's RTTI", returns a UClassID for this class.
michael@0 230 *
michael@0 231 * @stable ICU 2.4
michael@0 232 */
michael@0 233 static UClassID U_EXPORT2 getStaticClassID();
michael@0 234
michael@0 235 /**
michael@0 236 * ICU "poor man's RTTI", returns a UClassID for the actual class.
michael@0 237 *
michael@0 238 * @stable ICU 2.4
michael@0 239 */
michael@0 240 virtual UClassID getDynamicClassID() const;
michael@0 241
michael@0 242 // ======================= PRIVATES ===========================
michael@0 243
michael@0 244 protected:
michael@0 245
michael@0 246 // endElement and nextElements are really UChar32's, but we keep
michael@0 247 // them as signed int32_t's so we can do comparisons with
michael@0 248 // endElement set to -1. Leave them as int32_t's.
michael@0 249 /** The set
michael@0 250 * @stable ICU 2.4
michael@0 251 */
michael@0 252 const UnicodeSet* set;
michael@0 253 /** End range
michael@0 254 * @stable ICU 2.4
michael@0 255 */
michael@0 256 int32_t endRange;
michael@0 257 /** Range
michael@0 258 * @stable ICU 2.4
michael@0 259 */
michael@0 260 int32_t range;
michael@0 261 /** End element
michael@0 262 * @stable ICU 2.4
michael@0 263 */
michael@0 264 int32_t endElement;
michael@0 265 /** Next element
michael@0 266 * @stable ICU 2.4
michael@0 267 */
michael@0 268 int32_t nextElement;
michael@0 269 //UBool abbreviated;
michael@0 270 /** Next string
michael@0 271 * @stable ICU 2.4
michael@0 272 */
michael@0 273 int32_t nextString;
michael@0 274 /** String count
michael@0 275 * @stable ICU 2.4
michael@0 276 */
michael@0 277 int32_t stringCount;
michael@0 278
michael@0 279 /**
michael@0 280 * Points to the string to use when the caller asks for a
michael@0 281 * string and the current iteration item is a code point, not a string.
michael@0 282 * @internal
michael@0 283 */
michael@0 284 UnicodeString *cpString;
michael@0 285
michael@0 286 /** Copy constructor. Disallowed.
michael@0 287 * @stable ICU 2.4
michael@0 288 */
michael@0 289 UnicodeSetIterator(const UnicodeSetIterator&); // disallow
michael@0 290
michael@0 291 /** Assignment operator. Disallowed.
michael@0 292 * @stable ICU 2.4
michael@0 293 */
michael@0 294 UnicodeSetIterator& operator=(const UnicodeSetIterator&); // disallow
michael@0 295
michael@0 296 /** Load range
michael@0 297 * @stable ICU 2.4
michael@0 298 */
michael@0 299 virtual void loadRange(int32_t range);
michael@0 300
michael@0 301 };
michael@0 302
michael@0 303 inline UBool UnicodeSetIterator::isString() const {
michael@0 304 return codepoint == (UChar32)IS_STRING;
michael@0 305 }
michael@0 306
michael@0 307 inline UChar32 UnicodeSetIterator::getCodepoint() const {
michael@0 308 return codepoint;
michael@0 309 }
michael@0 310
michael@0 311 inline UChar32 UnicodeSetIterator::getCodepointEnd() const {
michael@0 312 return codepointEnd;
michael@0 313 }
michael@0 314
michael@0 315
michael@0 316 U_NAMESPACE_END
michael@0 317
michael@0 318 #endif

mercurial