aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--booki.c20
2 files changed, 19 insertions, 3 deletions
diff --git a/README.md b/README.md
index 0d103f7..69d710f 100644
--- a/README.md
+++ b/README.md
@@ -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:
diff --git a/booki.c b/booki.c
index 9a532fe..fc6468f 100644
--- a/booki.c
+++ b/booki.c
@@ -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;
}