Filename | /usr/lib/perl/5.14/Encode.pm |
Statements | Executed 78 statements in 2.44ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 1.41ms | 1.97ms | BEGIN@48 | Encode::
1 | 1 | 1 | 479µs | 510µs | predefine_encodings | Encode::
4 | 1 | 1 | 12µs | 12µs | define_encoding | Encode::
1 | 1 | 1 | 12µs | 15µs | BEGIN@5 | Encode::
1 | 1 | 1 | 11µs | 14µs | BEGIN@323 | Encode::utf8::
1 | 1 | 1 | 9µs | 20µs | BEGIN@240 | Encode::
1 | 1 | 1 | 6µs | 11µs | BEGIN@6 | Encode::
1 | 1 | 1 | 6µs | 46µs | BEGIN@13 | Encode::
1 | 1 | 1 | 4µs | 4µs | BEGIN@9 | Encode::
1 | 1 | 1 | 4µs | 4µs | CORE:match (opcode) | Encode::
0 | 0 | 0 | 0s | 0s | __ANON__[:283] | Encode::Internal::
0 | 0 | 0 | 0s | 0s | __ANON__[:258] | Encode::UTF_EBCDIC::
0 | 0 | 0 | 0s | 0s | __ANON__[:270] | 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 | find_encoding | Encode::
0 | 0 | 0 | 0s | 0s | from_to | Encode::
0 | 0 | 0 | 0s | 0s | getEncoding | Encode::
0 | 0 | 0 | 0s | 0s | perlio_ok | Encode::
0 | 0 | 0 | 0s | 0s | resolve_alias | Encode::
0 | 0 | 0 | 0s | 0s | __ANON__[:311] | Encode::utf8::
0 | 0 | 0 | 0s | 0s | __ANON__[:317] | Encode::utf8::
0 | 0 | 0 | 0s | 0s | __ANON__[:333] | Encode::utf8::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # | ||||
2 | # $Id: Encode.pm,v 2.42 2010/12/31 22:48:10 dankogai Exp $ | ||||
3 | # | ||||
4 | package Encode; | ||||
5 | 2 | 21µs | 2 | 18µs | # spent 15µs (12+3) within Encode::BEGIN@5 which was called:
# once (12µs+3µs) by MongoDB::MongoClient::BEGIN@34 at line 5 # spent 15µs making 1 call to Encode::BEGIN@5
# spent 3µs making 1 call to strict::import |
6 | 2 | 54µs | 2 | 16µs | # spent 11µs (6+5) within Encode::BEGIN@6 which was called:
# once (6µs+5µs) by MongoDB::MongoClient::BEGIN@34 at line 6 # spent 11µs making 1 call to Encode::BEGIN@6
# spent 5µs making 1 call to warnings::import |
7 | 1 | 11µs | 1 | 4µs | our $VERSION = sprintf "%d.%02d_01", q$Revision: 2.42 $ =~ /(\d+)/g; # spent 4µs making 1 call to Encode::CORE:match |
8 | sub DEBUG () { 0 } | ||||
9 | 2 | 34µs | 1 | 4µs | # spent 4µs within Encode::BEGIN@9 which was called:
# once (4µs+0s) by MongoDB::MongoClient::BEGIN@34 at line 9 # spent 4µs making 1 call to Encode::BEGIN@9 |
10 | 1 | 180µs | 1 | 207µs | XSLoader::load( __PACKAGE__, $VERSION ); # spent 207µs making 1 call to XSLoader::load |
11 | |||||
12 | 1 | 700ns | require Exporter; | ||
13 | 2 | 121µs | 2 | 87µs | # spent 46µs (6+40) within Encode::BEGIN@13 which was called:
# once (6µs+40µs) by MongoDB::MongoClient::BEGIN@34 at line 13 # spent 46µs making 1 call to Encode::BEGIN@13
# spent 40µ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 | 900ns | our @FB_CONSTS = qw( | ||
26 | FB_DEFAULT FB_CROAK FB_QUIET FB_WARN | ||||
27 | FB_PERLQQ FB_HTMLCREF FB_XMLCREF | ||||
28 | ); | ||||
29 | 1 | 6µ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 | 11µ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 | 400ns | our $ON_EBCDIC = ( ord("A") == 193 ); | ||
47 | |||||
48 | 2 | 1.11ms | 2 | 2.01ms | # spent 1.97ms (1.41+554µs) within Encode::BEGIN@48 which was called:
# once (1.41ms+554µs) by MongoDB::MongoClient::BEGIN@34 at line 48 # spent 1.97ms making 1 call to Encode::BEGIN@48
# spent 38µs making 1 call to Exporter::import |
49 | |||||
50 | # Make a %Encoding package variable to allow a certain amount of cheating | ||||
51 | 1 | 200ns | our %Encoding; | ||
52 | 1 | 100ns | our %ExtModule; | ||
53 | 1 | 116µ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 | 4 | 39µs | eval { | ||
58 | local $SIG{__DIE__}; | ||||
59 | local $SIG{__WARN__}; | ||||
60 | require Encode::ConfigLocal; | ||||
61 | }; | ||||
62 | |||||
63 | sub encodings { | ||||
64 | my $class = shift; | ||||
65 | my %enc; | ||||
66 | if ( @_ and $_[0] eq ":all" ) { | ||||
67 | %enc = ( %Encoding, %ExtModule ); | ||||
68 | } | ||||
69 | else { | ||||
70 | %enc = %Encoding; | ||||
71 | for my $mod ( map { m/::/o ? $_ : "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 12µs within Encode::define_encoding which was called 4 times, avg 3µs/call:
# 4 times (12µs+0s) by XSLoader::load at line 95 of XSLoader.pm, avg 3µs/call | ||||
89 | 28 | 18µs | my $obj = shift; | ||
90 | my $name = shift; | ||||
91 | $Encoding{$name} = $obj; | ||||
92 | my $lc = lc($name); | ||||
93 | define_alias( $lc => $obj ) unless $lc eq $name; | ||||
94 | while (@_) { | ||||
95 | my $alias = shift; | ||||
96 | define_alias( $alias, $obj ); | ||||
97 | } | ||||
98 | return $obj; | ||||
99 | } | ||||
100 | |||||
101 | sub getEncoding { | ||||
102 | my ( $class, $name, $skip_external ) = @_; | ||||
103 | |||||
104 | ref($name) && $name->can('renew') and return $name; | ||||
105 | exists $Encoding{$name} and return $Encoding{$name}; | ||||
106 | my $lc = lc $name; | ||||
107 | exists $Encoding{$lc} and return $Encoding{$lc}; | ||||
108 | |||||
109 | my $oc = $class->find_alias($name); | ||||
110 | defined($oc) and return $oc; | ||||
111 | $lc ne $name and $oc = $class->find_alias($lc); | ||||
112 | defined($oc) and return $oc; | ||||
113 | |||||
114 | unless ($skip_external) { | ||||
115 | if ( my $mod = $ExtModule{$name} || $ExtModule{$lc} ) { | ||||
116 | $mod =~ s,::,/,g; | ||||
117 | $mod .= '.pm'; | ||||
118 | eval { require $mod; }; | ||||
119 | exists $Encoding{$name} and return $Encoding{$name}; | ||||
120 | } | ||||
121 | } | ||||
122 | return; | ||||
123 | } | ||||
124 | |||||
125 | sub find_encoding($;$) { | ||||
126 | my ( $name, $skip_external ) = @_; | ||||
127 | return __PACKAGE__->getEncoding( $name, $skip_external ); | ||||
128 | } | ||||
129 | |||||
130 | sub resolve_alias($) { | ||||
131 | my $obj = find_encoding(shift); | ||||
132 | defined $obj and return $obj->name; | ||||
133 | return; | ||||
134 | } | ||||
135 | |||||
136 | sub clone_encoding($) { | ||||
137 | my $obj = find_encoding(shift); | ||||
138 | ref $obj or return; | ||||
139 | eval { require Storable }; | ||||
140 | $@ and return; | ||||
141 | return Storable::dclone($obj); | ||||
142 | } | ||||
143 | |||||
144 | sub encode($$;$) { | ||||
145 | my ( $name, $string, $check ) = @_; | ||||
146 | return undef unless defined $string; | ||||
147 | $string .= '' if ref $string; # stringify; | ||||
148 | $check ||= 0; | ||||
149 | unless ( defined $name ) { | ||||
150 | require Carp; | ||||
151 | Carp::croak("Encoding name should not be undef"); | ||||
152 | } | ||||
153 | my $enc = find_encoding($name); | ||||
154 | unless ( defined $enc ) { | ||||
155 | require Carp; | ||||
156 | Carp::croak("Unknown encoding '$name'"); | ||||
157 | } | ||||
158 | my $octets = $enc->encode( $string, $check ); | ||||
159 | $_[1] = $string if $check and !ref $check and !( $check & LEAVE_SRC() ); | ||||
160 | return $octets; | ||||
161 | } | ||||
162 | 1 | 2µs | *str2bytes = \&encode; | ||
163 | |||||
164 | sub decode($$;$) { | ||||
165 | my ( $name, $octets, $check ) = @_; | ||||
166 | return undef unless defined $octets; | ||||
167 | $octets .= '' if ref $octets; | ||||
168 | $check ||= 0; | ||||
169 | my $enc = find_encoding($name); | ||||
170 | unless ( defined $enc ) { | ||||
171 | require Carp; | ||||
172 | Carp::croak("Unknown encoding '$name'"); | ||||
173 | } | ||||
174 | my $string = $enc->decode( $octets, $check ); | ||||
175 | $_[1] = $octets if $check and !ref $check and !( $check & LEAVE_SRC() ); | ||||
176 | return $string; | ||||
177 | } | ||||
178 | 1 | 500ns | *bytes2str = \&decode; | ||
179 | |||||
180 | sub from_to($$$;$) { | ||||
181 | my ( $string, $from, $to, $check ) = @_; | ||||
182 | return undef unless defined $string; | ||||
183 | $check ||= 0; | ||||
184 | my $f = find_encoding($from); | ||||
185 | unless ( defined $f ) { | ||||
186 | require Carp; | ||||
187 | Carp::croak("Unknown encoding '$from'"); | ||||
188 | } | ||||
189 | my $t = find_encoding($to); | ||||
190 | unless ( defined $t ) { | ||||
191 | require Carp; | ||||
192 | Carp::croak("Unknown encoding '$to'"); | ||||
193 | } | ||||
194 | my $uni = $f->decode($string); | ||||
195 | $_[0] = $string = $t->encode( $uni, $check ); | ||||
196 | return undef if ( $check && length($uni) ); | ||||
197 | return defined( $_[0] ) ? length($string) : undef; | ||||
198 | } | ||||
199 | |||||
200 | sub encode_utf8($) { | ||||
201 | my ($str) = @_; | ||||
202 | utf8::encode($str); | ||||
203 | return $str; | ||||
204 | } | ||||
205 | |||||
206 | 1 | 100ns | my $utf8enc; | ||
207 | |||||
208 | sub decode_utf8($;$) { | ||||
209 | my ( $octets, $check ) = @_; | ||||
210 | return $octets if is_utf8($octets); | ||||
211 | return undef unless defined $octets; | ||||
212 | $octets .= '' if ref $octets; | ||||
213 | $check ||= 0; | ||||
214 | $utf8enc ||= find_encoding('utf8'); | ||||
215 | my $string = $utf8enc->decode( $octets, $check ); | ||||
216 | $_[0] = $octets if $check and !ref $check and !( $check & LEAVE_SRC() ); | ||||
217 | return $string; | ||||
218 | } | ||||
219 | |||||
220 | # sub decode_utf8($;$) { | ||||
221 | # my ( $str, $check ) = @_; | ||||
222 | # return $str if is_utf8($str); | ||||
223 | # if ($check) { | ||||
224 | # return decode( "utf8", $str, $check ); | ||||
225 | # } | ||||
226 | # else { | ||||
227 | # return decode( "utf8", $str ); | ||||
228 | # return $str; | ||||
229 | # } | ||||
230 | # } | ||||
231 | |||||
232 | 1 | 2µs | 1 | 510µs | predefine_encodings(1); # spent 510µs making 1 call to Encode::predefine_encodings |
233 | |||||
234 | # | ||||
235 | # This is to restore %Encoding if really needed; | ||||
236 | # | ||||
237 | |||||
238 | # spent 510µs (479+31) within Encode::predefine_encodings which was called:
# once (479µs+31µs) by MongoDB::MongoClient::BEGIN@34 at line 232 | ||||
239 | 4 | 84µs | require Encode::Encoding; | ||
240 | 2 | 448µs | 2 | 31µs | # spent 20µs (9+11) within Encode::BEGIN@240 which was called:
# once (9µs+11µs) by MongoDB::MongoClient::BEGIN@34 at line 240 # spent 20µs making 1 call to Encode::BEGIN@240
# spent 11µs making 1 call to warnings::unimport |
241 | my $use_xs = shift; | ||||
242 | 4 | 20µs | if ($ON_EBCDIC) { | ||
243 | |||||
244 | # was in Encode::UTF_EBCDIC | ||||
245 | package Encode::UTF_EBCDIC; | ||||
246 | push @Encode::UTF_EBCDIC::ISA, 'Encode::Encoding'; | ||||
247 | *decode = sub { | ||||
248 | my ( $obj, $str, $chk ) = @_; | ||||
249 | my $res = ''; | ||||
250 | for ( my $i = 0 ; $i < length($str) ; $i++ ) { | ||||
251 | $res .= | ||||
252 | chr( | ||||
253 | utf8::unicode_to_native( ord( substr( $str, $i, 1 ) ) ) | ||||
254 | ); | ||||
255 | } | ||||
256 | $_[1] = '' if $chk; | ||||
257 | return $res; | ||||
258 | }; | ||||
259 | *encode = sub { | ||||
260 | my ( $obj, $str, $chk ) = @_; | ||||
261 | my $res = ''; | ||||
262 | for ( my $i = 0 ; $i < length($str) ; $i++ ) { | ||||
263 | $res .= | ||||
264 | chr( | ||||
265 | utf8::native_to_unicode( ord( substr( $str, $i, 1 ) ) ) | ||||
266 | ); | ||||
267 | } | ||||
268 | $_[1] = '' if $chk; | ||||
269 | return $res; | ||||
270 | }; | ||||
271 | $Encode::Encoding{Unicode} = | ||||
272 | bless { Name => "UTF_EBCDIC" } => "Encode::UTF_EBCDIC"; | ||||
273 | } | ||||
274 | else { | ||||
275 | |||||
276 | package Encode::Internal; | ||||
277 | push @Encode::Internal::ISA, 'Encode::Encoding'; | ||||
278 | *decode = sub { | ||||
279 | my ( $obj, $str, $chk ) = @_; | ||||
280 | utf8::upgrade($str); | ||||
281 | $_[1] = '' if $chk; | ||||
282 | return $str; | ||||
283 | }; | ||||
284 | *encode = \&decode; | ||||
285 | $Encode::Encoding{Unicode} = | ||||
286 | bless { Name => "Internal" } => "Encode::Internal"; | ||||
287 | } | ||||
288 | |||||
289 | { | ||||
290 | |||||
291 | # was in Encode::utf8 | ||||
292 | package Encode::utf8; | ||||
293 | 5 | 12µs | push @Encode::utf8::ISA, 'Encode::Encoding'; | ||
294 | |||||
295 | # | ||||
296 | 2 | 1µs | if ($use_xs) { | ||
297 | Encode::DEBUG and warn __PACKAGE__, " XS on"; | ||||
298 | *decode = \&decode_xs; | ||||
299 | *encode = \&encode_xs; | ||||
300 | } | ||||
301 | else { | ||||
302 | Encode::DEBUG and warn __PACKAGE__, " XS off"; | ||||
303 | *decode = sub { | ||||
304 | my ( $obj, $octets, $chk ) = @_; | ||||
305 | my $str = Encode::decode_utf8($octets); | ||||
306 | if ( defined $str ) { | ||||
307 | $_[1] = '' if $chk; | ||||
308 | return $str; | ||||
309 | } | ||||
310 | return undef; | ||||
311 | }; | ||||
312 | *encode = sub { | ||||
313 | my ( $obj, $string, $chk ) = @_; | ||||
314 | my $octets = Encode::encode_utf8($string); | ||||
315 | $_[1] = '' if $chk; | ||||
316 | return $octets; | ||||
317 | }; | ||||
318 | } | ||||
319 | *cat_decode = sub { # ($obj, $dst, $src, $pos, $trm, $chk) | ||||
320 | # currently ignores $chk | ||||
321 | my ( $obj, undef, undef, $pos, $trm ) = @_; | ||||
322 | my ( $rdst, $rsrc, $rpos ) = \@_[ 1, 2, 3 ]; | ||||
323 | 2 | 131µs | 2 | 18µs | # spent 14µs (11+3) within Encode::utf8::BEGIN@323 which was called:
# once (11µs+3µs) by MongoDB::MongoClient::BEGIN@34 at line 323 # spent 14µs making 1 call to Encode::utf8::BEGIN@323
# spent 3µs making 1 call to bytes::import |
324 | if ( ( my $npos = index( $$rsrc, $trm, $pos ) ) >= 0 ) { | ||||
325 | $$rdst .= | ||||
326 | substr( $$rsrc, $pos, $npos - $pos + length($trm) ); | ||||
327 | $$rpos = $npos + length($trm); | ||||
328 | return 1; | ||||
329 | } | ||||
330 | $$rdst .= substr( $$rsrc, $pos ); | ||||
331 | $$rpos = length($$rsrc); | ||||
332 | return ''; | ||||
333 | }; | ||||
334 | $Encode::Encoding{utf8} = | ||||
335 | bless { Name => "utf8" } => "Encode::utf8"; | ||||
336 | $Encode::Encoding{"utf-8-strict"} = | ||||
337 | bless { Name => "utf-8-strict", strict_utf8 => 1 } => | ||||
338 | "Encode::utf8"; | ||||
339 | } | ||||
340 | } | ||||
341 | |||||
342 | 1 | 15µs | 1; | ||
343 | |||||
344 | __END__ | ||||
# spent 4µs within Encode::CORE:match which was called:
# once (4µs+0s) by MongoDB::MongoClient::BEGIN@34 at line 7 |