aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/memory.c')
-rw-r--r--src/nvim/memory.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/nvim/memory.c b/src/nvim/memory.c
index f4dffb0bcd..3f9e8f1ada 100644
--- a/src/nvim/memory.c
+++ b/src/nvim/memory.c
@@ -222,6 +222,35 @@ void *xmemdupz(const void *data, size_t len)
return memcpy(xmallocz(len), data, len);
}
+/// A version of strchr() that returns a pointer to the terminating NUL if it
+/// doesn't find `c`.
+///
+/// @param str The string to search.
+/// @param c The char to look for.
+/// @returns a pointer to the first instance of `c`, or to the NUL terminator
+/// if not found.
+char *xstrchrnul(const char *str, char c)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
+{
+ char *p = strchr(str, c);
+ return p ? p : (char *)(str + strlen(str));
+}
+
+/// A version of memchr() that returns a pointer one past the end
+/// if it doesn't find `c`.
+///
+/// @param addr The address of the memory object.
+/// @param c The char to look for.
+/// @param size The size of the memory object.
+/// @returns a pointer to the first instance of `c`, or one past the end if not
+/// found.
+void *xmemscan(const void *addr, char c, size_t size)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
+{
+ char *p = memchr(addr, c, size);
+ return p ? p : (char *)addr + size;
+}
+
/// The xstpcpy() function shall copy the string pointed to by src (including
/// the terminating NUL character) into the array pointed to by dst.
///