mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-19 16:59:47 +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:
parent
31f6ba0952
commit
14506e8f5e
Notes:
sideshowbarker
2024-07-18 17:04:53 +09:00
Author: https://github.com/MaxWipfli
Commit: 14506e8f5e
Pull-request: https://github.com/SerenityOS/serenity/pull/7478
Reviewed-by: https://github.com/awesomekling
3 changed files with 55 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue