memory: xstrchrnul and xmemscan.

This commit is contained in:
Scott Prager
2014-09-05 04:25:34 -04:00
parent afe7ba1e71
commit eff839b26d
4 changed files with 34 additions and 10 deletions

View File

@@ -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.
///