Merge pull request #660 from adamharrison/improved-heuristic
Improved heuristic to pay more attention to string length.
This commit is contained in:
commit
0db3648966
|
@ -591,56 +591,32 @@ static int f_exec(lua_State *L) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int f_fuzzy_match(lua_State *L) {
|
static int f_fuzzy_match(lua_State *L) {
|
||||||
size_t strLen, ptnLen;
|
size_t strLen, ptnLen;
|
||||||
const char *str = luaL_checklstring(L, 1, &strLen);
|
const char *str = luaL_checklstring(L, 1, &strLen);
|
||||||
const char *ptn = luaL_checklstring(L, 2, &ptnLen);
|
const char *ptn = luaL_checklstring(L, 2, &ptnLen);
|
||||||
bool files = false;
|
// If true match things *backwards*. This allows for better matching on filenames than the above
|
||||||
if (lua_gettop(L) > 2 && lua_isboolean(L,3))
|
|
||||||
files = lua_toboolean(L, 3);
|
|
||||||
|
|
||||||
int score = 0;
|
|
||||||
int run = 0;
|
|
||||||
|
|
||||||
// Match things *backwards*. This allows for better matching on filenames than the above
|
|
||||||
// function. For example, in the lite project, opening "renderer" has lib/font_render/build.sh
|
// function. For example, in the lite project, opening "renderer" has lib/font_render/build.sh
|
||||||
// as the first result, rather than src/renderer.c. Clearly that's wrong.
|
// as the first result, rather than src/renderer.c. Clearly that's wrong.
|
||||||
if (files) {
|
bool files = lua_gettop(L) > 2 && lua_isboolean(L,3) && lua_toboolean(L, 3);
|
||||||
const char* strEnd = str + strLen - 1;
|
int score = 0, run = 0, increment = files ? -1 : 1;
|
||||||
const char* ptnEnd = ptn + ptnLen - 1;
|
const char* strTarget = files ? str + strLen - 1 : str;
|
||||||
while (strEnd >= str && ptnEnd >= ptn) {
|
const char* ptnTarget = files ? ptn + ptnLen - 1 : ptn;
|
||||||
while (*strEnd == ' ') { strEnd--; }
|
while (strTarget >= str && ptnTarget >= ptn && *strTarget && *ptnTarget) {
|
||||||
while (*ptnEnd == ' ') { ptnEnd--; }
|
while (strTarget >= str && *strTarget == ' ') { strTarget += increment; }
|
||||||
if (tolower(*strEnd) == tolower(*ptnEnd)) {
|
while (ptnTarget >= ptn && *ptnTarget == ' ') { ptnTarget += increment; }
|
||||||
score += run * 10 - (*strEnd != *ptnEnd);
|
if (tolower(*strTarget) == tolower(*ptnTarget)) {
|
||||||
|
score += run * 10 - (*strTarget != *ptnTarget);
|
||||||
run++;
|
run++;
|
||||||
ptnEnd--;
|
ptnTarget += increment;
|
||||||
} else {
|
} else {
|
||||||
score -= 10;
|
score -= 10;
|
||||||
run = 0;
|
run = 0;
|
||||||
}
|
}
|
||||||
strEnd--;
|
strTarget += increment;
|
||||||
}
|
}
|
||||||
if (ptnEnd >= ptn) { return 0; }
|
if (ptnTarget >= ptn && *ptnTarget) { return 0; }
|
||||||
} else {
|
lua_pushnumber(L, score - (int)strLen * 10);
|
||||||
while (*str && *ptn) {
|
|
||||||
while (*str == ' ') { str++; }
|
|
||||||
while (*ptn == ' ') { ptn++; }
|
|
||||||
if (tolower(*str) == tolower(*ptn)) {
|
|
||||||
score += run * 10 - (*str != *ptn);
|
|
||||||
run++;
|
|
||||||
ptn++;
|
|
||||||
} else {
|
|
||||||
score -= 10;
|
|
||||||
run = 0;
|
|
||||||
}
|
|
||||||
str++;
|
|
||||||
}
|
|
||||||
if (*ptn) { return 0; }
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pushnumber(L, score - (int)strLen);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue