From: Michael D. Lowis Date: Wed, 17 Apr 2019 13:04:53 +0000 (-0400) Subject: cleaned up match algorithm code X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=646e438177adcbd9b9abcf92edf6bc1c84c09568;p=proto%2Fpick.git cleaned up match algorithm code --- diff --git a/pick.c b/pick.c index 5e94549..d3e24bc 100644 --- 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; }