AK: Add FlyString::is_one_of for variadic string comparison

This commit is contained in:
Kenneth Myhra 2023-03-24 18:25:10 +01:00 committed by Linus Groh
parent 3aa485aa09
commit d6cf9f5329
Notes: sideshowbarker 2024-07-16 23:52:22 +09:00
2 changed files with 22 additions and 0 deletions

View file

@ -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.

View file

@ -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));
}