From 832e9b8302087e1de49843b8b6db7581226003c8 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 20 Feb 2023 14:04:42 -0500 Subject: [PATCH] AK: Prepare Utf32View for use within templated LibGfx contexts Forward declare its iterator and add a peek() method analagous to Utf8CodePointIterator::peek(). --- AK/Forward.h | 2 ++ AK/Utf32View.cpp | 18 ++++++++++++++++++ AK/Utf32View.h | 3 +++ 3 files changed, 23 insertions(+) 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; }