On Wednesday, November 16, 2016 at 3:06:51 PM UTC-5, Andre Majorel wrote:
> How does it "not work" ? What do you get and what would you like
> to get instead ?
Test string: ł
urlencodeawk() = %00
urlencodepython() = %C5%82
Python encodes correctly
----
#
# Credit: Rosetta Stone May 2015
#
function urlencodeawk(str, c, len, res, i, ord) {
for (i = 0; i <= 255; i++)
ord[sprintf("%c", i)] = i
len = length(str)
res = ""
for (i = 1; i <= len; i++) {
c = substr(str, i, 1);
if (c ~ /[0-9A-Za-z]/)
res = res c
else
res = res "%" sprintf("%02X", ord[c])
}
return res
}
#
# url-encode via Python
# Credit:
https://askubuntu.com/questions/53770/how-can-i-encode-and-decode-percent-encoded-strings-on-the-command-line
#
function urlencodepython(str, command, safe) {
safe = str
gsub(/'/, "'\"'\"'", safe) # make safe for shell
gsub(/’/, "'\"’\"'", safe)
command = "python -c \"import urllib, sys; print urllib.quote(sys.argv[1])\" '" safe "'"
return sys2var(command)
}
function sys2var(command ,fish, scale, ship) {
# command = command " 2>/dev/null"
while ( (command | getline fish) > 0 ) {
if ( ++scale == 1 )
ship = fish
else
ship = ship "\n" fish
}
close(command)
return ship
}
function testurlendecode(str) {
print "Test string: " str
print ""
print "urlencodeawk() = " urlencodeawk(str)
print "urlencodepython() = " urlencodepython(str)
}
BEGIN {
testurlendecode("ł")
}