Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: url_decode function #2957

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,43 @@ def ascii_downcase:
def ascii_upcase:
explode | map( if 97 <= . and . <= 122 then . - 32 else . end) | implode;

# inverse of @uri in jq
def url_decode:
def unhex:
if 48 <= . and . <= 57 then . - 48 elif 65 <= . and . <= 70 then . - 55 else . - 87 end;

def bytes:
def loop($i):
if $i >= length then empty else 16 * (.[$i+1] | unhex) + (.[$i+2] | unhex), loop($i+3) end;
[loop(0)];

def codepoints:
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)];

# Note that URL-encoding implies percent-encoded UTF-8 octets, so we have to
# manually reassemble these into codepoints for implode
gsub("(?<m>(?:%[0-9a-fA-F]{2})+)"; .m | explode | bytes | codepoints | implode);

# reimplementation of @uri in jq to maintain a similar name to url_decode
def url_encode:
# The helper function checks whether the input corresponds to one of the characters: !'()*
def recode: . as $c | [33,39,40,41,42] | index($c);
def hex: if . < 10 then 48 + . else 55 + . end;
@uri
| explode
# 37 ==> "%", 50 ==> "2"
| map( if recode then (37, 50, ((. - 32) | hex)) else . end )
| implode;



# Streaming utilities
def truncate_stream(stream):
. as $n | null | stream | . as $input | if (.[0]|length) > $n then setpath([0];$input[0][$n:]) else empty end;
Expand Down
24 changes: 24 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -2091,3 +2091,27 @@ try ("foobar" | .[1.5]) catch .
null
"Cannot index string with number"

# url_encode/url_decode
url_encode
"="
"%3D"

url_encode
"á"
"%C3%A1"

url_encode | url_decode
"á"
"á"

url_decode
"http%3A%2F%2Ffoo%20bar%2F"
"http://foo bar/"

(@uri | url_decode) == (url_encode | url_decode)
"http://foo bar/"
true

@uri | url_decode | url_encode
"è"
"%C3%A8"
Loading