Make memchr() and memccpy() faster

This commit is contained in:
Justine Tunney 2024-09-30 05:54:34 -07:00
parent fef24d622a
commit e4d6eb382a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
3 changed files with 95 additions and 24 deletions

View file

@ -45,13 +45,14 @@
* @asyncsignalsafe
*/
void *memccpy(void *dst, const void *src, int c, size_t n) {
char *d;
size_t i;
const char *s;
for (d = dst, s = src, i = 0; i < n; ++i) {
if (((d[i] = s[i]) & 255) == (c & 255)) {
return d + i + 1;
}
const char *p;
// this memchr() call is only correct if your memchr() implementation
// offers the same readahead safety guarantees as cosmopolitan's does
if ((p = memchr(src, c, n))) {
size_t m = p + 1 - (const char *)src;
memmove(dst, src, m);
return (char *)dst + m;
}
memmove(dst, src, n);
return 0;
}