mpd/src/util/WStringCompare.cxx

44 lines
1.1 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-2-Clause
// author: Max Kellermann <max.kellermann@gmail.com>
2015-03-05 08:21:51 +01:00
#include "WStringCompare.hxx"
2015-03-05 08:21:51 +01:00
2019-08-15 17:57:20 +02:00
#include <string.h>
2015-03-05 08:21:51 +01:00
bool
StringEndsWith(const wchar_t *haystack, const wchar_t *needle) noexcept
2015-03-05 08:21:51 +01:00
{
2015-10-16 19:17:54 +02:00
const size_t haystack_length = StringLength(haystack);
const size_t needle_length = StringLength(needle);
2015-03-05 08:21:51 +01:00
2015-10-16 19:17:54 +02:00
return haystack_length >= needle_length &&
StringIsEqual(haystack + haystack_length - needle_length, needle);
2015-03-05 08:21:51 +01:00
}
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);
}
2015-03-05 08:21:51 +01:00
const wchar_t *
FindStringSuffix(const wchar_t *p, const wchar_t *suffix) noexcept
2015-03-05 08:21:51 +01:00
{
const size_t p_length = StringLength(p);
const size_t suffix_length = StringLength(suffix);
if (p_length < suffix_length)
return nullptr;
const auto *q = p + p_length - suffix_length;
return memcmp(q, suffix, suffix_length * sizeof(*suffix)) == 0
? q
: nullptr;
}