appendable.h 8.5 KB

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