URL decoding: Difference between revisions

Content deleted Content added
m →‎Raku: .. yet more shorter and logical code
→‎Raku: .. include both cases; remove original solution that handles multi-byte-chars incorrectly
Line 1,254: Line 1,254:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>my $url = "http%3A%2F%2Ffoo%20bar%2F";
<lang perl6>my @urls = < http%3A%2F%2Ffoo%20bar%2F
google.com/search?q=%60Abdu%27l-Bah%C3%A1 >;


say $url.subst: :g,
say .subst( :g,
/'%'(<:hexdigit>**2)/,
-> ($ord ) { chr(:16(~$ord)) }

# Alternately, you can use an in-place substitution:
$url ~~ s:g[ '%' (<:hexdigit> ** 2) ] = chr :16(~$0);
say $url;</lang>

To correctly decode also the second test case with multi-byte characters:

<lang perl6>my $url = "google.com/search?q=%60Abdu%27l-Bah%C3%A1";

say $url.subst: :g,
/ [ '%' ( <:hexdigit> ** 2 ) ]+ / ,
/ [ '%' ( <:hexdigit> ** 2 ) ]+ / ,
{ Blob.new((:16(~$_) for $0)).decode }
{ Blob.new((:16(~$_) for $0)).decode }
;</lang>
) for @urls;</lang>


=={{header|Red}}==
=={{header|Red}}==