From 4df884a6113c8ce685dddb44628a936188532888 Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Sat, 27 Sep 2014 21:00:19 -0400 Subject: memory: implement xmemrchr() --- src/nvim/memory.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') diff --git a/src/nvim/memory.c b/src/nvim/memory.c index 59edefec4a..4b24213ecd 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -389,6 +389,25 @@ char *xstrdup(const char *str) return ret; } +/// A version of memchr that starts the search at `src + len`. +/// +/// Based on glibc's memrchr. +/// +/// @param src The source memory object. +/// @param c The byte to search for. +/// @param len The length of the memory object. +/// @returns a pointer to the found byte in src[len], or NULL. +void *xmemrchr(void *src, uint8_t c, size_t len) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE +{ + while (len--) { + if (((uint8_t *)src)[len] == c) { + return (uint8_t *) src + len; + } + } + return NULL; +} + /// strndup() wrapper /// /// @see {xmalloc} -- cgit