From 0352f24a8ee0b8c079df065b5155ea576727db3d Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Thu, 22 Aug 2024 18:17:26 -0700 Subject: [PATCH 1/5] Modernize `std::mismatch` with ranges The new return value is `std::ranges::mismatch_result`, an alias for the pair-like type `std::ranges::in_in_result`. --- Source/Core/Core/Movie.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Core/Core/Movie.cpp b/Source/Core/Core/Movie.cpp index 89e6d45d31..19841d8cfc 100644 --- a/Source/Core/Core/Movie.cpp +++ b/Source/Core/Core/Movie.cpp @@ -1081,11 +1081,11 @@ void MovieManager::LoadInput(const std::string& movie_path) std::vector movInput(m_current_byte); t_record.ReadArray(movInput.data(), movInput.size()); - const auto result = std::mismatch(movInput.begin(), movInput.end(), m_temp_input.begin()); + const auto mismatch_result = std::ranges::mismatch(movInput, m_temp_input); - if (result.first != movInput.end()) + if (mismatch_result.in1 != movInput.end()) { - const ptrdiff_t mismatch_index = std::distance(movInput.begin(), result.first); + const ptrdiff_t mismatch_index = std::distance(movInput.begin(), mismatch_result.in1); // this is a "you did something wrong" alert for the user's benefit. // we'll try to say what's going on in excruciating detail, otherwise the user might not From 4fde0f2868212afc216cd9324af8d5efcc282887 Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:00:41 -0700 Subject: [PATCH 2/5] Modernize `std::search` with ranges The new return value is `std::ranges::subrange`. --- Source/Core/DiscIO/NANDImporter.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index 8ab09da8b3..80b52951b0 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -240,11 +240,9 @@ bool NANDImporter::ExtractCertificates() for (const PEMCertificate& certificate : certificates) { - const auto search_result = - std::search(content_bytes.begin(), content_bytes.end(), certificate.search_bytes.begin(), - certificate.search_bytes.end()); + const auto search_result = std::ranges::search(content_bytes, certificate.search_bytes); - if (search_result == content_bytes.end()) + if (search_result.empty()) { ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not find offset for certficate '{}'", certificate.filename); @@ -252,7 +250,8 @@ bool NANDImporter::ExtractCertificates() } const std::string pem_file_path = m_nand_root + std::string(certificate.filename); - const ptrdiff_t certificate_offset = std::distance(content_bytes.begin(), search_result); + const ptrdiff_t certificate_offset = + std::distance(content_bytes.begin(), search_result.begin()); constexpr int min_offset = 2; if (certificate_offset < min_offset) { From be0b13da97121d9ca1b1d454cf3a36bcccb5173b Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sat, 28 Sep 2024 20:18:33 -0700 Subject: [PATCH 3/5] Simplify `std::remove` with `std::erase` `std::erase` is a replacement for the remove-erase idiom. Changes to `OpenModeToAndroid` inadvertently revealed that the prior implementation had UB (potentially deleting the end iterator). This is now fixed. --- Source/Android/jni/AndroidCommon/AndroidCommon.cpp | 2 +- Source/Core/DolphinQt/Settings.cpp | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Source/Android/jni/AndroidCommon/AndroidCommon.cpp b/Source/Android/jni/AndroidCommon/AndroidCommon.cpp index 25ec479790..a976d573b8 100644 --- a/Source/Android/jni/AndroidCommon/AndroidCommon.cpp +++ b/Source/Android/jni/AndroidCommon/AndroidCommon.cpp @@ -62,7 +62,7 @@ bool IsPathAndroidContent(std::string_view uri) std::string OpenModeToAndroid(std::string mode) { // The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it. - mode.erase(std::remove(mode.begin(), mode.end(), 'b')); + std::erase(mode, 'b'); if (mode == "r") return "r"; diff --git a/Source/Core/DolphinQt/Settings.cpp b/Source/Core/DolphinQt/Settings.cpp index 8d4a3513b9..938e892962 100644 --- a/Source/Core/DolphinQt/Settings.cpp +++ b/Source/Core/DolphinQt/Settings.cpp @@ -309,11 +309,9 @@ void Settings::RemovePath(const QString& qpath) std::string path = qpath.toStdString(); std::vector paths = Config::GetIsoPaths(); - auto new_end = std::remove(paths.begin(), paths.end(), path); - if (new_end == paths.end()) + if (std::erase(paths, path) == 0) return; - paths.erase(new_end, paths.end()); Config::SetIsoPaths(paths); emit PathRemoved(qpath); } From 5f3a8ff0de9d89ec162e1d2be7611b10f3d3740f Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sat, 28 Sep 2024 20:19:03 -0700 Subject: [PATCH 4/5] Modernize `std::unique` with ranges The new return value is `std::ranges::subrange`. --- Source/Core/Common/FileSearch.cpp | 3 ++- Source/Core/InputCommon/ControllerInterface/MappingCommon.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Core/Common/FileSearch.cpp b/Source/Core/Common/FileSearch.cpp index 4a54c978c9..a92c50f7d1 100644 --- a/Source/Core/Common/FileSearch.cpp +++ b/Source/Core/Common/FileSearch.cpp @@ -98,7 +98,8 @@ std::vector DoFileSearch(const std::vector& directorie // not because std::filesystem returns duplicates). Also note that this pathname-based uniqueness // isn't as thorough as std::filesystem::equivalent. std::ranges::sort(result); - result.erase(std::unique(result.begin(), result.end()), result.end()); + const auto unique_result = std::ranges::unique(result); + result.erase(unique_result.begin(), unique_result.end()); // Dolphin expects to be able to use "/" (DIR_SEP) everywhere. // std::filesystem uses the OS separator. diff --git a/Source/Core/InputCommon/ControllerInterface/MappingCommon.cpp b/Source/Core/InputCommon/ControllerInterface/MappingCommon.cpp index 1877e8294c..b50621bc92 100644 --- a/Source/Core/InputCommon/ControllerInterface/MappingCommon.cpp +++ b/Source/Core/InputCommon/ControllerInterface/MappingCommon.cpp @@ -129,7 +129,8 @@ BuildExpression(const std::vector // Remove duplicates std::ranges::sort(alternations); - alternations.erase(std::unique(alternations.begin(), alternations.end()), alternations.end()); + const auto unique_result = std::ranges::unique(alternations); + alternations.erase(unique_result.begin(), unique_result.end()); return fmt::to_string(fmt::join(alternations, "|")); } From 3d0d03b871d7ea2cd668abfbcb3ba1ecf0e77ceb Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sat, 28 Sep 2024 20:32:59 -0700 Subject: [PATCH 5/5] Modernize `std::partition` with ranges The new return value is `std::ranges::subrange`. --- Source/Core/Core/IOS/Network/Socket.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Core/Core/IOS/Network/Socket.cpp b/Source/Core/Core/IOS/Network/Socket.cpp index b5ab5ffc4e..9e30d856d1 100644 --- a/Source/Core/Core/IOS/Network/Socket.cpp +++ b/Source/Core/Core/IOS/Network/Socket.cpp @@ -1085,10 +1085,10 @@ void WiiSockMan::UpdatePollCommands() std::vector original_order(pfds.size()); std::iota(original_order.begin(), original_order.end(), 0); // Select indices with valid fds - auto mid = std::partition(original_order.begin(), original_order.end(), [&](auto i) { + const auto partition_result = std::ranges::partition(original_order, [&](auto i) { return GetHostSocket(memory.Read_U32(pcmd.buffer_out + 0xc * i)) >= 0; }); - const auto n_valid = std::distance(original_order.begin(), mid); + const auto n_valid = std::distance(original_order.begin(), partition_result.begin()); // Move all the valid pollfds to the front of the vector for (auto i = 0; i < n_valid; ++i)