-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
84 lines (73 loc) · 2.28 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
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gasouza <gasouza@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/18 21:05:04 by gasouza #+# #+# */
/* Updated: 2023/03/22 14:44:36 by gasouza ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#ifndef EMPTY_CHARACTERS
# define EMPTY_CHARACTERS "\t\n\v\f\r "
#endif
#ifndef NUMBER_BASE
# define NUMBER_BASE 10
#endif
static const char *skip_empty_chars(const char *str);
static const char *skip_signal_prefix(const char *str);
static int ascii_to_number(const char c);
static int get_max_place_value(const char *str);
int ft_atoi(const char *str)
{
int is_negative;
int final_number;
const char *current_char;
int place_value;
final_number = 0;
str = skip_empty_chars(str);
is_negative = *str == '-';
current_char = skip_signal_prefix(str);
place_value = get_max_place_value(current_char);
while (*current_char && ft_isdigit(*current_char))
{
final_number += ascii_to_number(*current_char) * place_value;
current_char++;
place_value /= NUMBER_BASE;
}
if (is_negative)
final_number *= -1;
return (final_number);
}
static const char *skip_empty_chars(const char *str)
{
while (ft_strchr(EMPTY_CHARACTERS, *str))
str++;
return (str);
}
static const char *skip_signal_prefix(const char *str)
{
if (*str == '+' || *str == '-')
str++;
return (str);
}
static int ascii_to_number(const char c)
{
return (c - '0');
}
static int get_max_place_value(const char *str)
{
int max_place_value;
max_place_value = 0;
while (*str && ft_isdigit(*str))
{
if (!max_place_value)
max_place_value = 1;
else
max_place_value *= NUMBER_BASE;
str++;
}
return (max_place_value);
}