intl/icu/source/common/unicode/appendable.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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

mercurial