• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 // Copyright (C) 2009-2011, International Business Machines
4 // Corporation and others. All Rights Reserved.
5 //
6 // Copyright 2007 Google Inc. All Rights Reserved.
7 // Author: sanjay@google.com (Sanjay Ghemawat)
8 
9 #include "unicode/utypes.h"
10 #include "unicode/bytestream.h"
11 #include "cmemory.h"
12 
13 U_NAMESPACE_BEGIN
14 
~ByteSink()15 ByteSink::~ByteSink() {}
16 
GetAppendBuffer(int32_t min_capacity,int32_t,char * scratch,int32_t scratch_capacity,int32_t * result_capacity)17 char* ByteSink::GetAppendBuffer(int32_t min_capacity,
18                                 int32_t /*desired_capacity_hint*/,
19                                 char* scratch, int32_t scratch_capacity,
20                                 int32_t* result_capacity) {
21   if (min_capacity < 1 || scratch_capacity < min_capacity) {
22     *result_capacity = 0;
23     return NULL;
24   }
25   *result_capacity = scratch_capacity;
26   return scratch;
27 }
28 
Flush()29 void ByteSink::Flush() {}
30 
CheckedArrayByteSink(char * outbuf,int32_t capacity)31 CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity)
32     : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity),
33       size_(0), appended_(0), overflowed_(FALSE) {
34 }
35 
~CheckedArrayByteSink()36 CheckedArrayByteSink::~CheckedArrayByteSink() {}
37 
Reset()38 CheckedArrayByteSink& CheckedArrayByteSink::Reset() {
39   size_ = appended_ = 0;
40   overflowed_ = FALSE;
41   return *this;
42 }
43 
Append(const char * bytes,int32_t n)44 void CheckedArrayByteSink::Append(const char* bytes, int32_t n) {
45   if (n <= 0) {
46     return;
47   }
48   appended_ += n;
49   int32_t available = capacity_ - size_;
50   if (n > available) {
51     n = available;
52     overflowed_ = TRUE;
53   }
54   if (n > 0 && bytes != (outbuf_ + size_)) {
55     uprv_memcpy(outbuf_ + size_, bytes, n);
56   }
57   size_ += n;
58 }
59 
GetAppendBuffer(int32_t min_capacity,int32_t,char * scratch,int32_t scratch_capacity,int32_t * result_capacity)60 char* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity,
61                                             int32_t /*desired_capacity_hint*/,
62                                             char* scratch,
63                                             int32_t scratch_capacity,
64                                             int32_t* result_capacity) {
65   if (min_capacity < 1 || scratch_capacity < min_capacity) {
66     *result_capacity = 0;
67     return NULL;
68   }
69   int32_t available = capacity_ - size_;
70   if (available >= min_capacity) {
71     *result_capacity = available;
72     return outbuf_ + size_;
73   } else {
74     *result_capacity = scratch_capacity;
75     return scratch;
76   }
77 }
78 
79 U_NAMESPACE_END
80