Skip to content

Instantly share code, notes, and snippets.

@muink
Last active February 12, 2024 04:15
Show Gist options
  • Save muink/1831f68fe8702480fb205cf7d5a06f17 to your computer and use it in GitHub Desktop.
Save muink/1831f68fe8702480fb205cf7d5a06f17 to your computer and use it in GitHub Desktop.
def urldecode:
def hex2dec:
{"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"A":10,"B":11,"C":12,"D":13,"E":14,"F":15} as $hexTable
| [1,16,256,4096,65536,1048576,16777216,268435456] as $pow16
| def str2dec:
split("") | reverse as $arr
| last( foreach range(length) as $i (0; . + $hexTable[($arr[$i]|ascii_upcase)] * $pow16[$i]) );
if type == "string" then
str2dec
elif type == "array" then
last(
label $out | foreach range(length) as $i (.;
if (.[$i]|type) == "string" then
.[$i]=(.[$i]|str2dec)
else null, break $out end
)
)
else empty end;
def utf82uni:
def loop($i):
if $i >= length then empty
elif .[$i] >= 240 then (.[$i+3]-128) + 64*(.[$i+2]-128) + 4096*(.[$i+1]-128) + 262144*(.[$i]-240), loop($i+4)
elif .[$i] >= 224 then (.[$i+2]-128) + 64*(.[$i+1]-128) + 4096*(.[$i]-224), loop($i+3)
elif .[$i] >= 192 then (.[$i+1]-128) + 64*(.[$i]-192), loop($i+2)
else .[$i], loop($i+1)
end;
[loop(0)];
gsub("(?<x>(?:%[[:xdigit:]]{2})+)"; .x | .[1:] | split("%") | hex2dec | utf82uni | implode);
urldecode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment