-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_atoi_base.c
40 lines (35 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gamarcha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/04 22:57:12 by gamarcha #+# #+# */
/* Updated: 2021/06/03 13:22:10 by gamarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t get_num(int c, const char *base)
{
size_t i;
i = 0;
while (base[i])
if (c == base[i++])
break ;
return (i - 1);
}
int ft_atoi_base(const char *str, const char *base, size_t len_base)
{
int result;
int sign;
while (*str == 32 || (*str > 8 && *str < 14))
str++;
sign = 1;
if (*str == 43 || *str == 45)
sign *= (*str++ & 2) - 1;
result = 0;
while (*str && ft_ischarset(*str, base))
result = result * len_base + get_num(*str++, base);
return (result * sign);
}