|
ICU 73.2
73.2
|
00001 // © 2016 and later: Unicode, Inc. and others. 00002 // License & terms of use: http://www.unicode.org/copyright.html 00003 // Copyright (C) 2009-2012, International Business Machines 00004 // Corporation and others. All Rights Reserved. 00005 // 00006 // Copyright 2007 Google Inc. All Rights Reserved. 00007 // Author: sanjay@google.com (Sanjay Ghemawat) 00008 // 00009 // Abstract interface that consumes a sequence of bytes (ByteSink). 00010 // 00011 // Used so that we can write a single piece of code that can operate 00012 // on a variety of output string types. 00013 // 00014 // Various implementations of this interface are provided: 00015 // ByteSink: 00016 // CheckedArrayByteSink Write to a flat array, with bounds checking 00017 // StringByteSink Write to an STL string 00018 00019 // This code is a contribution of Google code, and the style used here is 00020 // a compromise between the original Google code and the ICU coding guidelines. 00021 // For example, data types are ICU-ified (size_t,int->int32_t), 00022 // and API comments doxygen-ified, but function names and behavior are 00023 // as in the original, if possible. 00024 // Assertion-style error handling, not available in ICU, was changed to 00025 // parameter "pinning" similar to UnicodeString. 00026 // 00027 // In addition, this is only a partial port of the original Google code, 00028 // limited to what was needed so far. The (nearly) complete original code 00029 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib 00030 // (see ICU ticket 6765, r25517). 00031 00032 #ifndef __BYTESTREAM_H__ 00033 #define __BYTESTREAM_H__ 00034 00040 #include "unicode/utypes.h" 00041 00042 #if U_SHOW_CPLUSPLUS_API 00043 00044 #include "unicode/uobject.h" 00045 #include "unicode/std_string.h" 00046 00047 U_NAMESPACE_BEGIN 00048 00053 class U_COMMON_API ByteSink : public UMemory { 00054 public: 00059 ByteSink() { } 00064 virtual ~ByteSink(); 00065 00072 virtual void Append(const char* bytes, int32_t n) = 0; 00073 00085 inline void AppendU8(const char* bytes, int32_t n) { 00086 Append(bytes, n); 00087 } 00088 00089 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN) 00090 00101 inline void AppendU8(const char8_t* bytes, int32_t n) { 00102 Append(reinterpret_cast<const char*>(bytes), n); 00103 } 00104 #endif 00105 00148 virtual char* GetAppendBuffer(int32_t min_capacity, 00149 int32_t desired_capacity_hint, 00150 char* scratch, int32_t scratch_capacity, 00151 int32_t* result_capacity); 00152 00161 virtual void Flush(); 00162 00163 private: 00164 ByteSink(const ByteSink &) = delete; 00165 ByteSink &operator=(const ByteSink &) = delete; 00166 }; 00167 00168 // ------------------------------------------------------------- 00169 // Some standard implementations 00170 00180 class U_COMMON_API CheckedArrayByteSink : public ByteSink { 00181 public: 00188 CheckedArrayByteSink(char* outbuf, int32_t capacity); 00193 virtual ~CheckedArrayByteSink(); 00202 virtual CheckedArrayByteSink& Reset(); 00209 virtual void Append(const char* bytes, int32_t n) override; 00224 virtual char* GetAppendBuffer(int32_t min_capacity, 00225 int32_t desired_capacity_hint, 00226 char* scratch, int32_t scratch_capacity, 00227 int32_t* result_capacity) override; 00233 int32_t NumberOfBytesWritten() const { return size_; } 00240 UBool Overflowed() const { return overflowed_; } 00248 int32_t NumberOfBytesAppended() const { return appended_; } 00249 private: 00250 char* outbuf_; 00251 const int32_t capacity_; 00252 int32_t size_; 00253 int32_t appended_; 00254 UBool overflowed_; 00255 00256 CheckedArrayByteSink() = delete; 00257 CheckedArrayByteSink(const CheckedArrayByteSink &) = delete; 00258 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete; 00259 }; 00260 00266 template<typename StringClass> 00267 class StringByteSink : public ByteSink { 00268 public: 00274 StringByteSink(StringClass* dest) : dest_(dest) { } 00282 StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) { 00283 if (initialAppendCapacity > 0 && 00284 (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) { 00285 dest->reserve(dest->length() + initialAppendCapacity); 00286 } 00287 } 00294 virtual void Append(const char* data, int32_t n) override { dest_->append(data, n); } 00295 private: 00296 StringClass* dest_; 00297 00298 StringByteSink() = delete; 00299 StringByteSink(const StringByteSink &) = delete; 00300 StringByteSink &operator=(const StringByteSink &) = delete; 00301 }; 00302 00303 U_NAMESPACE_END 00304 00305 #endif /* U_SHOW_CPLUSPLUS_API */ 00306 00307 #endif // __BYTESTREAM_H__
1.7.6.1