Corvus/Uts46.pm

filter: Statementstmt 100% 39 / 39 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 98.3% 58 / 59 CC 9 SCAR 14.1
CC
9
Cov
100%
_find
5.0
map_name
5.0
BEGIN
1.0
CRAP 4.1
« Corvus/ResultsCollector.pm ↑ Summary Corvus/Error.pm »
1 package JSON::Schema::Corvus::Uts46;
2  
3 # The mapping step of UTS #46 (Unicode IDNA Compatibility Processing, section 4 step 1), which the idn-hostname and
4 # idn-email formats apply to a name before they check it.
5 #
6 # An "ignored" code point is removed and a "mapped" one is replaced by its mapping. Every other code point is kept:
7 # "valid" and "deviation" ones (nontransitional processing, so ZERO WIDTH JOINER and ZERO WIDTH NON-JOINER stay and
8 # meet the contextual rules of RFC 5892), and "disallowed" ones, which the IDNA2008 checks that follow refuse. ASCII is
9 # kept as it is: its mapping only lowercases the letters, which does not change whether a name is valid. The
10 # normalization step (step 2) is not applied: a name that is not in Normalization Form C is valid as a host name, as
11 # the JSON-Schema-Test-Suite expects. The table (Uts46Data.pm) is the one the .NET engine and the Rust crate use.
12  
13 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use JSON::Schema::Corvus::Uts46Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 my $ENTRIES = $JSON::Schema::Corvus::Uts46Data::ENTRIES;
18 5 my $POOL = $JSON::Schema::Corvus::Uts46Data::POOL;
19 5 my $COUNT = length($ENTRIES) / 8;
20  
21 # The replacement of an ignored or mapped code point outside ASCII ('' when it is ignored), or undef.
22 71 ▶sub _find {
Subroutine _find: called CC=5 SCAR=16.1
23 71 my ($cp) = @_;
24 71 my ($low, $high) = (0, $COUNT);
25 71 ▶ while ($low < $high) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low < $high) 852 1 853
26 852 my $middle = ($low + $high) >> 1;
27 852 my ($s1, $s2, $s3, $count, $offset, $length) = unpack 'C C C n n C', substr($ENTRIES, $middle * 8, 8);
28 852 my $start = ($s1 << 16) | ($s2 << 8) | $s3;
29 852 ▶ if ($cp < $start) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cp < $start) { } 431 421 852
elsif ($cp > $start + $count) { } 351 70 421
30 431 $high = $middle;
31 }
32 elsif ($cp > $start + $count) {
33 351 $low = $middle + 1;
34 }
35 else {
36 70 ▶ return '' if $length == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $length == 0 3 67 70
37 67 my $text = substr($POOL, $offset, $length);
38 67 utf8::decode($text);
39 67 return $text;
40 }
41 }
42 1 return undef;
43 }
44  
45 # The mapped form of a name (a string of characters).
46 10 ▶sub map_name {
Subroutine map_name: called CC=5 SCAR=16.1
Pod undocumented
47 10 my ($name) = @_;
48 10 ▶ return $name if $name !~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /[^\x00-\x7f]/ 3 7 10
49 7 my $out = '';
50 7 for my $c (split //, $name) {
51 94 my $cp = ord $c;
52 94 ▶ if ($cp >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp >= 128) 71 23 94
53 71 my $replacement = _find($cp);
54 71 ▶ if (defined $replacement) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replacement) 70 1 71
55 70 $out .= $replacement;
56 70 next;
57 }
58 }
59 24 $out .= $c;
60 }
61 7 return $out;
62 }
63  
64 5 1;
« Corvus/ResultsCollector.pm ↑ Summary Corvus/Error.pm »