-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
45 lines (41 loc) · 1.44 KB
/
ft_atoi.c
File metadata and controls
45 lines (41 loc) · 1.44 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mschumac <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/05/22 02:34:30 by mschumac #+# #+# */
/* Updated: 2017/06/12 19:48:59 by mschumac ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_is_whitespace(char c)
{
return (c == '\n' || c == '\v' || c == '\t' || c == '\f' || c == '\r');
}
int ft_atoi(const char *s)
{
int sign;
long long result;
int i;
result = 0;
sign = 1;
i = 0;
while (*s == '0' || ft_is_whitespace(*s) || *s == ' ')
s++;
if (*s == '-')
sign = -1;
if (*s == '+' || *s == '-')
s++;
while (*s >= '0' && *s <= '9')
{
result = result * 10 + (int)(*s - '0');
s++;
i++;
}
if (i > 19 || result > 9223372036854775807)
return ((sign == -1) ? -1 : 0);
else
return (sign * (int)result);
}