2024-10-15 14:29:15 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* ft_memmove.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2024/10/15 13:57:12 by kcolin #+# #+# */
|
2024-10-22 12:15:48 +02:00
|
|
|
/* Updated: 2024/10/22 12:15:04 by kcolin ### ########.fr */
|
2024-10-15 14:29:15 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "libft.h"
|
2024-10-22 12:15:48 +02:00
|
|
|
#include <stdlib.h>
|
2024-10-15 14:29:15 +02:00
|
|
|
|
|
|
|
|
/*
|
2024-10-15 14:44:11 +02:00
|
|
|
* case 1: dest overlaps at start of src.
|
|
|
|
|
* src is before dest.
|
2024-10-15 14:29:15 +02:00
|
|
|
* copy back to front
|
|
|
|
|
*
|
2024-10-15 14:44:11 +02:00
|
|
|
* case 2: dest overlaps at beginning of src.
|
|
|
|
|
* src is after dest.
|
2024-10-15 14:29:15 +02:00
|
|
|
* copy front to back.
|
|
|
|
|
*
|
2024-10-15 14:44:11 +02:00
|
|
|
* case 3: dest and src do not overlap
|
2024-10-15 14:29:15 +02:00
|
|
|
* it doesn't matter how the data is copied
|
|
|
|
|
* merge with case 2
|
|
|
|
|
*/
|
2024-10-16 15:20:28 +02:00
|
|
|
void *ft_memmove(void *dest, const void *src, size_t n)
|
2024-10-15 14:29:15 +02:00
|
|
|
{
|
2024-10-16 15:20:28 +02:00
|
|
|
size_t i;
|
2024-10-15 14:29:15 +02:00
|
|
|
|
2024-10-23 11:39:34 +02:00
|
|
|
if ((dest == NULL && src == NULL) && n > 0)
|
2024-10-22 12:15:48 +02:00
|
|
|
return (dest);
|
2024-10-15 14:44:11 +02:00
|
|
|
if (dest >= src && dest <= (src + n))
|
2024-10-15 14:29:15 +02:00
|
|
|
{
|
2024-10-15 14:44:11 +02:00
|
|
|
i = n;
|
2024-10-15 14:29:15 +02:00
|
|
|
while (i > 0)
|
|
|
|
|
{
|
|
|
|
|
i--;
|
2024-10-15 14:44:11 +02:00
|
|
|
((char *)dest)[i] = ((char *)src)[i];
|
2024-10-15 14:29:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-10-15 14:44:11 +02:00
|
|
|
ft_memcpy(dest, src, n);
|
|
|
|
|
return (dest);
|
2024-10-15 14:29:15 +02:00
|
|
|
}
|
2024-10-22 12:15:48 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#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));
|
|
|
|
|
}
|
|
|
|
|
*/
|