1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
15 #include "pw_bytes/byte_builder.h"
19 ByteBuilder& ByteBuilder::append(size_t count, std::byte b) {
20 std::byte* const append_destination = &buffer_[size_];
21 std::memset(append_destination, static_cast<int>(b), ResizeForAppend(count));
25 ByteBuilder& ByteBuilder::append(const void* bytes, size_t count) {
26 std::byte* const append_destination = &buffer_[size_];
27 std::memcpy(append_destination, bytes, ResizeForAppend(count));
31 size_t ByteBuilder::ResizeForAppend(size_t bytes_to_append) {
36 if (bytes_to_append > max_size() - size()) {
37 status_ = Status::ResourceExhausted();
41 size_ += bytes_to_append;
43 return bytes_to_append;
46 void ByteBuilder::resize(size_t new_size) {
47 if (new_size <= size_) {
51 status_ = Status::OutOfRange();