mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-04 10:49:08 +00:00
feat(util): encode/decode a string to be used as a filename
This commit is contained in:
parent
7c29848e89
commit
e6f63970de
1 changed files with 17 additions and 0 deletions
|
@ -69,4 +69,21 @@ function M.icon(name, cat)
|
||||||
end
|
end
|
||||||
return " "
|
return " "
|
||||||
end
|
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
|
return M
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue