Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added more documentation.

Passing undef to uri_escape() is now ok.
uri_unescape() might take multiple values.
  • Loading branch information...
commit 5145328f231149706f3ac85645558d50569c58e1 1 parent 9940ee7
Gisle Aas authored

Showing 1 changed file with 27 additions and 11 deletions. Show diff stats Hide diff stats

  1. +27 11 URI/Escape.pm
38 URI/Escape.pm
... ... @@ -1,5 +1,5 @@
1 1 #
2   -# $Id: Escape.pm,v 3.3 1996/01/05 12:55:49 aas Exp $
  2 +# $Id: Escape.pm,v 3.4 1996/02/05 17:46:10 aas Exp $
3 3 #
4 4
5 5 package URI::Escape;
@@ -14,18 +14,32 @@ uri_unescape - Unescape escaped characters
14 14
15 15 use URI::Escape;
16 16 $safe = uri_escape("10% is enough\n");
  17 + $verysafe = uri_escape("foo", "\0-\377");
17 18 $str = uri_unescape($safe);
18 19
19 20 =head1 DESCRIPTION
20 21
21   -This module provide functions to escape and unescape URIs strings.
  22 +This module provide functions to escape and unescape URI strings.
22 23 Some characters are regarded as "unsafe" and must be escaped in
23 24 accordance with RFC 1738. Escaped characters are represented by a
24 25 triplet consisting of the character "%" followed by two hexadecimal
25 26 digits.
26 27
27 28 The uri_escape() function takes an optional second argument that
28   -overrides the set of characters that are to be escaped.
  29 +overrides the set of characters that are to be escaped. The set is
  30 +specified as a string that can be used in a regular expression
  31 +character class (between [ ]). E.g.:
  32 +
  33 + \x00-\x1f\x7f-\xff # all control and hi-bit characters
  34 + a-z # all lower case characters
  35 + ^A-Za-z # everything not a letter
  36 +
  37 +The default set of characters to be escaped is:
  38 +
  39 + \x00-\x20"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF
  40 +
  41 +The module can also export the %escapes hash which contains the
  42 +mapping from all characters to the corresponding escape code.
29 43
30 44 =head1 SEE ALSO
31 45
@@ -38,6 +52,8 @@ require Exporter;
38 52 @EXPORT = qw(uri_escape uri_unescape);
39 53 @EXPORT_OK = qw(%escapes);
40 54
  55 +use Carp;
  56 +
41 57 # Build a char->hex map
42 58 for (0..255) {
43 59 $escapes{chr($_)} = sprintf("%%%02X", $_);
@@ -46,30 +62,30 @@ for (0..255) {
46 62 sub uri_escape
47 63 {
48 64 my($text, $patn) = @_;
  65 + return undef unless defined $text;
49 66 if (defined $patn){
50 67 unless (exists $subst{$patn}) {
51 68 # Because we can't compile regex we fake it with a cached sub
52 69 $subst{$patn} =
53 70 eval "sub {\$_[0] =~ s/([$patn])/\$escapes{\$1}/g; }";
54   - die $@ if $@;
  71 + croak "uri_escape: $@" if $@;
55 72 }
56 73 &{$subst{$patn}}($text);
57   - return $text;
  74 + } else {
  75 + # Default unsafe characters. (RFC1738 section 2.2)
  76 + $text =~ s/([\x00-\x20"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF])/$escapes{$1}/g; #"
58 77 }
59   - # Default unsafe characters. (RFC1738 section 2.2)
60   - $text =~ s/([\x00-\x20"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF])/$escapes{$1}/g; #"
61 78 $text;
62 79 }
63 80
64 81 sub uri_unescape
65 82 {
66   - my($text) = @_;
67   - return undef unless defined $text;
68 83 # Note from RFC1630: "Sequences which start with a percent sign
69 84 # but are not followed by two hexadecimal characters are reserved
70 85 # for future extension"
71   - $text =~ s/%([\dA-Fa-f][\dA-Fa-f])/chr(hex($1))/eg;
72   - $text;
  86 + my @copy = @_;
  87 + for (@copy) { s/%([\dA-Fa-f]{2})/chr(hex($1))/eg; }
  88 + wantarray ? @copy : $copy[0];
73 89 }
74 90
75 91 1;

0 comments on commit 5145328

Please sign in to comment.
Something went wrong with that request. Please try again.