From f0517e2748d92a6959cc67929169064fdd5ce8a6 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Mon, 16 Jun 2025 18:20:51 -0700 Subject: [PATCH] replace various find_if calls C++11 has shorter equivalents. Signed-off-by: Rosen Penev --- src/command_download.cc | 7 +++---- src/command_groups.cc | 3 +-- src/core/view.cc | 2 +- src/display/window_http_queue.cc | 6 +++--- src/option_parser.cc | 2 +- src/rpc/object_storage.cc | 2 +- src/rpc/parse_options.cc | 2 +- 7 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/command_download.cc b/src/command_download.cc index 899f5748..e8933cba 100644 --- a/src/command_download.cc +++ b/src/command_download.cc @@ -362,8 +362,7 @@ f_multicall(core::Download* download, const torrent::Object::list_type& args) { for (torrent::FileList::const_iterator itr = download->file_list()->begin(), last = download->file_list()->end(); itr != last; itr++) { if (use_regex && - std::find_if(regex_list.begin(), regex_list.end(), - std::bind(&rak::regex::operator(), std::placeholders::_1, (*itr)->path()->as_string())) == regex_list.end()) + std::none_of(regex_list.begin(), regex_list.end(), [itr](const auto& r) { return r((*itr)->path()->as_string()); })) continue; torrent::Object::list_type& row = result.insert(result.end(), torrent::Object::create_list())->as_list(); @@ -557,7 +556,7 @@ d_list_push_back_unique(core::Download* download, const torrent::Object& rawArgs const torrent::Object& args = (rawArgs.is_list() && !rawArgs.as_list().empty()) ? rawArgs.as_list().front() : rawArgs; torrent::Object::list_type& list = download_get_variable(download, first_key, second_key).as_list(); - if (std::find_if(list.begin(), list.end(), [args](const torrent::Object& obj) { return torrent::object_equal(obj, args); }) == list.end()) + if (std::none_of(list.begin(), list.end(), [args](const torrent::Object& obj) { return torrent::object_equal(obj, args); })) list.push_back(rawArgs); return torrent::Object(); @@ -568,7 +567,7 @@ d_list_has(core::Download* download, const torrent::Object& rawArgs, const char* const torrent::Object& args = (rawArgs.is_list() && !rawArgs.as_list().empty()) ? rawArgs.as_list().front() : rawArgs; torrent::Object::list_type& list = download_get_variable(download, first_key, second_key).as_list(); - return (int64_t)(std::find_if(list.begin(), list.end(), [args](const torrent::Object& obj) { return torrent::object_equal(obj, args); }) != list.end()); + return (int64_t)(std::any_of(list.begin(), list.end(), [args](const auto& obj) { return torrent::object_equal(obj, args); })); } torrent::Object diff --git a/src/command_groups.cc b/src/command_groups.cc index 5fdb12b1..9ed61619 100644 --- a/src/command_groups.cc +++ b/src/command_groups.cc @@ -180,8 +180,7 @@ apply_cg_insert(const std::string& arg) { if (rpc::parse_whole_value_nothrow(arg.c_str(), &dummy)) throw torrent::input_error("Cannot use a value string as choke group name."); - if (arg.empty() || - std::find_if(cg_list_hack.begin(), cg_list_hack.end(), [&arg](torrent::choke_group* cg) { return arg == cg->name(); }) != cg_list_hack.end()) + if (arg.empty() || std::any_of(cg_list_hack.begin(), cg_list_hack.end(), [&arg](auto cg) { return arg == cg->name(); })) throw torrent::input_error("Duplicate name for choke group."); cg_list_hack.push_back(new torrent::choke_group()); diff --git a/src/core/view.cc b/src/core/view.cc index 9d1945df..c550c9a5 100644 --- a/src/core/view.cc +++ b/src/core/view.cc @@ -347,7 +347,7 @@ View::clear_filter_on() { inline void View::insert_visible(Download* d) { - iterator itr = std::find_if(begin_visible(), end_visible(), [&d, this](Download* d2) { return view_downloads_compare(m_sortNew)(d, d2); }); + auto itr = std::find_if(begin_visible(), end_visible(), [this, d](auto d2) { return view_downloads_compare(m_sortNew)(d, d2); }); m_size++; m_focus += (m_focus >= position(itr)); diff --git a/src/display/window_http_queue.cc b/src/display/window_http_queue.cc index 10b6a7dc..f7db3c54 100644 --- a/src/display/window_http_queue.cc +++ b/src/display/window_http_queue.cc @@ -119,9 +119,9 @@ WindowHttpQueue::receive_insert(torrent::net::HttpGet http_get) { void WindowHttpQueue::receive_erase(torrent::net::HttpGet http_get) { - Container::iterator itr = std::find_if(m_container.begin(), - m_container.end(), - [http_get](Node& n) { return http_get == n.m_http; }); + auto itr = std::find_if(m_container.begin(), + m_container.end(), + [http_get](auto& n) { return http_get == n.m_http; }); if (itr == m_container.end()) throw std::logic_error("WindowHttpQueue::receive_erase(...) tried to remove an object we don't have"); diff --git a/src/option_parser.cc b/src/option_parser.cc index 8164ca63..98a1bc84 100644 --- a/src/option_parser.cc +++ b/src/option_parser.cc @@ -88,7 +88,7 @@ bool OptionParser::has_flag(char flag, int argc, char** argv) { char options[3] = { '-', flag, '\0' }; - return std::find_if(argv, argv + argc, [&options](char* c) { return std::strcmp(c, options) == 0; }) != argv + argc; + return std::any_of(argv, argv + argc, [&options](char* c) { return std::strcmp(c, options) == 0; }); } std::string diff --git a/src/rpc/object_storage.cc b/src/rpc/object_storage.cc index 79ae6670..4497d9d8 100644 --- a/src/rpc/object_storage.cc +++ b/src/rpc/object_storage.cc @@ -241,7 +241,7 @@ object_storage::set_multi_key_obj(const torrent::raw_string& key, const std::str if (r_itr == m_rlookup.end()) r_itr = m_rlookup.insert(std::make_pair(cmd_key, rlookup_type::mapped_type())).first; - if (std::find_if(r_itr->second.begin(), r_itr->second.end(), [key](value_type* type) { return key == type->first; }) == r_itr->second.end()) + if (std::none_of(r_itr->second.begin(), r_itr->second.end(), [key](auto type) { return key == type->first; })) r_itr->second.push_back(&*itr); } diff --git a/src/rpc/parse_options.cc b/src/rpc/parse_options.cc index 4a63628a..9056a3f6 100644 --- a/src/rpc/parse_options.cc +++ b/src/rpc/parse_options.cc @@ -57,7 +57,7 @@ parse_option_flag(const std::string& option, parse_option_flag_type ftor) { if (first == next) throw torrent::input_error(option); - if (std::find_if(next, last, [](char c) { return !std::isspace(c, std::locale::classic()); }) != last) + if (std::any_of(next, last, [](char c) { return !std::isspace(c, std::locale::classic()); })) throw torrent::input_error(option); return ftor(std::string(first, next));