-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
53 lines (50 loc) · 1.69 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vportell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/10/31 08:36:08 by vportell #+# #+# */
/* Updated: 2016/11/05 13:16:05 by vportell ### ########.fr */
/* */
/* ************************************************************************** */
static int ft_overflow(unsigned long long nbr, int new_digit, int sign)
{
if (nbr >= 1000000000000000000)
{
if (sign == 1)
return (-1);
else
return (0);
}
if (nbr >= 922337203685477580 && sign == 1 && new_digit > 7)
return (-1);
if (nbr >= 922337203685477580 && sign == -1 && new_digit > 8)
return (0);
return (1);
}
int ft_atoi(const char *str)
{
int sign;
int overflow;
unsigned long long num;
sign = 1;
num = 0;
while (*str == ' ' || *str == '\t' || *str == '\n'
|| *str == '\v' || *str == '\f' || *str == '\r')
str++;
if (*str == '-')
sign = -1;
if (*str == '-' || *str == '+')
str++;
while (*str >= '0' && *str <= '9')
{
overflow = ft_overflow(num, *str - '0', sign);
if (overflow != 1)
return (overflow);
num = num * 10 + (*str - '0');
str++;
}
return (sign * num);
}