util/StringCompare: add StringEndsWithIgnoreCase(), StringStartsWithIgnoreCase()

This commit is contained in:
Max Kellermann 2018-09-06 19:29:25 +02:00
parent 2d6f9f9a9c
commit 728e4e9a38
4 changed files with 47 additions and 0 deletions

View File

@ -40,6 +40,17 @@ StringEndsWith(const char *haystack, const char *needle) noexcept
needle, needle_length) == 0; 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 * const char *
FindStringSuffix(const char *p, const char *suffix) noexcept FindStringSuffix(const char *p, const char *suffix) noexcept
{ {

View File

@ -56,6 +56,10 @@ gcc_pure gcc_nonnull_all
bool bool
StringEndsWith(const char *haystack, const char *needle) noexcept; 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 * Returns the portion of the string after a prefix. If the string
* does not begin with the specified prefix, this function returns * does not begin with the specified prefix, this function returns
@ -70,6 +74,13 @@ StringAfterPrefix(const char *haystack, StringView needle) noexcept
: nullptr; : 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, * Check if the given string ends with the specified suffix. If yes,
* returns the position of the suffix, and nullptr otherwise. * returns the position of the suffix, and nullptr otherwise.

View File

@ -39,6 +39,18 @@ StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept
StringIsEqual(haystack + haystack_length - needle_length, needle); 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 * const wchar_t *
FindStringSuffix(const wchar_t *p, const wchar_t *suffix) noexcept FindStringSuffix(const wchar_t *p, const wchar_t *suffix) noexcept
{ {

View File

@ -54,6 +54,11 @@ gcc_pure gcc_nonnull_all
bool bool
StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept; 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 * Returns the portion of the string after a prefix. If the string
* does not begin with the specified prefix, this function returns * does not begin with the specified prefix, this function returns
@ -68,6 +73,14 @@ StringAfterPrefix(const wchar_t *haystack, WStringView needle) noexcept
: nullptr; : 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, * Check if the given string ends with the specified suffix. If yes,
* returns the position of the suffix, and nullptr otherwise. * returns the position of the suffix, and nullptr otherwise.