mirror of
https://github.com/neovim/neovim.git
synced 2025-11-29 13:40:38 +00:00
memory: memchrsub and strchrsub
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
// Various routines dealing with allocation and deallocation of memory.
|
// Various routines dealing with allocation and deallocation of memory.
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -251,6 +252,37 @@ void *xmemscan(const void *addr, char c, size_t size)
|
|||||||
return p ? p : (char *)addr + 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 xstpcpy() function shall copy the string pointed to by src (including
|
||||||
/// the terminating NUL character) into the array pointed to by dst.
|
/// the terminating NUL character) into the array pointed to by dst.
|
||||||
///
|
///
|
||||||
|
|||||||
Reference in New Issue
Block a user