-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
75 lines (69 loc) · 1.88 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
74
75
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_atoi.c :+: :+: */
/* +:+ */
/* By: farodrig <farodrig@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/12/04 13:54:58 by farodrig #+# #+# */
/* Updated: 2021/02/28 21:51:47 by farodrig ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <limits.h>
static void add_number(const char *str, long *res)
{
while (ft_isdigit(*str))
{
if (*res == 0)
{
*res = (*str - '0');
}
else
{
*res = *res * 10 + (*str - '0');
}
str++;
}
}
static char *skip_empty_characters(const char *str)
{
while (str)
{
if ((*str == '\n') || (*str == '\t') || (*str == '\v'))
str++;
else if ((*str == ' ') || (*str == '\f') || (*str == '\r'))
str++;
else
break ;
}
return ((char *)str);
}
/*
** Converts the initial portion of the string pointed to by str to int
** representation.
*/
int ft_atoi(const char *str)
{
unsigned int is_neg;
long res;
res = 0;
is_neg = 0;
str = skip_empty_characters(str);
if (*str == '-')
is_neg = 1;
if (*str == '-' || *str == '+')
str++;
if (ft_isdigit(*str))
{
add_number(str, &res);
if (is_neg)
res = -res;
if ((res < INT_MIN && res > -LONG_MAX) || res >= LONG_MAX)
return (-1);
if (res > INT_MAX || res <= -LONG_MAX)
return (0);
return (res);
}
return (0);
}