diff options
| author | Ben Winston | 2024-05-26 21:03:18 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-26 21:03:18 -0400 |
| commit | ea977ce9907814cad0aa6986a539d29d56fe2bf7 (patch) | |
| tree | d629420452b200d6d0129a56bb13c50c87965a7b | |
| parent | 0779d2f02472c39a6473d9b224681ff9db5e4fc2 (diff) | |
add special characters to regex search
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | booki.c | 20 |
2 files changed, 19 insertions, 3 deletions
@@ -23,7 +23,7 @@ Things that don't work: - [ ] `search` with more than 5 query items - [ ] searching for a single author in a multi-author item - [x] ~~printing any array thing~~ `on` works, but others don't - - [ ] `^` or `$` in string searching + - [x] ~~`^` or `$` in string searching~~ should work - [ ] sorting in any way Things that might change: @@ -242,10 +242,26 @@ bool regex_match(const char* pattern, const struct es text) { // get lengths int pattern_length = strlen(pattern); - if (pattern_length > text.len) return false; + // 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 + 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; + else if (head_match) + // text must match the pattern starting from pattern + 1 + return 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; + } // we only need to compare while remaining text is - // as long or longer than pattern (without special) + // 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; } |
