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

44
srcs/ft_memmv.c Normal file
View file

@ -0,0 +1,44 @@
#include <stddef.h>
#include <stdint.h>
void *ft_memmove(void *dst, const void *src, size_t n)
{
unsigned char *d = dst;
const unsigned char *s = src;
if (d == s || n == 0)
return dst;
if (d < s) {
while (((uintptr_t)d % sizeof(size_t)) && n) {
*d++ = *s++;
n--;
}
while (n >= sizeof(size_t)) {
*(size_t *)d = *(const size_t *)s;
d += sizeof(size_t);
s += sizeof(size_t);
n -= sizeof(size_t);
}
while (n--) {
*d++ = *s++;
}
} else {
d += n;
s += n;
while (n && ((uintptr_t)d % sizeof(size_t))) {
*(--d) = *(--s);
n--;
}
while (n >= sizeof(size_t)) {
d -= sizeof(size_t);
s -= sizeof(size_t);
*(size_t *)d = *(const size_t *)s;
n -= sizeof(size_t);
}
while (n--) {
*(--d) = *(--s);
}
}
return (dst);
}