-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
39 lines (36 loc) · 1.41 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+
+:+ */
/* By: nkawaguc <nkawaguc@student.42tokyo.jp> +#+ +:+
+#+ */
/* +#+#+#+#+#+
+#+ */
/* Created: 2024/03/15 16:52:33 by nkawaguc #+# #+# */
/* Updated: 2024/03/15 16:52:33 by nkawaguc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
long ret;
long sign;
ret = 0l;
sign = 1l;
while ((9 <= *str && *str <= 13) || *str == ' ')
str++;
if (*str == '-' || *str == '+')
if (*str++ == '-')
sign = -1l;
while (ft_isdigit(*str))
{
if (ret > LONG_MAX / 10 || (ret == LONG_MAX / 10 && *str > '7'))
return ((int)LONG_MAX);
if (ret < LONG_MIN / 10 || (ret == LONG_MIN / 10 && *str > '8'))
return ((int)LONG_MIN);
ret = ret * 10l + (*str++ - '0') * sign;
}
return ((int)ret);
}