]> git.mdlowis.com Git - proto/pick.git/commitdiff
fixed a divide by 0
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 17 Apr 2019 13:10:00 +0000 (09:10 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 17 Apr 2019 13:10:00 +0000 (09:10 -0400)
pick.c

diff --git a/pick.c b/pick.c
index d3e24bc3b510b14cee4be33bc4e40921f0cb0023..2d4b69e9ff9c36349665a8baf5e8851e19d669e1 100644 (file)
--- 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(charstr, 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;