From f1f89d3f0f7192c06a73118e84adf50b1954f857 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 21 Jul 2024 13:33:41 +1000 Subject: [PATCH] BinarySpanReaderWriter: Add subspan methods --- src/common/binary_span_reader_writer.cpp | 35 ++++++++++++++++++++++++ src/common/binary_span_reader_writer.h | 8 ++++++ 2 files changed, 43 insertions(+) diff --git a/src/common/binary_span_reader_writer.cpp b/src/common/binary_span_reader_writer.cpp index e1011da00..a0ac8078b 100644 --- a/src/common/binary_span_reader_writer.cpp +++ b/src/common/binary_span_reader_writer.cpp @@ -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; @@ -30,6 +31,23 @@ bool BinarySpanReader::PeekCString(std::string_view* dst) return true; } +std::span BinarySpanReader::GetRemainingSpan(size_t size) const +{ + DebugAssert(size <= GetBufferRemaining()); + return m_buf.subspan(m_pos, size); +} + +std::span 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; @@ -96,6 +114,23 @@ BinarySpanWriter::BinarySpanWriter(std::span buf) : m_buf(buf) { } +std::span BinarySpanWriter::GetRemainingSpan(size_t size) const +{ + DebugAssert(size <= GetBufferRemaining()); + return m_buf.subspan(m_pos, size); +} + +std::span 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]] diff --git a/src/common/binary_span_reader_writer.h b/src/common/binary_span_reader_writer.h index 7bae91a4a..643047a39 100644 --- a/src/common/binary_span_reader_writer.h +++ b/src/common/binary_span_reader_writer.h @@ -22,6 +22,10 @@ public: ALWAYS_INLINE size_t GetBufferRemaining() const { return (m_buf.size() - m_pos); } ALWAYS_INLINE size_t GetBufferConsumed() const { return m_pos; } + std::span GetRemainingSpan() const; + std::span GetRemainingSpan(size_t size) const; + void IncrementPosition(size_t size); + // clang-format off template ALWAYS_INLINE bool ReadT(T* dst) { return Read(dst, sizeof(T)); } ALWAYS_INLINE bool ReadU8(u8* dst) { return ReadT(dst); } @@ -99,6 +103,10 @@ public: ALWAYS_INLINE size_t GetBufferRemaining() const { return (m_buf.size() - m_pos); } ALWAYS_INLINE size_t GetBufferWritten() const { return m_pos; } + std::span GetRemainingSpan() const; + std::span GetRemainingSpan(size_t size) const; + void IncrementPosition(size_t size); + // clang-format off template ALWAYS_INLINE bool WriteT(T dst) { return Write(&dst, sizeof(T)); } ALWAYS_INLINE bool WriteU8(u8 val) { return WriteT(val); }