-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_strlen.c
101 lines (91 loc) · 2.39 KB
/
ft_strlen.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadhesiv <oadhesiv@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/04 19:11:17 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 char **b)
{
t_byte *dest;
dest = (t_byte*)*b;
while ((t_ulong)dest % sizeof(long) != 0)
{
if (*dest == 0)
return (dest);
dest++;
}
*b = (void*)dest;
return ((void *)0);
}
static void *check_bytes(t_byte *data)
{
if (*(data + 0) == 0)
return ((void *)(data + 0));
if (*(data + 1) == 0)
return ((void *)(data + 1));
if (*(data + 2) == 0)
return ((void *)(data + 2));
if (*(data + 3) == 0)
return ((void *)(data + 3));
if (*(data + 4) == 0)
return ((void *)(data + 4));
if (*(data + 5) == 0)
return ((void *)(data + 5));
if (*(data + 6) == 0)
return ((void *)(data + 6));
if (*(data + 7) == 0)
return ((void *)(data + 7));
return ((void *)0);
}
static void *check_ulong(t_ulong *data)
{
t_ulong cell_one;
t_ulong cell_eighties;
void *ptr;
cell_one = make_memory_cell(0x01);
cell_eighties = make_memory_cell(0x80);
if (((*data - cell_one) & ~*data & cell_eighties) != 0)
{
ptr = check_bytes((t_byte *)data);
if (ptr)
return (ptr);
}
return ((void *)0);
}
size_t ft_strlen(const char *s)
{
void *ret;
void *save;
t_ulong *str_ulong;
if (!s)
return (0);
save = (void *)s;
ret = align_pointer(&s);
if (ret)
return (ret - save);
str_ulong = (t_ulong *)s;
while (1)
{
ret = check_ulong(str_ulong);
if (ret)
return (ret - save);
str_ulong++;
}
return (0);
}