Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
-- A table containing the matched file or directory names.
---

function os.match(mask)
function os.match(mask, matchType)
mask = path.normalize(mask)
local starpos = mask:find("%*")
local before = path.getdirectory(starpos and mask:sub(1, starpos - 1) or mask)
Expand All @@ -370,14 +370,14 @@

if recurse then
local submask = mask:sub(1, starpos) .. mask:sub(starpos + 2)
results = os.match(submask)
results = os.match(submask, matchType)

local pattern = mask:sub(1, starpos)
local m = os.matchstart(pattern)
while os.matchnext(m) do
if not os.matchisfile(m) then
local matchpath = path.join(before, os.matchname(m), mask:sub(starpos))
results = table.join(results, os.match(matchpath))
results = table.join(results, os.match(matchpath, matchType))
end
end
os.matchdone(m)
Expand All @@ -387,10 +387,17 @@
while os.matchnext(m) do
if not (slashpos and os.matchisfile(m)) then
local matchpath = path.join(before, matchpath, os.matchname(m))

if after then
results = table.join(results, os.match(path.join(matchpath, after)))
results = table.join(results, os.match(path.join(matchpath, after), matchType))
else
if matchType == "file" and os.matchisfile(m) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo? for below too

Suggested change
if matchType == "file" and os.matchisfile(m) then
if matchType == "file" and os.matchisfile(matchpath) then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.matchisfile takes a match context, which is m here, so this isn't a typo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not how not (A and B) works, either A or B can be false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, indeed, misread.

table.insert(results, matchpath)
elseif matchType == "folder" and not os.matchisfile(m) then
table.insert(results, matchpath)
else -- keep previous behaviour
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realised it's this here, it needs to check that matchType == nil or something, otherwise all folders when matching files are picked up, and all files when matching folders are picked up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition the comment is not meaningful outside of the pull request.

table.insert(results, matchpath)
end
end
end
end
Expand All @@ -412,12 +419,7 @@
---

function os.matchdirs(mask)
local results = os.match(mask)
for i = #results, 1, -1 do
if not os.isdir(results[i]) then
table.remove(results, i)
end
end
local results = os.match(mask, "folder")
return results
end

Expand All @@ -433,12 +435,7 @@
---

function os.matchfiles(mask)
local results = os.match(mask)
for i = #results, 1, -1 do
if not os.isfile(results[i]) then
table.remove(results, i)
end
end
local results = os.match(mask, "file")
return results
end

Expand Down