Skip to content

Commit 12286c9

Browse files
lemiretargos
authored andcommitted
src: use ranges library (C++20) more systematically
PR-URL: #58028 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Vladimir Morozov <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Shelley Vohr <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 0def4e2 commit 12286c9

16 files changed

+43
-53
lines changed

src/cleanup_queue.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ std::vector<CleanupQueue::CleanupHookCallback> CleanupQueue::GetOrdered()
1313
// We can't erase the copied elements from `cleanup_hooks_` yet, because we
1414
// need to be able to check whether they were un-scheduled by another hook.
1515

16-
std::sort(callbacks.begin(),
17-
callbacks.end(),
18-
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
19-
// Sort in descending order so that the most recently inserted
20-
// callbacks are run first.
21-
return a.insertion_order_counter_ > b.insertion_order_counter_;
22-
});
16+
std::ranges::sort(
17+
callbacks,
18+
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
19+
// Sort in descending order so that the most recently inserted
20+
// callbacks are run first.
21+
return a.insertion_order_counter_ > b.insertion_order_counter_;
22+
});
2323

2424
return callbacks;
2525
}

src/crypto/crypto_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void CryptoErrorStore::Capture() {
218218
ERR_error_string_n(err, buf, sizeof(buf));
219219
errors_.emplace_back(buf);
220220
}
221-
std::reverse(std::begin(errors_), std::end(errors_));
221+
std::ranges::reverse(errors_);
222222
}
223223

