-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
53 lines (48 loc) · 1.57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/02 15:26:37 by hshinaga #+# #+# */
/* Updated: 2024/11/05 00:38:29 by hshinaga ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char c)
{
return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v'
|| c == '\f');
}
static int ft_check_overflow(long result, int sign)
{
if (result * sign > 2147483647)
return (-1);
if (result * sign < -2147483648)
return (0);
return (1);
}
int ft_atoi(const char *str)
{
int sign;
long result;
sign = 1;
result = 0;
while (ft_isspace(*str))
str++;
if (*str == '-' || *str == '+')
{
if (*str == '-')
sign = -1;
str++;
}
while (*str >= '0' && *str <= '9')
{
result = result * 10 + (*str - '0');
if (ft_check_overflow(result, sign) != 1)
return (ft_check_overflow(result, sign));
str++;
}
return ((int)(result * sign));
}