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