libft/ft_memmove.c
Khaïs COLIN b86949e284 ft_memmove: initial implementation
This was tricky to visualize, consider bringing pen and paper next time
2024-10-15 14:29:15 +02:00

46 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:57:12 by kcolin #+# #+# */
/* Updated: 2024/10/15 14:12:15 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* case 1: dst overlaps at start of src.
* src is before dst.
* copy back to front
*
* case 2: dst overlaps at beginning of src.
* src is after dst.
* copy front to back.
*
* case 3: dst and src do not overlap
* it doesn't matter how the data is copied
* merge with case 2
*/
void *ft_memmove(void *dst, const void *src, t_size len)
{
t_size i;
if (dst >= src && dst <= (src + len))
{
i = len;
while (i > 0)
{
i--;
((char *)dst)[i] = ((char *)src)[i];
}
}
else
{
ft_memcpy(dst, src, len);
}
return (dst);
}