mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
memory: memchrsub and strchrsub
This commit is contained in:
parent
eff839b26d
commit
07bfc11448
@ -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.
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user