Misc: Remove StringUtil::{Starts,Ends}With, use C++20

This commit is contained in:
Stenzek 2023-12-13 21:15:07 +10:00
parent 79c226efff
commit 710698f7e1
No known key found for this signature in database
11 changed files with 37 additions and 48 deletions

View File

@ -206,17 +206,6 @@ inline std::string ToChars(bool value, int base)
std::optional<std::vector<u8>> DecodeHex(const std::string_view& str); std::optional<std::vector<u8>> DecodeHex(const std::string_view& str);
std::string EncodeHex(const u8* data, int length); std::string EncodeHex(const u8* data, int length);
/// starts_with from C++20
ALWAYS_INLINE static bool StartsWith(const std::string_view& str, const std::string_view& prefix)
{
return (str.compare(0, prefix.length(), prefix) == 0);
}
ALWAYS_INLINE static 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. /// StartsWith/EndsWith variants which aren't case sensitive.
ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view& str, const std::string_view& prefix) ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view& str, const std::string_view& prefix)
{ {

View File

@ -306,7 +306,7 @@ std::string ProcessPacket(const std::string_view& data)
// Try to invoke packet command. // Try to invoke packet command.
bool processed = false; bool processed = false;
for (const auto& command : COMMANDS) { for (const auto& command : COMMANDS) {
if (StringUtil::StartsWith(packet->data(), command.first)) { if (packet->starts_with(command.first)) {
Log_DebugPrintf("Processing command '%s'", command.first); Log_DebugPrintf("Processing command '%s'", command.first);
// Invoke command, remove command name from payload. // Invoke command, remove command name from payload.

View File

@ -775,7 +775,7 @@ bool MemoryCardImage::ImportSave(DataArray* data, const char* filename)
return false; return false;
} }
if (StringUtil::EndsWith(filename, ".mcs")) if (std::string_view(filename).ends_with(".mcs"))
{ {
return ImportSaveWithDirectoryFrame(data, filename, sd); return ImportSaveWithDirectoryFrame(data, filename, sd);
} }

View File

@ -82,7 +82,7 @@ static std::string ResolveHostPath(const std::string& path)
const std::string& root = g_settings.pcdrv_root; const std::string& root = g_settings.pcdrv_root;
std::string canonicalized_path = Path::Canonicalize(Path::Combine(root, path)); std::string canonicalized_path = Path::Canonicalize(Path::Combine(root, path));
if (canonicalized_path.length() < root.length() || // Length has to be longer (a file), if (canonicalized_path.length() < root.length() || // Length has to be longer (a file),
!StringUtil::StartsWith(canonicalized_path, root) || // and start with the host root, !canonicalized_path.starts_with(root) || // and start with the host root,
canonicalized_path[root.length()] != FS_OSPATH_SEPARATOR_CHARACTER) // and we can't access a sibling. canonicalized_path[root.length()] != FS_OSPATH_SEPARATOR_CHARACTER) // and we can't access a sibling.
{ {
Log_ErrorPrintf("Denying access to path outside of PCDrv directory. Requested path: '%s', " Log_ErrorPrintf("Denying access to path outside of PCDrv directory. Requested path: '%s', "

View File

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

View File

@ -500,7 +500,7 @@ std::vector<std::pair<std::string, std::string>> CDImage::GetDeviceList()
bool CDImage::IsDeviceName(const char* filename) bool CDImage::IsDeviceName(const char* filename)
{ {
return StringUtil::StartsWith(filename, "\\\\.\\"); return std::string_view(filename).starts_with("\\\\.\\");
} }
#else #else

View File

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

View File

@ -241,15 +241,15 @@ std::optional<InputBindingKey> InputManager::ParseInputBindingKey(const std::str
return std::nullopt; return std::nullopt;
// lameee, string matching // lameee, string matching
if (StringUtil::StartsWith(source, "Keyboard")) if (source.starts_with("Keyboard"))
{ {
return ParseHostKeyboardKey(source, sub_binding); return ParseHostKeyboardKey(source, sub_binding);
} }
else if (StringUtil::StartsWith(source, "Pointer")) else if (source.starts_with("Pointer"))
{ {
return ParsePointerKey(source, sub_binding); return ParsePointerKey(source, sub_binding);
} }
else if (StringUtil::StartsWith(source, "Sensor")) else if (source.starts_with("Sensor"))
{ {
return ParseSensorKey(source, sub_binding); return ParseSensorKey(source, sub_binding);
} }
@ -420,7 +420,7 @@ void InputManager::PrettifyInputBindingPart(const std::string_view binding, Smal
return; return;
// lameee, string matching // lameee, string matching
if (StringUtil::StartsWith(source, "Keyboard")) if (source.starts_with("Keyboard"))
{ {
std::optional<InputBindingKey> key = ParseHostKeyboardKey(source, sub_binding); std::optional<InputBindingKey> key = ParseHostKeyboardKey(source, sub_binding);
const char* icon = key.has_value() ? ConvertHostKeyboardCodeToIcon(key->data) : nullptr; const char* icon = key.has_value() ? ConvertHostKeyboardCodeToIcon(key->data) : nullptr;
@ -431,7 +431,7 @@ void InputManager::PrettifyInputBindingPart(const std::string_view binding, Smal
return; return;
} }
} }
else if (StringUtil::StartsWith(source, "Pointer")) else if (source.starts_with("Pointer"))
{ {
const std::optional<InputBindingKey> key = ParsePointerKey(source, sub_binding); const std::optional<InputBindingKey> key = ParsePointerKey(source, sub_binding);
if (key.has_value()) if (key.has_value())
@ -451,7 +451,7 @@ void InputManager::PrettifyInputBindingPart(const std::string_view binding, Smal
} }
} }
} }
else if (StringUtil::StartsWith(source, "Sensor")) else if (source.starts_with("Sensor"))
{ {
} }
else else
@ -691,7 +691,7 @@ std::optional<InputBindingKey> InputManager::ParsePointerKey(const std::string_v
key.source_type = InputSourceType::Pointer; key.source_type = InputSourceType::Pointer;
key.source_index = static_cast<u32>(pointer_index.value()); 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)); const std::optional<s32> button_number = StringUtil::FromChars<s32>(sub_binding.substr(6));
if (!button_number.has_value() || button_number.value() < 0) if (!button_number.has_value() || button_number.value() < 0)
@ -704,7 +704,7 @@ std::optional<InputBindingKey> InputManager::ParsePointerKey(const std::string_v
for (u32 i = 0; i < s_pointer_axis_names.size(); i++) 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.source_subtype = InputSubclass::PointerAxis;
key.data = i; key.data = i;
@ -736,7 +736,7 @@ std::optional<InputBindingKey> InputManager::ParsePointerKey(const std::string_v
std::optional<u32> InputManager::GetIndexFromPointerBinding(const std::string_view& source) std::optional<u32> InputManager::GetIndexFromPointerBinding(const std::string_view& source)
{ {
if (!StringUtil::StartsWith(source, "Pointer-")) if (!source.starts_with("Pointer-"))
return std::nullopt; return std::nullopt;
const std::optional<s32> pointer_index = StringUtil::FromChars<s32>(source.substr(8)); const std::optional<s32> pointer_index = StringUtil::FromChars<s32>(source.substr(8));
@ -763,7 +763,7 @@ std::optional<InputBindingKey> InputManager::ParseSensorKey(const std::string_vi
for (u32 i = 0; i < s_sensor_accelerometer_names.size(); i++) for (u32 i = 0; i < s_sensor_accelerometer_names.size(); i++)
{ {
if (StringUtil::StartsWith(sub_binding, s_sensor_accelerometer_names[i])) if (sub_binding.starts_with(s_sensor_accelerometer_names[i]))
{ {
key.source_subtype = InputSubclass::SensorAccelerometer; key.source_subtype = InputSubclass::SensorAccelerometer;
key.data = i; key.data = i;
@ -1855,14 +1855,14 @@ bool InputManager::MigrateBindings(SettingsInterface& si)
Log_DevPrintf("%s -> %s", old_bind.c_str(), new_bind.c_str()); Log_DevPrintf("%s -> %s", old_bind.c_str(), new_bind.c_str());
num_changes++; num_changes++;
} }
else if (StringUtil::StartsWith(old_bind.c_str(), "Keyboard/Keypad+")) else if (old_bind.starts_with("Keyboard/Keypad+"))
{ {
new_bind.format("Keyboard/Numpad{}", old_bind.substr(16)); new_bind.format("Keyboard/Numpad{}", old_bind.substr(16));
si.SetStringValue(new_section.c_str(), new_key, new_bind); si.SetStringValue(new_section.c_str(), new_key, new_bind);
Log_DevPrintf("%s -> %s", old_bind.c_str(), new_bind.c_str()); Log_DevPrintf("%s -> %s", old_bind.c_str(), new_bind.c_str());
num_changes++; num_changes++;
} }
else if (StringUtil::StartsWith(old_bind.c_str(), "Keyboard/")) else if (old_bind.starts_with("Keyboard/"))
{ {
// pass through as-is // pass through as-is
si.SetStringValue(new_section.c_str(), new_key, old_bind.c_str()); si.SetStringValue(new_section.c_str(), new_key, old_bind.c_str());

View File

@ -82,7 +82,7 @@ std::optional<InputBindingKey> InputSource::ParseGenericControllerKey(InputSourc
key.source_type = clazz; key.source_type = clazz;
key.source_index = source_index.value(); 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)); const std::optional<s32> axis_number = StringUtil::FromChars<s32>(sub_binding.substr(5));
if (!axis_number.has_value() || axis_number.value() < 0) if (!axis_number.has_value() || axis_number.value() < 0)
@ -98,7 +98,7 @@ std::optional<InputBindingKey> InputSource::ParseGenericControllerKey(InputSourc
else else
return std::nullopt; 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)); const std::optional<s32> axis_number = StringUtil::FromChars<s32>(sub_binding.substr(8));
if (!axis_number.has_value() || axis_number.value() < 0) if (!axis_number.has_value() || axis_number.value() < 0)
@ -107,7 +107,7 @@ std::optional<InputBindingKey> InputSource::ParseGenericControllerKey(InputSourc
key.data = static_cast<u32>(axis_number.value()); key.data = static_cast<u32>(axis_number.value());
key.modifier = InputModifier::FullAxis; 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)); const std::optional<s32> button_number = StringUtil::FromChars<s32>(sub_binding.substr(6));
if (!button_number.has_value() || button_number.value() < 0) if (!button_number.has_value() || button_number.value() < 0)

View File

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

View File

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