Skip to content

Commit

Permalink
Merge pull request #13116 from mitaclaw/ranges-modernization-8-trivia…
Browse files Browse the repository at this point in the history
…l-of

Ranges Algorithms Modernization - Of
  • Loading branch information
JMC47 authored Dec 26, 2024
2 parents f9ce2b9 + 2b0cd16 commit 532a862
Show file tree
Hide file tree
Showing 59 changed files with 148 additions and 166 deletions.
7 changes: 3 additions & 4 deletions Source/Android/jni/Cheats/GraphicsModGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsModGroup_getMods(JN
for (GraphicsModConfig& mod : mod_group->GetMods())
{
// If no group matches the mod's features, or if the mod has no features, skip it
if (std::none_of(mod.m_features.begin(), mod.m_features.end(),
[&groups](const GraphicsModFeatureConfig& feature) {
return groups.contains(feature.m_group);
}))
if (std::ranges::none_of(mod.m_features, [&groups](const GraphicsModFeatureConfig& feature) {
return groups.contains(feature.m_group);
}))
{
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Common/Crypto/ec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct Elt
{
bool IsZero() const
{
return std::all_of(data.begin(), data.end(), [](u8 b) { return b == 0; });
return std::ranges::all_of(data, [](u8 b) { return b == 0; });
}

void MulX()
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Common/Debug/MemoryPatches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void MemoryPatches::DisablePatch(const Core::CPUThreadGuard& guard, std::size_t

bool MemoryPatches::HasEnabledPatch(u32 address) const
{
return std::any_of(m_patches.begin(), m_patches.end(), [address](const MemoryPatch& patch) {
return std::ranges::any_of(m_patches, [address](const MemoryPatch& patch) {
return patch.address == address && patch.is_enabled == MemoryPatch::State::Enabled;
});
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Common/Debug/Watches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void Watches::DisableWatch(std::size_t index)

bool Watches::HasEnabledWatch(u32 address) const
{
return std::any_of(m_watches.begin(), m_watches.end(), [address](const auto& watch) {
return std::ranges::any_of(m_watches, [address](const auto& watch) {
return watch.address == address && watch.is_enabled == Watch::State::Enabled;
});
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Common/FatFsUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ static bool Unpack(const std::function<bool()>& cancelled, const std::string pat
const bool is_path_traversal_attack =
(childname.find("\\") != std::string_view::npos) ||
(childname.find('/') != std::string_view::npos) ||
std::all_of(childname.begin(), childname.end(), [](char c) { return c == '.'; });
std::ranges::all_of(childname, [](char c) { return c == '.'; });
if (is_path_traversal_attack)
{
ERROR_LOG_FMT(
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Common/FileSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::vector<std::string> DoFileSearch(const std::vector<std::string>& directorie
// N.B. This avoids doing any copies
auto ext_matches = [&native_exts](const fs::path& path) {
const std::basic_string_view<fs::path::value_type> native_path = path.native();
return std::any_of(native_exts.cbegin(), native_exts.cend(), [&native_path](const auto& ext) {
return std::ranges::any_of(native_exts, [&native_path](const auto& ext) {
const auto compare_len = ext.native().length();
if (native_path.length() < compare_len)
return false;
Expand Down
7 changes: 3 additions & 4 deletions Source/Core/Common/NandPaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static bool IsIllegalCharacter(char c)
std::string EscapeFileName(const std::string& filename)
{
// Prevent paths from containing special names like ., .., ..., ...., and so on
if (std::all_of(filename.begin(), filename.end(), [](char c) { return c == '.'; }))
if (std::ranges::all_of(filename, [](char c) { return c == '.'; }))
return ReplaceAll(filename, ".", "__2e__");

// Escape all double underscores since we will use double underscores for our escape sequences
Expand Down Expand Up @@ -170,8 +170,7 @@ std::string UnescapeFileName(const std::string& filename)

bool IsFileNameSafe(const std::string_view filename)
{
return !filename.empty() &&
!std::all_of(filename.begin(), filename.end(), [](char c) { return c == '.'; }) &&
std::none_of(filename.begin(), filename.end(), IsIllegalCharacter);
return !filename.empty() && !std::ranges::all_of(filename, [](char c) { return c == '.'; }) &&
std::ranges::none_of(filename, IsIllegalCharacter);
}
} // namespace Common
15 changes: 15 additions & 0 deletions Source/Core/Common/StringUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ inline bool IsAlpha(char c)
return std::isalpha(c, std::locale::classic());
}

inline bool IsAlnum(char c)
{
return std::isalnum(c, std::locale::classic());
}

inline bool IsUpper(char c)
{
return std::isupper(c, std::locale::classic());
}

inline bool IsXDigit(char c)
{
return std::isxdigit(c /* no locale needed */) != 0;
}

inline char ToLower(char ch)
{
return std::tolower(ch, std::locale::classic());
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/Core/Config/DefaultLocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ static DiscIO::Language ComputeDefaultLanguage()

static std::optional<std::string> TryParseCountryCode(const std::string& locale)
{
const auto is_upper = [](char c) { return std::isupper(c, std::locale::classic()); };

for (const std::string& part : SplitString(locale, '-'))
{
if (part.size() == 2 && is_upper(part[0]) && is_upper(part[1]))
if (part.size() == 2 && Common::IsUpper(part[0]) && Common::IsUpper(part[1]))
return part;
}

Expand Down
5 changes: 2 additions & 3 deletions Source/Core/Core/ConfigLoaders/BaseConfigLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ class BaseConfigLayerLoader final : public Config::ConfigLayerLoader
for (const auto& value : section_map)
{
const Config::Location location{system.first, section_name, value.first};
const bool load_disallowed =
std::any_of(begin(s_setting_disallowed), end(s_setting_disallowed),
[&location](const Config::Location* l) { return *l == location; });
const bool load_disallowed = std::ranges::any_of(
s_setting_disallowed, [&location](const auto* l) { return *l == location; });
if (load_disallowed)
continue;

Expand Down
7 changes: 3 additions & 4 deletions Source/Core/Core/ConfigLoaders/IsSettingSaveable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ bool IsSettingSaveable(const Config::Location& config_location)
&Config::WIIMOTE_BB_SOURCE.GetLocation(),
};

return std::any_of(begin(s_setting_saveable), end(s_setting_saveable),
[&config_location](const Config::Location* location) {
return *location == config_location;
});
return std::ranges::any_of(s_setting_saveable, [&config_location](const auto* location) {
return *location == config_location;
});
}
} // namespace ConfigLoaders
2 changes: 1 addition & 1 deletion Source/Core/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void DisplayMessage(std::string message, int time_in_ms)
return;

// Actually displaying non-ASCII could cause things to go pear-shaped
if (!std::all_of(message.begin(), message.end(), Common::IsPrintableCharacter))
if (!std::ranges::all_of(message, Common::IsPrintableCharacter))
return;

OSD::AddMessage(std::move(message), time_in_ms);
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Debugger/CodeTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ HitType CodeTrace::TraceLogic(const TraceOutput& current_instr, bool first_hit)

// Checks if the intstruction is a type that needs special handling.
const auto CompareInstruction = [](std::string_view instruction, const auto& type_compare) {
return std::any_of(type_compare.begin(), type_compare.end(),
[&instruction](std::string_view s) { return instruction.starts_with(s); });
return std::ranges::any_of(
type_compare, [&instruction](std::string_view s) { return instruction.starts_with(s); });
};

// Exclusions from updating tracking logic. mt operations are too complex and specialized.
Expand Down
10 changes: 5 additions & 5 deletions Source/Core/Core/HW/GCMemcard/GCMemcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ std::pair<GCMemcardErrorCode, std::optional<GCMemcard>> GCMemcard::Open(std::str
MBIT_SIZE_MEMORY_CARD_2043,
}};

if (!std::any_of(valid_megabits.begin(), valid_megabits.end(),
[filesize_megabits](u64 mbits) { return mbits == filesize_megabits; }))
if (!std::ranges::any_of(valid_megabits,
[filesize_megabits](u64 mbits) { return mbits == filesize_megabits; }))
{
error_code.Set(GCMemcardValidityIssues::INVALID_CARD_SIZE);
return std::make_pair(error_code, std::nullopt);
Expand Down Expand Up @@ -1296,8 +1296,8 @@ GCMemcardErrorCode Header::CheckForErrors(u16 card_size_mbits) const
error_code.Set(GCMemcardValidityIssues::MISMATCHED_CARD_SIZE);

// unused areas, should always be filled with 0xFF
if (std::any_of(m_unused_1.begin(), m_unused_1.end(), [](u8 val) { return val != 0xFF; }) ||
std::any_of(m_unused_2.begin(), m_unused_2.end(), [](u8 val) { return val != 0xFF; }))
if (std::ranges::any_of(m_unused_1, [](u8 val) { return val != 0xFF; }) ||
std::ranges::any_of(m_unused_2, [](u8 val) { return val != 0xFF; }))
{
error_code.Set(GCMemcardValidityIssues::DATA_IN_UNUSED_AREA);
}
Expand Down Expand Up @@ -1361,7 +1361,7 @@ GCMemcardErrorCode Directory::CheckForErrors() const
error_code.Set(GCMemcardValidityIssues::INVALID_CHECKSUM);

// unused area, should always be filled with 0xFF
if (std::any_of(m_padding.begin(), m_padding.end(), [](u8 val) { return val != 0xFF; }))
if (std::ranges::any_of(m_padding, [](u8 val) { return val != 0xFF; }))
error_code.Set(GCMemcardValidityIssues::DATA_IN_UNUSED_AREA);

return error_code;
Expand Down
3 changes: 1 addition & 2 deletions Source/Core/Core/HW/WiimoteReal/WiimoteReal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,7 @@ void WiimoteScanner::SetScanMode(WiimoteScanMode scan_mode)
bool WiimoteScanner::IsReady() const
{
std::lock_guard lg(m_backends_mutex);
return std::any_of(m_backends.begin(), m_backends.end(),
[](const auto& backend) { return backend->IsReady(); });
return std::ranges::any_of(m_backends, &WiimoteScannerBackend::IsReady);
}

static void CheckForDisconnectedWiimotes()
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/IOS/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ bool IOCtlVRequest::HasNumberOfValidVectors(const size_t in_count, const size_t
return false;

auto IsValidVector = [](const auto& vector) { return vector.size == 0 || vector.address != 0; };
return std::all_of(in_vectors.begin(), in_vectors.end(), IsValidVector) &&
std::all_of(io_vectors.begin(), io_vectors.end(), IsValidVector);
return std::ranges::all_of(in_vectors, IsValidVector) &&
std::ranges::all_of(io_vectors, IsValidVector);
}

void IOCtlRequest::Log(std::string_view device_name, Common::Log::LogType type,
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/IOS/ES/Formats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ std::string TMDReader::GetGameID() const
std::memcpy(game_id, m_bytes.data() + offsetof(TMDHeader, title_id) + 4, 4);
std::memcpy(game_id + 4, m_bytes.data() + offsetof(TMDHeader, group_id), 2);

if (std::all_of(std::begin(game_id), std::end(game_id), Common::IsPrintableCharacter))
if (std::ranges::all_of(game_id, Common::IsPrintableCharacter))
return std::string(game_id, sizeof(game_id));

return fmt::format("{:016x}", GetTitleId());
Expand Down
3 changes: 1 addition & 2 deletions Source/Core/Core/IOS/ES/NandUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ static bool IsValidPartOfTitleID(const std::string& string)
{
if (string.length() != 8)
return false;
return std::all_of(string.begin(), string.end(),
[](const auto character) { return std::isxdigit(character) != 0; });
return std::ranges::all_of(string, Common::IsXDigit);
}

static std::vector<u64> GetTitlesInTitleOrImport(FS::FileSystem* fs, const std::string& titles_dir)
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/Core/IOS/ES/TitleManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ static bool HasAllRequiredContents(Kernel& ios, const ES::TMDReader& tmd)
const u64 title_id = tmd.GetTitleId();
const std::vector<ES::Content> contents = tmd.GetContents();
const ES::SharedContentMap shared_content_map{ios.GetFSCore()};
return std::all_of(contents.cbegin(), contents.cend(), [&](const ES::Content& content) {
return std::ranges::all_of(contents, [&](const ES::Content& content) {
if (content.IsOptional())
return true;

Expand Down Expand Up @@ -878,7 +878,7 @@ ReturnCode ESCore::DeleteSharedContent(const std::array<u8, 20>& sha1) const

// Check whether the shared content is used by a system title.
const std::vector<u64> titles = GetInstalledTitles();
const bool is_used_by_system_title = std::any_of(titles.begin(), titles.end(), [&](u64 id) {
const bool is_used_by_system_title = std::ranges::any_of(titles, [&](u64 id) {
if (!ES::IsTitleType(id, ES::TitleType::System))
return false;

Expand All @@ -887,8 +887,8 @@ ReturnCode ESCore::DeleteSharedContent(const std::array<u8, 20>& sha1) const
return true;

const auto contents = tmd.GetContents();
return std::any_of(contents.begin(), contents.end(),
[&sha1](const auto& content) { return content.sha1 == sha1; });
return std::ranges::any_of(contents,
[&sha1](const auto& content) { return content.sha1 == sha1; });
});

// Any shared content used by a system title cannot be deleted.
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/IOS/FS/FileSystemCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool IsValidNonRootPath(std::string_view path)
bool IsValidFilename(std::string_view filename)
{
return filename.length() <= MaxFilenameLength &&
!std::any_of(filename.begin(), filename.end(), [](char c) { return c == '/'; });
!std::ranges::any_of(filename, [](char c) { return c == '/'; });
}

SplitPathResult SplitPathAndBasename(std::string_view path)
Expand Down
7 changes: 3 additions & 4 deletions Source/Core/Core/IOS/FS/HostBackend/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ ResultCode HostFileSystem::Format(Uid uid)
ResultCode HostFileSystem::CreateFileOrDirectory(Uid uid, Gid gid, const std::string& path,
FileAttribute attr, Modes modes, bool is_file)
{
if (!IsValidNonRootPath(path) ||
!std::all_of(path.begin(), path.end(), Common::IsPrintableCharacter))
if (!IsValidNonRootPath(path) || !std::ranges::all_of(path, Common::IsPrintableCharacter))
{
return ResultCode::Invalid;
}
Expand Down Expand Up @@ -516,14 +515,14 @@ ResultCode HostFileSystem::CreateDirectory(Uid uid, Gid gid, const std::string&

bool HostFileSystem::IsFileOpened(const std::string& path) const
{
return std::any_of(m_handles.begin(), m_handles.end(), [&path](const Handle& handle) {
return std::ranges::any_of(m_handles, [&path](const Handle& handle) {
return handle.opened && handle.wii_path == path;
});
}

bool HostFileSystem::IsDirectoryInUse(const std::string& path) const
{
return std::any_of(m_handles.begin(), m_handles.end(), [&path](const Handle& handle) {
return std::ranges::any_of(m_handles, [&path](const Handle& handle) {
return handle.opened && handle.wii_path.starts_with(path);
});
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/IOS/Network/KD/Mail/WC24FriendList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ bool WC24FriendList::CheckFriendList() const

bool WC24FriendList::DoesFriendExist(u64 friend_id) const
{
return std::any_of(m_data.friend_codes.cbegin(), m_data.friend_codes.cend(),
[&friend_id](const u64 v) { return v == friend_id; });
return std::ranges::any_of(m_data.friend_codes,
[&friend_id](const u64 v) { return v == friend_id; });
}

std::vector<u64> WC24FriendList::GetUnconfirmedFriends() const
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/IOS/USB/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool Device::HasClass(const u8 device_class) const
if (GetDeviceDescriptor().bDeviceClass == device_class)
return true;
const auto interfaces = GetInterfaces(0);
return std::any_of(interfaces.begin(), interfaces.end(), [device_class](const auto& interface) {
return std::ranges::any_of(interfaces, [device_class](const auto& interface) {
return interface.bInterfaceClass == device_class;
});
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/IOS/USB/OH0/OH0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ std::optional<IPCReply> OH0::RegisterClassChangeHook(const IOCtlVRequest& reques

bool OH0::HasDeviceWithVidPid(const u16 vid, const u16 pid) const
{
return std::any_of(m_devices.begin(), m_devices.end(), [=](const auto& device) {
return std::ranges::any_of(m_devices, [=](const auto& device) {
return device.second->GetVid() == vid && device.second->GetPid() == pid;
});
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/IOS/VersionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ bool IsEmulated(u32 major_version)
if (major_version == static_cast<u32>(Titles::BC & 0xffffffff))
return true;

return std::any_of(
ios_memory_values.begin(), ios_memory_values.end(),
[major_version](const MemoryValues& values) { return values.ios_number == major_version; });
return std::ranges::any_of(ios_memory_values, [major_version](const MemoryValues& values) {
return values.ios_number == major_version;
});
}

bool IsEmulated(u64 title_id)
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/IOS/WFS/WFSSRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ s32 WFSSRVDevice::Rename(std::string source, std::string dest) const

INFO_LOG_FMT(IOS_WFS, "IOCTL_WFS_RENAME: {} to {}", source, dest);

const bool opened = std::any_of(m_fds.begin(), m_fds.end(),
[&](const auto& fd) { return fd.in_use && fd.path == source; });
const bool opened =
std::ranges::any_of(m_fds, [&](const auto& fd) { return fd.in_use && fd.path == source; });

if (opened)
return WFS_FILE_IS_OPENED;
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Movie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static std::array<u8, 20> ConvertGitRevisionToBytes(const std::string& revision)
{
std::array<u8, 20> revision_bytes{};

if (revision.size() % 2 == 0 && std::all_of(revision.begin(), revision.end(), ::isxdigit))
if (revision.size() % 2 == 0 && std::ranges::all_of(revision, Common::IsXDigit))
{
// The revision string normally contains a git commit hash,
// which is 40 hexadecimal digits long. In DTM files, each pair of
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/NetPlayClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2486,8 +2486,8 @@ bool NetPlayClient::PlayerHasControllerMapped(const PlayerId pid) const
{
const auto mapping_matches_player_id = [pid](const PlayerId& mapping) { return mapping == pid; };

return std::any_of(m_pad_map.begin(), m_pad_map.end(), mapping_matches_player_id) ||
std::any_of(m_wiimote_map.begin(), m_wiimote_map.end(), mapping_matches_player_id);
return std::ranges::any_of(m_pad_map, mapping_matches_player_id) ||
std::ranges::any_of(m_wiimote_map, mapping_matches_player_id);
}

bool NetPlayClient::IsLocalPlayer(const PlayerId pid) const
Expand Down Expand Up @@ -2543,7 +2543,7 @@ bool NetPlayClient::DoAllPlayersHaveGame()
{
std::lock_guard lkp(m_crit.players);

return std::all_of(std::begin(m_players), std::end(m_players), [](auto entry) {
return std::ranges::all_of(m_players, [](const auto& entry) {
return entry.second.game_status == SyncIdentifierComparison::SameGame;
});
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/NetPlayCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ static bool DecompressPacketIntoFolderInternal(sf::Packet& packet, const std::st
if (name.find('\\') != std::string::npos)
return false;
#endif
if (std::all_of(name.begin(), name.end(), [](char c) { return c == '.'; }))
if (std::ranges::all_of(name, [](char c) { return c == '.'; }))
return false;

bool is_folder;
Expand Down
Loading

0 comments on commit 532a862

Please sign in to comment.