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 * Copyright (C) 2011-2012, International Business Machines 6*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved. 7*0e209d39SAndroid Build Coastguard Worker ******************************************************************************* 8*0e209d39SAndroid Build Coastguard Worker * file name: appendable.h 9*0e209d39SAndroid Build Coastguard Worker * encoding: UTF-8 10*0e209d39SAndroid Build Coastguard Worker * tab size: 8 (not used) 11*0e209d39SAndroid Build Coastguard Worker * indentation:4 12*0e209d39SAndroid Build Coastguard Worker * 13*0e209d39SAndroid Build Coastguard Worker * created on: 2010dec07 14*0e209d39SAndroid Build Coastguard Worker * created by: Markus W. Scherer 15*0e209d39SAndroid Build Coastguard Worker */ 16*0e209d39SAndroid Build Coastguard Worker 17*0e209d39SAndroid Build Coastguard Worker #ifndef __APPENDABLE_H__ 18*0e209d39SAndroid Build Coastguard Worker #define __APPENDABLE_H__ 19*0e209d39SAndroid Build Coastguard Worker 20*0e209d39SAndroid Build Coastguard Worker /** 21*0e209d39SAndroid Build Coastguard Worker * \file 22*0e209d39SAndroid Build Coastguard Worker * \brief C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts). 23*0e209d39SAndroid Build Coastguard Worker */ 24*0e209d39SAndroid Build Coastguard Worker 25*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 26*0e209d39SAndroid Build Coastguard Worker 27*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API 28*0e209d39SAndroid Build Coastguard Worker 29*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h" 30*0e209d39SAndroid Build Coastguard Worker 31*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 32*0e209d39SAndroid Build Coastguard Worker 33*0e209d39SAndroid Build Coastguard Worker class UnicodeString; 34*0e209d39SAndroid Build Coastguard Worker 35*0e209d39SAndroid Build Coastguard Worker /** 36*0e209d39SAndroid Build Coastguard Worker * Base class for objects to which Unicode characters and strings can be appended. 37*0e209d39SAndroid Build Coastguard Worker * Combines elements of Java Appendable and ICU4C ByteSink. 38*0e209d39SAndroid Build Coastguard Worker * 39*0e209d39SAndroid Build Coastguard Worker * This class can be used in APIs where it does not matter whether the actual destination is 40*0e209d39SAndroid Build Coastguard Worker * a UnicodeString, a char16_t[] array, a UnicodeSet, or any other object 41*0e209d39SAndroid Build Coastguard Worker * that receives and processes characters and/or strings. 42*0e209d39SAndroid Build Coastguard Worker * 43*0e209d39SAndroid Build Coastguard Worker * Implementation classes must implement at least appendCodeUnit(char16_t). 44*0e209d39SAndroid Build Coastguard Worker * The base class provides default implementations for the other methods. 45*0e209d39SAndroid Build Coastguard Worker * 46*0e209d39SAndroid Build Coastguard Worker * The methods do not take UErrorCode parameters. 47*0e209d39SAndroid Build Coastguard Worker * If an error occurs (e.g., out-of-memory), 48*0e209d39SAndroid Build Coastguard Worker * in addition to returning false from failing operations, 49*0e209d39SAndroid Build Coastguard Worker * the implementation must prevent unexpected behavior (e.g., crashes) 50*0e209d39SAndroid Build Coastguard Worker * from further calls and should make the error condition available separately 51*0e209d39SAndroid Build Coastguard Worker * (e.g., store a UErrorCode, make/keep a UnicodeString bogus). 52*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 53*0e209d39SAndroid Build Coastguard Worker */ 54*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API Appendable : public UObject { 55*0e209d39SAndroid Build Coastguard Worker public: 56*0e209d39SAndroid Build Coastguard Worker /** 57*0e209d39SAndroid Build Coastguard Worker * Destructor. 58*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 59*0e209d39SAndroid Build Coastguard Worker */ 60*0e209d39SAndroid Build Coastguard Worker ~Appendable(); 61*0e209d39SAndroid Build Coastguard Worker 62*0e209d39SAndroid Build Coastguard Worker /** 63*0e209d39SAndroid Build Coastguard Worker * Appends a 16-bit code unit. 64*0e209d39SAndroid Build Coastguard Worker * @param c code unit 65*0e209d39SAndroid Build Coastguard Worker * @return true if the operation succeeded 66*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 67*0e209d39SAndroid Build Coastguard Worker */ 68*0e209d39SAndroid Build Coastguard Worker virtual UBool appendCodeUnit(char16_t c) = 0; 69*0e209d39SAndroid Build Coastguard Worker 70*0e209d39SAndroid Build Coastguard Worker /** 71*0e209d39SAndroid Build Coastguard Worker * Appends a code point. 72*0e209d39SAndroid Build Coastguard Worker * The default implementation calls appendCodeUnit(char16_t) once or twice. 73*0e209d39SAndroid Build Coastguard Worker * @param c code point 0..0x10ffff 74*0e209d39SAndroid Build Coastguard Worker * @return true if the operation succeeded 75*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 76*0e209d39SAndroid Build Coastguard Worker */ 77*0e209d39SAndroid Build Coastguard Worker virtual UBool appendCodePoint(UChar32 c); 78*0e209d39SAndroid Build Coastguard Worker 79*0e209d39SAndroid Build Coastguard Worker /** 80*0e209d39SAndroid Build Coastguard Worker * Appends a string. 81*0e209d39SAndroid Build Coastguard Worker * The default implementation calls appendCodeUnit(char16_t) for each code unit. 82*0e209d39SAndroid Build Coastguard Worker * @param s string, must not be nullptr if length!=0 83*0e209d39SAndroid Build Coastguard Worker * @param length string length, or -1 if NUL-terminated 84*0e209d39SAndroid Build Coastguard Worker * @return true if the operation succeeded 85*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 86*0e209d39SAndroid Build Coastguard Worker */ 87*0e209d39SAndroid Build Coastguard Worker virtual UBool appendString(const char16_t *s, int32_t length); 88*0e209d39SAndroid Build Coastguard Worker 89*0e209d39SAndroid Build Coastguard Worker /** 90*0e209d39SAndroid Build Coastguard Worker * Tells the object that the caller is going to append roughly 91*0e209d39SAndroid Build Coastguard Worker * appendCapacity char16_ts. A subclass might use this to pre-allocate 92*0e209d39SAndroid Build Coastguard Worker * a larger buffer if necessary. 93*0e209d39SAndroid Build Coastguard Worker * The default implementation does nothing. (It always returns true.) 94*0e209d39SAndroid Build Coastguard Worker * @param appendCapacity estimated number of char16_ts that will be appended 95*0e209d39SAndroid Build Coastguard Worker * @return true if the operation succeeded 96*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 97*0e209d39SAndroid Build Coastguard Worker */ 98*0e209d39SAndroid Build Coastguard Worker virtual UBool reserveAppendCapacity(int32_t appendCapacity); 99*0e209d39SAndroid Build Coastguard Worker 100*0e209d39SAndroid Build Coastguard Worker /** 101*0e209d39SAndroid Build Coastguard Worker * Returns a writable buffer for appending and writes the buffer's capacity to 102*0e209d39SAndroid Build Coastguard Worker * *resultCapacity. Guarantees *resultCapacity>=minCapacity. 103*0e209d39SAndroid Build Coastguard Worker * May return a pointer to the caller-owned scratch buffer which must have 104*0e209d39SAndroid Build Coastguard Worker * scratchCapacity>=minCapacity. 105*0e209d39SAndroid Build Coastguard Worker * The returned buffer is only valid until the next operation 106*0e209d39SAndroid Build Coastguard Worker * on this Appendable. 107*0e209d39SAndroid Build Coastguard Worker * 108*0e209d39SAndroid Build Coastguard Worker * After writing at most *resultCapacity char16_ts, call appendString() with the 109*0e209d39SAndroid Build Coastguard Worker * pointer returned from this function and the number of char16_ts written. 110*0e209d39SAndroid Build Coastguard Worker * Many appendString() implementations will avoid copying char16_ts if this function 111*0e209d39SAndroid Build Coastguard Worker * returned an internal buffer. 112*0e209d39SAndroid Build Coastguard Worker * 113*0e209d39SAndroid Build Coastguard Worker * Partial usage example: 114*0e209d39SAndroid Build Coastguard Worker * \code 115*0e209d39SAndroid Build Coastguard Worker * int32_t capacity; 116*0e209d39SAndroid Build Coastguard Worker * char16_t* buffer = app.getAppendBuffer(..., &capacity); 117*0e209d39SAndroid Build Coastguard Worker * ... Write n char16_ts into buffer, with n <= capacity. 118*0e209d39SAndroid Build Coastguard Worker * app.appendString(buffer, n); 119*0e209d39SAndroid Build Coastguard Worker * \endcode 120*0e209d39SAndroid Build Coastguard Worker * In many implementations, that call to append will avoid copying char16_ts. 121*0e209d39SAndroid Build Coastguard Worker * 122*0e209d39SAndroid Build Coastguard Worker * If the Appendable allocates or reallocates an internal buffer, it should use 123*0e209d39SAndroid Build Coastguard Worker * the desiredCapacityHint if appropriate. 124*0e209d39SAndroid Build Coastguard Worker * If a caller cannot provide a reasonable guess at the desired capacity, 125*0e209d39SAndroid Build Coastguard Worker * it should pass desiredCapacityHint=0. 126*0e209d39SAndroid Build Coastguard Worker * 127*0e209d39SAndroid Build Coastguard Worker * If a non-scratch buffer is returned, the caller may only pass 128*0e209d39SAndroid Build Coastguard Worker * a prefix to it to appendString(). 129*0e209d39SAndroid Build Coastguard Worker * That is, it is not correct to pass an interior pointer to appendString(). 130*0e209d39SAndroid Build Coastguard Worker * 131*0e209d39SAndroid Build Coastguard Worker * The default implementation always returns the scratch buffer. 132*0e209d39SAndroid Build Coastguard Worker * 133*0e209d39SAndroid Build Coastguard Worker * @param minCapacity required minimum capacity of the returned buffer; 134*0e209d39SAndroid Build Coastguard Worker * must be non-negative 135*0e209d39SAndroid Build Coastguard Worker * @param desiredCapacityHint desired capacity of the returned buffer; 136*0e209d39SAndroid Build Coastguard Worker * must be non-negative 137*0e209d39SAndroid Build Coastguard Worker * @param scratch default caller-owned buffer 138*0e209d39SAndroid Build Coastguard Worker * @param scratchCapacity capacity of the scratch buffer 139*0e209d39SAndroid Build Coastguard Worker * @param resultCapacity pointer to an integer which will be set to the 140*0e209d39SAndroid Build Coastguard Worker * capacity of the returned buffer 141*0e209d39SAndroid Build Coastguard Worker * @return a buffer with *resultCapacity>=minCapacity 142*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 143*0e209d39SAndroid Build Coastguard Worker */ 144*0e209d39SAndroid Build Coastguard Worker virtual char16_t *getAppendBuffer(int32_t minCapacity, 145*0e209d39SAndroid Build Coastguard Worker int32_t desiredCapacityHint, 146*0e209d39SAndroid Build Coastguard Worker char16_t *scratch, int32_t scratchCapacity, 147*0e209d39SAndroid Build Coastguard Worker int32_t *resultCapacity); 148*0e209d39SAndroid Build Coastguard Worker }; 149*0e209d39SAndroid Build Coastguard Worker 150*0e209d39SAndroid Build Coastguard Worker /** 151*0e209d39SAndroid Build Coastguard Worker * An Appendable implementation which writes to a UnicodeString. 152*0e209d39SAndroid Build Coastguard Worker * 153*0e209d39SAndroid Build Coastguard Worker * This class is not intended for public subclassing. 154*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 155*0e209d39SAndroid Build Coastguard Worker */ 156*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API UnicodeStringAppendable : public Appendable { 157*0e209d39SAndroid Build Coastguard Worker public: 158*0e209d39SAndroid Build Coastguard Worker /** 159*0e209d39SAndroid Build Coastguard Worker * Aliases the UnicodeString (keeps its reference) for writing. 160*0e209d39SAndroid Build Coastguard Worker * @param s The UnicodeString to which this Appendable will write. 161*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 162*0e209d39SAndroid Build Coastguard Worker */ UnicodeStringAppendable(UnicodeString & s)163*0e209d39SAndroid Build Coastguard Worker explicit UnicodeStringAppendable(UnicodeString &s) : str(s) {} 164*0e209d39SAndroid Build Coastguard Worker 165*0e209d39SAndroid Build Coastguard Worker /** 166*0e209d39SAndroid Build Coastguard Worker * Destructor. 167*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 168*0e209d39SAndroid Build Coastguard Worker */ 169*0e209d39SAndroid Build Coastguard Worker ~UnicodeStringAppendable(); 170*0e209d39SAndroid Build Coastguard Worker 171*0e209d39SAndroid Build Coastguard Worker /** 172*0e209d39SAndroid Build Coastguard Worker * Appends a 16-bit code unit to the string. 173*0e209d39SAndroid Build Coastguard Worker * @param c code unit 174*0e209d39SAndroid Build Coastguard Worker * @return true if the operation succeeded 175*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 176*0e209d39SAndroid Build Coastguard Worker */ 177*0e209d39SAndroid Build Coastguard Worker virtual UBool appendCodeUnit(char16_t c) override; 178*0e209d39SAndroid Build Coastguard Worker 179*0e209d39SAndroid Build Coastguard Worker /** 180*0e209d39SAndroid Build Coastguard Worker * Appends a code point to the string. 181*0e209d39SAndroid Build Coastguard Worker * @param c code point 0..0x10ffff 182*0e209d39SAndroid Build Coastguard Worker * @return true if the operation succeeded 183*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 184*0e209d39SAndroid Build Coastguard Worker */ 185*0e209d39SAndroid Build Coastguard Worker virtual UBool appendCodePoint(UChar32 c) override; 186*0e209d39SAndroid Build Coastguard Worker 187*0e209d39SAndroid Build Coastguard Worker /** 188*0e209d39SAndroid Build Coastguard Worker * Appends a string to the UnicodeString. 189*0e209d39SAndroid Build Coastguard Worker * @param s string, must not be nullptr if length!=0 190*0e209d39SAndroid Build Coastguard Worker * @param length string length, or -1 if NUL-terminated 191*0e209d39SAndroid Build Coastguard Worker * @return true if the operation succeeded 192*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 193*0e209d39SAndroid Build Coastguard Worker */ 194*0e209d39SAndroid Build Coastguard Worker virtual UBool appendString(const char16_t *s, int32_t length) override; 195*0e209d39SAndroid Build Coastguard Worker 196*0e209d39SAndroid Build Coastguard Worker /** 197*0e209d39SAndroid Build Coastguard Worker * Tells the UnicodeString that the caller is going to append roughly 198*0e209d39SAndroid Build Coastguard Worker * appendCapacity char16_ts. 199*0e209d39SAndroid Build Coastguard Worker * @param appendCapacity estimated number of char16_ts that will be appended 200*0e209d39SAndroid Build Coastguard Worker * @return true if the operation succeeded 201*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 202*0e209d39SAndroid Build Coastguard Worker */ 203*0e209d39SAndroid Build Coastguard Worker virtual UBool reserveAppendCapacity(int32_t appendCapacity) override; 204*0e209d39SAndroid Build Coastguard Worker 205*0e209d39SAndroid Build Coastguard Worker /** 206*0e209d39SAndroid Build Coastguard Worker * Returns a writable buffer for appending and writes the buffer's capacity to 207*0e209d39SAndroid Build Coastguard Worker * *resultCapacity. Guarantees *resultCapacity>=minCapacity. 208*0e209d39SAndroid Build Coastguard Worker * May return a pointer to the caller-owned scratch buffer which must have 209*0e209d39SAndroid Build Coastguard Worker * scratchCapacity>=minCapacity. 210*0e209d39SAndroid Build Coastguard Worker * The returned buffer is only valid until the next write operation 211*0e209d39SAndroid Build Coastguard Worker * on the UnicodeString. 212*0e209d39SAndroid Build Coastguard Worker * 213*0e209d39SAndroid Build Coastguard Worker * For details see Appendable::getAppendBuffer(). 214*0e209d39SAndroid Build Coastguard Worker * 215*0e209d39SAndroid Build Coastguard Worker * @param minCapacity required minimum capacity of the returned buffer; 216*0e209d39SAndroid Build Coastguard Worker * must be non-negative 217*0e209d39SAndroid Build Coastguard Worker * @param desiredCapacityHint desired capacity of the returned buffer; 218*0e209d39SAndroid Build Coastguard Worker * must be non-negative 219*0e209d39SAndroid Build Coastguard Worker * @param scratch default caller-owned buffer 220*0e209d39SAndroid Build Coastguard Worker * @param scratchCapacity capacity of the scratch buffer 221*0e209d39SAndroid Build Coastguard Worker * @param resultCapacity pointer to an integer which will be set to the 222*0e209d39SAndroid Build Coastguard Worker * capacity of the returned buffer 223*0e209d39SAndroid Build Coastguard Worker * @return a buffer with *resultCapacity>=minCapacity 224*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 225*0e209d39SAndroid Build Coastguard Worker */ 226*0e209d39SAndroid Build Coastguard Worker virtual char16_t *getAppendBuffer(int32_t minCapacity, 227*0e209d39SAndroid Build Coastguard Worker int32_t desiredCapacityHint, 228*0e209d39SAndroid Build Coastguard Worker char16_t *scratch, int32_t scratchCapacity, 229*0e209d39SAndroid Build Coastguard Worker int32_t *resultCapacity) override; 230*0e209d39SAndroid Build Coastguard Worker 231*0e209d39SAndroid Build Coastguard Worker private: 232*0e209d39SAndroid Build Coastguard Worker UnicodeString &str; 233*0e209d39SAndroid Build Coastguard Worker }; 234*0e209d39SAndroid Build Coastguard Worker 235*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 236*0e209d39SAndroid Build Coastguard Worker 237*0e209d39SAndroid Build Coastguard Worker #endif /* U_SHOW_CPLUSPLUS_API */ 238*0e209d39SAndroid Build Coastguard Worker 239*0e209d39SAndroid Build Coastguard Worker #endif // __APPENDABLE_H__ 240