-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
40 lines (37 loc) · 1.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lubaujar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/05 08:42:02 by lubaujar #+# #+# */
/* Updated: 2014/11/13 14:29:02 by lubaujar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(char const *s)
{
unsigned int digit;
int positive;
int value;
value = 0;
digit = 0;
while (*s == ' ' || *s == '\t' || *s == '\n'
|| *s == '\r' || *s == '\v' || *s == '\f')
s++;
positive = (*s == '-' ? -1 : 1);
if (*s == '-' || *s == '+')
s++;
if (*s == '0')
s++;
if (ft_strlen(s) > 19)
return (positive == 1 ? -1 : 0);
while (ft_isdigit(*s) && *s)
{
digit = (int)(*s - '0');
value = (value * 10) + digit;
s++;
}
return (value * positive);
}