| 1 | # based on RFC 3492 | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use List::Util (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | sub pyc_base () { 36 } | ||||||||||||||||||||||||||||||
| 9 | sub pyc_tmin () { 1 } | ||||||||||||||||||||||||||||||
| 10 | sub pyc_tmax () { 26 } | ||||||||||||||||||||||||||||||
| 11 | sub pyc_initial_bias () { 72 } | ||||||||||||||||||||||||||||||
| 12 | sub pyc_initial_n () { 128 } | ||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | sub pyc_digits () { "abcdefghijklmnopqrstuvwxyz0123456789" } | ||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | sub pyc_adapt($$$) { | ||||||||||||||||||||||||||||
|
Subroutine
pyc_adapt: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 17 | 12 | my ($delta, $numpoints, $firsttime) = @_; | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | 12 | ▶ | $delta = $firsttime ? $delta / 700 : $delta >> 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 20 | 12 | $delta += $delta / $numpoints; | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 12 | my $k; | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | 12 | ▶ | while ($delta > (pyc_base - pyc_tmin) * pyc_tmax / 2) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | 6 | $delta /= pyc_base - pyc_tmin; | |||||||||||||||||||||||||||||
| 26 | 6 | $k += pyc_base; | |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | 12 | $k + $delta * (pyc_base - pyc_tmin + 1) / ($delta + 38) | |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||
| 32 | 5 | ▶ | sub punycode_encode($) { | ||||||||||||||||||||||||||||
|
Subroutine
punycode_encode: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 33 | 5 | my ($input) = @_; | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | 5 | my ($n, $bias, $delta) = (pyc_initial_n, pyc_initial_bias); | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | 5 | (my $output = $input) =~ y/\x00-\x7f//cd; | |||||||||||||||||||||||||||||
| 38 | 5 | my $h = my $b = length $output; | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | 5 | my @input = split '', $input; | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | $output .= "-" if $b && $h < @input; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$b and $h < @input
MC/DC50%1/2
$b and $h < @input
$b
$h < @input
Truth table66%2/3
A and B
A= $bB= $h < @input
| |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | 5 | ▶ | while ($h < @input) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 45 | 9 | my $m = List::Util::min grep { $_ >= $n } map ord, @input; | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | $m - $n <= (0x7fffffff - $delta) / ($h + 1) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 48 | or Carp::croak "punycode_encode: overflow in punycode delta encoding"; | ||||||||||||||||||||||||||||||
| 49 | 9 | $delta += ($m - $n) * ($h + 1); | |||||||||||||||||||||||||||||
| 50 | 9 | $n = $m; | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 9 | for my $i (@input) { | |||||||||||||||||||||||||||||
| 53 | 25 | my $c = ord $i; | |||||||||||||||||||||||||||||
| 54 | 25 | ▶ | ++$delta < 0x7fffffff | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
$c < $n && (++$delta < 2147483647 || Carp::croak('punycode_encode: overflow in punycode delta encoding'))
++$delta < 2147483647 or Carp::croak("punycode_encode: overflow in punycode delta encoding")
MC/DC0%0/2
$c < $n && (++$delta < 2147483647 || Carp::croak('punycode_encode: overflow in punycode delta encoding'))
$c < $n
++$delta < 2147483647
Truth table33%1/3
A && B
A= $c < $nB= ++$delta < 2147483647
| |||||||||||||||||||||||||||||||
| 55 | or Carp::croak "punycode_encode: overflow in punycode delta encoding" | ||||||||||||||||||||||||||||||
| 56 | if $c < $n; | ||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | 25 | ▶ | if ($c == $n) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 59 | 9 | my ($q, $k) = ($delta, pyc_base); | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | 9 | while () { | |||||||||||||||||||||||||||||
| 62 | 29 | my $t = List::Util::min pyc_tmax, List::Util::max pyc_tmin, $k - $bias; | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 29 | ▶ | last if $q < $t; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 20 | $output .= substr pyc_digits, $t + (($q - $t) % (pyc_base - $t)), 1; | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | 20 | $q = ($q - $t) / (pyc_base - $t); | |||||||||||||||||||||||||||||
| 69 | 20 | $k += pyc_base; | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 9 | $output .= substr pyc_digits, $q, 1; | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | 9 | $bias = pyc_adapt $delta, $h + 1, $h == $b; | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||
| 76 | 9 | $delta = 0; | |||||||||||||||||||||||||||||
| 77 | 9 | ++$h; | |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 9 | ++$delta; | |||||||||||||||||||||||||||||
| 82 | 9 | ++$n; | |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | $output | ||||||||||||||||||||||||||||||
| 86 | 5 | } | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | sub punycode_decode($) { | ||||||||||||||||||||||||||||
|
Subroutine
punycode_decode: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 89 | 2 | my ($input) = @_; | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||
| 91 | 2 | my ($n, $bias, $i) = (pyc_initial_n, pyc_initial_bias); | |||||||||||||||||||||||||||||
| 92 | 2 | my $output; | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | 2 | ▶ | if ($input =~ /^(.*?)-([^-]*)$/x) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 95 | 1 | $output = $1; | |||||||||||||||||||||||||||||
| 96 | 1 | $input = $2; | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | $output =~ /[^\x00-\x7f]/ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 99 | and Carp::croak "punycode_decode: malformed punycode"; | ||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||
| 102 | 2 | ▶ | while (length $input) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 103 | 3 | my $oldi = $i; | |||||||||||||||||||||||||||||
| 104 | 3 | my $w = 1; | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | 3 | for (my $k = pyc_base; ; $k += pyc_base) { | |||||||||||||||||||||||||||||
| 107 | 9 | ▶ | (my $digit = index pyc_digits, substr $input, 0, 1, "") | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | >= 0 | ||||||||||||||||||||||||||||||
| 109 | or Carp::croak "punycode_decode: malformed punycode"; | ||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||
| 111 | 9 | $i += $digit * $w; | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | 9 | my $t = List::Util::max pyc_tmin, List::Util::min pyc_tmax, $k - $bias; | |||||||||||||||||||||||||||||
| 114 | 9 | ▶ | last if $digit < $t; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | 6 | $w *= pyc_base - $t; | |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||
| 119 | 3 | my $outlen = 1 + length $output; | |||||||||||||||||||||||||||||
| 120 | 3 | $bias = pyc_adapt $i - $oldi, $outlen, $oldi == 0; | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | 3 | $n += $i / $outlen; | |||||||||||||||||||||||||||||
| 123 | 3 | $i %= $outlen; | |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | 3 | substr $output, $i, 0, chr $n; | |||||||||||||||||||||||||||||
| 126 | 3 | ++$i; | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | $output | ||||||||||||||||||||||||||||||
| 130 | 2 | } | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | 1 | 1 | |||||||||||||||||||||||||||||