-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
56 lines (51 loc) · 1.54 KB
/
Copy pathft_atoi.c
File metadata and controls
56 lines (51 loc) · 1.54 KB
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
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: user <user@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/29 15:30:00 by user #+# #+# */
/* Updated: 2023/04/29 15:30:00 by user ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_space(char c)
{
char *space;
space = "\t\n\v\f\r ";
while (*space)
{
if (*space == c)
return (1);
space++;
}
return (0);
}
int ft_atoi(const char *str)
{
int sign;
long long ans;
size_t i;
i = 0;
ans = 0;
sign = 1;
while (is_space(str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign = -1;
i++;
}
while (ft_isdigit(str[i]))
{
if (sign == 1 && ans > (LONG_MAX - (str[i] - '0')) / 10)
return ((int)LONG_MAX);
else if (sign == -1 && (-1 * ans) < (LONG_MIN + (str[i] - '0')) / 10)
return ((int)LONG_MIN);
ans = ans * 10 + (str[i] - '0');
i++;
}
return ((int)(ans * sign));
}