diff options
author | Max Kellermann <max@musicpd.org> | 2018-09-06 19:29:25 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2018-09-06 19:52:43 +0200 |
commit | 728e4e9a385a89b0ea38c4a0a89dbd0c123f1df3 (patch) | |
tree | 65ce70b0cb5fa791dbfc0a1463d494ec23a584a5 /src/util | |
parent | 2d6f9f9a9c985c9a2e1fc155fdaa3fe07a57440b (diff) |
util/StringCompare: add StringEndsWithIgnoreCase(), StringStartsWithIgnoreCase()
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/StringCompare.cxx | 11 | ||||
-rw-r--r-- | src/util/StringCompare.hxx | 11 | ||||
-rw-r--r-- | src/util/WStringCompare.cxx | 12 | ||||
-rw-r--r-- | src/util/WStringCompare.hxx | 13 |
4 files changed, 47 insertions, 0 deletions
diff --git a/src/util/StringCompare.cxx b/src/util/StringCompare.cxx index 22f55ea63..643edff40 100644 --- a/src/util/StringCompare.cxx +++ b/src/util/StringCompare.cxx @@ -40,6 +40,17 @@ StringEndsWith(const char *haystack, const char *needle) noexcept needle, needle_length) == 0; } +bool +StringEndsWithIgnoreCase(const char *haystack, const char *needle) noexcept +{ + const size_t haystack_length = StringLength(haystack); + const size_t needle_length = StringLength(needle); + + return haystack_length >= needle_length && + StringIsEqualIgnoreCase(haystack + haystack_length - needle_length, + needle); +} + const char * FindStringSuffix(const char *p, const char *suffix) noexcept { diff --git a/src/util/StringCompare.hxx b/src/util/StringCompare.hxx index 383943c55..3a3184cd1 100644 --- a/src/util/StringCompare.hxx +++ b/src/util/StringCompare.hxx @@ -56,6 +56,10 @@ gcc_pure gcc_nonnull_all bool StringEndsWith(const char *haystack, const char *needle) noexcept; +gcc_pure gcc_nonnull_all +bool +StringEndsWithIgnoreCase(const char *haystack, const char *needle) noexcept; + /** * Returns the portion of the string after a prefix. If the string * does not begin with the specified prefix, this function returns @@ -70,6 +74,13 @@ StringAfterPrefix(const char *haystack, StringView needle) noexcept : nullptr; } +gcc_pure gcc_nonnull_all +static inline bool +StringStartsWithIgnoreCase(const char *haystack, StringView needle) noexcept +{ + return StringIsEqualIgnoreCase(haystack, needle.data, needle.size); +} + /** * Check if the given string ends with the specified suffix. If yes, * returns the position of the suffix, and nullptr otherwise. diff --git a/src/util/WStringCompare.cxx b/src/util/WStringCompare.cxx index 18e77b77b..b255f8e64 100644 --- a/src/util/WStringCompare.cxx +++ b/src/util/WStringCompare.cxx @@ -39,6 +39,18 @@ StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept StringIsEqual(haystack + haystack_length - needle_length, needle); } +bool +StringEndsWithIgnoreCase(const wchar_t *haystack, + const wchar_t *needle) noexcept +{ + const size_t haystack_length = StringLength(haystack); + const size_t needle_length = StringLength(needle); + + return haystack_length >= needle_length && + StringIsEqualIgnoreCase(haystack + haystack_length - needle_length, + needle); +} + const wchar_t * FindStringSuffix(const wchar_t *p, const wchar_t *suffix) noexcept { diff --git a/src/util/WStringCompare.hxx b/src/util/WStringCompare.hxx index 056e490ac..99ec8ddfb 100644 --- a/src/util/WStringCompare.hxx +++ b/src/util/WStringCompare.hxx @@ -54,6 +54,11 @@ gcc_pure gcc_nonnull_all bool StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept; +gcc_pure gcc_nonnull_all +bool +StringEndsWithIgnoreCase(const wchar_t *haystack, + const wchar_t *needle) noexcept; + /** * Returns the portion of the string after a prefix. If the string * does not begin with the specified prefix, this function returns @@ -68,6 +73,14 @@ StringAfterPrefix(const wchar_t *haystack, WStringView needle) noexcept : nullptr; } +gcc_pure gcc_nonnull_all +static inline bool +StringStartsWithIgnoreCase(const wchar_t *haystack, + WStringView needle) noexcept +{ + return StringIsEqualIgnoreCase(haystack, needle.data, needle.size); +} + /** * Check if the given string ends with the specified suffix. If yes, * returns the position of the suffix, and nullptr otherwise. |