-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_memchr.c
110 lines (100 loc) · 2.79 KB
/
ft_memchr.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadhesiv <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/04 18:56:22 by oadhesiv #+# #+# */
/* Updated: 2021/01/06 18:00:00 by oadhesiv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static t_ulong make_memory_cell(int c)
{
t_ulong ret;
t_byte i;
ret = (t_byte)c;
i = 1;
while (i < sizeof(long))
ret |= ret << 8 * i++;
return (ret);
}
static void *align_pointer(const void **b, t_byte c, size_t *len)
{
t_byte *dest;
dest = (t_byte*)*b;
while ((t_ulong)dest % sizeof(long) != 0 && *len)
{
if (*dest == c)
return (dest);
dest++;
*len -= 1;
}
*b = (void*)dest;
return ((void *)0);
}
static void *check_bytes(t_byte *data, t_byte c, t_byte len)
{
if (len > 0 && *(data + 0) == c)
return ((void *)(data + 0));
if (len > 1 && *(data + 1) == c)
return ((void *)(data + 1));
if (len > 2 && *(data + 2) == c)
return ((void *)(data + 2));
if (len > 3 && *(data + 3) == c)
return ((void *)(data + 3));
if (len > 4 && *(data + 4) == c)
return ((void *)(data + 4));
if (len > 5 && *(data + 5) == c)
return ((void *)(data + 5));
if (len > 6 && *(data + 6) == c)
return ((void *)(data + 6));
if (len > 7 && *(data + 7) == c)
return ((void *)(data + 7));
return ((void *)0);
}
static void *check_ulong(t_ulong *data, t_byte c)
{
t_ulong cell;
t_ulong cell_one;
t_ulong cell_eighties;
t_ulong temp;
void *ptr;
cell = make_memory_cell(c);
cell_one = make_memory_cell(0x01);
cell_eighties = make_memory_cell(0x80);
temp = *data ^ cell;
if (((temp - cell_one) & ~temp & cell_eighties) != 0)
{
ptr = check_bytes((t_byte *)data, c, 8);
if (ptr)
return (ptr);
}
return ((void *)0);
}
void *ft_memchr(const void *s, int c, size_t n)
{
void *ret;
t_ulong *str_ulong;
t_byte c_byte;
if (!s || !n)
return ((void *)0);
c_byte = (t_byte)c;
ret = align_pointer(&s, c_byte, &n);
if (ret)
return (ret);
str_ulong = (t_ulong *)s;
while (n > sizeof(long))
{
ret = check_ulong(str_ulong, c_byte);
if (ret)
return (ret);
str_ulong++;
n -= sizeof(long);
}
ret = check_bytes((t_byte *)str_ulong, c_byte, n);
if (ret)
return (ret);
return ((void *)0);
}