Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/String/Format.pm |
Statements | Executed 10247 statements in 15.2ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
445 | 1 | 1 | 7.96ms | 29.9ms | stringf | String::Format::
445 | 1 | 1 | 7.28ms | 18.5ms | _replace | String::Format::
445 | 1 | 1 | 1.39ms | 1.39ms | CORE:subst (opcode) | String::Format::
890 | 1 | 1 | 758µs | 758µs | CORE:substcont (opcode) | String::Format::
445 | 1 | 1 | 662µs | 662µs | CORE:regcomp (opcode) | String::Format::
1 | 1 | 1 | 12µs | 24µs | BEGIN@21 | String::Format::
1 | 1 | 1 | 7µs | 20µs | BEGIN@23 | String::Format::
1 | 1 | 1 | 7µs | 56µs | BEGIN@24 | String::Format::
1 | 1 | 1 | 6µs | 35µs | BEGIN@22 | String::Format::
1 | 1 | 1 | 2µs | 2µs | CORE:qr (opcode) | String::Format::
0 | 0 | 0 | 0s | 0s | __ANON__[:93] | String::Format::
0 | 0 | 0 | 0s | 0s | stringfactory | String::Format::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package String::Format; | ||||
2 | |||||
3 | # ---------------------------------------------------------------------- | ||||
4 | # Copyright (C) 2002,2009 darren chamberlain <darren@cpan.org> | ||||
5 | # | ||||
6 | # This program is free software; you can redistribute it and/or | ||||
7 | # modify it under the terms of the GNU General Public License as | ||||
8 | # published by the Free Software Foundation; version 2. | ||||
9 | # | ||||
10 | # This program is distributed in the hope that it will be useful, but | ||||
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | # General Public License for more details. | ||||
14 | # | ||||
15 | # You should have received a copy of the GNU General Public License | ||||
16 | # along with this program; if not, write to the Free Software | ||||
17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
18 | # 02110-1301 USA. | ||||
19 | # ------------------------------------------------------------------- | ||||
20 | |||||
21 | 2 | 30µs | 2 | 36µs | # spent 24µs (12+12) within String::Format::BEGIN@21 which was called:
# once (12µs+12µs) by Perl::Critic::Violation::BEGIN@21 at line 21 # spent 24µs making 1 call to String::Format::BEGIN@21
# spent 12µs making 1 call to strict::import |
22 | 2 | 22µs | 2 | 63µs | # spent 35µs (6+28) within String::Format::BEGIN@22 which was called:
# once (6µs+28µs) by Perl::Critic::Violation::BEGIN@21 at line 22 # spent 35µs making 1 call to String::Format::BEGIN@22
# spent 28µs making 1 call to vars::import |
23 | 2 | 21µs | 2 | 33µs | # spent 20µs (7+13) within String::Format::BEGIN@23 which was called:
# once (7µs+13µs) by Perl::Critic::Violation::BEGIN@21 at line 23 # spent 20µs making 1 call to String::Format::BEGIN@23
# spent 13µs making 1 call to Exporter::import |
24 | 2 | 299µs | 2 | 104µs | # spent 56µs (7+48) within String::Format::BEGIN@24 which was called:
# once (7µs+48µs) by Perl::Critic::Violation::BEGIN@21 at line 24 # spent 56µs making 1 call to String::Format::BEGIN@24
# spent 48µs making 1 call to base::import |
25 | |||||
26 | 1 | 600ns | $VERSION = '1.17'; | ||
27 | 1 | 700ns | @EXPORT = qw(stringf); | ||
28 | |||||
29 | # spent 18.5ms (7.28+11.2) within String::Format::_replace which was called 445 times, avg 42µs/call:
# 445 times (7.28ms+11.2ms) by String::Format::stringf at line 85, avg 42µs/call | ||||
30 | 445 | 754µs | my ($args, $orig, $alignment, $min_width, | ||
31 | $max_width, $passme, $formchar) = @_; | ||||
32 | |||||
33 | # For unknown escapes, return the orignial | ||||
34 | 445 | 161µs | return $orig unless defined $args->{$formchar}; | ||
35 | |||||
36 | 445 | 110µs | $alignment = '+' unless defined $alignment; | ||
37 | |||||
38 | 445 | 201µs | my $replacement = $args->{$formchar}; | ||
39 | 445 | 228µs | if (ref $replacement eq 'CODE') { | ||
40 | # $passme gets passed to subrefs. | ||||
41 | 445 | 57µs | $passme ||= ""; | ||
42 | 445 | 126µs | $passme =~ tr/{}//d; | ||
43 | 445 | 645µs | 445 | 11.2ms | $replacement = $replacement->($passme); # spent 11.2ms making 445 calls to Perl::Critic::Policy::__ANON__[Perl/Critic/Policy.pm:467], avg 25µs/call |
44 | } | ||||
45 | |||||
46 | 445 | 131µs | my $replength = length $replacement; | ||
47 | 445 | 48µs | $min_width ||= $replength; | ||
48 | 445 | 45µs | $max_width ||= $replength; | ||
49 | |||||
50 | # length of replacement is between min and max | ||||
51 | 445 | 51µs | if (($replength > $min_width) && ($replength < $max_width)) { | ||
52 | return $replacement; | ||||
53 | } | ||||
54 | |||||
55 | # length of replacement is longer than max; truncate | ||||
56 | 445 | 10µs | if ($replength > $max_width) { | ||
57 | return substr($replacement, 0, $max_width); | ||||
58 | } | ||||
59 | |||||
60 | # length of replacement is less than min: pad | ||||
61 | 445 | 70µs | if ($alignment eq '-') { | ||
62 | # left align; pad in front | ||||
63 | return $replacement . " " x ($min_width - $replength); | ||||
64 | } | ||||
65 | |||||
66 | # right align, pad at end | ||||
67 | 445 | 1.20ms | return " " x ($min_width - $replength) . $replacement; | ||
68 | } | ||||
69 | |||||
70 | 1 | 9µs | 1 | 2µs | my $regex = qr/ # spent 2µs making 1 call to String::Format::CORE:qr |
71 | (% # leading '%' | ||||
72 | (-)? # left-align, rather than right | ||||
73 | (\d*)? # (optional) minimum field width | ||||
74 | (?:\.(\d*))? # (optional) maximum field width | ||||
75 | ({.*?})? # (optional) stuff inside | ||||
76 | (\S) # actual format character | ||||
77 | )/x; | ||||
78 | # spent 29.9ms (7.96+21.9) within String::Format::stringf which was called 445 times, avg 67µs/call:
# 445 times (7.96ms+21.9ms) by Perl::Critic::Policy::to_string at line 478 of Perl/Critic/Policy.pm, avg 67µs/call | ||||
79 | 445 | 144µs | my $format = shift || return; | ||
80 | 445 | 2.48ms | 445 | 612µs | my $args = UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ }; # spent 612µs making 445 calls to UNIVERSAL::isa, avg 1µs/call |
81 | 445 | 326µs | $args->{'n'} = "\n" unless exists $args->{'n'}; | ||
82 | 445 | 85µs | $args->{'t'} = "\t" unless exists $args->{'t'}; | ||
83 | 445 | 182µs | $args->{'%'} = "%" unless exists $args->{'%'}; | ||
84 | |||||
85 | 890 | 6.23ms | 2225 | 21.3ms | $format =~ s/$regex/_replace($args, $1, $2, $3, $4, $5, $6)/ge; # spent 18.5ms making 445 calls to String::Format::_replace, avg 42µs/call
# spent 1.39ms making 445 calls to String::Format::CORE:subst, avg 3µs/call
# spent 758µs making 890 calls to String::Format::CORE:substcont, avg 852ns/call
# spent 662µs making 445 calls to String::Format::CORE:regcomp, avg 1µs/call |
86 | |||||
87 | 445 | 1.48ms | return $format; | ||
88 | } | ||||
89 | |||||
90 | sub stringfactory { | ||||
91 | shift; # It's a class method, but we don't actually want the class | ||||
92 | my $args = UNIVERSAL::isa($_[0], "HASH") ? shift : { @_ }; | ||||
93 | return sub { stringf($_[0], $args) }; | ||||
94 | } | ||||
95 | |||||
96 | 1 | 3µs | 1; | ||
97 | __END__ | ||||
# spent 2µs within String::Format::CORE:qr which was called:
# once (2µs+0s) by Perl::Critic::Violation::BEGIN@21 at line 70 | |||||
# spent 662µs within String::Format::CORE:regcomp which was called 445 times, avg 1µs/call:
# 445 times (662µs+0s) by String::Format::stringf at line 85, avg 1µs/call | |||||
# spent 1.39ms within String::Format::CORE:subst which was called 445 times, avg 3µs/call:
# 445 times (1.39ms+0s) by String::Format::stringf at line 85, avg 3µs/call | |||||
# spent 758µs within String::Format::CORE:substcont which was called 890 times, avg 852ns/call:
# 890 times (758µs+0s) by String::Format::stringf at line 85, avg 852ns/call |