Skip to content

Commit

Permalink
uri_escape: allow characters to escape to be specified as a Regexp ob…
Browse files Browse the repository at this point in the history
…ject
  • Loading branch information
haarg authored and oalders committed Oct 11, 2022
1 parent 2f88ac6 commit a3f9616
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/URI/Escape.pm
Expand Up @@ -71,6 +71,13 @@ as the reserved characters. I.e. the default is:
"^A-Za-z0-9\-\._~"
The second argument can also be specified as a regular expression object:
qr/[^A-Za-z]/
Any strings matched by this regular expression will have all of their
characters escaped.
=item uri_escape_utf8( $string )
=item uri_escape_utf8( $string, $unsafe )
Expand Down Expand Up @@ -162,6 +169,12 @@ sub uri_escape {
return undef unless defined $text;
my $re;
if (defined $patn){
if (ref $patn eq 'Regexp') {
$text =~ s{($patn)}{
join('', map +($escapes{$_} || _fail_hi($_)), split //, "$1")
}ge;
return $text;
}
$re = $subst{$patn};
if (!defined $re) {
$re = $patn;
Expand Down
12 changes: 12 additions & 0 deletions t/escape.t
Expand Up @@ -104,6 +104,18 @@ like join('', warnings {
}xs,
'bad escapes emit warnings';

is
uri_escape ('abcd-[]', qr/[bc]/),
'a%62%63d-[]',
'allows regexp objects',
;

is
uri_escape ('a12b21c12d', qr/12/),
'a%31%32b21c%31%32d',
'allows regexp objects matching multiple characters',
;

is $escapes{"%"}, "%25";

is uri_escape_utf8("|abcå"), "%7Cabc%C3%A5";
Expand Down

0 comments on commit a3f9616

Please sign in to comment.