src: use ranges library (C++20) more systematically

PR-URL: https://github.com/nodejs/node/pull/58028
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Daniel Lemire authored and Michaël Zasso committed 2025-07-03 10:50:12 +02:00
1 parent 0def4e23b1
commit 12286c9f64
16 files changed
+43 -53

No files matched your search

+7 -7
View File
@@ -13,13 +13,13 @@ std::vector<CleanupQueue::CleanupHookCallback> CleanupQueue::GetOrdered()
// We can't erase the copied elements from `cleanup_hooks_` yet, because we
// need to be able to check whether they were un-scheduled by another hook.
std::sort(callbacks.begin(),
callbacks.end(),
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
// Sort in descending order so that the most recently inserted
// callbacks are run first.
return a.insertion_order_counter_ > b.insertion_order_counter_;
});
std::ranges::sort(
callbacks,
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
// Sort in descending order so that the most recently inserted
// callbacks are run first.
return a.insertion_order_counter_ > b.insertion_order_counter_;
});
return callbacks;
}
+1 -1
View File
@@ -218,7 +218,7 @@ void CryptoErrorStore::Capture() {
ERR_error_string_n(err, buf, sizeof(buf));
errors_.emplace_back(buf);
}
std::reverse(std::begin(errors_), std::end(errors_));
std::ranges::reverse(errors_);
}
bool CryptoErrorStore::Empty() const {
+2 -1
View File
@@ -10,6 +10,7 @@
#include <algorithm>
#include <cstring>
#include <map>
#include <ranges>
#define ACCEPT_KEY_LENGTH nbytes::Base64EncodedSize(20)
@@ -205,7 +206,7 @@ static bool IsIPAddress(const std::string& host) {
// (other than ::/128) that represent non-routable IPv4 addresses. However,
// this translation assumes that the host is interpreted as an IPv6 address
// in the first place, at which point DNS rebinding should not be an issue.
if (std::all_of(ipv6, ipv6 + sizeof(ipv6), [](auto b) { return b == 0; })) {
if (std::ranges::all_of(ipv6, [](auto b) { return b == 0; })) {
return false;
}
+1 -1
View File
@@ -451,7 +451,7 @@ void InspectorSocketServer::TerminateConnections() {
bool InspectorSocketServer::TargetExists(const std::string& id) {
const std::vector<std::string>& target_ids = delegate_->GetTargetIds();
const auto& found = std::find(target_ids.begin(), target_ids.end(), id);
const auto& found = std::ranges::find(target_ids, id);
return found != target_ids.end();
}
+7 -8
View File
@@ -760,17 +760,16 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
// TODO(aduh95): remove this when the harmony-import-attributes flag
// is removed in V8.
if (std::find(v8_args.begin(),
v8_args.end(),
"--no-harmony-import-attributes") == v8_args.end()) {
if (std::ranges::find(v8_args, "--no-harmony-import-attributes") ==
v8_args.end()) {
v8_args.emplace_back("--harmony-import-attributes");
}
auto env_opts = per_process::cli_options->per_isolate->per_env;
if (std::find(v8_args.begin(), v8_args.end(),
"--abort-on-uncaught-exception") != v8_args.end() ||
std::find(v8_args.begin(), v8_args.end(),
"--abort_on_uncaught_exception") != v8_args.end()) {
if (std::ranges::find(v8_args, "--abort-on-uncaught-exception") !=
v8_args.end() ||
std::ranges::find(v8_args, "--abort_on_uncaught_exception") !=
v8_args.end()) {
env_opts->abort_on_uncaught_exception = true;
}
@@ -782,7 +781,7 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
// Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the
// performance penalty of frequent EINTR wakeups when the profiler is running.
// Only do this for v8.log profiling, as it breaks v8::CpuProfiler users.
if (std::find(v8_args.begin(), v8_args.end(), "--prof") != v8_args.end()) {
if (std::ranges::find(v8_args, "--prof") != v8_args.end()) {
uv_loop_configure(uv_default_loop(), UV_LOOP_BLOCK_SIGNAL, SIGPROF);
}
#endif
+1 -2
View File
@@ -385,8 +385,7 @@ static void GetGroups(const FunctionCallbackInfo<Value>& args) {
groups.resize(ngroups);
gid_t egid = getegid();
if (std::find(groups.begin(), groups.end(), egid) == groups.end())
groups.push_back(egid);
if (std::ranges::find(groups, egid) == groups.end()) groups.push_back(egid);
Local<Value> result;
if (ToV8Value(env->context(), groups).ToLocal(&result)) {
args.GetReturnValue().Set(result);
+1 -1
View File
@@ -28,7 +28,7 @@ std::vector<Dotenv::env_file_data> Dotenv::GetDataFromArgs(
std::vector<Dotenv::env_file_data> env_files;
// This will be an iterator, pointing to args.end() if no matches are found
auto matched_arg = std::find_if(args.begin(), args.end(), find_match);
auto matched_arg = std::ranges::find_if(args, find_match);
while (matched_arg != args.end()) {
if (*matched_arg == "--") {
+1 -3
View File
@@ -692,9 +692,7 @@ class Http2Session : public AsyncWrap,
bool has_pending_rststream(int32_t stream_id) {
return pending_rst_streams_.end() !=
std::find(pending_rst_streams_.begin(),
pending_rst_streams_.end(),
stream_id);
std::ranges::find(pending_rst_streams_, stream_id);
}
// Handle reads/writes from the underlying network transport.
+5 -8
View File
@@ -414,9 +414,8 @@ class SerializerDelegate : public ValueSerializer::Delegate {
if (!host_objects_[i]->NestedTransferables().To(&nested_transferables))
return Nothing<bool>();
for (auto& nested_transferable : nested_transferables) {
if (std::find(host_objects_.begin(),
host_objects_.end(),
nested_transferable) == host_objects_.end()) {
if (std::ranges::find(host_objects_, nested_transferable) ==
host_objects_.end()) {
AddHostObject(nested_transferable);
}
}
@@ -517,8 +516,7 @@ Maybe<bool> Message::Serialize(Environment* env,
ThrowDataCloneException(context, env->transfer_unsupported_type_str());
return Nothing<bool>();
}
if (std::find(array_buffers.begin(), array_buffers.end(), ab) !=
array_buffers.end()) {
if (std::ranges::find(array_buffers, ab) != array_buffers.end()) {
ThrowDataCloneException(
context,
FIXED_ONE_BYTE_STRING(
@@ -564,9 +562,8 @@ Maybe<bool> Message::Serialize(Environment* env,
"MessagePort in transfer list is already detached"));
return Nothing<bool>();
}
if (std::find(delegate.host_objects_.begin(),
delegate.host_objects_.end(),
host_object) != delegate.host_objects_.end()) {
if (std::ranges::find(delegate.host_objects_, host_object) !=
delegate.host_objects_.end()) {
ThrowDataCloneException(
context,
String::Concat(
+5 -5
View File
@@ -560,11 +560,11 @@ void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
}
void PerIsolatePlatformData::DeleteFromScheduledTasks(DelayedTask* task) {
auto it = std::find_if(scheduled_delayed_tasks_.begin(),
scheduled_delayed_tasks_.end(),
[task](const DelayedTaskPointer& delayed) -> bool {
return delayed.get() == task;
});
auto it =
std::ranges::find_if(scheduled_delayed_tasks_,
[task](const DelayedTaskPointer& delayed) -> bool {
return delayed.get() == task;
});
CHECK_NE(it, scheduled_delayed_tasks_.end());
scheduled_delayed_tasks_.erase(it);
}
+2 -3
View File
@@ -97,9 +97,8 @@ static void SetVersions(Isolate* isolate, Local<Object> versions) {
NODE_VERSIONS_KEYS(V)
#undef V
std::sort(&versions_array[0],
&versions_array[arraysize(versions_array)],
[](auto& a, auto& b) { return a.first < b.first; });
std::ranges::sort(versions_array,
[](auto& a, auto& b) { return a.first < b.first; });
for (const auto& version : versions_array) {
versions
+4 -4
View File
@@ -22,6 +22,7 @@
#include <ctime>
#include <cwctype>
#include <fstream>
#include <ranges>
constexpr int NODE_REPORT_VERSION = 5;
constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
@@ -537,7 +538,7 @@ static void PrintJavaScriptErrorStack(JSONWriter* writer,
line = ss.find('\n');
while (line != -1) {
l = ss.substr(0, line);
l.erase(l.begin(), std::find_if(l.begin(), l.end(), [](int ch) {
l.erase(l.begin(), std::ranges::find_if(l, [](int ch) {
return !std::iswspace(ch);
}));
writer->json_element(l);
@@ -810,9 +811,8 @@ static void PrintComponentVersions(JSONWriter* writer) {
NODE_VERSIONS_KEYS(V)
#undef V
std::sort(&versions_array[0],
&versions_array[arraysize(versions_array)],
[](auto& a, auto& b) { return a.first < b.first; });
std::ranges::sort(versions_array,
[](auto& a, auto& b) { return a.first < b.first; });
for (const auto& version : versions_array) {
writer->json_keyvalue(version.first, version.second);
+3 -4
View File
@@ -400,10 +400,9 @@ ExitCode GenerateSnapshotForSEA(const SeaConfig& config,
return exit_code;
}
auto& persistents = snapshot.env_info.principal_realm.persistent_values;
auto it = std::find_if(
persistents.begin(), persistents.end(), [](const PropInfo& prop) {
return prop.name == "snapshot_deserialize_main";
});
auto it = std::ranges::find_if(persistents, [](const PropInfo& prop) {
return prop.name == "snapshot_deserialize_main";
});
if (it == persistents.end()) {
FPrintF(
stderr,
+1 -2
View File
@@ -351,8 +351,7 @@ void RunTask(const std::shared_ptr<InitializationResultImpl>& result,
PositionalArgs GetPositionalArgs(const std::vector<std::string>& args) {
// If the "--" flag is not found, return an empty optional
// Otherwise, return the positional arguments as a single string
if (auto dash_dash = std::find(args.begin(), args.end(), "--");
dash_dash != args.end()) {
if (auto dash_dash = std::ranges::find(args, "--"); dash_dash != args.end()) {
PositionalArgs positional_args{};
positional_args.reserve(args.size() - (dash_dash - args.begin()));
for (auto it = dash_dash + 1; it != args.end(); ++it) {
+1 -1
View File
@@ -389,7 +389,7 @@ void SigintWatchdogHelper::Register(SigintWatchdogBase* wd) {
void SigintWatchdogHelper::Unregister(SigintWatchdogBase* wd) {
Mutex::ScopedLock lock(list_mutex_);
auto it = std::find(watchdogs_.begin(), watchdogs_.end(), wd);
auto it = std::ranges::find(watchdogs_, wd);
CHECK_NE(it, watchdogs_.end());
watchdogs_.erase(it);
+1 -2
View File
@@ -318,8 +318,7 @@ class TestSocketServerDelegate : public SocketServerDelegate {
void StartSession(int session_id, const std::string& target_id) override {
session_id_ = session_id;
CHECK_NE(targets_.end(),
std::find(targets_.begin(), targets_.end(), target_id));
CHECK_NE(targets_.end(), std::ranges::find(targets_, target_id));
harness_->Connected(session_id_);
}