feat: added memchr, memcmp, memmv and realloc

This commit is contained in:
Zoëy Noort 2025-05-31 19:37:00 +02:00
parent 3287cd402f
commit 0f35f48c25
4 changed files with 129 additions and 0 deletions

35
srcs/ft_memchr.c Normal file
View file

@ -0,0 +1,35 @@
#include <stddef.h>
#include <stdint.h>
void *ft_memchr(const void *s, int c, size_t n)
{
const unsigned char *p = s;
unsigned char uc = (unsigned char)c;
while (n && ((uintptr_t)p % sizeof(size_t))) {
if (*p == uc)
return ((void *)p);
++p;
--n;
}
size_t mask = 0;
for (size_t i = 0; i < sizeof(size_t); ++i)
mask |= (size_t)uc << (i * 8);
while (n >= sizeof(size_t)) {
size_t word = *(const size_t *)p;
size_t cmp = word ^ mask;
for (size_t i = 0; i < sizeof(size_t); ++i) {
if (((cmp >> (i * 8)) & 0xFF) == 0)
return ((void *)(p + i));
}
p += sizeof(size_t);
n -= sizeof(size_t);
}
while (n--) {
if (*p == uc)
return ((void *)p);
++p;
}
return (NULL);
}