Commit 0b956cf9 authored by Max Kellermann's avatar Max Kellermann

util/StringAPI: add memrchr() wrapper

parent 2c3eb5b8
...@@ -96,6 +96,27 @@ StringFindLast(char *haystack, char needle) noexcept ...@@ -96,6 +96,27 @@ StringFindLast(char *haystack, char needle) noexcept
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
static inline const char * static inline const char *
StringFindLast(const char *haystack, char needle, size_t size) noexcept
{
#if defined(__GLIBC__) || defined(__BIONIC__)
/* memrchr() is a GNU extension (and also available on
Android) */
return (const char *)memrchr(haystack, needle, size);
#else
/* emulate for everybody else */
const auto *p = haystack + size;
while (p > haystack) {
--p;
if (*p == needle)
return p;
}
return nullptr;
#endif
}
gcc_pure gcc_nonnull_all
static inline const char *
StringFindAny(const char *haystack, const char *accept) noexcept StringFindAny(const char *haystack, const char *accept) noexcept
{ {
return strpbrk(haystack, accept); return strpbrk(haystack, accept);
......
...@@ -94,6 +94,11 @@ struct BasicStringView : ConstBuffer<T> { ...@@ -94,6 +94,11 @@ struct BasicStringView : ConstBuffer<T> {
return StringFind(data, ch, this->size); return StringFind(data, ch, this->size);
} }
gcc_pure
pointer_type FindLast(value_type ch) const noexcept {
return StringFindLast(data, ch, size);
}
/** /**
* Split the string at the first occurrence of the given * Split the string at the first occurrence of the given
* character. If the character is not found, then the first * character. If the character is not found, then the first
......
...@@ -92,6 +92,21 @@ StringFindLast(wchar_t *haystack, wchar_t needle) noexcept ...@@ -92,6 +92,21 @@ StringFindLast(wchar_t *haystack, wchar_t needle) noexcept
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
static inline const wchar_t * static inline const wchar_t *
StringFindLast(const wchar_t *haystack, wchar_t needle, size_t size) noexcept
{
/* there's no wmemrchr() unfortunately */
const auto *p = haystack + size;
while (p > haystack) {
--p;
if (*p == needle)
return p;
}
return nullptr;
}
gcc_pure gcc_nonnull_all
static inline const wchar_t *
StringFindAny(const wchar_t *haystack, const wchar_t *accept) noexcept StringFindAny(const wchar_t *haystack, const wchar_t *accept) noexcept
{ {
return wcspbrk(haystack, accept); return wcspbrk(haystack, accept);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment