From eb872545872d2bf17d7c45a30a596d14a79e14dc Mon Sep 17 00:00:00 2001 From: Mihai Sebea Date: Mon, 28 Mar 2022 17:53:52 +0200 Subject: [PATCH 1/2] ! checking if each match found is either a file or folder appears to be expensive on large projects (at least on windows) ! for a 5k files project GetAttributesW takes around 20% of the time --- src/base/os.lua | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/base/os.lua b/src/base/os.lua index c0296dcaa..333d14f56 100644 --- a/src/base/os.lua +++ b/src/base/os.lua @@ -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) @@ -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) @@ -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 table.insert(results, matchpath) + elseif matchType == "folder" and not os.matchisfile(m) then + table.insert(results, matchpath) + else -- keep previous behaviour + table.insert(results, matchpath) + end end end end @@ -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 @@ -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 From d70fdf12b46e14aa99888fce98c7985e012cfa97 Mon Sep 17 00:00:00 2001 From: Mihai Sebea Date: Sun, 10 Apr 2022 09:22:49 +0200 Subject: [PATCH 2/2] ! added documentation ! fix indeting --- src/base/os.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/base/os.lua b/src/base/os.lua index 333d14f56..ec77f9688 100644 --- a/src/base/os.lua +++ b/src/base/os.lua @@ -350,6 +350,10 @@ -- @param mask -- The file search pattern. Use "*" to match any part of a file or -- directory name, "**" to recurse into subdirectories. +-- @param matchType +-- folder will match only folders +-- file will match only files +-- if not set it will match both -- @return -- A table containing the matched file or directory names. --- @@ -392,7 +396,7 @@ results = table.join(results, os.match(path.join(matchpath, after), matchType)) else if matchType == "file" and os.matchisfile(m) then - table.insert(results, matchpath) + table.insert(results, matchpath) elseif matchType == "folder" and not os.matchisfile(m) then table.insert(results, matchpath) else -- keep previous behaviour