diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-11-24 04:51:55 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-11-24 04:51:55 +0100 |
| commit | 997cb1c39c603512b7db760e9537c39ea3c68a38 (patch) | |
| tree | b09df18114389b331a8b07254c78f881afb222b5 /functions_reference/ref_ft_atoi_base.c | |
| parent | 5a47d63887a0878243b17799b19c4a0f76d3756d (diff) | |
| download | libasm_test-997cb1c39c603512b7db760e9537c39ea3c68a38.tar.gz libasm_test-997cb1c39c603512b7db760e9537c39ea3c68a38.tar.bz2 libasm_test-997cb1c39c603512b7db760e9537c39ea3c68a38.zip | |
Added reference bonus functions to test against assembly one
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; +} |
