]> git.mdlowis.com Git - proto/pick.git/commitdiff
cleaned up match algorithm code
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 17 Apr 2019 13:04:53 +0000 (09:04 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 17 Apr 2019 13:04:53 +0000 (09:04 -0400)
pick.c

diff --git a/pick.c b/pick.c
index 5e9454916c63afbe0a5deeb05d78069f6b5a68c7..d3e24bc3b510b14cee4be33bc4e40921f0cb0023 100644 (file)
--- a/pick.c
+++ b/pick.c
@@ -77,7 +77,7 @@ static char* find_char(char *str, int ch) {
     return NULL;
 }
 
-static bool find_match(char *string, char* query, size_t offset, char **start, char **end) {
+static inline bool find_match(char *string, char* query, size_t offset, char **start, char **end) {
     char *s, *e;
     /* find first match char or bail */
     s = e = find_char(&string[offset], *query);
@@ -94,17 +94,18 @@ static bool find_match(char *string, char* query, size_t offset, char **start, c
 }
 
 static bool match(char *string, size_t offset, size_t *start, size_t *end) {
-    char *s, *e;
-    if (!find_match(string, Query, offset, &s, &e))
+    char *s1, *e1, *s2, *e2;
+    /* first check if we match at all */
+    if (!find_match(string, Query, offset, &s1, &e1))
         return false;
-    *start = s - string;
-    *end   = e - string;
 
-    /* Less than or equal is used in order to obtain the left-most match. */
-    if (match(string, offset + 1, start, end) && (size_t)(e - s) <= *end - *start) {
-        *start = s - string;
-        *end   = e - string;
-    }
+    /* next find the longest match. If multiple, take the left most one */
+    while (find_match(string, Query, ++offset, &s1, &e1) && ((e1-s1) <= (e2-s2)))
+        s1 = s2, e1 = e2;
+
+    /* return the best match */
+    *start = s1 - string;
+    *end   = e1 - string;
     return true;
 }