bytestream.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #ifndef U_HIDE_DRAFT_API
  65. /**
  66. * Appends n bytes to this. Same as Append().
  67. * Call AppendU8() with u8"string literals" which are const char * in C++11
  68. * but const char8_t * in C++20.
  69. * If the compiler does support char8_t as a distinct type,
  70. * then an AppendU8() overload for that is defined and will be chosen.
  71. *
  72. * @param bytes the pointer to the bytes
  73. * @param n the number of bytes; must be non-negative
  74. * @draft ICU 67
  75. */
  76. inline void AppendU8(const char* bytes, int32_t n) {
  77. Append(bytes, n);
  78. }
  79. #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
  80. /**
  81. * Appends n bytes to this. Same as Append() but for a const char8_t * pointer.
  82. * Call AppendU8() with u8"string literals" which are const char * in C++11
  83. * but const char8_t * in C++20.
  84. * If the compiler does support char8_t as a distinct type,
  85. * then this AppendU8() overload for that is defined and will be chosen.
  86. *
  87. * @param bytes the pointer to the bytes
  88. * @param n the number of bytes; must be non-negative
  89. * @draft ICU 67
  90. */
  91. inline void AppendU8(const char8_t* bytes, int32_t n) {
  92. Append(reinterpret_cast<const char*>(bytes), n);
  93. }
  94. #endif
  95. #endif // U_HIDE_DRAFT_API
  96. /**
  97. * Returns a writable buffer for appending and writes the buffer's capacity to
  98. * *result_capacity. Guarantees *result_capacity>=min_capacity.
  99. * May return a pointer to the caller-owned scratch buffer which must have
  100. * scratch_capacity>=min_capacity.
  101. * The returned buffer is only valid until the next operation
  102. * on this ByteSink.
  103. *
  104. * After writing at most *result_capacity bytes, call Append() with the
  105. * pointer returned from this function and the number of bytes written.
  106. * Many Append() implementations will avoid copying bytes if this function
  107. * returned an internal buffer.
  108. *
  109. * Partial usage example:
  110. * int32_t capacity;
  111. * char* buffer = sink->GetAppendBuffer(..., &capacity);
  112. * ... Write n bytes into buffer, with n <= capacity.
  113. * sink->Append(buffer, n);
  114. * In many implementations, that call to Append will avoid copying bytes.
  115. *
  116. * If the ByteSink allocates or reallocates an internal buffer, it should use
  117. * the desired_capacity_hint if appropriate.
  118. * If a caller cannot provide a reasonable guess at the desired capacity,
  119. * it should pass desired_capacity_hint=0.
  120. *
  121. * If a non-scratch buffer is returned, the caller may only pass
  122. * a prefix to it to Append().
  123. * That is, it is not correct to pass an interior pointer to Append().
  124. *
  125. * The default implementation always returns the scratch buffer.
  126. *
  127. * @param min_capacity required minimum capacity of the returned buffer;
  128. * must be non-negative
  129. * @param desired_capacity_hint desired capacity of the returned buffer;
  130. * must be non-negative
  131. * @param scratch default caller-owned buffer
  132. * @param scratch_capacity capacity of the scratch buffer
  133. * @param result_capacity pointer to an integer which will be set to the
  134. * capacity of the returned buffer
  135. * @return a buffer with *result_capacity>=min_capacity
  136. * @stable ICU 4.2
  137. */
  138. virtual char* GetAppendBuffer(int32_t min_capacity,
  139. int32_t desired_capacity_hint,
  140. char* scratch, int32_t scratch_capacity,
  141. int32_t* result_capacity);
  142. /**
  143. * Flush internal buffers.
  144. * Some byte sinks use internal buffers or provide buffering
  145. * and require calling Flush() at the end of the stream.
  146. * The ByteSink should be ready for further Append() calls after Flush().
  147. * The default implementation of Flush() does nothing.
  148. * @stable ICU 4.2
  149. */
  150. virtual void Flush();
  151. private:
  152. ByteSink(const ByteSink &) = delete;
  153. ByteSink &operator=(const ByteSink &) = delete;
  154. };
  155. // -------------------------------------------------------------
  156. // Some standard implementations
  157. /**
  158. * Implementation of ByteSink that writes to a flat byte array,
  159. * with bounds-checking:
  160. * This sink will not write more than capacity bytes to outbuf.
  161. * If more than capacity bytes are Append()ed, then excess bytes are ignored,
  162. * and Overflowed() will return true.
  163. * Overflow does not cause a runtime error.
  164. * @stable ICU 4.2
  165. */
  166. class U_COMMON_API CheckedArrayByteSink : public ByteSink {
  167. public:
  168. /**
  169. * Constructs a ByteSink that will write to outbuf[0..capacity-1].
  170. * @param outbuf buffer to write to
  171. * @param capacity size of the buffer
  172. * @stable ICU 4.2
  173. */
  174. CheckedArrayByteSink(char* outbuf, int32_t capacity);
  175. /**
  176. * Destructor.
  177. * @stable ICU 4.2
  178. */
  179. virtual ~CheckedArrayByteSink();
  180. /**
  181. * Returns the sink to its original state, without modifying the buffer.
  182. * Useful for reusing both the buffer and the sink for multiple streams.
  183. * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
  184. * and Overflowed()=FALSE.
  185. * @return *this
  186. * @stable ICU 4.6
  187. */
  188. virtual CheckedArrayByteSink& Reset();
  189. /**
  190. * Append "bytes[0,n-1]" to this.
  191. * @param bytes the pointer to the bytes
  192. * @param n the number of bytes; must be non-negative
  193. * @stable ICU 4.2
  194. */
  195. virtual void Append(const char* bytes, int32_t n);
  196. /**
  197. * Returns a writable buffer for appending and writes the buffer's capacity to
  198. * *result_capacity. For details see the base class documentation.
  199. * @param min_capacity required minimum capacity of the returned buffer;
  200. * must be non-negative
  201. * @param desired_capacity_hint desired capacity of the returned buffer;
  202. * must be non-negative
  203. * @param scratch default caller-owned buffer
  204. * @param scratch_capacity capacity of the scratch buffer
  205. * @param result_capacity pointer to an integer which will be set to the
  206. * capacity of the returned buffer
  207. * @return a buffer with *result_capacity>=min_capacity
  208. * @stable ICU 4.2
  209. */
  210. virtual char* GetAppendBuffer(int32_t min_capacity,
  211. int32_t desired_capacity_hint,
  212. char* scratch, int32_t scratch_capacity,
  213. int32_t* result_capacity);
  214. /**
  215. * Returns the number of bytes actually written to the sink.
  216. * @return number of bytes written to the buffer
  217. * @stable ICU 4.2
  218. */
  219. int32_t NumberOfBytesWritten() const { return size_; }
  220. /**
  221. * Returns true if any bytes were discarded, i.e., if there was an
  222. * attempt to write more than 'capacity' bytes.
  223. * @return TRUE if more than 'capacity' bytes were Append()ed
  224. * @stable ICU 4.2
  225. */
  226. UBool Overflowed() const { return overflowed_; }
  227. /**
  228. * Returns the number of bytes appended to the sink.
  229. * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
  230. * else they return the same number.
  231. * @return number of bytes written to the buffer
  232. * @stable ICU 4.6
  233. */
  234. int32_t NumberOfBytesAppended() const { return appended_; }
  235. private:
  236. char* outbuf_;
  237. const int32_t capacity_;
  238. int32_t size_;
  239. int32_t appended_;
  240. UBool overflowed_;
  241. CheckedArrayByteSink() = delete;
  242. CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
  243. CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
  244. };
  245. /**
  246. * Implementation of ByteSink that writes to a "string".
  247. * The StringClass is usually instantiated with a std::string.
  248. * @stable ICU 4.2
  249. */
  250. template<typename StringClass>
  251. class StringByteSink : public ByteSink {
  252. public:
  253. /**
  254. * Constructs a ByteSink that will append bytes to the dest string.
  255. * @param dest pointer to string object to append to
  256. * @stable ICU 4.2
  257. */
  258. StringByteSink(StringClass* dest) : dest_(dest) { }
  259. /**
  260. * Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
  261. *
  262. * @param dest pointer to string object to append to
  263. * @param initialAppendCapacity capacity beyond dest->length() to be reserve()d
  264. * @stable ICU 60
  265. */
  266. StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
  267. if (initialAppendCapacity > 0 &&
  268. (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
  269. dest->reserve(dest->length() + initialAppendCapacity);
  270. }
  271. }
  272. /**
  273. * Append "bytes[0,n-1]" to this.
  274. * @param data the pointer to the bytes
  275. * @param n the number of bytes; must be non-negative
  276. * @stable ICU 4.2
  277. */
  278. virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
  279. private:
  280. StringClass* dest_;
  281. StringByteSink() = delete;
  282. StringByteSink(const StringByteSink &) = delete;
  283. StringByteSink &operator=(const StringByteSink &) = delete;
  284. };
  285. U_NAMESPACE_END
  286. #endif /* U_SHOW_CPLUSPLUS_API */
  287. #endif // __BYTESTREAM_H__