Skip to content

Commit

Permalink
Simplify find_files
Browse files Browse the repository at this point in the history
  • Loading branch information
zauguin committed Jan 27, 2024
1 parent e19db1d commit 1490a71
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/luaotfload-database.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,31 +333,6 @@ local list_dir_or_empty do
end
end

local function find_files_indeed (acc, dirs, filter)
if not next (dirs) then --- done
return acc
end

local dir = dirs[#dirs]
dirs[#dirs] = nil

local newfiles = { }
for ent in list_dir_or_empty (dir) do
if ent ~= "." and ent ~= ".." then
local fullpath = dir .. "/" .. ent
if filter (fullpath) == true then
if lfsisdir (fullpath) then
dirs[#dirs+1] = fullpath
elseif lfsisfile (fullpath) then
newfiles[#newfiles+1] = fullpath
end
end
end
end
return find_files_indeed (tableappend (acc, newfiles),
dirs, filter)
end

local function dummyfilter () return true end

--- the optional filter function receives the full path of a file
Expand All @@ -366,9 +341,30 @@ local function dummyfilter () return true end

--- string -> function? -> string list
local function find_files (root, filter)
if lfsisdir (root) then
return find_files_indeed ({}, { root }, filter or dummyfilter)
if not lfsisdir (root) then return end

local files = {}
local dirs = { root }
filter = filter or dummyfilter

while dirs[1] do
local dir = dirs[#dirs]
dirs[#dirs] = nil

for ent in list_dir_or_empty (dir) do
if ent ~= "." and ent ~= ".." then
local fullpath = dir .. "/" .. ent
if filter (fullpath) == true then
if lfsisdir (fullpath) then
dirs[#dirs+1] = fullpath
elseif lfsisfile (fullpath) then
files[#files+1] = fullpath
end
end
end
end
end
return files
end


Expand Down

0 comments on commit 1490a71

Please sign in to comment.