From eff839b26df8b10b2334e2fbdaf81fbb3236b873 Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Fri, 5 Sep 2014 04:25:34 -0400 Subject: memory: xstrchrnul and xmemscan. --- src/nvim/memory.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/nvim/memory.c') 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. /// -- cgit From 07bfc11448e98fa6a0e3d0218da6ffb2cb997930 Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Fri, 5 Sep 2014 11:49:09 -0400 Subject: memory: memchrsub and strchrsub --- src/nvim/memory.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/nvim/memory.c') diff --git a/src/nvim/memory.c b/src/nvim/memory.c index 3f9e8f1ada..59edefec4a 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -1,5 +1,6 @@ // Various routines dealing with allocation and deallocation of memory. +#include #include #include #include @@ -251,6 +252,37 @@ void *xmemscan(const void *addr, char c, size_t size) return p ? p : (char *)addr + size; } +/// Replaces every instance of `c` with `x`. +/// +/// @warning Will read past `str + strlen(str)` if `c == NUL`. +/// +/// @param str A NUL-terminated string. +/// @param c The unwanted byte. +/// @param x The replacement. +void strchrsub(char *str, char c, char x) + FUNC_ATTR_NONNULL_ALL +{ + assert(c != '\0'); + while ((str = strchr(str, c))) { + *str++ = x; + } +} + +/// Replaces every instance of `c` with `x`. +/// +/// @param data An object in memory. May contain NULs. +/// @param c The unwanted byte. +/// @param x The replacement. +/// @param len The length of data. +void memchrsub(void *data, char c, char x, size_t len) + FUNC_ATTR_NONNULL_ALL +{ + char *p = data, *end = (char *)data + len; + while ((p = memchr(p, c, (size_t)(end - p)))) { + *p++ = x; + } +} + /// The xstpcpy() function shall copy the string pointed to by src (including /// the terminating NUL character) into the array pointed to by dst. /// -- cgit