blib/lib/Net/IDN/PP.pm

filter: Statementstmt 97.1% 103 / 106 Branchbran 92.5% 37 / 40 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 7 totaltotal 89.2% 157 / 176 CC 26 SCAR 21.2
CC
26
Cov
94%
encode_label
13.0
decode_label
8.0
decode_digit
5.9
CRAP 8.4
↑ Summary
1 package Net::IDN::PP 0.02;
2 # ABSTRACT: A pure-Perl implementation of the Punycode algorithm for encoding internationalized domain names (IDNs)
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5  
6 sub encode {
7 9 ▶ my $name = pop;
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
8 9 return join('.', map { encode_label($_) } split(/\./, $name));
9 }
10  
11 sub decode {
12 9 ▶ my $name = pop;
Subroutine decode: called CC=1 SCAR=0.0
Pod undocumented
13 9 return join('.', map { decode_label($_) } split(/\./, $name));
14 }
15  
16 sub encode_label {
17 11 ▶ my $label = lc(pop);
Subroutine encode_label: called CC=13 SCAR=25.6
Pod undocumented
18  
19 # if ASCII-only: return as-is
20 11 ▶ return $label if ($label =~ /^[\x00-\x7F]+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $label =~ /^[\x00-\x7F]+$/u 3 8 11
21  
22 # extract code points
23 8 my @cps = unpack("U*", $label);
24  
25 # Initialize @output with only the ASCII code points
26 8 my @output = map { chr($_) } grep { $_ < 0x80 } @cps;
27  
28 8 my $basic_length = scalar(@output);
29 8 my $output_str = join('', @output);
30 8 ▶ $output_str .= '-' if ($basic_length > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $basic_length > 0 7 1 8
31  
32 # Punycode parameters
33 8 my $n = 128;
34 8 my $delta = 0;
35 8 my $bias = 72;
36 8 my $h = $basic_length;
37 8 my $len = scalar(@cps);
38  
39 8 ▶ while ($h < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h < $len) 11 8 19
40 # Find the minimum code point >= n
41 11 my $m = 0x10FFFF;
42  
43 11 for my $cp (@cps) {
44 55 ▶ $m = $cp if $cp >= $n && $cp < $m;
Branch: 1 branch
BranchTrueFalseTotal
if $cp >= $n and $cp < $m 12 43 55
Condition100%3/3
$cp >= $n and $cp < $m
!ll&&!rl&&r
38512
MC/DC100%2/2
$cp >= $n and $cp < $m
$cp >= $n $cp < $m
Truth table100%3/3
A and B
A= $cp >= $nB= $cp < $m
ABresult
0X0
100
111
45 }
46  
47 11 my $inc = ($m - $n) * ($h + 1);
48 11 $delta += $inc;
49 11 $n = $m;
50  
51 11 for my $cp (@cps) {
52 55 ▶ if ($cp < $n) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cp < $n) { } 38 17 55
elsif ($cp == $n) { } 11 6 17
53 38 $delta++;
54  
55 } elsif ($cp == $n) {
56 11 my $q = $delta;
57  
58 11 for (my $k = 36; ; $k += 36) {
59 30 my $t;
60  
61 30 ▶ if ($k <= $bias) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k <= $bias) { } 16 14 30
elsif ($k >= $bias + 26) { } 13 1 14
62 16 $t = 1;
63  
64 } elsif ($k >= $bias + 26) {
65 13 $t = 26;
66  
67 } else {
68 1 $t = $k - $bias;
69  
70 }
71  
72 30 ▶ last if ($q < $t);
Branch: 1 branch
BranchTrueFalseTotal
if $q < $t 11 19 30
73  
74 19 my $code = $t + (($q - $t) % (36 - $t));
75 19 $output_str .= encode_digit($code);
76 19 $q = int(($q - $t) / (36 - $t));
77 }
78  
79 11 $output_str .= encode_digit($q);
80 11 $bias = adapt($delta, $h + 1, $h == $basic_length);
81 11 $delta = 0;
82 11 $h++;
83 }
84 }
85  
86 11 $delta++;
87 11 $n++;
88 }
89  
90 8 return q{xn--}.$output_str;
91 }
92  
93 sub encode_digit {
94 30 ▶ my $d = shift;
Subroutine encode_digit: called CC=1 SCAR=0.0
Pod undocumented
95 30 return chr($d + 22 + 75 * ($d < 26)); # 0..25 = a..z, 26..35 = 0..9
96 }
97  
98 sub decode_digit {
99 33 ▶ my $ch = lc(shift);
Subroutine decode_digit: called CC=5 SCAR=17.8
Pod undocumented
100 33 my $cp = ord($ch);
101  
102 # 'a'..'z' => 0..25
103 33 ▶ return $cp - 97 if ($cp >= 97 && $cp <= 122);
Branch: 1 branch
BranchTrueFalseTotal
if $cp >= 97 and $cp <= 122 29 4 33
Condition66%2/3
$cp >= 97 and $cp <= 122
!ll&&!rl&&r
4029
MC/DC50%1/2
$cp >= 97 and $cp <= 122
$cp >= 97 $cp <= 122
Truth table66%2/3
A and B
A= $cp >= 97B= $cp <= 122
ABresult
0X0
100
111
104  
105 # '0'..'9' => 26..35
106  
107 4 ▶ return $cp - 22 if ($cp >= 48 && $cp <= 57);
Branch: 1 branch
BranchTrueFalseTotal
if $cp >= 48 and $cp <= 57 4 0 4
Condition33%1/3
$cp >= 48 and $cp <= 57
!ll&&!rl&&r
004
MC/DC0%0/2
$cp >= 48 and $cp <= 57
$cp >= 48 $cp <= 57
Truth table33%1/3
A and B
A= $cp >= 48B= $cp <= 57
ABresult
0X0
100
111
108  
109 0 return 36; # invalid
110 }
111  
112 sub adapt {
113 23 ▶ my ($delta, $numpoints, $first_time) = @_;
Subroutine adapt: called CC=3 SCAR=11.4
Pod undocumented
114 23 ▶ $delta = int($first_time ? $delta / 700 : $delta / 2);
Branch: 1 branch
BranchTrueFalseTotal
$first_time ? : 17 6 23
115 23 $delta += int($delta / $numpoints);
116  
117 23 my $k = 0;
118  
119 23 ▶ while ($delta > 455) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 455) 0 23 23
120 0 $delta = int($delta / 35);
121 0 $k += 36;
122 }
123  
124 23 return $k + int((36 * $delta) / ($delta + 38));
125 }
126  
127 sub decode_label {
128 11 ▶ my $label = shift;
Subroutine decode_label: called CC=8 SCAR=20.8
Pod undocumented
129  
130 11 ▶ return $label unless ($label =~ /^xn--/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $label =~ /^xn--/iu 2 9 11
131  
132 9 my $input = lc(substr($label, 4));
133  
134 9 my @output;
135 9 my $dash_pos = rindex($input, '-');
136 9 ▶ if ($dash_pos != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dash_pos != -1) 8 1 9
137 8 my $basic = substr($input, 0, $dash_pos);
138 8 @output = map { ord($_) } split(//, $basic);
139 8 $input = substr($input, $dash_pos + 1);
140 }
141  
142 9 my $n = 128;
143 9 my $i = 0;
144 9 my $bias = 72;
145  
146 9 my $in_idx = 0;
147 9 my $in_len = length($input);
148  
149 9 ▶ while ($in_idx < $in_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_idx < $in_len) 12 9 21
150 12 my $oldi = $i;
151 12 my $w = 1;
152  
153 12 for (my $k = 36; ; $k += 36) {
154 33 ▶ last if ($in_idx >= $in_len);
Branch: 1 branch
BranchTrueFalseTotal
if $in_idx >= $in_len 0 33 33
155  
156 33 my $c = substr($input, $in_idx++, 1);
157 33 my $d = decode_digit($c);
158  
159 33 $i += $d * $w;
160  
161 33 my $t;
162 33 ▶ if ($k <= $bias) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k <= $bias) { } 18 15 33
elsif ($k >= $bias + 26) { } 14 1 15
163 18 $t = 1;
164  
165 } elsif ($k >= $bias + 26) {
166 14 $t = 26;
167  
168 } else {
169 1 $t = $k - $bias;
170  
171 }
172  
173 33 ▶ last if ($d < $t);
Branch: 1 branch
BranchTrueFalseTotal
if $d < $t 12 21 33
174 21 $w *= (36 - $t);
175 }
176  
177 12 my $out_len_plus_1 = scalar(@output) + 1;
178  
179 12 $bias = adapt($i - $oldi, $out_len_plus_1, $oldi == 0);
180  
181 12 my $q = int($i / $out_len_plus_1);
182  
183 12 $n += $q;
184  
185 12 $i = $i % $out_len_plus_1;
186  
187 12 splice(@output, $i, 0, $n);
188  
189 12 $i++;
190 }
191  
192 9 return join('', map { chr($_) } @output);
193 }
194  
195 1 1;
196  
197 __END__
↑ Summary