-
Notifications
You must be signed in to change notification settings - Fork 4
/
ft_atoi.c
41 lines (38 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vbrazhni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/01 15:35:30 by vbrazhni #+# #+# */
/* Updated: 2018/07/01 15:35:32 by vbrazhni ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
unsigned long result;
unsigned long border;
size_t i;
int sign;
result = 0;
border = (unsigned long)(FT_LONG_MAX / 10);
i = 0;
while (ft_isspace(str[i]))
i++;
sign = (str[i] == '-') ? -1 : 1;
if (str[i] == '-' || str[i] == '+')
i++;
while (ft_isdigit(str[i]))
{
if ((result > border || (result == border && (str[i] - '0') > 7))
&& sign == 1)
return (-1);
else if ((result > border || (result == border && (str[i] - '0') > 8))
&& sign == -1)
return (0);
result = result * 10 + (str[i++] - '0');
}
return ((int)(result * sign));
}