Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/appendable.h
48773 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4* Copyright (C) 2011-2012, International Business Machines5* Corporation and others. All Rights Reserved.6*******************************************************************************7* file name: appendable.h8* encoding: UTF-89* tab size: 8 (not used)10* indentation:411*12* created on: 2010dec0713* created by: Markus W. Scherer14*/1516#ifndef __APPENDABLE_H__17#define __APPENDABLE_H__1819/**20* \file21* \brief C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).22*/2324#include "unicode/utypes.h"25#include "unicode/uobject.h"2627U_NAMESPACE_BEGIN2829class UnicodeString;3031/**32* Base class for objects to which Unicode characters and strings can be appended.33* Combines elements of Java Appendable and ICU4C ByteSink.34*35* This class can be used in APIs where it does not matter whether the actual destination is36* a UnicodeString, a char16_t[] array, a UnicodeSet, or any other object37* that receives and processes characters and/or strings.38*39* Implementation classes must implement at least appendCodeUnit(char16_t).40* The base class provides default implementations for the other methods.41*42* The methods do not take UErrorCode parameters.43* If an error occurs (e.g., out-of-memory),44* in addition to returning FALSE from failing operations,45* the implementation must prevent unexpected behavior (e.g., crashes)46* from further calls and should make the error condition available separately47* (e.g., store a UErrorCode, make/keep a UnicodeString bogus).48* @stable ICU 4.849*/50class U_COMMON_API Appendable : public UObject {51public:52/**53* Destructor.54* @stable ICU 4.855*/56~Appendable();5758/**59* Appends a 16-bit code unit.60* @param c code unit61* @return TRUE if the operation succeeded62* @stable ICU 4.863*/64virtual UBool appendCodeUnit(char16_t c) = 0;6566/**67* Appends a code point.68* The default implementation calls appendCodeUnit(char16_t) once or twice.69* @param c code point 0..0x10ffff70* @return TRUE if the operation succeeded71* @stable ICU 4.872*/73virtual UBool appendCodePoint(UChar32 c);7475/**76* Appends a string.77* The default implementation calls appendCodeUnit(char16_t) for each code unit.78* @param s string, must not be NULL if length!=079* @param length string length, or -1 if NUL-terminated80* @return TRUE if the operation succeeded81* @stable ICU 4.882*/83virtual UBool appendString(const char16_t *s, int32_t length);8485/**86* Tells the object that the caller is going to append roughly87* appendCapacity char16_ts. A subclass might use this to pre-allocate88* a larger buffer if necessary.89* The default implementation does nothing. (It always returns TRUE.)90* @param appendCapacity estimated number of char16_ts that will be appended91* @return TRUE if the operation succeeded92* @stable ICU 4.893*/94virtual UBool reserveAppendCapacity(int32_t appendCapacity);9596/**97* Returns a writable buffer for appending and writes the buffer's capacity to98* *resultCapacity. Guarantees *resultCapacity>=minCapacity.99* May return a pointer to the caller-owned scratch buffer which must have100* scratchCapacity>=minCapacity.101* The returned buffer is only valid until the next operation102* on this Appendable.103*104* After writing at most *resultCapacity char16_ts, call appendString() with the105* pointer returned from this function and the number of char16_ts written.106* Many appendString() implementations will avoid copying char16_ts if this function107* returned an internal buffer.108*109* Partial usage example:110* \code111* int32_t capacity;112* char16_t* buffer = app.getAppendBuffer(..., &capacity);113* ... Write n char16_ts into buffer, with n <= capacity.114* app.appendString(buffer, n);115* \endcode116* In many implementations, that call to append will avoid copying char16_ts.117*118* If the Appendable allocates or reallocates an internal buffer, it should use119* the desiredCapacityHint if appropriate.120* If a caller cannot provide a reasonable guess at the desired capacity,121* it should pass desiredCapacityHint=0.122*123* If a non-scratch buffer is returned, the caller may only pass124* a prefix to it to appendString().125* That is, it is not correct to pass an interior pointer to appendString().126*127* The default implementation always returns the scratch buffer.128*129* @param minCapacity required minimum capacity of the returned buffer;130* must be non-negative131* @param desiredCapacityHint desired capacity of the returned buffer;132* must be non-negative133* @param scratch default caller-owned buffer134* @param scratchCapacity capacity of the scratch buffer135* @param resultCapacity pointer to an integer which will be set to the136* capacity of the returned buffer137* @return a buffer with *resultCapacity>=minCapacity138* @stable ICU 4.8139*/140virtual char16_t *getAppendBuffer(int32_t minCapacity,141int32_t desiredCapacityHint,142char16_t *scratch, int32_t scratchCapacity,143int32_t *resultCapacity);144};145146/**147* An Appendable implementation which writes to a UnicodeString.148*149* This class is not intended for public subclassing.150* @stable ICU 4.8151*/152class U_COMMON_API UnicodeStringAppendable : public Appendable {153public:154/**155* Aliases the UnicodeString (keeps its reference) for writing.156* @param s The UnicodeString to which this Appendable will write.157* @stable ICU 4.8158*/159explicit UnicodeStringAppendable(UnicodeString &s) : str(s) {}160161/**162* Destructor.163* @stable ICU 4.8164*/165~UnicodeStringAppendable();166167/**168* Appends a 16-bit code unit to the string.169* @param c code unit170* @return TRUE if the operation succeeded171* @stable ICU 4.8172*/173virtual UBool appendCodeUnit(char16_t c);174175/**176* Appends a code point to the string.177* @param c code point 0..0x10ffff178* @return TRUE if the operation succeeded179* @stable ICU 4.8180*/181virtual UBool appendCodePoint(UChar32 c);182183/**184* Appends a string to the UnicodeString.185* @param s string, must not be NULL if length!=0186* @param length string length, or -1 if NUL-terminated187* @return TRUE if the operation succeeded188* @stable ICU 4.8189*/190virtual UBool appendString(const char16_t *s, int32_t length);191192/**193* Tells the UnicodeString that the caller is going to append roughly194* appendCapacity char16_ts.195* @param appendCapacity estimated number of char16_ts that will be appended196* @return TRUE if the operation succeeded197* @stable ICU 4.8198*/199virtual UBool reserveAppendCapacity(int32_t appendCapacity);200201/**202* Returns a writable buffer for appending and writes the buffer's capacity to203* *resultCapacity. Guarantees *resultCapacity>=minCapacity.204* May return a pointer to the caller-owned scratch buffer which must have205* scratchCapacity>=minCapacity.206* The returned buffer is only valid until the next write operation207* on the UnicodeString.208*209* For details see Appendable::getAppendBuffer().210*211* @param minCapacity required minimum capacity of the returned buffer;212* must be non-negative213* @param desiredCapacityHint desired capacity of the returned buffer;214* must be non-negative215* @param scratch default caller-owned buffer216* @param scratchCapacity capacity of the scratch buffer217* @param resultCapacity pointer to an integer which will be set to the218* capacity of the returned buffer219* @return a buffer with *resultCapacity>=minCapacity220* @stable ICU 4.8221*/222virtual char16_t *getAppendBuffer(int32_t minCapacity,223int32_t desiredCapacityHint,224char16_t *scratch, int32_t scratchCapacity,225int32_t *resultCapacity);226227private:228UnicodeString &str;229};230231U_NAMESPACE_END232233#endif // __APPENDABLE_H__234235236