mirror of
https://github.com/RetroDECK/Duckstation.git
synced 2025-01-20 07:15:38 +00:00
BinarySpanReaderWriter: Add subspan methods
This commit is contained in:
parent
52f9e8556a
commit
f1f89d3f0f
|
@ -2,6 +2,7 @@
|
||||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||||
|
|
||||||
#include "binary_span_reader_writer.h"
|
#include "binary_span_reader_writer.h"
|
||||||
|
#include "assert.h"
|
||||||
#include "small_string.h"
|
#include "small_string.h"
|
||||||
|
|
||||||
BinarySpanReader::BinarySpanReader() = default;
|
BinarySpanReader::BinarySpanReader() = default;
|
||||||
|
@ -30,6 +31,23 @@ bool BinarySpanReader::PeekCString(std::string_view* dst)
|
||||||
return true;
|
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)
|
bool BinarySpanReader::ReadCString(std::string* dst)
|
||||||
{
|
{
|
||||||
std::string_view sv;
|
std::string_view sv;
|
||||||
|
@ -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)
|
bool BinarySpanWriter::WriteCString(std::string_view val)
|
||||||
{
|
{
|
||||||
if ((m_pos + val.size() + 1) > m_buf.size()) [[unlikely]]
|
if ((m_pos + val.size() + 1) > m_buf.size()) [[unlikely]]
|
||||||
|
|
|
@ -22,6 +22,10 @@ public:
|
||||||
ALWAYS_INLINE size_t GetBufferRemaining() const { return (m_buf.size() - m_pos); }
|
ALWAYS_INLINE size_t GetBufferRemaining() const { return (m_buf.size() - m_pos); }
|
||||||
ALWAYS_INLINE size_t GetBufferConsumed() const { return 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
|
// clang-format off
|
||||||
template<typename T> ALWAYS_INLINE bool ReadT(T* dst) { return Read(dst, sizeof(T)); }
|
template<typename T> ALWAYS_INLINE bool ReadT(T* dst) { return Read(dst, sizeof(T)); }
|
||||||
ALWAYS_INLINE bool ReadU8(u8* dst) { return ReadT(dst); }
|
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 GetBufferRemaining() const { return (m_buf.size() - m_pos); }
|
||||||
ALWAYS_INLINE size_t GetBufferWritten() const { return 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
|
// clang-format off
|
||||||
template<typename T> ALWAYS_INLINE bool WriteT(T dst) { return Write(&dst, sizeof(T)); }
|
template<typename T> ALWAYS_INLINE bool WriteT(T dst) { return Write(&dst, sizeof(T)); }
|
||||||
ALWAYS_INLINE bool WriteU8(u8 val) { return WriteT(val); }
|
ALWAYS_INLINE bool WriteU8(u8 val) { return WriteT(val); }
|
||||||
|
|
Loading…
Reference in a new issue