mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Generalize Span::contains_slow to use the Traits infrastructure
This allows, for example, checking if a Span<String> contains a value without having to allocate a String.
This commit is contained in:
parent
faf4ba63c2
commit
e4213f5767
Notes:
sideshowbarker
2024-07-18 04:46:35 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/e4213f5767 Pull-request: https://github.com/SerenityOS/serenity/pull/23595
2 changed files with 24 additions and 2 deletions
|
@ -210,10 +210,11 @@ public:
|
|||
return size();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool contains_slow(T const& value) const
|
||||
template<typename V>
|
||||
[[nodiscard]] constexpr bool contains_slow(V const& value) const
|
||||
{
|
||||
for (size_t i = 0; i < size(); ++i) {
|
||||
if (at(i) == value)
|
||||
if (Traits<RemoveReference<T>>::equals(at(i), value))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -138,3 +138,24 @@ TEST_CASE(starts_with)
|
|||
ReadonlyBytes hey_bytes_u8 { hey_array, 3 };
|
||||
EXPECT(bytes.starts_with(hey_bytes_u8));
|
||||
}
|
||||
|
||||
TEST_CASE(contains_slow)
|
||||
{
|
||||
Vector<String> list { "abc"_string, "def"_string, "ghi"_string };
|
||||
auto span = list.span();
|
||||
|
||||
EXPECT(span.contains_slow("abc"_string));
|
||||
EXPECT(span.contains_slow("abc"sv));
|
||||
|
||||
EXPECT(span.contains_slow("def"_string));
|
||||
EXPECT(span.contains_slow("def"sv));
|
||||
|
||||
EXPECT(span.contains_slow("ghi"_string));
|
||||
EXPECT(span.contains_slow("ghi"sv));
|
||||
|
||||
EXPECT(!span.contains_slow("whf"_string));
|
||||
EXPECT(!span.contains_slow("whf"sv));
|
||||
|
||||
EXPECT(!span.contains_slow(String {}));
|
||||
EXPECT(!span.contains_slow(StringView {}));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue