Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/5.18.2/darwin-2level/Encode.pm |
Statements | Executed 96 statements in 2.65ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 1.41ms | 2.09ms | BEGIN@48 | Encode::
1 | 1 | 1 | 786µs | 900µs | BEGIN@13 | Encode::
1 | 1 | 1 | 445µs | 482µs | predefine_encodings | Encode::
1 | 1 | 1 | 186µs | 188µs | BEGIN@325 | Encode::utf8::
11 | 1 | 1 | 167µs | 180µs | decode_xs (xsub) | Encode::utf8::
2 | 1 | 1 | 23µs | 97µs | getEncoding (recurses: max depth 1, inclusive time 8µs) | Encode::
4 | 1 | 1 | 17µs | 17µs | define_encoding | Encode::
2 | 2 | 2 | 14µs | 104µs | find_encoding (recurses: max depth 1, inclusive time 15µs) | Encode::
1 | 1 | 1 | 12µs | 28µs | BEGIN@5 | Encode::
1 | 1 | 1 | 9µs | 19µs | BEGIN@242 | Encode::
2 | 2 | 2 | 8µs | 8µs | PERLQQ (xsub) | Encode::
1 | 1 | 1 | 8µs | 15µs | BEGIN@6 | Encode::
1 | 1 | 1 | 8µs | 47µs | BEGIN@8 | Encode::
1 | 1 | 1 | 6µs | 6µs | BEGIN@9 | Encode::
1 | 1 | 1 | 4µs | 4µs | CORE:match (opcode) | Encode::
2 | 1 | 1 | 2µs | 2µs | CORE:subst (opcode) | Encode::
1 | 1 | 1 | 700ns | 700ns | STOP_AT_PARTIAL (xsub) | Encode::
1 | 1 | 1 | 700ns | 700ns | WARN_ON_ERR (xsub) | Encode::
0 | 0 | 0 | 0s | 0s | __ANON__[:285] | Encode::Internal::
0 | 0 | 0 | 0s | 0s | __ANON__[:260] | Encode::UTF_EBCDIC::
0 | 0 | 0 | 0s | 0s | __ANON__[:272] | Encode::UTF_EBCDIC::
0 | 0 | 0 | 0s | 0s | clone_encoding | Encode::
0 | 0 | 0 | 0s | 0s | decode | Encode::
0 | 0 | 0 | 0s | 0s | decode_utf8 | Encode::
0 | 0 | 0 | 0s | 0s | encode | Encode::
0 | 0 | 0 | 0s | 0s | encode_utf8 | Encode::
0 | 0 | 0 | 0s | 0s | encodings | Encode::
0 | 0 | 0 | 0s | 0s | from_to | Encode::
0 | 0 | 0 | 0s | 0s | perlio_ok | Encode::
0 | 0 | 0 | 0s | 0s | resolve_alias | Encode::
0 | 0 | 0 | 0s | 0s | __ANON__[:313] | Encode::utf8::
0 | 0 | 0 | 0s | 0s | __ANON__[:319] | Encode::utf8::
0 | 0 | 0 | 0s | 0s | __ANON__[:335] | Encode::utf8::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # | ||||
2 | # $Id: Encode.pm,v 2.49 2013/03/05 03:13:47 dankogai Exp dankogai $ | ||||
3 | # | ||||
4 | package Encode; | ||||
5 | 2 | 20µs | 2 | 43µs | # spent 28µs (12+16) within Encode::BEGIN@5 which was called:
# once (12µs+16µs) by Pod::Text::BEGIN@32 at line 5 # spent 28µs making 1 call to Encode::BEGIN@5
# spent 16µs making 1 call to strict::import |
6 | 2 | 45µs | 2 | 21µs | # spent 15µs (8+7) within Encode::BEGIN@6 which was called:
# once (8µs+7µs) by Pod::Text::BEGIN@32 at line 6 # spent 15µs making 1 call to Encode::BEGIN@6
# spent 7µs making 1 call to warnings::import |
7 | 1 | 11µs | 1 | 4µs | our $VERSION = sprintf "%d.%02d", q$Revision: 2.49 $ =~ /(\d+)/g; # spent 4µs making 1 call to Encode::CORE:match |
8 | 2 | 24µs | 2 | 86µs | # spent 47µs (8+39) within Encode::BEGIN@8 which was called:
# once (8µs+39µs) by Pod::Text::BEGIN@32 at line 8 # spent 47µs making 1 call to Encode::BEGIN@8
# spent 39µs making 1 call to constant::import |
9 | 2 | 30µs | 1 | 6µs | # spent 6µs within Encode::BEGIN@9 which was called:
# once (6µs+0s) by Pod::Text::BEGIN@32 at line 9 # spent 6µs making 1 call to Encode::BEGIN@9 |
10 | 1 | 323µs | 1 | 355µs | XSLoader::load( __PACKAGE__, $VERSION ); # spent 355µs making 1 call to XSLoader::load |
11 | |||||
12 | 1 | 300ns | require Exporter; | ||
13 | 2 | 174µs | 2 | 962µs | # spent 900µs (786+113) within Encode::BEGIN@13 which was called:
# once (786µs+113µs) by Pod::Text::BEGIN@32 at line 13 # spent 900µs making 1 call to Encode::BEGIN@13
# spent 63µs making 1 call to base::import |
14 | |||||
15 | # Public, encouraged API is exported by default | ||||
16 | |||||
17 | 1 | 2µs | our @EXPORT = qw( | ||
18 | decode decode_utf8 encode encode_utf8 str2bytes bytes2str | ||||
19 | encodings find_encoding clone_encoding | ||||
20 | ); | ||||
21 | 1 | 2µs | our @FB_FLAGS = qw( | ||
22 | DIE_ON_ERR WARN_ON_ERR RETURN_ON_ERR LEAVE_SRC | ||||
23 | PERLQQ HTMLCREF XMLCREF STOP_AT_PARTIAL | ||||
24 | ); | ||||
25 | 1 | 1µs | our @FB_CONSTS = qw( | ||
26 | FB_DEFAULT FB_CROAK FB_QUIET FB_WARN | ||||
27 | FB_PERLQQ FB_HTMLCREF FB_XMLCREF | ||||
28 | ); | ||||
29 | 1 | 4µs | our @EXPORT_OK = ( | ||
30 | qw( | ||||
31 | _utf8_off _utf8_on define_encoding from_to is_16bit is_8bit | ||||
32 | is_utf8 perlio_ok resolve_alias utf8_downgrade utf8_upgrade | ||||
33 | ), | ||||
34 | @FB_FLAGS, @FB_CONSTS, | ||||
35 | ); | ||||
36 | |||||
37 | 1 | 12µs | our %EXPORT_TAGS = ( | ||
38 | all => [ @EXPORT, @EXPORT_OK ], | ||||
39 | default => [ @EXPORT ], | ||||
40 | fallbacks => [ @FB_CONSTS ], | ||||
41 | fallback_all => [ @FB_CONSTS, @FB_FLAGS ], | ||||
42 | ); | ||||
43 | |||||
44 | # Documentation moved after __END__ for speed - NI-S | ||||
45 | |||||
46 | 1 | 300ns | our $ON_EBCDIC = ( ord("A") == 193 ); | ||
47 | |||||
48 | 2 | 994µs | 2 | 2.12ms | # spent 2.09ms (1.41+676µs) within Encode::BEGIN@48 which was called:
# once (1.41ms+676µs) by Pod::Text::BEGIN@32 at line 48 # spent 2.09ms making 1 call to Encode::BEGIN@48
# spent 28µs making 1 call to Exporter::import |
49 | |||||
50 | # Make a %Encoding package variable to allow a certain amount of cheating | ||||
51 | 1 | 100ns | our %Encoding; | ||
52 | 1 | 0s | our %ExtModule; | ||
53 | 1 | 95µs | require Encode::Config; | ||
54 | # See | ||||
55 | # https://bugzilla.redhat.com/show_bug.cgi?id=435505#c2 | ||||
56 | # to find why sig handers inside eval{} are disabled. | ||||
57 | 1 | 200ns | eval { | ||
58 | 1 | 2µs | local $SIG{__DIE__}; | ||
59 | 1 | 500ns | local $SIG{__WARN__}; | ||
60 | 1 | 36µs | require Encode::ConfigLocal; | ||
61 | }; | ||||
62 | |||||
63 | sub encodings { | ||||
64 | my %enc; | ||||
65 | my $arg = $_[1] || ''; | ||||
66 | if ( $arg eq ":all" ) { | ||||
67 | %enc = ( %Encoding, %ExtModule ); | ||||
68 | } | ||||
69 | else { | ||||
70 | %enc = %Encoding; | ||||
71 | for my $mod ( map { m/::/ ? $_ : "Encode::$_" } @_ ) { | ||||
72 | DEBUG and warn $mod; | ||||
73 | for my $enc ( keys %ExtModule ) { | ||||
74 | $ExtModule{$enc} eq $mod and $enc{$enc} = $mod; | ||||
75 | } | ||||
76 | } | ||||
77 | } | ||||
78 | return sort { lc $a cmp lc $b } | ||||
79 | grep { !/^(?:Internal|Unicode|Guess)$/o } keys %enc; | ||||
80 | } | ||||
81 | |||||
82 | sub perlio_ok { | ||||
83 | my $obj = ref( $_[0] ) ? $_[0] : find_encoding( $_[0] ); | ||||
84 | $obj->can("perlio_ok") and return $obj->perlio_ok(); | ||||
85 | return 0; # safety net | ||||
86 | } | ||||
87 | |||||
88 | # spent 17µs within Encode::define_encoding which was called 4 times, avg 4µs/call:
# 4 times (17µs+0s) by XSLoader::load at line 95 of XSLoader.pm, avg 4µs/call | ||||
89 | 4 | 1µs | my $obj = shift; | ||
90 | 4 | 600ns | my $name = shift; | ||
91 | 4 | 2µs | $Encoding{$name} = $obj; | ||
92 | 4 | 4µs | my $lc = lc($name); | ||
93 | 4 | 600ns | define_alias( $lc => $obj ) unless $lc eq $name; | ||
94 | 4 | 1µs | while (@_) { | ||
95 | my $alias = shift; | ||||
96 | define_alias( $alias, $obj ); | ||||
97 | } | ||||
98 | 4 | 10µs | return $obj; | ||
99 | } | ||||
100 | |||||
101 | # spent 97µs (23+74) within Encode::getEncoding which was called 2 times, avg 48µs/call:
# 2 times (23µs+74µs) by Encode::find_encoding at line 129, avg 48µs/call | ||||
102 | 2 | 1µs | my ( $class, $name, $skip_external ) = @_; | ||
103 | |||||
104 | 2 | 9µs | 2 | 2µs | $name =~ s/\s+//g; # https://rt.cpan.org/Ticket/Display.html?id=65796 # spent 2µs making 2 calls to Encode::CORE:subst, avg 800ns/call |
105 | |||||
106 | 2 | 400ns | ref($name) && $name->can('renew') and return $name; | ||
107 | 2 | 6µs | exists $Encoding{$name} and return $Encoding{$name}; | ||
108 | 1 | 1µs | my $lc = lc $name; | ||
109 | 1 | 400ns | exists $Encoding{$lc} and return $Encoding{$lc}; | ||
110 | |||||
111 | 1 | 3µs | 1 | 80µs | my $oc = $class->find_alias($name); # spent 80µs making 1 call to Encode::Alias::find_alias |
112 | 1 | 2µs | defined($oc) and return $oc; | ||
113 | $lc ne $name and $oc = $class->find_alias($lc); | ||||
114 | defined($oc) and return $oc; | ||||
115 | |||||
116 | unless ($skip_external) { | ||||
117 | if ( my $mod = $ExtModule{$name} || $ExtModule{$lc} ) { | ||||
118 | $mod =~ s,::,/,g; | ||||
119 | $mod .= '.pm'; | ||||
120 | eval { require $mod; }; | ||||
121 | exists $Encoding{$name} and return $Encoding{$name}; | ||||
122 | } | ||||
123 | } | ||||
124 | return; | ||||
125 | } | ||||
126 | |||||
127 | # spent 104µs (14+89) within Encode::find_encoding which was called 2 times, avg 52µs/call:
# once (7µs+97µs) by Path::Tiny::CORE:open at line 816 of Path/Tiny.pm
# once (8µs+-8µs) by Encode::Alias::find_alias at line 46 of Encode/Alias.pm | ||||
128 | 2 | 1µs | my ( $name, $skip_external ) = @_; | ||
129 | 2 | 11µs | 2 | 97µs | return __PACKAGE__->getEncoding( $name, $skip_external ); # spent 104µs making 2 calls to Encode::getEncoding, avg 52µs/call, recursion: max depth 1, sum of overlapping time 8µs |
130 | } | ||||
131 | |||||
132 | sub resolve_alias($) { | ||||
133 | my $obj = find_encoding(shift); | ||||
134 | defined $obj and return $obj->name; | ||||
135 | return; | ||||
136 | } | ||||
137 | |||||
138 | sub clone_encoding($) { | ||||
139 | my $obj = find_encoding(shift); | ||||
140 | ref $obj or return; | ||||
141 | eval { require Storable }; | ||||
142 | $@ and return; | ||||
143 | return Storable::dclone($obj); | ||||
144 | } | ||||
145 | |||||
146 | sub encode($$;$) { | ||||
147 | my ( $name, $string, $check ) = @_; | ||||
148 | return undef unless defined $string; | ||||
149 | $string .= ''; # stringify; | ||||
150 | $check ||= 0; | ||||
151 | unless ( defined $name ) { | ||||
152 | require Carp; | ||||
153 | Carp::croak("Encoding name should not be undef"); | ||||
154 | } | ||||
155 | my $enc = find_encoding($name); | ||||
156 | unless ( defined $enc ) { | ||||
157 | require Carp; | ||||
158 | Carp::croak("Unknown encoding '$name'"); | ||||
159 | } | ||||
160 | my $octets = $enc->encode( $string, $check ); | ||||
161 | $_[1] = $string if $check and !ref $check and !( $check & LEAVE_SRC() ); | ||||
162 | return $octets; | ||||
163 | } | ||||
164 | 1 | 900ns | *str2bytes = \&encode; | ||
165 | |||||
166 | sub decode($$;$) { | ||||
167 | my ( $name, $octets, $check ) = @_; | ||||
168 | return undef unless defined $octets; | ||||
169 | $octets .= ''; | ||||
170 | $check ||= 0; | ||||
171 | my $enc = find_encoding($name); | ||||
172 | unless ( defined $enc ) { | ||||
173 | require Carp; | ||||
174 | Carp::croak("Unknown encoding '$name'"); | ||||
175 | } | ||||
176 | my $string = $enc->decode( $octets, $check ); | ||||
177 | $_[1] = $octets if $check and !ref $check and !( $check & LEAVE_SRC() ); | ||||
178 | return $string; | ||||
179 | } | ||||
180 | 1 | 200ns | *bytes2str = \&decode; | ||
181 | |||||
182 | sub from_to($$$;$) { | ||||
183 | my ( $string, $from, $to, $check ) = @_; | ||||
184 | return undef unless defined $string; | ||||
185 | $check ||= 0; | ||||
186 | my $f = find_encoding($from); | ||||
187 | unless ( defined $f ) { | ||||
188 | require Carp; | ||||
189 | Carp::croak("Unknown encoding '$from'"); | ||||
190 | } | ||||
191 | my $t = find_encoding($to); | ||||
192 | unless ( defined $t ) { | ||||
193 | require Carp; | ||||
194 | Carp::croak("Unknown encoding '$to'"); | ||||
195 | } | ||||
196 | my $uni = $f->decode($string); | ||||
197 | $_[0] = $string = $t->encode( $uni, $check ); | ||||
198 | return undef if ( $check && length($uni) ); | ||||
199 | return defined( $_[0] ) ? length($string) : undef; | ||||
200 | } | ||||
201 | |||||
202 | sub encode_utf8($) { | ||||
203 | my ($str) = @_; | ||||
204 | utf8::encode($str); | ||||
205 | return $str; | ||||
206 | } | ||||
207 | |||||
208 | 1 | 100ns | my $utf8enc; | ||
209 | |||||
210 | sub decode_utf8($;$) { | ||||
211 | my ( $octets, $check ) = @_; | ||||
212 | return $octets if is_utf8($octets); | ||||
213 | return undef unless defined $octets; | ||||
214 | $octets .= '' if ref $octets; | ||||
215 | $check ||= 0; | ||||
216 | $utf8enc ||= find_encoding('utf8'); | ||||
217 | my $string = $utf8enc->decode( $octets, $check ); | ||||
218 | $_[0] = $octets if $check and !ref $check and !( $check & LEAVE_SRC() ); | ||||
219 | return $string; | ||||
220 | } | ||||
221 | |||||
222 | # sub decode_utf8($;$) { | ||||
223 | # my ( $str, $check ) = @_; | ||||
224 | # return $str if is_utf8($str); | ||||
225 | # if ($check) { | ||||
226 | # return decode( "utf8", $str, $check ); | ||||
227 | # } | ||||
228 | # else { | ||||
229 | # return decode( "utf8", $str ); | ||||
230 | # return $str; | ||||
231 | # } | ||||
232 | # } | ||||
233 | |||||
234 | 1 | 2µs | 1 | 482µs | predefine_encodings(1); # spent 482µs making 1 call to Encode::predefine_encodings |
235 | |||||
236 | # | ||||
237 | # This is to restore %Encoding if really needed; | ||||
238 | # | ||||
239 | |||||
240 | # spent 482µs (445+37) within Encode::predefine_encodings which was called:
# once (445µs+37µs) by Pod::Text::BEGIN@32 at line 234 | ||||
241 | 1 | 56µs | require Encode::Encoding; | ||
242 | 2 | 432µs | 2 | 28µs | # spent 19µs (9+10) within Encode::BEGIN@242 which was called:
# once (9µs+10µs) by Pod::Text::BEGIN@32 at line 242 # spent 19µs making 1 call to Encode::BEGIN@242
# spent 10µs making 1 call to warnings::unimport |
243 | 1 | 300ns | my $use_xs = shift; | ||
244 | 1 | 400ns | if ($ON_EBCDIC) { | ||
245 | |||||
246 | # was in Encode::UTF_EBCDIC | ||||
247 | package Encode::UTF_EBCDIC; | ||||
248 | push @Encode::UTF_EBCDIC::ISA, 'Encode::Encoding'; | ||||
249 | *decode = sub { | ||||
250 | my ( undef, $str, $chk ) = @_; | ||||
251 | my $res = ''; | ||||
252 | for ( my $i = 0 ; $i < length($str) ; $i++ ) { | ||||
253 | $res .= | ||||
254 | chr( | ||||
255 | utf8::unicode_to_native( ord( substr( $str, $i, 1 ) ) ) | ||||
256 | ); | ||||
257 | } | ||||
258 | $_[1] = '' if $chk; | ||||
259 | return $res; | ||||
260 | }; | ||||
261 | *encode = sub { | ||||
262 | my ( undef, $str, $chk ) = @_; | ||||
263 | my $res = ''; | ||||
264 | for ( my $i = 0 ; $i < length($str) ; $i++ ) { | ||||
265 | $res .= | ||||
266 | chr( | ||||
267 | utf8::native_to_unicode( ord( substr( $str, $i, 1 ) ) ) | ||||
268 | ); | ||||
269 | } | ||||
270 | $_[1] = '' if $chk; | ||||
271 | return $res; | ||||
272 | }; | ||||
273 | $Encode::Encoding{Unicode} = | ||||
274 | bless { Name => "UTF_EBCDIC" } => "Encode::UTF_EBCDIC"; | ||||
275 | } | ||||
276 | else { | ||||
277 | |||||
278 | package Encode::Internal; | ||||
279 | 1 | 5µs | push @Encode::Internal::ISA, 'Encode::Encoding'; | ||
280 | *decode = sub { | ||||
281 | my ( undef, $str, $chk ) = @_; | ||||
282 | utf8::upgrade($str); | ||||
283 | $_[1] = '' if $chk; | ||||
284 | return $str; | ||||
285 | 1 | 2µs | }; | ||
286 | 1 | 300ns | *encode = \&decode; | ||
287 | 1 | 2µs | $Encode::Encoding{Unicode} = | ||
288 | bless { Name => "Internal" } => "Encode::Internal"; | ||||
289 | } | ||||
290 | |||||
291 | { | ||||
292 | |||||
293 | # was in Encode::utf8 | ||||
294 | 1 | 4µs | package Encode::utf8; | ||
295 | 1 | 2µs | push @Encode::utf8::ISA, 'Encode::Encoding'; | ||
296 | |||||
297 | # | ||||
298 | 1 | 100ns | if ($use_xs) { | ||
299 | Encode::DEBUG and warn __PACKAGE__, " XS on"; | ||||
300 | 1 | 400ns | *decode = \&decode_xs; | ||
301 | 1 | 200ns | *encode = \&encode_xs; | ||
302 | } | ||||
303 | else { | ||||
304 | Encode::DEBUG and warn __PACKAGE__, " XS off"; | ||||
305 | *decode = sub { | ||||
306 | my ( undef, $octets, $chk ) = @_; | ||||
307 | my $str = Encode::decode_utf8($octets); | ||||
308 | if ( defined $str ) { | ||||
309 | $_[1] = '' if $chk; | ||||
310 | return $str; | ||||
311 | } | ||||
312 | return undef; | ||||
313 | }; | ||||
314 | *encode = sub { | ||||
315 | my ( undef, $string, $chk ) = @_; | ||||
316 | my $octets = Encode::encode_utf8($string); | ||||
317 | $_[1] = '' if $chk; | ||||
318 | return $octets; | ||||
319 | }; | ||||
320 | } | ||||
321 | *cat_decode = sub { # ($obj, $dst, $src, $pos, $trm, $chk) | ||||
322 | # currently ignores $chk | ||||
323 | my ( undef, undef, undef, $pos, $trm ) = @_; | ||||
324 | my ( $rdst, $rsrc, $rpos ) = \@_[ 1, 2, 3 ]; | ||||
325 | 2 | 284µs | 2 | 190µs | # spent 188µs (186+2) within Encode::utf8::BEGIN@325 which was called:
# once (186µs+2µs) by Pod::Text::BEGIN@32 at line 325 # spent 188µs making 1 call to Encode::utf8::BEGIN@325
# spent 2µs making 1 call to bytes::import |
326 | if ( ( my $npos = index( $$rsrc, $trm, $pos ) ) >= 0 ) { | ||||
327 | $$rdst .= | ||||
328 | substr( $$rsrc, $pos, $npos - $pos + length($trm) ); | ||||
329 | $$rpos = $npos + length($trm); | ||||
330 | return 1; | ||||
331 | } | ||||
332 | $$rdst .= substr( $$rsrc, $pos ); | ||||
333 | $$rpos = length($$rsrc); | ||||
334 | return ''; | ||||
335 | 1 | 2µs | }; | ||
336 | 1 | 1µs | $Encode::Encoding{utf8} = | ||
337 | bless { Name => "utf8" } => "Encode::utf8"; | ||||
338 | 1 | 1µs | $Encode::Encoding{"utf-8-strict"} = | ||
339 | bless { Name => "utf-8-strict", strict_utf8 => 1 } | ||||
340 | => "Encode::utf8"; | ||||
341 | } | ||||
342 | } | ||||
343 | |||||
344 | 1 | 20µs | 1; | ||
345 | |||||
346 | __END__ | ||||
# spent 4µs within Encode::CORE:match which was called:
# once (4µs+0s) by Pod::Text::BEGIN@32 at line 7 | |||||
# spent 2µs within Encode::CORE:subst which was called 2 times, avg 800ns/call:
# 2 times (2µs+0s) by Encode::getEncoding at line 104, avg 800ns/call | |||||
# spent 8µs within Encode::PERLQQ which was called 2 times, avg 4µs/call:
# once (7µs+0s) by XSLoader::load at line 95 of XSLoader.pm
# once (800ns+0s) by PerlIO::import at line 16 of PerlIO/encoding.pm | |||||
# spent 700ns within Encode::STOP_AT_PARTIAL which was called:
# once (700ns+0s) by PerlIO::import at line 16 of PerlIO/encoding.pm | |||||
# spent 700ns within Encode::WARN_ON_ERR which was called:
# once (700ns+0s) by PerlIO::import at line 16 of PerlIO/encoding.pm | |||||
# spent 180µs (167+13) within Encode::utf8::decode_xs which was called 11 times, avg 16µs/call:
# 11 times (167µs+13µs) by Path::Tiny::CORE:readline at line 997 of Path/Tiny.pm, avg 16µs/call |