-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
49 lines (46 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tedison <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/01 10:22:48 by tedison #+# #+# */
/* Updated: 2021/04/14 12:02:35 by tedison ### ########.fr */
/* */
/* ************************************************************************** */
int ft_is_space(char c, int *i)
{
*i = 0;
if (c == '\t' || c == '\n' || c == '\v' || c == '\f'
|| c == '\r' || c == ' ')
return (1);
return (0);
}
int ft_atoi(const char *str)
{
int i;
long long sign;
unsigned long long number;
int c;
i = 0;
sign = 1;
number = 0;
while (ft_is_space(str[i], &c))
i++;
if (str[i] == '-' || str[i] == '+')
if (str[i++] == '-')
sign *= -1;
while (str[i] >= '0' && str[i] <= '9')
{
number += str[i++] - '0';
c++;
if (str[i] >= '0' && str[i] <= '9')
number *= 10;
}
if ((number > 9223372036854775807ull && sign > 0) || (sign > 0 && c > 19))
return (-1);
else if ((number > 9223372036854775808ull && sign < 0) || c > 19)
return (0);
return (number * sign);
}