/proc/self/cwd/pw_bytes/byte_builder.cc
Line | Count | Source (jump to first uncovered line) |
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 | | |
19 | 0 | ByteBuilder& ByteBuilder::append(size_t count, std::byte b) { |
20 | 0 | std::byte* const append_destination = buffer_.data() + size_; |
21 | 0 | std::fill_n(append_destination, ResizeForAppend(count), b); |
22 | 0 | return *this; |
23 | 0 | } |
24 | | |
25 | 0 | ByteBuilder& ByteBuilder::append(const void* bytes, size_t count) { |
26 | 0 | std::byte* const append_destination = buffer_.data() + size_; |
27 | 0 | std::copy_n(static_cast<const std::byte*>(bytes), |
28 | 0 | ResizeForAppend(count), |
29 | 0 | append_destination); |
30 | 0 | return *this; |
31 | 0 | } |
32 | | |
33 | 0 | size_t ByteBuilder::ResizeForAppend(size_t bytes_to_append) { |
34 | 0 | if (!status_.ok()) { |
35 | 0 | return 0; |
36 | 0 | } |
37 | | |
38 | 0 | if (bytes_to_append > max_size() - size()) { |
39 | 0 | status_ = Status::ResourceExhausted(); |
40 | 0 | return 0; |
41 | 0 | } |
42 | | |
43 | 0 | size_ += bytes_to_append; |
44 | 0 | status_ = OkStatus(); |
45 | 0 | return bytes_to_append; |
46 | 0 | } |
47 | | |
48 | 0 | void ByteBuilder::resize(size_t new_size) { |
49 | 0 | if (new_size <= size_) { |
50 | 0 | size_ = new_size; |
51 | 0 | status_ = OkStatus(); |
52 | 0 | } else { |
53 | 0 | status_ = Status::OutOfRange(); |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | } // namespace pw |