feat(util): encode/decode a string to be used as a filename

This commit is contained in:
Folke Lemaitre 2024-12-01 09:07:26 +01:00
parent 7c29848e89
commit e6f63970de
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -69,4 +69,21 @@ function M.icon(name, cat)
end
return ""
end
-- Encodes a string to be used as a file name.
---@param str string
function M.file_encode(str)
return str:gsub("([^%w%-_%.\t ])", function(c)
return string.format("_%%%02X", string.byte(c))
end)
end
-- Decodes a file name to a string.
---@param str string
function M.file_decode(str)
return str:gsub("_%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
end
return M