| 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 | 280998 | static int my_char_is_not_a_separator(char const c, char const *sep) | |
| 17 | { | ||
| 18 |
2/2✓ Branch 0 taken 303351 times.
✓ Branch 1 taken 251586 times.
|
554937 | for (int i = 0; sep[i] != '\0'; i++) { |
| 19 |
2/2✓ Branch 0 taken 29412 times.
✓ Branch 1 taken 273939 times.
|
303351 | if (sep[i] == c) |
| 20 | 29412 | return 0; | |
| 21 | } | ||
| 22 | 251586 | return 1; | |
| 23 | } | ||
| 24 | |||
| 25 | 1636 | static void check_words_and_num(char const *str, int *sep, int *word, | |
| 26 | char const *separator) | ||
| 27 | { | ||
| 28 |
2/2✓ Branch 0 taken 89349 times.
✓ Branch 1 taken 1636 times.
|
90985 | for (int i = 0; str[i] != '\0'; i++) { |
| 29 |
4/4✓ Branch 1 taken 81495 times.
✓ Branch 2 taken 7854 times.
✓ Branch 3 taken 6980 times.
✓ Branch 4 taken 74515 times.
|
89349 | if (my_char_is_not_a_separator(str[i], separator) && *sep == 1) { |
| 30 | 6980 | *word = *word + 1; | |
| 31 | 6980 | *sep = 0; | |
| 32 | } | ||
| 33 |
2/2✓ Branch 1 taken 7854 times.
✓ Branch 2 taken 81495 times.
|
89349 | if (my_char_is_not_a_separator(str[i], separator) == 0) |
| 34 | 7854 | *sep = 1; | |
| 35 | } | ||
| 36 | 1636 | return; | |
| 37 | } | ||
| 38 | |||
| 39 | 6980 | static void count_letter(char const *str, int which_lettre, int *nbr_lettre, | |
| 40 | char const *separator) | ||
| 41 | { | ||
| 42 | 6980 | for (int i = which_lettre; str[i] != '\0' | |
| 43 |
4/4✓ Branch 0 taken 87345 times.
✓ Branch 1 taken 1130 times.
✓ Branch 3 taken 81495 times.
✓ Branch 4 taken 5850 times.
|
88475 | && my_char_is_not_a_separator(str[i], separator); i++) |
| 44 | 81495 | *nbr_lettre = *nbr_lettre + 1; | |
| 45 | 6980 | } | |
| 46 | |||
| 47 | 6980 | static void reset_nbr_letter(char const *str, | |
| 48 | int *which_lettre, int *nbr_lettre, char const *separator) | ||
| 49 | { | ||
| 50 | 6980 | for (; str[*which_lettre] != '\0' | |
| 51 |
4/4✓ Branch 0 taken 13315 times.
✓ Branch 1 taken 1515 times.
✓ Branch 3 taken 7850 times.
✓ Branch 4 taken 5465 times.
|
14830 | && my_char_is_not_a_separator(str[*which_lettre], separator) == 0; |
| 52 | 7850 | *which_lettre = *which_lettre + 1); | |
| 53 | 6980 | *nbr_lettre = 0; | |
| 54 | 6980 | } | |
| 55 | |||
| 56 | 1636 | static char *nbr_sep(char *str, char const *separator) | |
| 57 | { | ||
| 58 | 1636 | int nbr_sep = 0; | |
| 59 | |||
| 60 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1636 times.
|
1640 | for (int i = 0; my_char_is_not_a_separator(str[i], separator) == 0; i++) |
| 61 | 4 | nbr_sep++; | |
| 62 | 1636 | return &str[nbr_sep]; | |
| 63 | } | ||
| 64 | |||
| 65 | 1636 | static char **initialise_value(char **string, char const *str, | |
| 66 | int **info, char const *separator) | ||
| 67 | { | ||
| 68 | char **array_of_word; | ||
| 69 | |||
| 70 | 1636 | *string = nbr_sep(*string, separator); | |
| 71 | 1636 | check_words_and_num(str, info[1], info[0], separator); | |
| 72 | 1636 | array_of_word = malloc(sizeof(char *) * ((*(info[0])) + 1)); | |
| 73 | 1636 | return array_of_word; | |
| 74 | } | ||
| 75 | |||
| 76 | 1636 | char **my_str_to_word_array_select(char const *str, char const *separator) | |
| 77 | { | ||
| 78 | 1636 | int word = 0; | |
| 79 | 1636 | int sep = 1; | |
| 80 | 1636 | int *info[2] = {&word, &sep}; | |
| 81 | 1636 | int nbr_lettre = 0; | |
| 82 | 1636 | int which_lettre = 0; | |
| 83 | 1636 | char *string = (char *)str; | |
| 84 | 1636 | char **array_of_word = initialise_value(&string, str, info, separator); | |
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 6980 times.
✓ Branch 1 taken 1636 times.
|
8616 | for (int elem = 0; elem < word; elem++) { |
| 87 | 6980 | count_letter(string, which_lettre, &nbr_lettre, separator); | |
| 88 | 6980 | array_of_word[elem] = malloc(sizeof(char) * (nbr_lettre + 1)); | |
| 89 |
2/2✓ Branch 0 taken 81495 times.
✓ Branch 1 taken 6980 times.
|
88475 | for (int chara = 0; chara < nbr_lettre; chara++) { |
| 90 | 81495 | array_of_word[elem][chara] = string[which_lettre]; | |
| 91 | 81495 | which_lettre++; | |
| 92 | } | ||
| 93 | 6980 | array_of_word[elem][nbr_lettre] = '\0'; | |
| 94 | 6980 | reset_nbr_letter(string, &which_lettre, &nbr_lettre, separator); | |
| 95 | } | ||
| 96 | 1636 | array_of_word[word] = NULL; | |
| 97 | 1636 | return array_of_word; | |
| 98 | } | ||
| 99 |