URI/_punycode.pm

filter: Statementstmt 96.8% 121 / 125 Branchbran 83.3% 45 / 54 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 94.4% 17 / 18 Podpod 100% 2 / 2 totaltotal 90.6% 194 / 214 CC 33 SCAR 19.4
CC
33
Cov
91%
encode_punycode
13.0
decode_punycode
10.1
_code_point
6.1
CRAP 7.0
« URI/urn.pm ↑ Summary URI/data.pm »
1 package URI::_punycode;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '5.40';
7  
8 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 our @EXPORT = qw(encode_punycode decode_punycode);
10  
11 4 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $DEBUG = 0;
14  
15 4 ▶use constant BASE => 36;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use constant TMIN => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use constant TMAX => 26;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use constant SKEW => 38;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use constant DAMP => 700;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use constant INITIAL_BIAS => 72;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use constant INITIAL_N => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 my $Delimiter = chr 0x2D;
24 4 my $BasicRE = qr/[\x00-\x7f]/;
25  
26 0 ▶sub _croak { require Carp; Carp::croak(@_); }
Subroutine _croak: not called CC=1 SCAR=6.9
27  
28 169 ▶sub _digit_value {
Subroutine _digit_value: called CC=4 SCAR=14.1
29 169 my $code = shift;
30 169 ▶ return ord($code) - ord("A") if $code =~ /[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /[A-Z]/ 3 166 169
31 166 ▶ return ord($code) - ord("a") if $code =~ /[a-z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /[a-z]/ 125 41 166
32 41 ▶ return ord($code) - ord("0") + 26 if $code =~ /[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /[0-9]/ 41 0 41
33 0 return;
34 }
35  
36 203 ▶sub _code_point {
Subroutine _code_point: called CC=5 SCAR=18.1
37 203 my $digit = shift;
38 203 ▶ return $digit + ord('a') if 0 <= $digit && $digit <= 25;
Branch: 1 branch
BranchTrueFalseTotal
if 0 <= $digit and $digit <= 25 155 48 203
Condition66%2/3
0 <= $digit and $digit <= 25
!ll&&!rl&&r
048155
MC/DC50%1/2
0 <= $digit and $digit <= 25
0 <= $digit $digit <= 25
Truth table66%2/3
A and B
A= 0 <= $digitB= $digit <= 25
ABresult
0X0
100
111
39 48 ▶ return $digit + ord('0') - 26 if 26 <= $digit && $digit <= 36;
Branch: 1 branch
BranchTrueFalseTotal
if 26 <= $digit and $digit <= 36 48 0 48
Condition33%1/3
26 <= $digit and $digit <= 36
!ll&&!rl&&r
0048
MC/DC0%0/2
26 <= $digit and $digit <= 36
26 <= $digit $digit <= 36
Truth table33%1/3
A and B
A= 26 <= $digitB= $digit <= 36
ABresult
0X0
100
111
40 0 die 'NOT COME HERE';
41 }
42  
43 185 ▶sub _adapt {
Subroutine _adapt: called CC=3 SCAR=11.0
44 185 my ($delta, $numpoints, $firsttime) = @_;
45 185 ▶ $delta = $firsttime ? $delta / DAMP : $delta / 2;
Branch: 1 branch
BranchTrueFalseTotal
$firsttime ? : 46 139 185
46 185 $delta += $delta / $numpoints;
47 185 my $k = 0;
48 185 ▶ while ($delta > ((BASE - TMIN) * TMAX) / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 455) 22 185 207
49 22 $delta /= BASE - TMIN;
50 22 $k += BASE;
51 }
52 185 return $k + (((BASE - TMIN + 1) * $delta) / ($delta + SKEW));
53 }
54  
55 19 ▶sub decode_punycode {
Subroutine decode_punycode: called CC=10 SCAR=23.1
Pod documented
56 19 my $code = shift;
57  
58 19 my $n = INITIAL_N;
59 19 my $i = 0;
60 19 my $bias = INITIAL_BIAS;
61 19 my @output;
62  
63 19 ▶ if ($code =~ s/(.*)$Delimiter//o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ s/(.*)$Delimiter//o) 11 8 19
64 11 push @output, map ord, split //, $1;
65 11 ▶ return _croak('non-basic code point') unless $1 =~ /^$BasicRE*$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 =~ /^$BasicRE*$/o 0 11 11
66 }
67  
68 19 ▶ while ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 85 19 104
69 85 my $oldi = $i;
70 85 my $w = 1;
71 LOOP:
72 85 for (my $k = BASE; 1; $k += BASE) {
73 169 my $cp = substr($code, 0, 1, '');
74 169 my $digit = _digit_value($cp);
75 169 ▶ defined $digit or return _croak("invalid punycode input");
Branch: 1 branch
BranchTrueFalseTotal
defined $digit or return _croak("invalid punycode input") 169 0 169
76 169 $i += $digit * $w;
77 169 ▶ my $t
Branch: 2 branches
BranchTrueFalseTotal
$k <= $bias ? : 45 124 169
$k >= $bias + 26 ? : 101 23 124
78 = ($k <= $bias) ? TMIN
79 : ($k >= $bias + TMAX) ? TMAX
80 : $k - $bias;
81 169 ▶ last LOOP if $digit < $t;
Branch: 1 branch
BranchTrueFalseTotal
if $digit < $t 85 84 169
82 84 $w *= (BASE - $t);
83 }
84 85 $bias = _adapt($i - $oldi, @output + 1, $oldi == 0);
85 85 ▶ warn "bias becomes $bias" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::_punycode::DEBUG 0 85 85
86 85 $n += $i / (@output + 1);
87 85 $i = $i % (@output + 1);
88 85 splice(@output, $i, 0, $n);
89 85 ▶ warn join " ", map sprintf('%04x', $_), @output if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::_punycode::DEBUG 0 85 85
90 85 $i++;
91 }
92 19 return join '', map chr, @output;
93 }
94  
95 28 ▶sub encode_punycode {
Subroutine encode_punycode: called CC=13 SCAR=25.7
Pod documented
96 28 my $input = shift;
97 28 my @input = split //, $input;
98  
99 28 my $n = INITIAL_N;
100 28 my $delta = 0;
101 28 my $bias = INITIAL_BIAS;
102  
103 28 my @output;
104 28 my @basic = grep /$BasicRE/, @input;
105 28 my $h = my $b = @basic;
106 28 push @output, @basic;
107 28 ▶ push @output, $Delimiter if $b && $h < @input;
Branch: 1 branch
BranchTrueFalseTotal
if $b and $h < @input 16 12 28
Condition100%3/3
$b and $h < @input
!ll&&!rl&&r
11116
MC/DC100%2/2
$b and $h < @input
$b $h < @input
Truth table100%3/3
A and B
A= $bB= $h < @input
ABresult
0X0
100
111
108 28 ▶ warn "basic codepoints: (@output)" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::_punycode::DEBUG 0 28 28
109  
110 28 ▶ while ($h < @input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h < @input) 87 28 115
111 87 my $m = _min(grep { $_ >= $n } map ord, @input);
112 87 ▶ warn sprintf "next code point to insert is %04x", $m if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::_punycode::DEBUG 0 87 87
113 87 $delta += ($m - $n) * ($h + 1);
114 87 $n = $m;
115 87 for my $i (@input) {
116 1183 my $c = ord($i);
117 1183 ▶ $delta++ if $c < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $c < $n 721 462 1183
118 1183 ▶ if ($c == $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c == $n) 100 1083 1183
119 100 my $q = $delta;
120 LOOP:
121 100 for (my $k = BASE; 1; $k += BASE) {
122 203 ▶ my $t
Branch: 2 branches
BranchTrueFalseTotal
$k <= $bias ? : 61 142 203
$k >= $bias + 26 ? : 116 26 142
123 = ($k <= $bias) ? TMIN
124 : ($k >= $bias + TMAX) ? TMAX
125 : $k - $bias;
126 203 ▶ last LOOP if $q < $t;
Branch: 1 branch
BranchTrueFalseTotal
if $q < $t 100 103 203
127 103 my $cp = _code_point($t + (($q - $t) % (BASE - $t)));
128 103 push @output, chr($cp);
129 103 $q = ($q - $t) / (BASE - $t);
130 }
131 100 push @output, chr(_code_point($q));
132 100 $bias = _adapt($delta, $h + 1, $h == $b);
133 100 ▶ warn "bias becomes $bias" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::_punycode::DEBUG 0 100 100
134 100 $delta = 0;
135 100 $h++;
136 }
137 }
138 87 $delta++;
139 87 $n++;
140 }
141 28 return join '', @output;
142 }
143  
144 87 ▶sub _min {
Subroutine _min: called CC=3 SCAR=11.0
145 87 my $min = shift;
146 87 ▶ for (@_) { $min = $_ if $_ <= $min }
Branch: 1 branch
BranchTrueFalseTotal
if $_ <= $min 74 301 375
147 87 return $min;
148 }
149  
150 4 1;
151 __END__
« URI/urn.pm ↑ Summary URI/data.pm »