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

filter: Statementstmt 100% 144 / 144 Branchbran 81.4% 44 / 54 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 2 totaltotal 92.9% 210 / 226 CC 30 SCAR 22.1
CC
30
Cov
94%
decode_punycode
15.5
encode_punycode
14.0
_adapt
3.0
CRAP 9.1
« blib/lib/Net/IDN/UTS46.pm ↑ Summary blib/lib/Net/IDN/Encode.pm »
1 package Net::IDN::Punycode::PP;
2  
3 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = "2.502";
13  
14 2 our @ISA = qw(Exporter);
15 2 our @EXPORT = ();
16 2 our @EXPORT_OK = qw(encode_punycode decode_punycode);
17 2 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
18  
19 2 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use constant BASE => 36;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use constant TMIN => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use constant TMAX => 26;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use constant SKEW => 38;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use constant DAMP => 700;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use constant INITIAL_BIAS => 72;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use constant INITIAL_N => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 ▶use constant UNICODE_MIN => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use constant UNICODE_MAX => 0x10FFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 my $Delimiter = chr 0x2D;
33 2 my $BasicRE = "\x00-\x7f";
34 2 my $PunyRE = "A-Za-z0-9";
35  
36 464 ▶sub _adapt {
Subroutine _adapt: called CC=3 SCAR=11.0
37 464 my($delta, $numpoints, $firsttime) = @_;
38 464 ▶ $delta = int($firsttime ? $delta / DAMP : $delta / 2);
Branch: 1 branch
BranchTrueFalseTotal
$firsttime ? : 42 422 464
39 464 $delta += int($delta / $numpoints);
40 464 my $k = 0;
41 464 ▶ while ($delta > int(((BASE - TMIN) * TMAX) / 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 455) 86 464 550
42 86 $delta /= BASE - TMIN;
43 86 $k += BASE;
44 }
45 464 return $k + (((BASE - TMIN + 1) * $delta) / ($delta + SKEW));
46 }
47  
48 21 ▶sub decode_punycode {
Subroutine decode_punycode: called CC=15 SCAR=27.4
Pod undocumented
49 21 ▶ die("Usage: Net::IDN::Punycode::decode_punycode(input)") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 21 21
50 2 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 21 my $input = shift;
53  
54 21 my $n = INITIAL_N;
55 21 my $i = 0;
56 21 my $bias = INITIAL_BIAS;
57 21 my @output;
58  
59 21 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 21 21
60 21 ▶ return '' unless length $input;
Branch: 1 branch
BranchTrueFalseTotal
unless length $input 0 21 21
61  
62 21 ▶ if($input =~ s/(.*)$Delimiter//os) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ s/(.*)$Delimiter//os) 10 11 21
63 10 my $base_chars = $1;
64 10 ▶ croak("non-base character in input for decode_punycode")
Branch: 1 branch
BranchTrueFalseTotal
if $base_chars =~ /[^$BasicRE]/so 0 10 10
65 if $base_chars =~ m/[^$BasicRE]/os;
66 10 push @output, split //, $base_chars;
67 }
68 21 my $code = $input;
69  
70 21 ▶ croak('invalid digit in input for decode_punycode') if $code =~ m/[^$PunyRE]/os;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /[^$PunyRE]/so 0 21 21
71  
72 21 utf8::downgrade($input); ## handling failure of downgrade is more expensive than
73 ## doing the above regexp w/ utf8 semantics
74  
75 21 ▶ while(length $code)
Branch: 1 branch
BranchTrueFalseTotal
if (length $code) 232 21 253
76 {
77 232 my $oldi = $i;
78 232 my $w = 1;
79 LOOP:
80 232 for (my $k = BASE; 1; $k += BASE) {
81 431 my $cp = substr($code, 0, 1, '');
82 431 ▶ croak("incomplete encoded code point in decode_punycode") if !defined $cp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cp 0 431 431
83 431 my $digit = ord $cp;
84  
85 ## NB: this depends on the PunyRE catching invalid digit characters
86 ## before they turn up here
87 ##
88 431 ▶ $digit = $digit < 0x40 ? $digit + (26-0x30) : ($digit & 0x1f) -1;
Branch: 1 branch
BranchTrueFalseTotal
$digit < 64 ? : 116 315 431
89  
90 431 $i += $digit * $w;
91 431 my $t = $k - $bias;
92 431 ▶ $t = $t < TMIN ? TMIN : $t > TMAX ? TMAX : $t;
Branch: 2 branches
BranchTrueFalseTotal
$t < 1 ? : 71 360 431
$t > 26 ? : 280 80 360
93  
94 431 ▶ last LOOP if $digit < $t;
Branch: 1 branch
BranchTrueFalseTotal
if $digit < $t 232 199 431
95 199 $w *= (BASE - $t);
96 }
97 232 $bias = _adapt($i - $oldi, @output + 1, $oldi == 0);
98 232 $n += $i / (@output + 1);
99 232 $i = $i % (@output + 1);
100 232 ▶ croak('invalid code point') if $n < UNICODE_MIN or $n > UNICODE_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 or $n > 1114111 0 232 232
Condition33%1/3
$n < 0 or $n > 1114111
l!l&&r!l&&!r
00232
MC/DC0%0/2
$n < 0 or $n > 1114111
$n < 0 $n > 1114111
Truth table33%1/3
A or B
A= $n < 0B= $n > 1114111
ABresult
1X1
011
000
101 232 splice(@output, $i, 0, chr($n));
102 232 $i++;
103 }
104 21 return join '', @output;
105 }
106  
107 21 ▶sub encode_punycode {
Subroutine encode_punycode: called CC=14 SCAR=26.4
Pod undocumented
108 21 ▶ die("Usage: Net::IDN::Punycode::encode_punycode(input)") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 21 21
109 2 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 21 my $input = shift;
112 21 my $input_length = length $input;
113  
114 ## my $output = join '', $input =~ m/([$BasicRE]+)/og; ## slower
115 21 my $output = $input; $output =~ s/[^$BasicRE]+//ogs;
116  
117 21 my $h = my $bb = length $output;
118 21 ▶ $output .= $Delimiter if $bb > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bb > 0 10 11 21
119 21 utf8::downgrade($output); ## no unnecessary use of utf8 semantics
120  
121 21 my @input = map ord, split //, $input;
122 21 my @chars = sort { $a<=> $b } grep { $_ >= INITIAL_N } @input;
123  
124 21 my $n = INITIAL_N;
125 21 my $delta = 0;
126 21 my $bias = INITIAL_BIAS;
127  
128 21 foreach my $m (@chars) {
129 232 ▶ next if $m < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $m < $n 54 178 232
130 178 $delta += ($m - $n) * ($h + 1);
131 178 $n = $m;
132 178 ▶ for(my $i = 0; $i < $input_length; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $input_length) 3432 178 3610
133 {
134 3432 my $c = $input[$i];
135 3432 ▶ $delta++ if $c < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $c < $n 1893 1539 3432
136 3432 ▶ if ($c == $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c == $n) 232 3200 3432
137 232 my $q = $delta;
138 LOOP:
139 232 for (my $k = BASE; 1; $k += BASE) {
140 431 my $t = $k - $bias;
141 431 ▶ $t = $t < TMIN ? TMIN : $t > TMAX ? TMAX : $t;
Branch: 2 branches
BranchTrueFalseTotal
$t < 1 ? : 71 360 431
$t > 26 ? : 280 80 360
142  
143 431 ▶ last LOOP if $q < $t;
Branch: 1 branch
BranchTrueFalseTotal
if $q < $t 232 199 431
144  
145 199 my $o = $t + (($q - $t) % (BASE - $t));
146 199 ▶ $output .= chr $o + ($o < 26 ? 0x61 : 0x30-26);
Branch: 1 branch
BranchTrueFalseTotal
$o < 26 ? : 83 116 199
147  
148 199 $q = int(($q - $t) / (BASE - $t));
149 }
150 232 ▶ croak("input exceeds punycode limit") if $q > BASE;
Branch: 1 branch
BranchTrueFalseTotal
if $q > 36 0 232 232
151 232 ▶ $output .= chr $q + ($q < 26 ? 0x61 : 0x30-26);
Branch: 1 branch
BranchTrueFalseTotal
$q < 26 ? : 232 0 232
152  
153 232 $bias = _adapt($delta, $h + 1, $h == $bb);
154 232 $delta = 0;
155 232 $h++;
156 }
157 }
158 178 $delta++;
159 178 $n++;
160 }
161 21 return $output;
162 }
163  
164 2 1;
165 __END__
« blib/lib/Net/IDN/UTS46.pm ↑ Summary blib/lib/Net/IDN/Encode.pm »