libft/ft_memmove.c
2024-10-22 12:15:48 +02:00

73 lines
2.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:57:12 by kcolin #+# #+# */
/* Updated: 2024/10/22 12:15:04 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
/*
* case 1: dest overlaps at start of src.
* src is before dest.
* copy back to front
*
* case 2: dest overlaps at beginning of src.
* src is after dest.
* copy front to back.
*
* case 3: dest and src do not overlap
* it doesn't matter how the data is copied
* merge with case 2
*/
void *ft_memmove(void *dest, const void *src, size_t n)
{
size_t i;
if ((dest == NULL || src == NULL) && n > 0)
return (dest);
if (dest >= src && dest <= (src + n))
{
i = n;
while (i > 0)
{
i--;
((char *)dest)[i] = ((char *)src)[i];
}
}
else
ft_memcpy(dest, src, n);
return (dest);
}
/*
#include <string.h>
#include <stdio.h>
#include "libft.h"
int main(void)
{
char buf[27] = "abcdefghijklmnopqrstuvwxyz";
char buf2[27] = "abcdefghijklmnopqrstuvwxyz";
printf("%p\n", ft_memmove(buf, buf+3, 6));
printf("%s\n", buf);
printf("%p\n", ft_memmove(buf+10, buf+8, 6));
printf("%s\n", buf);
printf("%p\n", ft_memmove(buf, buf+6, 6));
printf("%s\n", buf);
printf("%p\n", memmove(buf2, buf2+3, 6));
printf("%s\n", buf2);
printf("%p\n", memmove(buf2+10, buf2+8, 6));
printf("%s\n", buf2);
printf("%p\n", memmove(buf2, buf2+6, 6));
printf("%s\n", buf2);
printf("%p\n", ft_memmove(NULL, NULL, 6));
}
*/