blib/lib/URI/Encode.pm

filter: Statementstmt 96.2% 76 / 79 Branchbran 71.4% 20 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 88.5% 116 / 131 CC 16 SCAR 14.0
CC
16
Cov
88%
encode
8.0
_encode_literal_percent
5.6
_get_encoded_char
2.3
CRAP 4.0
↑ Summary
1 package URI::Encode;
2  
3 #######################
4 # LOAD MODULES
5 #######################
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #######################
14 # VERSION
15 #######################
16 2 our $VERSION = '1.1.1';
17  
18 #######################
19 # EXPORT
20 #######################
21 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 our (@EXPORT_OK);
23  
24 2 @EXPORT_OK = qw(uri_encode uri_decode);
25  
26 #######################
27 # SETTINGS
28 #######################
29  
30 # Reserved characters
31 2 my $reserved_re
32 = qr{([^a-zA-Z0-9\-\_\.\~\!\*\'\(\)\;\:\@\&\=\+\$\,\/\?\#\[\]\%])}x;
33  
34 # Un-reserved characters
35 2 my $unreserved_re = qr{([^a-zA-Z0-9\Q-_.~\E\%])}x;
36  
37 # Encoded character set
38 2 my $encoded_chars = qr{%([a-fA-F0-9]{2})}x;
39  
40 #######################
41 # CONSTRUCTOR
42 #######################
43 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
44 6 my ( $class, @in ) = @_;
45  
46 # Check Input
47 6 my $defaults = {
48  
49 # this module, unlike URI::Escape,
50 # does not encode reserved characters
51 encode_reserved => 0,
52  
53 # Allow Double encoding?
54 # defaults to YES
55 double_encode => 1,
56 };
57  
58 6 my $input = {};
59 0 ▶ if ( ref $in[0] eq 'HASH' ) { $input = $in[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in[0] eq 'HASH') { } 0 6 6
60 6 else { $input = {@in}; }
61  
62 # Set options
63 my $options = {
64  
65 # Defaults
66 6 %{$defaults},
67  
68 # Input
69 6 %{$input},
70  
71 # Encoding Map
72 enc_map =>
73 1536 { ( map { chr($_) => sprintf( "%%%02X", $_ ) } ( 0 ... 255 ) ) },
74  
75 # Decoding Map
76 dec_map =>
77 6 { ( map { sprintf( "%02X", $_ ) => chr($_) } ( 0 ... 255 ) ), },
78 };
79  
80 # Return
81 6 my $self = bless $options, $class;
82 6 return $self;
83 } ## end sub new
84  
85 #######################
86 # ENCODE
87 #######################
88 9 ▶sub encode {
Subroutine encode: called CC=8 SCAR=20.8
Pod documented
89 9 my ( $self, $data, $options ) = @_;
90  
91 # Check for data
92 # Allow to be '0'
93 9 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 9 9
94  
95 9 my $enc_res = $self->{encode_reserved};
96 9 my $double_encode = $self->{double_encode};
97  
98 9 ▶ if ( defined $options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options) 7 2 9
99 7 ▶ if ( ref $options eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options eq 'HASH') { } 5 2 7
100 $enc_res = $options->{encode_reserved}
101 5 ▶ if exists $options->{encode_reserved};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"encode_reserved"} 2 3 5
102 $double_encode = $options->{double_encode}
103 5 ▶ if exists $options->{double_encode};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"double_encode"} 3 2 5
104 } ## end if ( ref $options eq 'HASH')
105 else {
106 2 $enc_res = $options;
107 }
108 } ## end if ( defined $options )
109  
110 # UTF-8 encode
111 9 $data = Encode::encode( 'utf-8-strict', $data );
112  
113 # Encode a literal '%'
114 9 ▶ if ($double_encode) { $data =~ s{(\%)}{$self->_get_encoded_char($1)}gex; }
Branch: 1 branch
BranchTrueFalseTotal
if ($double_encode) { } 7 2 9
115 2 else { $data =~ s{(\%)(.*)}{$self->_encode_literal_percent($1, $2)}gex; }
116  
117 # Percent Encode
118 9 ▶ if ($enc_res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc_res) { } 4 5 9
119 4 $data =~ s{$unreserved_re}{$self->_get_encoded_char($1)}gex;
120 }
121 else {
122 5 $data =~ s{$reserved_re}{$self->_get_encoded_char($1)}gex;
123 }
124  
125 # Done
126 9 return $data;
127 } ## end sub encode
128  
129 #######################
130 # DECODE
131 #######################
132 4 ▶sub decode {
Subroutine decode: called CC=2 SCAR=7.0
Pod documented
133 4 my ( $self, $data ) = @_;
134  
135 # Check for data
136 # Allow to be '0'
137 4 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 4 4
138  
139 # Percent Decode
140 4 $data =~ s{$encoded_chars}{ $self->_get_decoded_char($1) }gex;
141  
142 4 return $data;
143 } ## end sub decode
144  
145 #######################
146 # EXPORTED FUNCTIONS
147 #######################
148  
149 # Encoder
150 4 ▶sub uri_encode { return __PACKAGE__->new()->encode(@_); }
Subroutine uri_encode: called CC=1 SCAR=0.0
Pod documented
151  
152 # Decoder
153 1 ▶sub uri_decode { return __PACKAGE__->new()->decode(@_); }
Subroutine uri_decode: called CC=1 SCAR=0.0
Pod documented
154  
155 #######################
156 # INTERNAL
157 #######################
158  
159  
160 85 ▶sub _get_encoded_char {
Subroutine _get_encoded_char: called CC=2 SCAR=8.1
161 85 my ( $self, $char ) = @_;
162 85 ▶ return $self->{enc_map}->{$char} if exists $self->{enc_map}->{$char};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"enc_map"}{$char} 85 0 85
163 0 return $char;
164 } ## end sub _get_encoded_char
165  
166  
167 2 ▶sub _encode_literal_percent {
Subroutine _encode_literal_percent: called CC=5 SCAR=17.3
168 2 my ( $self, $char, $post ) = @_;
169  
170 2 ▶ return $self->_get_encoded_char($char) if not defined $post;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $post 0 2 2
171  
172 2 my $return_char;
173 2 ▶ if ( $post =~ m{^([a-fA-F0-9]{2})}x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($post =~ /^([a-fA-F0-9]{2})/x) 2 0 2
174 2 ▶ if ( exists $self->{dec_map}->{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dec_map"}{$1}) 2 0 2
175 2 $return_char = join( '', $char, $post );
176 }
177 } ## end if ( $post =~ m{^([a-fA-F0-9]{2})}x)
178  
179 2 ▶ $return_char ||= join( '', $self->_get_encoded_char($char), $post );
Condition33%1/3
$return_char ||= join("", $self->_get_encoded_char($char), $post)
l!l&&r!l&&!r
200
MC/DC0%0/2
$return_char ||= join("", $self->_get_encoded_char($char), $post)
$return_char join("", $self->_get_encoded_char($char), $post)
Truth table33%1/3
A ||= B
A= $return_charB= join("", $self->_get_encoded_char($char), $post)
ABresult
1X1
011
000
180 2 return $return_char;
181 } ## end sub _encode_literal_percent
182  
183  
184 14 ▶sub _get_decoded_char {
Subroutine _get_decoded_char: called CC=2 SCAR=8.1
185 14 my ( $self, $char ) = @_;
186 return $self->{dec_map}->{ uc($char) }
187 14 ▶ if exists $self->{dec_map}->{ uc($char) };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"dec_map"}{uc $char} 14 0 14
188 0 return $char;
189 } ## end sub _get_decoded_char
190  
191 #######################
192 2 1;
193  
194 __END__
↑ Summary