diff options
| author | Ben Winston | 2024-05-28 20:33:58 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-28 20:33:58 -0400 |
| commit | 2465fef44a6aa503d774a40ccdf9ea2272c1e85a (patch) | |
| tree | 04be4396bf7e762b311c8f0fb9cf19d432b39d1e /booki.c | |
| parent | f46b627c10605694375323394fc5697bd895198f (diff) | |
update regex_match to follow the list
Diffstat (limited to 'booki.c')
| -rw-r--r-- | booki.c | 22 |
1 files changed, 14 insertions, 8 deletions
@@ -274,31 +274,37 @@ bool regex_match(const char* pattern, const struct es text) { // get lengths int pattern_length = strlen(pattern); - // TODO it won't always be +2, but that accounts for both specials - if (pattern_length > (text.len + 2)) return false; - bool head_match = *pattern == '^'; bool tail_match = *(pattern + pattern_length - 1) == '$'; // if we have either head or tail (or both), we only need to compare once + bool valid = false; if (head_match && tail_match) // text must be identical to pattern (minus ^ and $) - return text.len == (pattern_length - 2) && strncasecmp(pattern + 1, text.ptr, pattern_length - 2) == 0; + valid = text.len == (pattern_length - 2) && strncasecmp(pattern + 1, text.ptr, pattern_length - 2) == 0; else if (head_match) // text must match the pattern starting from pattern + 1 - return strncasecmp(pattern + 1, text.ptr, pattern_length - 1) == 0; + valid = strncasecmp(pattern + 1, text.ptr, pattern_length - 1) == 0; else if (tail_match) { // text starting from (pattern + 1) from the end must match pattern (without $) - return strncasecmp(pattern, text.ptr + (text.len - pattern_length + 1), pattern_length - 1) == 0; + valid = strncasecmp(pattern, text.ptr + (text.len - pattern_length + 1), pattern_length - 1) == 0; } // we only need to compare while remaining text is // as long or longer than pattern for (int i = 0; i <= (text.len - pattern_length); i++) { - if (strncasecmp(pattern, text.ptr + i, pattern_length) == 0) return true; + if (strncasecmp(pattern, text.ptr + i, pattern_length) == 0) { + valid = true; + break; + } } - return false; + if (valid) + return valid; + else if (text.next != NULL) + return regex_match(pattern, *(text.next)); + + return valid; } char* load_file(char* filename) { |
