From 710698f7e1eeb0ecd58be293da378f6361fcd375 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Wed, 13 Dec 2023 21:15:07 +1000 Subject: [PATCH] Misc: Remove StringUtil::{Starts,Ends}With, use C++20 --- src/common/string_util.h | 11 ----------- src/core/gdb_protocol.cpp | 2 +- src/core/memory_card_image.cpp | 2 +- src/core/pcdrv.cpp | 2 +- src/duckstation-qt/qtkeycodes.cpp | 2 +- src/util/cd_image_device.cpp | 2 +- src/util/dinput_source.cpp | 10 +++++----- src/util/input_manager.cpp | 24 ++++++++++++------------ src/util/input_source.cpp | 6 +++--- src/util/sdl_input_source.cpp | 18 +++++++++--------- src/util/xinput_source.cpp | 6 +++--- 11 files changed, 37 insertions(+), 48 deletions(-) diff --git a/src/common/string_util.h b/src/common/string_util.h index 7b079a31e..4eabfd307 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -206,17 +206,6 @@ inline std::string ToChars(bool value, int base) std::optional> DecodeHex(const std::string_view& str); 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. ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view& str, const std::string_view& prefix) { diff --git a/src/core/gdb_protocol.cpp b/src/core/gdb_protocol.cpp index 8276a5c79..dc9af1cd8 100644 --- a/src/core/gdb_protocol.cpp +++ b/src/core/gdb_protocol.cpp @@ -306,7 +306,7 @@ std::string ProcessPacket(const std::string_view& data) // Try to invoke packet command. bool processed = false; 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); // Invoke command, remove command name from payload. diff --git a/src/core/memory_card_image.cpp b/src/core/memory_card_image.cpp index 0b970f71d..ed27affae 100644 --- a/src/core/memory_card_image.cpp +++ b/src/core/memory_card_image.cpp @@ -775,7 +775,7 @@ bool MemoryCardImage::ImportSave(DataArray* data, const char* filename) return false; } - if (StringUtil::EndsWith(filename, ".mcs")) + if (std::string_view(filename).ends_with(".mcs")) { return ImportSaveWithDirectoryFrame(data, filename, sd); } diff --git a/src/core/pcdrv.cpp b/src/core/pcdrv.cpp index 88ed5bb1e..17590271e 100644 --- a/src/core/pcdrv.cpp +++ b/src/core/pcdrv.cpp @@ -82,7 +82,7 @@ static std::string ResolveHostPath(const std::string& path) const std::string& root = g_settings.pcdrv_root; std::string canonicalized_path = Path::Canonicalize(Path::Combine(root, path)); 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. { Log_ErrorPrintf("Denying access to path outside of PCDrv directory. Requested path: '%s', " diff --git a/src/duckstation-qt/qtkeycodes.cpp b/src/duckstation-qt/qtkeycodes.cpp index 8de6d928e..cdcf0b161 100644 --- a/src/duckstation-qt/qtkeycodes.cpp +++ b/src/duckstation-qt/qtkeycodes.cpp @@ -462,7 +462,7 @@ std::optional 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; diff --git a/src/util/cd_image_device.cpp b/src/util/cd_image_device.cpp index d38da8f89..e4006b66f 100644 --- a/src/util/cd_image_device.cpp +++ b/src/util/cd_image_device.cpp @@ -500,7 +500,7 @@ std::vector> CDImage::GetDeviceList() bool CDImage::IsDeviceName(const char* filename) { - return StringUtil::StartsWith(filename, "\\\\.\\"); + return std::string_view(filename).starts_with("\\\\.\\"); } #else diff --git a/src/util/dinput_source.cpp b/src/util/dinput_source.cpp index 8622dd7ba..52256ff3e 100644 --- a/src/util/dinput_source.cpp +++ b/src/util/dinput_source.cpp @@ -326,7 +326,7 @@ void DInputSource::UpdateMotorState(InputBindingKey large_key, InputBindingKey s std::optional 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 player_id = StringUtil::FromChars(device.substr(7)); @@ -337,7 +337,7 @@ std::optional DInputSource::ParseKeyString(const std::string_vi key.source_type = InputSourceType::DInput; key.source_index = static_cast(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 axis_index = StringUtil::FromChars(binding.substr(5), 10, &end); @@ -350,7 +350,7 @@ std::optional 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 axis_index = StringUtil::FromChars(binding.substr(8), 10, &end); @@ -363,7 +363,7 @@ std::optional 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; @@ -383,7 +383,7 @@ std::optional 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 button_index = StringUtil::FromChars(binding.substr(6)); if (!button_index.has_value()) diff --git a/src/util/input_manager.cpp b/src/util/input_manager.cpp index d7611e03e..ef58ae06e 100644 --- a/src/util/input_manager.cpp +++ b/src/util/input_manager.cpp @@ -241,15 +241,15 @@ std::optional 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); } - else if (StringUtil::StartsWith(source, "Sensor")) + else if (source.starts_with("Sensor")) { return ParseSensorKey(source, sub_binding); } @@ -420,7 +420,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 key = ParseHostKeyboardKey(source, sub_binding); const char* icon = key.has_value() ? ConvertHostKeyboardCodeToIcon(key->data) : nullptr; @@ -431,7 +431,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 key = ParsePointerKey(source, sub_binding); 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 @@ -691,7 +691,7 @@ std::optional InputManager::ParsePointerKey(const std::string_v key.source_type = InputSourceType::Pointer; key.source_index = static_cast(pointer_index.value()); - if (StringUtil::StartsWith(sub_binding, "Button")) + if (sub_binding.starts_with("Button")) { const std::optional button_number = StringUtil::FromChars(sub_binding.substr(6)); if (!button_number.has_value() || button_number.value() < 0) @@ -704,7 +704,7 @@ std::optional 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; @@ -736,7 +736,7 @@ std::optional InputManager::ParsePointerKey(const std::string_v std::optional InputManager::GetIndexFromPointerBinding(const std::string_view& source) { - if (!StringUtil::StartsWith(source, "Pointer-")) + if (!source.starts_with("Pointer-")) return std::nullopt; const std::optional pointer_index = StringUtil::FromChars(source.substr(8)); @@ -763,7 +763,7 @@ std::optional InputManager::ParseSensorKey(const std::string_vi 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.data = i; @@ -1855,14 +1855,14 @@ bool InputManager::MigrateBindings(SettingsInterface& si) Log_DevPrintf("%s -> %s", old_bind.c_str(), new_bind.c_str()); 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)); si.SetStringValue(new_section.c_str(), new_key, new_bind); Log_DevPrintf("%s -> %s", old_bind.c_str(), new_bind.c_str()); num_changes++; } - else if (StringUtil::StartsWith(old_bind.c_str(), "Keyboard/")) + else if (old_bind.starts_with("Keyboard/")) { // pass through as-is si.SetStringValue(new_section.c_str(), new_key, old_bind.c_str()); diff --git a/src/util/input_source.cpp b/src/util/input_source.cpp index 6165b35d3..b8e161f0b 100644 --- a/src/util/input_source.cpp +++ b/src/util/input_source.cpp @@ -82,7 +82,7 @@ std::optional InputSource::ParseGenericControllerKey(InputSourc 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 axis_number = StringUtil::FromChars(sub_binding.substr(5)); if (!axis_number.has_value() || axis_number.value() < 0) @@ -98,7 +98,7 @@ std::optional InputSource::ParseGenericControllerKey(InputSourc else return std::nullopt; } - else if (StringUtil::StartsWith(sub_binding, "FullAxis")) + else if (sub_binding.starts_with("FullAxis")) { const std::optional axis_number = StringUtil::FromChars(sub_binding.substr(8)); if (!axis_number.has_value() || axis_number.value() < 0) @@ -107,7 +107,7 @@ std::optional InputSource::ParseGenericControllerKey(InputSourc key.data = static_cast(axis_number.value()); key.modifier = InputModifier::FullAxis; } - else if (StringUtil::StartsWith(sub_binding, "Button")) + else if (sub_binding.starts_with("Button")) { const std::optional button_number = StringUtil::FromChars(sub_binding.substr(6)); if (!button_number.has_value() || button_number.value() < 0) diff --git a/src/util/sdl_input_source.cpp b/src/util/sdl_input_source.cpp index bdb442be6..f2792f971 100644 --- a/src/util/sdl_input_source.cpp +++ b/src/util/sdl_input_source.cpp @@ -324,7 +324,7 @@ std::vector> SDLInputSource::EnumerateDevice std::optional 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 player_id = StringUtil::FromChars(device.substr(4)); @@ -335,7 +335,7 @@ std::optional SDLInputSource::ParseKeyString(const std::string_ key.source_type = InputSourceType::SDL; key.source_index = static_cast(player_id.value()); - if (StringUtil::EndsWith(binding, "Motor")) + if (binding.ends_with("Motor")) { key.source_subtype = InputSubclass::ControllerMotor; if (binding == "LargeMotor") @@ -353,7 +353,7 @@ std::optional 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; @@ -364,7 +364,7 @@ std::optional 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(axis_name.substr(4), 10, &end)) @@ -388,7 +388,7 @@ std::optional 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(binding.substr(8), 10, &end)) @@ -400,7 +400,7 @@ std::optional 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(binding.substr(3), 10, &hat_dir); value.has_value() && !hat_dir.empty()) @@ -419,7 +419,7 @@ std::optional 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(binding.substr(6))) { @@ -584,7 +584,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 player_id = StringUtil::FromChars(device.substr(4)); @@ -893,7 +893,7 @@ std::vector SDLInputSource::EnumerateMotors() bool SDLInputSource::GetGenericBindingMapping(const std::string_view& device, GenericInputBindingMapping* mapping) { - if (!StringUtil::StartsWith(device, "SDL-")) + if (!device.starts_with("SDL-")) return false; const std::optional player_id = StringUtil::FromChars(device.substr(4)); diff --git a/src/util/xinput_source.cpp b/src/util/xinput_source.cpp index 5a96c6706..dec3fc9e5 100644 --- a/src/util/xinput_source.cpp +++ b/src/util/xinput_source.cpp @@ -253,7 +253,7 @@ std::vector> XInputSource::EnumerateDevices( std::optional 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 player_id = StringUtil::FromChars(device.substr(7)); @@ -264,7 +264,7 @@ std::optional XInputSource::ParseKeyString(const std::string_vi key.source_type = InputSourceType::XInput; key.source_index = static_cast(player_id.value()); - if (StringUtil::EndsWith(binding, "Motor")) + if (binding.ends_with("Motor")) { key.source_subtype = InputSubclass::ControllerMotor; if (binding == "LargeMotor") @@ -386,7 +386,7 @@ std::vector XInputSource::EnumerateMotors() bool XInputSource::GetGenericBindingMapping(const std::string_view& device, GenericInputBindingMapping* mapping) { - if (!StringUtil::StartsWith(device, "XInput-")) + if (!device.starts_with("XInput-")) return false; const std::optional player_id = StringUtil::FromChars(device.substr(7));