| 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 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 6 | ▶ | use JSON::Schema::Corvus::Uts46Data; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 6 | my $ENTRIES = $JSON::Schema::Corvus::Uts46Data::ENTRIES; | |||||||||||||
| 18 | 6 | my $POOL = $JSON::Schema::Corvus::Uts46Data::POOL; | |||||||||||||
| 19 | 6 | 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 53 | 71 | my $replacement = _find($cp); | |||||||||||||
| 54 | 71 | ▶ | if (defined $replacement) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 70 | $out .= $replacement; | |||||||||||||
| 56 | 70 | next; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | } | ||||||||||||||
| 59 | 24 | $out .= $c; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | 7 | return $out; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 6 | 1; | |||||||||||||