1*61c4878aSAndroid Build Coastguard Worker // Copyright 2020 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker #include "pw_bytes/byte_builder.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker namespace pw {
18*61c4878aSAndroid Build Coastguard Worker
append(size_t count,std::byte b)19*61c4878aSAndroid Build Coastguard Worker ByteBuilder& ByteBuilder::append(size_t count, std::byte b) {
20*61c4878aSAndroid Build Coastguard Worker std::byte* const append_destination = buffer_.data() + size_;
21*61c4878aSAndroid Build Coastguard Worker std::fill_n(append_destination, ResizeForAppend(count), b);
22*61c4878aSAndroid Build Coastguard Worker return *this;
23*61c4878aSAndroid Build Coastguard Worker }
24*61c4878aSAndroid Build Coastguard Worker
append(const void * bytes,size_t count)25*61c4878aSAndroid Build Coastguard Worker ByteBuilder& ByteBuilder::append(const void* bytes, size_t count) {
26*61c4878aSAndroid Build Coastguard Worker std::byte* const append_destination = buffer_.data() + size_;
27*61c4878aSAndroid Build Coastguard Worker std::copy_n(static_cast<const std::byte*>(bytes),
28*61c4878aSAndroid Build Coastguard Worker ResizeForAppend(count),
29*61c4878aSAndroid Build Coastguard Worker append_destination);
30*61c4878aSAndroid Build Coastguard Worker return *this;
31*61c4878aSAndroid Build Coastguard Worker }
32*61c4878aSAndroid Build Coastguard Worker
ResizeForAppend(size_t bytes_to_append)33*61c4878aSAndroid Build Coastguard Worker size_t ByteBuilder::ResizeForAppend(size_t bytes_to_append) {
34*61c4878aSAndroid Build Coastguard Worker if (!status_.ok()) {
35*61c4878aSAndroid Build Coastguard Worker return 0;
36*61c4878aSAndroid Build Coastguard Worker }
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker if (bytes_to_append > max_size() - size()) {
39*61c4878aSAndroid Build Coastguard Worker status_ = Status::ResourceExhausted();
40*61c4878aSAndroid Build Coastguard Worker return 0;
41*61c4878aSAndroid Build Coastguard Worker }
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard Worker size_ += bytes_to_append;
44*61c4878aSAndroid Build Coastguard Worker status_ = OkStatus();
45*61c4878aSAndroid Build Coastguard Worker return bytes_to_append;
46*61c4878aSAndroid Build Coastguard Worker }
47*61c4878aSAndroid Build Coastguard Worker
resize(size_t new_size)48*61c4878aSAndroid Build Coastguard Worker void ByteBuilder::resize(size_t new_size) {
49*61c4878aSAndroid Build Coastguard Worker if (new_size <= size_) {
50*61c4878aSAndroid Build Coastguard Worker size_ = new_size;
51*61c4878aSAndroid Build Coastguard Worker status_ = OkStatus();
52*61c4878aSAndroid Build Coastguard Worker } else {
53*61c4878aSAndroid Build Coastguard Worker status_ = Status::OutOfRange();
54*61c4878aSAndroid Build Coastguard Worker }
55*61c4878aSAndroid Build Coastguard Worker }
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker } // namespace pw
58