appendable.h 8.4 KB

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