224224
bool CryptoErrorStore::Empty() const {

src/inspector_socket.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <algorithm>
1111
#include <cstring>
1212
#include <map>
13+
#include <ranges>
1314

1415
#define ACCEPT_KEY_LENGTH nbytes::Base64EncodedSize(20)
1516

@@ -205,7 +206,7 @@ static bool IsIPAddress(const std::string& host) {
205206
// (other than ::/128) that represent non-routable IPv4 addresses. However,
206207
// this translation assumes that the host is interpreted as an IPv6 address
207208
// in the first place, at which point DNS rebinding should not be an issue.
208-
if (std::all_of(ipv6, ipv6 + sizeof(ipv6), [](auto b) { return b == 0; })) {
209+
if (std::ranges::all_of(ipv6, [](auto b) { return b == 0; })) {
209210
return false;
210211
}
211212

src/inspector_socket_server.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ void InspectorSocketServer::TerminateConnections() {
451451

452452
bool InspectorSocketServer::TargetExists(const std::string& id) {
453453
const std::vector<std::string>& target_ids = delegate_->GetTargetIds();
454-
const auto& found = std::find(target_ids.begin(), target_ids.end(), id);
454+
const auto& found = std::ranges::find(target_ids, id);
455455
return found != target_ids.end();
456456
}
457457

src/node.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -760,17 +760,16 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
760760

761761
// TODO(aduh95): remove this when the harmony-import-attributes flag
762762
// is removed in V8.
763-
if (std::find(v8_args.begin(),
764-
v8_args.end(),
765-
"--no-harmony-import-attributes") == v8_args.end()) {
763+
if (std::ranges::find(v8_args, "--no-harmony-import-attributes") ==
764+
v8_args.end()) {
766765
v8_args.emplace_back("--harmony-import-attributes");
767766
}
768767

769768
auto env_opts = per_process::cli_options->per_isolate->per_env;
770-
if (std::find(v8_args.begin(), v8_args.end(),
771-
"--abort-on-uncaught-exception") != v8_args.end() ||
772-
std::find(v8_args.begin(), v8_args.end(),
773-
"--abort_on_uncaught_exception") != v8_args.end()) {
769+
if (std::ranges::find(v8_args, "--abort-on-uncaught-exception") !=
770+
v8_args.end() ||
771+
std::ranges::find(v8_args, "--abort_on_uncaught_exception") !=
772+
v8_args.end()) {
774773
env_opts->abort_on_uncaught_exception = true;
775774
}
776775

@@ -782,7 +781,7 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
782781
// Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the
783782
// performance penalty of frequent EINTR wakeups when the profiler is running.
784783
// Only do this for v8.log profiling, as it breaks v8::CpuProfiler users.
785-
if (std::find(v8_args.begin(), v8_args.end(), "--prof") != v8_args.end()) {
784+
if (std::ranges::find(v8_args, "--prof") != v8_args.end()) {
786785
uv_loop_configure(uv_default_loop(), UV_LOOP_BLOCK_SIGNAL, SIGPROF);
787786
}
788787
#endif

src/node_credentials.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ static void GetGroups(const FunctionCallbackInfo<Value>& args) {
385385

386386
groups.resize(ngroups);
387387
gid_t egid = getegid();
388-
if (std::find(groups.begin(), groups.end(), egid) == groups.end())
389-
groups.push_back(egid);
388+
if (std::ranges::find(groups, egid) == groups.end()) groups.push_back(egid);
390389
Local<Value> result;
391390
if (ToV8Value(env->context(), groups).ToLocal(&result)) {
392391
args.GetReturnValue().Set(result);

src/node_dotenv.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ std::vector<Dotenv::env_file_data> Dotenv::GetDataFromArgs(
2828

2929
std::vector<Dotenv::env_file_data> env_files;
3030
// This will be an iterator, pointing to args.end() if no matches are found
31-
auto matched_arg = std::find_if(args.begin(), args.end(), find_match);
31+
auto matched_arg = std::ranges::find_if(args, find_match);
3232

3333
while (matched_arg != args.end()) {
3434
if (*matched_arg == "--") {

src/node_http2.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,7 @@ class Http2Session : public AsyncWrap,
692692

693693
bool has_pending_rststream(int32_t stream_id) {
694694
return pending_rst_streams_.end() !=
695-
std::find(pending_rst_streams_.begin(),
696-
pending_rst_streams_.end(),
697-
stream_id);
695+
std::ranges::find(pending_rst_streams_, stream_id);
698696
}
699697

700698
// Handle reads/writes from the underlying network transport.

src/node_messaging.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,8 @@ class SerializerDelegate : public ValueSerializer::Delegate {
414414
if (!host_objects_[i]->NestedTransferables().To(&nested_transferables))
415415
return Nothing<bool>();
416416
for (auto& nested_transferable : nested_transferables) {
417-
if (std::find(host_objects_.begin(),
418-
host_objects_.end(),
419-
nested_transferable) == host_objects_.end()) {
417+
if (std::ranges::find(host_objects_, nested_transferable) ==
418+
host_objects_.end()) {
420419
AddHostObject(nested_transferable);
421420
}
422421
}
@@ -517,8 +516,7 @@ Maybe<bool> Message::Serialize(Environment* env,
517516
ThrowDataCloneException(context, env->transfer_unsupported_type_str());
518517
return Nothing<bool>();
519518
}
520-
if (std::find(array_buffers.begin(), array_buffers.end(), ab) !=
521-
array_buffers.end()) {
519+
if (std::ranges::find(array_buffers, ab) != array_buffers.end()) {
522520
ThrowDataCloneException(
523521
context,
524522
FIXED_ONE_BYTE_STRING(
@@ -564,9 +562,8 @@ Maybe<bool> Message::Serialize(Environment* env,
564562
"MessagePort in transfer list is already detached"));
565563
return Nothing<bool>();
566564
}
567-
if (std::find(delegate.host_objects_.begin(),
568-
delegate.host_objects_.end(),
569-
host_object) != delegate.host_objects_.end()) {
565+
if (std::ranges::find(delegate.host_objects_, host_object) !=
566+
delegate.host_objects_.end()) {
570567
ThrowDataCloneException(
571568
context,
572569
String::Concat(

src/node_platform.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,11 @@ void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
560560
}
561561

562562
void PerIsolatePlatformData::DeleteFromScheduledTasks(DelayedTask* task) {
563-
auto it = std::find_if(scheduled_delayed_tasks_.begin(),
564-
scheduled_delayed_tasks_.end(),
565-
[task](const DelayedTaskPointer& delayed) -> bool {
566-
return delayed.get() == task;
567-
});
563+
auto it =
564+
std::ranges::find_if(scheduled_delayed_tasks_,
565+
[task](const DelayedTaskPointer& delayed) -> bool {
566+
return delayed.get() == task;
567+
});
568568
CHECK_NE(it, scheduled_delayed_tasks_.end());
569569
scheduled_delayed_tasks_.erase(it);
570570
}

0 commit comments

Comments
 (0)