-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
112 lines (104 loc) · 3.35 KB
/
ft_atoi.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
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 09:13:00 by jbadaire #+# #+# */
/* Updated: 2023/11/24 15:38:51 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/24 14:31:47 by jbadaire #+# #+# */
/* Updated: 2023/01/17 17:11:31 by jbadaire ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <limits.h>
int ft_atoi(const char *nptr)
{
int index;
int result;
int is_pos;
index = 0;
is_pos = 1;
result = 0;
while (ft_is_space(nptr[index]))
index++;
while (ft_is_sign(nptr[index]))
{
if (index > 0 && ft_is_sign(nptr[index -1]))
return (0);
if (nptr[index++] == '-')
is_pos = 0;
}
while (ft_isdigit(nptr[index]))
{
if (result != ((result * 10 + (is_pos * (nptr[index] - '0'))) / 10))
return (((is_pos + 1) / 2 / -1));
result = result * 10 + (nptr[index++] - 48);
}
if (!is_pos)
result = 0 - result;
return (result);
}
long ft_atoi_long(const char *nptr)
{
int index;
int result;
int is_pos;
index = 0;
is_pos = 1;
result = 0;
while (ft_is_space(nptr[index]))
index++;
while (ft_is_sign(nptr[index]))
{
if (index > 0 && ft_is_sign(nptr[index -1]))
return (0);
if (nptr[index++] == '-')
is_pos = 0;
}
while (ft_isdigit(nptr[index]))
{
if (result != ((result * 10 + (is_pos * (nptr[index] - '0'))) / 10))
return (((is_pos + 1) / 2 / -1));
result = result * 10 + (nptr[index++] - 48);
}
if (!is_pos)
result = 0 - result;
return (result);
}
long long ft_ll_overflow_atoi(const char *str)
{
int sign;
long long value;
int i;
sign = 1;
value = 0;
i = 0;
while (ft_is_space(str[i]))
i++;
if (ft_is_sign(str[i]))
sign = 1 - 2 * (str[i++] == '-');
while (str[i] && ft_isdigit(str[i]))
{
if (value > LLONG_MAX / 10 || \
(value == LLONG_MAX / 10 && str[i] - '0' > 7))
{
if (sign == 1)
return (LLONG_MAX);
else
return (LLONG_MIN);
}
value = 10 * value + (str[i++] - '0');
}
return (value * sign);
}