diff options
author | Guido Cella <guido@guidocella.xyz> | 2024-02-05 10:58:28 +0100 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2024-02-08 18:18:33 +0100 |
commit | 36153d1a4a6e194e373de64cc2bced5018daafd9 (patch) | |
tree | 9a1c249f8dc5ea758576b08744801a8dcdae8eac /player/lua | |
parent | aa100d1512d371f57d1311033e256c806d7099ca (diff) | |
download | mpv-36153d1a4a6e194e373de64cc2bced5018daafd9.tar.bz2 mpv-36153d1a4a6e194e373de64cc2bced5018daafd9.tar.xz |
console.lua: move functions to strip common characters
Move common_prefix_length() and related functions before the first call
to common_prefix_length(). It works now because it's global but if we
ever make all functions local for consistency it will stop working.
Diffstat (limited to 'player/lua')
-rw-r--r-- | player/lua/console.lua | 68 |
1 files changed, 34 insertions, 34 deletions
diff --git a/player/lua/console.lua b/player/lua/console.lua index 49860f3e85..65f73fc9b5 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -969,6 +969,40 @@ function build_completers() return completers end +function common_prefix_length(s1, s2) + local common_count = 0 + for i = 1, #s1 do + if s1:byte(i) ~= s2:byte(i) then + break + end + common_count = common_count + 1 + end + return common_count +end + +function max_overlap_length(s1, s2) + for s1_offset = 0, #s1 - 1 do + local match = true + for i = 1, #s1 - s1_offset do + if s1:byte(s1_offset + i) ~= s2:byte(i) then + match = false + break + end + end + if match then + return #s1 - s1_offset + end + end + return 0 +end + +-- If str starts with the first or last characters of prefix, strip them. +local function strip_common_characters(str, prefix) + return str:sub(1 + math.max( + common_prefix_length(prefix, str), + max_overlap_length(prefix, str))) +end + -- Find the longest common case-sensitive prefix of the entries in "list". local function find_common_prefix(list) local prefix = list[1] @@ -1026,40 +1060,6 @@ local function complete_match(part, list) return {}, part end -function common_prefix_length(s1, s2) - local common_count = 0 - for i = 1, #s1 do - if s1:byte(i) ~= s2:byte(i) then - break - end - common_count = common_count + 1 - end - return common_count -end - -function max_overlap_length(s1, s2) - for s1_offset = 0, #s1 - 1 do - local match = true - for i = 1, #s1 - s1_offset do - if s1:byte(s1_offset + i) ~= s2:byte(i) then - match = false - break - end - end - if match then - return #s1 - s1_offset - end - end - return 0 -end - --- If str starts with the first or last characters of prefix, strip them. -local function strip_common_characters(str, prefix) - return str:sub(1 + math.max( - common_prefix_length(prefix, str), - max_overlap_length(prefix, str))) -end - local function cycle_through_suggestions(backwards) selected_suggestion_index = selected_suggestion_index + (backwards and -1 or 1) |