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