mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 11:57:35 +00:00
AK: Add String::is_one_of for variadic string comparison
This commit is contained in:
parent
9db9b2f9be
commit
bd9b65e82f
2 changed files with 22 additions and 0 deletions
|
@ -86,6 +86,12 @@ public:
|
||||||
// NOTE: UTF-8 is defined in a way that lexicographic ordering of code points is equivalent to lexicographic ordering of bytes.
|
// NOTE: UTF-8 is defined in a way that lexicographic ordering of code points is equivalent to lexicographic ordering of bytes.
|
||||||
[[nodiscard]] int operator<=>(String const& other) const { return this->bytes_as_string_view().compare(other.bytes_as_string_view()); }
|
[[nodiscard]] int operator<=>(String const& other) const { return this->bytes_as_string_view().compare(other.bytes_as_string_view()); }
|
||||||
|
|
||||||
|
template<typename... Ts>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const
|
||||||
|
{
|
||||||
|
return (this->operator==(forward<Ts>(strings)) || ...);
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] u32 hash() const;
|
[[nodiscard]] u32 hash() const;
|
||||||
|
|
||||||
template<Arithmetic T>
|
template<Arithmetic T>
|
||||||
|
|
|
@ -162,3 +162,19 @@ TEST_CASE(to_uppercase)
|
||||||
EXPECT_EQ(result, "ʼN"sv);
|
EXPECT_EQ(result, "ʼN"sv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(is_one_of)
|
||||||
|
{
|
||||||
|
auto foo = MUST(String::from_utf8("foo"sv));
|
||||||
|
auto bar = MUST(String::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));
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue