|
||||
Warning, file /include/unicode/bytestream.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // © 2016 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 // Copyright (C) 2009-2012, International Business Machines 0004 // Corporation and others. All Rights Reserved. 0005 // 0006 // Copyright 2007 Google Inc. All Rights Reserved. 0007 // Author: sanjay@google.com (Sanjay Ghemawat) 0008 // 0009 // Abstract interface that consumes a sequence of bytes (ByteSink). 0010 // 0011 // Used so that we can write a single piece of code that can operate 0012 // on a variety of output string types. 0013 // 0014 // Various implementations of this interface are provided: 0015 // ByteSink: 0016 // CheckedArrayByteSink Write to a flat array, with bounds checking 0017 // StringByteSink Write to an STL string 0018 0019 // This code is a contribution of Google code, and the style used here is 0020 // a compromise between the original Google code and the ICU coding guidelines. 0021 // For example, data types are ICU-ified (size_t,int->int32_t), 0022 // and API comments doxygen-ified, but function names and behavior are 0023 // as in the original, if possible. 0024 // Assertion-style error handling, not available in ICU, was changed to 0025 // parameter "pinning" similar to UnicodeString. 0026 // 0027 // In addition, this is only a partial port of the original Google code, 0028 // limited to what was needed so far. The (nearly) complete original code 0029 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib 0030 // (see ICU ticket 6765, r25517). 0031 0032 #ifndef __BYTESTREAM_H__ 0033 #define __BYTESTREAM_H__ 0034 0035 /** 0036 * \file 0037 * \brief C++ API: Interface for writing bytes, and implementation classes. 0038 */ 0039 0040 #include "unicode/utypes.h" 0041 0042 #if U_SHOW_CPLUSPLUS_API 0043 0044 #include "unicode/uobject.h" 0045 #include "unicode/std_string.h" 0046 0047 U_NAMESPACE_BEGIN 0048 0049 /** 0050 * A ByteSink can be filled with bytes. 0051 * @stable ICU 4.2 0052 */ 0053 class U_COMMON_API ByteSink : public UMemory { 0054 public: 0055 /** 0056 * Default constructor. 0057 * @stable ICU 4.2 0058 */ 0059 ByteSink() { } 0060 /** 0061 * Virtual destructor. 0062 * @stable ICU 4.2 0063 */ 0064 virtual ~ByteSink(); 0065 0066 /** 0067 * Append "bytes[0,n-1]" to this. 0068 * @param bytes the pointer to the bytes 0069 * @param n the number of bytes; must be non-negative 0070 * @stable ICU 4.2 0071 */ 0072 virtual void Append(const char* bytes, int32_t n) = 0; 0073 0074 /** 0075 * Appends n bytes to this. Same as Append(). 0076 * Call AppendU8() with u8"string literals" which are const char * in C++11 0077 * but const char8_t * in C++20. 0078 * If the compiler does support char8_t as a distinct type, 0079 * then an AppendU8() overload for that is defined and will be chosen. 0080 * 0081 * @param bytes the pointer to the bytes 0082 * @param n the number of bytes; must be non-negative 0083 * @stable ICU 67 0084 */ 0085 inline void AppendU8(const char* bytes, int32_t n) { 0086 Append(bytes, n); 0087 } 0088 0089 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN) 0090 /** 0091 * Appends n bytes to this. Same as Append() but for a const char8_t * pointer. 0092 * Call AppendU8() with u8"string literals" which are const char * in C++11 0093 * but const char8_t * in C++20. 0094 * If the compiler does support char8_t as a distinct type, 0095 * then this AppendU8() overload for that is defined and will be chosen. 0096 * 0097 * @param bytes the pointer to the bytes 0098 * @param n the number of bytes; must be non-negative 0099 * @stable ICU 67 0100 */ 0101 inline void AppendU8(const char8_t* bytes, int32_t n) { 0102 Append(reinterpret_cast<const char*>(bytes), n); 0103 } 0104 #endif 0105 0106 /** 0107 * Returns a writable buffer for appending and writes the buffer's capacity to 0108 * *result_capacity. Guarantees *result_capacity>=min_capacity. 0109 * May return a pointer to the caller-owned scratch buffer which must have 0110 * scratch_capacity>=min_capacity. 0111 * The returned buffer is only valid until the next operation 0112 * on this ByteSink. 0113 * 0114 * After writing at most *result_capacity bytes, call Append() with the 0115 * pointer returned from this function and the number of bytes written. 0116 * Many Append() implementations will avoid copying bytes if this function 0117 * returned an internal buffer. 0118 * 0119 * Partial usage example: 0120 * int32_t capacity; 0121 * char* buffer = sink->GetAppendBuffer(..., &capacity); 0122 * ... Write n bytes into buffer, with n <= capacity. 0123 * sink->Append(buffer, n); 0124 * In many implementations, that call to Append will avoid copying bytes. 0125 * 0126 * If the ByteSink allocates or reallocates an internal buffer, it should use 0127 * the desired_capacity_hint if appropriate. 0128 * If a caller cannot provide a reasonable guess at the desired capacity, 0129 * it should pass desired_capacity_hint=0. 0130 * 0131 * If a non-scratch buffer is returned, the caller may only pass 0132 * a prefix to it to Append(). 0133 * That is, it is not correct to pass an interior pointer to Append(). 0134 * 0135 * The default implementation always returns the scratch buffer. 0136 * 0137 * @param min_capacity required minimum capacity of the returned buffer; 0138 * must be non-negative 0139 * @param desired_capacity_hint desired capacity of the returned buffer; 0140 * must be non-negative 0141 * @param scratch default caller-owned buffer 0142 * @param scratch_capacity capacity of the scratch buffer 0143 * @param result_capacity pointer to an integer which will be set to the 0144 * capacity of the returned buffer 0145 * @return a buffer with *result_capacity>=min_capacity 0146 * @stable ICU 4.2 0147 */ 0148 virtual char* GetAppendBuffer(int32_t min_capacity, 0149 int32_t desired_capacity_hint, 0150 char* scratch, int32_t scratch_capacity, 0151 int32_t* result_capacity); 0152 0153 /** 0154 * Flush internal buffers. 0155 * Some byte sinks use internal buffers or provide buffering 0156 * and require calling Flush() at the end of the stream. 0157 * The ByteSink should be ready for further Append() calls after Flush(). 0158 * The default implementation of Flush() does nothing. 0159 * @stable ICU 4.2 0160 */ 0161 virtual void Flush(); 0162 0163 private: 0164 ByteSink(const ByteSink &) = delete; 0165 ByteSink &operator=(const ByteSink &) = delete; 0166 }; 0167 0168 // ------------------------------------------------------------- 0169 // Some standard implementations 0170 0171 /** 0172 * Implementation of ByteSink that writes to a flat byte array, 0173 * with bounds-checking: 0174 * This sink will not write more than capacity bytes to outbuf. 0175 * If more than capacity bytes are Append()ed, then excess bytes are ignored, 0176 * and Overflowed() will return true. 0177 * Overflow does not cause a runtime error. 0178 * @stable ICU 4.2 0179 */ 0180 class U_COMMON_API CheckedArrayByteSink : public ByteSink { 0181 public: 0182 /** 0183 * Constructs a ByteSink that will write to outbuf[0..capacity-1]. 0184 * @param outbuf buffer to write to 0185 * @param capacity size of the buffer 0186 * @stable ICU 4.2 0187 */ 0188 CheckedArrayByteSink(char* outbuf, int32_t capacity); 0189 /** 0190 * Destructor. 0191 * @stable ICU 4.2 0192 */ 0193 virtual ~CheckedArrayByteSink(); 0194 /** 0195 * Returns the sink to its original state, without modifying the buffer. 0196 * Useful for reusing both the buffer and the sink for multiple streams. 0197 * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0 0198 * and Overflowed()=false. 0199 * @return *this 0200 * @stable ICU 4.6 0201 */ 0202 virtual CheckedArrayByteSink& Reset(); 0203 /** 0204 * Append "bytes[0,n-1]" to this. 0205 * @param bytes the pointer to the bytes 0206 * @param n the number of bytes; must be non-negative 0207 * @stable ICU 4.2 0208 */ 0209 virtual void Append(const char* bytes, int32_t n) override; 0210 /** 0211 * Returns a writable buffer for appending and writes the buffer's capacity to 0212 * *result_capacity. For details see the base class documentation. 0213 * @param min_capacity required minimum capacity of the returned buffer; 0214 * must be non-negative 0215 * @param desired_capacity_hint desired capacity of the returned buffer; 0216 * must be non-negative 0217 * @param scratch default caller-owned buffer 0218 * @param scratch_capacity capacity of the scratch buffer 0219 * @param result_capacity pointer to an integer which will be set to the 0220 * capacity of the returned buffer 0221 * @return a buffer with *result_capacity>=min_capacity 0222 * @stable ICU 4.2 0223 */ 0224 virtual char* GetAppendBuffer(int32_t min_capacity, 0225 int32_t desired_capacity_hint, 0226 char* scratch, int32_t scratch_capacity, 0227 int32_t* result_capacity) override; 0228 /** 0229 * Returns the number of bytes actually written to the sink. 0230 * @return number of bytes written to the buffer 0231 * @stable ICU 4.2 0232 */ 0233 int32_t NumberOfBytesWritten() const { return size_; } 0234 /** 0235 * Returns true if any bytes were discarded, i.e., if there was an 0236 * attempt to write more than 'capacity' bytes. 0237 * @return true if more than 'capacity' bytes were Append()ed 0238 * @stable ICU 4.2 0239 */ 0240 UBool Overflowed() const { return overflowed_; } 0241 /** 0242 * Returns the number of bytes appended to the sink. 0243 * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten() 0244 * else they return the same number. 0245 * @return number of bytes written to the buffer 0246 * @stable ICU 4.6 0247 */ 0248 int32_t NumberOfBytesAppended() const { return appended_; } 0249 private: 0250 char* outbuf_; 0251 const int32_t capacity_; 0252 int32_t size_; 0253 int32_t appended_; 0254 UBool overflowed_; 0255 0256 CheckedArrayByteSink() = delete; 0257 CheckedArrayByteSink(const CheckedArrayByteSink &) = delete; 0258 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete; 0259 }; 0260 0261 /** 0262 * Implementation of ByteSink that writes to a "string". 0263 * The StringClass is usually instantiated with a std::string. 0264 * @stable ICU 4.2 0265 */ 0266 template<typename StringClass> 0267 class StringByteSink : public ByteSink { 0268 public: 0269 /** 0270 * Constructs a ByteSink that will append bytes to the dest string. 0271 * @param dest pointer to string object to append to 0272 * @stable ICU 4.2 0273 */ 0274 StringByteSink(StringClass* dest) : dest_(dest) { } 0275 /** 0276 * Constructs a ByteSink that reserves append capacity and will append bytes to the dest string. 0277 * 0278 * @param dest pointer to string object to append to 0279 * @param initialAppendCapacity capacity beyond dest->length() to be reserve()d 0280 * @stable ICU 60 0281 */ 0282 StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) { 0283 if (initialAppendCapacity > 0 && 0284 (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) { 0285 dest->reserve(dest->length() + initialAppendCapacity); 0286 } 0287 } 0288 /** 0289 * Append "bytes[0,n-1]" to this. 0290 * @param data the pointer to the bytes 0291 * @param n the number of bytes; must be non-negative 0292 * @stable ICU 4.2 0293 */ 0294 virtual void Append(const char* data, int32_t n) override { dest_->append(data, n); } 0295 private: 0296 StringClass* dest_; 0297 0298 StringByteSink() = delete; 0299 StringByteSink(const StringByteSink &) = delete; 0300 StringByteSink &operator=(const StringByteSink &) = delete; 0301 }; 0302 0303 U_NAMESPACE_END 0304 0305 #endif /* U_SHOW_CPLUSPLUS_API */ 0306 0307 #endif // __BYTESTREAM_H__
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |