diff options
Diffstat (limited to 'functions_reference/ref_ft_atoi_base.c')
| -rw-r--r-- | functions_reference/ref_ft_atoi_base.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/functions_reference/ref_ft_atoi_base.c b/functions_reference/ref_ft_atoi_base.c new file mode 100644 index 0000000..b2a4231 --- /dev/null +++ b/functions_reference/ref_ft_atoi_base.c @@ -0,0 +1,44 @@ +#include "libasm_test.h" + +static bool +valid_base(char *base) +{ + if (strlen(base) < 2) + return false; + while (*base) + { + if (isspace(*base) || *base == '+' || *base == '-') + return false; + for (int i = 1; base[i]; i++) + if (base[i] == *base) + return false; + base++; + } + return true; +} + +int +ref_ft_atoi_base(char *str, char *base) +{ + long int nb; + int radix; + bool is_negative; + + if (!valid_base(base)) + return 0; + while (isspace(*str)) + str++; + is_negative = false; + while (*str == '+' || *str == '-') + if (*str++ == '-') + is_negative = !is_negative; + radix = strlen(base); + nb = 0; + while (*str && strchr(base, *str) != NULL) + { + nb *= radix; + nb += strchr(base, *str) - base; + str++; + } + return is_negative ? -nb : nb; +} |
