ICU 78.2: common/unicode/bytestream.h Source File

ICU 78.2 78.2

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 <type_traits>
45
46 #include "unicode/uobject.h"
47 #include "unicode/std_string.h"
48
49 U_NAMESPACE_BEGIN
50
55 class U_COMMON_API ByteSink : public UMemory {
56 public:
61 ByteSink() { }
66 virtual ~ByteSink();
67
74 virtual void Append(const char* bytes, int32_t n) = 0;
75
87 inline void AppendU8(const char* bytes, int32_t n) {
88 Append(bytes, n);
89 }
90
91 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
103 inline void AppendU8(const char8_t* bytes, int32_t n) {
104 Append(reinterpret_cast<const char*>(bytes), n);
105 }
106 #endif
107
150 virtual char* GetAppendBuffer(int32_t min_capacity,
151 int32_t desired_capacity_hint,
152 char* scratch, int32_t scratch_capacity,
153 int32_t* result_capacity);
154
163 virtual void Flush();
164
165 private:
166 ByteSink(const ByteSink &) = delete;
167 ByteSink &operator=(const ByteSink &) = delete;
168 };
169
170 // -------------------------------------------------------------
171 // Some standard implementations
172
182 class U_COMMON_API CheckedArrayByteSink : public ByteSink {
183 public:
190 CheckedArrayByteSink(char* outbuf, int32_t capacity);
195 virtual ~CheckedArrayByteSink();
204 virtual CheckedArrayByteSink& Reset();
211 virtual void Append(const char* bytes, int32_t n) override;
226 virtual char* GetAppendBuffer(int32_t min_capacity,
227 int32_t desired_capacity_hint,
228 char* scratch, int32_t scratch_capacity,
229 int32_t* result_capacity) override;
235 int32_t NumberOfBytesWritten() const { return size_; }
242 UBool Overflowed() const { return overflowed_; }
250 int32_t NumberOfBytesAppended() const { return appended_; }
251 private:
252 char* outbuf_;
253 const int32_t capacity_;
254 int32_t size_;
255 int32_t appended_;
256 UBool overflowed_;
257
258 CheckedArrayByteSink() = delete;
259 CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
260 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
261 };
262
263 namespace prv {
265 template<typename StringClass, typename = void>
266 struct value_type_or_char {
268 using type = char;
269 };
271 template<typename StringClass>
272 struct value_type_or_char<StringClass, std::void_t<typename StringClass::value_type>> {
274 using type = typename StringClass::value_type;
275 };
277 template<typename StringClass>
278 using value_type_or_char_t = typename value_type_or_char<StringClass>::type;
279 }
280
290 template<typename StringClass>
291 class StringByteSink : public ByteSink {
292 using Unit = typename prv::value_type_or_char_t<StringClass>;
293 public:
299 StringByteSink(StringClass* dest) : dest_(dest) { }
307 StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
308 if (initialAppendCapacity > 0 &&
309 static_cast<uint32_t>(initialAppendCapacity) > dest->capacity() - dest->length()) {
310 dest->reserve(dest->length() + initialAppendCapacity);
311 }
312 }
319 virtual void Append(const char* data, int32_t n) override {
320 if constexpr (std::is_same_v<Unit, char>) {
321 dest_->append(data, n);
322 } else {
323 dest_->append(reinterpret_cast<const Unit*>(data), n);
324 }
325 }
326 private:
327 StringClass* dest_;
328
329 StringByteSink() = delete;
330 StringByteSink(const StringByteSink &) = delete;
331 StringByteSink &operator=(const StringByteSink &) = delete;
332 };
333
334 U_NAMESPACE_END
335
336 #endif /* U_SHOW_CPLUSPLUS_API */
337
338 #endif // __BYTESTREAM_H__
icu::prv::value_type_or_char_t
typename value_type_or_char< StringClass >::type value_type_or_char_t
Definition: bytestream.h:278
icu::ByteSink
A ByteSink can be filled with bytes.
Definition: bytestream.h:55
icu::ByteSink::AppendU8
void AppendU8(const char *bytes, int32_t n)
Appends n bytes to this.
Definition: bytestream.h:87
icu::ByteSink::GetAppendBuffer
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.
icu::ByteSink::Append
virtual void Append(const char *bytes, int32_t n)=0
Append "bytes[0,n-1]" to this.
icu::ByteSink::ByteSink
ByteSink()
Default constructor.
Definition: bytestream.h:61
icu::ByteSink::AppendU8
void AppendU8(const char8_t *bytes, int32_t n)
Appends n bytes to this.
Definition: bytestream.h:103
icu::ByteSink::Flush
virtual void Flush()
Flush internal buffers.
icu::ByteSink::~ByteSink
virtual ~ByteSink()
Virtual destructor.
icu::CheckedArrayByteSink
Implementation of ByteSink that writes to a flat byte array, with bounds-checking: This sink will not...
Definition: bytestream.h:182
icu::CheckedArrayByteSink::NumberOfBytesWritten
int32_t NumberOfBytesWritten() const
Returns the number of bytes actually written to the sink.
Definition: bytestream.h:235
icu::CheckedArrayByteSink::GetAppendBuffer
virtual char * GetAppendBuffer(int32_t min_capacity, int32_t desired_capacity_hint, char *scratch, int32_t scratch_capacity, int32_t *result_capacity) override
Returns a writable buffer for appending and writes the buffer's capacity to *result_capacity.
icu::CheckedArrayByteSink::CheckedArrayByteSink
CheckedArrayByteSink(char *outbuf, int32_t capacity)
Constructs a ByteSink that will write to outbuf[0..capacity-1].
icu::CheckedArrayByteSink::~CheckedArrayByteSink
virtual ~CheckedArrayByteSink()
Destructor.
icu::CheckedArrayByteSink::NumberOfBytesAppended
int32_t NumberOfBytesAppended() const
Returns the number of bytes appended to the sink.
Definition: bytestream.h:250
icu::CheckedArrayByteSink::Overflowed
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:242
icu::CheckedArrayByteSink::Reset
virtual CheckedArrayByteSink & Reset()
Returns the sink to its original state, without modifying the buffer.
icu::CheckedArrayByteSink::Append
virtual void Append(const char *bytes, int32_t n) override
Append "bytes[0,n-1]" to this.
icu::StringByteSink
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:291
icu::StringByteSink::StringByteSink
StringByteSink(StringClass *dest, int32_t initialAppendCapacity)
Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
Definition: bytestream.h:307
icu::StringByteSink::StringByteSink
StringByteSink(StringClass *dest)
Constructs a ByteSink that will append bytes to the dest string.
Definition: bytestream.h:299
icu::StringByteSink::Append
virtual void Append(const char *data, int32_t n) override
Append "bytes[0,n-1]" to this.
Definition: bytestream.h:319
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
std_string.h
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
icu::prv::value_type_or_char< StringClass, std::void_t< typename StringClass::value_type > >::type
typename StringClass::value_type type
Definition: bytestream.h:274
icu::prv::value_type_or_char
Definition: bytestream.h:266
icu::prv::value_type_or_char::type
char type
Definition: bytestream.h:268
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
uobject.h
C++ API: Common ICU base class UObject.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:315