-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi_base.c
67 lines (61 loc) · 1.65 KB
/
ft_atoi_base.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbrophy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/24 15:52:37 by dbrophy #+# #+# */
/* Updated: 2019/10/28 13:54:51 by dbrophy ### ########.fr */
/* */
/* ************************************************************************** */
static int calc_base_len(char *base)
{
int len;
int i;
len = -1;
while (base[++len])
{
i = len;
while (base[++i])
if (base[i] == base[len] || base[len] == '-' || base[len] == '+')
return (-1);
}
return (len);
}
static int ft_chr_idx(char *str, int len, char c)
{
int i;
i = -1;
while (++i < len)
{
if (str[i] == c)
return (i);
}
return (-1);
}
int ft_atoi_base(char *str, char *base)
{
int out;
int baselen;
int idx;
int sign;
baselen = calc_base_len(base);
out = 0;
if (baselen < 2)
return (0);
if (*str == '-')
str -= (sign = -1);
else if (*str == '+')
str += (sign = 1);
else
sign = 1;
while (*str)
{
idx = ft_chr_idx(base, baselen, *(str++));
if (idx < 0)
return (0);
out = out * baselen + idx * sign;
}
return (out);
}