Merge pull request #11487 from lioncash/str

Common: Replace StringBeginsWith/StringEndsWith with std equivalents
This commit is contained in:
Pierre Bourdon 2023-01-24 21:51:15 +01:00 committed by GitHub
commit 4b2c00e239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 57 additions and 97 deletions

View File

@ -55,7 +55,7 @@ jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector<std::string>& v
bool IsPathAndroidContent(const std::string& uri)
{
return StringBeginsWith(uri, "content://");
return uri.starts_with("content://");
}
std::string OpenModeToAndroid(std::string mode)

View File

@ -107,7 +107,7 @@ static std::string ReadCpuinfoField(const std::string& field)
while (std::getline(file, line))
{
if (!StringBeginsWith(line, field))
if (!line.starts_with(field))
continue;
auto non_tab = line.find_first_not_of("\t", field.length());
if (non_tab == line.npos)

View File

@ -3,11 +3,11 @@
#include "Common/Debug/CodeTrace.h"
#include <algorithm>
#include <chrono>
#include <regex>
#include "Common/Event.h"
#include "Common/StringUtil.h"
#include "Core/Debugger/PPCDebugInterface.h"
#include "Core/HW/CPU.h"
#include "Core/PowerPC/PowerPC.h"
@ -16,9 +16,8 @@ namespace
{
bool IsInstructionLoadStore(std::string_view ins)
{
return (StringBeginsWith(ins, "l") && !StringBeginsWith(ins, "li")) ||
StringBeginsWith(ins, "st") || StringBeginsWith(ins, "psq_l") ||
StringBeginsWith(ins, "psq_s");
return (ins.starts_with('l') && !ins.starts_with("li")) || ins.starts_with("st") ||
ins.starts_with("psq_l") || ins.starts_with("psq_s");
}
u32 GetMemoryTargetSize(std::string_view instr)
@ -95,7 +94,7 @@ InstructionAttributes CodeTrace::GetInstructionAttributes(const TraceOutput& ins
tmp_attributes.memory_target = instruction.memory_target;
tmp_attributes.memory_target_size = GetMemoryTargetSize(instr);
if (StringBeginsWith(instr, "st") || StringBeginsWith(instr, "psq_s"))
if (instr.starts_with("st") || instr.starts_with("psq_s"))
tmp_attributes.is_store = true;
else
tmp_attributes.is_load = true;
@ -263,9 +262,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 StringBeginsWith(instruction, s); });
return std::any_of(type_compare.begin(), type_compare.end(),
[&instruction](std::string_view s) { return instruction.starts_with(s); });
};
// Exclusions from updating tracking logic. mt operations are too complex and specialized.
@ -280,12 +278,12 @@ HitType CodeTrace::TraceLogic(const TraceOutput& current_instr, bool first_hit)
static const std::array<std::string_view, 2> mover{"mr", "fmr"};
// Link register for when r0 gets overwritten
if (StringBeginsWith(instr.instruction, "mflr") && match_reg0)
if (instr.instruction.starts_with("mflr") && match_reg0)
{
m_reg_autotrack.erase(reg_itr);
return HitType::OVERWRITE;
}
else if (StringBeginsWith(instr.instruction, "mtlr") && match_reg0)
if (instr.instruction.starts_with("mtlr") && match_reg0)
{
// LR is not something tracked
return HitType::MOVED;

View File

@ -171,7 +171,7 @@ bool Delete(const std::string& filename, IfAbsentBehavior behavior)
DEBUG_LOG_FMT(COMMON, "Delete: file {}", filename);
#ifdef ANDROID
if (StringBeginsWith(filename, "content://"))
if (filename.starts_with("content://"))
{
const bool success = DeleteAndroidContent(filename);
if (!success)
@ -1054,7 +1054,7 @@ void SetUserPath(unsigned int dir_index, std::string path)
#endif
// Directories should end with a separator, files should not.
while (StringEndsWith(path, "/"))
while (path.ends_with('/'))
path.pop_back();
if (path.empty())
return;

View File

@ -76,7 +76,7 @@ std::string GetMiiDatabasePath(std::optional<FromWhichRoot> from)
bool IsTitlePath(const std::string& path, std::optional<FromWhichRoot> from, u64* title_id)
{
std::string expected_prefix = RootUserPath(from) + "/title/";
if (!StringBeginsWith(path, expected_prefix))
if (!path.starts_with(expected_prefix))
{
return false;
}

View File

@ -431,16 +431,6 @@ std::string ReplaceAll(std::string result, std::string_view src, std::string_vie
return result;
}
bool StringBeginsWith(std::string_view str, std::string_view begin)
{
return str.size() >= begin.size() && std::equal(begin.begin(), begin.end(), str.begin());
}
bool StringEndsWith(std::string_view str, std::string_view end)
{
return str.size() >= end.size() && std::equal(end.rbegin(), end.rend(), str.rbegin());
}
void StringPopBackIf(std::string* s, char c)
{
if (!s->empty() && s->back() == c)

View File

@ -169,8 +169,6 @@ std::string WithUnifiedPathSeparators(std::string path);
// This requires forward slashes to be used for the path separators, even on Windows.
std::string PathToFileName(std::string_view path);
bool StringBeginsWith(std::string_view str, std::string_view begin);
bool StringEndsWith(std::string_view str, std::string_view end);
void StringPopBackIf(std::string* s, char c);
size_t StringUTF8CodePointCount(const std::string& str);

View File

@ -73,7 +73,7 @@ static std::vector<std::string> ReadM3UFile(const std::string& m3u_path,
// This is the UTF-8 representation of U+FEFF.
constexpr std::string_view utf8_bom = "\xEF\xBB\xBF";
if (StringBeginsWith(line, utf8_bom))
if (line.starts_with(utf8_bom))
{
WARN_LOG_FMT(BOOT, "UTF-8 BOM in file: {}", m3u_path);
line.erase(0, utf8_bom.length());

View File

@ -643,17 +643,17 @@ std::string GetMemcardPath(std::string configured_filename, ExpansionInterface::
constexpr std::string_view jp_region = "." JAP_DIR;
constexpr std::string_view eu_region = "." EUR_DIR;
std::optional<DiscIO::Region> path_region = std::nullopt;
if (StringEndsWith(name, us_region))
if (name.ends_with(us_region))
{
name = name.substr(0, name.size() - us_region.size());
path_region = DiscIO::Region::NTSC_U;
}
else if (StringEndsWith(name, jp_region))
else if (name.ends_with(jp_region))
{
name = name.substr(0, name.size() - jp_region.size());
path_region = DiscIO::Region::NTSC_J;
}
else if (StringEndsWith(name, eu_region))
else if (name.ends_with(eu_region))
{
name = name.substr(0, name.size() - eu_region.size());
path_region = DiscIO::Region::PAL;
@ -695,7 +695,7 @@ std::string GetGCIFolderPath(std::string configured_folder, ExpansionInterface::
// If there's no region code just insert one at the end.
UnifyPathSeparators(configured_folder);
while (StringEndsWith(configured_folder, "/"))
while (configured_folder.ends_with('/'))
configured_folder.pop_back();
constexpr std::string_view us_region = "/" USA_DIR;
@ -703,17 +703,17 @@ std::string GetGCIFolderPath(std::string configured_folder, ExpansionInterface::
constexpr std::string_view eu_region = "/" EUR_DIR;
std::string_view base_path = configured_folder;
std::optional<DiscIO::Region> path_region = std::nullopt;
if (StringEndsWith(base_path, us_region))
if (base_path.ends_with(us_region))
{
base_path = base_path.substr(0, base_path.size() - us_region.size());
path_region = DiscIO::Region::NTSC_U;
}
else if (StringEndsWith(base_path, jp_region))
else if (base_path.ends_with(jp_region))
{
base_path = base_path.substr(0, base_path.size() - jp_region.size());
path_region = DiscIO::Region::NTSC_J;
}
else if (StringEndsWith(base_path, eu_region))
else if (base_path.ends_with(eu_region))
{
base_path = base_path.substr(0, base_path.size() - eu_region.size());
path_region = DiscIO::Region::PAL;

View File

@ -197,7 +197,7 @@ void CEXIETHERNET::XLinkNetworkInterface::ReadThreadHandler(
INFO_LOG_FMT(SP1, "Received XLink Kai control data: {}", control_msg);
// connected;identifier;
if (StringBeginsWith(control_msg, "connected"))
if (control_msg.starts_with("connected"))
{
NOTICE_LOG_FMT(SP1, "XLink Kai BBA connected");
OSD::AddMessage("XLink Kai BBA connected", 4500);
@ -226,7 +226,7 @@ void CEXIETHERNET::XLinkNetworkInterface::ReadThreadHandler(
}
}
// disconnected;optional_identifier;optional_message;
else if (StringBeginsWith(control_msg, "disconnected"))
else if (control_msg.starts_with("disconnected"))
{
NOTICE_LOG_FMT(SP1, "XLink Kai BBA disconnected");
// Show OSD message for 15 seconds to make sure the user sees it
@ -247,7 +247,7 @@ void CEXIETHERNET::XLinkNetworkInterface::ReadThreadHandler(
break;
}
// keepalive;
else if (StringBeginsWith(control_msg, "keepalive"))
else if (control_msg.starts_with("keepalive"))
{
DEBUG_LOG_FMT(SP1, "XLink Kai BBA keepalive");
@ -262,7 +262,7 @@ void CEXIETHERNET::XLinkNetworkInterface::ReadThreadHandler(
}
}
// message;message_text;
else if (StringBeginsWith(control_msg, "message"))
else if (control_msg.starts_with("message"))
{
std::string msg = control_msg.substr(8, control_msg.length() - 1);
@ -271,7 +271,7 @@ void CEXIETHERNET::XLinkNetworkInterface::ReadThreadHandler(
OSD::AddMessage(std::move(msg), 15000);
}
// chat;message_text;
else if (StringBeginsWith(control_msg, "chat"))
else if (control_msg.starts_with("chat"))
{
std::string msg = control_msg.substr(5, control_msg.length() - 1);
@ -279,7 +279,7 @@ void CEXIETHERNET::XLinkNetworkInterface::ReadThreadHandler(
OSD::AddMessage(std::move(msg), 5000);
}
// directmessage;message_text;
else if (StringBeginsWith(control_msg, "directmessage"))
else if (control_msg.starts_with("directmessage"))
{
std::string msg = control_msg.substr(14, control_msg.length() - 1);

View File

@ -67,8 +67,7 @@ CEXIETHERNET::CEXIETHERNET(BBADeviceType type)
// Perform sanity check on BBA MAC address, XLink requires the vendor OUI to be Nintendo's and
// to be one of the two used for the GameCube.
// Don't actually stop the BBA from initializing though
if (!StringBeginsWith(mac_addr_setting, "00:09:bf") &&
!StringBeginsWith(mac_addr_setting, "00:17:ab"))
if (!mac_addr_setting.starts_with("00:09:bf") && !mac_addr_setting.starts_with("00:17:ab"))
{
PanicAlertFmtT(
"BBA MAC address {0} invalid for XLink Kai. A valid Nintendo GameCube MAC address "

View File

@ -31,7 +31,7 @@ HostFileSystem::HostFilename HostFileSystem::BuildFilename(const std::string& wi
{
for (const auto& redirect : m_nand_redirects)
{
if (StringBeginsWith(wii_path, redirect.source_path) &&
if (wii_path.starts_with(redirect.source_path) &&
(wii_path.size() == redirect.source_path.size() ||
wii_path[redirect.source_path.size()] == '/'))
{
@ -121,9 +121,9 @@ HostFileSystem::HostFileSystem(const std::string& root_path,
std::vector<NandRedirect> nand_redirects)
: m_root_path{root_path}, m_nand_redirects(std::move(nand_redirects))
{
while (StringEndsWith(m_root_path, "/"))
while (m_root_path.ends_with('/'))
m_root_path.pop_back();
File::CreateFullPath(m_root_path + "/");
File::CreateFullPath(m_root_path + '/');
ResetFst();
LoadFst();
}
@ -493,7 +493,7 @@ bool HostFileSystem::IsFileOpened(const std::string& path) const
bool HostFileSystem::IsDirectoryInUse(const std::string& path) const
{
return std::any_of(m_handles.begin(), m_handles.end(), [&path](const Handle& handle) {
return handle.opened && StringBeginsWith(handle.wii_path, path);
return handle.opened && handle.wii_path.starts_with(path);
});
}

View File

@ -252,7 +252,7 @@ bool PPCSymbolDB::LoadMap(const std::string& filename, bool bad)
continue;
// Support CodeWarrior and Dolphin map
if (StringEndsWith(line, " section layout\n") || strcmp(temp, ".text") == 0 ||
if (std::string_view{line}.ends_with(" section layout\n") || strcmp(temp, ".text") == 0 ||
strcmp(temp, ".init") == 0)
{
section_name = temp;
@ -284,7 +284,7 @@ bool PPCSymbolDB::LoadMap(const std::string& filename, bool bad)
// 3] _stack_addr found as linker generated symbol
// ...
// 10] EXILock(func, global) found in exi.a EXIBios.c
if (StringEndsWith(temp, "]"))
if (std::string_view{temp}.ends_with(']'))
continue;
// TODO - Handle/Write a parser for:

View File

@ -21,9 +21,9 @@ namespace
{
SignatureDB::HandlerType GetHandlerType(const std::string& file_path)
{
if (StringEndsWith(file_path, ".csv"))
if (file_path.ends_with(".csv"))
return SignatureDB::HandlerType::CSV;
if (StringEndsWith(file_path, ".mega"))
if (file_path.ends_with(".mega"))
return SignatureDB::HandlerType::MEGA;
return SignatureDB::HandlerType::DSY;
}

View File

@ -53,7 +53,7 @@ static std::vector<u8> ReadHexString(std::string_view sv)
{
if ((sv.size() % 2) == 1)
return {};
if (StringBeginsWith(sv, "0x") || StringBeginsWith(sv, "0X"))
if (sv.starts_with("0x") || sv.starts_with("0X"))
sv = sv.substr(2);
std::vector<u8> result;
@ -245,7 +245,7 @@ bool Disc::IsValidForGame(const std::string& game_id, std::optional<u16> revisio
const int disc_number_int = std::optional<int>(disc_number).value_or(-1);
const int revision_int = std::optional<int>(revision).value_or(-1);
if (m_game_filter.m_game && !StringBeginsWith(game_id_full, *m_game_filter.m_game))
if (m_game_filter.m_game && !game_id_full.starts_with(*m_game_filter.m_game))
return false;
if (m_game_filter.m_developer && game_developer != *m_game_filter.m_developer)
return false;
@ -276,7 +276,7 @@ std::vector<Patch> Disc::GeneratePatches(const std::string& game_id) const
bool replaced = false;
for (const auto& r : replacements)
{
if (StringBeginsWith(sv, r.first))
if (sv.starts_with(r.first))
{
for (char c : r.second)
result.push_back(c);

View File

@ -64,13 +64,13 @@ FileDataLoaderHostFS::MakeAbsoluteFromRelative(std::string_view external_relativ
return std::nullopt;
#endif
std::string result = StringBeginsWith(external_relative_path, "/") ? m_sd_root : m_patch_root;
std::string result = external_relative_path.starts_with('/') ? m_sd_root : m_patch_root;
std::string_view work = external_relative_path;
// Strip away all leading and trailing path separators.
while (StringBeginsWith(work, "/"))
while (work.starts_with('/'))
work.remove_prefix(1);
while (StringEndsWith(work, "/"))
while (work.ends_with('/'))
work.remove_suffix(1);
size_t depth = 0;
while (true)
@ -126,7 +126,7 @@ FileDataLoaderHostFS::MakeAbsoluteFromRelative(std::string_view external_relativ
work = work.substr(separator_position + 1);
// Remove any potential extra path separators.
while (StringBeginsWith(work, "/"))
while (work.starts_with('/'))
work = work.substr(1);
}
return result;
@ -425,9 +425,9 @@ static void ApplyFolderPatchToFST(const Patch& patch, const Folder& folder,
return std::string(b);
if (b.empty())
return std::string(a);
if (StringEndsWith(a, "/"))
if (a.ends_with('/'))
a.remove_suffix(1);
if (StringBeginsWith(b, "/"))
if (b.starts_with('/'))
b.remove_prefix(1);
return fmt::format("{}/{}", a, b);
};

View File

@ -247,7 +247,7 @@ std::vector<RedumpVerifier::PotentialMatch> RedumpVerifier::ScanDatfile(const st
if (version % 0x30 != m_revision % 0x30)
continue;
if (serials.empty() || StringBeginsWith(serials, "DS"))
if (serials.empty() || serials.starts_with("DS"))
{
// GC Datel discs have no serials in Redump, Wii Datel discs have serials like "DS000101"
if (!m_game_id.empty())
@ -666,7 +666,7 @@ bool VolumeVerifier::CheckPartition(const Partition& partition)
{
std::string file_name = f.GetName();
Common::ToLower(&file_name);
if (StringBeginsWith(file_name, correct_ios))
if (file_name.starts_with(correct_ios))
{
has_correct_ios = true;
break;
@ -865,13 +865,13 @@ void VolumeVerifier::CheckMisc()
bool inconsistent_game_id = true;
if (game_id_encrypted == "RELSAB")
{
if (StringBeginsWith(game_id_unencrypted, "410"))
if (game_id_unencrypted.starts_with("410"))
{
// This is the Wii Backup Disc (aka "pinkfish" disc),
// which legitimately has an inconsistent game ID.
inconsistent_game_id = false;
}
else if (StringBeginsWith(game_id_unencrypted, "010"))
else if (game_id_unencrypted.starts_with("010"))
{
// Hacked version of the Wii Backup Disc (aka "pinkfish" disc).
std::string proper_game_id = game_id_unencrypted;
@ -1016,7 +1016,7 @@ void VolumeVerifier::CheckMisc()
"though the files are not identical."));
}
if (IsDisc(m_volume.GetVolumeType()) && StringBeginsWith(game_id_unencrypted, "R8P"))
if (IsDisc(m_volume.GetVolumeType()) && game_id_unencrypted.starts_with("R8P"))
CheckSuperPaperMario();
}

View File

@ -235,17 +235,16 @@ u32 CodeViewWidget::AddressForRow(int row) const
static bool IsBranchInstructionWithLink(std::string_view ins)
{
return StringEndsWith(ins, "l") || StringEndsWith(ins, "la") || StringEndsWith(ins, "l+") ||
StringEndsWith(ins, "la+") || StringEndsWith(ins, "l-") || StringEndsWith(ins, "la-");
return ins.ends_with('l') || ins.ends_with("la") || ins.ends_with("l+") || ins.ends_with("la+") ||
ins.ends_with("l-") || ins.ends_with("la-");
}
static bool IsInstructionLoadStore(std::string_view ins)
{
// Could add check for context address being near PC, because we need gprs to be correct for the
// load/store.
return (StringBeginsWith(ins, "l") && !StringBeginsWith(ins, "li")) ||
StringBeginsWith(ins, "st") || StringBeginsWith(ins, "psq_l") ||
StringBeginsWith(ins, "psq_s");
return (ins.starts_with('l') && !ins.starts_with("li")) || ins.starts_with("st") ||
ins.starts_with("psq_l") || ins.starts_with("psq_s");
}
void CodeViewWidget::Update()

View File

@ -526,7 +526,7 @@ bool GameCubePane::SetGCIFolder(ExpansionInterface::Slot slot, const QString& pa
std::string raw_path =
WithUnifiedPathSeparators(QFileInfo(path).absoluteFilePath().toStdString());
while (StringEndsWith(raw_path, "/"))
while (raw_path.ends_with('/'))
raw_path.pop_back();
// The user might be attempting to reset this path to its default, check for this.

View File

@ -642,7 +642,7 @@ void Init(const std::string& game_id)
{
hasbind = true;
type = BIND_AXIS;
if (StringBeginsWith(value, "Device ''"))
if (value.starts_with("Device ''"))
sscanf(value.c_str(), "Device ''-Axis %d%c", &bindnum, &modifier);
else
sscanf(value.c_str(), "Device '%127[^\']'-Axis %d%c", dev, &bindnum, &modifier);
@ -651,7 +651,7 @@ void Init(const std::string& game_id)
{
hasbind = true;
type = BIND_BUTTON;
if (StringBeginsWith(value, "Device ''"))
if (value.starts_with("Device ''"))
sscanf(value.c_str(), "Device ''-Button %d", &bindnum);
else
sscanf(value.c_str(), "Device '%127[^\']'-Button %d", dev, &bindnum);

View File

@ -16,30 +16,6 @@ TEST(StringUtil, JoinStrings)
EXPECT_EQ("???", JoinStrings({"?", "?"}, "?"));
}
TEST(StringUtil, StringBeginsWith)
{
EXPECT_TRUE(StringBeginsWith("abc", "a"));
EXPECT_FALSE(StringBeginsWith("abc", "b"));
EXPECT_TRUE(StringBeginsWith("abc", "ab"));
EXPECT_FALSE(StringBeginsWith("a", "ab"));
EXPECT_FALSE(StringBeginsWith("", "a"));
EXPECT_FALSE(StringBeginsWith("", "ab"));
EXPECT_TRUE(StringBeginsWith("abc", ""));
EXPECT_TRUE(StringBeginsWith("", ""));
}
TEST(StringUtil, StringEndsWith)
{
EXPECT_TRUE(StringEndsWith("abc", "c"));
EXPECT_FALSE(StringEndsWith("abc", "b"));
EXPECT_TRUE(StringEndsWith("abc", "bc"));
EXPECT_FALSE(StringEndsWith("a", "ab"));
EXPECT_FALSE(StringEndsWith("", "a"));
EXPECT_FALSE(StringEndsWith("", "ab"));
EXPECT_TRUE(StringEndsWith("abc", ""));
EXPECT_TRUE(StringEndsWith("", ""));
}
TEST(StringUtil, StringPopBackIf)
{
std::string abc = "abc";