-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi_for_printf.c
69 lines (62 loc) · 1.85 KB
/
ft_atoi_for_printf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_for_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ysoroko <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/02 15:31:19 by ysoroko #+# #+# */
/* Updated: 2021/01/15 15:34:34 by ysoroko ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_char_is_in_charset(char c, const char *charset)
{
int i;
if (!charset)
return (0);
i = 0;
while (charset[i])
{
if (c == charset[i])
return (1);
i++;
}
return (0);
}
static int ft_extreme(int number, unsigned long long extreme, int sign)
{
unsigned long long min;
unsigned long long max;
min = 9223372036854775808U;
max = 9223372036854775807U;
if (extreme > max && sign == 1)
return (-1);
if (extreme > min && sign == -1)
return (0);
return (number * sign);
}
int ft_atoi_p(const char *str)
{
int i;
int sign;
int number;
unsigned long long extreme;
char *my_str;
if (!str)
return (0);
i = 0;
sign = 1;
number = 0;
extreme = 0;
my_str = (char *)(str);
while (ft_char_is_in_charset(my_str[i], "0.*-"))
i++;
while (my_str[i] >= '0' && my_str[i] <= '9')
{
number = number * 10 + my_str[i] - '0';
extreme = extreme * 10 + my_str[i] - '0';
i++;
}
return (ft_extreme(number, extreme, sign));
}