summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Myhra <kennethmyhra@gmail.com>2023-03-24 18:25:10 +0100
committerLinus Groh <mail@linusgroh.de>2023-04-06 23:49:08 +0200
commitd6cf9f53297a8fe2e8455b0502cd458cd42ba12d (patch)
tree27e8b7e6c50fa061d16a405da7c8f01a1b665c7d
parent3aa485aa09af5dc82aaf96ed24a9aa2796116a26 (diff)
downloadserenity-d6cf9f53297a8fe2e8455b0502cd458cd42ba12d.zip
AK: Add FlyString::is_one_of for variadic string comparison
-rw-r--r--AK/FlyString.h6
-rw-r--r--Tests/AK/TestFlyString.cpp16
2 files changed, 22 insertions, 0 deletions
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<typename... Ts>
+ [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts... strings) const
+ {
+ return (... || this->operator==(forward<Ts>(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));
+}