util/StringAPI: add memrchr() wrapper
This commit is contained in:
parent
2c3eb5b8ad
commit
0b956cf968
@ -94,6 +94,27 @@ StringFindLast(char *haystack, char needle) noexcept
|
||||
return strrchr(haystack, needle);
|
||||
}
|
||||
|
||||
gcc_pure gcc_nonnull_all
|
||||
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
|
||||
|
@ -94,6 +94,11 @@ struct BasicStringView : ConstBuffer<T> {
|
||||
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
|
||||
* character. If the character is not found, then the first
|
||||
|
@ -90,6 +90,21 @@ StringFindLast(wchar_t *haystack, wchar_t needle) noexcept
|
||||
return wcsrchr(haystack, needle);
|
||||
}
|
||||
|
||||
gcc_pure gcc_nonnull_all
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user