Skip to content

Instantly share code, notes, and snippets.

@AlexDaniel
Created January 11, 2019 18:26
Show Gist options
  • Save AlexDaniel/158721b3ee4e4c2bbed8b8273fca02ae to your computer and use it in GitHub Desktop.
Save AlexDaniel/158721b3ee4e4c2bbed8b8273fca02ae to your computer and use it in GitHub Desktop.
my $RFC3986_unreserved = rx/<[0..9A..Za..z\-.~_]>/;
my $RFC3986_reserved = rx/<[:/?#\[\]@!$&'()*+,;=]>/;
my %escapes;
for (0..255) {
next if chr($_) ~~ /$RFC3986_unreserved/;
# for uri_decode
%escapes{sprintf("%%%02X", $_)} = chr($_);
}
my &enc = sub (Str:D $m) {
$m.encode.list.map(*.fmt('%%%02X')).join
}
sub uri_encode (Str:D $text) is export
{
return $text.comb.map({ if $RFC3986_unreserved or $RFC3986_reserved { $_ } else { &enc($_) }}).join;
}
sub uri_encode_component (Str:D $text) is export
{
return $text.comb.map({ if $RFC3986_unreserved { $_ } else { &enc($_) }}).join;
}
my &dec = sub ($m) {
Buf.new($m<bit>.list.map({:16($_.Str)})).decode;
}
sub uri_decode (Str:D $text) is export
{
return $text.subst(/[\%$<bit>=[<[0..9A..Fa..f]>** 2]]+/, &dec, :g);
}
sub uri_decode_component (Str:D $text) is export
{
return $text.subst(/[\%$<bit>=[<[0..9A..Fa..f]>** 2]]+/, &dec, :g);
}
use Test;
plan 23;
# encode
is uri_encode(" "), "%20%20", 'Encode " "';
is uri_encode("|abcå"), "%7Cabc%C3%A5", 'Encode "|abcå"';
is uri_encode("-.~_"), "-.~_", 'Encode -.~_';
is uri_encode(":/?#\[\]@!\$\&'()*+,;="), ":/?#\[\]@!\$\&'()*+,;=", 'Encode :/?#\[\]@!$&\'()*+,;=';
is uri_encode("abc"), "abc", 'Encode "abc"';
is uri_encode("<\">"), "%3C%22%3E", 'Encode "<\"';
is uri_encode("Hello World!"), "Hello%20World!", 'Encode "Hello World!"';
is uri_encode("http://perltricks.com/"), "http://perltricks.com/",
'Encode "http://perltricks.com/"';
is uri_encode("https://perltricks.com/"), "https://perltricks.com/",
'Encode "https://perltricks.com/"';
is uri_encode("https://www.example.com/🇩🇪"), "https://www.example.com/%F0%9F%87%A9%F0%9F%87%AA",
'Encode "https://www.example.com/🇩🇪"';
is uri_encode_component("Hello World!"), "Hello%20World%21", 'Encode components "Hello World!"';
is uri_encode_component('#$&+,/:;=?@'), '%23%24%26%2B%2C%2F%3A%3B%3D%3F%40',
'Encode components \'#$&+,/:;=?@\'';
is uri_encode_component("🇩🇪"), "%F0%9F%87%A9%F0%9F%87%AA", 'Encode components "🇩🇪"';
# decode
is uri_decode("%20%20"), " ", 'Decode to " "';
is uri_decode("%7Cabc%C3%A5"), "|abcå", 'Decode to "|abcå"';
is uri_decode("abc"), "abc", 'Decode to "abc"';
is uri_decode("~%2A%27%28%29"), "~*'()", 'Decode to "~*\'()"';
is uri_decode("%3C%22%3E"), "<\">", 'Decode to "<\"';
is uri_decode("http://perltricks.com/"), "http://perltricks.com/",
'Decode to "http://perltricks.com/"';
is uri_decode("https://perltricks.com/"), "https://perltricks.com/",
'Decode tp "https://perltricks.com/"';
is uri_decode("https://www.example.com/%F0%9F%87%A9%F0%9F%87%AA"), "https://www.example.com/🇩🇪",
'Decode "https://www.example.com/%F0%9F%87%A9%F0%9F%87%AA"';
is uri_decode_component('%23%24%26%2B%2C%2F%3A%3B%3D%3F%40'), '#$&+,/:;=?@',
'Decode components to \'#$&+,/:;=?@\'';
is uri_decode_component("%F0%9F%87%A9%F0%9F%87%AA"), "🇩🇪", 'Decode components "🇩🇪"';
# vim: ft=perl6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment