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