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 | 91 | static char not_a_number(char c) | |
19 | { | ||
20 |
8/8✓ Branch 0 taken 59 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 21 times.
✓ Branch 6 taken 11 times.
✓ Branch 7 taken 11 times.
|
91 | if ((c < '0' || c > '9') && c != '-' && c != '+') |
21 | 11 | return 1; | |
22 | 80 | return 0; | |
23 | } | ||
24 | |||
25 | 36 | static char check_end(char *number, number_settings_t *settings, | |
26 | int i, int start) | ||
27 | { | ||
28 | 36 | my_errno = 0; | |
29 |
6/6✓ Branch 0 taken 31 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 times.
|
36 | if ((!settings->letter_before && not_a_number(number[i]) && start == -1) |
30 |
6/6✓ Branch 0 taken 12 times.
✓ Branch 1 taken 23 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 times.
|
35 | || (!settings->letter_after && not_a_number(number[i]) && start != -1) |
31 |
6/6✓ Branch 0 taken 11 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 9 times.
|
34 | || (!settings->letter_after && (number[i] == '-' || number[i] == '+') |
32 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | && start != -1) |
33 |
6/6✓ Branch 0 taken 25 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 15 times.
✓ Branch 5 taken 1 times.
|
33 | || (!settings->multiple_signe && i != 0 && (number[i] == '-' || |
34 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
16 | number[i] == '+') && (number[i - 1] == '-' || number[i - 1] == '+')) |
35 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
32 | || (!settings->overflow && (i > start + 9 || |
36 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
15 | (i > start + 8 && number[start] != '1' && number[start] != '2')))) { |
37 | 4 | my_errno = 1; | |
38 | 4 | return 1; | |
39 | } | ||
40 |
5/6✓ Branch 0 taken 22 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
32 | if (settings->letter_after && not_a_number(number[i]) && start != -1) |
41 | 1 | return 1; | |
42 | 31 | return 0; | |
43 | } | ||
44 | |||
45 | 12 | int my_super_number(char *number, number_settings_t settings) | |
46 | { | ||
47 | 12 | int nb = 0; | |
48 | 12 | int start = -1; | |
49 | 12 | char neg = 1; | |
50 | |||
51 |
3/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 11 times.
|
12 | if (number == NULL || number[0] == '\0') |
52 | 1 | return nb; | |
53 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
|
42 | for (int i = 0; number[i] != '\0'; i++) { |
54 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 31 times.
|
36 | if (check_end(number, &settings, i, start)) |
55 | 5 | return nb * neg; | |
56 |
5/6✓ Branch 0 taken 5 times.
✓ Branch 1 taken 26 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
|
31 | if ((settings.letter_before && not_a_number(number[i]) && start == -1) |
57 |
3/6✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
28 | || (settings.letter_after && not_a_number(number[i]) && start != -1)) |
58 | 3 | neg = 1; | |
59 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
31 | if (number[i] == '-' && start == -1) |
60 | 7 | neg = neg * -1; | |
61 |
6/6✓ Branch 0 taken 22 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 3 times.
|
31 | if (start == -1 && number[i] >= '1' && number[i] <= '9') |
62 | 9 | start = i; | |
63 |
4/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 3 times.
|
31 | if (number[i] >= '0' && number[i] <= '9') |
64 | 18 | nb = nb * 10 + (number[i] - 48); | |
65 | } | ||
66 | 6 | return nb * neg; | |
67 | } | ||
68 |