-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
39 lines (36 loc) · 1.33 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: thakala <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/02 12:19:47 by thakala #+# #+# */
/* Updated: 2021/12/08 14:42:14 by thakala ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
char coefficient;
long result;
while (ft_isspace(*str))
str++;
coefficient = 1;
if (*str == '+' || *str == '-')
coefficient = -(*str++ - 44);
result = 0;
while (*str == '0')
str++;
while (ft_isdigit(*str))
{
result = 10 * result + coefficient * (*str++ - '0');
if (((result >> 63) | 0x1) != coefficient)
{
if (coefficient == -1)
return (0);
return (-1);
}
}
return ((int)result);
}