util: Simplify splitFile* util functions

* Use a single match call instead of two gsubs
reviewable/pr6976/r4
NiLuJe 3 years ago
parent d80d6dc562
commit c1496e943b

@ -810,11 +810,11 @@ end
--- If the given path has a trailing /, returns the entire path as the directory --- If the given path has a trailing /, returns the entire path as the directory
--- path and "" as the file name. --- path and "" as the file name.
---- @string file ---- @string file
---- @treturn string path, filename ---- @treturn string directory, filename
function util.splitFilePathName(file) function util.splitFilePathName(file)
if file == nil or file == "" then return "", "" end if file == nil or file == "" then return "", "" end
if string.find(file, "/") == nil then return "", file end if string.find(file, "/") == nil then return "", file end
return string.gsub(file, "(.*/)(.*)", "%1"), string.gsub(file, ".*/", "") return file:match'(.*/)(.*)'
end end
--- Splits a file name into its pure file name and suffix --- Splits a file name into its pure file name and suffix
@ -823,7 +823,7 @@ end
function util.splitFileNameSuffix(file) function util.splitFileNameSuffix(file)
if file == nil or file == "" then return "", "" end if file == nil or file == "" then return "", "" end
if string.find(file, "%.") == nil then return file, "" end if string.find(file, "%.") == nil then return file, "" end
return string.gsub(file, "(.*)%.(.*)", "%1"), string.gsub(file, ".*%.", "") return file:match'(.*)%.(.*)'
end end
--- Gets file extension --- Gets file extension
@ -835,7 +835,7 @@ function util.getFileNameSuffix(file)
end end
--- Companion helper function that returns the script's language, --- Companion helper function that returns the script's language,
--- based on the filme extension. --- based on the file extension.
---- @string filename ---- @string filename
---- @treturn string (lowercase) (or nil if not Device:canExecuteScript(file)) ---- @treturn string (lowercase) (or nil if not Device:canExecuteScript(file))
function util.getScriptType(file) function util.getScriptType(file)

Loading…
Cancel
Save