From e37377600db4a53bed2ccffcad9806529a18d105 Mon Sep 17 00:00:00 2001 From: Jonne Ransijn Date: Thu, 10 Apr 2025 15:47:41 +0200 Subject: [PATCH] AK: Allow `Optional<{,Fly}String>` to be used in constant expressions --- AK/FlyString.h | 38 +++++++++++++++++++------------------- AK/String.h | 32 ++++++++++++++++---------------- AK/StringBase.h | 4 ++-- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/AK/FlyString.h b/AK/FlyString.h index 7bf52e82d15..f6c72cda764 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -85,19 +85,19 @@ public: private: friend class Optional; - explicit FlyString(nullptr_t) + explicit constexpr FlyString(nullptr_t) : m_data(nullptr) { } - explicit FlyString(Detail::StringBase data) + explicit constexpr FlyString(Detail::StringBase data) : m_data(move(data)) { } Detail::StringBase m_data; - bool is_invalid() const { return m_data.raw(Badge {}) == 0; } + constexpr bool is_invalid() const { return m_data.raw(Badge {}) == 0; } }; void did_destroy_fly_string_data(Badge, Detail::StringData const&); @@ -110,38 +110,38 @@ class Optional : public OptionalBase { public: using ValueType = FlyString; - Optional() = default; + constexpr Optional() = default; template V> - Optional(V) { } + constexpr Optional(V) { } - Optional(Optional const& other) + constexpr Optional(Optional const& other) { if (other.has_value()) m_value = other.m_value; } - Optional(Optional&& other) - : m_value(other.m_value) + constexpr Optional(Optional&& other) + : m_value(move(other.m_value)) { } template requires(!IsSame>) - explicit(!IsConvertible) Optional(U&& value) + explicit(!IsConvertible) constexpr Optional(U&& value) requires(!IsSame, Optional> && IsConstructible) : m_value(forward(value)) { } template V> - Optional& operator=(V) + constexpr Optional& operator=(V) { clear(); return *this; } - Optional& operator=(Optional const& other) + constexpr Optional& operator=(Optional const& other) { if (this != &other) { clear(); @@ -150,7 +150,7 @@ public: return *this; } - Optional& operator=(Optional&& other) + constexpr Optional& operator=(Optional&& other) { if (this != &other) { clear(); @@ -159,37 +159,37 @@ public: return *this; } - void clear() + constexpr void clear() { m_value = FlyString(nullptr); } - [[nodiscard]] bool has_value() const + [[nodiscard]] constexpr bool has_value() const { return !m_value.is_invalid(); } - [[nodiscard]] FlyString& value() & + [[nodiscard]] constexpr FlyString& value() & { VERIFY(has_value()); return m_value; } - [[nodiscard]] FlyString const& value() const& + [[nodiscard]] constexpr FlyString const& value() const& { VERIFY(has_value()); return m_value; } - [[nodiscard]] FlyString value() && + [[nodiscard]] constexpr FlyString value() && { return release_value(); } - [[nodiscard]] FlyString release_value() + [[nodiscard]] constexpr FlyString release_value() { VERIFY(has_value()); - FlyString released_value = m_value; + FlyString released_value = move(m_value); clear(); return released_value; } diff --git a/AK/String.h b/AK/String.h index f0ba2a58783..da02061ea73 100644 --- a/AK/String.h +++ b/AK/String.h @@ -227,7 +227,7 @@ private: using ShortString = Detail::ShortString; - bool is_invalid() const + constexpr bool is_invalid() const { return raw(Badge {}) == 0; } @@ -251,38 +251,38 @@ class Optional : public OptionalBase { public: using ValueType = String; - Optional() = default; + constexpr Optional() = default; template V> - Optional(V) { } + constexpr Optional(V) { } - Optional(Optional const& other) + constexpr Optional(Optional const& other) { if (other.has_value()) m_value = other.m_value; } - Optional(Optional&& other) + constexpr Optional(Optional&& other) : m_value(move(other.m_value)) { } template requires(!IsSame>) - explicit(!IsConvertible) Optional(U&& value) + explicit(!IsConvertible) constexpr Optional(U&& value) requires(!IsSame, Optional> && IsConstructible) : m_value(forward(value)) { } template V> - Optional& operator=(V) + constexpr Optional& operator=(V) { clear(); return *this; } - Optional& operator=(Optional const& other) + constexpr Optional& operator=(Optional const& other) { if (this != &other) { m_value = other.m_value; @@ -290,7 +290,7 @@ public: return *this; } - Optional& operator=(Optional&& other) + constexpr Optional& operator=(Optional&& other) { if (this != &other) { m_value = move(other.m_value); @@ -298,37 +298,37 @@ public: return *this; } - void clear() + constexpr void clear() { m_value = String(nullptr); } - [[nodiscard]] bool has_value() const + [[nodiscard]] constexpr bool has_value() const { return !m_value.is_invalid(); } - [[nodiscard]] String& value() & + [[nodiscard]] constexpr String& value() & { VERIFY(has_value()); return m_value; } - [[nodiscard]] String const& value() const& + [[nodiscard]] constexpr String const& value() const& { VERIFY(has_value()); return m_value; } - [[nodiscard]] String value() && + [[nodiscard]] constexpr String value() && { return release_value(); } - [[nodiscard]] String release_value() + [[nodiscard]] constexpr String release_value() { VERIFY(has_value()); - String released_value = m_value; + String released_value = move(m_value); clear(); return released_value; } diff --git a/AK/StringBase.h b/AK/StringBase.h index df468afd4c5..ab74b1d5727 100644 --- a/AK/StringBase.h +++ b/AK/StringBase.h @@ -70,8 +70,8 @@ public: [[nodiscard]] bool operator==(StringBase const&) const; - [[nodiscard]] ALWAYS_INLINE FlatPtr raw(Badge) const { return bit_cast(m_impl); } - [[nodiscard]] ALWAYS_INLINE FlatPtr raw(Badge) const { return bit_cast(m_impl); } + [[nodiscard]] ALWAYS_INLINE constexpr FlatPtr raw(Badge) const { return bit_cast(m_impl); } + [[nodiscard]] ALWAYS_INLINE constexpr FlatPtr raw(Badge) const { return bit_cast(m_impl); } template ALWAYS_INLINE ErrorOr replace_with_new_string(Badge, size_t byte_count, Func&& callback)