feat: added memchr, memcmp, memmv and realloc
This commit is contained in:
parent
3287cd402f
commit
0f35f48c25
4 changed files with 129 additions and 0 deletions
35
srcs/ft_memchr.c
Normal file
35
srcs/ft_memchr.c
Normal 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);
|
||||
}
|
||||
28
srcs/ft_memcmp.c
Normal file
28
srcs/ft_memcmp.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#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);
|
||||
}
|
||||
44
srcs/ft_memmv.c
Normal file
44
srcs/ft_memmv.c
Normal 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);
|
||||
}
|
||||
22
srcs/ft_realloc.c
Normal file
22
srcs/ft_realloc.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include "libft.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void *ft_realloc(void *ptr, size_t old_size, size_t new_size)
|
||||
{
|
||||
if (new_size == 0) {
|
||||
free(ptr);
|
||||
return (NULL);
|
||||
}
|
||||
if (!ptr)
|
||||
return (malloc(new_size));
|
||||
|
||||
void *new_ptr = malloc(new_size);
|
||||
if (!new_ptr)
|
||||
return (NULL);
|
||||
|
||||
size_t copy_size = old_size < new_size ? old_size : new_size;
|
||||
ft_memcpy(new_ptr, ptr, copy_size);
|
||||
free(ptr);
|
||||
return (new_ptr);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue