Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2024 | ||
3 | ** my_super_number | ||
4 | ** File description: | ||
5 | ** Returns an int number starting from a string number (number) | ||
6 | ** and change my_errno if an error occurs depending on the settings (settings) | ||
7 | */ | ||
8 | /** | ||
9 | * @file my_super_number.c | ||
10 | * @brief The file containing the my_super_number function | ||
11 | * @author Nicolas TORO | ||
12 | */ | ||
13 | |||
14 | #include "my.h" | ||
15 | |||
16 | int my_errno; | ||
17 | |||
18 | 12 | static char not_a_number(char c) | |
19 | { | ||
20 |
2/8✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
12 | if ((c < '0' || c > '9') && c != '-' && c != '+') |
21 | ✗ | return 1; | |
22 | 12 | return 0; | |
23 | } | ||
24 | |||
25 | 4 | static char check_end(char *number, number_settings_t *settings, | |
26 | int i, int start) | ||
27 | { | ||
28 | 4 | my_errno = 0; | |
29 |
2/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
4 | if ((!settings->letter_before && not_a_number(number[i]) && start == -1) |
30 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
4 | || (!settings->letter_after && not_a_number(number[i]) && start != -1) |
31 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
4 | || (!settings->letter_after && (number[i] == '-' || number[i] == '+') |
32 | ✗ | && start != -1) | |
33 |
4/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
4 | || (!settings->multiple_signe && i != 0 && (number[i] == '-' || |
34 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2 | number[i] == '+') && (number[i - 1] == '-' || number[i - 1] == '+')) |
35 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | || (!settings->overflow && (i > start + 9 || |
36 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2 | (i > start + 8 && number[start] != '1' && number[start] != '2')))) { |
37 | ✗ | my_errno = 1; | |
38 | ✗ | return 1; | |
39 | } | ||
40 |
2/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
4 | if (settings->letter_after && not_a_number(number[i]) && start != -1) |
41 | ✗ | return 1; | |
42 | 4 | return 0; | |
43 | } | ||
44 | |||
45 | 2 | int my_super_number(char *number, number_settings_t settings) | |
46 | { | ||
47 | 2 | int nb = 0; | |
48 | 2 | int start = -1; | |
49 | 2 | char neg = 1; | |
50 | |||
51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (number[0] == '\0') |
52 | ✗ | return nb; | |
53 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (int i = 0; number[i] != '\0'; i++) { |
54 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (check_end(number, &settings, i, start)) |
55 | ✗ | return nb * neg; | |
56 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
4 | if ((settings.letter_before && not_a_number(number[i]) && start == -1) |
57 |
2/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
4 | || (settings.letter_after && not_a_number(number[i]) && start != -1)) |
58 | ✗ | neg = 1; | |
59 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4 | if (number[i] == '-' && start == -1) |
60 | ✗ | neg = neg * -1; | |
61 |
4/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
4 | if (start == -1 && number[i] >= '1' && number[i] <= '9') |
62 | 2 | start = i; | |
63 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | if (number[i] >= '0' && number[i] <= '9') |
64 | 4 | nb = nb * 10 + (number[i] - 48); | |
65 | } | ||
66 | 2 | return nb * neg; | |
67 | } | ||
68 |