mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-26 14:28:30 +00:00
Make memchr() and memccpy() faster
This commit is contained in:
parent
fef24d622a
commit
e4d6eb382a
3 changed files with 95 additions and 24 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue