From e4fb837f4bf886e9688ea3c12e081a2d7b4e6a75 Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sat, 21 Sep 2024 18:09:34 -0700 Subject: [PATCH] Modernize `std::find_if` with ranges In BTEmu.cpp, `std::mem_fn` was not necessary for the predicate to compile. --- .../Core/Core/ConfigLoaders/GameConfigLoader.cpp | 7 +++---- Source/Core/Core/DSP/DSPTables.h | 5 ++--- Source/Core/Core/HW/AddressSpace.cpp | 14 ++++++-------- .../Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp | 4 ++-- Source/Core/Core/HotkeyManager.cpp | 4 ++-- Source/Core/Core/IOS/ES/ES.cpp | 7 +++---- Source/Core/Core/IOS/ES/Formats.cpp | 2 +- Source/Core/Core/IOS/ES/Identity.cpp | 2 +- Source/Core/Core/IOS/FS/HostBackend/File.cpp | 4 ++-- Source/Core/Core/IOS/IOSC.cpp | 3 +-- Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp | 3 +-- Source/Core/Core/IOS/USB/USBV5.cpp | 5 +++-- Source/Core/Core/IOS/USB/USB_HID/HIDv5.cpp | 9 ++++----- Source/Core/Core/IOS/USB/USB_VEN/VEN.cpp | 9 ++++----- Source/Core/Core/PowerPC/BreakPoints.cpp | 7 +++---- Source/Core/Core/WC24PatchEngine.cpp | 7 +++---- Source/Core/DiscIO/Blob.cpp | 4 ++-- Source/Core/DiscIO/RiivolutionPatcher.cpp | 2 +- Source/Core/DiscIO/VolumeVerifier.cpp | 3 +-- .../DolphinQt/Debugger/GekkoSyntaxHighlight.cpp | 7 +++---- Source/Core/DolphinQt/FIFO/FIFOAnalyzer.cpp | 11 ++++++----- Source/Core/UICommon/ResourcePack/ResourcePack.cpp | 7 +++---- Source/Core/VideoBackends/Metal/MTLStateTracker.mm | 4 ++-- Source/Core/VideoCommon/Statistics.cpp | 2 +- 24 files changed, 60 insertions(+), 72 deletions(-) diff --git a/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp b/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp index 1b5addcd28..020e8d6da3 100644 --- a/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp +++ b/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp @@ -153,10 +153,9 @@ static std::pair GetINILocationFromConfig(const Locati return it->first; static const INIToSectionMap& ini_to_section = GetINIToSectionMap(); - const auto it2 = - std::find_if(ini_to_section.begin(), ini_to_section.end(), [&location](const auto& entry) { - return entry.second.first == location.system && entry.second.second == location.section; - }); + const auto it2 = std::ranges::find_if(ini_to_section, [&location](const auto& entry) { + return entry.second.first == location.system && entry.second.second == location.section; + }); if (it2 != ini_to_section.end()) return {it2->first, location.key}; diff --git a/Source/Core/Core/DSP/DSPTables.h b/Source/Core/Core/DSP/DSPTables.h index e2ec833273..f58d9bfc0d 100644 --- a/Source/Core/Core/DSP/DSPTables.h +++ b/Source/Core/Core/DSP/DSPTables.h @@ -113,8 +113,7 @@ const DSPOPCTemplate* GetExtOpTemplate(UDSPInstruction inst); template auto FindByOpcode(UDSPInstruction opcode, const std::array& data) { - return std::find_if(data.cbegin(), data.cend(), [opcode](const auto& info) { - return (opcode & info.opcode_mask) == info.opcode; - }); + return std::ranges::find_if( + data, [opcode](const auto& info) { return (opcode & info.opcode_mask) == info.opcode; }); } } // namespace DSP diff --git a/Source/Core/Core/HW/AddressSpace.cpp b/Source/Core/Core/HW/AddressSpace.cpp index e9fc552d4d..adee6ba97c 100644 --- a/Source/Core/Core/HW/AddressSpace.cpp +++ b/Source/Core/Core/HW/AddressSpace.cpp @@ -331,18 +331,16 @@ private: std::vector::iterator FindAppropriateAccessor(const Core::CPUThreadGuard& guard, u32 address) { - return std::find_if(m_accessor_mappings.begin(), m_accessor_mappings.end(), - [&guard, address](const AccessorMapping& a) { - return a.accessors->IsValidAddress(guard, address - a.base); - }); + return std::ranges::find_if(m_accessor_mappings, [&guard, address](const AccessorMapping& a) { + return a.accessors->IsValidAddress(guard, address - a.base); + }); } std::vector::const_iterator FindAppropriateAccessor(const Core::CPUThreadGuard& guard, u32 address) const { - return std::find_if(m_accessor_mappings.begin(), m_accessor_mappings.end(), - [&guard, address](const AccessorMapping& a) { - return a.accessors->IsValidAddress(guard, address - a.base); - }); + return std::ranges::find_if(m_accessor_mappings, [&guard, address](const AccessorMapping& a) { + return a.accessors->IsValidAddress(guard, address - a.base); + }); } }; diff --git a/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp b/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp index a6a000fa78..c7be096c75 100644 --- a/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp +++ b/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp @@ -146,8 +146,8 @@ std::vector GCMemcardDirectory::GetFileNamesForGameID(const std::st if (!gci_file.ReadBytes(&gci.m_gci_header, Memcard::DENTRY_SIZE)) continue; - const auto same_identity_save_it = std::find_if( - loaded_saves.begin(), loaded_saves.end(), [&gci](const Memcard::DEntry& entry) { + const auto same_identity_save_it = + std::ranges::find_if(loaded_saves, [&gci](const Memcard::DEntry& entry) { return Memcard::HasSameIdentity(gci.m_gci_header, entry); }); if (same_identity_save_it != loaded_saves.end()) diff --git a/Source/Core/Core/HotkeyManager.cpp b/Source/Core/Core/HotkeyManager.cpp index 6981aaaa7d..5178377aef 100644 --- a/Source/Core/Core/HotkeyManager.cpp +++ b/Source/Core/Core/HotkeyManager.cpp @@ -420,8 +420,8 @@ ControllerEmu::ControlGroup* HotkeyManager::GetHotkeyGroup(HotkeyGroup group) co int HotkeyManager::FindGroupByID(int id) const { - const auto i = std::find_if(s_groups_info.begin(), s_groups_info.end(), - [id](const auto& entry) { return entry.last >= id; }); + const auto i = + std::ranges::find_if(s_groups_info, [id](const auto& entry) { return entry.last >= id; }); return static_cast(std::distance(s_groups_info.begin(), i)); } diff --git a/Source/Core/Core/IOS/ES/ES.cpp b/Source/Core/Core/IOS/ES/ES.cpp index 3634518f6f..bf388a5b67 100644 --- a/Source/Core/Core/IOS/ES/ES.cpp +++ b/Source/Core/Core/IOS/ES/ES.cpp @@ -527,14 +527,13 @@ void ESDevice::DoState(PointerWrap& p) ESDevice::ContextArray::iterator ESDevice::FindActiveContext(s32 fd) { - return std::find_if(m_contexts.begin(), m_contexts.end(), - [fd](const auto& context) { return context.ipc_fd == fd && context.active; }); + return std::ranges::find_if( + m_contexts, [fd](const auto& context) { return context.ipc_fd == fd && context.active; }); } ESDevice::ContextArray::iterator ESDevice::FindInactiveContext() { - return std::find_if(m_contexts.begin(), m_contexts.end(), - [](const auto& context) { return !context.active; }); + return std::ranges::find_if(m_contexts, [](const auto& context) { return !context.active; }); } std::optional ESDevice::Open(const OpenRequest& request) diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index 2826f7aed5..2442c74559 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -726,7 +726,7 @@ CertReader::CertReader(std::vector&& bytes) : SignedBlobReader(std::move(byt {SignatureType::ECC, PublicKeyType::ECC, sizeof(CertECC)}, }}; - const auto info = std::find_if(types.cbegin(), types.cend(), [this](const CertStructInfo& entry) { + const auto info = std::ranges::find_if(types, [this](const CertStructInfo& entry) { return m_bytes.size() >= std::get<2>(entry) && std::get<0>(entry) == GetSignatureType() && std::get<1>(entry) == GetPublicKeyType(); }); diff --git a/Source/Core/Core/IOS/ES/Identity.cpp b/Source/Core/Core/IOS/ES/Identity.cpp index 34fc6861f0..454e912f5a 100644 --- a/Source/Core/Core/IOS/ES/Identity.cpp +++ b/Source/Core/Core/IOS/ES/Identity.cpp @@ -139,7 +139,7 @@ ReturnCode ESCore::VerifySign(const std::vector& hash, const std::vector if (certs.empty()) return ES_EINVAL; - const auto ap_iterator = std::find_if(certs.begin(), certs.end(), [](const auto& entry) { + const auto ap_iterator = std::ranges::find_if(certs, [](const auto& entry) { return entry.first.length() > 2 && entry.first.compare(0, 2, "AP") == 0; }); if (ap_iterator == certs.end()) diff --git a/Source/Core/Core/IOS/FS/HostBackend/File.cpp b/Source/Core/Core/IOS/FS/HostBackend/File.cpp index 1379ebc049..17e42e0aec 100644 --- a/Source/Core/Core/IOS/FS/HostBackend/File.cpp +++ b/Source/Core/Core/IOS/FS/HostBackend/File.cpp @@ -207,8 +207,8 @@ Result HostFileSystem::GetFileStatus(Fd fd) HostFileSystem::Handle* HostFileSystem::AssignFreeHandle() { - const auto it = std::find_if(m_handles.begin(), m_handles.end(), - [](const Handle& handle) { return !handle.opened; }); + const auto it = + std::ranges::find_if(m_handles, [](const Handle& handle) { return !handle.opened; }); if (it == m_handles.end()) return nullptr; diff --git a/Source/Core/Core/IOS/IOSC.cpp b/Source/Core/Core/IOS/IOSC.cpp index 357aee143e..07248ca2a9 100644 --- a/Source/Core/Core/IOS/IOSC.cpp +++ b/Source/Core/Core/IOS/IOSC.cpp @@ -669,8 +669,7 @@ IOSC::KeyEntry::KeyEntry(ObjectType type_, ObjectSubType subtype_, std::vector #include #include +#include #include "Common/Assert.h" #include "Common/ChunkFile.h" @@ -215,8 +216,8 @@ void USBV5ResourceManager::OnDeviceChange(const ChangeEvent event, if (interface.bAlternateSetting != 0) continue; - auto it = std::find_if(m_usbv5_devices.rbegin(), m_usbv5_devices.rend(), - [](const USBV5Device& entry) { return !entry.in_use; }); + auto it = std::ranges::find_if(m_usbv5_devices | std::views::reverse, + [](const USBV5Device& entry) { return !entry.in_use; }); if (it == m_usbv5_devices.rend()) return; diff --git a/Source/Core/Core/IOS/USB/USB_HID/HIDv5.cpp b/Source/Core/Core/IOS/USB/USB_HID/HIDv5.cpp index 1fa3b58686..f126ff19a5 100644 --- a/Source/Core/Core/IOS/USB/USB_HID/HIDv5.cpp +++ b/Source/Core/Core/IOS/USB/USB_HID/HIDv5.cpp @@ -169,11 +169,10 @@ IPCReply USB_HIDv5::GetDeviceInfo(USBV5Device& device, const IOCtlRequest& reque memory.CopyToEmu(request.buffer_out + 56, &config_descriptor, sizeof(config_descriptor)); std::vector interfaces = host_device->GetInterfaces(0); - auto it = std::find_if(interfaces.begin(), interfaces.end(), - [&](const USB::InterfaceDescriptor& interface) { - return interface.bInterfaceNumber == device.interface_number && - interface.bAlternateSetting == alt_setting; - }); + auto it = std::ranges::find_if(interfaces, [&](const USB::InterfaceDescriptor& interface) { + return interface.bInterfaceNumber == device.interface_number && + interface.bAlternateSetting == alt_setting; + }); if (it == interfaces.end()) return IPCReply(IPC_EINVAL); it->Swap(); diff --git a/Source/Core/Core/IOS/USB/USB_VEN/VEN.cpp b/Source/Core/Core/IOS/USB/USB_VEN/VEN.cpp index eec7df9392..a0dd4393d8 100644 --- a/Source/Core/Core/IOS/USB/USB_VEN/VEN.cpp +++ b/Source/Core/Core/IOS/USB/USB_VEN/VEN.cpp @@ -152,11 +152,10 @@ IPCReply USB_VEN::GetDeviceInfo(USBV5Device& device, const IOCtlRequest& request memory.CopyToEmu(request.buffer_out + 40, &config_descriptor, sizeof(config_descriptor)); std::vector interfaces = host_device->GetInterfaces(0); - auto it = std::find_if(interfaces.begin(), interfaces.end(), - [&](const USB::InterfaceDescriptor& interface) { - return interface.bInterfaceNumber == device.interface_number && - interface.bAlternateSetting == alt_setting; - }); + auto it = std::ranges::find_if(interfaces, [&](const USB::InterfaceDescriptor& interface) { + return interface.bInterfaceNumber == device.interface_number && + interface.bAlternateSetting == alt_setting; + }); if (it == interfaces.end()) return IPCReply(IPC_EINVAL); it->Swap(); diff --git a/Source/Core/Core/PowerPC/BreakPoints.cpp b/Source/Core/Core/PowerPC/BreakPoints.cpp index 565dc961e5..1fef4e4e25 100644 --- a/Source/Core/Core/PowerPC/BreakPoints.cpp +++ b/Source/Core/Core/PowerPC/BreakPoints.cpp @@ -352,10 +352,9 @@ void MemChecks::Clear() TMemCheck* MemChecks::GetMemCheck(u32 address, size_t size) { - const auto iter = - std::find_if(m_mem_checks.begin(), m_mem_checks.end(), [address, size](const auto& mc) { - return mc.end_address >= address && address + size - 1 >= mc.start_address; - }); + const auto iter = std::ranges::find_if(m_mem_checks, [address, size](const auto& mc) { + return mc.end_address >= address && address + size - 1 >= mc.start_address; + }); // None found if (iter == m_mem_checks.cend()) diff --git a/Source/Core/Core/WC24PatchEngine.cpp b/Source/Core/Core/WC24PatchEngine.cpp index 7a9b5a25ff..f16efba192 100644 --- a/Source/Core/Core/WC24PatchEngine.cpp +++ b/Source/Core/Core/WC24PatchEngine.cpp @@ -117,10 +117,9 @@ void Reload() std::optional GetNetworkPatch(std::string_view source, IsKD is_kd) { - const auto patch = - std::find_if(s_patches.begin(), s_patches.end(), [&source, &is_kd](const NetworkPatch& p) { - return p.source == source && p.is_kd == is_kd && p.enabled; - }); + const auto patch = std::ranges::find_if(s_patches, [&source, &is_kd](const NetworkPatch& p) { + return p.source == source && p.is_kd == is_kd && p.enabled; + }); if (patch == s_patches.end()) return std::nullopt; diff --git a/Source/Core/DiscIO/Blob.cpp b/Source/Core/DiscIO/Blob.cpp index 1109b00a1b..aa83504fd3 100644 --- a/Source/Core/DiscIO/Blob.cpp +++ b/Source/Core/DiscIO/Blob.cpp @@ -84,8 +84,8 @@ SectorReader::~SectorReader() const SectorReader::Cache* SectorReader::FindCacheLine(u64 block_num) { - auto itr = std::find_if(m_cache.begin(), m_cache.end(), - [&](const Cache& entry) { return entry.Contains(block_num); }); + auto itr = + std::ranges::find_if(m_cache, [&](const Cache& entry) { return entry.Contains(block_num); }); if (itr == m_cache.end()) return nullptr; diff --git a/Source/Core/DiscIO/RiivolutionPatcher.cpp b/Source/Core/DiscIO/RiivolutionPatcher.cpp index 8c54968c35..eff36077f9 100644 --- a/Source/Core/DiscIO/RiivolutionPatcher.cpp +++ b/Source/Core/DiscIO/RiivolutionPatcher.cpp @@ -366,7 +366,7 @@ static FSTBuilderNode* FindFileNodeInFST(std::string_view path, std::vectorbegin(), fst->end(), [&](const FSTBuilderNode& node) { + const auto it = std::ranges::find_if(*fst, [&](const FSTBuilderNode& node) { return Common::CaseInsensitiveEquals(node.m_filename, name); }); diff --git a/Source/Core/DiscIO/VolumeVerifier.cpp b/Source/Core/DiscIO/VolumeVerifier.cpp index 5a4a9c6856..e6da0fcc3e 100644 --- a/Source/Core/DiscIO/VolumeVerifier.cpp +++ b/Source/Core/DiscIO/VolumeVerifier.cpp @@ -472,8 +472,7 @@ std::vector VolumeVerifier::CheckPartitions() AddProblem(Severity::High, Common::GetStringT("The install partition is missing.")); if (ShouldHaveMasterpiecePartitions() && - types.cend() == - std::find_if(types.cbegin(), types.cend(), [](u32 type) { return type >= 0xFF; })) + types.cend() == std::ranges::find_if(types, [](u32 type) { return type >= 0xFF; })) { // i18n: This string is referring to a game mode in Super Smash Bros. Brawl called Masterpieces // where you play demos of NES/SNES/N64 games. Official translations: diff --git a/Source/Core/DolphinQt/Debugger/GekkoSyntaxHighlight.cpp b/Source/Core/DolphinQt/Debugger/GekkoSyntaxHighlight.cpp index 0523944f63..d29bf100db 100644 --- a/Source/Core/DolphinQt/Debugger/GekkoSyntaxHighlight.cpp +++ b/Source/Core/DolphinQt/Debugger/GekkoSyntaxHighlight.cpp @@ -162,10 +162,9 @@ void GekkoSyntaxHighlight::highlightBlock(const QString& text) } else if (m_mode == 1) { - auto paren_it = std::find_if(info->parens.begin(), info->parens.end(), - [this](const std::pair& p) { - return p.first == m_cursor_loc || p.second == m_cursor_loc; - }); + auto paren_it = std::ranges::find_if(info->parens, [this](const std::pair& p) { + return p.first == m_cursor_loc || p.second == m_cursor_loc; + }); if (paren_it != info->parens.end()) { HighlightSubstr(paren_it->first, 1, HighlightFormat::Paren); diff --git a/Source/Core/DolphinQt/FIFO/FIFOAnalyzer.cpp b/Source/Core/DolphinQt/FIFO/FIFOAnalyzer.cpp index e33ba4c214..09cc7edb71 100644 --- a/Source/Core/DolphinQt/FIFO/FIFOAnalyzer.cpp +++ b/Source/Core/DolphinQt/FIFO/FIFOAnalyzer.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -472,9 +473,8 @@ void FIFOAnalyzer::FindNext() const int index = m_detail_list->currentRow(); ASSERT(index >= 0); - auto next_result = - std::find_if(m_search_results.begin(), m_search_results.end(), - [index](auto& result) { return result.m_cmd > static_cast(index); }); + auto next_result = std::ranges::find_if( + m_search_results, [index](auto& result) { return result.m_cmd > static_cast(index); }); if (next_result != m_search_results.end()) { ShowSearchResult(next_result - m_search_results.begin()); @@ -487,8 +487,9 @@ void FIFOAnalyzer::FindPrevious() ASSERT(index >= 0); auto prev_result = - std::find_if(m_search_results.rbegin(), m_search_results.rend(), - [index](auto& result) { return result.m_cmd < static_cast(index); }); + std::ranges::find_if(m_search_results | std::views::reverse, [index](auto& result) { + return result.m_cmd < static_cast(index); + }); if (prev_result != m_search_results.rend()) { ShowSearchResult((m_search_results.rend() - prev_result) - 1); diff --git a/Source/Core/UICommon/ResourcePack/ResourcePack.cpp b/Source/Core/UICommon/ResourcePack/ResourcePack.cpp index 659e8808a1..c55377c67b 100644 --- a/Source/Core/UICommon/ResourcePack/ResourcePack.cpp +++ b/Source/Core/UICommon/ResourcePack/ResourcePack.cpp @@ -170,10 +170,9 @@ bool ResourcePack::Install(const std::string& path) continue; const std::string texture_name = texture_zip_path.substr(texture_zip_path_prefix.size()); - auto texture_it = std::find_if( - m_textures.cbegin(), m_textures.cend(), [&texture_name](const std::string& texture) { - return mz_path_compare_wc(texture.c_str(), texture_name.c_str(), 1) == MZ_OK; - }); + auto texture_it = std::ranges::find_if(m_textures, [&texture_name](const std::string& texture) { + return mz_path_compare_wc(texture.c_str(), texture_name.c_str(), 1) == MZ_OK; + }); if (texture_it == m_textures.cend()) continue; const auto texture = *texture_it; diff --git a/Source/Core/VideoBackends/Metal/MTLStateTracker.mm b/Source/Core/VideoBackends/Metal/MTLStateTracker.mm index b67c4162b1..cfab208b8f 100644 --- a/Source/Core/VideoBackends/Metal/MTLStateTracker.mm +++ b/Source/Core/VideoBackends/Metal/MTLStateTracker.mm @@ -61,8 +61,8 @@ static NSString* GetName(Metal::StateTracker::UploadBuffer buffer) bool Metal::StateTracker::UsageTracker::PrepareForAllocation(u64 last_draw, size_t amt) { - auto removeme = std::find_if(m_usage.begin(), m_usage.end(), - [last_draw](UsageEntry usage) { return usage.drawno > last_draw; }); + auto removeme = std::ranges::find_if( + m_usage, [last_draw](UsageEntry usage) { return usage.drawno > last_draw; }); if (removeme != m_usage.begin()) m_usage.erase(m_usage.begin(), removeme); diff --git a/Source/Core/VideoCommon/Statistics.cpp b/Source/Core/VideoCommon/Statistics.cpp index 378bffcd2a..311455f16d 100644 --- a/Source/Core/VideoCommon/Statistics.cpp +++ b/Source/Core/VideoCommon/Statistics.cpp @@ -176,7 +176,7 @@ void Statistics::AddScissorRect() } else { - add = std::find_if(scissors.begin(), scissors.end(), [&](auto& s) { + add = std::ranges::find_if(scissors, [&](auto& s) { return s.Matches(scissor, show_scissors, show_viewports); }) == scissors.end(); }