| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** my_str_to_word_array_string | ||
| 4 | ** File description: | ||
| 5 | ** Returns an array of words delimited by a separator (separator) | ||
| 6 | ** from a string (str) | ||
| 7 | */ | ||
| 8 | /** | ||
| 9 | * @file my_str_to_word_array_string.c | ||
| 10 | * @brief The file containing the my_str_to_word_array_string function | ||
| 11 | * @author Nicolas TORO | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "my.h" | ||
| 15 | |||
| 16 | 93 | static int my_char_is_not_a_separator(char const *str, char const *sep) | |
| 17 | { | ||
| 18 | 2/2✓ Branch 2 taken 13 times. ✓ Branch 3 taken 80 times. | 93 | if (my_strncmp(str, sep, my_strlen(sep)) == 0) | 
| 19 | 13 | return 0; | |
| 20 | 80 | return 1; | |
| 21 | } | ||
| 22 | |||
| 23 | 1 | static void check_words_and_num(char const *str, int *sep, int *word, | |
| 24 | char const *separator) | ||
| 25 | { | ||
| 26 | 2/2✓ Branch 0 taken 30 times. ✓ Branch 1 taken 1 times. | 31 | for (int i = 0; str[i] != '\0'; i++) { | 
| 27 | 4/4✓ Branch 1 taken 26 times. ✓ Branch 2 taken 4 times. ✓ Branch 3 taken 2 times. ✓ Branch 4 taken 24 times. | 30 | if (my_char_is_not_a_separator(str + i, separator) && *sep == 1) { | 
| 28 | 2 | *word = *word + 1; | |
| 29 | 2 | *sep = 0; | |
| 30 | } | ||
| 31 | 2/2✓ Branch 1 taken 4 times. ✓ Branch 2 taken 26 times. | 30 | if (my_char_is_not_a_separator(str + i, separator) == 0) | 
| 32 | 4 | *sep = 1; | |
| 33 | } | ||
| 34 | 1 | return; | |
| 35 | } | ||
| 36 | |||
| 37 | 2 | static void count_letter(char const *str, int which_lettre, int *nbr_lettre, | |
| 38 | char const *separator) | ||
| 39 | { | ||
| 40 | 2 | for (int i = which_lettre; str[i] != '\0' | |
| 41 | 4/4✓ Branch 0 taken 27 times. ✓ Branch 1 taken 1 times. ✓ Branch 3 taken 26 times. ✓ Branch 4 taken 1 times. | 28 | && my_char_is_not_a_separator(str + i, separator); i++) | 
| 42 | 26 | *nbr_lettre = *nbr_lettre + 1; | |
| 43 | 2 | } | |
| 44 | |||
| 45 | 2 | static void reset_nbr_letter(char const *str, | |
| 46 | int *which_lettre, int *nbr_lettre, char const *separator) | ||
| 47 | { | ||
| 48 | 2 | for (; str[*which_lettre] != '\0' | |
| 49 | 4/4✓ Branch 0 taken 3 times. ✓ Branch 1 taken 1 times. ✓ Branch 3 taken 2 times. ✓ Branch 4 taken 1 times. | 4 | && my_char_is_not_a_separator(str + *which_lettre, separator) == 0; | 
| 50 | 2 | *which_lettre = *which_lettre + my_strlen(separator)); | |
| 51 | 2 | *nbr_lettre = 0; | |
| 52 | 2 | } | |
| 53 | |||
| 54 | 1 | static char *nbr_sep(char *str, char const *separator) | |
| 55 | { | ||
| 56 | 1 | int nbr_sep = 0; | |
| 57 | |||
| 58 | 2/2✓ Branch 1 taken 2 times. ✓ Branch 2 taken 1 times. | 3 | for (int i = 0; my_char_is_not_a_separator(str + i, separator) == 0; i++) | 
| 59 | 2 | nbr_sep++; | |
| 60 | 1 | return &str[nbr_sep]; | |
| 61 | } | ||
| 62 | |||
| 63 | 1 | static char **initialise_value(char **string, char const *str, | |
| 64 | int **info, char const *separator) | ||
| 65 | { | ||
| 66 | char **array_of_word; | ||
| 67 | |||
| 68 | 1 | *string = nbr_sep(*string, separator); | |
| 69 | 1 | check_words_and_num(str, info[1], info[0], separator); | |
| 70 | 1 | array_of_word = malloc(sizeof(char *) * ((*(info[0])) + 1)); | |
| 71 | 1 | return array_of_word; | |
| 72 | } | ||
| 73 | |||
| 74 | 1 | char **my_str_to_word_array_string(char const *str, char const *separator) | |
| 75 | { | ||
| 76 | 1 | int word = 0; | |
| 77 | 1 | int sep = 1; | |
| 78 | 1 | int *info[2] = {&word, &sep}; | |
| 79 | 1 | int nbr_lettre = 0; | |
| 80 | 1 | int which_lettre = 0; | |
| 81 | 1 | char *string = (char *)str; | |
| 82 | 1 | char **array_of_word = initialise_value(&string, str, info, separator); | |
| 83 | |||
| 84 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 1 times. | 3 | for (int elem = 0; elem < word; elem++) { | 
| 85 | 2 | count_letter(string, which_lettre, &nbr_lettre, separator); | |
| 86 | 2 | array_of_word[elem] = malloc(sizeof(char) * (nbr_lettre + 1)); | |
| 87 | 2/2✓ Branch 0 taken 26 times. ✓ Branch 1 taken 2 times. | 28 | for (int chara = 0; chara < nbr_lettre; chara++) { | 
| 88 | 26 | array_of_word[elem][chara] = string[which_lettre]; | |
| 89 | 26 | which_lettre++; | |
| 90 | } | ||
| 91 | 2 | array_of_word[elem][nbr_lettre] = '\0'; | |
| 92 | 2 | reset_nbr_letter(string, &which_lettre, &nbr_lettre, separator); | |
| 93 | } | ||
| 94 | 1 | array_of_word[word] = NULL; | |
| 95 | 1 | return array_of_word; | |
| 96 | } | ||
| 97 |