intl/icu/source/common/bytestream.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/bytestream.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +// Copyright (C) 2009-2011, International Business Machines
     1.5 +// Corporation and others. All Rights Reserved.
     1.6 +//
     1.7 +// Copyright 2007 Google Inc. All Rights Reserved.
     1.8 +// Author: sanjay@google.com (Sanjay Ghemawat)
     1.9 +
    1.10 +#include "unicode/utypes.h"
    1.11 +#include "unicode/bytestream.h"
    1.12 +#include "cmemory.h"
    1.13 +
    1.14 +U_NAMESPACE_BEGIN
    1.15 +
    1.16 +ByteSink::~ByteSink() {}
    1.17 +
    1.18 +char* ByteSink::GetAppendBuffer(int32_t min_capacity,
    1.19 +                                int32_t /*desired_capacity_hint*/,
    1.20 +                                char* scratch, int32_t scratch_capacity,
    1.21 +                                int32_t* result_capacity) {
    1.22 +  if (min_capacity < 1 || scratch_capacity < min_capacity) {
    1.23 +    *result_capacity = 0;
    1.24 +    return NULL;
    1.25 +  }
    1.26 +  *result_capacity = scratch_capacity;
    1.27 +  return scratch;
    1.28 +}
    1.29 +
    1.30 +void ByteSink::Flush() {}
    1.31 +
    1.32 +CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity)
    1.33 +    : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity),
    1.34 +      size_(0), appended_(0), overflowed_(FALSE) {
    1.35 +}
    1.36 +
    1.37 +CheckedArrayByteSink::~CheckedArrayByteSink() {}
    1.38 +
    1.39 +CheckedArrayByteSink& CheckedArrayByteSink::Reset() {
    1.40 +  size_ = appended_ = 0;
    1.41 +  overflowed_ = FALSE;
    1.42 +  return *this;
    1.43 +}
    1.44 +
    1.45 +void CheckedArrayByteSink::Append(const char* bytes, int32_t n) {
    1.46 +  if (n <= 0) {
    1.47 +    return;
    1.48 +  }
    1.49 +  appended_ += n;
    1.50 +  int32_t available = capacity_ - size_;
    1.51 +  if (n > available) {
    1.52 +    n = available;
    1.53 +    overflowed_ = TRUE;
    1.54 +  }
    1.55 +  if (n > 0 && bytes != (outbuf_ + size_)) {
    1.56 +    uprv_memcpy(outbuf_ + size_, bytes, n);
    1.57 +  }
    1.58 +  size_ += n;
    1.59 +}
    1.60 +
    1.61 +char* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity,
    1.62 +                                            int32_t /*desired_capacity_hint*/,
    1.63 +                                            char* scratch,
    1.64 +                                            int32_t scratch_capacity,
    1.65 +                                            int32_t* result_capacity) {
    1.66 +  if (min_capacity < 1 || scratch_capacity < min_capacity) {
    1.67 +    *result_capacity = 0;
    1.68 +    return NULL;
    1.69 +  }
    1.70 +  int32_t available = capacity_ - size_;
    1.71 +  if (available >= min_capacity) {
    1.72 +    *result_capacity = available;
    1.73 +    return outbuf_ + size_;
    1.74 +  } else {
    1.75 +    *result_capacity = scratch_capacity;
    1.76 +    return scratch;
    1.77 +  }
    1.78 +}
    1.79 +
    1.80 +U_NAMESPACE_END

mercurial