Wed, 31 Dec 2014 06:09:35 +0100
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 | * |
michael@0 | 4 | * Copyright (C) 2002-2012, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #ifndef STRENUM_H |
michael@0 | 11 | #define STRENUM_H |
michael@0 | 12 | |
michael@0 | 13 | #include "unicode/uobject.h" |
michael@0 | 14 | #include "unicode/unistr.h" |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * \file |
michael@0 | 18 | * \brief C++ API: String Enumeration |
michael@0 | 19 | */ |
michael@0 | 20 | |
michael@0 | 21 | U_NAMESPACE_BEGIN |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * Base class for 'pure' C++ implementations of uenum api. Adds a |
michael@0 | 25 | * method that returns the next UnicodeString since in C++ this can |
michael@0 | 26 | * be a common storage format for strings. |
michael@0 | 27 | * |
michael@0 | 28 | * <p>The model is that the enumeration is over strings maintained by |
michael@0 | 29 | * a 'service.' At any point, the service might change, invalidating |
michael@0 | 30 | * the enumerator (though this is expected to be rare). The iterator |
michael@0 | 31 | * returns an error if this has occurred. Lack of the error is no |
michael@0 | 32 | * guarantee that the service didn't change immediately after the |
michael@0 | 33 | * call, so the returned string still might not be 'valid' on |
michael@0 | 34 | * subsequent use.</p> |
michael@0 | 35 | * |
michael@0 | 36 | * <p>Strings may take the form of const char*, const UChar*, or const |
michael@0 | 37 | * UnicodeString*. The type you get is determine by the variant of |
michael@0 | 38 | * 'next' that you call. In general the StringEnumeration is |
michael@0 | 39 | * optimized for one of these types, but all StringEnumerations can |
michael@0 | 40 | * return all types. Returned strings are each terminated with a NUL. |
michael@0 | 41 | * Depending on the service data, they might also include embedded NUL |
michael@0 | 42 | * characters, so API is provided to optionally return the true |
michael@0 | 43 | * length, counting the embedded NULs but not counting the terminating |
michael@0 | 44 | * NUL.</p> |
michael@0 | 45 | * |
michael@0 | 46 | * <p>The pointers returned by next, unext, and snext become invalid |
michael@0 | 47 | * upon any subsequent call to the enumeration's destructor, next, |
michael@0 | 48 | * unext, snext, or reset.</p> |
michael@0 | 49 | * |
michael@0 | 50 | * ICU 2.8 adds some default implementations and helper functions |
michael@0 | 51 | * for subclasses. |
michael@0 | 52 | * |
michael@0 | 53 | * @stable ICU 2.4 |
michael@0 | 54 | */ |
michael@0 | 55 | class U_COMMON_API StringEnumeration : public UObject { |
michael@0 | 56 | public: |
michael@0 | 57 | /** |
michael@0 | 58 | * Destructor. |
michael@0 | 59 | * @stable ICU 2.4 |
michael@0 | 60 | */ |
michael@0 | 61 | virtual ~StringEnumeration(); |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * Clone this object, an instance of a subclass of StringEnumeration. |
michael@0 | 65 | * Clones can be used concurrently in multiple threads. |
michael@0 | 66 | * If a subclass does not implement clone(), or if an error occurs, |
michael@0 | 67 | * then NULL is returned. |
michael@0 | 68 | * The clone functions in all subclasses return a base class pointer |
michael@0 | 69 | * because some compilers do not support covariant (same-as-this) |
michael@0 | 70 | * return types; cast to the appropriate subclass if necessary. |
michael@0 | 71 | * The caller must delete the clone. |
michael@0 | 72 | * |
michael@0 | 73 | * @return a clone of this object |
michael@0 | 74 | * |
michael@0 | 75 | * @see getDynamicClassID |
michael@0 | 76 | * @stable ICU 2.8 |
michael@0 | 77 | */ |
michael@0 | 78 | virtual StringEnumeration *clone() const; |
michael@0 | 79 | |
michael@0 | 80 | /** |
michael@0 | 81 | * <p>Return the number of elements that the iterator traverses. If |
michael@0 | 82 | * the iterator is out of sync with its service, status is set to |
michael@0 | 83 | * U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p> |
michael@0 | 84 | * |
michael@0 | 85 | * <p>The return value will not change except possibly as a result of |
michael@0 | 86 | * a subsequent call to reset, or if the iterator becomes out of sync.</p> |
michael@0 | 87 | * |
michael@0 | 88 | * <p>This is a convenience function. It can end up being very |
michael@0 | 89 | * expensive as all the items might have to be pre-fetched |
michael@0 | 90 | * (depending on the storage format of the data being |
michael@0 | 91 | * traversed).</p> |
michael@0 | 92 | * |
michael@0 | 93 | * @param status the error code. |
michael@0 | 94 | * @return number of elements in the iterator. |
michael@0 | 95 | * |
michael@0 | 96 | * @stable ICU 2.4 */ |
michael@0 | 97 | virtual int32_t count(UErrorCode& status) const = 0; |
michael@0 | 98 | |
michael@0 | 99 | /** |
michael@0 | 100 | * <p>Returns the next element as a NUL-terminated char*. If there |
michael@0 | 101 | * are no more elements, returns NULL. If the resultLength pointer |
michael@0 | 102 | * is not NULL, the length of the string (not counting the |
michael@0 | 103 | * terminating NUL) is returned at that address. If an error |
michael@0 | 104 | * status is returned, the value at resultLength is undefined.</p> |
michael@0 | 105 | * |
michael@0 | 106 | * <p>The returned pointer is owned by this iterator and must not be |
michael@0 | 107 | * deleted by the caller. The pointer is valid until the next call |
michael@0 | 108 | * to next, unext, snext, reset, or the enumerator's destructor.</p> |
michael@0 | 109 | * |
michael@0 | 110 | * <p>If the iterator is out of sync with its service, status is set |
michael@0 | 111 | * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p> |
michael@0 | 112 | * |
michael@0 | 113 | * <p>If the native service string is a UChar* string, it is |
michael@0 | 114 | * converted to char* with the invariant converter. If the |
michael@0 | 115 | * conversion fails (because a character cannot be converted) then |
michael@0 | 116 | * status is set to U_INVARIANT_CONVERSION_ERROR and the return |
michael@0 | 117 | * value is undefined (though not NULL).</p> |
michael@0 | 118 | * |
michael@0 | 119 | * Starting with ICU 2.8, the default implementation calls snext() |
michael@0 | 120 | * and handles the conversion. |
michael@0 | 121 | * Either next() or snext() must be implemented differently by a subclass. |
michael@0 | 122 | * |
michael@0 | 123 | * @param status the error code. |
michael@0 | 124 | * @param resultLength a pointer to receive the length, can be NULL. |
michael@0 | 125 | * @return a pointer to the string, or NULL. |
michael@0 | 126 | * |
michael@0 | 127 | * @stable ICU 2.4 |
michael@0 | 128 | */ |
michael@0 | 129 | virtual const char* next(int32_t *resultLength, UErrorCode& status); |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * <p>Returns the next element as a NUL-terminated UChar*. If there |
michael@0 | 133 | * are no more elements, returns NULL. If the resultLength pointer |
michael@0 | 134 | * is not NULL, the length of the string (not counting the |
michael@0 | 135 | * terminating NUL) is returned at that address. If an error |
michael@0 | 136 | * status is returned, the value at resultLength is undefined.</p> |
michael@0 | 137 | * |
michael@0 | 138 | * <p>The returned pointer is owned by this iterator and must not be |
michael@0 | 139 | * deleted by the caller. The pointer is valid until the next call |
michael@0 | 140 | * to next, unext, snext, reset, or the enumerator's destructor.</p> |
michael@0 | 141 | * |
michael@0 | 142 | * <p>If the iterator is out of sync with its service, status is set |
michael@0 | 143 | * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p> |
michael@0 | 144 | * |
michael@0 | 145 | * Starting with ICU 2.8, the default implementation calls snext() |
michael@0 | 146 | * and handles the conversion. |
michael@0 | 147 | * |
michael@0 | 148 | * @param status the error code. |
michael@0 | 149 | * @param resultLength a ponter to receive the length, can be NULL. |
michael@0 | 150 | * @return a pointer to the string, or NULL. |
michael@0 | 151 | * |
michael@0 | 152 | * @stable ICU 2.4 |
michael@0 | 153 | */ |
michael@0 | 154 | virtual const UChar* unext(int32_t *resultLength, UErrorCode& status); |
michael@0 | 155 | |
michael@0 | 156 | /** |
michael@0 | 157 | * <p>Returns the next element a UnicodeString*. If there are no |
michael@0 | 158 | * more elements, returns NULL.</p> |
michael@0 | 159 | * |
michael@0 | 160 | * <p>The returned pointer is owned by this iterator and must not be |
michael@0 | 161 | * deleted by the caller. The pointer is valid until the next call |
michael@0 | 162 | * to next, unext, snext, reset, or the enumerator's destructor.</p> |
michael@0 | 163 | * |
michael@0 | 164 | * <p>If the iterator is out of sync with its service, status is set |
michael@0 | 165 | * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p> |
michael@0 | 166 | * |
michael@0 | 167 | * Starting with ICU 2.8, the default implementation calls next() |
michael@0 | 168 | * and handles the conversion. |
michael@0 | 169 | * Either next() or snext() must be implemented differently by a subclass. |
michael@0 | 170 | * |
michael@0 | 171 | * @param status the error code. |
michael@0 | 172 | * @return a pointer to the string, or NULL. |
michael@0 | 173 | * |
michael@0 | 174 | * @stable ICU 2.4 |
michael@0 | 175 | */ |
michael@0 | 176 | virtual const UnicodeString* snext(UErrorCode& status); |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * <p>Resets the iterator. This re-establishes sync with the |
michael@0 | 180 | * service and rewinds the iterator to start at the first |
michael@0 | 181 | * element.</p> |
michael@0 | 182 | * |
michael@0 | 183 | * <p>Previous pointers returned by next, unext, or snext become |
michael@0 | 184 | * invalid, and the value returned by count might change.</p> |
michael@0 | 185 | * |
michael@0 | 186 | * @param status the error code. |
michael@0 | 187 | * |
michael@0 | 188 | * @stable ICU 2.4 |
michael@0 | 189 | */ |
michael@0 | 190 | virtual void reset(UErrorCode& status) = 0; |
michael@0 | 191 | |
michael@0 | 192 | /** |
michael@0 | 193 | * Compares this enumeration to other to check if both are equal |
michael@0 | 194 | * |
michael@0 | 195 | * @param that The other string enumeration to compare this object to |
michael@0 | 196 | * @return TRUE if the enumerations are equal. FALSE if not. |
michael@0 | 197 | * @stable ICU 3.6 |
michael@0 | 198 | */ |
michael@0 | 199 | virtual UBool operator==(const StringEnumeration& that)const; |
michael@0 | 200 | /** |
michael@0 | 201 | * Compares this enumeration to other to check if both are not equal |
michael@0 | 202 | * |
michael@0 | 203 | * @param that The other string enumeration to compare this object to |
michael@0 | 204 | * @return TRUE if the enumerations are equal. FALSE if not. |
michael@0 | 205 | * @stable ICU 3.6 |
michael@0 | 206 | */ |
michael@0 | 207 | virtual UBool operator!=(const StringEnumeration& that)const; |
michael@0 | 208 | |
michael@0 | 209 | protected: |
michael@0 | 210 | /** |
michael@0 | 211 | * UnicodeString field for use with default implementations and subclasses. |
michael@0 | 212 | * @stable ICU 2.8 |
michael@0 | 213 | */ |
michael@0 | 214 | UnicodeString unistr; |
michael@0 | 215 | /** |
michael@0 | 216 | * char * default buffer for use with default implementations and subclasses. |
michael@0 | 217 | * @stable ICU 2.8 |
michael@0 | 218 | */ |
michael@0 | 219 | char charsBuffer[32]; |
michael@0 | 220 | /** |
michael@0 | 221 | * char * buffer for use with default implementations and subclasses. |
michael@0 | 222 | * Allocated in constructor and in ensureCharsCapacity(). |
michael@0 | 223 | * @stable ICU 2.8 |
michael@0 | 224 | */ |
michael@0 | 225 | char *chars; |
michael@0 | 226 | /** |
michael@0 | 227 | * Capacity of chars, for use with default implementations and subclasses. |
michael@0 | 228 | * @stable ICU 2.8 |
michael@0 | 229 | */ |
michael@0 | 230 | int32_t charsCapacity; |
michael@0 | 231 | |
michael@0 | 232 | /** |
michael@0 | 233 | * Default constructor for use with default implementations and subclasses. |
michael@0 | 234 | * @stable ICU 2.8 |
michael@0 | 235 | */ |
michael@0 | 236 | StringEnumeration(); |
michael@0 | 237 | |
michael@0 | 238 | /** |
michael@0 | 239 | * Ensures that chars is at least as large as the requested capacity. |
michael@0 | 240 | * For use with default implementations and subclasses. |
michael@0 | 241 | * |
michael@0 | 242 | * @param capacity Requested capacity. |
michael@0 | 243 | * @param status ICU in/out error code. |
michael@0 | 244 | * @stable ICU 2.8 |
michael@0 | 245 | */ |
michael@0 | 246 | void ensureCharsCapacity(int32_t capacity, UErrorCode &status); |
michael@0 | 247 | |
michael@0 | 248 | /** |
michael@0 | 249 | * Converts s to Unicode and sets unistr to the result. |
michael@0 | 250 | * For use with default implementations and subclasses, |
michael@0 | 251 | * especially for implementations of snext() in terms of next(). |
michael@0 | 252 | * This is provided with a helper function instead of a default implementation |
michael@0 | 253 | * of snext() to avoid potential infinite loops between next() and snext(). |
michael@0 | 254 | * |
michael@0 | 255 | * For example: |
michael@0 | 256 | * \code |
michael@0 | 257 | * const UnicodeString* snext(UErrorCode& status) { |
michael@0 | 258 | * int32_t resultLength=0; |
michael@0 | 259 | * const char *s=next(&resultLength, status); |
michael@0 | 260 | * return setChars(s, resultLength, status); |
michael@0 | 261 | * } |
michael@0 | 262 | * \endcode |
michael@0 | 263 | * |
michael@0 | 264 | * @param s String to be converted to Unicode. |
michael@0 | 265 | * @param length Length of the string. |
michael@0 | 266 | * @param status ICU in/out error code. |
michael@0 | 267 | * @return A pointer to unistr. |
michael@0 | 268 | * @stable ICU 2.8 |
michael@0 | 269 | */ |
michael@0 | 270 | UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status); |
michael@0 | 271 | }; |
michael@0 | 272 | |
michael@0 | 273 | U_NAMESPACE_END |
michael@0 | 274 | |
michael@0 | 275 | /* STRENUM_H */ |
michael@0 | 276 | #endif |