diff options
author | Scott Prager <splinterofchaos@gmail.com> | 2014-09-05 11:49:09 -0400 |
---|---|---|
committer | Scott Prager <splinterofchaos@gmail.com> | 2014-09-30 19:35:42 -0400 |
commit | 07bfc11448e98fa6a0e3d0218da6ffb2cb997930 (patch) | |
tree | a0aecfc38b190c7349f21bc7ab8b0ebd86cfd323 | |
parent | eff839b26df8b10b2334e2fbdaf81fbb3236b873 (diff) | |
download | rneovim-07bfc11448e98fa6a0e3d0218da6ffb2cb997930.tar.gz rneovim-07bfc11448e98fa6a0e3d0218da6ffb2cb997930.tar.bz2 rneovim-07bfc11448e98fa6a0e3d0218da6ffb2cb997930.zip |
memory: memchrsub and strchrsub
-rw-r--r-- | src/nvim/memory.c | 32 |
1 files changed, 32 insertions, 0 deletions
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 <assert.h> #include <errno.h> #include <inttypes.h> #include <string.h> @@ -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. /// |