ICU 69.1  69.1
bytestream.h
Go to the documentation of this file.
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 
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 
47 U_NAMESPACE_BEGIN
48 
53 class U_COMMON_API ByteSink : public UMemory {
54 public:
59  ByteSink() { }
64  virtual ~ByteSink();
65 
72  virtual void Append(const char* bytes, int32_t n) = 0;
73 
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)
101  inline void AppendU8(const char8_t* bytes, int32_t n) {
102  Append(reinterpret_cast<const char*>(bytes), n);
103  }
104 #endif
105 
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 
161  virtual void Flush();
162 
163 private:
164  ByteSink(const ByteSink &) = delete;
165  ByteSink &operator=(const ByteSink &) = delete;
166 };
167 
168 // -------------------------------------------------------------
169 // Some standard implementations
170 
181 public:
188  CheckedArrayByteSink(char* outbuf, int32_t capacity);
209  virtual void Append(const char* bytes, int32_t n);
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);
233  int32_t NumberOfBytesWritten() const { return size_; }
240  UBool Overflowed() const { return overflowed_; }
248  int32_t NumberOfBytesAppended() const { return appended_; }
249 private:
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 
266 template<typename StringClass>
267 class StringByteSink : public ByteSink {
268  public:
274  StringByteSink(StringClass* dest) : dest_(dest) { }
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  }
294  virtual void Append(const char* data, int32_t n) { 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 
303 U_NAMESPACE_END
304 
305 #endif /* U_SHOW_CPLUSPLUS_API */
306 
307 #endif // __BYTESTREAM_H__
A ByteSink can be filled with bytes.
Definition: bytestream.h:53
void AppendU8(const char *bytes, int32_t n)
Appends n bytes to this.
Definition: bytestream.h:85
virtual char * GetAppendBuffer(int32_t min_capacity, int32_t desired_capacity_hint, char *scratch, int32_t scratch_capacity, int32_t *result_capacity)
Returns a writable buffer for appending and writes the buffer's capacity to *result_capacity.
virtual void Append(const char *bytes, int32_t n)=0
Append "bytes[0,n-1]" to this.
ByteSink()
Default constructor.
Definition: bytestream.h:59
void AppendU8(const char8_t *bytes, int32_t n)
Appends n bytes to this.
Definition: bytestream.h:101
virtual void Flush()
Flush internal buffers.
virtual ~ByteSink()
Virtual destructor.
Implementation of ByteSink that writes to a flat byte array, with bounds-checking: This sink will not...
Definition: bytestream.h:180
int32_t NumberOfBytesWritten() const
Returns the number of bytes actually written to the sink.
Definition: bytestream.h:233
virtual char * GetAppendBuffer(int32_t min_capacity, int32_t desired_capacity_hint, char *scratch, int32_t scratch_capacity, int32_t *result_capacity)
Returns a writable buffer for appending and writes the buffer's capacity to *result_capacity.
CheckedArrayByteSink(char *outbuf, int32_t capacity)
Constructs a ByteSink that will write to outbuf[0..capacity-1].
virtual ~CheckedArrayByteSink()
Destructor.
int32_t NumberOfBytesAppended() const
Returns the number of bytes appended to the sink.
Definition: bytestream.h:248
virtual void Append(const char *bytes, int32_t n)
Append "bytes[0,n-1]" to this.
UBool Overflowed() const
Returns true if any bytes were discarded, i.e., if there was an attempt to write more than 'capacity'...
Definition: bytestream.h:240
virtual CheckedArrayByteSink & Reset()
Returns the sink to its original state, without modifying the buffer.
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:267
StringByteSink(StringClass *dest, int32_t initialAppendCapacity)
Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
Definition: bytestream.h:282
virtual void Append(const char *data, int32_t n)
Append "bytes[0,n-1]" to this.
Definition: bytestream.h:294
StringByteSink(StringClass *dest)
Constructs a ByteSink that will append bytes to the dest string.
Definition: bytestream.h:274
UMemory is the common ICU base class.
Definition: uobject.h:115
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
C++ API: Common ICU base class UObject.
Basic definitions for ICU, for both C and C++ APIs.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:300