From d12036132e62902a0158aea940890276029c5d98 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Thu, 1 Jun 2023 23:39:05 +0200 Subject: [PATCH] AK: Allow passing an offset to `CircularBuffer::next_read_span()` --- AK/CircularBuffer.cpp | 15 +++++++++++++-- AK/CircularBuffer.h | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/AK/CircularBuffer.cpp b/AK/CircularBuffer.cpp index dc42c8bd516..dbb57fc495d 100644 --- a/AK/CircularBuffer.cpp +++ b/AK/CircularBuffer.cpp @@ -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 diff --git a/AK/CircularBuffer.h b/AK/CircularBuffer.h index 11ba360c9c6..962e2a19c9b 100644 --- a/AK/CircularBuffer.h +++ b/AK/CircularBuffer.h @@ -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 {};