ft_memmove: initial implementation

This was tricky to visualize, consider bringing pen and paper next time
This commit is contained in:
Khaïs COLIN 2024-10-15 14:29:15 +02:00
parent fc5aa8e037
commit b86949e284
3 changed files with 51 additions and 3 deletions

46
ft_memmove.c Normal file
View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}