StringUtil: Replace StartsWith/EndsWith with C++20 equivalents

This commit is contained in:
Stenzek 2023-12-22 19:22:33 +10:00 committed by Connor McLaughlin
parent d73d698fd5
commit 76f36b780c
14 changed files with 72 additions and 83 deletions

View File

@ -209,17 +209,6 @@ namespace StringUtil
std::optional<std::vector<u8>> DecodeHex(const std::string_view& str);
std::string EncodeHex(const u8* data, int length);
/// starts_with from C++20
static inline bool StartsWith(const std::string_view& str, const std::string_view& prefix)
{
return (str.compare(0, prefix.length(), prefix) == 0);
}
static inline bool EndsWith(const std::string_view& str, const std::string_view& suffix)
{
const std::size_t suffix_length = suffix.length();
return (str.length() >= suffix_length && str.compare(str.length() - suffix_length, suffix_length, suffix) == 0);
}
/// StartsWith/EndsWith variants which aren't case sensitive.
static inline bool StartsWithNoCase(const std::string_view& str, const std::string_view& prefix)
{

View File

@ -476,7 +476,7 @@ std::optional<u32> InputManager::ConvertHostKeyboardStringToCode(const std::stri
{
std::string_view compare_name = str;
u32 modifier_bits = 0;
if (StringUtil::StartsWith(compare_name, "Numpad"))
if (compare_name.starts_with("Numpad"))
{
compare_name = compare_name.substr(6);
modifier_bits |= Qt::KeypadModifier;

View File

@ -429,12 +429,12 @@ static bool cdvdUncheckedLoadDiscElf(ElfObject* elfo, IsoReader& isor, const std
bool cdvdLoadElf(ElfObject* elfo, const std::string_view& elfpath, bool isPSXElf, Error* error)
{
if (StringUtil::StartsWith(elfpath, "host:"))
if (elfpath.starts_with("host:"))
{
std::string host_filename(elfpath.substr(5));
return elfo->OpenFile(host_filename, isPSXElf, error);
}
else if (StringUtil::StartsWith(elfpath, "cdrom:") || StringUtil::StartsWith(elfpath, "cdrom0:"))
else if (elfpath.starts_with("cdrom:") || elfpath.starts_with("cdrom0:"))
{
IsoReader isor;
if (!isor.Open(error))
@ -451,7 +451,7 @@ bool cdvdLoadElf(ElfObject* elfo, const std::string_view& elfpath, bool isPSXElf
bool cdvdLoadDiscElf(ElfObject* elfo, IsoReader& isor, const std::string_view& elfpath, bool isPSXElf, Error* error)
{
if (!StringUtil::StartsWith(elfpath, "cdrom:") && !StringUtil::StartsWith(elfpath, "cdrom0:"))
if (!elfpath.starts_with("cdrom:") && !elfpath.starts_with("cdrom0:"))
return false;
return cdvdUncheckedLoadDiscElf(elfo, isor, elfpath, isPSXElf, error);

View File

@ -225,7 +225,7 @@ std::vector<AdapterEntry> PCAPAdapter::GetAdapters()
entry.type = Pcsx2Config::DEV9Options::NetApi::PCAP_Switched;
#ifdef _WIN32
//guid
if (!StringUtil::StartsWith(d->name, PCAPPREFIX))
if (!std::string_view(d->name).ends_with(PCAPPREFIX))
{
Console.Error("PCAP: Unexpected Device: ", d->name);
d = d->next;

View File

@ -187,7 +187,7 @@ void GameDatabase::parseAndInsert(const std::string_view& serial, const c4::yml:
auto fix = std::string(n.val().str, n.val().len);
// Enum values don't end with Hack, but gamedb does, so remove it before comparing.
if (StringUtil::EndsWith(fix, "Hack"))
if (fix.ends_with("Hack"))
{
fix.erase(fix.size() - 4);
for (GamefixId id = GamefixId_FIRST; id < pxEnumEnd; ++id)

View File

@ -231,65 +231,65 @@ GameList::Region GameList::ParseDatabaseRegion(const std::string_view& db_region
// clang-format off
////// NTSC //////
//////////////////
if (StringUtil::StartsWith(db_region, "NTSC-B"))
if (db_region.starts_with("NTSC-B"))
return Region::NTSC_B;
else if (StringUtil::StartsWith(db_region, "NTSC-C"))
else if (db_region.starts_with("NTSC-C"))
return Region::NTSC_C;
else if (StringUtil::StartsWith(db_region, "NTSC-HK"))
else if (db_region.starts_with("NTSC-HK"))
return Region::NTSC_HK;
else if (StringUtil::StartsWith(db_region, "NTSC-J"))
else if (db_region.starts_with("NTSC-J"))
return Region::NTSC_J;
else if (StringUtil::StartsWith(db_region, "NTSC-K"))
else if (db_region.starts_with("NTSC-K"))
return Region::NTSC_K;
else if (StringUtil::StartsWith(db_region, "NTSC-T"))
else if (db_region.starts_with("NTSC-T"))
return Region::NTSC_T;
else if (StringUtil::StartsWith(db_region, "NTSC-U"))
else if (db_region.starts_with("NTSC-U"))
return Region::NTSC_U;
////// PAL //////
//////////////////
else if (StringUtil::StartsWith(db_region, "PAL-AF"))
else if (db_region.starts_with("PAL-AF"))
return Region::PAL_AF;
else if (StringUtil::StartsWith(db_region, "PAL-AU"))
else if (db_region.starts_with("PAL-AU"))
return Region::PAL_AU;
else if (StringUtil::StartsWith(db_region, "PAL-A"))
else if (db_region.starts_with("PAL-A"))
return Region::PAL_A;
else if (StringUtil::StartsWith(db_region, "PAL-BE"))
else if (db_region.starts_with("PAL-BE"))
return Region::PAL_BE;
else if (StringUtil::StartsWith(db_region, "PAL-E"))
else if (db_region.starts_with("PAL-E"))
return Region::PAL_E;
else if (StringUtil::StartsWith(db_region, "PAL-FI"))
else if (db_region.starts_with("PAL-FI"))
return Region::PAL_FI;
else if (StringUtil::StartsWith(db_region, "PAL-F"))
else if (db_region.starts_with("PAL-F"))
return Region::PAL_F;
else if (StringUtil::StartsWith(db_region, "PAL-GR"))
else if (db_region.starts_with("PAL-GR"))
return Region::PAL_GR;
else if (StringUtil::StartsWith(db_region, "PAL-G"))
else if (db_region.starts_with("PAL-G"))
return Region::PAL_G;
else if (StringUtil::StartsWith(db_region, "PAL-IN"))
else if (db_region.starts_with("PAL-IN"))
return Region::PAL_IN;
else if (StringUtil::StartsWith(db_region, "PAL-I"))
else if (db_region.starts_with("PAL-I"))
return Region::PAL_I;
else if (StringUtil::StartsWith(db_region, "PAL-M"))
else if (db_region.starts_with("PAL-M"))
return Region::PAL_M;
else if (StringUtil::StartsWith(db_region, "PAL-NL"))
else if (db_region.starts_with("PAL-NL"))
return Region::PAL_NL;
else if (StringUtil::StartsWith(db_region, "PAL-NO"))
else if (db_region.starts_with("PAL-NO"))
return Region::PAL_NO;
else if (StringUtil::StartsWith(db_region, "PAL-PL"))
else if (db_region.starts_with("PAL-PL"))
return Region::PAL_PL;
else if (StringUtil::StartsWith(db_region, "PAL-P"))
else if (db_region.starts_with("PAL-P"))
return Region::PAL_P;
else if (StringUtil::StartsWith(db_region, "PAL-R"))
else if (db_region.starts_with("PAL-R"))
return Region::PAL_R;
else if (StringUtil::StartsWith(db_region, "PAL-SC"))
else if (db_region.starts_with("PAL-SC"))
return Region::PAL_SC;
else if (StringUtil::StartsWith(db_region, "PAL-SWI"))
else if (db_region.starts_with("PAL-SWI"))
return Region::PAL_SWI;
else if (StringUtil::StartsWith(db_region, "PAL-SW"))
else if (db_region.starts_with("PAL-SW"))
return Region::PAL_SW;
else if (StringUtil::StartsWith(db_region, "PAL-S"))
else if (db_region.starts_with("PAL-S"))
return Region::PAL_S;
else if (StringUtil::StartsWith(db_region, "PAL-UK"))
else if (db_region.starts_with("PAL-UK"))
return Region::PAL_UK;
else
return Region::Other;

View File

@ -325,7 +325,7 @@ void DInputSource::UpdateMotorState(InputBindingKey large_key, InputBindingKey s
std::optional<InputBindingKey> DInputSource::ParseKeyString(const std::string_view& device, const std::string_view& binding)
{
if (!StringUtil::StartsWith(device, "DInput-") || binding.empty())
if (!device.starts_with("DInput-") || binding.empty())
return std::nullopt;
const std::optional<s32> player_id = StringUtil::FromChars<s32>(device.substr(7));
@ -336,7 +336,7 @@ std::optional<InputBindingKey> DInputSource::ParseKeyString(const std::string_vi
key.source_type = InputSourceType::DInput;
key.source_index = static_cast<u32>(player_id.value());
if (StringUtil::StartsWith(binding, "+Axis") || StringUtil::StartsWith(binding, "-Axis"))
if (binding.starts_with("+Axis") || binding.starts_with("-Axis"))
{
std::string_view end;
const std::optional<u32> axis_index = StringUtil::FromChars<u32>(binding.substr(5), 10, &end);
@ -349,7 +349,7 @@ std::optional<InputBindingKey> DInputSource::ParseKeyString(const std::string_vi
key.invert = (end == "~");
return key;
}
else if (StringUtil::StartsWith(binding, "FullAxis"))
else if (binding.starts_with("FullAxis"))
{
std::string_view end;
const std::optional<u32> axis_index = StringUtil::FromChars<u32>(binding.substr(8), 10, &end);
@ -362,7 +362,7 @@ std::optional<InputBindingKey> DInputSource::ParseKeyString(const std::string_vi
key.invert = (end == "~");
return key;
}
else if (StringUtil::StartsWith(binding, "Hat"))
else if (binding.starts_with("Hat"))
{
if (binding[3] < '0' || binding[3] > '9' || binding.length() < 5)
return std::nullopt;
@ -382,7 +382,7 @@ std::optional<InputBindingKey> DInputSource::ParseKeyString(const std::string_vi
// bad direction
return std::nullopt;
}
else if (StringUtil::StartsWith(binding, "Button"))
else if (binding.starts_with("Button"))
{
const std::optional<u32> button_index = StringUtil::FromChars<u32>(binding.substr(6));
if (!button_index.has_value())

View File

@ -233,11 +233,11 @@ std::optional<InputBindingKey> InputManager::ParseInputBindingKey(const std::str
return std::nullopt;
// lameee, string matching
if (StringUtil::StartsWith(source, "Keyboard"))
if (source.starts_with("Keyboard"))
{
return ParseHostKeyboardKey(source, sub_binding);
}
else if (StringUtil::StartsWith(source, "Pointer"))
else if (source.starts_with("Pointer"))
{
return ParsePointerKey(source, sub_binding);
}
@ -411,7 +411,7 @@ void InputManager::PrettifyInputBindingPart(const std::string_view binding, Smal
return;
// lameee, string matching
if (StringUtil::StartsWith(source, "Keyboard"))
if (source.starts_with("Keyboard"))
{
std::optional<InputBindingKey> key = ParseHostKeyboardKey(source, sub_binding);
const char* icon = key.has_value() ? ConvertHostKeyboardCodeToIcon(key->data) : nullptr;
@ -422,7 +422,7 @@ void InputManager::PrettifyInputBindingPart(const std::string_view binding, Smal
return;
}
}
else if (StringUtil::StartsWith(source, "Pointer"))
else if (source.starts_with("Pointer"))
{
const std::optional<InputBindingKey> key = ParsePointerKey(source, sub_binding);
if (key.has_value())
@ -633,7 +633,7 @@ std::optional<InputBindingKey> InputManager::ParsePointerKey(const std::string_v
key.source_type = InputSourceType::Pointer;
key.source_index = static_cast<u32>(pointer_index.value());
if (StringUtil::StartsWith(sub_binding, "Button"))
if (sub_binding.starts_with("Button"))
{
const std::optional<s32> button_number = StringUtil::FromChars<s32>(sub_binding.substr(6));
if (!button_number.has_value() || button_number.value() < 0)
@ -646,7 +646,7 @@ std::optional<InputBindingKey> InputManager::ParsePointerKey(const std::string_v
for (u32 i = 0; i < s_pointer_axis_names.size(); i++)
{
if (StringUtil::StartsWith(sub_binding, s_pointer_axis_names[i]))
if (sub_binding.starts_with(s_pointer_axis_names[i]))
{
key.source_subtype = InputSubclass::PointerAxis;
key.data = i;
@ -678,7 +678,7 @@ std::optional<InputBindingKey> InputManager::ParsePointerKey(const std::string_v
std::optional<u32> InputManager::GetIndexFromPointerBinding(const std::string_view& source)
{
if (!StringUtil::StartsWith(source, "Pointer-"))
if (!source.starts_with("Pointer-"))
return std::nullopt;
const std::optional<s32> pointer_index = StringUtil::FromChars<s32>(source.substr(8));

View File

@ -92,7 +92,7 @@ std::optional<InputBindingKey> InputSource::ParseGenericControllerKey(
key.source_type = clazz;
key.source_index = source_index.value();
if (StringUtil::StartsWith(sub_binding, "+Axis") || StringUtil::StartsWith(sub_binding, "-Axis"))
if (sub_binding.starts_with("+Axis") || sub_binding.starts_with("-Axis"))
{
const std::optional<s32> axis_number = StringUtil::FromChars<s32>(sub_binding.substr(5));
if (!axis_number.has_value() || axis_number.value() < 0)
@ -108,7 +108,7 @@ std::optional<InputBindingKey> InputSource::ParseGenericControllerKey(
else
return std::nullopt;
}
else if (StringUtil::StartsWith(sub_binding, "FullAxis"))
else if (sub_binding.starts_with("FullAxis"))
{
const std::optional<s32> axis_number = StringUtil::FromChars<s32>(sub_binding.substr(8));
if (!axis_number.has_value() || axis_number.value() < 0)
@ -117,7 +117,7 @@ std::optional<InputBindingKey> InputSource::ParseGenericControllerKey(
key.data = static_cast<u32>(axis_number.value());
key.modifier = InputModifier::FullAxis;
}
else if (StringUtil::StartsWith(sub_binding, "Button"))
else if (sub_binding.starts_with("Button"))
{
const std::optional<s32> button_number = StringUtil::FromChars<s32>(sub_binding.substr(6));
if (!button_number.has_value() || button_number.value() < 0)

View File

@ -328,7 +328,7 @@ std::vector<std::pair<std::string, std::string>> SDLInputSource::EnumerateDevice
std::optional<InputBindingKey> SDLInputSource::ParseKeyString(const std::string_view& device, const std::string_view& binding)
{
if (!StringUtil::StartsWith(device, "SDL-") || binding.empty())
if (!device.starts_with("SDL-") || binding.empty())
return std::nullopt;
const std::optional<s32> player_id = StringUtil::FromChars<s32>(device.substr(4));
@ -339,7 +339,7 @@ std::optional<InputBindingKey> SDLInputSource::ParseKeyString(const std::string_
key.source_type = InputSourceType::SDL;
key.source_index = static_cast<u32>(player_id.value());
if (StringUtil::EndsWith(binding, "Motor"))
if (binding.ends_with("Motor"))
{
key.source_subtype = InputSubclass::ControllerMotor;
if (binding == "LargeMotor")
@ -357,7 +357,7 @@ std::optional<InputBindingKey> SDLInputSource::ParseKeyString(const std::string_
return std::nullopt;
}
}
else if (StringUtil::EndsWith(binding, "Haptic"))
else if (binding.ends_with("Haptic"))
{
key.source_subtype = InputSubclass::ControllerHaptic;
key.data = 0;
@ -368,7 +368,7 @@ std::optional<InputBindingKey> SDLInputSource::ParseKeyString(const std::string_
// likely an axis
const std::string_view axis_name(binding.substr(1));
if (StringUtil::StartsWith(axis_name, "Axis"))
if (axis_name.starts_with("Axis"))
{
std::string_view end;
if (auto value = StringUtil::FromChars<u32>(axis_name.substr(4), 10, &end))
@ -392,7 +392,7 @@ std::optional<InputBindingKey> SDLInputSource::ParseKeyString(const std::string_
}
}
}
else if (StringUtil::StartsWith(binding, "FullAxis"))
else if (binding.starts_with("FullAxis"))
{
std::string_view end;
if (auto value = StringUtil::FromChars<u32>(binding.substr(8), 10, &end))
@ -404,7 +404,7 @@ std::optional<InputBindingKey> SDLInputSource::ParseKeyString(const std::string_
return key;
}
}
else if (StringUtil::StartsWith(binding, "Hat"))
else if (binding.starts_with("Hat"))
{
std::string_view hat_dir;
if (auto value = StringUtil::FromChars<u32>(binding.substr(3), 10, &hat_dir); value.has_value() && !hat_dir.empty())
@ -423,7 +423,7 @@ std::optional<InputBindingKey> SDLInputSource::ParseKeyString(const std::string_
else
{
// must be a button
if (StringUtil::StartsWith(binding, "Button"))
if (binding.starts_with("Button"))
{
if (auto value = StringUtil::FromChars<u32>(binding.substr(6)))
{
@ -575,7 +575,7 @@ bool SDLInputSource::ProcessSDLEvent(const SDL_Event* event)
SDL_Joystick* SDLInputSource::GetJoystickForDevice(const std::string_view& device)
{
if (!StringUtil::StartsWith(device, "SDL-"))
if (!device.starts_with("SDL-"))
return nullptr;
const std::optional<s32> player_id = StringUtil::FromChars<s32>(device.substr(4));
@ -890,7 +890,7 @@ std::vector<InputBindingKey> SDLInputSource::EnumerateMotors()
bool SDLInputSource::GetGenericBindingMapping(const std::string_view& device, InputManager::GenericInputBindingMapping* mapping)
{
if (!StringUtil::StartsWith(device, "SDL-"))
if (!device.starts_with("SDL-"))
return false;
const std::optional<s32> player_id = StringUtil::FromChars<s32>(device.substr(4));

View File

@ -266,7 +266,7 @@ std::vector<std::pair<std::string, std::string>> XInputSource::EnumerateDevices(
std::optional<InputBindingKey> XInputSource::ParseKeyString(const std::string_view& device, const std::string_view& binding)
{
if (!StringUtil::StartsWith(device, "XInput-") || binding.empty())
if (!device.starts_with("XInput-") || binding.empty())
return std::nullopt;
const std::optional<s32> player_id = StringUtil::FromChars<s32>(device.substr(7));
@ -277,7 +277,7 @@ std::optional<InputBindingKey> XInputSource::ParseKeyString(const std::string_vi
key.source_type = InputSourceType::XInput;
key.source_index = static_cast<u32>(player_id.value());
if (StringUtil::EndsWith(binding, "Motor"))
if (binding.ends_with("Motor"))
{
key.source_subtype = InputSubclass::ControllerMotor;
if (binding == "LargeMotor")
@ -399,7 +399,7 @@ std::vector<InputBindingKey> XInputSource::EnumerateMotors()
bool XInputSource::GetGenericBindingMapping(const std::string_view& device, InputManager::GenericInputBindingMapping* mapping)
{
if (!StringUtil::StartsWith(device, "XInput-"))
if (!device.starts_with("XInput-"))
return false;
const std::optional<s32> player_id = StringUtil::FromChars<s32>(device.substr(7));

View File

@ -169,7 +169,7 @@ namespace R3000A
// For now it just supports relative folders from the location of the elf
std::string native_path(Path::Canonicalize(path));
std::string new_path;
if (!hostRoot.empty() && StringUtil::StartsWith(native_path, hostRoot))
if (!hostRoot.empty() && native_path.starts_with(hostRoot))
new_path = std::move(native_path);
else if (!hostRoot.empty()) // relative paths
new_path = Path::Combine(hostRoot, native_path);
@ -195,7 +195,7 @@ namespace R3000A
{
// Only allow descendants of the hostfs directory.
if (canonicalized_path.length() <= hostRoot.length() || // Length has to be equal or longer,
!StringUtil::StartsWith(canonicalized_path, hostRoot) || // and start with the host root,
!canonicalized_path.starts_with(hostRoot) || // and start with the host root,
canonicalized_path[hostRoot.length()] != FS_OSPATH_SEPARATOR_CHARACTER) // and we can't access a sibling.
{
Console.Error(fmt::format(

View File

@ -323,7 +323,7 @@ void FileMemoryCard::Open()
FileSystem::SetPathCompression(fname.c_str(), EmuConfig.McdCompressNTFS);
#endif
if (StringUtil::EndsWith(fname, ".bin"))
if (fname.ends_with(".bin"))
{
std::string newname(fname + "x");
if (!ConvertNoECCtoRAW(fname.c_str(), newname.c_str()))
@ -385,7 +385,7 @@ void FileMemoryCard::Close()
std::fclose(m_file[slot]);
m_file[slot] = nullptr;
if (StringUtil::EndsWith(m_filenames[slot], ".bin"))
if (m_filenames[slot].ends_with(".bin"))
{
const std::string name_in(m_filenames[slot] + 'x');
if (ConvertRAWtoNoECC(name_in.c_str(), m_filenames[slot].c_str()))
@ -890,8 +890,8 @@ std::vector<AvailableMcdInfo> FileMcd_GetAvailableCards(bool include_in_use_card
}
// We only want relevant file types.
if (!(StringUtil::EndsWith(fd.FileName, ".ps2") || StringUtil::EndsWith(fd.FileName, ".mcr") ||
StringUtil::EndsWith(fd.FileName, ".mcd") || StringUtil::EndsWith(fd.FileName, ".bin")))
if (!(fd.FileName.ends_with(".ps2") || fd.FileName.ends_with(".mcr") ||
fd.FileName.ends_with(".mcd") || fd.FileName.ends_with(".bin")))
continue;
if (fd.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY)

View File

@ -576,7 +576,7 @@ bool FolderMemoryCard::AddFolder(MemoryCardFileEntry* const dirEntry, const std:
bool FolderMemoryCard::AddFile(MemoryCardFileEntry* const dirEntry, const std::string& dirPath, const EnumeratedFileEntry& fileEntry, MemoryCardFileMetadataReference* parent)
{
const std::string filePath(Path::Combine(dirPath, fileEntry.m_fileName));
pxAssertMsg(StringUtil::StartsWith(filePath, m_folderName.c_str()), "Full file path starts with MC folder path");
pxAssertMsg(filePath.starts_with(m_folderName), "Full file path starts with MC folder path");
const std::string relativeFilePath(filePath.substr(m_folderName.length() + 1));
if (auto file = FileSystem::OpenManagedCFile(filePath.c_str(), "rb"); file)
@ -668,7 +668,7 @@ u32 FolderMemoryCard::CalculateRequiredClustersOfDirectory(const std::string& di
FileSystem::FindFiles(dirPath.c_str(), "*", FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_FOLDERS | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RELATIVE_PATHS, &files);
for (const FILESYSTEM_FIND_DATA& fd : files)
{
if (StringUtil::StartsWith(fd.FileName, "_pcsx2_"))
if (fd.FileName.starts_with("_pcsx2_"))
continue;
++requiredFileEntryPages;
@ -1803,7 +1803,7 @@ std::vector<FolderMemoryCard::EnumeratedFileEntry> FolderMemoryCard::GetOrderedF
for (FILESYSTEM_FIND_DATA& fd : results)
{
if (StringUtil::StartsWith(fd.FileName, "_pcsx2_"))
if (fd.FileName.starts_with("_pcsx2_"))
continue;
std::string filePath(Path::Combine(dirPath, fd.FileName));
@ -2176,7 +2176,7 @@ void FileAccessHelper::CloseMatching(const std::string_view& path)
{
for (auto it = m_files.begin(); it != m_files.end();)
{
if (StringUtil::StartsWith(it->second.hostFilePath, path))
if (it->second.hostFilePath.starts_with(path))
{
CloseFileHandle(it->second.fileHandle, it->second.fileRef->entry);
it = m_files.erase(it);