Skip to content

Commit

Permalink
BinarySpanReaderWriter: Add subspan methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jul 21, 2024
1 parent 52f9e85 commit f1f89d3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/common/binary_span_reader_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

#include "binary_span_reader_writer.h"
#include "assert.h"
#include "small_string.h"

BinarySpanReader::BinarySpanReader() = default;
Expand Down Expand Up @@ -30,6 +31,23 @@ bool BinarySpanReader::PeekCString(std::string_view* dst)
return true;
}

std::span<const u8> BinarySpanReader::GetRemainingSpan(size_t size) const
{
DebugAssert(size <= GetBufferRemaining());
return m_buf.subspan(m_pos, size);
}

std::span<const u8> BinarySpanReader::GetRemainingSpan() const
{
return m_buf.subspan(m_pos, m_buf.size() - m_pos);
}

void BinarySpanReader::IncrementPosition(size_t size)
{
DebugAssert(size < GetBufferRemaining());
m_pos += size;
}

bool BinarySpanReader::ReadCString(std::string* dst)
{
std::string_view sv;
Expand Down Expand Up @@ -96,6 +114,23 @@ BinarySpanWriter::BinarySpanWriter(std::span<u8> buf) : m_buf(buf)
{
}

std::span<u8> BinarySpanWriter::GetRemainingSpan(size_t size) const
{
DebugAssert(size <= GetBufferRemaining());
return m_buf.subspan(m_pos, size);
}

std::span<u8> BinarySpanWriter::GetRemainingSpan() const
{
return m_buf.subspan(m_pos, m_buf.size() - m_pos);
}

void BinarySpanWriter::IncrementPosition(size_t size)
{
DebugAssert(size < GetBufferRemaining());
m_pos += size;
}

bool BinarySpanWriter::WriteCString(std::string_view val)
{
if ((m_pos + val.size() + 1) > m_buf.size()) [[unlikely]]
Expand Down
8 changes: 8 additions & 0 deletions src/common/binary_span_reader_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class BinarySpanReader
ALWAYS_INLINE size_t GetBufferRemaining() const { return (m_buf.size() - m_pos); }
ALWAYS_INLINE size_t GetBufferConsumed() const { return m_pos; }

std::span<const u8> GetRemainingSpan() const;
std::span<const u8> GetRemainingSpan(size_t size) const;
void IncrementPosition(size_t size);

// clang-format off
template<typename T> ALWAYS_INLINE bool ReadT(T* dst) { return Read(dst, sizeof(T)); }
ALWAYS_INLINE bool ReadU8(u8* dst) { return ReadT(dst); }
Expand Down Expand Up @@ -99,6 +103,10 @@ class BinarySpanWriter
ALWAYS_INLINE size_t GetBufferRemaining() const { return (m_buf.size() - m_pos); }
ALWAYS_INLINE size_t GetBufferWritten() const { return m_pos; }

std::span<u8> GetRemainingSpan() const;
std::span<u8> GetRemainingSpan(size_t size) const;
void IncrementPosition(size_t size);

// clang-format off
template<typename T> ALWAYS_INLINE bool WriteT(T dst) { return Write(&dst, sizeof(T)); }
ALWAYS_INLINE bool WriteU8(u8 val) { return WriteT(val); }
Expand Down

0 comments on commit f1f89d3

Please sign in to comment.