| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** my_getnbr_base | ||
| 4 | ** File description: | ||
| 5 | ** Returns an int number starting from a string number (str) | ||
| 6 | ** in the requested base (base) | ||
| 7 | */ | ||
| 8 | /** | ||
| 9 | * @file my_getnbr_base.c | ||
| 10 | * @brief The file containing the my_getnbr_base function | ||
| 11 | * @author Nicolas TORO | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "my.h" | ||
| 15 | |||
| 16 | 1262 | static int is_valid(char c, char const *base) | |
| 17 | { | ||
| 18 | 2/2✓ Branch 1 taken 9374 times. ✓ Branch 2 taken 2 times. | 9376 | for (int i = 0; i < my_strlen(base); i++) { | 
| 19 | 2/2✓ Branch 0 taken 1260 times. ✓ Branch 1 taken 8114 times. | 9374 | if (c == base[i]) | 
| 20 | 1260 | return 1; | |
| 21 | } | ||
| 22 | 3/4✓ Branch 0 taken 1 times. ✓ Branch 1 taken 1 times. ✗ Branch 2 not taken. ✓ Branch 3 taken 1 times. | 2 | if (c == '-' || c == '+') | 
| 23 | 1 | return 1; | |
| 24 | 1 | return 0; | |
| 25 | } | ||
| 26 | |||
| 27 | 1262 | static int calculate_base(char const *str, char const *base, | |
| 28 | int *index, int **ptr) | ||
| 29 | { | ||
| 30 | 2/2✓ Branch 1 taken 1261 times. ✓ Branch 2 taken 1 times. | 1262 | if (is_valid(str[index[0]], base) == 1) { | 
| 31 | 2/2✓ Branch 0 taken 1 times. ✓ Branch 1 taken 1260 times. | 1261 | if (str[index[0]] == '-') { | 
| 32 | 1 | *(ptr[0]) = - *(ptr[0]); | |
| 33 | 1 | return 0; | |
| 34 | } | ||
| 35 | 2/2✓ Branch 0 taken 81 times. ✓ Branch 1 taken 1179 times. | 1260 | if (str[index[0]] == base[index[1]]) { | 
| 36 | 81 | return index[1] * my_compute_power_rec(my_strlen(base), | |
| 37 | 81 | my_strlen(str) - index[0] - 1); | |
| 38 | } | ||
| 39 | } else { | ||
| 40 | 1 | *(ptr[1]) = 1; | |
| 41 | } | ||
| 42 | 1180 | return 0; | |
| 43 | } | ||
| 44 | |||
| 45 | 28 | int my_getnbr_base(char const *str, char const *base) | |
| 46 | { | ||
| 47 | 28 | int result = 0; | |
| 48 | 28 | int signe_save = 0; | |
| 49 | 28 | int signe = 1; | |
| 50 | 28 | int error = 0; | |
| 51 | int index[2]; | ||
| 52 | int *ptr[2]; | ||
| 53 | |||
| 54 | 2/2✓ Branch 1 taken 83 times. ✓ Branch 2 taken 28 times. | 111 | for (int i = my_strlen(str) - 1; i >= 0; i--) { | 
| 55 | 6/6✓ Branch 1 taken 1264 times. ✓ Branch 2 taken 81 times. ✓ Branch 3 taken 1263 times. ✓ Branch 4 taken 1 times. ✓ Branch 5 taken 1262 times. ✓ Branch 6 taken 1 times. | 1345 | for (int j = 0; j < my_strlen(base) && error == 0 && signe == 1; j++) { | 
| 56 | 1262 | index[0] = i; | |
| 57 | 1262 | index[1] = j; | |
| 58 | 1262 | ptr[0] = &signe; | |
| 59 | 1262 | ptr[1] = &error; | |
| 60 | 1262 | result = result + calculate_base(str, base, index, ptr); | |
| 61 | } | ||
| 62 | 83 | signe_save = signe; | |
| 63 | 83 | signe = 1; | |
| 64 | } | ||
| 65 | 28 | return result * signe_save; | |
| 66 | } | ||
| 67 |