Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | // Copyright (C) 2009-2011, 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 | #include "unicode/utypes.h" |
michael@0 | 8 | #include "unicode/bytestream.h" |
michael@0 | 9 | #include "cmemory.h" |
michael@0 | 10 | |
michael@0 | 11 | U_NAMESPACE_BEGIN |
michael@0 | 12 | |
michael@0 | 13 | ByteSink::~ByteSink() {} |
michael@0 | 14 | |
michael@0 | 15 | char* ByteSink::GetAppendBuffer(int32_t min_capacity, |
michael@0 | 16 | int32_t /*desired_capacity_hint*/, |
michael@0 | 17 | char* scratch, int32_t scratch_capacity, |
michael@0 | 18 | int32_t* result_capacity) { |
michael@0 | 19 | if (min_capacity < 1 || scratch_capacity < min_capacity) { |
michael@0 | 20 | *result_capacity = 0; |
michael@0 | 21 | return NULL; |
michael@0 | 22 | } |
michael@0 | 23 | *result_capacity = scratch_capacity; |
michael@0 | 24 | return scratch; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | void ByteSink::Flush() {} |
michael@0 | 28 | |
michael@0 | 29 | CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity) |
michael@0 | 30 | : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity), |
michael@0 | 31 | size_(0), appended_(0), overflowed_(FALSE) { |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | CheckedArrayByteSink::~CheckedArrayByteSink() {} |
michael@0 | 35 | |
michael@0 | 36 | CheckedArrayByteSink& CheckedArrayByteSink::Reset() { |
michael@0 | 37 | size_ = appended_ = 0; |
michael@0 | 38 | overflowed_ = FALSE; |
michael@0 | 39 | return *this; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | void CheckedArrayByteSink::Append(const char* bytes, int32_t n) { |
michael@0 | 43 | if (n <= 0) { |
michael@0 | 44 | return; |
michael@0 | 45 | } |
michael@0 | 46 | appended_ += n; |
michael@0 | 47 | int32_t available = capacity_ - size_; |
michael@0 | 48 | if (n > available) { |
michael@0 | 49 | n = available; |
michael@0 | 50 | overflowed_ = TRUE; |
michael@0 | 51 | } |
michael@0 | 52 | if (n > 0 && bytes != (outbuf_ + size_)) { |
michael@0 | 53 | uprv_memcpy(outbuf_ + size_, bytes, n); |
michael@0 | 54 | } |
michael@0 | 55 | size_ += n; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | char* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity, |
michael@0 | 59 | int32_t /*desired_capacity_hint*/, |
michael@0 | 60 | char* scratch, |
michael@0 | 61 | int32_t scratch_capacity, |
michael@0 | 62 | int32_t* result_capacity) { |
michael@0 | 63 | if (min_capacity < 1 || scratch_capacity < min_capacity) { |
michael@0 | 64 | *result_capacity = 0; |
michael@0 | 65 | return NULL; |
michael@0 | 66 | } |
michael@0 | 67 | int32_t available = capacity_ - size_; |
michael@0 | 68 | if (available >= min_capacity) { |
michael@0 | 69 | *result_capacity = available; |
michael@0 | 70 | return outbuf_ + size_; |
michael@0 | 71 | } else { |
michael@0 | 72 | *result_capacity = scratch_capacity; |
michael@0 | 73 | return scratch; |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | U_NAMESPACE_END |