-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
73 lines (67 loc) · 1.85 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ibohonos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:18:15 by ibohonos #+# #+# */
/* Updated: 2017/11/07 10:50:41 by ibohonos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_delimiter(char temp)
{
if ((temp >= 9 && temp <= 13) || temp == 32)
return (1);
return (0);
}
static int is_min_max(const char *nbr, int minus)
{
int i;
i = 0;
while ((nbr[i] >= '0' && nbr[i] <= '9') || nbr[i] == '-' || nbr[i] == '+')
i++;
if (minus)
i--;
if (i > 19)
{
if (minus)
return (0);
return (1);
}
else if (i == 19)
{
if (minus && ft_strcmp(nbr, "-9223372036854775807") > 0)
return (0);
if (ft_strcmp(nbr, "9223372036854775807") > 0)
return (1);
}
return (2);
}
int ft_atoi(const char *nptr)
{
int minus;
int number;
minus = 0;
number = 0;
while (is_delimiter(*nptr))
nptr++;
if (*nptr == '-')
minus = 1;
if (is_min_max(nptr, minus) == 0)
return (0);
else if (is_min_max(nptr, minus) == 1)
return (-1);
if (*nptr == '-' || *nptr == '+')
nptr++;
while (*nptr >= '0' && *nptr <= '9')
{
number *= 10;
number += (int)(*nptr - '0');
nptr++;
}
if (minus)
number = -number;
return (number);
}