minishell
ft_atoi_strict.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* ft_atoi_strict.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: kfujita <kfujita@student.42tokyo.jp> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2023/02/12 22:16:40 by kfujita #+# #+# */
9 /* Updated: 2023/03/02 11:20:35 by kfujita ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "../ft_is/ft_is.h"
14 #include "../ft_math/ft_math.h"
15 
16 // - NULL
17 #include <stddef.h>
18 
19 static bool set_value(char c, int *value, int sign)
20 {
21  int c_value;
22 
23  if (!ft_isdigit(c))
24  return (false);
25  c_value = (c - '0') * sign;
26  if (!can_mul(*value, 10) || !can_add(*value * 10, c_value))
27  return (false);
28  *value = (*value * 10) + c_value;
29  return (true);
30 }
31 
32 bool ft_atoi_strict(const char *str, const char **endptr, int *dst)
33 {
34  int sign;
35 
36  if (str == NULL || dst == NULL)
37  return (false);
38  if (*endptr != NULL)
39  *endptr = str;
40  sign = 1;
41  if (*str == '-')
42  sign = -1;
43  if (*str == '-' || *str == '+')
44  str++;
45  if (*endptr != NULL)
46  *endptr = str;
47  if (!ft_isdigit(*str))
48  return (false);
49  *dst = 0;
50  while (set_value(*str, dst, sign))
51  str++;
52  if (*endptr != NULL)
53  *endptr = str;
54  return (true);
55 }
bool can_add(int a, int b)
Definition: can_add.c:18
bool can_mul(int a, int b)
Definition: can_mul.c:18
static bool set_value(char c, int *value, int sign)
bool ft_atoi_strict(const char *str, const char **endptr, int *dst)
int ft_isdigit(int c)
Definition: ft_isdigit.c:15