2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2015-11-06 09:09:02 +01:00
|
|
|
|
|
|
|
#ifndef STRING_COMPARE_HXX
|
|
|
|
#define STRING_COMPARE_HXX
|
|
|
|
|
2018-08-20 15:33:16 +02:00
|
|
|
#include "StringAPI.hxx"
|
2015-11-06 09:09:02 +01:00
|
|
|
|
|
|
|
#ifdef _UNICODE
|
|
|
|
#include "WStringCompare.hxx"
|
|
|
|
#endif
|
|
|
|
|
2020-03-22 19:12:02 +01:00
|
|
|
#include <string_view>
|
|
|
|
|
2022-11-17 17:16:08 +01:00
|
|
|
[[gnu::nonnull]]
|
|
|
|
static constexpr bool
|
2017-05-16 23:02:36 +02:00
|
|
|
StringIsEmpty(const char *string) noexcept
|
2015-11-06 09:37:07 +01:00
|
|
|
{
|
2017-05-16 23:01:43 +02:00
|
|
|
return *string == 0;
|
2015-11-06 09:37:07 +01:00
|
|
|
}
|
|
|
|
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]]
|
2020-03-13 20:36:48 +01:00
|
|
|
static inline bool
|
|
|
|
StringIsEqual(std::string_view a, std::string_view b) noexcept
|
|
|
|
{
|
|
|
|
return a.size() == b.size() &&
|
|
|
|
StringIsEqual(a.data(), b.data(), b.size());
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]]
|
2020-03-13 20:36:48 +01:00
|
|
|
static inline bool
|
|
|
|
StringIsEqualIgnoreCase(std::string_view a, std::string_view b) noexcept
|
|
|
|
{
|
|
|
|
return a.size() == b.size() &&
|
|
|
|
StringIsEqualIgnoreCase(a.data(), b.data(), b.size());
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]] [[gnu::nonnull]]
|
2015-11-06 09:30:54 +01:00
|
|
|
static inline bool
|
2022-05-31 13:18:42 +02:00
|
|
|
StringStartsWith(const char *haystack, std::string_view needle) noexcept
|
2015-11-06 09:30:54 +01:00
|
|
|
{
|
2022-05-31 13:18:42 +02:00
|
|
|
return StringIsEqual(haystack, needle.data(), needle.size());
|
2015-11-06 09:30:54 +01:00
|
|
|
}
|
2015-11-06 09:09:02 +01:00
|
|
|
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]] [[gnu::nonnull]]
|
2015-11-06 09:09:02 +01:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
StringEndsWith(const char *haystack, const char *needle) noexcept;
|
2015-11-06 09:09:02 +01:00
|
|
|
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]] [[gnu::nonnull]]
|
2018-09-06 19:29:25 +02:00
|
|
|
bool
|
|
|
|
StringEndsWithIgnoreCase(const char *haystack, const char *needle) noexcept;
|
|
|
|
|
2015-11-06 09:09:02 +01:00
|
|
|
/**
|
|
|
|
* Returns the portion of the string after a prefix. If the string
|
|
|
|
* does not begin with the specified prefix, this function returns
|
|
|
|
* nullptr.
|
|
|
|
*/
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]] [[gnu::nonnull]]
|
2015-11-06 09:30:54 +01:00
|
|
|
static inline const char *
|
2022-05-31 13:18:42 +02:00
|
|
|
StringAfterPrefix(const char *haystack, std::string_view needle) noexcept
|
2015-11-06 09:30:54 +01:00
|
|
|
{
|
|
|
|
return StringStartsWith(haystack, needle)
|
2022-05-31 13:18:42 +02:00
|
|
|
? haystack + needle.size()
|
2015-11-06 09:30:54 +01:00
|
|
|
: nullptr;
|
|
|
|
}
|
2015-11-06 09:09:02 +01:00
|
|
|
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]] [[gnu::nonnull]]
|
2018-09-06 19:29:25 +02:00
|
|
|
static inline bool
|
2022-05-31 13:18:42 +02:00
|
|
|
StringStartsWithIgnoreCase(const char *haystack, std::string_view needle) noexcept
|
2018-09-06 19:29:25 +02:00
|
|
|
{
|
2022-05-31 13:18:42 +02:00
|
|
|
return StringIsEqualIgnoreCase(haystack, needle.data(), needle.size());
|
2018-09-06 19:29:25 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]]
|
2020-03-13 18:47:00 +01:00
|
|
|
static inline bool
|
2022-05-31 13:18:42 +02:00
|
|
|
StringStartsWithIgnoreCase(std::string_view haystack, std::string_view needle) noexcept
|
2020-03-13 18:47:00 +01:00
|
|
|
{
|
2022-05-31 13:18:42 +02:00
|
|
|
return haystack.size() >= needle.size() &&
|
|
|
|
StringIsEqualIgnoreCase(haystack.data(),
|
|
|
|
needle.data(), needle.size());
|
2020-03-13 18:47:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-22 19:15:55 +01:00
|
|
|
/**
|
|
|
|
* Returns the portion of the string after a prefix. If the string
|
|
|
|
* does not begin with the specified prefix, this function returns
|
|
|
|
* nullptr.
|
|
|
|
* This function is case-independent.
|
|
|
|
*/
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]] [[gnu::nonnull]]
|
2018-10-29 14:21:58 +01:00
|
|
|
static inline const char *
|
2022-05-31 13:18:42 +02:00
|
|
|
StringAfterPrefixIgnoreCase(const char *haystack, std::string_view needle) noexcept
|
2018-10-29 14:21:58 +01:00
|
|
|
{
|
|
|
|
return StringStartsWithIgnoreCase(haystack, needle)
|
2022-05-31 13:18:42 +02:00
|
|
|
? haystack + needle.size()
|
2018-10-29 14:21:58 +01:00
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]]
|
2022-05-31 13:18:42 +02:00
|
|
|
static inline std::string_view
|
|
|
|
StringAfterPrefixIgnoreCase(std::string_view haystack,
|
|
|
|
std::string_view needle) noexcept
|
2020-03-13 18:47:00 +01:00
|
|
|
{
|
|
|
|
return StringStartsWithIgnoreCase(haystack, needle)
|
2022-05-31 13:18:42 +02:00
|
|
|
? haystack.substr(needle.size())
|
|
|
|
: std::string_view{};
|
2020-03-13 18:47:00 +01:00
|
|
|
}
|
|
|
|
|
2015-11-06 09:09:02 +01:00
|
|
|
/**
|
|
|
|
* Check if the given string ends with the specified suffix. If yes,
|
|
|
|
* returns the position of the suffix, and nullptr otherwise.
|
|
|
|
*/
|
2021-07-22 13:35:36 +02:00
|
|
|
[[gnu::pure]] [[gnu::nonnull]]
|
2015-11-06 09:09:02 +01:00
|
|
|
const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
FindStringSuffix(const char *p, const char *suffix) noexcept;
|
2015-11-06 09:09:02 +01:00
|
|
|
|
2022-06-30 21:07:35 +02:00
|
|
|
template<typename T>
|
|
|
|
bool
|
|
|
|
SkipPrefix(std::basic_string_view<T> &haystack,
|
|
|
|
std::basic_string_view<T> needle) noexcept
|
|
|
|
{
|
|
|
|
bool match = haystack.starts_with(needle);
|
|
|
|
if (match)
|
|
|
|
haystack.remove_prefix(needle.size());
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool
|
|
|
|
RemoveSuffix(std::basic_string_view<T> &haystack,
|
|
|
|
std::basic_string_view<T> needle) noexcept
|
|
|
|
{
|
|
|
|
bool match = haystack.ends_with(needle);
|
|
|
|
if (match)
|
|
|
|
haystack.remove_suffix(needle.size());
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2015-11-06 09:09:02 +01:00
|
|
|
#endif
|