shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 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 |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 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 |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_bytes/byte_builder.h" |
| 16 | |
| 17 | namespace pw { |
| 18 | |
shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 19 | ByteBuilder& ByteBuilder::append(size_t count, std::byte b) { |
Dennis Kormalev | 3950de4 | 2022-06-21 14:18:57 +0000 | [diff] [blame] | 20 | std::byte* const append_destination = buffer_.data() + size_; |
Wyatt Hepler | 5fa58f6 | 2021-11-15 11:52:23 -0800 | [diff] [blame] | 21 | std::fill_n(append_destination, ResizeForAppend(count), b); |
shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 22 | return *this; |
| 23 | } |
| 24 | |
| 25 | ByteBuilder& ByteBuilder::append(const void* bytes, size_t count) { |
Dennis Kormalev | 3950de4 | 2022-06-21 14:18:57 +0000 | [diff] [blame] | 26 | std::byte* const append_destination = buffer_.data() + size_; |
Wyatt Hepler | 5fa58f6 | 2021-11-15 11:52:23 -0800 | [diff] [blame] | 27 | std::copy_n(static_cast<const std::byte*>(bytes), |
| 28 | ResizeForAppend(count), |
| 29 | append_destination); |
shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 30 | return *this; |
| 31 | } |
| 32 | |
| 33 | size_t ByteBuilder::ResizeForAppend(size_t bytes_to_append) { |
shaneajg | 3181d18 | 2020-06-17 20:17:23 -0400 | [diff] [blame] | 34 | if (!status_.ok()) { |
| 35 | return 0; |
shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 36 | } |
| 37 | |
shaneajg | 3181d18 | 2020-06-17 20:17:23 -0400 | [diff] [blame] | 38 | if (bytes_to_append > max_size() - size()) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 39 | status_ = Status::ResourceExhausted(); |
shaneajg | 3181d18 | 2020-06-17 20:17:23 -0400 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | size_ += bytes_to_append; |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 44 | status_ = OkStatus(); |
shaneajg | 3181d18 | 2020-06-17 20:17:23 -0400 | [diff] [blame] | 45 | return bytes_to_append; |
shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void ByteBuilder::resize(size_t new_size) { |
| 49 | if (new_size <= size_) { |
| 50 | size_ = new_size; |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 51 | status_ = OkStatus(); |
shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 52 | } else { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 53 | status_ = Status::OutOfRange(); |
shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
shaneajg | 9c19db4 | 2020-06-11 15:49:51 -0400 | [diff] [blame] | 57 | } // namespace pw |