RosettaCodeData/Task/URL-encoding/AWK/url-encoding.awk

25 lines
507 B
Awk
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
BEGIN {
2026-04-30 12:34:36 -04:00
for (i = 0; i <= 255; i++)
ord[sprintf("%c", i)] = i
2023-07-01 11:58:00 -04:00
}
# Encode string with application/x-www-form-urlencoded escapes.
function escape(str, c, len, res) {
2026-04-30 12:34:36 -04:00
len = length(str)
res = ""
for (i = 1; i <= len; i++) {
c = substr(str, i, 1);
if (c ~ /[0-9A-Za-z]/)
#if (c ~ /[-._*0-9A-Za-z]/)
res = res c
#else if (c == " ")
# res = res "+"
else
res = res "%" sprintf("%02X", ord[c])
}
return res
2023-07-01 11:58:00 -04:00
}
# Escape every line of input.
{ print escape($0) }