Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/cleanup_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,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 {
Expand Down
3 changes: 2 additions & 1 deletion src/inspector_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <algorithm>
#include <cstring>
#include <map>
#include <ranges>

#define ACCEPT_KEY_LENGTH nbytes::Base64EncodedSize(20)

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/inspector_socket_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
15 changes: 7 additions & 8 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -758,17 +758,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;
}

Expand All @@ -780,7 +779,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
Expand Down
3 changes: 1 addition & 2 deletions src/node_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "--") {
Expand Down
4 changes: 1 addition & 3 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 5 additions & 8 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,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);
}
Expand Down
5 changes: 2 additions & 3 deletions src/node_process_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -811,9 +812,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);
Expand Down
7 changes: 3 additions & 4 deletions src/node_sea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,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,
Expand Down
3 changes: 1 addition & 2 deletions src/node_task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/node_watchdog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions test/cctest/test_inspector_socket_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}

Expand Down
Loading