1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

AK: Implement Utf8CodepointIterator::peek(size_t)

This adds a peek method for Utf8CodepointIterator, which enables it to
be used in some parsing cases where peeking is necessary.

peek(0) is equivalent to operator*, expect that peek() does not contain
any assertions and will just return an empty Optional<u32>.

This also implements a test case for iterating UTF-8.
This commit is contained in:
Max Wipfli 2021-05-24 00:29:16 +02:00 committed by Andreas Kling
parent 31f6ba0952
commit 14506e8f5e
3 changed files with 55 additions and 0 deletions

View file

@ -240,4 +240,21 @@ u32 Utf8CodepointIterator::operator*() const
return code_point_value_so_far;
}
Optional<u32> Utf8CodepointIterator::peek(size_t offset) const
{
if (offset == 0) {
if (this->done())
return {};
return this->operator*();
}
auto new_iterator = *this;
for (size_t index = 0; index < offset; ++index) {
++new_iterator;
if (new_iterator.done())
return {};
}
return *new_iterator;
}
}