]> git.mdlowis.com Git - proto/pick.git/commitdiff
fixed regression by rolling back change to match function interface
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 17 Apr 2019 15:05:56 +0000 (11:05 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 17 Apr 2019 15:05:56 +0000 (11:05 -0400)
pick.c

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