From 444e65071108070a66aa3de136cddcc6fe5f5989 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 12 Mar 2022 23:56:58 +1000 Subject: [PATCH] StringUtil: Add StartsWithNoCase/EndsWithNoCase --- common/StringUtil.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/common/StringUtil.h b/common/StringUtil.h index ac10ac2e4e..1f0d0c9560 100644 --- a/common/StringUtil.h +++ b/common/StringUtil.h @@ -153,6 +153,17 @@ namespace StringUtil 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 char* prefix) + { + return (Strncasecmp(str.data(), prefix, std::strlen(prefix)) == 0); + } + static inline bool EndsWithNoCase(const std::string_view& str, const char* suffix) + { + const std::size_t suffix_length = std::strlen(suffix); + return (str.length() >= suffix_length && Strncasecmp(str.data() + (str.length() - suffix_length), suffix, suffix_length) == 0); + } + /// Strip whitespace from the start/end of the string. std::string_view StripWhitespace(const std::string_view& str);