diff --git a/src/common/string_util.h b/src/common/string_util.h index 7c2af92ec..f928f24db 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -1,4 +1,5 @@ #pragma once +#include "types.h" #include #include #include @@ -24,7 +25,7 @@ bool WildcardMatch(const char* subject, const char* mask, bool case_sensitive = std::size_t Strlcpy(char* dst, const char* src, std::size_t size); /// Platform-independent strcasecmp -inline int Strcasecmp(const char* s1, const char* s2) +static inline int Strcasecmp(const char* s1, const char* s2) { #ifdef _MSC_VER return _stricmp(s1, s2); @@ -35,7 +36,7 @@ inline int Strcasecmp(const char* s1, const char* s2) /// Wrapper arond std::from_chars template -std::optional FromChars(const std::string_view str) +static inline std::optional FromChars(std::string_view str) { T value; @@ -54,4 +55,10 @@ std::optional FromChars(const std::string_view str) return value; } +/// starts_with from C++20 +ALWAYS_INLINE static bool StartsWith(std::string_view str, const char* prefix) +{ + return (str.compare(0, std::strlen(prefix), prefix) == 0); +} + } // namespace StringUtil diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index ee4a5d6b0..1c097996e 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -214,7 +214,7 @@ bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const } #ifdef WITH_SDL2 - if (device == "Controller") + if (StringUtil::StartsWith(device, "Controller")) { const std::optional controller_index = StringUtil::FromChars(device.substr(10)); if (!controller_index || *controller_index < 0) @@ -223,7 +223,7 @@ bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const return false; } - if (button.find_first_of("Button") == 0) + if (StringUtil::StartsWith(button, "Button")) { const std::optional button_index = StringUtil::FromChars(button.substr(6)); if (!button_index || @@ -235,7 +235,7 @@ bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const return true; } - else if (button.find_first_of("+Axis") == 0 || button.find_first_of("-Axis")) + else if (StringUtil::StartsWith(button, "+Axis") || StringUtil::StartsWith(button, "-Axis")) { const std::optional axis_index = StringUtil::FromChars(button.substr(5)); const bool positive = (button[0] == '+'); @@ -262,7 +262,7 @@ bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const st const std::string_view& axis, InputAxisHandler handler) { #ifdef WITH_SDL2 - if (device == "Controller") + if (StringUtil::StartsWith(device, "Controller")) { const std::optional controller_index = StringUtil::FromChars(device.substr(10)); if (!controller_index || *controller_index < 0) @@ -271,7 +271,7 @@ bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const st return false; } - if (axis.find_first_of("Axis") == 0) + if (StringUtil::StartsWith(axis, "Axis")) { const std::optional axis_index = StringUtil::FromChars(axis.substr(4)); if (!axis_index ||