summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGraham Knop <haarg@haarg.org>2022-10-10 20:12:21 +0200
committerOlaf Alders <olaf@wundersolutions.com>2022-10-11 10:47:25 -0400
commita3f96169b002e1fa747713654edfa9f528d17cbb (patch)
treefee02187c2bff0fc96bae11345cdf56d20c44145
parent2f88ac69f58f548eeb21d942690348588c8aeff2 (diff)
downloaduri-a3f96169b002e1fa747713654edfa9f528d17cbb.tar.gz
uri_escape: allow characters to escape to be specified as a Regexp object
-rw-r--r--lib/URI/Escape.pm13
-rw-r--r--t/escape.t12
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/URI/Escape.pm b/lib/URI/Escape.pm
index 4d8bc5e..87479ca 100644
--- a/lib/URI/Escape.pm
+++ b/lib/URI/Escape.pm
@@ -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 )
@@ -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;
diff --git a/t/escape.t b/t/escape.t
index 16694dd..6450b18 100644
--- a/t/escape.t
+++ b/t/escape.t
@@ -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";