-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
54 lines (48 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
50
51
52
53
54
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yoaoki <yoaoki@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/17 20:05:27 by yoaoki #+# #+# */
/* Updated: 2024/07/19 23:09:00 by yoaoki ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char c)
{
return (c == ' ' || (c >= 9 && c <= 13));
}
static int is_overflow(long long num, int c)
{
return (num > (LONG_MAX - c) / 10);
}
static int is_underflow(long long num, int c)
{
return (num < (LONG_MIN + c) / 10);
}
int ft_atoi(const char *str)
{
long long result;
int minus;
result = 0;
minus = 1;
while (ft_isspace(*str))
str++;
if (*str == '-' || *str == '+')
{
if (*str == '-')
minus = -1;
str++;
}
while (ft_isdigit(*str))
{
if (is_overflow(result * minus, *str - '0'))
return ((int)LONG_MAX);
else if (is_underflow(result * minus, *str - '0'))
return ((int)LONG_MIN);
result = result * 10 + (*str++ - '0');
}
return (result * minus);
}