annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/unicode/bytestream.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 // © 2016 and later: Unicode, Inc. and others.
jpayne@69 2 // License & terms of use: http://www.unicode.org/copyright.html
jpayne@69 3 // Copyright (C) 2009-2012, International Business Machines
jpayne@69 4 // Corporation and others. All Rights Reserved.
jpayne@69 5 //
jpayne@69 6 // Copyright 2007 Google Inc. All Rights Reserved.
jpayne@69 7 // Author: sanjay@google.com (Sanjay Ghemawat)
jpayne@69 8 //
jpayne@69 9 // Abstract interface that consumes a sequence of bytes (ByteSink).
jpayne@69 10 //
jpayne@69 11 // Used so that we can write a single piece of code that can operate
jpayne@69 12 // on a variety of output string types.
jpayne@69 13 //
jpayne@69 14 // Various implementations of this interface are provided:
jpayne@69 15 // ByteSink:
jpayne@69 16 // CheckedArrayByteSink Write to a flat array, with bounds checking
jpayne@69 17 // StringByteSink Write to an STL string
jpayne@69 18
jpayne@69 19 // This code is a contribution of Google code, and the style used here is
jpayne@69 20 // a compromise between the original Google code and the ICU coding guidelines.
jpayne@69 21 // For example, data types are ICU-ified (size_t,int->int32_t),
jpayne@69 22 // and API comments doxygen-ified, but function names and behavior are
jpayne@69 23 // as in the original, if possible.
jpayne@69 24 // Assertion-style error handling, not available in ICU, was changed to
jpayne@69 25 // parameter "pinning" similar to UnicodeString.
jpayne@69 26 //
jpayne@69 27 // In addition, this is only a partial port of the original Google code,
jpayne@69 28 // limited to what was needed so far. The (nearly) complete original code
jpayne@69 29 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
jpayne@69 30 // (see ICU ticket 6765, r25517).
jpayne@69 31
jpayne@69 32 #ifndef __BYTESTREAM_H__
jpayne@69 33 #define __BYTESTREAM_H__
jpayne@69 34
jpayne@69 35 /**
jpayne@69 36 * \file
jpayne@69 37 * \brief C++ API: Interface for writing bytes, and implementation classes.
jpayne@69 38 */
jpayne@69 39
jpayne@69 40 #include "unicode/utypes.h"
jpayne@69 41
jpayne@69 42 #if U_SHOW_CPLUSPLUS_API
jpayne@69 43
jpayne@69 44 #include "unicode/uobject.h"
jpayne@69 45 #include "unicode/std_string.h"
jpayne@69 46
jpayne@69 47 U_NAMESPACE_BEGIN
jpayne@69 48
jpayne@69 49 /**
jpayne@69 50 * A ByteSink can be filled with bytes.
jpayne@69 51 * @stable ICU 4.2
jpayne@69 52 */
jpayne@69 53 class U_COMMON_API ByteSink : public UMemory {
jpayne@69 54 public:
jpayne@69 55 /**
jpayne@69 56 * Default constructor.
jpayne@69 57 * @stable ICU 4.2
jpayne@69 58 */
jpayne@69 59 ByteSink() { }
jpayne@69 60 /**
jpayne@69 61 * Virtual destructor.
jpayne@69 62 * @stable ICU 4.2
jpayne@69 63 */
jpayne@69 64 virtual ~ByteSink();
jpayne@69 65
jpayne@69 66 /**
jpayne@69 67 * Append "bytes[0,n-1]" to this.
jpayne@69 68 * @param bytes the pointer to the bytes
jpayne@69 69 * @param n the number of bytes; must be non-negative
jpayne@69 70 * @stable ICU 4.2
jpayne@69 71 */
jpayne@69 72 virtual void Append(const char* bytes, int32_t n) = 0;
jpayne@69 73
jpayne@69 74 #ifndef U_HIDE_DRAFT_API
jpayne@69 75 /**
jpayne@69 76 * Appends n bytes to this. Same as Append().
jpayne@69 77 * Call AppendU8() with u8"string literals" which are const char * in C++11
jpayne@69 78 * but const char8_t * in C++20.
jpayne@69 79 * If the compiler does support char8_t as a distinct type,
jpayne@69 80 * then an AppendU8() overload for that is defined and will be chosen.
jpayne@69 81 *
jpayne@69 82 * @param bytes the pointer to the bytes
jpayne@69 83 * @param n the number of bytes; must be non-negative
jpayne@69 84 * @draft ICU 67
jpayne@69 85 */
jpayne@69 86 inline void AppendU8(const char* bytes, int32_t n) {
jpayne@69 87 Append(bytes, n);
jpayne@69 88 }
jpayne@69 89
jpayne@69 90 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
jpayne@69 91 /**
jpayne@69 92 * Appends n bytes to this. Same as Append() but for a const char8_t * pointer.
jpayne@69 93 * Call AppendU8() with u8"string literals" which are const char * in C++11
jpayne@69 94 * but const char8_t * in C++20.
jpayne@69 95 * If the compiler does support char8_t as a distinct type,
jpayne@69 96 * then this AppendU8() overload for that is defined and will be chosen.
jpayne@69 97 *
jpayne@69 98 * @param bytes the pointer to the bytes
jpayne@69 99 * @param n the number of bytes; must be non-negative
jpayne@69 100 * @draft ICU 67
jpayne@69 101 */
jpayne@69 102 inline void AppendU8(const char8_t* bytes, int32_t n) {
jpayne@69 103 Append(reinterpret_cast<const char*>(bytes), n);
jpayne@69 104 }
jpayne@69 105 #endif
jpayne@69 106 #endif // U_HIDE_DRAFT_API
jpayne@69 107
jpayne@69 108 /**
jpayne@69 109 * Returns a writable buffer for appending and writes the buffer's capacity to
jpayne@69 110 * *result_capacity. Guarantees *result_capacity>=min_capacity.
jpayne@69 111 * May return a pointer to the caller-owned scratch buffer which must have
jpayne@69 112 * scratch_capacity>=min_capacity.
jpayne@69 113 * The returned buffer is only valid until the next operation
jpayne@69 114 * on this ByteSink.
jpayne@69 115 *
jpayne@69 116 * After writing at most *result_capacity bytes, call Append() with the
jpayne@69 117 * pointer returned from this function and the number of bytes written.
jpayne@69 118 * Many Append() implementations will avoid copying bytes if this function
jpayne@69 119 * returned an internal buffer.
jpayne@69 120 *
jpayne@69 121 * Partial usage example:
jpayne@69 122 * int32_t capacity;
jpayne@69 123 * char* buffer = sink->GetAppendBuffer(..., &capacity);
jpayne@69 124 * ... Write n bytes into buffer, with n <= capacity.
jpayne@69 125 * sink->Append(buffer, n);
jpayne@69 126 * In many implementations, that call to Append will avoid copying bytes.
jpayne@69 127 *
jpayne@69 128 * If the ByteSink allocates or reallocates an internal buffer, it should use
jpayne@69 129 * the desired_capacity_hint if appropriate.
jpayne@69 130 * If a caller cannot provide a reasonable guess at the desired capacity,
jpayne@69 131 * it should pass desired_capacity_hint=0.
jpayne@69 132 *
jpayne@69 133 * If a non-scratch buffer is returned, the caller may only pass
jpayne@69 134 * a prefix to it to Append().
jpayne@69 135 * That is, it is not correct to pass an interior pointer to Append().
jpayne@69 136 *
jpayne@69 137 * The default implementation always returns the scratch buffer.
jpayne@69 138 *
jpayne@69 139 * @param min_capacity required minimum capacity of the returned buffer;
jpayne@69 140 * must be non-negative
jpayne@69 141 * @param desired_capacity_hint desired capacity of the returned buffer;
jpayne@69 142 * must be non-negative
jpayne@69 143 * @param scratch default caller-owned buffer
jpayne@69 144 * @param scratch_capacity capacity of the scratch buffer
jpayne@69 145 * @param result_capacity pointer to an integer which will be set to the
jpayne@69 146 * capacity of the returned buffer
jpayne@69 147 * @return a buffer with *result_capacity>=min_capacity
jpayne@69 148 * @stable ICU 4.2
jpayne@69 149 */
jpayne@69 150 virtual char* GetAppendBuffer(int32_t min_capacity,
jpayne@69 151 int32_t desired_capacity_hint,
jpayne@69 152 char* scratch, int32_t scratch_capacity,
jpayne@69 153 int32_t* result_capacity);
jpayne@69 154
jpayne@69 155 /**
jpayne@69 156 * Flush internal buffers.
jpayne@69 157 * Some byte sinks use internal buffers or provide buffering
jpayne@69 158 * and require calling Flush() at the end of the stream.
jpayne@69 159 * The ByteSink should be ready for further Append() calls after Flush().
jpayne@69 160 * The default implementation of Flush() does nothing.
jpayne@69 161 * @stable ICU 4.2
jpayne@69 162 */
jpayne@69 163 virtual void Flush();
jpayne@69 164
jpayne@69 165 private:
jpayne@69 166 ByteSink(const ByteSink &) = delete;
jpayne@69 167 ByteSink &operator=(const ByteSink &) = delete;
jpayne@69 168 };
jpayne@69 169
jpayne@69 170 // -------------------------------------------------------------
jpayne@69 171 // Some standard implementations
jpayne@69 172
jpayne@69 173 /**
jpayne@69 174 * Implementation of ByteSink that writes to a flat byte array,
jpayne@69 175 * with bounds-checking:
jpayne@69 176 * This sink will not write more than capacity bytes to outbuf.
jpayne@69 177 * If more than capacity bytes are Append()ed, then excess bytes are ignored,
jpayne@69 178 * and Overflowed() will return true.
jpayne@69 179 * Overflow does not cause a runtime error.
jpayne@69 180 * @stable ICU 4.2
jpayne@69 181 */
jpayne@69 182 class U_COMMON_API CheckedArrayByteSink : public ByteSink {
jpayne@69 183 public:
jpayne@69 184 /**
jpayne@69 185 * Constructs a ByteSink that will write to outbuf[0..capacity-1].
jpayne@69 186 * @param outbuf buffer to write to
jpayne@69 187 * @param capacity size of the buffer
jpayne@69 188 * @stable ICU 4.2
jpayne@69 189 */
jpayne@69 190 CheckedArrayByteSink(char* outbuf, int32_t capacity);
jpayne@69 191 /**
jpayne@69 192 * Destructor.
jpayne@69 193 * @stable ICU 4.2
jpayne@69 194 */
jpayne@69 195 virtual ~CheckedArrayByteSink();
jpayne@69 196 /**
jpayne@69 197 * Returns the sink to its original state, without modifying the buffer.
jpayne@69 198 * Useful for reusing both the buffer and the sink for multiple streams.
jpayne@69 199 * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
jpayne@69 200 * and Overflowed()=FALSE.
jpayne@69 201 * @return *this
jpayne@69 202 * @stable ICU 4.6
jpayne@69 203 */
jpayne@69 204 virtual CheckedArrayByteSink& Reset();
jpayne@69 205 /**
jpayne@69 206 * Append "bytes[0,n-1]" to this.
jpayne@69 207 * @param bytes the pointer to the bytes
jpayne@69 208 * @param n the number of bytes; must be non-negative
jpayne@69 209 * @stable ICU 4.2
jpayne@69 210 */
jpayne@69 211 virtual void Append(const char* bytes, int32_t n);
jpayne@69 212 /**
jpayne@69 213 * Returns a writable buffer for appending and writes the buffer's capacity to
jpayne@69 214 * *result_capacity. For details see the base class documentation.
jpayne@69 215 * @param min_capacity required minimum capacity of the returned buffer;
jpayne@69 216 * must be non-negative
jpayne@69 217 * @param desired_capacity_hint desired capacity of the returned buffer;
jpayne@69 218 * must be non-negative
jpayne@69 219 * @param scratch default caller-owned buffer
jpayne@69 220 * @param scratch_capacity capacity of the scratch buffer
jpayne@69 221 * @param result_capacity pointer to an integer which will be set to the
jpayne@69 222 * capacity of the returned buffer
jpayne@69 223 * @return a buffer with *result_capacity>=min_capacity
jpayne@69 224 * @stable ICU 4.2
jpayne@69 225 */
jpayne@69 226 virtual char* GetAppendBuffer(int32_t min_capacity,
jpayne@69 227 int32_t desired_capacity_hint,
jpayne@69 228 char* scratch, int32_t scratch_capacity,
jpayne@69 229 int32_t* result_capacity);
jpayne@69 230 /**
jpayne@69 231 * Returns the number of bytes actually written to the sink.
jpayne@69 232 * @return number of bytes written to the buffer
jpayne@69 233 * @stable ICU 4.2
jpayne@69 234 */
jpayne@69 235 int32_t NumberOfBytesWritten() const { return size_; }
jpayne@69 236 /**
jpayne@69 237 * Returns true if any bytes were discarded, i.e., if there was an
jpayne@69 238 * attempt to write more than 'capacity' bytes.
jpayne@69 239 * @return TRUE if more than 'capacity' bytes were Append()ed
jpayne@69 240 * @stable ICU 4.2
jpayne@69 241 */
jpayne@69 242 UBool Overflowed() const { return overflowed_; }
jpayne@69 243 /**
jpayne@69 244 * Returns the number of bytes appended to the sink.
jpayne@69 245 * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
jpayne@69 246 * else they return the same number.
jpayne@69 247 * @return number of bytes written to the buffer
jpayne@69 248 * @stable ICU 4.6
jpayne@69 249 */
jpayne@69 250 int32_t NumberOfBytesAppended() const { return appended_; }
jpayne@69 251 private:
jpayne@69 252 char* outbuf_;
jpayne@69 253 const int32_t capacity_;
jpayne@69 254 int32_t size_;
jpayne@69 255 int32_t appended_;
jpayne@69 256 UBool overflowed_;
jpayne@69 257
jpayne@69 258 CheckedArrayByteSink() = delete;
jpayne@69 259 CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
jpayne@69 260 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
jpayne@69 261 };
jpayne@69 262
jpayne@69 263 /**
jpayne@69 264 * Implementation of ByteSink that writes to a "string".
jpayne@69 265 * The StringClass is usually instantiated with a std::string.
jpayne@69 266 * @stable ICU 4.2
jpayne@69 267 */
jpayne@69 268 template<typename StringClass>
jpayne@69 269 class StringByteSink : public ByteSink {
jpayne@69 270 public:
jpayne@69 271 /**
jpayne@69 272 * Constructs a ByteSink that will append bytes to the dest string.
jpayne@69 273 * @param dest pointer to string object to append to
jpayne@69 274 * @stable ICU 4.2
jpayne@69 275 */
jpayne@69 276 StringByteSink(StringClass* dest) : dest_(dest) { }
jpayne@69 277 /**
jpayne@69 278 * Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
jpayne@69 279 *
jpayne@69 280 * @param dest pointer to string object to append to
jpayne@69 281 * @param initialAppendCapacity capacity beyond dest->length() to be reserve()d
jpayne@69 282 * @stable ICU 60
jpayne@69 283 */
jpayne@69 284 StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
jpayne@69 285 if (initialAppendCapacity > 0 &&
jpayne@69 286 (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
jpayne@69 287 dest->reserve(dest->length() + initialAppendCapacity);
jpayne@69 288 }
jpayne@69 289 }
jpayne@69 290 /**
jpayne@69 291 * Append "bytes[0,n-1]" to this.
jpayne@69 292 * @param data the pointer to the bytes
jpayne@69 293 * @param n the number of bytes; must be non-negative
jpayne@69 294 * @stable ICU 4.2
jpayne@69 295 */
jpayne@69 296 virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
jpayne@69 297 private:
jpayne@69 298 StringClass* dest_;
jpayne@69 299
jpayne@69 300 StringByteSink() = delete;
jpayne@69 301 StringByteSink(const StringByteSink &) = delete;
jpayne@69 302 StringByteSink &operator=(const StringByteSink &) = delete;
jpayne@69 303 };
jpayne@69 304
jpayne@69 305 U_NAMESPACE_END
jpayne@69 306
jpayne@69 307 #endif /* U_SHOW_CPLUSPLUS_API */
jpayne@69 308
jpayne@69 309 #endif // __BYTESTREAM_H__