Created
February 3, 2019 09:17
-
-
Save warptozero/ebbb27148d21da62d87cc19433c0a167 to your computer and use it in GitHub Desktop.
Simple test to show correctness of file extension matching patterns in lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local filenames = { | |
["filename"] = "nil", | |
[".filename"] = "nil", | |
["filename."] = "nil", | |
[".filename."] = "nil", | |
["filename.ext"] = "ext", | |
[".filename.ext"] = "ext", | |
["filename.ext."] = "nil", | |
["file.name.ext"] = "ext", | |
[".file.name.ext"] = "ext", | |
["file.name.ext."] = "nil", | |
[".file.name.ext."] = "nil", | |
} | |
local patterns = { | |
"%.(.*)$", "%.(.-)$", "%.(.+)$", | |
".*%.(.*)$", ".*%.(.-)$", ".*%.(.+)$", | |
".-%.(.*)$", ".-%.(.-)$", ".-%.(.+)$", | |
".+%.(.*)$", ".+%.(.-)$", ".+%.(.+)$", | |
"%.([^.]*)$", "%.([^.]-)$", "%.([^.]+)$", | |
".*%.([^.]*)$", ".*%.([^.]-)$", ".*%.([^.]+)$", | |
".-%.([^.]*)$", ".-%.([^.]-)$", ".-%.([^.]+)$", | |
".+%.([^.]*)$", ".+%.([^.]-)$", ".+%.([^.]+)$", | |
"[^.]+$", | |
} | |
local res = {} | |
for f,ext in pairs(filenames) do | |
-- print() print(f, ext) | |
for i,p in ipairs(patterns) do | |
local m = string.match(f, p) | |
local is_corr = tostring(m) == ext | |
if not res[i] then res[i] = {} end | |
if not is_corr then | |
table.insert(res[i], f) | |
end | |
-- print(p.." ", m) | |
end | |
end | |
print() print("pattern", " ", "fails at") | |
for i,fails in pairs(res) do | |
table.sort(fails) | |
print(patterns[i].." ", table.unpack(fails)) | |
end | |
print() print("correct patterns:") | |
for i,p in ipairs(patterns) do | |
if #res[i] == 0 then | |
print(p) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment