AK: Allow passing an offset to CircularBuffer::next_read_span()

This commit is contained in:
Tim Schumacher 2023-06-01 23:39:05 +02:00 committed by Linus Groh
parent 046a9faeb3
commit d12036132e
Notes: sideshowbarker 2024-07-17 01:00:06 +09:00
2 changed files with 14 additions and 3 deletions

View File

@ -97,9 +97,20 @@ Bytes CircularBuffer::next_write_span()
return m_buffer.span().slice(m_reading_head + m_used_space, capacity() - (m_reading_head + m_used_space));
}
ReadonlyBytes CircularBuffer::next_read_span() const
ReadonlyBytes CircularBuffer::next_read_span(size_t offset) const
{
return m_buffer.span().slice(m_reading_head, min(capacity() - m_reading_head, m_used_space));
auto reading_head = m_reading_head;
auto used_space = m_used_space;
if (offset > 0) {
if (offset >= used_space)
return Bytes {};
reading_head = (reading_head + offset) % capacity();
used_space -= offset;
}
return m_buffer.span().slice(reading_head, min(capacity() - reading_head, used_space));
}
ReadonlyBytes CircularBuffer::next_read_span_with_seekback(size_t distance) const

View File

@ -50,7 +50,7 @@ protected:
[[nodiscard]] bool is_wrapping_around() const;
[[nodiscard]] Bytes next_write_span();
[[nodiscard]] ReadonlyBytes next_read_span() const;
[[nodiscard]] ReadonlyBytes next_read_span(size_t offset = 0) const;
[[nodiscard]] ReadonlyBytes next_read_span_with_seekback(size_t distance) const;
ByteBuffer m_buffer {};