Skip to content

Commit

Permalink
Merge pull request #3966 from randombit/jack/constexpr-buffer-stuffer
Browse files Browse the repository at this point in the history
Apply constexpr to BufferStuffer
  • Loading branch information
randombit authored Apr 4, 2024
2 parents 64676be + bf12b5d commit e9c6c16
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/lib/utils/stl_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ class BufferSlicer final {
*/
class BufferStuffer {
public:
BufferStuffer(std::span<uint8_t> buffer) : m_buffer(buffer) {}
constexpr BufferStuffer(std::span<uint8_t> buffer) : m_buffer(buffer) {}

/**
* @returns a span for the next @p bytes bytes in the concatenated buffer.
* Checks that the buffer is not exceded.
*/
std::span<uint8_t> next(size_t bytes) {
constexpr std::span<uint8_t> next(size_t bytes) {
BOTAN_STATE_CHECK(m_buffer.size() >= bytes);

auto result = m_buffer.first(bytes);
Expand All @@ -213,7 +213,7 @@ class BufferStuffer {
}

template <size_t bytes>
std::span<uint8_t, bytes> next() {
constexpr std::span<uint8_t, bytes> next() {
BOTAN_STATE_CHECK(m_buffer.size() >= bytes);

auto result = m_buffer.first<bytes>();
Expand All @@ -229,21 +229,21 @@ class BufferStuffer {
/**
* @returns a reference to the next single byte in the buffer
*/
uint8_t& next_byte() { return next(1)[0]; }
constexpr uint8_t& next_byte() { return next(1)[0]; }

void append(std::span<const uint8_t> buffer) {
constexpr void append(std::span<const uint8_t> buffer) {
auto sink = next(buffer.size());
std::copy(buffer.begin(), buffer.end(), sink.begin());
}

void append(uint8_t b, size_t repeat = 1) {
constexpr void append(uint8_t b, size_t repeat = 1) {
auto sink = next(repeat);
std::fill(sink.begin(), sink.end(), b);
}

bool full() const { return m_buffer.empty(); }
constexpr bool full() const { return m_buffer.empty(); }

size_t remaining_capacity() const { return m_buffer.size(); }
constexpr size_t remaining_capacity() const { return m_buffer.size(); }

private:
std::span<uint8_t> m_buffer;
Expand Down

0 comments on commit e9c6c16

Please sign in to comment.