refactor: prefer std::ranges over begin() and end() (#43464)

This commit is contained in:
Charles Kerr 2024-08-26 09:58:32 -05:00 committed by GitHub
parent 56829f75c1
commit 2390706030
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 30 additions and 40 deletions

View file

@ -165,7 +165,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
base::AtExitManager atexit_manager; base::AtExitManager atexit_manager;
base::i18n::InitializeICU(); base::i18n::InitializeICU();
auto ret = electron::NodeMain(argv.size(), argv.data()); auto ret = electron::NodeMain(argv.size(), argv.data());
std::for_each(argv.begin(), argv.end(), free); std::ranges::for_each(argv, free);
return ret; return ret;
} }

View file

@ -34,11 +34,9 @@ bool RegisteringMediaKeyForUntrustedClient(const ui::Accelerator& accelerator) {
bool MapHasMediaKeys( bool MapHasMediaKeys(
const std::map<ui::Accelerator, base::RepeatingClosure>& accelerator_map) { const std::map<ui::Accelerator, base::RepeatingClosure>& accelerator_map) {
auto media_key = std::find_if( return std::ranges::any_of(accelerator_map, [](const auto& ac) {
accelerator_map.begin(), accelerator_map.end(), return Command::IsMediaKey(ac.first);
[](const auto& ac) { return Command::IsMediaKey(ac.first); }); });
return media_key != accelerator_map.end();
} }
#endif #endif

View file

@ -455,8 +455,7 @@ struct Converter<network::mojom::SSLConfigPtr> {
!options.Get("disabledCipherSuites", &(*out)->disabled_cipher_suites)) { !options.Get("disabledCipherSuites", &(*out)->disabled_cipher_suites)) {
return false; return false;
} }
std::sort((*out)->disabled_cipher_suites.begin(), std::ranges::sort((*out)->disabled_cipher_suites);
(*out)->disabled_cipher_suites.end());
// TODO(nornagon): also support other SSLConfig properties? // TODO(nornagon): also support other SSLConfig properties?
return true; return true;

View file

