blib/lib/Net/IDN/Punycode/PP.pm

filter: Statementstmt 100% 168 / 168 Branchbran 95.9% 71 / 74 Conditioncond 92.8% 13 / 14 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 2 totaltotal 97.5% 283 / 290 CC 44 SCAR 24.9
CC
44
Cov
98%
encode_punycode
21.0
decode_punycode
18.0
_die
3.0
CRAP 12.1
« blib/lib/Net/IDN/UTS46.pm ↑ Summary blib/lib/Net/IDN/Encode.pm »
1 package Net::IDN::Punycode::PP;
2  
3 19 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 19 our $VERSION = "2.600";
13  
14 19 our @ISA = qw(Exporter);
15 19 our @EXPORT = ();
16 19 our @EXPORT_OK = qw(encode_punycode decode_punycode);
17 19 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
18  
19 ## no "use integer" - IV arithmetic wraps below PUNYCODE_MAXINT on 32-bit
20 ## perls, whereas NV arithmetic is exact up to 2**53 everywhere
21  
22 19 ▶use constant BASE => 36;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 19 ▶use constant TMIN => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 19 ▶use constant TMAX => 26;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 19 ▶use constant SKEW => 38;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 19 ▶use constant DAMP => 700;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 19 ▶use constant INITIAL_BIAS => 72;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 19 ▶use constant INITIAL_N => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 19 ▶use constant UNICODE_MAX => 0x10FFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 19 ▶use constant PUNYCODE_MAXINT => 0xFFFFFFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 19 ▶use constant SURROGATE_MIN => 0xD800;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 19 ▶use constant SURROGATE_MAX => 0xDFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 19 my $Delimiter = chr 0x2D;
36 19 my $BasicRE = "\x00-\x7f";
37 19 my $PunyRE = "A-Za-z0-9";
38  
39 142482 ▶sub _adapt {
Subroutine _adapt: called CC=3 SCAR=11.0
40 142482 my($delta, $numpoints, $firsttime) = @_;
41 142482 ▶ $delta = int($firsttime ? $delta / DAMP : $delta / 2);
Branch: 1 branch
BranchTrueFalseTotal
$firsttime ? : 2951 139531 142482
42 142482 $delta += int($delta / $numpoints);
43 142482 my $k = 0;
44 142482 ▶ while ($delta > int(((BASE - TMIN) * TMAX) / 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 455) 807 142482 143289
45 807 $delta = int($delta / (BASE - TMIN));
46 807 $k += BASE;
47 }
48 142482 return $k + int(((BASE - TMIN + 1) * $delta) / ($delta + SKEW));
49 }
50  
51 ## die, not croak - Carp itself fails while it formats a malformed argument
52 12 ▶sub _die {
Subroutine _die: called CC=3 SCAR=11.0
53 12 my $frame = 0;
54 12 ▶ $frame++ while ((caller $frame)[0] || '') eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
((caller $frame)[0] || "") eq __PACKAGE__ and do { ++$frame } 20 12 32
Condition50%1/2
(caller $frame)[0] || ''
l!l
320
MC/DC0%0/1
(caller $frame)[0] || ''
(caller $frame)[0]
Truth table50%1/2
A
A= (caller $frame)[0]
Aresult
11
00
55 12 my ($file, $line) = (caller $frame)[1, 2];
56 12 die "$_[0] at $file line $line.\n";
57 }
58  
59 3075 ▶sub _check_input {
Subroutine _check_input: called CC=3 SCAR=11.0
60 3075 my ($input, $message) = @_;
61 3075 ▶ unless (defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input) 2 3073 3075
62 2 warnings::warnif('uninitialized',
63 'Use of uninitialized value in subroutine entry');
64 2 return;
65 }
66 3073 $input = "$input"; ## stringify an overloaded object once
67 3073 ▶ _die($message) unless utf8::valid($input);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::valid($input) 8 3065 3073
68 3065 return $input;
69 }
70  
71 134 ▶sub decode_punycode {
Subroutine decode_punycode: called CC=18 SCAR=28.9
Pod undocumented
72 134 ▶ _die("Usage: Net::IDN::Punycode::decode_punycode(input)") unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 2 132 134
73 19 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 132 my $input = _check_input(shift,
76 "non-base character in input for decode_punycode");
77 128 ▶ return '' unless defined $input && length $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input and length $input 2 126 128
Condition100%3/3
defined $input and length $input
!ll&&!rl&&r
11126
MC/DC100%2/2
defined $input and length $input
defined $input length $input
Truth table100%3/3
A and B
A= defined $inputB= length $input
ABresult
0X0
100
111
78  
79 126 my $n = INITIAL_N;
80 126 my $i = 0;
81 126 my $bias = INITIAL_BIAS;
82 126 my @output;
83  
84 126 ▶ if($input =~ s/(.*)$Delimiter//os) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ s/(.*)$Delimiter//os) 66 60 126
85 66 my $base_chars = $1;
86 66 ▶ croak("non-base character in input for decode_punycode")
Branch: 1 branch
BranchTrueFalseTotal
if $base_chars =~ /[^$BasicRE]/so 7 59 66
87 if $base_chars =~ m/[^$BasicRE]/os;
88 59 push @output, split //, $base_chars;
89 }
90 119 my $code = $input;
91  
92 119 ▶ croak("non-base character in input for decode_punycode")
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /[^$BasicRE]/so 6 113 119
93 if $code =~ m/[^$BasicRE]/os;
94 113 ▶ croak('invalid digit in input for decode_punycode') if $code =~ m/[^$PunyRE]/os;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /[^$PunyRE]/so 7 106 113
95  
96 106 utf8::downgrade($code); ## handling failure of downgrade is more expensive than
97 ## doing the above regexp w/ utf8 semantics
98  
99 106 ▶ while(length $code)
Branch: 1 branch
BranchTrueFalseTotal
if (length $code) 344 23 367
100 {
101 344 my $oldi = $i;
102 344 my $w = 1;
103 LOOP:
104 344 for (my $k = BASE; 1; $k += BASE) {
105 994 my $cp = substr($code, 0, 1, '');
106 994 ▶ croak("incomplete encoded code point in decode_punycode") if !length $cp;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cp 24 970 994
107 970 my $digit = ord $cp;
108  
109 ## NB: this depends on the PunyRE catching invalid digit characters
110 ## before they turn up here
111 ##
112 970 ▶ $digit = $digit < 0x40 ? $digit + (26-0x30) : ($digit & 0x1f) -1;
Branch: 1 branch
BranchTrueFalseTotal
$digit < 64 ? : 452 518 970
113  
114 970 ▶ croak("input exceeds punycode limit")
Branch: 1 branch
BranchTrueFalseTotal
if $digit > (4294967295 - $i) / $w 21 949 970
115 if $digit > (PUNYCODE_MAXINT - $i) / $w;
116 949 $i += $digit * $w;
117 949 my $t = $k - $bias;
118 949 ▶ $t = $t < TMIN ? TMIN : $t > TMAX ? TMAX : $t;
Branch: 2 branches
BranchTrueFalseTotal
$t < 1 ? : 229 720 949
$t > 26 ? : 612 108 720
119  
120 949 ▶ last LOOP if $digit < $t;
Branch: 1 branch
BranchTrueFalseTotal
if $digit < $t 299 650 949
121 650 $w *= (BASE - $t);
122 }
123 299 my $len = @output + 1;
124 299 my $step = int($i / $len);
125 299 $bias = _adapt($i - $oldi, $len, $oldi == 0);
126 299 ▶ croak('invalid code point') if $step > UNICODE_MAX - $n;
Branch: 1 branch
BranchTrueFalseTotal
if $step > 1114111 - $n 31 268 299
127 268 $n += $step;
128 268 ▶ croak('invalid code point')
Branch: 1 branch
BranchTrueFalseTotal
if $n >= 55296 and $n <= 57343 7 261 268
Condition100%3/3
$n >= 55296 and $n <= 57343
!ll&&!rl&&r
26017
MC/DC100%2/2
$n >= 55296 and $n <= 57343
$n >= 55296 $n <= 57343
Truth table100%3/3
A and B
A= $n >= 55296B= $n <= 57343
ABresult
0X0
100
111
129 if $n >= SURROGATE_MIN && $n <= SURROGATE_MAX;
130 261 $i = $i % $len;
131 261 splice(@output, $i, 0, chr($n));
132 261 $i++;
133 }
134 23 return join '', @output;
135 }
136  
137 2945 ▶sub encode_punycode {
Subroutine encode_punycode: called CC=21 SCAR=30.5
Pod undocumented
138 2945 ▶ _die("Usage: Net::IDN::Punycode::encode_punycode(input)") unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 2 2943 2945
139 19 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
140  
141 2943 my $input = _check_input(shift,
142 "malformed UTF-8 in input for encode_punycode");
143 2939 ▶ return '' unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 1 2938 2939
144 2938 my $input_length = length $input;
145  
146 ## my $output = join '', $input =~ m/([$BasicRE]+)/og; ## slower
147 2938 my $output = $input; $output =~ s/[^$BasicRE]+//ogs;
148  
149 2938 my $h = my $bb = length $output;
150 2938 ▶ $output .= $Delimiter if $bb > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bb > 0 107 2831 2938
151 2938 utf8::downgrade($output); ## no unnecessary use of utf8 semantics
152  
153 2938 my @input = map ord, split //, $input;
154 2938 my @chars = sort { $a<=> $b } grep { $_ >= INITIAL_N } @input;
155 croak("invalid code point") if grep {
156 2938 ▶ $_ > UNICODE_MAX || ($_ >= SURROGATE_MIN && $_ <= SURROGATE_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 1114111 or $_ >= 55296 and $_ <= 57343;} @chars 21 2917 2938
Condition100%6/6
$_ > 1114111 || $_ >= 55296 && $_ <= 57343
l!l&&r!l&&!r
147142197
$_ >= 55296 and $_ <= 57343
!ll&&!rl&&r
55980862177
MC/DC100%3/3
$_ > 1114111 || $_ >= 55296 && $_ <= 57343
$_ > 1114111 $_ >= 55296 $_ <= 57343
Truth table100%4/4
A || B and C
A= $_ > 1114111B= $_ >= 55296C= $_ <= 57343
ABCresult
1XX1
0111
00X0
0100
157 } @chars;
158  
159 2917 my $n = INITIAL_N;
160 2917 my $delta = 0;
161 2917 my $bias = INITIAL_BIAS;
162  
163 2917 foreach my $m (@chars) {
164 142197 ▶ next if $m < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $m < $n 1560 140637 142197
165 140637 ▶ croak("input exceeds punycode limit")
Branch: 1 branch
BranchTrueFalseTotal
if $m - $n > (4294967295 - $delta) / ($h + 1) 7 140630 140637
166 if $m - $n > (PUNYCODE_MAXINT - $delta) / ($h + 1);
167 140630 $delta += ($m - $n) * ($h + 1);
168 140630 $n = $m;
169 140630 ▶ for(my $i = 0; $i < $input_length; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $input_length) 7130908 140623 7271531
170 {
171 7130908 my $c = $input[$i];
172 7130908 ▶ $delta++ if $c < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $c < $n 3548081 3582827 7130908
173 7130908 ▶ if ($c == $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c == $n) 142190 6988718 7130908
174 142190 ▶ croak("input exceeds punycode limit") if $delta > PUNYCODE_MAXINT;
Branch: 1 branch
BranchTrueFalseTotal
if $delta > 4294967295 7 142183 142190
175 142183 my $q = $delta;
176 LOOP:
177 142183 for (my $k = BASE; 1; $k += BASE) {
178 221090 my $t = $k - $bias;
179 221090 ▶ $t = $t < TMIN ? TMIN : $t > TMAX ? TMAX : $t;
Branch: 2 branches
BranchTrueFalseTotal
$t < 1 ? : 6373 214717 221090
$t > 26 ? : 156243 58474 214717
180  
181 221090 ▶ last LOOP if $q < $t;
Branch: 1 branch
BranchTrueFalseTotal
if $q < $t 142183 78907 221090
182  
183 78907 my $o = $t + (($q - $t) % (BASE - $t));
184 78907 ▶ $output .= chr $o + ($o < 26 ? 0x61 : 0x30-26);
Branch: 1 branch
BranchTrueFalseTotal
$o < 26 ? : 13160 65747 78907
185  
186 78907 $q = int(($q - $t) / (BASE - $t));
187 }
188 142183 ▶ croak("input exceeds punycode limit") if $q >= BASE;
Branch: 1 branch
BranchTrueFalseTotal
if $q >= 36 0 142183 142183
189 142183 ▶ $output .= chr $q + ($q < 26 ? 0x61 : 0x30-26);
Branch: 1 branch
BranchTrueFalseTotal
$q < 26 ? : 142183 0 142183
190  
191 142183 $bias = _adapt($delta, $h + 1, $h == $bb);
192 142183 $delta = 0;
193 142183 $h++;
194 }
195 }
196 140623 ▶ croak("input exceeds punycode limit") if $delta >= PUNYCODE_MAXINT;
Branch: 1 branch
BranchTrueFalseTotal
if $delta >= 4294967295 0 140623 140623
197 140623 $delta++;
198 140623 $n++;
199 }
200 2903 return $output;
201 }
202  
203 19 1;
204 __END__
« blib/lib/Net/IDN/UTS46.pm ↑ Summary blib/lib/Net/IDN/Encode.pm »