From d6cf9f53297a8fe2e8455b0502cd458cd42ba12d Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Fri, 24 Mar 2023 18:25:10 +0100 Subject: [PATCH] AK: Add FlyString::is_one_of for variadic string comparison --- AK/FlyString.h | 6 ++++++ Tests/AK/TestFlyString.cpp | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/AK/FlyString.h b/AK/FlyString.h index a2e68cef47..f6f09b2122 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -58,6 +58,12 @@ public: // Compare this FlyString against another string with ASCII caseless matching. [[nodiscard]] bool equals_ignoring_ascii_case(FlyString const&) const; + template + [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts... strings) const + { + return (... || this->operator==(forward(strings))); + } + private: // This will hold either the pointer to the Detail::StringData it represents or the raw bytes of // an inlined short string. diff --git a/Tests/AK/TestFlyString.cpp b/Tests/AK/TestFlyString.cpp index ff0cd0d5f7..dc182c2b8f 100644 --- a/Tests/AK/TestFlyString.cpp +++ b/Tests/AK/TestFlyString.cpp @@ -118,3 +118,19 @@ TEST_CASE(moved_fly_string_becomes_empty) EXPECT_EQ(fly1, "thisisdefinitelymorethan7bytes"sv); EXPECT_EQ(FlyString::number_of_fly_strings(), 1u); } + +TEST_CASE(is_one_of) +{ + auto foo = MUST(FlyString::from_utf8("foo"sv)); + auto bar = MUST(FlyString::from_utf8("bar"sv)); + + EXPECT(foo.is_one_of(foo)); + EXPECT(foo.is_one_of(foo, bar)); + EXPECT(foo.is_one_of(bar, foo)); + EXPECT(!foo.is_one_of(bar)); + + EXPECT(!bar.is_one_of("foo"sv)); + EXPECT(bar.is_one_of("foo"sv, "bar"sv)); + EXPECT(bar.is_one_of("bar"sv, "foo"sv)); + EXPECT(bar.is_one_of("bar"sv)); +}