Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/unicode/appendable.h
38827 views
/*1*******************************************************************************2* Copyright (C) 2011-2012, International Business Machines3* Corporation and others. All Rights Reserved.4*******************************************************************************5* file name: appendable.h6* encoding: US-ASCII7* tab size: 8 (not used)8* indentation:49*10* created on: 2010dec0711* created by: Markus W. Scherer12*/1314#ifndef __APPENDABLE_H__15#define __APPENDABLE_H__1617/**18* \file19* \brief C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (UChars).20*/2122#include "unicode/utypes.h"23#include "unicode/uobject.h"2425U_NAMESPACE_BEGIN2627class UnicodeString;2829/**30* Base class for objects to which Unicode characters and strings can be appended.31* Combines elements of Java Appendable and ICU4C ByteSink.32*33* This class can be used in APIs where it does not matter whether the actual destination is34* a UnicodeString, a UChar[] array, a UnicodeSet, or any other object35* that receives and processes characters and/or strings.36*37* Implementation classes must implement at least appendCodeUnit(UChar).38* The base class provides default implementations for the other methods.39*40* The methods do not take UErrorCode parameters.41* If an error occurs (e.g., out-of-memory),42* in addition to returning FALSE from failing operations,43* the implementation must prevent unexpected behavior (e.g., crashes)44* from further calls and should make the error condition available separately45* (e.g., store a UErrorCode, make/keep a UnicodeString bogus).46* @stable ICU 4.847*/48class U_COMMON_API Appendable : public UObject {49public:50/**51* Destructor.52* @stable ICU 4.853*/54~Appendable();5556/**57* Appends a 16-bit code unit.58* @param c code unit59* @return TRUE if the operation succeeded60* @stable ICU 4.861*/62virtual UBool appendCodeUnit(UChar c) = 0;6364/**65* Appends a code point.66* The default implementation calls appendCodeUnit(UChar) once or twice.67* @param c code point 0..0x10ffff68* @return TRUE if the operation succeeded69* @stable ICU 4.870*/71virtual UBool appendCodePoint(UChar32 c);7273/**74* Appends a string.75* The default implementation calls appendCodeUnit(UChar) for each code unit.76* @param s string, must not be NULL if length!=077* @param length string length, or -1 if NUL-terminated78* @return TRUE if the operation succeeded79* @stable ICU 4.880*/81virtual UBool appendString(const UChar *s, int32_t length);8283/**84* Tells the object that the caller is going to append roughly85* appendCapacity UChars. A subclass might use this to pre-allocate86* a larger buffer if necessary.87* The default implementation does nothing. (It always returns TRUE.)88* @param appendCapacity estimated number of UChars that will be appended89* @return TRUE if the operation succeeded90* @stable ICU 4.891*/92virtual UBool reserveAppendCapacity(int32_t appendCapacity);9394/**95* Returns a writable buffer for appending and writes the buffer's capacity to96* *resultCapacity. Guarantees *resultCapacity>=minCapacity.97* May return a pointer to the caller-owned scratch buffer which must have98* scratchCapacity>=minCapacity.99* The returned buffer is only valid until the next operation100* on this Appendable.101*102* After writing at most *resultCapacity UChars, call appendString() with the103* pointer returned from this function and the number of UChars written.104* Many appendString() implementations will avoid copying UChars if this function105* returned an internal buffer.106*107* Partial usage example:108* \code109* int32_t capacity;110* UChar* buffer = app.getAppendBuffer(..., &capacity);111* ... Write n UChars into buffer, with n <= capacity.112* app.appendString(buffer, n);113* \endcode114* In many implementations, that call to append will avoid copying UChars.115*116* If the Appendable allocates or reallocates an internal buffer, it should use117* the desiredCapacityHint if appropriate.118* If a caller cannot provide a reasonable guess at the desired capacity,119* it should pass desiredCapacityHint=0.120*121* If a non-scratch buffer is returned, the caller may only pass122* a prefix to it to appendString().123* That is, it is not correct to pass an interior pointer to appendString().124*125* The default implementation always returns the scratch buffer.126*127* @param minCapacity required minimum capacity of the returned buffer;128* must be non-negative129* @param desiredCapacityHint desired capacity of the returned buffer;130* must be non-negative131* @param scratch default caller-owned buffer132* @param scratchCapacity capacity of the scratch buffer133* @param resultCapacity pointer to an integer which will be set to the134* capacity of the returned buffer135* @return a buffer with *resultCapacity>=minCapacity136* @stable ICU 4.8137*/138virtual UChar *getAppendBuffer(int32_t minCapacity,139int32_t desiredCapacityHint,140UChar *scratch, int32_t scratchCapacity,141int32_t *resultCapacity);142};143144/**145* An Appendable implementation which writes to a UnicodeString.146*147* This class is not intended for public subclassing.148* @stable ICU 4.8149*/150class U_COMMON_API UnicodeStringAppendable : public Appendable {151public:152/**153* Aliases the UnicodeString (keeps its reference) for writing.154* @param s The UnicodeString to which this Appendable will write.155* @stable ICU 4.8156*/157explicit UnicodeStringAppendable(UnicodeString &s) : str(s) {}158159/**160* Destructor.161* @stable ICU 4.8162*/163~UnicodeStringAppendable();164165/**166* Appends a 16-bit code unit to the string.167* @param c code unit168* @return TRUE if the operation succeeded169* @stable ICU 4.8170*/171virtual UBool appendCodeUnit(UChar c);172173/**174* Appends a code point to the string.175* @param c code point 0..0x10ffff176* @return TRUE if the operation succeeded177* @stable ICU 4.8178*/179virtual UBool appendCodePoint(UChar32 c);180181/**182* Appends a string to the UnicodeString.183* @param s string, must not be NULL if length!=0184* @param length string length, or -1 if NUL-terminated185* @return TRUE if the operation succeeded186* @stable ICU 4.8187*/188virtual UBool appendString(const UChar *s, int32_t length);189190/**191* Tells the UnicodeString that the caller is going to append roughly192* appendCapacity UChars.193* @param appendCapacity estimated number of UChars that will be appended194* @return TRUE if the operation succeeded195* @stable ICU 4.8196*/197virtual UBool reserveAppendCapacity(int32_t appendCapacity);198199/**200* Returns a writable buffer for appending and writes the buffer's capacity to201* *resultCapacity. Guarantees *resultCapacity>=minCapacity.202* May return a pointer to the caller-owned scratch buffer which must have203* scratchCapacity>=minCapacity.204* The returned buffer is only valid until the next write operation205* on the UnicodeString.206*207* For details see Appendable::getAppendBuffer().208*209* @param minCapacity required minimum capacity of the returned buffer;210* must be non-negative211* @param desiredCapacityHint desired capacity of the returned buffer;212* must be non-negative213* @param scratch default caller-owned buffer214* @param scratchCapacity capacity of the scratch buffer215* @param resultCapacity pointer to an integer which will be set to the216* capacity of the returned buffer217* @return a buffer with *resultCapacity>=minCapacity218* @stable ICU 4.8219*/220virtual UChar *getAppendBuffer(int32_t minCapacity,221int32_t desiredCapacityHint,222UChar *scratch, int32_t scratchCapacity,223int32_t *resultCapacity);224225private:226UnicodeString &str;227};228229U_NAMESPACE_END230231#endif // __APPENDABLE_H__232233234