From a184ec9cc326d3ced15b080d2d38ff8d2aed2a7b Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 7 Nov 2021 14:04:42 -0500 Subject: [PATCH 1/2] Improved heuristic to pay more attention to string length. --- src/api/system.c | 60 +++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/src/api/system.c b/src/api/system.c index bcd1b997..85d0db2a 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -591,56 +591,32 @@ static int f_exec(lua_State *L) { return 0; } - static int f_fuzzy_match(lua_State *L) { size_t strLen, ptnLen; const char *str = luaL_checklstring(L, 1, &strLen); const char *ptn = luaL_checklstring(L, 2, &ptnLen); - bool files = false; - 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 + // If true 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 // as the first result, rather than src/renderer.c. Clearly that's wrong. - if (files) { - const char* strEnd = str + strLen - 1; - const char* ptnEnd = ptn + ptnLen - 1; - while (strEnd >= str && ptnEnd >= ptn) { - while (*strEnd == ' ') { strEnd--; } - while (*ptnEnd == ' ') { ptnEnd--; } - if (tolower(*strEnd) == tolower(*ptnEnd)) { - score += run * 10 - (*strEnd != *ptnEnd); - run++; - ptnEnd--; - } else { - score -= 10; - run = 0; - } - strEnd--; + bool files = lua_gettop(L) > 2 && lua_isboolean(L,3) && lua_toboolean(L, 3); + int score = 0, run = 0, increment = files ? -1 : 1; + const char* strTarget = files ? str + strLen - 1 : str; + const char* ptnTarget = files ? ptn + ptnLen - 1 : ptn; + while (*strTarget && *ptnTarget) { + while (*strTarget == ' ') { strTarget += increment; } + while (*ptnTarget == ' ') { ptnTarget += increment; } + if (tolower(*strTarget) == tolower(*ptnTarget)) { + score += run * 10 - (*strTarget != *ptnTarget); + run++; + ptnTarget += increment; + } else { + score -= 10; + run = 0; } - if (ptnEnd >= ptn) { return 0; } - } else { - 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; } + strTarget += increment; } - - lua_pushnumber(L, score - (int)strLen); + if (*ptnTarget) { return 0; } + lua_pushnumber(L, score - (int)strLen * 10); return 1; } From 934f9c05d42255f604af9e969001056c1a9e8a86 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 7 Nov 2021 15:01:03 -0500 Subject: [PATCH 2/2] Screwed up checks. --- src/api/system.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/system.c b/src/api/system.c index 85d0db2a..c998fa05 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -602,9 +602,9 @@ static int f_fuzzy_match(lua_State *L) { int score = 0, run = 0, increment = files ? -1 : 1; const char* strTarget = files ? str + strLen - 1 : str; const char* ptnTarget = files ? ptn + ptnLen - 1 : ptn; - while (*strTarget && *ptnTarget) { - while (*strTarget == ' ') { strTarget += increment; } - while (*ptnTarget == ' ') { ptnTarget += increment; } + while (strTarget >= str && ptnTarget >= ptn && *strTarget && *ptnTarget) { + while (strTarget >= str && *strTarget == ' ') { strTarget += increment; } + while (ptnTarget >= ptn && *ptnTarget == ' ') { ptnTarget += increment; } if (tolower(*strTarget) == tolower(*ptnTarget)) { score += run * 10 - (*strTarget != *ptnTarget); run++; @@ -615,7 +615,7 @@ static int f_fuzzy_match(lua_State *L) { } strTarget += increment; } - if (*ptnTarget) { return 0; } + if (ptnTarget >= ptn && *ptnTarget) { return 0; } lua_pushnumber(L, score - (int)strLen * 10); return 1; }