line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::Format; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
4
|
|
|
|
|
|
|
# Copyright (C) 2002,2009 darren chamberlain |
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
|
6
|
|
|
6
|
|
412124
|
use strict; |
|
6
|
|
|
|
|
60
|
|
|
6
|
|
|
|
|
238
|
|
22
|
6
|
|
|
6
|
|
43
|
use vars qw($VERSION @EXPORT); |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
442
|
|
23
|
6
|
|
|
6
|
|
46
|
use Exporter; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
309
|
|
24
|
6
|
|
|
6
|
|
43
|
use base qw(Exporter); |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
4479
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$VERSION = '1.17_50'; |
27
|
|
|
|
|
|
|
@EXPORT = qw(stringf); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _replace { |
30
|
31
|
|
|
31
|
|
191
|
my ($args, $orig, $alignment, $min_width, |
31
|
|
|
|
|
|
|
$max_width, $passme, $formchar) = @_; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# For unknown escapes, return the orignial |
34
|
31
|
100
|
|
|
|
118
|
return $orig unless defined $args->{$formchar}; |
35
|
|
|
|
|
|
|
|
36
|
27
|
50
|
|
|
|
72
|
$alignment = '+' unless defined $alignment; |
37
|
|
|
|
|
|
|
|
38
|
27
|
|
|
|
|
59
|
my $replacement = $args->{$formchar}; |
39
|
27
|
100
|
|
|
|
77
|
if (ref $replacement eq 'CODE') { |
40
|
|
|
|
|
|
|
# $passme gets passed to subrefs. |
41
|
3
|
|
100
|
|
|
15
|
$passme ||= ""; |
42
|
3
|
|
|
|
|
7
|
$passme =~ tr/{}//d; |
43
|
3
|
|
|
|
|
9
|
$replacement = $replacement->($passme); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
27
|
|
|
|
|
140
|
my $replength = length $replacement; |
47
|
27
|
|
66
|
|
|
126
|
$min_width ||= $replength; |
48
|
27
|
|
66
|
|
|
117
|
$max_width ||= $replength; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# length of replacement is between min and max |
51
|
27
|
50
|
33
|
|
|
67
|
if (($replength > $min_width) && ($replength < $max_width)) { |
52
|
0
|
|
|
|
|
0
|
return $replacement; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# length of replacement is longer than max; truncate |
56
|
27
|
100
|
|
|
|
63
|
if ($replength > $max_width) { |
57
|
1
|
|
|
|
|
13
|
return substr($replacement, 0, $max_width); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# length of replacement is less than min: pad |
61
|
26
|
50
|
|
|
|
77
|
if ($alignment eq '-') { |
62
|
|
|
|
|
|
|
# left align; pad in front |
63
|
0
|
|
|
|
|
0
|
return $replacement . " " x ($min_width - $replength); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# right align, pad at end |
67
|
26
|
|
|
|
|
193
|
return " " x ($min_width - $replength) . $replacement; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $regex = 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
|
|
|
|
|
|
|
sub stringf { |
79
|
19
|
|
50
|
19
|
1
|
7826
|
my $format = shift || return; |
80
|
19
|
100
|
|
|
|
100
|
my $args = UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ }; |
81
|
19
|
100
|
|
|
|
71
|
$args->{'n'} = "\n" unless exists $args->{'n'}; |
82
|
19
|
100
|
|
|
|
60
|
$args->{'t'} = "\t" unless exists $args->{'t'}; |
83
|
19
|
100
|
|
|
|
58
|
$args->{'%'} = "%" unless exists $args->{'%'}; |
84
|
|
|
|
|
|
|
|
85
|
19
|
|
|
|
|
189
|
$format =~ s/$regex/_replace($args, $1, $2, $3, $4, $5, $6)/ge; |
|
31
|
|
|
|
|
101
|
|
86
|
|
|
|
|
|
|
|
87
|
19
|
|
|
|
|
85
|
return $format; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub stringfactory { |
91
|
1
|
|
|
1
|
0
|
150
|
shift; # It's a class method, but we don't actually want the class |
92
|
1
|
50
|
|
|
|
7
|
my $args = UNIVERSAL::isa($_[0], "HASH") ? shift : { @_ }; |
93
|
1
|
|
|
1
|
|
6
|
return sub { stringf($_[0], $args) }; |
|
1
|
|
|
|
|
23
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
__END__ |