Loading...
Searching...
No Matches
bytestream.h
Go to the documentation of this file.
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
// Copyright (C) 2009-2012, International Business Machines
// Corporation and others. All Rights Reserved.
//
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: sanjay@google.com (Sanjay Ghemawat)
//
// Abstract interface that consumes a sequence of bytes (ByteSink).
//
// Used so that we can write a single piece of code that can operate
// on a variety of output string types.
//
// Various implementations of this interface are provided:
// ByteSink:
// CheckedArrayByteSink Write to a flat array, with bounds checking
// StringByteSink Write to an STL string
// This code is a contribution of Google code, and the style used here is
// a compromise between the original Google code and the ICU coding guidelines.
// For example, data types are ICU-ified (size_t,int->int32_t),
// and API comments doxygen-ified, but function names and behavior are
// as in the original, if possible.
// Assertion-style error handling, not available in ICU, was changed to
// parameter "pinning" similar to UnicodeString.
//
// In addition, this is only a partial port of the original Google code,
// limited to what was needed so far. The (nearly) complete original code
// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
// (see ICU ticket 6765, r25517).
#ifndef __BYTESTREAM_H__
#define __BYTESTREAM_H__
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#include <type_traits>
#include "unicode/uobject.h"
#include "unicode/std_string.h"
U_NAMESPACE_BEGIN
class U_COMMON_API ByteSink : public UMemory {
public:
ByteSink() { }
virtual ~ByteSink();
virtual void Append(const char* bytes, int32_t n) = 0;
inline void AppendU8(const char* bytes, int32_t n) {
Append(bytes, n);
}
#if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
inline void AppendU8(const char8_t* bytes, int32_t n) {
Append(reinterpret_cast<const char*>(bytes), n);
}
#endif
virtual char* GetAppendBuffer(int32_t min_capacity,
int32_t desired_capacity_hint,
char* scratch, int32_t scratch_capacity,
int32_t* result_capacity);
virtual void Flush();
private:
ByteSink(const ByteSink &) = delete;
ByteSink &operator=(const ByteSink &) = delete;
};
// -------------------------------------------------------------
// Some standard implementations
class U_COMMON_API CheckedArrayByteSink : public ByteSink {
public:
CheckedArrayByteSink(char* outbuf, int32_t capacity);
virtual ~CheckedArrayByteSink();
virtual CheckedArrayByteSink& Reset();
virtual void Append(const char* bytes, int32_t n) override;
virtual char* GetAppendBuffer(int32_t min_capacity,
int32_t desired_capacity_hint,
char* scratch, int32_t scratch_capacity,
int32_t* result_capacity) override;
int32_t NumberOfBytesWritten() const { return size_; }
UBool Overflowed() const { return overflowed_; }
int32_t NumberOfBytesAppended() const { return appended_; }
private:
char* outbuf_;
const int32_t capacity_;
int32_t size_;
int32_t appended_;
UBool overflowed_;
CheckedArrayByteSink() = delete;
CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
};
namespace prv {
template<typename StringClass, typename = void>
struct value_type_or_char {
using type = char;
};
template<typename StringClass>
struct value_type_or_char<StringClass, std::void_t<typename StringClass::value_type>> {
using type = typename StringClass::value_type;
};
template<typename StringClass>
using value_type_or_char_t = typename value_type_or_char<StringClass>::type;
}
template<typename StringClass>
class StringByteSink : public ByteSink {
using Unit = typename prv::value_type_or_char_t<StringClass>;
public:
StringByteSink(StringClass* dest) : dest_(dest) { }
StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
if (initialAppendCapacity > 0 &&
static_cast<uint32_t>(initialAppendCapacity) > dest->capacity() - dest->length()) {
dest->reserve(dest->length() + initialAppendCapacity);
}
}
virtual void Append(const char* data, int32_t n) override {
if constexpr (std::is_same_v<Unit, char>) {
dest_->append(data, n);
} else {
dest_->append(reinterpret_cast<const Unit*>(data), n);
}
}
private:
StringClass* dest_;
StringByteSink() = delete;
StringByteSink(const StringByteSink &) = delete;
StringByteSink &operator=(const StringByteSink &) = delete;
};
U_NAMESPACE_END
#endif /* U_SHOW_CPLUSPLUS_API */
#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::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::AppendU8
void AppendU8(const char *bytes, int32_t n)
Appends n bytes to this.
Definition bytestream.h:87
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::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::Reset
virtual CheckedArrayByteSink & Reset()
Returns the sink to its original state, without modifying the buffer.
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::Append
virtual void Append(const char *bytes, int32_t n) override
Append "bytes[0,n-1]" to this.
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::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