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);
}
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;
}