Jump to content

URL decoding: Difference between revisions

→‎Raku: .. include both cases; remove original solution that handles multi-byte-chars incorrectly
m (→‎Raku: .. yet more shorter and logical code)
(→‎Raku: .. include both cases; remove original solution that handles multi-byte-chars incorrectly)
Line 1,254:
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>my $url@urls = "< http%3A%2F%2Ffoo%20bar%2F";
<lang perl6>my $url = " google.com/search?q=%60Abdu%27l-Bah%C3%A1" >;
 
say $url.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 ) ]+ / ,
{ Blob.new((:16(~$_) for $0)).decode }
) for @urls;</lang>
 
=={{header|Red}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.