@ -49,8 +49,8 @@ bool FilterMatch(const blink::mojom::HidDeviceFilterPtr& filter,
if (filter->usage) { if (filter->usage) {
if (filter->usage->is_page()) { if (filter->usage->is_page()) {
const uint16_t usage_page = filter->usage->get_page(); const uint16_t usage_page = filter->usage->get_page();
auto find_it = auto find_it = std::ranges::find_if(
std::find_if(device.collections.begin(), device.collections.end(), device.collections,
[=](const device::mojom::HidCollectionInfoPtr& c) { [=](const device::mojom::HidCollectionInfoPtr& c) {
return usage_page == c->usage->usage_page; return usage_page == c->usage->usage_page;
}); });

View file

@ -38,8 +38,8 @@ void NotificationPresenter::RemoveNotification(Notification* notification) {
void NotificationPresenter::CloseNotificationWithId( void NotificationPresenter::CloseNotificationWithId(
const std::string& notification_id) { const std::string& notification_id) {
auto it = std::find_if(notifications_.begin(), notifications_.end(), auto it = std::ranges::find_if(
[&notification_id](const Notification* n) { notifications_, [&notification_id](const Notification* n) {
return n->notification_id() == notification_id; return n->notification_id() == notification_id;
}); });
if (it != notifications_.end()) { if (it != notifications_.end()) {

View file

@ -177,8 +177,7 @@ void SerialChooserController::OnDeviceChosen(const std::string& port_id) {
if (port_id.empty()) { if (port_id.empty()) {
RunCallback(/*port=*/nullptr); RunCallback(/*port=*/nullptr);
} else { } else {
const auto it = const auto it = std::ranges::find_if(ports_, [&port_id](const auto& ptr) {
std::find_if(ports_.begin(), ports_.end(), [&port_id](const auto& ptr) {
return ptr->token.ToString() == port_id; return ptr->token.ToString() == port_id;
}); });
if (it != ports_.end()) { if (it != ports_.end()) {
@ -194,8 +193,7 @@ void SerialChooserController::OnDeviceChosen(const std::string& port_id) {
void SerialChooserController::OnGetDevices( void SerialChooserController::OnGetDevices(
std::vector<device::mojom::SerialPortInfoPtr> ports) { std::vector<device::mojom::SerialPortInfoPtr> ports) {
// Sort ports by file paths. // Sort ports by file paths.
std::sort(ports.begin(), ports.end(), std::ranges::sort(ports, [](const auto& port1, const auto& port2) {
[](const auto& port1, const auto& port2) {
return port1->path.BaseName() < port2->path.BaseName(); return port1->path.BaseName() < port2->path.BaseName();
}); });

View file

@ -409,15 +409,15 @@ void ClientFrameViewLinux::LayoutButtonsOnSide(
frame_buttons = trailing_frame_buttons_; frame_buttons = trailing_frame_buttons_;
// We always lay buttons out going from the edge towards the center, but // We always lay buttons out going from the edge towards the center, but
// they are given to us as left-to-right, so reverse them. // they are given to us as left-to-right, so reverse them.
std::reverse(frame_buttons.begin(), frame_buttons.end()); std::ranges::reverse(frame_buttons);
break; break;
default: default:
NOTREACHED(); NOTREACHED();
} }
for (views::FrameButton frame_button : frame_buttons) { for (views::FrameButton frame_button : frame_buttons) {
auto* button = std::find_if( auto* button =
nav_buttons_.begin(), nav_buttons_.end(), [&](const NavButton& test) { std::ranges::find_if(nav_buttons_, [&](const NavButton& test) {
return test.type != skip_type && test.frame_button == frame_button; return test.type != skip_type && test.frame_button == frame_button;
}); });
CHECK(button != nav_buttons_.end()) CHECK(button != nav_buttons_.end())

View file

@ -211,8 +211,7 @@ NotifyIcon* NotifyIconHost::CreateNotifyIcon(std::optional<UUID> guid) {
} }
void NotifyIconHost::Remove(NotifyIcon* icon) { void NotifyIconHost::Remove(NotifyIcon* icon) {
NotifyIcons::iterator i( const auto i = std::ranges::find(notify_icons_, icon);
std::find(notify_icons_.begin(), notify_icons_.end(), icon));
if (i == notify_icons_.end()) { if (i == notify_icons_.end()) {
NOTREACHED(); NOTREACHED();
@ -241,11 +240,7 @@ LRESULT CALLBACK NotifyIconHost::WndProc(HWND hwnd,
LPARAM lparam) { LPARAM lparam) {
if (message == taskbar_created_message_) { if (message == taskbar_created_message_) {
// We need to reset all of our icons because the taskbar went away. // We need to reset all of our icons because the taskbar went away.
for (NotifyIcons::const_iterator i(notify_icons_.begin()); std::ranges::for_each(notify_icons_, [](auto* icon) { icon->ResetIcon(); });
i != notify_icons_.end(); ++i) {
auto* win_icon = static_cast<NotifyIcon*>(*i);
win_icon->ResetIcon();
}
return TRUE; return TRUE;
} else if (message == kNotifyIconMessage) { } else if (message == kNotifyIconMessage) {
NotifyIcon* win_icon = nullptr; NotifyIcon* win_icon = nullptr;

View file

@ -85,7 +85,7 @@ void WindowList::CloseAllWindows() {
std::vector<base::WeakPtr<NativeWindow>> weak_windows = std::vector<base::WeakPtr<NativeWindow>> weak_windows =
ConvertToWeakPtrVector(GetInstance()->windows_); ConvertToWeakPtrVector(GetInstance()->windows_);
#if BUILDFLAG(IS_MAC) #if BUILDFLAG(IS_MAC)
std::reverse(weak_windows.begin(), weak_windows.end()); std::ranges::reverse(weak_windows);
#endif #endif
for (const auto& window : weak_windows) { for (const auto& window : weak_windows) {
if (window && !window->IsClosed()) if (window && !window->IsClosed())

View file

@ -67,7 +67,7 @@ void SetCrashKey(const std::string& key, const std::string& value) {
auto& crash_key_names = GetExtraCrashKeyNames(); auto& crash_key_names = GetExtraCrashKeyNames();
auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key); auto iter = std::ranges::find(crash_key_names, key);
if (iter == crash_key_names.end()) { if (iter == crash_key_names.end()) {
crash_key_names.emplace_back(key); crash_key_names.emplace_back(key);
GetExtraCrashKeys().emplace_back(crash_key_names.back().c_str()); GetExtraCrashKeys().emplace_back(crash_key_names.back().c_str());
@ -79,7 +79,7 @@ void SetCrashKey(const std::string& key, const std::string& value) {
void ClearCrashKey(const std::string& key) { void ClearCrashKey(const std::string& key) {
const auto& crash_key_names = GetExtraCrashKeyNames(); const auto& crash_key_names = GetExtraCrashKeyNames();
auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key); auto iter = std::ranges::find(crash_key_names, key);
if (iter != crash_key_names.end()) { if (iter != crash_key_names.end()) {
GetExtraCrashKeys()[iter - crash_key_names.begin()].Clear(); GetExtraCrashKeys()[iter - crash_key_names.begin()].Clear();
} }

View file

@ -800,7 +800,7 @@ std::shared_ptr<node::Environment> NodeBindings::CreateEnvironment(
#if BUILDFLAG(IS_WIN) #if BUILDFLAG(IS_WIN)
auto& electron_args = ElectronCommandLine::argv(); auto& electron_args = ElectronCommandLine::argv();
std::vector<std::string> args(electron_args.size()); std::vector<std::string> args(electron_args.size());
std::transform(electron_args.cbegin(), electron_args.cend(), args.begin(), std::ranges::transform(electron_args, args.begin(),
[](auto& a) { return base::WideToUTF8(a); }); [](auto& a) { return base::WideToUTF8(a); });
#else #else
auto args = ElectronCommandLine::argv(); auto args = ElectronCommandLine::argv();