-
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: bkaztaou <bkaztaou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/10 00:51:21 by bkaztaou #+# #+# */
/* Updated: 2023/07/10 00:51:53 by bkaztaou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static int ft_isdigit(int c)
{
return (c >= '1' && c <= '9');
}
static int ft_iswspace(int c)
{
if (c == 32 || (c >= 9 && c <= 13))
return (1);
return (0);
}
static int ft_isorange(unsigned long long result, int sign)
{
if (result > LLONG_MAX && sign == -1)
return (0);
if (result > LLONG_MAX && sign == 1)
return (-1);
return (result * sign);
}
int ft_atoi(const char *str)
{
size_t i;
int sign;
unsigned long long result;
i = 0;
result = 0;
sign = 1;
while (ft_iswspace(str[i]))
i++;
if (str[i] == '-')
sign *= -1;
if (str[i] == '-' || str[i] == '+')
i++;
while (str[i] && ft_isdigit(str[i]))
result = result * 10 + str[i++] - '0';
return (ft_isorange(result, sign));
}