}
}
-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 */
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;
}
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;