-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
42 lines (39 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/31 19:59:24 by dskrypny #+# #+# */
/* Updated: 2018/05/23 19:08:05 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
unsigned long res;
size_t i;
int flag;
i = 0;
flag = 1;
res = 0;
while ((str[i] <= '\r' && str[i] >= '\t') || str[i] == ' ')
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
flag = -1;
i++;
}
while (str[i] && (str[i] <= '9' && str[i] >= '0'))
{
res = res * 10 + str[i] - '0';
if (res > 922337236854775807 && flag == 1)
return (-1);
if (res > 922337236854775808 && flag == -1)
return (0);
i++;
}
return ((int)res * flag);
}