1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:17:36 +00:00

AK: Add an optional starting offset to CircularBuffer::offset_of

This parameter allows to start searching after an offset. For example,
to resume a search.

It is unfortunately a breaking change in API so this patch also modifies
one user and one test.
This commit is contained in:
Lucas CHOLLET 2023-01-07 13:09:20 -05:00 committed by Andrew Kaster
parent 34922c0cc0
commit 9a7accddb7
4 changed files with 57 additions and 9 deletions

View file

@ -52,19 +52,29 @@ bool CircularBuffer::is_wrapping_around() const
return capacity() <= m_reading_head + m_used_space;
}
Optional<size_t> CircularBuffer::offset_of(StringView needle, Optional<size_t> until) const
Optional<size_t> CircularBuffer::offset_of(StringView needle, Optional<size_t> from, Optional<size_t> until) const
{
auto const read_from = from.value_or(0);
auto const read_until = until.value_or(m_used_space);
VERIFY(read_from <= read_until);
Array<ReadonlyBytes, 2> spans {};
spans[0] = next_read_span();
if (spans[0].size() > read_until)
spans[0] = spans[0].trim(read_until);
else if (is_wrapping_around())
spans[1] = m_buffer.span().slice(0, read_until - spans[0].size());
if (read_from > 0)
spans[0] = spans[0].slice(min(spans[0].size(), read_from));
return AK::memmem(spans.begin(), spans.end(), needle.bytes());
if (spans[0].size() + read_from > read_until)
spans[0] = spans[0].trim(read_until - read_from);
if (is_wrapping_around())
spans[1] = m_buffer.span().slice(max(spans[0].size(), read_from) - spans[0].size(), min(read_until, m_used_space) - spans[0].size());
auto maybe_found = AK::memmem(spans.begin(), spans.end(), needle.bytes());
if (maybe_found.has_value())
*maybe_found += read_from;
return maybe_found;
}
void CircularBuffer::clear()