mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-08 05:27:14 +09:00
AK: Add an AK::find
helper to return a reference to the found value
This is often more convenient than dealing with iterators. This commit includes a couple conversions to find_value as examples.
This commit is contained in:
parent
9cab5dc0c9
commit
7f37a8f60f
Notes:
github-actions[bot]
2025-04-06 11:46:22 +00:00
Author: https://github.com/trflynn89
Commit: 7f37a8f60f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4237
5 changed files with 34 additions and 30 deletions
12
AK/Find.h
12
AK/Find.h
|
@ -7,6 +7,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Concepts.h>
|
#include <AK/Concepts.h>
|
||||||
|
#include <AK/Optional.h>
|
||||||
#include <AK/Traits.h>
|
#include <AK/Traits.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
|
||||||
|
@ -36,10 +37,21 @@ requires(requires(TIterator it) { it.index(); })
|
||||||
return find_if(first, last, [&]<typename T>(T const& entry) { return Traits<T>::equals(entry, value); }).index();
|
return find_if(first, last, [&]<typename T>(T const& entry) { return Traits<T>::equals(entry, value); }).index();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<IterableContainer Container, typename TUnaryPredicate>
|
||||||
|
[[nodiscard]] constexpr auto find_value(Container const& container, TUnaryPredicate&& pred)
|
||||||
|
-> Optional<decltype(*container.begin())>
|
||||||
|
{
|
||||||
|
auto it = find_if(container.begin(), container.end(), forward<TUnaryPredicate>(pred));
|
||||||
|
if (it != container.end())
|
||||||
|
return *it;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if USING_AK_GLOBALLY
|
#if USING_AK_GLOBALLY
|
||||||
using AK::find;
|
using AK::find;
|
||||||
using AK::find_if;
|
using AK::find_if;
|
||||||
using AK::find_index;
|
using AK::find_index;
|
||||||
|
using AK::find_value;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -901,13 +901,11 @@ static KeyCodeData key_code_data(u32 code_point)
|
||||||
{ 0xE027, {}, UIEvents::KeyCode::Key_Minus, UIEvents::KeyModifier::Mod_Keypad },
|
{ 0xE027, {}, UIEvents::KeyCode::Key_Minus, UIEvents::KeyModifier::Mod_Keypad },
|
||||||
});
|
});
|
||||||
|
|
||||||
auto it = find_if(key_code_data.begin(), key_code_data.end(), [&](auto const& data) {
|
auto data = find_value(key_code_data, [&](auto const& data) {
|
||||||
return data.key == code_point || data.alternate_key == code_point;
|
return data.key == code_point || data.alternate_key == code_point;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (it == key_code_data.end())
|
return data.value_or({ .key = code_point });
|
||||||
return { .key = code_point };
|
|
||||||
return *it;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/webdriver/#dfn-shifted-character
|
// https://w3c.github.io/webdriver/#dfn-shifted-character
|
||||||
|
|
|
@ -29,14 +29,9 @@ ReadonlySpan<AutocompleteEngine> autocomplete_engines()
|
||||||
|
|
||||||
Optional<AutocompleteEngine const&> find_autocomplete_engine_by_name(StringView name)
|
Optional<AutocompleteEngine const&> find_autocomplete_engine_by_name(StringView name)
|
||||||
{
|
{
|
||||||
auto it = AK::find_if(builtin_autocomplete_engines.begin(), builtin_autocomplete_engines.end(),
|
return find_value(builtin_autocomplete_engines, [&](auto const& engine) {
|
||||||
[&](auto const& engine) {
|
return engine.name == name;
|
||||||
return engine.name == name;
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if (it == builtin_autocomplete_engines.end())
|
|
||||||
return {};
|
|
||||||
return *it;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Autocomplete::Autocomplete() = default;
|
Autocomplete::Autocomplete() = default;
|
||||||
|
|
|
@ -30,28 +30,16 @@ ReadonlySpan<SearchEngine> search_engines()
|
||||||
|
|
||||||
Optional<SearchEngine const&> find_search_engine_by_name(StringView name)
|
Optional<SearchEngine const&> find_search_engine_by_name(StringView name)
|
||||||
{
|
{
|
||||||
auto it = AK::find_if(builtin_search_engines.begin(), builtin_search_engines.end(),
|
return find_value(builtin_search_engines, [&](auto const& engine) {
|
||||||
[&](auto const& engine) {
|
return engine.name == name;
|
||||||
return engine.name == name;
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if (it == builtin_search_engines.end())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
return *it;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_url)
|
Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_url)
|
||||||
{
|
{
|
||||||
auto it = AK::find_if(builtin_search_engines.begin(), builtin_search_engines.end(),
|
return find_value(builtin_search_engines, [&](auto const& engine) {
|
||||||
[&](auto const& engine) {
|
return engine.query_url == query_url;
|
||||||
return engine.query_url == query_url;
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if (it == builtin_search_engines.end())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
return *it;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String format_search_query_for_display(StringView query_url, StringView query)
|
String format_search_query_for_display(StringView query_url, StringView query)
|
||||||
|
|
|
@ -51,3 +51,14 @@ TEST_CASE(should_return_index_to_first_predicate_matching_value_in_container)
|
||||||
|
|
||||||
EXPECT(4 == AK::find_index(a.begin(), a.end(), 0));
|
EXPECT(4 == AK::find_index(a.begin(), a.end(), 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(find_value)
|
||||||
|
{
|
||||||
|
static constexpr Array array { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
|
||||||
|
|
||||||
|
auto value = find_value(array, [](auto value) { return value > 5; });
|
||||||
|
EXPECT_EQ(value, 6);
|
||||||
|
|
||||||
|
value = find_value(array, [](auto value) { return value == 12389; });
|
||||||
|
EXPECT(!value.has_value());
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue