util/StringAPI: add memrchr() wrapper

This commit is contained in:
Max Kellermann
2019-09-07 23:59:59 +02:00
parent 2c3eb5b8ad
commit 0b956cf968
3 changed files with 41 additions and 0 deletions

View File

@@ -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