jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: // Copyright (C) 2009-2012, International Business Machines jpayne@69: // Corporation and others. All Rights Reserved. jpayne@69: // jpayne@69: // Copyright 2007 Google Inc. All Rights Reserved. jpayne@69: // Author: sanjay@google.com (Sanjay Ghemawat) jpayne@69: // jpayne@69: // Abstract interface that consumes a sequence of bytes (ByteSink). jpayne@69: // jpayne@69: // Used so that we can write a single piece of code that can operate jpayne@69: // on a variety of output string types. jpayne@69: // jpayne@69: // Various implementations of this interface are provided: jpayne@69: // ByteSink: jpayne@69: // CheckedArrayByteSink Write to a flat array, with bounds checking jpayne@69: // StringByteSink Write to an STL string jpayne@69: jpayne@69: // This code is a contribution of Google code, and the style used here is jpayne@69: // a compromise between the original Google code and the ICU coding guidelines. jpayne@69: // For example, data types are ICU-ified (size_t,int->int32_t), jpayne@69: // and API comments doxygen-ified, but function names and behavior are jpayne@69: // as in the original, if possible. jpayne@69: // Assertion-style error handling, not available in ICU, was changed to jpayne@69: // parameter "pinning" similar to UnicodeString. jpayne@69: // jpayne@69: // In addition, this is only a partial port of the original Google code, jpayne@69: // limited to what was needed so far. The (nearly) complete original code jpayne@69: // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib jpayne@69: // (see ICU ticket 6765, r25517). jpayne@69: jpayne@69: #ifndef __BYTESTREAM_H__ jpayne@69: #define __BYTESTREAM_H__ jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: Interface for writing bytes, and implementation classes. jpayne@69: */ jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: #include "unicode/uobject.h" jpayne@69: #include "unicode/std_string.h" jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: /** jpayne@69: * A ByteSink can be filled with bytes. jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: class U_COMMON_API ByteSink : public UMemory { jpayne@69: public: jpayne@69: /** jpayne@69: * Default constructor. jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: ByteSink() { } jpayne@69: /** jpayne@69: * Virtual destructor. jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: virtual ~ByteSink(); jpayne@69: jpayne@69: /** jpayne@69: * Append "bytes[0,n-1]" to this. jpayne@69: * @param bytes the pointer to the bytes jpayne@69: * @param n the number of bytes; must be non-negative jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: virtual void Append(const char* bytes, int32_t n) = 0; jpayne@69: jpayne@69: #ifndef U_HIDE_DRAFT_API jpayne@69: /** jpayne@69: * Appends n bytes to this. Same as Append(). jpayne@69: * Call AppendU8() with u8"string literals" which are const char * in C++11 jpayne@69: * but const char8_t * in C++20. jpayne@69: * If the compiler does support char8_t as a distinct type, jpayne@69: * then an AppendU8() overload for that is defined and will be chosen. jpayne@69: * jpayne@69: * @param bytes the pointer to the bytes jpayne@69: * @param n the number of bytes; must be non-negative jpayne@69: * @draft ICU 67 jpayne@69: */ jpayne@69: inline void AppendU8(const char* bytes, int32_t n) { jpayne@69: Append(bytes, n); jpayne@69: } jpayne@69: jpayne@69: #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN) jpayne@69: /** jpayne@69: * Appends n bytes to this. Same as Append() but for a const char8_t * pointer. jpayne@69: * Call AppendU8() with u8"string literals" which are const char * in C++11 jpayne@69: * but const char8_t * in C++20. jpayne@69: * If the compiler does support char8_t as a distinct type, jpayne@69: * then this AppendU8() overload for that is defined and will be chosen. jpayne@69: * jpayne@69: * @param bytes the pointer to the bytes jpayne@69: * @param n the number of bytes; must be non-negative jpayne@69: * @draft ICU 67 jpayne@69: */ jpayne@69: inline void AppendU8(const char8_t* bytes, int32_t n) { jpayne@69: Append(reinterpret_cast(bytes), n); jpayne@69: } jpayne@69: #endif jpayne@69: #endif // U_HIDE_DRAFT_API jpayne@69: jpayne@69: /** jpayne@69: * Returns a writable buffer for appending and writes the buffer's capacity to jpayne@69: * *result_capacity. Guarantees *result_capacity>=min_capacity. jpayne@69: * May return a pointer to the caller-owned scratch buffer which must have jpayne@69: * scratch_capacity>=min_capacity. jpayne@69: * The returned buffer is only valid until the next operation jpayne@69: * on this ByteSink. jpayne@69: * jpayne@69: * After writing at most *result_capacity bytes, call Append() with the jpayne@69: * pointer returned from this function and the number of bytes written. jpayne@69: * Many Append() implementations will avoid copying bytes if this function jpayne@69: * returned an internal buffer. jpayne@69: * jpayne@69: * Partial usage example: jpayne@69: * int32_t capacity; jpayne@69: * char* buffer = sink->GetAppendBuffer(..., &capacity); jpayne@69: * ... Write n bytes into buffer, with n <= capacity. jpayne@69: * sink->Append(buffer, n); jpayne@69: * In many implementations, that call to Append will avoid copying bytes. jpayne@69: * jpayne@69: * If the ByteSink allocates or reallocates an internal buffer, it should use jpayne@69: * the desired_capacity_hint if appropriate. jpayne@69: * If a caller cannot provide a reasonable guess at the desired capacity, jpayne@69: * it should pass desired_capacity_hint=0. jpayne@69: * jpayne@69: * If a non-scratch buffer is returned, the caller may only pass jpayne@69: * a prefix to it to Append(). jpayne@69: * That is, it is not correct to pass an interior pointer to Append(). jpayne@69: * jpayne@69: * The default implementation always returns the scratch buffer. jpayne@69: * jpayne@69: * @param min_capacity required minimum capacity of the returned buffer; jpayne@69: * must be non-negative jpayne@69: * @param desired_capacity_hint desired capacity of the returned buffer; jpayne@69: * must be non-negative jpayne@69: * @param scratch default caller-owned buffer jpayne@69: * @param scratch_capacity capacity of the scratch buffer jpayne@69: * @param result_capacity pointer to an integer which will be set to the jpayne@69: * capacity of the returned buffer jpayne@69: * @return a buffer with *result_capacity>=min_capacity jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: virtual char* GetAppendBuffer(int32_t min_capacity, jpayne@69: int32_t desired_capacity_hint, jpayne@69: char* scratch, int32_t scratch_capacity, jpayne@69: int32_t* result_capacity); jpayne@69: jpayne@69: /** jpayne@69: * Flush internal buffers. jpayne@69: * Some byte sinks use internal buffers or provide buffering jpayne@69: * and require calling Flush() at the end of the stream. jpayne@69: * The ByteSink should be ready for further Append() calls after Flush(). jpayne@69: * The default implementation of Flush() does nothing. jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: virtual void Flush(); jpayne@69: jpayne@69: private: jpayne@69: ByteSink(const ByteSink &) = delete; jpayne@69: ByteSink &operator=(const ByteSink &) = delete; jpayne@69: }; jpayne@69: jpayne@69: // ------------------------------------------------------------- jpayne@69: // Some standard implementations jpayne@69: jpayne@69: /** jpayne@69: * Implementation of ByteSink that writes to a flat byte array, jpayne@69: * with bounds-checking: jpayne@69: * This sink will not write more than capacity bytes to outbuf. jpayne@69: * If more than capacity bytes are Append()ed, then excess bytes are ignored, jpayne@69: * and Overflowed() will return true. jpayne@69: * Overflow does not cause a runtime error. jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: class U_COMMON_API CheckedArrayByteSink : public ByteSink { jpayne@69: public: jpayne@69: /** jpayne@69: * Constructs a ByteSink that will write to outbuf[0..capacity-1]. jpayne@69: * @param outbuf buffer to write to jpayne@69: * @param capacity size of the buffer jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: CheckedArrayByteSink(char* outbuf, int32_t capacity); jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: virtual ~CheckedArrayByteSink(); jpayne@69: /** jpayne@69: * Returns the sink to its original state, without modifying the buffer. jpayne@69: * Useful for reusing both the buffer and the sink for multiple streams. jpayne@69: * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0 jpayne@69: * and Overflowed()=FALSE. jpayne@69: * @return *this jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: virtual CheckedArrayByteSink& Reset(); jpayne@69: /** jpayne@69: * Append "bytes[0,n-1]" to this. jpayne@69: * @param bytes the pointer to the bytes jpayne@69: * @param n the number of bytes; must be non-negative jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: virtual void Append(const char* bytes, int32_t n); jpayne@69: /** jpayne@69: * Returns a writable buffer for appending and writes the buffer's capacity to jpayne@69: * *result_capacity. For details see the base class documentation. jpayne@69: * @param min_capacity required minimum capacity of the returned buffer; jpayne@69: * must be non-negative jpayne@69: * @param desired_capacity_hint desired capacity of the returned buffer; jpayne@69: * must be non-negative jpayne@69: * @param scratch default caller-owned buffer jpayne@69: * @param scratch_capacity capacity of the scratch buffer jpayne@69: * @param result_capacity pointer to an integer which will be set to the jpayne@69: * capacity of the returned buffer jpayne@69: * @return a buffer with *result_capacity>=min_capacity jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: virtual char* GetAppendBuffer(int32_t min_capacity, jpayne@69: int32_t desired_capacity_hint, jpayne@69: char* scratch, int32_t scratch_capacity, jpayne@69: int32_t* result_capacity); jpayne@69: /** jpayne@69: * Returns the number of bytes actually written to the sink. jpayne@69: * @return number of bytes written to the buffer jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: int32_t NumberOfBytesWritten() const { return size_; } jpayne@69: /** jpayne@69: * Returns true if any bytes were discarded, i.e., if there was an jpayne@69: * attempt to write more than 'capacity' bytes. jpayne@69: * @return TRUE if more than 'capacity' bytes were Append()ed jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: UBool Overflowed() const { return overflowed_; } jpayne@69: /** jpayne@69: * Returns the number of bytes appended to the sink. jpayne@69: * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten() jpayne@69: * else they return the same number. jpayne@69: * @return number of bytes written to the buffer jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: int32_t NumberOfBytesAppended() const { return appended_; } jpayne@69: private: jpayne@69: char* outbuf_; jpayne@69: const int32_t capacity_; jpayne@69: int32_t size_; jpayne@69: int32_t appended_; jpayne@69: UBool overflowed_; jpayne@69: jpayne@69: CheckedArrayByteSink() = delete; jpayne@69: CheckedArrayByteSink(const CheckedArrayByteSink &) = delete; jpayne@69: CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete; jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * Implementation of ByteSink that writes to a "string". jpayne@69: * The StringClass is usually instantiated with a std::string. jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: template jpayne@69: class StringByteSink : public ByteSink { jpayne@69: public: jpayne@69: /** jpayne@69: * Constructs a ByteSink that will append bytes to the dest string. jpayne@69: * @param dest pointer to string object to append to jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: StringByteSink(StringClass* dest) : dest_(dest) { } jpayne@69: /** jpayne@69: * Constructs a ByteSink that reserves append capacity and will append bytes to the dest string. jpayne@69: * jpayne@69: * @param dest pointer to string object to append to jpayne@69: * @param initialAppendCapacity capacity beyond dest->length() to be reserve()d jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) { jpayne@69: if (initialAppendCapacity > 0 && jpayne@69: (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) { jpayne@69: dest->reserve(dest->length() + initialAppendCapacity); jpayne@69: } jpayne@69: } jpayne@69: /** jpayne@69: * Append "bytes[0,n-1]" to this. jpayne@69: * @param data the pointer to the bytes jpayne@69: * @param n the number of bytes; must be non-negative jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: virtual void Append(const char* data, int32_t n) { dest_->append(data, n); } jpayne@69: private: jpayne@69: StringClass* dest_; jpayne@69: jpayne@69: StringByteSink() = delete; jpayne@69: StringByteSink(const StringByteSink &) = delete; jpayne@69: StringByteSink &operator=(const StringByteSink &) = delete; jpayne@69: }; jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */ jpayne@69: jpayne@69: #endif // __BYTESTREAM_H__