From 2211f72a60988241e20522525bd0ce46a4f61eb6 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 17 Apr 2019 09:10:00 -0400 Subject: [PATCH] fixed a divide by 0 --- pick.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/pick.c b/pick.c index d3e24bc..2d4b69e 100644 --- a/pick.c +++ b/pick.c @@ -70,17 +70,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 *string, char* query, size_t offset, char **start, char **end) { +static inline bool find_match(char* choice, char* query, char **start, char **end) { char *s, *e; /* find first match char or bail */ - s = e = find_char(&string[offset], *query); + s = e = find_char(choice, *query); if (s == NULL) return false; /* find the end of the match */ @@ -93,19 +93,19 @@ static inline bool find_match(char *string, char* query, size_t offset, char **s return true; } -static bool match(char *string, size_t offset, size_t *start, size_t *end) { - char *s1, *e1, *s2, *e2; +static bool match(char* choice, size_t *start, size_t *end) { + char *s1, *e1, *s2, *e2, *cstr = choice; /* first check if we match at all */ - if (!find_match(string, Query, offset, &s1, &e1)) + if (!find_match(cstr, Query, &s1, &e1)) return false; /* next find the longest match. If multiple, take the left most one */ - while (find_match(string, Query, ++offset, &s1, &e1) && ((e1-s1) <= (e2-s2))) + while (find_match(++cstr, Query, &s1, &e1) && ((e1-s1) <= (e2-s2))) s1 = s2, e1 = e2; /* return the best match */ - *start = s1 - string; - *end = e1 - string; + *start = s1 - choice; + *end = e1 - choice; return true; } @@ -113,8 +113,9 @@ 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; - if (match(choice->string, 0, &choice->match_start, &choice->match_end)) { - float clen = (float)(choice->match_end - choice->match_start); + bool found = match(choice->string, &choice->match_start, &choice->match_end); + float clen = (float)(choice->match_end - choice->match_start); + if (found && clen > 0) { choice->score = qlen / clen / (float)(choice->length); } else { choice->match_start = 0; -- 2.52.0