From fa1849b855c1f5877e5e14b16db194a14f0588f3 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 10 Nov 2024 10:57:48 +0000 Subject: [PATCH] Codechange: Use std::range::find_if where possible. --- src/build_vehicle_gui.cpp | 2 +- src/highscore.cpp | 2 +- src/linkgraph/linkgraphjob.h | 4 ++-- src/network/network.cpp | 2 +- src/network/network_crypto.cpp | 2 +- src/newgrf_text.cpp | 4 ++-- src/picker_gui.cpp | 2 +- src/saveload/waypoint_sl.cpp | 2 +- src/textfile_gui.cpp | 2 +- src/town_gui.cpp | 2 +- 10 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 1f91ef8a90..22c69a5215 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -1256,7 +1256,7 @@ struct BuildVehicleWindow : Window { /* Select the first unshaded engine in the list as default when opening the window */ EngineID engine = INVALID_ENGINE; - auto it = std::find_if(this->eng_list.begin(), this->eng_list.end(), [&](GUIEngineListItem &item) { return !HasFlag(item.flags, EngineDisplayFlags::Shaded); }); + auto it = std::ranges::find_if(this->eng_list, [&](GUIEngineListItem &item) { return !HasFlag(item.flags, EngineDisplayFlags::Shaded); }); if (it != this->eng_list.end()) engine = it->engine_id; this->SelectEngine(engine); } diff --git a/src/highscore.cpp b/src/highscore.cpp index 63fe367039..112b88d5a2 100644 --- a/src/highscore.cpp +++ b/src/highscore.cpp @@ -59,7 +59,7 @@ int8_t SaveHighScoreValue(const Company *c) auto &highscores = _highscore_table[SP_CUSTOM]; uint16_t score = c->old_economy[0].performance_history; - auto it = std::find_if(highscores.begin(), highscores.end(), [&score](auto &highscore) { return highscore.score <= score; }); + auto it = std::ranges::find_if(highscores, [&score](auto &highscore) { return highscore.score <= score; }); /* If we cannot find it, our score is not high enough. */ if (it == highscores.end()) return -1; diff --git a/src/linkgraph/linkgraphjob.h b/src/linkgraph/linkgraphjob.h index b84eaf6417..cca66efa92 100644 --- a/src/linkgraph/linkgraphjob.h +++ b/src/linkgraph/linkgraphjob.h @@ -101,7 +101,7 @@ public: */ EdgeAnnotation &operator[](NodeID to) { - auto it = std::find_if(this->edges.begin(), this->edges.end(), [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; }); + auto it = std::ranges::find_if(this->edges, [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; }); assert(it != this->edges.end()); return *it; } @@ -113,7 +113,7 @@ public: */ const EdgeAnnotation &operator[](NodeID to) const { - auto it = std::find_if(this->edges.begin(), this->edges.end(), [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; }); + auto it = std::ranges::find_if(this->edges, [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; }); assert(it != this->edges.end()); return *it; } diff --git a/src/network/network.cpp b/src/network/network.cpp index 991cbb1694..ba498b100b 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -167,7 +167,7 @@ bool NetworkCanJoinCompany(CompanyID company_id) */ static auto FindKey(auto *authorized_keys, std::string_view authorized_key) { - return std::find_if(authorized_keys->begin(), authorized_keys->end(), [authorized_key](auto &value) { return StrEqualsIgnoreCase(value, authorized_key); }); + return std::ranges::find_if(*authorized_keys, [authorized_key](auto &value) { return StrEqualsIgnoreCase(value, authorized_key); }); } /** diff --git a/src/network/network_crypto.cpp b/src/network/network_crypto.cpp index 38a1f9dd09..783a5e398c 100644 --- a/src/network/network_crypto.cpp +++ b/src/network/network_crypto.cpp @@ -363,7 +363,7 @@ NetworkAuthenticationServerHandler::ResponseResult X25519AuthenticationHandler:: NetworkAuthenticationMethod method = static_cast(p.Recv_uint8()); auto is_of_method = [method](Handler &handler) { return handler->GetAuthenticationMethod() == method; }; - auto it = std::find_if(handlers.begin(), handlers.end(), is_of_method); + auto it = std::ranges::find_if(handlers, is_of_method); if (it == handlers.end()) return INVALID; this->current_handler = it->get(); diff --git a/src/newgrf_text.cpp b/src/newgrf_text.cpp index 97eb460795..97ec944d80 100644 --- a/src/newgrf_text.cpp +++ b/src/newgrf_text.cpp @@ -560,7 +560,7 @@ StringID AddGRFString(uint32_t grfid, uint16_t stringid, uint8_t langid_to_add, } } - auto it = std::find_if(std::begin(_grf_text), std::end(_grf_text), [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; }); + auto it = std::ranges::find_if(_grf_text, [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; }); if (it == std::end(_grf_text)) { /* Too many strings allocated, return empty. */ if (_grf_text.size() == TAB_SIZE_NEWGRF) return STR_EMPTY; @@ -586,7 +586,7 @@ StringID AddGRFString(uint32_t grfid, uint16_t stringid, uint8_t langid_to_add, */ StringID GetGRFStringID(uint32_t grfid, StringID stringid) { - auto it = std::find_if(std::begin(_grf_text), std::end(_grf_text), [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; }); + auto it = std::ranges::find_if(_grf_text, [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; }); if (it != std::end(_grf_text)) { uint id = static_cast(it - std::begin(_grf_text)); return MakeStringID(TEXT_TAB_NEWGRF_START, id); diff --git a/src/picker_gui.cpp b/src/picker_gui.cpp index c4f4049a45..5f7410a4f8 100644 --- a/src/picker_gui.cpp +++ b/src/picker_gui.cpp @@ -612,7 +612,7 @@ void PickerWindow::EnsureSelectedTypeIsVisible() int class_index = this->callbacks.GetSelectedClass(); int index = this->callbacks.GetSelectedType(); - auto it = std::find_if(std::begin(this->types), std::end(this->types), [class_index, index](const auto &item) { return item.class_index == class_index && item.index == index; }); + auto it = std::ranges::find_if(this->types, [class_index, index](const auto &item) { return item.class_index == class_index && item.index == index; }); if (it == std::end(this->types)) return; int pos = static_cast(std::distance(std::begin(this->types), it)); diff --git a/src/saveload/waypoint_sl.cpp b/src/saveload/waypoint_sl.cpp index 9dc9de2376..733561a58d 100644 --- a/src/saveload/waypoint_sl.cpp +++ b/src/saveload/waypoint_sl.cpp @@ -87,7 +87,7 @@ void MoveWaypointsToBaseStations() * from the GRF ID / station index. */ for (OldWaypoint &wp : _old_waypoints) { const auto specs = StationClass::Get(STAT_CLASS_WAYP)->Specs(); - auto found = std::find_if(std::begin(specs), std::end(specs), [&wp](const StationSpec *spec) { return spec != nullptr && spec->grf_prop.grffile->grfid == wp.grfid && spec->grf_prop.local_id == wp.localidx; }); + auto found = std::ranges::find_if(specs, [&wp](const StationSpec *spec) { return spec != nullptr && spec->grf_prop.grffile->grfid == wp.grfid && spec->grf_prop.local_id == wp.localidx; }); if (found != std::end(specs)) wp.spec = *found; } } diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp index 2251b095b9..9a649ad323 100644 --- a/src/textfile_gui.cpp +++ b/src/textfile_gui.cpp @@ -312,7 +312,7 @@ void TextfileWindow::CheckHyperlinkClick(Point pt) size_t line_index; size_t subline; if (IsWidgetLowered(WID_TF_WRAPTEXT)) { - auto it = std::find_if(std::begin(this->lines), std::end(this->lines), [clicked_row](const Line &l) { return l.top <= clicked_row && l.bottom > clicked_row; }); + auto it = std::ranges::find_if(this->lines, [clicked_row](const Line &l) { return l.top <= clicked_row && l.bottom > clicked_row; }); if (it == this->lines.cend()) return; line_index = it - this->lines.cbegin(); subline = clicked_row - it->top; diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 8b011c9191..a3b0f6f7ff 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -1562,7 +1562,7 @@ public: dst.insert(item); } else { /* Search for spec by grfid and local index. */ - auto it = std::find_if(specs.begin(), specs.end(), [&item](const HouseSpec &spec) { return spec.grf_prop.grffile != nullptr && spec.grf_prop.grffile->grfid == item.grfid && spec.grf_prop.local_id == item.local_id; }); + auto it = std::ranges::find_if(specs, [&item](const HouseSpec &spec) { return spec.grf_prop.grffile != nullptr && spec.grf_prop.grffile->grfid == item.grfid && spec.grf_prop.local_id == item.local_id; }); if (it == specs.end()) { /* Not preset, hide from UI. */ dst.insert({item.grfid, item.local_id, -1, -1});