Filename | /usr/share/perl5/URI/_punycode.pm |
Statements | Executed 26 statements in 1.55ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 154µs | 157µs | BEGIN@10 | URI::_punycode::
1 | 1 | 1 | 12µs | 25µs | BEGIN@3 | URI::_punycode::
1 | 1 | 1 | 9µs | 45µs | BEGIN@20 | URI::_punycode::
1 | 1 | 1 | 9µs | 46µs | BEGIN@14 | URI::_punycode::
1 | 1 | 1 | 8µs | 37µs | BEGIN@15 | URI::_punycode::
1 | 1 | 1 | 8µs | 35µs | BEGIN@16 | URI::_punycode::
1 | 1 | 1 | 8µs | 34µs | BEGIN@17 | URI::_punycode::
1 | 1 | 1 | 8µs | 34µs | BEGIN@18 | URI::_punycode::
1 | 1 | 1 | 7µs | 33µs | BEGIN@19 | URI::_punycode::
1 | 1 | 1 | 2µs | 2µs | CORE:qr (opcode) | URI::_punycode::
0 | 0 | 0 | 0s | 0s | _croak | URI::_punycode::
0 | 0 | 0 | 0s | 0s | adapt | URI::_punycode::
0 | 0 | 0 | 0s | 0s | code_point | URI::_punycode::
0 | 0 | 0 | 0s | 0s | decode_punycode | URI::_punycode::
0 | 0 | 0 | 0s | 0s | digit_value | URI::_punycode::
0 | 0 | 0 | 0s | 0s | encode_punycode | URI::_punycode::
0 | 0 | 0 | 0s | 0s | min | URI::_punycode::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package URI::_punycode; | ||||
2 | |||||
3 | 2 | 66µs | 2 | 39µs | # spent 25µs (12+14) within URI::_punycode::BEGIN@3 which was called:
# once (12µs+14µs) by URI::_idna::BEGIN@7 at line 3 # spent 25µs making 1 call to URI::_punycode::BEGIN@3
# spent 14µs making 1 call to strict::import |
4 | 1 | 700ns | our $VERSION = "0.04"; | ||
5 | |||||
6 | 1 | 500ns | require Exporter; | ||
7 | 1 | 6µs | our @ISA = qw(Exporter); | ||
8 | 1 | 700ns | our @EXPORT = qw(encode_punycode decode_punycode); | ||
9 | |||||
10 | 2 | 186µs | 2 | 159µs | # spent 157µs (154+3) within URI::_punycode::BEGIN@10 which was called:
# once (154µs+3µs) by URI::_idna::BEGIN@7 at line 10 # spent 157µs making 1 call to URI::_punycode::BEGIN@10
# spent 3µs making 1 call to integer::import |
11 | |||||
12 | 1 | 200ns | our $DEBUG = 0; | ||
13 | |||||
14 | 2 | 33µs | 2 | 82µs | # spent 46µs (9+37) within URI::_punycode::BEGIN@14 which was called:
# once (9µs+37µs) by URI::_idna::BEGIN@7 at line 14 # spent 46µs making 1 call to URI::_punycode::BEGIN@14
# spent 37µs making 1 call to constant::import |
15 | 2 | 31µs | 2 | 66µs | # spent 37µs (8+29) within URI::_punycode::BEGIN@15 which was called:
# once (8µs+29µs) by URI::_idna::BEGIN@7 at line 15 # spent 37µs making 1 call to URI::_punycode::BEGIN@15
# spent 29µs making 1 call to constant::import |
16 | 2 | 30µs | 2 | 62µs | # spent 35µs (8+27) within URI::_punycode::BEGIN@16 which was called:
# once (8µs+27µs) by URI::_idna::BEGIN@7 at line 16 # spent 35µs making 1 call to URI::_punycode::BEGIN@16
# spent 27µs making 1 call to constant::import |
17 | 2 | 31µs | 2 | 60µs | # spent 34µs (8+26) within URI::_punycode::BEGIN@17 which was called:
# once (8µs+26µs) by URI::_idna::BEGIN@7 at line 17 # spent 34µs making 1 call to URI::_punycode::BEGIN@17
# spent 26µs making 1 call to constant::import |
18 | 2 | 30µs | 2 | 60µs | # spent 34µs (8+26) within URI::_punycode::BEGIN@18 which was called:
# once (8µs+26µs) by URI::_idna::BEGIN@7 at line 18 # spent 34µs making 1 call to URI::_punycode::BEGIN@18
# spent 26µs making 1 call to constant::import |
19 | 2 | 30µs | 2 | 59µs | # spent 33µs (7+26) within URI::_punycode::BEGIN@19 which was called:
# once (7µs+26µs) by URI::_idna::BEGIN@7 at line 19 # spent 33µs making 1 call to URI::_punycode::BEGIN@19
# spent 26µs making 1 call to constant::import |
20 | 2 | 1.09ms | 2 | 80µs | # spent 45µs (9+36) within URI::_punycode::BEGIN@20 which was called:
# once (9µs+36µs) by URI::_idna::BEGIN@7 at line 20 # spent 45µs making 1 call to URI::_punycode::BEGIN@20
# spent 36µs making 1 call to constant::import |
21 | |||||
22 | 1 | 300ns | my $Delimiter = chr 0x2D; | ||
23 | 1 | 9µs | 1 | 2µs | my $BasicRE = qr/[\x00-\x7f]/; # spent 2µs making 1 call to URI::_punycode::CORE:qr |
24 | |||||
25 | sub _croak { require Carp; Carp::croak(@_); } | ||||
26 | |||||
27 | sub digit_value { | ||||
28 | my $code = shift; | ||||
29 | return ord($code) - ord("A") if $code =~ /[A-Z]/; | ||||
30 | return ord($code) - ord("a") if $code =~ /[a-z]/; | ||||
31 | return ord($code) - ord("0") + 26 if $code =~ /[0-9]/; | ||||
32 | return; | ||||
33 | } | ||||
34 | |||||
35 | sub code_point { | ||||
36 | my $digit = shift; | ||||
37 | return $digit + ord('a') if 0 <= $digit && $digit <= 25; | ||||
38 | return $digit + ord('0') - 26 if 26 <= $digit && $digit <= 36; | ||||
39 | die 'NOT COME HERE'; | ||||
40 | } | ||||
41 | |||||
42 | sub adapt { | ||||
43 | my($delta, $numpoints, $firsttime) = @_; | ||||
44 | $delta = $firsttime ? $delta / DAMP : $delta / 2; | ||||
45 | $delta += $delta / $numpoints; | ||||
46 | my $k = 0; | ||||
47 | while ($delta > ((BASE - TMIN) * TMAX) / 2) { | ||||
48 | $delta /= BASE - TMIN; | ||||
49 | $k += BASE; | ||||
50 | } | ||||
51 | return $k + (((BASE - TMIN + 1) * $delta) / ($delta + SKEW)); | ||||
52 | } | ||||
53 | |||||
54 | sub decode_punycode { | ||||
55 | my $code = shift; | ||||
56 | |||||
57 | my $n = INITIAL_N; | ||||
58 | my $i = 0; | ||||
59 | my $bias = INITIAL_BIAS; | ||||
60 | my @output; | ||||
61 | |||||
62 | if ($code =~ s/(.*)$Delimiter//o) { | ||||
63 | push @output, map ord, split //, $1; | ||||
64 | return _croak('non-basic code point') unless $1 =~ /^$BasicRE*$/o; | ||||
65 | } | ||||
66 | |||||
67 | while ($code) { | ||||
68 | my $oldi = $i; | ||||
69 | my $w = 1; | ||||
70 | LOOP: | ||||
71 | for (my $k = BASE; 1; $k += BASE) { | ||||
72 | my $cp = substr($code, 0, 1, ''); | ||||
73 | my $digit = digit_value($cp); | ||||
74 | defined $digit or return _croak("invalid punycode input"); | ||||
75 | $i += $digit * $w; | ||||
76 | my $t = ($k <= $bias) ? TMIN | ||||
77 | : ($k >= $bias + TMAX) ? TMAX : $k - $bias; | ||||
78 | last LOOP if $digit < $t; | ||||
79 | $w *= (BASE - $t); | ||||
80 | } | ||||
81 | $bias = adapt($i - $oldi, @output + 1, $oldi == 0); | ||||
82 | warn "bias becomes $bias" if $DEBUG; | ||||
83 | $n += $i / (@output + 1); | ||||
84 | $i = $i % (@output + 1); | ||||
85 | splice(@output, $i, 0, $n); | ||||
86 | warn join " ", map sprintf('%04x', $_), @output if $DEBUG; | ||||
87 | $i++; | ||||
88 | } | ||||
89 | return join '', map chr, @output; | ||||
90 | } | ||||
91 | |||||
92 | sub encode_punycode { | ||||
93 | my $input = shift; | ||||
94 | my @input = split //, $input; | ||||
95 | |||||
96 | my $n = INITIAL_N; | ||||
97 | my $delta = 0; | ||||
98 | my $bias = INITIAL_BIAS; | ||||
99 | |||||
100 | my @output; | ||||
101 | my @basic = grep /$BasicRE/, @input; | ||||
102 | my $h = my $b = @basic; | ||||
103 | push @output, @basic; | ||||
104 | push @output, $Delimiter if $b && $h < @input; | ||||
105 | warn "basic codepoints: (@output)" if $DEBUG; | ||||
106 | |||||
107 | while ($h < @input) { | ||||
108 | my $m = min(grep { $_ >= $n } map ord, @input); | ||||
109 | warn sprintf "next code point to insert is %04x", $m if $DEBUG; | ||||
110 | $delta += ($m - $n) * ($h + 1); | ||||
111 | $n = $m; | ||||
112 | for my $i (@input) { | ||||
113 | my $c = ord($i); | ||||
114 | $delta++ if $c < $n; | ||||
115 | if ($c == $n) { | ||||
116 | my $q = $delta; | ||||
117 | LOOP: | ||||
118 | for (my $k = BASE; 1; $k += BASE) { | ||||
119 | my $t = ($k <= $bias) ? TMIN : | ||||
120 | ($k >= $bias + TMAX) ? TMAX : $k - $bias; | ||||
121 | last LOOP if $q < $t; | ||||
122 | my $cp = code_point($t + (($q - $t) % (BASE - $t))); | ||||
123 | push @output, chr($cp); | ||||
124 | $q = ($q - $t) / (BASE - $t); | ||||
125 | } | ||||
126 | push @output, chr(code_point($q)); | ||||
127 | $bias = adapt($delta, $h + 1, $h == $b); | ||||
128 | warn "bias becomes $bias" if $DEBUG; | ||||
129 | $delta = 0; | ||||
130 | $h++; | ||||
131 | } | ||||
132 | } | ||||
133 | $delta++; | ||||
134 | $n++; | ||||
135 | } | ||||
136 | return join '', @output; | ||||
137 | } | ||||
138 | |||||
139 | sub min { | ||||
140 | my $min = shift; | ||||
141 | for (@_) { $min = $_ if $_ <= $min } | ||||
142 | return $min; | ||||
143 | } | ||||
144 | |||||
145 | 1 | 6µs | 1; | ||
146 | __END__ | ||||
# spent 2µs within URI::_punycode::CORE:qr which was called:
# once (2µs+0s) by URI::_idna::BEGIN@7 at line 23 |