| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** my_str_to_word_array_select | ||
| 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_str_to_word_array_select.c | ||
| 10 | * @brief The file containing the my_str_to_word_array_select function | ||
| 11 | * @author Nicolas TORO | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "my.h" | ||
| 15 | |||
| 16 | 241715 | static int my_char_is_not_a_separator(char const c, char const *sep) | |
| 17 | { | ||
| 18 |
2/2✓ Branch 0 taken 264601 times.
✓ Branch 1 taken 216495 times.
|
481096 | for (int i = 0; sep[i] != '\0'; i++) { |
| 19 |
2/2✓ Branch 0 taken 25220 times.
✓ Branch 1 taken 239381 times.
|
264601 | if (sep[i] == c) |
| 20 | 25220 | return 0; | |
| 21 | } | ||
| 22 | 216495 | return 1; | |
| 23 | } | ||
| 24 | |||
| 25 | 1558 | static void check_words_and_num(char const *str, int *sep, int *word, | |
| 26 | char const *separator) | ||
| 27 | { | ||
| 28 |
2/2✓ Branch 0 taken 76984 times.
✓ Branch 1 taken 1558 times.
|
78542 | for (int i = 0; str[i] != '\0'; i++) { |
| 29 |
4/4✓ Branch 1 taken 70174 times.
✓ Branch 2 taken 6810 times.
✓ Branch 3 taken 5854 times.
✓ Branch 4 taken 64320 times.
|
76984 | if (my_char_is_not_a_separator(str[i], separator) && *sep == 1) { |
| 30 | 5854 | *word = *word + 1; | |
| 31 | 5854 | *sep = 0; | |
| 32 | } | ||
| 33 |
2/2✓ Branch 1 taken 6810 times.
✓ Branch 2 taken 70174 times.
|
76984 | if (my_char_is_not_a_separator(str[i], separator) == 0) |
| 34 | 6810 | *sep = 1; | |
| 35 | } | ||
| 36 | 1558 | return; | |
| 37 | } | ||
| 38 | |||
| 39 | 5854 | static void count_letter(char const *str, int which_lettre, int *nbr_lettre, | |
| 40 | char const *separator) | ||
| 41 | { | ||
| 42 | 5854 | for (int i = which_lettre; str[i] != '\0' | |
| 43 |
4/4✓ Branch 0 taken 74964 times.
✓ Branch 1 taken 1064 times.
✓ Branch 3 taken 70174 times.
✓ Branch 4 taken 4790 times.
|
76028 | && my_char_is_not_a_separator(str[i], separator); i++) |
| 44 | 70174 | *nbr_lettre = *nbr_lettre + 1; | |
| 45 | 5854 | } | |
| 46 | |||
| 47 | 5854 | static void reset_nbr_letter(char const *str, | |
| 48 | int *which_lettre, int *nbr_lettre, char const *separator) | ||
| 49 | { | ||
| 50 | 5854 | for (; str[*which_lettre] != '\0' | |
| 51 |
4/4✓ Branch 0 taken 11221 times.
✓ Branch 1 taken 1439 times.
✓ Branch 3 taken 6806 times.
✓ Branch 4 taken 4415 times.
|
12660 | && my_char_is_not_a_separator(str[*which_lettre], separator) == 0; |
| 52 | 6806 | *which_lettre = *which_lettre + 1); | |
| 53 | 5854 | *nbr_lettre = 0; | |
| 54 | 5854 | } | |
| 55 | |||
| 56 | 1558 | static char *nbr_sep(char *str, char const *separator) | |
| 57 | { | ||
| 58 | 1558 | int nbr_sep = 0; | |
| 59 | |||
| 60 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1558 times.
|
1562 | for (int i = 0; my_char_is_not_a_separator(str[i], separator) == 0; i++) |
| 61 | 4 | nbr_sep++; | |
| 62 | 1558 | return &str[nbr_sep]; | |
| 63 | } | ||
| 64 | |||
| 65 | 1558 | static char **initialise_value(char **string, char const *str, | |
| 66 | int **info, char const *separator) | ||
| 67 | { | ||
| 68 | char **array_of_word; | ||
| 69 | |||
| 70 | 1558 | *string = nbr_sep(*string, separator); | |
| 71 | 1558 | check_words_and_num(str, info[1], info[0], separator); | |
| 72 | 1558 | array_of_word = malloc(sizeof(char *) * ((*(info[0])) + 1)); | |
| 73 | 1558 | return array_of_word; | |
| 74 | } | ||
| 75 | |||
| 76 | 1558 | char **my_str_to_word_array_select(char const *str, char const *separator) | |
| 77 | { | ||
| 78 | 1558 | int word = 0; | |
| 79 | 1558 | int sep = 1; | |
| 80 | 1558 | int *info[2] = {&word, &sep}; | |
| 81 | 1558 | int nbr_lettre = 0; | |
| 82 | 1558 | int which_lettre = 0; | |
| 83 | 1558 | char *string = (char *)str; | |
| 84 | 1558 | char **array_of_word = initialise_value(&string, str, info, separator); | |
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 5854 times.
✓ Branch 1 taken 1558 times.
|
7412 | for (int elem = 0; elem < word; elem++) { |
| 87 | 5854 | count_letter(string, which_lettre, &nbr_lettre, separator); | |
| 88 | 5854 | array_of_word[elem] = malloc(sizeof(char) * (nbr_lettre + 1)); | |
| 89 |
2/2✓ Branch 0 taken 70174 times.
✓ Branch 1 taken 5854 times.
|
76028 | for (int chara = 0; chara < nbr_lettre; chara++) { |
| 90 | 70174 | array_of_word[elem][chara] = string[which_lettre]; | |
| 91 | 70174 | which_lettre++; | |
| 92 | } | ||
| 93 | 5854 | array_of_word[elem][nbr_lettre] = '\0'; | |
| 94 | 5854 | reset_nbr_letter(string, &which_lettre, &nbr_lettre, separator); | |
| 95 | } | ||
| 96 | 1558 | array_of_word[word] = NULL; | |
| 97 | 1558 | return array_of_word; | |
| 98 | } | ||
| 99 |