Changed the score calculation for type ahead in the Tracker. Previously if there

was at least one character matching at the start of any file it would have
gotten a score >= 1. This rendered the substring matching completely useless
as soon as this happened, because it would always get a score < 1 depending on
the position of the occurance. Now substring matching is the first (and without
word mode, the only) score to get. Since the score depends on the position of
the match, this doesn't change anything for exact matches at the beginning, but
it does allow for substring matches even if there is a file starting with the
same letter as the search string. Also use strcasestr() instead of strstr() to
make the search case insensitive.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28233 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz 2008-10-18 20:12:09 +00:00
parent 3b839d857b
commit aa22e8db2e
1 changed files with 9 additions and 30 deletions

View File

@ -1589,35 +1589,20 @@ float
ComputeTypeAheadScore(const char *text, const char *match, size_t matchLength,
bool wordMode)
{
float first = -1;
float second = -1;
float third = -1;
// highest score: exact match
float score = 0;
size_t pos = 0;
for (; pos < matchLength; pos++) {
if (text[pos] == '\0') {
score = 0;
break;
}
if (tolower(text[pos]) != tolower(match[pos]))
break;
const char* found = strcasestr(text, match);
if (found != NULL) {
if (found == text)
return kExactMatchScore;
score++;
return 1.f / (found - text);
}
if (pos == matchLength) {
// we don't need to look any further
return kExactMatchScore;
}
first = score;
// there was no exact match
// second best: all characters at word beginnings
if (wordMode) {
score = 0;
float score = 0;
for (int32 j = 0, k = 0; match[j]; j++) {
while (text[k]
&& tolower(text[k]) != tolower(match[j])) {
@ -1642,17 +1627,11 @@ ComputeTypeAheadScore(const char *text, const char *match, size_t matchLength,
score += 1.f / (k + 1);
k++;
}
second = score;
return score;
}
// acceptable last: exact match inside the string
score = 0;
const char* found = strstr(text + 1, match);
if (found != NULL)
score = 1.f / (found - text);
third = score;
return max_c(first, max_c(second, third));
return -1;
}