fix: support natural ordering for numbers with >12 digits (#652)
* fix: support natural ordering for numbers with >12 digits Changes the column ordering code when `view_options.natural_order` is enabled, so that it can support larger numbers. The previous 12-digit padding approach breaks for numbers above 12 digits. This length-prefixed approach can scale to much higher numbers. I picked %03 (padding 3 digits) because most filesystems don't allow more than 255 bytes in a path segment, and "255" is 3 digits long. * add memoization to natural order sorting * remove call to unpack
This commit is contained in:
parent
bbad9a76b2
commit
07f80ad645
2 changed files with 12 additions and 10 deletions
|
|
@ -228,8 +228,8 @@ M.register("type", {
|
|||
end,
|
||||
})
|
||||
|
||||
local function pad_number(int)
|
||||
return string.format("%012d", int)
|
||||
local function adjust_number(int)
|
||||
return string.format("%03d%s", #int, int)
|
||||
end
|
||||
|
||||
M.register("name", {
|
||||
|
|
@ -256,14 +256,16 @@ M.register("name", {
|
|||
end
|
||||
end
|
||||
else
|
||||
if config.view_options.case_insensitive then
|
||||
return function(entry)
|
||||
return entry[FIELD_NAME]:gsub("%d+", pad_number):lower()
|
||||
end
|
||||
else
|
||||
return function(entry)
|
||||
return entry[FIELD_NAME]:gsub("%d+", pad_number)
|
||||
local memo = {}
|
||||
return function(entry)
|
||||
if memo[entry] == nil then
|
||||
local name = entry[FIELD_NAME]:gsub("0*(%d+)", adjust_number)
|
||||
if config.view_options.case_insensitive then
|
||||
name = name:lower()
|
||||
end
|
||||
memo[entry] = name
|
||||
end
|
||||
return memo[entry]
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
|
|
|||
|
|
@ -587,7 +587,7 @@ local function get_sort_function(adapter, num_entries)
|
|||
end
|
||||
return function(a, b)
|
||||
for _, sort_fn in ipairs(idx_funs) do
|
||||
local get_sort_value, order = unpack(sort_fn)
|
||||
local get_sort_value, order = sort_fn[1], sort_fn[2]
|
||||
local a_val = get_sort_value(a)
|
||||
local b_val = get_sort_value(b)
|
||||
if a_val ~= b_val then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue