Add script for percent-escaping (and unescaping)

This commit is contained in:
Talia 2021-07-16 08:27:30 +02:00
parent 1328c65502
commit 8cf20e57f9
1 changed files with 22 additions and 0 deletions

22
bin/percent Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env lua
local args do
local parse = require 'arrr' {
{ "Unescape instead of escaping", "--decode", "-d" };
}
args = parse{...}
end
local pattern, replace
if args.decode then
pattern, replace = "%%%x%x", function(escape)
return string.char(tonumber(escape:sub(2, 3), 16))
end
else
pattern = "[:/?#@!$&'()*+,;=[%]%%\n ]"
replace = function(plain)
return string.format("%%%0X", plain:byte())
end
end
io.write((io.read("a*"):gsub(pattern, replace)))