← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 19:05:14 2015
Reported on Fri Jul 31 19:08:09 2015

Filename/usr/share/perl5/vendor_perl/CGI/Util.pm
StatementsExecuted 2859 statements in 7.41ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
218421.88ms3.08msCGI::Util::::rearrangeCGI::Util::rearrange
1111.52ms1.93msCGI::Util::::BEGIN@10CGI::Util::BEGIN@10
1111.38ms1.57msCGI::Util::::BEGIN@2CGI::Util::BEGIN@2
218111.20ms1.20msCGI::Util::::_rearrange_paramsCGI::Util::_rearrange_params
4333920µs1.08msCGI::Util::::make_attributesCGI::Util::make_attributes
4511164µs164µsCGI::Util::::simple_escapeCGI::Util::simple_escape
42114µs14µsCGI::Util::::unescapeCGI::Util::unescape
11113µs32µsCGI::Util::::BEGIN@4CGI::Util::BEGIN@4
0000s0sCGI::Util::::ascii2ebcdicCGI::Util::ascii2ebcdic
0000s0sCGI::Util::::ebcdic2asciiCGI::Util::ebcdic2ascii
0000s0sCGI::Util::::escapeCGI::Util::escape
0000s0sCGI::Util::::expire_calcCGI::Util::expire_calc
0000s0sCGI::Util::::expiresCGI::Util::expires
0000s0sCGI::Util::::rearrange_headerCGI::Util::rearrange_header
0000s0sCGI::Util::::utf8_chrCGI::Util::utf8_chr
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package CGI::Util;
22170µs21.69ms
# spent 1.57ms (1.38+194µs) within CGI::Util::BEGIN@2 which was called: # once (1.38ms+194µs) by CGI::BEGIN@28 at line 2
use base 'Exporter';
# spent 1.57ms making 1 call to CGI::Util::BEGIN@2 # spent 112µs making 1 call to base::import
3116µsrequire 5.008001;
4293µs252µs
# spent 32µs (13+20) within CGI::Util::BEGIN@4 which was called: # once (13µs+20µs) by CGI::BEGIN@28 at line 4
use strict;
# spent 32µs making 1 call to CGI::Util::BEGIN@4 # spent 20µs making 1 call to strict::import
513µsour @EXPORT_OK = qw(rearrange rearrange_header make_attributes unescape escape
6 expires ebcdic2ascii ascii2ebcdic);
7
81400nsour $VERSION = '3.62';
9
1022.70ms21.99ms
# spent 1.93ms (1.52+409µs) within CGI::Util::BEGIN@10 which was called: # once (1.52ms+409µs) by CGI::BEGIN@28 at line 10
use constant EBCDIC => "\t" ne "\011";
# spent 1.93ms making 1 call to CGI::Util::BEGIN@10 # spent 56µs making 1 call to constant::import
11
12# This option is not documented and may change or go away.
13# The HTML spec does not require attributes to be sorted,
14# but it's useful for testing to get a predictable order back.
151100nsour $SORT_ATTRIBUTES;
16
17# (ord('^') == 95) for codepage 1047 as on os390, vmesa
18120µsour @A2E = (
19 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 21, 11, 12, 13, 14, 15,
20 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
21 64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
22 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
23 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
24 215,216,217,226,227,228,229,230,231,232,233,173,224,189, 95,109,
25 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
26 151,152,153,162,163,164,165,166,167,168,169,192, 79,208,161, 7,
27 32, 33, 34, 35, 36, 37, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
28 48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,255,
29 65,170, 74,177,159,178,106,181,187,180,154,138,176,202,175,188,
30 144,143,234,250,190,160,182,179,157,218,155,139,183,184,185,171,
31 100,101, 98,102, 99,103,158,104,116,113,114,115,120,117,118,119,
32 172,105,237,238,235,239,236,191,128,253,254,251,252,186,174, 89,
33 68, 69, 66, 70, 67, 71,156, 72, 84, 81, 82, 83, 88, 85, 86, 87,
34 140, 73,205,206,203,207,204,225,112,221,222,219,220,141,142,223
35 );
36121µsour @E2A = (
37 0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15,
38 16, 17, 18, 19,157, 10, 8,135, 24, 25,146,143, 28, 29, 30, 31,
39 128,129,130,131,132,133, 23, 27,136,137,138,139,140, 5, 6, 7,
40 144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26,
41 32,160,226,228,224,225,227,229,231,241,162, 46, 60, 40, 43,124,
42 38,233,234,235,232,237,238,239,236,223, 33, 36, 42, 41, 59, 94,
43 45, 47,194,196,192,193,195,197,199,209,166, 44, 37, 95, 62, 63,
44 248,201,202,203,200,205,206,207,204, 96, 58, 35, 64, 39, 61, 34,
45 216, 97, 98, 99,100,101,102,103,104,105,171,187,240,253,254,177,
46 176,106,107,108,109,110,111,112,113,114,170,186,230,184,198,164,
47 181,126,115,116,117,118,119,120,121,122,161,191,208, 91,222,174,
48 172,163,165,183,169,167,182,188,189,190,221,168,175, 93,180,215,
49 123, 65, 66, 67, 68, 69, 70, 71, 72, 73,173,244,246,242,243,245,
50 125, 74, 75, 76, 77, 78, 79, 80, 81, 82,185,251,252,249,250,255,
51 92,247, 83, 84, 85, 86, 87, 88, 89, 90,178,212,214,210,211,213,
52 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,179,219,220,217,218,159
53 );
54
55if (EBCDIC && ord('^') == 106) { # as in the BS2000 posix-bc coded character set
56 $A2E[91] = 187; $A2E[92] = 188; $A2E[94] = 106; $A2E[96] = 74;
57 $A2E[123] = 251; $A2E[125] = 253; $A2E[126] = 255; $A2E[159] = 95;
58 $A2E[162] = 176; $A2E[166] = 208; $A2E[168] = 121; $A2E[172] = 186;
59 $A2E[175] = 161; $A2E[217] = 224; $A2E[219] = 221; $A2E[221] = 173;
60 $A2E[249] = 192;
61
62 $E2A[74] = 96; $E2A[95] = 159; $E2A[106] = 94; $E2A[121] = 168;
63 $E2A[161] = 175; $E2A[173] = 221; $E2A[176] = 162; $E2A[186] = 172;
64 $E2A[187] = 91; $E2A[188] = 92; $E2A[192] = 249; $E2A[208] = 166;
65 $E2A[221] = 219; $E2A[224] = 217; $E2A[251] = 123; $E2A[253] = 125;
66 $E2A[255] = 126;
67 }
68elsif (EBCDIC && ord('^') == 176) { # as in codepage 037 on os400
69 $A2E[10] = 37; $A2E[91] = 186; $A2E[93] = 187; $A2E[94] = 176;
70 $A2E[133] = 21; $A2E[168] = 189; $A2E[172] = 95; $A2E[221] = 173;
71
72 $E2A[21] = 133; $E2A[37] = 10; $E2A[95] = 172; $E2A[173] = 221;
73 $E2A[176] = 94; $E2A[186] = 91; $E2A[187] = 93; $E2A[189] = 168;
74}
75
76# Smart rearrangement of parameters to allow named parameter
77# calling. We do the rearrangement if:
78# the first parameter begins with a -
79
80
# spent 3.08ms (1.88+1.20) within CGI::Util::rearrange which was called 218 times, avg 14µs/call: # 207 times (1.81ms+1.13ms) by Foswiki::Request::param at line 410 of /var/www/foswiki11/lib/Foswiki/Request.pm, avg 14µs/call # 6 times (34µs+21µs) by Foswiki::Request::header at line 549 of /var/www/foswiki11/lib/Foswiki/Request.pm, avg 9µs/call # 3 times (23µs+38µs) by Foswiki::Request::url at line 241 of /var/www/foswiki11/lib/Foswiki/Request.pm, avg 20µs/call # 2 times (11µs+7µs) by CGI::delete at line 6 of (eval 46)[CGI.pm:884], avg 9µs/call
sub rearrange {
81218204µs my ($order,@param) = @_;
82218487µs2181.20ms my ($result, $leftover) = _rearrange_params( $order, @param );
# spent 1.20ms making 218 calls to CGI::Util::_rearrange_params, avg 5µs/call
83218220µs push @$result, make_attributes( $leftover, defined $CGI::Q ? $CGI::Q->{escape} : 1 )
84 if keys %$leftover;
85218809µs @$result;
86}
87
88sub rearrange_header {
89 my ($order,@param) = @_;
90
91 my ($result,$leftover) = _rearrange_params( $order, @param );
92 push @$result, make_attributes( $leftover, 0, 1 ) if keys %$leftover;
93
94 @$result;
95}
96
97
# spent 1.20ms within CGI::Util::_rearrange_params which was called 218 times, avg 5µs/call: # 218 times (1.20ms+0s) by CGI::Util::rearrange at line 82, avg 5µs/call
sub _rearrange_params {
98218175µs my($order,@param) = @_;
99218111µs return [] unless @param;
100
101209139µs if (ref($param[0]) eq 'HASH') {
102 @param = %{$param[0]};
103 } else {
104 return \@param
105209972µs unless (defined($param[0]) && substr($param[0],0,1) eq '-');
106 }
107
108 # map parameters into positional indices
1093800ns my ($i,%pos);
1103800ns $i = 0;
11133µs foreach (@$order) {
1122422µs foreach (ref($_) eq 'ARRAY' ? @$_ : $_) { $pos{lc($_)} = $i; }
113103µs $i++;
114 }
115
11632µs my (@result,%leftover);
11737µs $#result = $#$order; # preextend
11832µs while (@param) {
11964µs my $key = lc(shift(@param));
12068µs $key =~ s/^\-//;
121610µs if (exists $pos{$key}) {
122 $result[$pos{$key}] = shift(@param);
123 } else {
124 $leftover{$key} = shift(@param);
125 }
126 }
127
128314µs return \@result, \%leftover;
129}
130
131
# spent 1.08ms (920µs+164µs) within CGI::Util::make_attributes which was called 43 times, avg 25µs/call: # 24 times (497µs+62µs) by CGI::a at line 6 of (eval 204)[CGI.pm:884], avg 23µs/call # 16 times (316µs+66µs) by CGI::span at line 6 of (eval 245)[CGI.pm:884], avg 24µs/call # 3 times (107µs+36µs) by CGI::img at line 6 of (eval 244)[CGI.pm:884], avg 48µs/call
sub make_attributes {
1324317µs my $attr = shift;
1334342µs return () unless $attr && ref($attr) && ref($attr) eq 'HASH';
1344320µs my $escape = shift || 0;
1354311µs my $do_not_quote = shift;
136
1374322µs my $quote = $do_not_quote ? '' : '"';
138
13943103µs my @attr_keys= keys %$attr;
1404312µs if ($SORT_ATTRIBUTES) {
141 @attr_keys= sort @attr_keys;
142 }
1434310µs my(@att);
1444354µs foreach (@attr_keys) {
1456331µs my($key) = $_;
1466358µs $key=~s/^\-//; # get rid of initial - if present
147
148 # old way: breaks EBCDIC!
149 # $key=~tr/A-Z_/a-z-/; # parameters are lower case, use dashes
150
1516379µs ($key="\L$key") =~ tr/_/-/; # parameters are lower case, use dashes
152
15363115µs45164µs my $value = $escape ? simple_escape($attr->{$_}) : $attr->{$_};
# spent 164µs making 45 calls to CGI::Util::simple_escape, avg 4µs/call
15463155µs push(@att,defined($attr->{$_}) ? qq/$key=$quote$value$quote/ : qq/$key/);
155 }
15643170µs return @att;
157}
158
159
# spent 164µs within CGI::Util::simple_escape which was called 45 times, avg 4µs/call: # 45 times (164µs+0s) by CGI::Util::make_attributes at line 153, avg 4µs/call
sub simple_escape {
1604525µs return unless defined(my $toencode = shift);
1614524µs $toencode =~ s{&}{&}gso;
1624512µs $toencode =~ s{<}{&lt;}gso;
1634514µs $toencode =~ s{>}{&gt;}gso;
1644513µs $toencode =~ s{\"}{&quot;}gso;
165# Doesn't work. Can't work. forget it.
166# $toencode =~ s{\x8b}{&#139;}gso;
167# $toencode =~ s{\x9b}{&#155;}gso;
16845146µs $toencode;
169}
170
171sub utf8_chr {
172 my $c = shift(@_);
173 my $u = chr($c);
174 utf8::encode($u); # drop utf8 flag
175 return $u;
176}
177
178# unescape URL-encoded data
179
# spent 14µs within CGI::Util::unescape which was called 4 times, avg 4µs/call: # 2 times (9µs+0s) by CGI::parse_params at line 788 of CGI.pm, avg 5µs/call # 2 times (5µs+0s) by CGI::parse_params at line 789 of CGI.pm, avg 3µs/call
sub unescape {
18042µs shift() if @_ > 0 and (ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass));
18142µs my $todecode = shift;
1824500ns return undef unless defined($todecode);
18342µs $todecode =~ tr/+/ /; # pluses become spaces
18441µs if (EBCDIC) {
185 $todecode =~ s/%([0-9a-fA-F]{2})/chr $A2E[hex($1)]/ge;
186 } else {
187 # handle surrogate pairs first -- dankogai. Ref: http://unicode.org/faq/utf_bom.html#utf16-2
18841µs $todecode =~ s{
189 %u([Dd][89a-bA-B][0-9a-fA-F]{2}) # hi
190 %u([Dd][c-fC-F][0-9a-fA-F]{2}) # lo
191 }{
192 utf8_chr(
193 0x10000
194 + (hex($1) - 0xD800) * 0x400
195 + (hex($2) - 0xDC00)
196 )
197 }gex;
19841µs $todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
199 defined($1)? chr hex($1) : utf8_chr(hex($2))/ge;
200 }
201410µs return $todecode;
202}
203
204# URL-encode data
205#
206# We cannot use the %u escapes, they were rejected by W3C, so the official
207# way is %XX-escaped utf-8 encoding.
208# Naturally, Unicode strings have to be converted to their utf-8 byte
209# representation.
210# Byte strings were traditionally used directly as a sequence of octets.
211# This worked if they actually represented binary data (i.e. in CGI::Compress).
212# This also worked if these byte strings were actually utf-8 encoded; e.g.,
213# when the source file used utf-8 without the apropriate "use utf8;".
214# This fails if the byte string is actually a Latin 1 encoded string, but it
215# was always so and cannot be fixed without breaking the binary data case.
216# -- Stepan Kasal <skasal@redhat.com>
217#
218
219sub escape {
220 # If we being called in an OO-context, discard the first argument.
221 shift() if @_ > 1 and ( ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass));
222 my $toencode = shift;
223 return undef unless defined($toencode);
224 utf8::encode($toencode) if utf8::is_utf8($toencode);
225 if (EBCDIC) {
226 $toencode=~s/([^a-zA-Z0-9_.~-])/uc sprintf("%%%02x",$E2A[ord($1)])/eg;
227 } else {
228 $toencode=~s/([^a-zA-Z0-9_.~-])/uc sprintf("%%%02x",ord($1))/eg;
229 }
230 return $toencode;
231}
232
233# This internal routine creates date strings suitable for use in
234# cookies and HTTP headers. (They differ, unfortunately.)
235# Thanks to Mark Fisher for this.
236sub expires {
237 my($time,$format) = @_;
238 $format ||= 'http';
239
240 my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
241 my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/;
242
243 # pass through preformatted dates for the sake of expire_calc()
244 $time = expire_calc($time);
245 return $time unless $time =~ /^\d+$/;
246
247 # make HTTP/cookie date string from GMT'ed time
248 # (cookies use '-' as date separator, HTTP uses ' ')
249 my($sc) = ' ';
250 $sc = '-' if $format eq "cookie";
251 my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time);
252 $year += 1900;
253 return sprintf("%s, %02d$sc%s$sc%04d %02d:%02d:%02d GMT",
254 $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec);
255}
256
257# This internal routine creates an expires time exactly some number of
258# hours from the current time. It incorporates modifications from
259# Mark Fisher.
260sub expire_calc {
261 my($time) = @_;
262 my(%mult) = ('s'=>1,
263 'm'=>60,
264 'h'=>60*60,
265 'd'=>60*60*24,
266 'M'=>60*60*24*30,
267 'y'=>60*60*24*365);
268 # format for time can be in any of the forms...
269 # "now" -- expire immediately
270 # "+180s" -- in 180 seconds
271 # "+2m" -- in 2 minutes
272 # "+12h" -- in 12 hours
273 # "+1d" -- in 1 day
274 # "+3M" -- in 3 months
275 # "+2y" -- in 2 years
276 # "-3m" -- 3 minutes ago(!)
277 # If you don't supply one of these forms, we assume you are
278 # specifying the date yourself
279 my($offset);
280 if (!$time || (lc($time) eq 'now')) {
281 $offset = 0;
282 } elsif ($time=~/^\d+/) {
283 return $time;
284 } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([smhdMy])/) {
285 $offset = ($mult{$2} || 1)*$1;
286 } else {
287 return $time;
288 }
289 my $cur_time = time;
290 return ($cur_time+$offset);
291}
292
293sub ebcdic2ascii {
294 my $data = shift;
295 $data =~ s/(.)/chr $E2A[ord($1)]/ge;
296 $data;
297}
298
299sub ascii2ebcdic {
300 my $data = shift;
301 $data =~ s/(.)/chr $A2E[ord($1)]/ge;
302 $data;
303}
304
305145µs1;
306
307__END__