|
| 1 | +#include "main.h" |
| 2 | + |
| 3 | +int strlen_no_wilds(char *str); |
| 4 | +void iterate_wild(char **wildstr); |
| 5 | +char *postfix_match(char *str, char *postfix); |
| 6 | +int wildcmp(char *s1, char *s2); |
| 7 | + |
| 8 | +/** |
| 9 | + * strlen_no_wilds - Returns the length of a string, |
| 10 | + * ignoring wildcard characters. |
| 11 | + * @str: The string to be measured. |
| 12 | + * |
| 13 | + * Return: The length. |
| 14 | + */ |
| 15 | +int strlen_no_wilds(char *str) |
| 16 | +{ |
| 17 | + int len = 0, index = 0; |
| 18 | + |
| 19 | + if (*(str + index)) |
| 20 | + { |
| 21 | + if (*str != '*') |
| 22 | + len++; |
| 23 | + |
| 24 | + index++; |
| 25 | + len += strlen_no_wilds(str + index); |
| 26 | + } |
| 27 | + |
| 28 | + return (len); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * iterate_wild - Iterates through a string located at a wildcard |
| 33 | + * until it points to a non-wildcard character. |
| 34 | + * @wildstr: The string to be iterated through. |
| 35 | + */ |
| 36 | +void iterate_wild(char **wildstr) |
| 37 | +{ |
| 38 | + if (**wildstr == '*') |
| 39 | + { |
| 40 | + (*wildstr)++; |
| 41 | + iterate_wild(wildstr); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * postfix_match - Checks if a string str matches the postfix of |
| 47 | + * another string potentially containing wildcards. |
| 48 | + * @str: The string to be matched. |
| 49 | + * @postfix: The postfix. |
| 50 | + * |
| 51 | + * Return: If str and postfix are identical - a pointer to the null byte |
| 52 | + * located at the end of postfix. |
| 53 | + * Otherwise - a pointer to the first unmatched character in postfix. |
| 54 | + */ |
| 55 | +char *postfix_match(char *str, char *postfix) |
| 56 | +{ |
| 57 | + int str_len = strlen_no_wilds(str) - 1; |
| 58 | + int postfix_len = strlen_no_wilds(postfix) - 1; |
| 59 | + |
| 60 | + if (*postfix == '*') |
| 61 | + iterate_wild(&postfix); |
| 62 | + |
| 63 | + if (*(str + str_len - postfix_len) == *postfix && *postfix != '\0') |
| 64 | + { |
| 65 | + postfix++; |
| 66 | + return (postfix_match(str, postfix)); |
| 67 | + } |
| 68 | + |
| 69 | + return (postfix); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * wildcmp - Compares two strings, considering wildcard characters. |
| 74 | + * @s1: The first string to be compared. |
| 75 | + * @s2: The second string to be compared - may contain wildcards. |
| 76 | + * |
| 77 | + * Return: If the strings can be considered identical - 1. |
| 78 | + * Otherwise - 0. |
| 79 | + */ |
| 80 | +int wildcmp(char *s1, char *s2) |
| 81 | +{ |
| 82 | + if (*s2 == '*') |
| 83 | + { |
| 84 | + iterate_wild(&s2); |
| 85 | + s2 = postfix_match(s1, s2); |
| 86 | + } |
| 87 | + |
| 88 | + if (*s2 == '\0') |
| 89 | + return (1); |
| 90 | + |
| 91 | + if (*s1 != *s2) |
| 92 | + return (0); |
| 93 | + |
| 94 | + return (wildcmp(++s1, ++s2)); |
| 95 | +} |
0 commit comments