-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
executable file
·47 lines (44 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: josgarci <josgarci@student.42madrid> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/16 11:21:29 by josgarci #+# #+# */
/* Updated: 2021/12/12 22:12:44 by josgarci ### ########.fr */
/* */
/* ************************************************************************** */
static int check_overflows(unsigned long long n, int neg)
{
if (neg == -1 && n > 9223372036854775807)
return (0);
if (neg == 1 && n > 9223372036854775807)
return (-1);
return (n);
}
int ft_atoi(const char *str)
{
int i;
unsigned long long n;
int neg;
i = 0;
neg = 1;
n = 0;
while (str[i] == ' ' || str[i] == '\t' || str[i] == '\r'
|| str[i] == '\f' || str[i] == '\v' || str[i] == '\n')
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
neg = -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
n = n * 10 + str[i] - '0';
i++;
}
n = check_overflows(n, neg);
return (n * neg);
}