tgrekov-libft
HIVE libft Oct 2023
Loading...
Searching...
No Matches
ft_atoi.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_atoi.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2023/10/25 13:12:00 by tgrekov #+# #+# */
9/* Updated: 2023/11/16 08:55:47 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include "libft.h"
21
22static int ft_isspace(char c)
23{
24 return (c == ' ' || c == '\t' || c == '\n'
25 || c == '\v' || c == '\f' || c == '\r');
26}
27
42int ft_atoi(const char *str)
43{
44 long nbr;
45 int sign;
46
47 while (ft_isspace(*str))
48 str++;
49 sign = 1;
50 if (*str == '-' || *str == '+')
51 {
52 if (*str == '-')
53 sign *= -1;
54 str++;
55 }
56 nbr = 0;
57 while (ft_isdigit(*str))
58 {
59 if (nbr > FT_LONG_MAX / 10 || (nbr == FT_LONG_MAX / 10 && *str > '7'))
60 {
61 if (sign == 1)
62 return ((int) FT_LONG_MAX);
63 return ((int) -FT_LONG_MAX - 1L);
64 }
65 nbr = nbr * 10 + (*(str++) - '0');
66 }
67 return (nbr * sign);
68}
int ft_atoi(const char *str)
Converts ASCII string str to integer representation.
Definition ft_atoi.c:42
int ft_isdigit(int c)
Is c a numeric character.
Definition ft_isdigit.c:26
#define FT_LONG_MAX
Not allowed to include limits.h.
Definition libft.h:29