GCC Code Coverage Report


Directory: ./
File: lib/my/my_super_array.c
Date: 2024-05-07 14:54:03
Exec Total Coverage
Lines: 47 47 100.0%
Functions: 6 6 100.0%
Branches: 23 24 95.8%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2023
3 ** my_super_array
4 ** File description:
5 ** Returns an array of words delimited by a list of separator (separator)
6 ** from a string (str)
7 */
8 /**
9 * @file my_super_array.c
10 * @brief The file containing the my_super_array function
11 * @author Nicolas TORO
12 */
13
14 #include "my.h"
15
16 1292 static int my_char_is_sep(char c, char *sep)
17 {
18
2/2
✓ Branch 0 taken 3546 times.
✓ Branch 1 taken 1141 times.
4687 for (int i = 0; sep[i] != '\0'; i++) {
19
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 3395 times.
3546 if (c == sep[i])
20 151 return 1;
21 }
22 1141 return 0;
23 }
24
25 27 int count_words(char *str, char *sep)
26 {
27 27 int n = 0;
28 27 int do_it = 1;
29
30
2/2
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 27 times.
424 for (int i = 0; str[i] != '\0'; i++) {
31
4/4
✓ Branch 1 taken 359 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 295 times.
397 if (!my_char_is_sep(str[i], sep) && do_it) {
32 64 n++;
33 64 do_it = 0;
34 }
35
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 359 times.
397 if (my_char_is_sep(str[i], sep))
36 38 do_it = 1;
37 }
38 27 return n;
39 }
40
41 64 int count_letters(char *str, char *sep, int *save)
42 {
43 64 int n = 0;
44
45 64 for (int i = *save; str[i] != '\0'
46
4/4
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 27 times.
✓ Branch 3 taken 359 times.
✓ Branch 4 taken 37 times.
423 && !my_char_is_sep(str[i], sep); i++) {
47 359 (*save)++;
48 359 n++;
49 }
50 64 return n;
51 }
52
53 64 int decale_save(int *save, char *str, char *sep)
54 {
55 64 for (int i = *save; str[i] != '\0'
56
3/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 64 times.
102 && my_char_is_sep(str[i], sep); i++) {
57 38 (*save)++;
58 }
59 64 return 0;
60 }
61
62 64 int fill_str(char *to_fill, int nb_letters, int save, char *source)
63 {
64 64 int a = 0;
65
66 64 for (int i = save - nb_letters;
67
2/2
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 64 times.
423 i < nb_letters + (save - nb_letters); i++) {
68 359 to_fill[a] = source[i];
69 359 a++;
70 }
71 64 return 0;
72 }
73
74 27 char **my_super_array(char *str, char *sep)
75 {
76 27 int nb_words = count_words(str, sep);
77 27 char **array = malloc(sizeof(char *) * (nb_words + 1));
78 27 int save = 0;
79 27 int nb_letters = 0;
80
81
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 27 times.
91 for (int i = 0; i < nb_words; i++) {
82 64 decale_save(&save, str, sep);
83 64 nb_letters = count_letters(str, sep, &save);
84 64 array[i] = malloc(sizeof(char) * (nb_letters + 1));
85 64 fill_str(array[i], nb_letters, save, str);
86 64 array[i][nb_letters] = '\0';
87 }
88 27 array[nb_words] = NULL;
89 27 return array;
90 }
91