-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_memccpy.c
62 lines (58 loc) · 2.15 KB
/
ft_memccpy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memccpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abaudot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/05 15:05:50 by abaudot #+# #+# */
/* Updated: 2021/01/05 15:05:54 by abaudot ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static inline void *find(void *dest, const void *src, t_byte c)
{
if ((((t_byte *)dest)[0] = ((t_byte *)src)[0]) == c)
return ((t_byte *)dest + 1);
if ((((t_byte *)dest)[1] = ((t_byte *)src)[1]) == c)
return ((t_byte *)dest + 2);
if ((((t_byte *)dest)[2] = ((t_byte *)src)[2]) == c)
return ((t_byte *)dest + 3);
if ((((t_byte *)dest)[3] = ((t_byte *)src)[3]) == c)
return ((t_byte *)dest + 4);
if ((((t_byte *)dest)[4] = ((t_byte *)src)[4]) == c)
return ((t_byte *)dest + 5);
if ((((t_byte *)dest)[5] = ((t_byte *)src)[5]) == c)
return ((t_byte *)dest + 6);
if ((((t_byte *)dest)[6] = ((t_byte *)src)[6]) == c)
return ((t_byte *)dest + 7);
if ((((t_byte *)dest)[7] = ((t_byte *)src)[7]) == c)
return ((t_byte *)dest + 8);
return (dest);
}
void *ft_memccpy(void *dest, const void *src, int c, size_t n)
{
t_op cc;
t_op lw;
c = (t_byte)c;
while (n % OPSIZ && n--)
if ((*(t_byte *)dest++ = *(t_byte *)src++) == c)
return ((t_op *)dest);
cc = c * LOMAGIC;
while (n)
{
lw = *(t_op *)src ^ cc;
if ((lw - LOMAGIC) & ~lw & HIMAGIC)
{
dest = find(dest, src, (t_byte)c);
if (*(t_byte *)(dest - 1) == (t_byte)c)
return ((t_op *)dest);
}
else
*(t_op *)dest = *(t_op *)src;
n -= OPSIZ;
dest += OPSIZ;
src += OPSIZ;
}
return (NULL);
}