-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
42 lines (39 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stales <stales@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/17 18:24:34 by stales #+# #+# */
/* Updated: 2022/04/04 16:13:18 by stales ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @brief Converts the initial portion of the string pointed to by str
* to int.
*
* @param nptr String to convert
*
* @return (int) The converted value or 0 on error
*/
int ft_atoi(char *nptr)
{
long int to_dec;
int neg;
to_dec = 0;
neg = 1;
while (*nptr == ' ' || (*nptr >= '\t' && *nptr <= '\r'))
nptr++;
if ((*nptr == '+' || *nptr == '-'))
if (*nptr++ == '-')
neg = ~(neg - 1);
while (*nptr >= '0' && *nptr <= '9')
to_dec = (to_dec * 0xA) + (*nptr++ & 0xF);
if (neg == -1 && to_dec < -2147483648)
return (0);
if (neg && to_dec < -2147483648)
return (-1);
return (to_dec * neg);
}