-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
52 lines (47 loc) · 1.58 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nelidris <nelidris@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/02 14:53:51 by nelidris #+# #+# */
/* Updated: 2021/11/07 11:32:34 by nelidris ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int _convert(const char *str, int sign)
{
size_t i;
size_t rslt;
i = 0;
rslt = 0;
while (str[i] && ft_isdigit(str[i]))
{
if ((unsigned long)rslt * 10 + (str[i] - 48)
> 9223372036854775807 && sign == 1)
return (-1);
else if ((unsigned long)rslt * 10 + (str[i] - 48)
> (unsigned long)9223372036854775807 + 1 && sign == -1)
return (0);
rslt = (rslt * 10) + (str[i] - 48);
i++;
}
return ((long)rslt * sign);
}
int ft_atoi(const char *str)
{
size_t i;
int sign;
i = 0;
sign = 1;
while ((str[i] >= '\t' && str[i] <= '\r') || str[i] == ' ')
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
sign = -1;
i++;
}
return (_convert(&str[i], sign));
}