libft/srcs/ft_memcmp.c

28 lines
668 B
C

#include <stddef.h>
#include <stdint.h>
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *p1 = s1, *p2 = s2;
while (n >= sizeof(size_t)) {
size_t w1 = *(const size_t *)p1;
size_t w2 = *(const size_t *)p2;
if (w1 != w2) {
for (size_t i = 0; i < sizeof(size_t); ++i) {
if (p1[i] != p2[i])
return (p1[i] - p2[i]);
}
}
p1 += sizeof(size_t);
p2 += sizeof(size_t);
n -= sizeof(size_t);
}
while (n--) {
if (*p1 != *p2)
return (*p1 - *p2);
++p1;
++p2;
}
return (0);
}