From c0802ae674e07a8ecd26f6417dddb054629b8f26 Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Mon, 27 Jan 2025 17:07:21 +0100 Subject: [PATCH] fzy: replace fzy.lua with the C version fzy.lua was added because it is less code than the C version, despite being slower. But it makes a visible difference in the new history selector, even if you don't let the history grow that huge, because it has long lines with full paths. This makes it slower than the property selector with 12k items even with fewer history entries than that, because properties are shorter strings. Even on new a CPU it causes visible lag on the first character typed with 10k history entries. With something like 100k entries it is totally unusable. Using the C version completely fixes performance. It also visibly speeds up the property list, especially on old hardware. When typing the first character in it on a 2013 CPU, Lua 5.1 fzy takes 300ms, Luajit fzy takes 130ms, while C fzy takes 10ms. It can also help with loadfile autocompletion in very large directories, or arbitrary user scripts using huge haystacks. Since this is just a copy pasted library we don't have to update and not much bigger than the Lua version, there is no reason to give up the extra performance. We copy pasted osdep/dirent-win.h which is twice as big, though more self-contained. This C version can also be exposed to Javascript too, if someone wants that. Or it was suggested to use it for --sub-auto=fuzzy. --- fzy/bonus.h | 153 +++++++++++++++++++ fzy/match.c | 329 +++++++++++++++++++++++++++++++++++++++++ fzy/match.h | 81 ++++++++++ meson.build | 2 +- player/lua.c | 85 ++++++++++- player/lua/fzy.lua | 297 ------------------------------------- player/lua/meson.build | 2 +- 7 files changed, 647 insertions(+), 302 deletions(-) create mode 100644 fzy/bonus.h create mode 100644 fzy/match.c create mode 100644 fzy/match.h delete mode 100644 player/lua/fzy.lua diff --git a/fzy/bonus.h b/fzy/bonus.h new file mode 100644 index 0000000000000..083a2b5d80f0b --- /dev/null +++ b/fzy/bonus.h @@ -0,0 +1,153 @@ +/* The MIT License (MIT) +Copyright (c) 2020 Seth Warn +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +/* bonus.h + * precomputed scoring for the fzy algorithm + * + * original code by John Hawthorn, https://github.com/jhawthorn/fzy + * modifications by + * Rom Grk, https://github.com/romgrk + * Seth Warn, https://github.com/swarn + */ + +#ifndef BONUS_H +#define BONUS_H + +#include "match.h" + + +#define SCORE_GAP_LEADING (-0.005) +#define SCORE_GAP_TRAILING (-0.005) +#define SCORE_GAP_INNER (-0.01) +#define SCORE_MATCH_CONSECUTIVE (1.0) +#define SCORE_MATCH_SLASH (0.9) +#define SCORE_MATCH_WORD (0.8) +#define SCORE_MATCH_CAPITAL (0.7) +#define SCORE_MATCH_DOT (0.6) + +// clang-format off +#define ASSIGN_LOWER(v) \ + ['a'] = (v), \ + ['b'] = (v), \ + ['c'] = (v), \ + ['d'] = (v), \ + ['e'] = (v), \ + ['f'] = (v), \ + ['g'] = (v), \ + ['h'] = (v), \ + ['i'] = (v), \ + ['j'] = (v), \ + ['k'] = (v), \ + ['l'] = (v), \ + ['m'] = (v), \ + ['n'] = (v), \ + ['o'] = (v), \ + ['p'] = (v), \ + ['q'] = (v), \ + ['r'] = (v), \ + ['s'] = (v), \ + ['t'] = (v), \ + ['u'] = (v), \ + ['v'] = (v), \ + ['w'] = (v), \ + ['x'] = (v), \ + ['y'] = (v), \ + ['z'] = (v) + +#define ASSIGN_UPPER(v) \ + ['A'] = (v), \ + ['B'] = (v), \ + ['C'] = (v), \ + ['D'] = (v), \ + ['E'] = (v), \ + ['F'] = (v), \ + ['G'] = (v), \ + ['H'] = (v), \ + ['I'] = (v), \ + ['J'] = (v), \ + ['K'] = (v), \ + ['L'] = (v), \ + ['M'] = (v), \ + ['N'] = (v), \ + ['O'] = (v), \ + ['P'] = (v), \ + ['Q'] = (v), \ + ['R'] = (v), \ + ['S'] = (v), \ + ['T'] = (v), \ + ['U'] = (v), \ + ['V'] = (v), \ + ['W'] = (v), \ + ['X'] = (v), \ + ['Y'] = (v), \ + ['Z'] = (v) + +#define ASSIGN_DIGIT(v) \ + ['0'] = (v), \ + ['1'] = (v), \ + ['2'] = (v), \ + ['3'] = (v), \ + ['4'] = (v), \ + ['5'] = (v), \ + ['6'] = (v), \ + ['7'] = (v), \ + ['8'] = (v), \ + ['9'] = (v) + +static const score_t bonus_states[3][256] = { + { 0 }, + { + ['/'] = SCORE_MATCH_SLASH, + ['\\'] = SCORE_MATCH_SLASH, + ['-'] = SCORE_MATCH_WORD, + ['_'] = SCORE_MATCH_WORD, + [' '] = SCORE_MATCH_WORD, + ['.'] = SCORE_MATCH_DOT, + }, + { + ['/'] = SCORE_MATCH_SLASH, + ['\\'] = SCORE_MATCH_SLASH, + ['-'] = SCORE_MATCH_WORD, + ['_'] = SCORE_MATCH_WORD, + [' '] = SCORE_MATCH_WORD, + ['.'] = SCORE_MATCH_DOT, + + /* ['a' ... 'z'] = SCORE_MATCH_CAPITAL, */ + ASSIGN_LOWER(SCORE_MATCH_CAPITAL) + } +}; + +static const index_t bonus_index[256] = { + /* ['A' ... 'Z'] = 2 */ + ASSIGN_UPPER(2), + + /* ['a' ... 'z'] = 1 */ + ASSIGN_LOWER(1), + + /* ['0' ... '9'] = 1 */ + ASSIGN_DIGIT(1) +}; + +// clang-format on + +#define COMPUTE_BONUS(last_ch, ch) \ + (bonus_states[bonus_index[(unsigned char)(ch)]][(unsigned char)(last_ch)]) + +#endif + diff --git a/fzy/match.c b/fzy/match.c new file mode 100644 index 0000000000000..971bc67841fa0 --- /dev/null +++ b/fzy/match.c @@ -0,0 +1,329 @@ +/* The MIT License (MIT) +Copyright (c) 2020 Seth Warn +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +/* match.c + * C implementation of fzy matching + * + * original code by John Hawthorn, https://github.com/jhawthorn/fzy + * modifications by + * Rom Grk, https://github.com/romgrk + * Seth Warn, https://github.com/swarn + */ + +#include "match.h" + +#include +#include +#include + +#include "bonus.h" + + +int has_match(char const * needle, char const * haystack, int case_sensitive) +{ + char needle_lower[MATCH_MAX_LEN + 1]; + char haystack_lower[MATCH_MAX_LEN + 1]; + + if (! case_sensitive) + { + int const n = (int)strlen(needle); + int const m = (int)strlen(haystack); + + for (int i = 0; i < n; i++) + needle_lower[i] = (char)tolower(needle[i]); + for (int i = 0; i < m; i++) + haystack_lower[i] = (char)tolower(haystack[i]); + + needle_lower[n] = 0; + haystack_lower[m] = 0; + + needle = needle_lower; + haystack = haystack_lower; + } + + while (*needle) + { + haystack = strchr(haystack, *needle++); + if (! haystack) + return 0; + + haystack++; + } + return 1; +} + + +#define SWAP(x, y, T) \ + do \ + { \ + T SWAP = x; \ + (x) = y; \ + (y) = SWAP; \ + } while (0) + +#define max(a, b) (((a) > (b)) ? (a) : (b)) + + +struct match_struct +{ + int needle_len; + int haystack_len; + + char const * needle; + char const * haystack; + + char lower_needle[MATCH_MAX_LEN]; + char lower_haystack[MATCH_MAX_LEN]; + + score_t match_bonus[MATCH_MAX_LEN]; +}; + + +static void precompute_bonus(char const * haystack, score_t * match_bonus) +{ + /* Which positions are beginning of words */ + char last_ch = '/'; + for (int i = 0; haystack[i]; i++) + { + char ch = haystack[i]; + match_bonus[i] = COMPUTE_BONUS(last_ch, ch); + last_ch = ch; + } +} + +static void setup_match_struct( + struct match_struct * match, + char const * needle, + char const * haystack, + int is_case_sensitive) +{ + match->needle_len = (int)strlen(needle); + match->haystack_len = (int)strlen(haystack); + + if (match->haystack_len > MATCH_MAX_LEN || match->needle_len > match->haystack_len) + { + return; + } + + if (is_case_sensitive) + { + match->needle = needle; + match->haystack = haystack; + } + else + { + for (int i = 0; i < match->needle_len; i++) + match->lower_needle[i] = (char)tolower(needle[i]); + + for (int i = 0; i < match->haystack_len; i++) + match->lower_haystack[i] = (char)tolower(haystack[i]); + + match->needle = match->lower_needle; + match->haystack = match->lower_haystack; + } + + precompute_bonus(haystack, match->match_bonus); +} + +static inline void match_row( + struct match_struct const * match, + int row, + score_t * curr_D, + score_t * curr_M, + score_t const * last_D, + score_t const * last_M) +{ + unsigned n = match->needle_len; + unsigned m = match->haystack_len; + int i = row; + + char const * needle = match->needle; + char const * haystack = match->haystack; + score_t const * match_bonus = match->match_bonus; + + score_t prev_score = SCORE_MIN; + score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER; + + for (int j = 0; j < m; j++) + { + if (needle[i] == haystack[j]) + { + score_t score = SCORE_MIN; + if (i == 0) + { + // The match_bonus values are computed out to the length of the + // haystack in precompute_bonus. The index j is less than m, + // the length of the haystack. So the "garbage value" warning + // here is false. + // NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) + score = (j * SCORE_GAP_LEADING) + match_bonus[j]; + } + else if (j) + { + /* i > 0 && j > 0*/ + score = + // NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) + max(last_M[j - 1] + match_bonus[j], + + /* consecutive match, doesn't stack with match_bonus */ + last_D[j - 1] + SCORE_MATCH_CONSECUTIVE); + } + curr_D[j] = score; + curr_M[j] = prev_score = max(score, prev_score + gap_score); + } + else + { + curr_D[j] = SCORE_MIN; + curr_M[j] = prev_score = prev_score + gap_score; + } + } +} + +score_t match(char const * needle, char const * haystack, int case_sensitive) +{ + if (! *needle) + return SCORE_MIN; + + struct match_struct match; + setup_match_struct(&match, needle, haystack, case_sensitive); + + unsigned n = match.needle_len; + unsigned m = match.haystack_len; + + // Unreasonably large candidate; return no score. If it is a valid match, + // it will still be returned, it will just be ranked below any reasonably + // sized candidates. + if (m > MATCH_MAX_LEN || n > m) + return SCORE_MIN; + + // If `needle` is a subsequence of `haystack` and the same length, then + // they are the same string. + if (n == m) + return SCORE_MAX; + + // D[][] Stores the best score for this position ending with a match. + // M[][] Stores the best possible score at this position. + score_t D[2][MATCH_MAX_LEN]; + score_t M[2][MATCH_MAX_LEN]; + + score_t * last_D = D[0]; + score_t * last_M = M[0]; + score_t * curr_D = D[1]; + score_t * curr_M = M[1]; + + for (int i = 0; i < n; i++) + { + match_row(&match, i, curr_D, curr_M, last_D, last_M); + + SWAP(curr_D, last_D, score_t *); + SWAP(curr_M, last_M, score_t *); + } + + return last_M[m - 1]; +} + +score_t match_positions( + char const * needle, + char const * haystack, + index_t * positions, + int is_case_sensitive) +{ + if (! *needle) + return SCORE_MIN; + + struct match_struct match; + setup_match_struct(&match, needle, haystack, is_case_sensitive); + + int n = match.needle_len; + int m = match.haystack_len; + + // Unreasonably large candidate; return no score. If it is a valid match, + // it will still be returned, it will just be ranked below any reasonably + // sized candidates + if (m > MATCH_MAX_LEN || n > m) + return SCORE_MIN; + + // If `needle` is a subsequence of `haystack` and the same length, then + // they are the same string. + if (n == m) + { + if (positions) + for (int i = 0; i < n; i++) + positions[i] = i; + + return SCORE_MAX; + } + + // D[][] Stores the best score for this position ending with a match. + // M[][] Stores the best possible score at this position. + typedef score_t score_row_t[MATCH_MAX_LEN]; + score_row_t * const D = malloc(sizeof(score_row_t) * n); + score_row_t * const M = malloc(sizeof(score_row_t) * n); + + score_t * last_D = NULL; + score_t * last_M = NULL; + score_t * curr_D = NULL; + score_t * curr_M = NULL; + + for (int i = 0; i < n; i++) + { + curr_D = &D[i][0]; + curr_M = &M[i][0]; + + match_row(&match, i, curr_D, curr_M, last_D, last_M); + + last_D = curr_D; + last_M = curr_M; + } + + /* backtrace to find the positions of optimal matching */ + int match_required = 0; + for (int i = n - 1, j = m - 1; i >= 0; i--) + { + for (; j >= 0; j--) + { + // There may be multiple paths which result in the optimal + // weight. + // + // For simplicity, we will pick the first one we encounter, + // the latest in the candidate string. + // NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) + if (D[i][j] != SCORE_MIN && (match_required || D[i][j] == M[i][j])) + { + // If this score was determined using SCORE_MATCH_CONSECUTIVE, + // the previous character MUST be a match + match_required = + i && j && M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE; + + if (positions) + positions[i] = j--; + + break; + } + } + } + + score_t result = M[n - 1][m - 1]; + + free(M); + free(D); + + return result; +} + diff --git a/fzy/match.h b/fzy/match.h new file mode 100644 index 0000000000000..be88e07fa3b3c --- /dev/null +++ b/fzy/match.h @@ -0,0 +1,81 @@ +/* The MIT License (MIT) +Copyright (c) 2020 Seth Warn +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +/* match.h + * c interface to fzy matching + * + * original code by John Hawthorn, https://github.com/jhawthorn/fzy + * modifications by + * Rom Grk, https://github.com/romgrk + * Seth Warn, https://github.com/swarn + */ + +#ifndef FZY_NATIVE_H +#define FZY_NATIVE_H + +#include +#include + + +typedef double score_t; +typedef uint32_t index_t; + +#define SCORE_MAX INFINITY +#define SCORE_MIN (-INFINITY) +#define MATCH_MAX_LEN 1024 + + +// Return true if `needle` is a subsequence of `haystack`. +// +// Control case sensitivity of matches with `case_sensitive` +int has_match(char const * needle, char const * haystack, int case_sensitive); + + +// Compute a matching score for two strings. +// +// Note: if `has_match(needle, haystack)` is not true, the return value +// is undefined. +// +// Returns a score measuring the quality of the match. Better matches get +// higher scores. +// +// - returns `SCORE_MIN` where `needle` or `haystack` are longer than +// `MATCH_MAX_LEN`. +// +// - returns `SCORE_MIN` when `needle` or `haystack` are empty strings. +// +// - return `SCORE_MAX` when `strlen(needle) == strlen(haystack)` +score_t match(char const * needle, char const * haystack, int case_sensitive); + + +// Compute a matching score and the indices of matching characters. +// +// - The score is returned as in match() +// +// - `positions` is an array that will be filled in such that `positions[i]` is +// the index of `haystack` where `needle[i]` matches in the optimal match. +score_t match_positions( + char const * needle, + char const * haystack, + index_t * positions, + int is_case_sensitive); + + +#endif + diff --git a/meson.build b/meson.build index 64c49eb4b52a8..5bb2a527947c8 100644 --- a/meson.build +++ b/meson.build @@ -737,7 +737,7 @@ features += {'lua': lua.found()} lua_version = lua.name() if features['lua'] dependencies += lua - sources += files('player/lua.c') + sources += files('player/lua.c', 'fzy/match.c') endif if not features['lua'] and lua_opt == 'enabled' error('Lua enabled but no suitable Lua version could be found!') diff --git a/player/lua.c b/player/lua.c index 9ccd1f4d4432d..e488c0ce05c2e 100644 --- a/player/lua.c +++ b/player/lua.c @@ -49,6 +49,9 @@ #include "client.h" #include "libmpv/client.h" +#include "fzy/bonus.h" +#include "fzy/match.h" + // List of builtin modules and their contents as strings. // All these are generated from player/lua/*.lua static const char * const builtin_lua_scripts[][2] = { @@ -57,9 +60,6 @@ static const char * const builtin_lua_scripts[][2] = { }, {"mp.assdraw", # include "player/lua/assdraw.lua.inc" - }, - {"mp.fzy", -# include "player/lua/fzy.lua.inc" }, {"mp.input", # include "player/lua/input.lua.inc" @@ -114,6 +114,7 @@ static int mp_cpcall (lua_State *L, lua_CFunction func, void *ud) lua_pushlightuserdata(L, ud); return lua_pcall(L, 1, 0, 0); } +#define lua_objlen(L,i) lua_rawlen(L, (i)) #endif // Ensure that the given argument exists, even if it's nil. Can be used to @@ -1208,6 +1209,78 @@ static int script_get_env_list(lua_State *L) return 1; } +// The next 2 functions are taken from https://github.com/swarn/fzy-lua/blob/main/src/fzy_native.c +// Given an array of `count` 0-based indices, push a table on to `L` with +// equivalent 1-based indices. +static void push_indices(lua_State * L, index_t const * const indices, int count) +{ + lua_createtable(L, count, 0); + for (int i = 0; i < count; i++) + { + // Convert from 0-indexing to 1-indexing. + lua_pushinteger(L, indices[i] + 1); + lua_rawseti(L, -2, i + 1); + } +} + +static int script_filter(lua_State *L) +{ + char const * const needle = luaL_checkstring(L, 1); + int const needle_len = (int)strlen(needle); + + int const haystacks_idx = 2; + luaL_checktype(L, haystacks_idx, LUA_TTABLE); + int const haystacks_len = (int)lua_objlen(L, haystacks_idx); + + bool case_sensitive = false; + if (lua_gettop(L) > 2) + case_sensitive = lua_toboolean(L, 3); + + // Push the result array onto the lua stack. + lua_newtable(L); + int const result_idx = lua_gettop(L); + int result_len = 0; + + // Call `positions` on each haystack string. + for (int i = 1; i <= haystacks_len; i++) + { + lua_rawgeti(L, haystacks_idx, i); + char const * haystack = luaL_checkstring(L, -1); + + if (has_match(needle, haystack, case_sensitive)) + { + result_len++; + + // Make the {idx, positions, score} table. + lua_createtable(L, 3, 0); + + // Set the idx + lua_pushinteger(L, i); + lua_rawseti(L, -2, 1); + + // Generate the positions and the score + index_t result[MATCH_MAX_LEN]; + score_t score = match_positions(needle, haystack, result, case_sensitive); + + // Set the positions + push_indices(L, result, needle_len); + lua_rawseti(L, -2, 2); + + // Set the score + lua_pushnumber(L, score); + lua_rawseti(L, -2, 3); + + // Add this table to the result + lua_rawseti(L, result_idx, result_len); + } + + // Pop the current haystack string off the lua stack. + lua_pop(L, 1); + } + + return 1; +} + #define FN_ENTRY(name) {#name, script_ ## name, 0} #define AF_ENTRY(name) {#name, 0, script_ ## name} struct fn_entry { @@ -1260,6 +1333,11 @@ static const struct fn_entry utils_fns[] = { {0} }; +static const struct fn_entry fzy_fns[] = { + FN_ENTRY(filter), + {0} +}; + typedef struct autofree_data { af_CFunction target; void *ctx; @@ -1338,6 +1416,7 @@ static void add_functions(struct script_ctx *ctx) register_package_fns(L, "mp", main_fns); register_package_fns(L, "mp.utils", utils_fns); + register_package_fns(L, "mp.fzy", fzy_fns); } const struct mp_scripting mp_scripting_lua = { diff --git a/player/lua/fzy.lua b/player/lua/fzy.lua deleted file mode 100644 index 7b490d52bcce1..0000000000000 --- a/player/lua/fzy.lua +++ /dev/null @@ -1,297 +0,0 @@ ---[[ The MIT License (MIT) - -Copyright (c) 2020 Seth Warn - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. ]] - --- The lua implementation of the fzy string matching algorithm - -local SCORE_GAP_LEADING = -0.005 -local SCORE_GAP_TRAILING = -0.005 -local SCORE_GAP_INNER = -0.01 -local SCORE_MATCH_CONSECUTIVE = 1.0 -local SCORE_MATCH_SLASH = 0.9 -local SCORE_MATCH_WORD = 0.8 -local SCORE_MATCH_CAPITAL = 0.7 -local SCORE_MATCH_DOT = 0.6 -local SCORE_MAX = math.huge -local SCORE_MIN = -math.huge -local MATCH_MAX_LENGTH = 1024 - -local fzy = {} - --- Check if `needle` is a subsequence of the `haystack`. --- --- Usually called before `score` or `positions`. --- --- Args: --- needle (string) --- haystack (string) --- case_sensitive (bool, optional): defaults to false --- --- Returns: --- bool -function fzy.has_match(needle, haystack, case_sensitive) - if not case_sensitive then - needle = string.lower(needle) - haystack = string.lower(haystack) - end - - local j = 1 - for i = 1, string.len(needle) do - j = string.find(haystack, needle:sub(i, i), j, true) - if not j then - return false - else - j = j + 1 - end - end - - return true -end - -local function is_lower(c) - return c:match("%l") -end - -local function is_upper(c) - return c:match("%u") -end - -local function precompute_bonus(haystack) - local match_bonus = {} - - local last_char = "/" - for i = 1, string.len(haystack) do - local this_char = haystack:sub(i, i) - if last_char == "/" or last_char == "\\" then - match_bonus[i] = SCORE_MATCH_SLASH - elseif last_char == "-" or last_char == "_" or last_char == " " then - match_bonus[i] = SCORE_MATCH_WORD - elseif last_char == "." then - match_bonus[i] = SCORE_MATCH_DOT - elseif is_lower(last_char) and is_upper(this_char) then - match_bonus[i] = SCORE_MATCH_CAPITAL - else - match_bonus[i] = 0 - end - - last_char = this_char - end - - return match_bonus -end - -local function compute(needle, haystack, D, M, case_sensitive) - -- Note that the match bonuses must be computed before the arguments are - -- converted to lowercase, since there are bonuses for camelCase. - local match_bonus = precompute_bonus(haystack) - local n = string.len(needle) - local m = string.len(haystack) - - if not case_sensitive then - needle = string.lower(needle) - haystack = string.lower(haystack) - end - - -- Because lua only grants access to chars through substring extraction, - -- get all the characters from the haystack once now, to reuse below. - local haystack_chars = {} - for i = 1, m do - haystack_chars[i] = haystack:sub(i, i) - end - - for i = 1, n do - D[i] = {} - M[i] = {} - - local prev_score = SCORE_MIN - local gap_score = i == n and SCORE_GAP_TRAILING or SCORE_GAP_INNER - local needle_char = needle:sub(i, i) - - for j = 1, m do - if needle_char == haystack_chars[j] then - local score = SCORE_MIN - if i == 1 then - score = ((j - 1) * SCORE_GAP_LEADING) + match_bonus[j] - elseif j > 1 then - local a = M[i - 1][j - 1] + match_bonus[j] - local b = D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE - score = math.max(a, b) - end - D[i][j] = score - prev_score = math.max(score, prev_score + gap_score) - M[i][j] = prev_score - else - D[i][j] = SCORE_MIN - prev_score = prev_score + gap_score - M[i][j] = prev_score - end - end - end -end - --- Compute a matching score. --- --- Args: --- needle (string): must be a subsequence of `haystack`, or the result is --- undefined. --- haystack (string) --- case_sensitive (bool, optional): defaults to false --- --- Returns: --- number: higher scores indicate better matches. See also `get_score_min` --- and `get_score_max`. -function fzy.score(needle, haystack, case_sensitive) - local n = string.len(needle) - local m = string.len(haystack) - - if n == 0 or m == 0 or m > MATCH_MAX_LENGTH or n > m then - return SCORE_MIN - elseif n == m then - return SCORE_MAX - else - local D = {} - local M = {} - compute(needle, haystack, D, M, case_sensitive) - return M[n][m] - end -end - --- Compute the locations where fzy matches a string. --- --- Determine where each character of the `needle` is matched to the `haystack` --- in the optimal match. --- --- Args: --- needle (string): must be a subsequence of `haystack`, or the result is --- undefined. --- haystack (string) --- case_sensitive (bool, optional): defaults to false --- --- Returns: --- {int,...}: indices, where `indices[n]` is the location of the `n`th --- character of `needle` in `haystack`. --- number: the same matching score returned by `score` -function fzy.positions(needle, haystack, case_sensitive) - local n = string.len(needle) - local m = string.len(haystack) - - if n == 0 or m == 0 or m > MATCH_MAX_LENGTH or n > m then - return {}, SCORE_MIN - elseif n == m then - local consecutive = {} - for i = 1, n do - consecutive[i] = i - end - return consecutive, SCORE_MAX - end - - local D = {} - local M = {} - compute(needle, haystack, D, M, case_sensitive) - - local positions = {} - local match_required = false - local j = m - for i = n, 1, -1 do - while j >= 1 do - if D[i][j] ~= SCORE_MIN and (match_required or D[i][j] == M[i][j]) then - match_required = (i ~= 1) and (j ~= 1) and ( - M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE) - positions[i] = j - j = j - 1 - break - else - j = j - 1 - end - end - end - - return positions, M[n][m] -end - --- Apply `has_match` and `positions` to an array of haystacks. --- --- Args: --- needle (string) --- haystack ({string, ...}) --- case_sensitive (bool, optional): defaults to false --- --- Returns: --- {{idx, positions, score}, ...}: an array with one entry per matching line --- in `haystacks`, each entry giving the index of the line in `haystacks` --- as well as the equivalent to the return value of `positions` for that --- line. -function fzy.filter(needle, haystacks, case_sensitive) - local result = {} - - for i, line in ipairs(haystacks) do - if fzy.has_match(needle, line, case_sensitive) then - local p, s = fzy.positions(needle, line, case_sensitive) - table.insert(result, {i, p, s}) - end - end - - return result -end - --- The lowest value returned by `score`. --- --- In two special cases: --- - an empty `needle`, or --- - a `needle` or `haystack` larger than than `get_max_length`, --- the `score` function will return this exact value, which can be used as a --- sentinel. This is the lowest possible score. -function fzy.get_score_min() - return SCORE_MIN -end - --- The score returned for exact matches. This is the highest possible score. -function fzy.get_score_max() - return SCORE_MAX -end - --- The maximum size for which `fzy` will evaluate scores. -function fzy.get_max_length() - return MATCH_MAX_LENGTH -end - --- The minimum score returned for normal matches. --- --- For matches that don't return `get_score_min`, their score will be greater --- than than this value. -function fzy.get_score_floor() - return MATCH_MAX_LENGTH * SCORE_GAP_INNER -end - --- The maximum score for non-exact matches. --- --- For matches that don't return `get_score_max`, their score will be less than --- this value. -function fzy.get_score_ceiling() - return MATCH_MAX_LENGTH * SCORE_MATCH_CONSECUTIVE -end - --- The name of the currently-running implementation, "lua" or "native". -function fzy.get_implementation_name() - return "lua" -end - -return fzy diff --git a/player/lua/meson.build b/player/lua/meson.build index ac4c54b4dd4fd..6f4831dfba7e8 100644 --- a/player/lua/meson.build +++ b/player/lua/meson.build @@ -1,6 +1,6 @@ lua_files = ['defaults.lua', 'assdraw.lua', 'options.lua', 'osc.lua', 'ytdl_hook.lua', 'stats.lua', 'console.lua', 'auto_profiles.lua', - 'input.lua', 'fzy.lua', 'select.lua'] + 'input.lua', 'select.lua'] foreach file: lua_files lua_file = custom_target(file, input: file,