From 076d6899a65d05b8db146d5f8bb39874bd6f1ca5 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 17 Apr 2019 11:05:56 -0400 Subject: [PATCH] fixed regression by rolling back change to match function interface --- pick.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pick.c b/pick.c index 2d4b69e..d321686 100644 --- a/pick.c +++ b/pick.c @@ -50,7 +50,8 @@ static int by_score(const void* a, const void* b) { else if (ca->score > cb->score) return -1; else - return strcmp(ca->string, cb->string); + return 0; +// return strcmp(ca->string, cb->string); } static void load_choices(void) { @@ -70,17 +71,17 @@ static void load_choices(void) { } } -static char* find_char(char* str, int ch) { +static char* find_char(char *str, int ch) { for (; *str; str++) if (tolower(*str) == tolower(ch)) return str; return NULL; } -static inline bool find_match(char* choice, char* query, 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(choice, *query); + s = e = find_char(&string[offset], *query); if (s == NULL) return false; /* find the end of the match */ @@ -93,19 +94,20 @@ static inline bool find_match(char* choice, char* query, char **start, char **en return true; } -static bool match(char* choice, size_t *start, size_t *end) { - char *s1, *e1, *s2, *e2, *cstr = choice; +static bool match(char *string, size_t offset, size_t *start, size_t *end) { + char *s1 = 0, *e1 = 0, *s2, *e2; /* first check if we match at all */ - if (!find_match(cstr, Query, &s1, &e1)) + if (!find_match(string, Query, offset, &s1, &e1)) return false; + s2 = s1, e2 = e1; // Init s2 and e2 before use below /* next find the longest match. If multiple, take the left most one */ - while (find_match(++cstr, Query, &s1, &e1) && ((e1-s1) <= (e2-s2))) + while (find_match(string, Query, ++offset, &s1, &e1) && ((e1-s1) <= (e2-s2))) s1 = s2, e1 = e2; /* return the best match */ - *start = s1 - choice; - *end = e1 - choice; + *start = s1 - string; + *end = e1 - string; return true; } @@ -113,9 +115,8 @@ static void score(void) { for (unsigned int i = 0; i < vec_size(&Choices); i++) { Choice* choice = (Choice*)vec_at(&Choices, i); float qlen = (float)QueryIdx; - bool found = match(choice->string, &choice->match_start, &choice->match_end); - float clen = (float)(choice->match_end - choice->match_start); - if (found && clen > 0) { + if (match(choice->string, 0, &choice->match_start, &choice->match_end)) { + float clen = (float)(choice->match_end - choice->match_start) + 1; choice->score = qlen / clen / (float)(choice->length); } else { choice->match_start = 0; -- 2.51.0