bytestream.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // Copyright (C) 2009-2012, International Business Machines
  4. // Corporation and others. All Rights Reserved.
  5. //
  6. // Copyright 2007 Google Inc. All Rights Reserved.
  7. // Author: sanjay@google.com (Sanjay Ghemawat)
  8. //
  9. // Abstract interface that consumes a sequence of bytes (ByteSink).
  10. //
  11. // Used so that we can write a single piece of code that can operate
  12. // on a variety of output string types.
  13. //
  14. // Various implementations of this interface are provided:
  15. // ByteSink:
  16. // CheckedArrayByteSink Write to a flat array, with bounds checking
  17. // StringByteSink Write to an STL string
  18. // This code is a contribution of Google code, and the style used here is
  19. // a compromise between the original Google code and the ICU coding guidelines.
  20. // For example, data types are ICU-ified (size_t,int->int32_t),
  21. // and API comments doxygen-ified, but function names and behavior are
  22. // as in the original, if possible.
  23. // Assertion-style error handling, not available in ICU, was changed to
  24. // parameter "pinning" similar to UnicodeString.
  25. //
  26. // In addition, this is only a partial port of the original Google code,
  27. // limited to what was needed so far. The (nearly) complete original code
  28. // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
  29. // (see ICU ticket 6765, r25517).
  30. #ifndef __BYTESTREAM_H__
  31. #define __BYTESTREAM_H__
  32. /**
  33. * \file
  34. * \brief C++ API: Interface for writing bytes, and implementation classes.
  35. */
  36. #include "unicode/utypes.h"
  37. #if U_SHOW_CPLUSPLUS_API
  38. #include "unicode/uobject.h"
  39. #include "unicode/std_string.h"
  40. U_NAMESPACE_BEGIN
  41. /**
  42. * A ByteSink can be filled with bytes.
  43. * @stable ICU 4.2
  44. */
  45. class U_COMMON_API ByteSink : public UMemory {
  46. public:
  47. /**
  48. * Default constructor.
  49. * @stable ICU 4.2
  50. */
  51. ByteSink() { }
  52. /**
  53. * Virtual destructor.
  54. * @stable ICU 4.2
  55. */
  56. virtual ~ByteSink();
  57. /**
  58. * Append "bytes[0,n-1]" to this.
  59. * @param bytes the pointer to the bytes
  60. * @param n the number of bytes; must be non-negative
  61. * @stable ICU 4.2
  62. */
  63. virtual void Append(const char* bytes, int32_t n) = 0;
  64. /**
  65. * Appends n bytes to this. Same as Append().
  66. * Call AppendU8() with u8"string literals" which are const char * in C++11
  67. * but const char8_t * in C++20.
  68. * If the compiler does support char8_t as a distinct type,
  69. * then an AppendU8() overload for that is defined and will be chosen.
  70. *
  71. * @param bytes the pointer to the bytes
  72. * @param n the number of bytes; must be non-negative
  73. * @stable ICU 67
  74. */
  75. inline void AppendU8(const char* bytes, int32_t n) {
  76. Append(bytes, n);
  77. }
  78. #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
  79. /**
  80. * Appends n bytes to this. Same as Append() but for a const char8_t * pointer.
  81. * Call AppendU8() with u8"string literals" which are const char * in C++11
  82. * but const char8_t * in C++20.
  83. * If the compiler does support char8_t as a distinct type,
  84. * then this AppendU8() overload for that is defined and will be chosen.
  85. *
  86. * @param bytes the pointer to the bytes
  87. * @param n the number of bytes; must be non-negative
  88. * @stable ICU 67
  89. */
  90. inline void AppendU8(const char8_t* bytes, int32_t n) {
  91. Append(reinterpret_cast<const char*>(bytes), n);
  92. }
  93. #endif
  94. /**
  95. * Returns a writable buffer for appending and writes the buffer's capacity to
  96. * *result_capacity. Guarantees *result_capacity>=min_capacity.
  97. * May return a pointer to the caller-owned scratch buffer which must have
  98. * scratch_capacity>=min_capacity.
  99. * The returned buffer is only valid until the next operation
  100. * on this ByteSink.
  101. *
  102. * After writing at most *result_capacity bytes, call Append() with the
  103. * pointer returned from this function and the number of bytes written.
  104. * Many Append() implementations will avoid copying bytes if this function
  105. * returned an internal buffer.
  106. *
  107. * Partial usage example:
  108. * int32_t capacity;
  109. * char* buffer = sink->GetAppendBuffer(..., &capacity);
  110. * ... Write n bytes into buffer, with n <= capacity.
  111. * sink->Append(buffer, n);
  112. * In many implementations, that call to Append will avoid copying bytes.
  113. *
  114. * If the ByteSink allocates or reallocates an internal buffer, it should use
  115. * the desired_capacity_hint if appropriate.
  116. * If a caller cannot provide a reasonable guess at the desired capacity,
  117. * it should pass desired_capacity_hint=0.
  118. *
  119. * If a non-scratch buffer is returned, the caller may only pass
  120. * a prefix to it to Append().
  121. * That is, it is not correct to pass an interior pointer to Append().
  122. *
  123. * The default implementation always returns the scratch buffer.
  124. *
  125. * @param min_capacity required minimum capacity of the returned buffer;
  126. * must be non-negative
  127. * @param desired_capacity_hint desired capacity of the returned buffer;
  128. * must be non-negative
  129. * @param scratch default caller-owned buffer
  130. * @param scratch_capacity capacity of the scratch buffer
  131. * @param result_capacity pointer to an integer which will be set to the
  132. * capacity of the returned buffer
  133. * @return a buffer with *result_capacity>=min_capacity
  134. * @stable ICU 4.2
  135. */
  136. virtual char* GetAppendBuffer(int32_t min_capacity,
  137. int32_t desired_capacity_hint,
  138. char* scratch, int32_t scratch_capacity,
  139. int32_t* result_capacity);
  140. /**
  141. * Flush internal buffers.
  142. * Some byte sinks use internal buffers or provide buffering
  143. * and require calling Flush() at the end of the stream.
  144. * The ByteSink should be ready for further Append() calls after Flush().
  145. * The default implementation of Flush() does nothing.
  146. * @stable ICU 4.2
  147. */
  148. virtual void Flush();
  149. private:
  150. ByteSink(const ByteSink &) = delete;
  151. ByteSink &operator=(const ByteSink &) = delete;
  152. };
  153. // -------------------------------------------------------------
  154. // Some standard implementations
  155. /**
  156. * Implementation of ByteSink that writes to a flat byte array,
  157. * with bounds-checking:
  158. * This sink will not write more than capacity bytes to outbuf.
  159. * If more than capacity bytes are Append()ed, then excess bytes are ignored,
  160. * and Overflowed() will return true.
  161. * Overflow does not cause a runtime error.
  162. * @stable ICU 4.2
  163. */
  164. class U_COMMON_API CheckedArrayByteSink : public ByteSink {
  165. public:
  166. /**
  167. * Constructs a ByteSink that will write to outbuf[0..capacity-1].
  168. * @param outbuf buffer to write to
  169. * @param capacity size of the buffer
  170. * @stable ICU 4.2
  171. */
  172. CheckedArrayByteSink(char* outbuf, int32_t capacity);
  173. /**
  174. * Destructor.
  175. * @stable ICU 4.2
  176. */
  177. virtual ~CheckedArrayByteSink();
  178. /**
  179. * Returns the sink to its original state, without modifying the buffer.
  180. * Useful for reusing both the buffer and the sink for multiple streams.
  181. * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
  182. * and Overflowed()=false.
  183. * @return *this
  184. * @stable ICU 4.6
  185. */
  186. virtual CheckedArrayByteSink& Reset();
  187. /**
  188. * Append "bytes[0,n-1]" to this.
  189. * @param bytes the pointer to the bytes
  190. * @param n the number of bytes; must be non-negative
  191. * @stable ICU 4.2
  192. */
  193. virtual void Append(const char* bytes, int32_t n) override;
  194. /**
  195. * Returns a writable buffer for appending and writes the buffer's capacity to
  196. * *result_capacity. For details see the base class documentation.
  197. * @param min_capacity required minimum capacity of the returned buffer;
  198. * must be non-negative
  199. * @param desired_capacity_hint desired capacity of the returned buffer;
  200. * must be non-negative
  201. * @param scratch default caller-owned buffer
  202. * @param scratch_capacity capacity of the scratch buffer
  203. * @param result_capacity pointer to an integer which will be set to the
  204. * capacity of the returned buffer
  205. * @return a buffer with *result_capacity>=min_capacity
  206. * @stable ICU 4.2
  207. */
  208. virtual char* GetAppendBuffer(int32_t min_capacity,
  209. int32_t desired_capacity_hint,
  210. char* scratch, int32_t scratch_capacity,
  211. int32_t* result_capacity) override;
  212. /**
  213. * Returns the number of bytes actually written to the sink.
  214. * @return number of bytes written to the buffer
  215. * @stable ICU 4.2
  216. */
  217. int32_t NumberOfBytesWritten() const { return size_; }
  218. /**
  219. * Returns true if any bytes were discarded, i.e., if there was an
  220. * attempt to write more than 'capacity' bytes.
  221. * @return true if more than 'capacity' bytes were Append()ed
  222. * @stable ICU 4.2
  223. */
  224. UBool Overflowed() const { return overflowed_; }
  225. /**
  226. * Returns the number of bytes appended to the sink.
  227. * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
  228. * else they return the same number.
  229. * @return number of bytes written to the buffer
  230. * @stable ICU 4.6
  231. */
  232. int32_t NumberOfBytesAppended() const { return appended_; }
  233. private:
  234. char* outbuf_;
  235. const int32_t capacity_;
  236. int32_t size_;
  237. int32_t appended_;
  238. UBool overflowed_;
  239. CheckedArrayByteSink() = delete;
  240. CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
  241. CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
  242. };
  243. /**
  244. * Implementation of ByteSink that writes to a "string".
  245. * The StringClass is usually instantiated with a std::string.
  246. * @stable ICU 4.2
  247. */
  248. template<typename StringClass>
  249. class StringByteSink : public ByteSink {
  250. public:
  251. /**
  252. * Constructs a ByteSink that will append bytes to the dest string.
  253. * @param dest pointer to string object to append to
  254. * @stable ICU 4.2
  255. */
  256. StringByteSink(StringClass* dest) : dest_(dest) { }
  257. /**
  258. * Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
  259. *
  260. * @param dest pointer to string object to append to
  261. * @param initialAppendCapacity capacity beyond dest->length() to be reserve()d
  262. * @stable ICU 60
  263. */
  264. StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
  265. if (initialAppendCapacity > 0 &&
  266. (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
  267. dest->reserve(dest->length() + initialAppendCapacity);
  268. }
  269. }
  270. /**
  271. * Append "bytes[0,n-1]" to this.
  272. * @param data the pointer to the bytes
  273. * @param n the number of bytes; must be non-negative
  274. * @stable ICU 4.2
  275. */
  276. virtual void Append(const char* data, int32_t n) override { dest_->append(data, n); }
  277. private:
  278. StringClass* dest_;
  279. StringByteSink() = delete;
  280. StringByteSink(const StringByteSink &) = delete;
  281. StringByteSink &operator=(const StringByteSink &) = delete;
  282. };
  283. U_NAMESPACE_END
  284. #endif /* U_SHOW_CPLUSPLUS_API */
  285. #endif // __BYTESTREAM_H__