intl/icu/source/common/unicode/bytestream.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial