diff --git a/AK/Forward.h b/AK/Forward.h index b58158dd8c8..081333ebd9f 100644 --- a/AK/Forward.h +++ b/AK/Forward.h @@ -44,6 +44,7 @@ class Time; class URL; class String; class Utf16View; +class Utf32CodePointIterator; class Utf32View; class Utf8CodePointIterator; class Utf8View; @@ -201,6 +202,7 @@ using AK::Time; using AK::Traits; using AK::URL; using AK::Utf16View; +using AK::Utf32CodePointIterator; using AK::Utf32View; using AK::Utf8CodePointIterator; using AK::Utf8View; diff --git a/AK/Utf32View.cpp b/AK/Utf32View.cpp index de18abb9589..ebb4bbd1f7a 100644 --- a/AK/Utf32View.cpp +++ b/AK/Utf32View.cpp @@ -9,6 +9,24 @@ namespace AK { +Optional Utf32CodePointIterator::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; +} + ErrorOr Formatter::format(FormatBuilder& builder, Utf32View const& string) { return builder.builder().try_append(string); diff --git a/AK/Utf32View.h b/AK/Utf32View.h index 2c3c122f99e..8009879c9c0 100644 --- a/AK/Utf32View.h +++ b/AK/Utf32View.h @@ -43,6 +43,9 @@ public: return *m_ptr; } + // NOTE: This returns {} if the peek is at or past EOF. + Optional peek(size_t offset = 0) const; + constexpr int code_point_length_in_bytes() const { return sizeof(u32); } bool done() const { return !m_length; }