line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::sprintfn; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
17934
|
use 5.010001; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
545
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
our @EXPORT = qw(sprintfn printfn); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.07'; # VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $distance = 10; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $re1 = qr/[^)]+/s; |
16
|
|
|
|
|
|
|
my $re2 = qr{(? |
17
|
|
|
|
|
|
|
% |
18
|
|
|
|
|
|
|
(? \d+\$ | \((?$re1)\)\$?)? |
19
|
|
|
|
|
|
|
(? [ +0#-]+)? |
20
|
|
|
|
|
|
|
(? \*?[v])? |
21
|
|
|
|
|
|
|
(? -?\d+ | |
22
|
|
|
|
|
|
|
\*\d+\$? | |
23
|
|
|
|
|
|
|
\((?$re1)\))? |
24
|
|
|
|
|
|
|
(?\.?) |
25
|
|
|
|
|
|
|
(? |
26
|
|
|
|
|
|
|
(?: \d+ | \* | |
27
|
|
|
|
|
|
|
\((?$re1)\) ) ) ? |
28
|
|
|
|
|
|
|
(? [%csduoxefgXEGbBpniDUOF]) |
29
|
|
|
|
|
|
|
)}x; |
30
|
|
|
|
|
|
|
our $regex = qr{($re2|%|[^%]+)}s; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# faster version, without using named capture |
33
|
|
|
|
|
|
|
if (1) { |
34
|
|
|
|
|
|
|
$regex = qr{( #all=1 |
35
|
|
|
|
|
|
|
( #fmt=2 |
36
|
|
|
|
|
|
|
% |
37
|
|
|
|
|
|
|
(#pi=3 |
38
|
|
|
|
|
|
|
\d+\$ | \( |
39
|
|
|
|
|
|
|
(#npi=4 |
40
|
|
|
|
|
|
|
[^)]+)\)\$?)? |
41
|
|
|
|
|
|
|
(#flags=5 |
42
|
|
|
|
|
|
|
[ +0#-]+)? |
43
|
|
|
|
|
|
|
(#vflag=6 |
44
|
|
|
|
|
|
|
\*?[v])? |
45
|
|
|
|
|
|
|
(#width=7 |
46
|
|
|
|
|
|
|
-?\d+ | |
47
|
|
|
|
|
|
|
\*\d+\$? | |
48
|
|
|
|
|
|
|
\((#nwidth=8 |
49
|
|
|
|
|
|
|
[^)]+)\))? |
50
|
|
|
|
|
|
|
(#dot=9 |
51
|
|
|
|
|
|
|
\.?) |
52
|
|
|
|
|
|
|
(#prec=10 |
53
|
|
|
|
|
|
|
(?: \d+ | \* | |
54
|
|
|
|
|
|
|
\((#nprec=11 |
55
|
|
|
|
|
|
|
[^)]+)\) ) ) ? |
56
|
|
|
|
|
|
|
(#conv=12 |
57
|
|
|
|
|
|
|
[%csduoxefgXEGbBpniDUOF]) |
58
|
|
|
|
|
|
|
) | % | [^%]+ |
59
|
|
|
|
|
|
|
)}xs; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub sprintfn { |
63
|
31
|
|
|
31
|
1
|
14995
|
my ($format, @args) = @_; |
64
|
|
|
|
|
|
|
|
65
|
31
|
|
|
|
|
38
|
my $hash; |
66
|
31
|
100
|
|
|
|
92
|
if (ref($args[0]) eq 'HASH') { |
67
|
14
|
|
|
|
|
18
|
$hash = shift(@args); |
68
|
|
|
|
|
|
|
} |
69
|
31
|
100
|
|
|
|
297
|
return sprintf($format, @args) if !$hash; |
70
|
|
|
|
|
|
|
|
71
|
14
|
|
|
|
|
12
|
my %indexes; # key = $hash key, value = index for @args |
72
|
14
|
|
|
|
|
30
|
push @args, (undef) x $distance; |
73
|
|
|
|
|
|
|
|
74
|
14
|
|
|
|
|
98
|
$format =~ s{$regex}{ |
75
|
56
|
|
|
|
|
181
|
my ($all, $fmt, $pi, $npi, $flags, |
76
|
|
|
|
|
|
|
$vflag, $width, $nwidth, $dot, $prec, |
77
|
|
|
|
|
|
|
$nprec, $conv) = |
78
|
|
|
|
|
|
|
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12); |
79
|
|
|
|
|
|
|
|
80
|
56
|
|
|
|
|
47
|
my $res; |
81
|
56
|
100
|
|
|
|
69
|
if ($fmt) { |
82
|
|
|
|
|
|
|
|
83
|
21
|
100
|
|
|
|
41
|
if (defined $npi) { |
84
|
14
|
|
|
|
|
15
|
my $i = $indexes{$npi}; |
85
|
14
|
100
|
|
|
|
21
|
if (!$i) { |
86
|
13
|
|
|
|
|
14
|
$i = @args + 1; |
87
|
13
|
|
|
|
|
18
|
push @args, $hash->{$npi}; |
88
|
13
|
|
|
|
|
23
|
$indexes{$npi} = $i; |
89
|
|
|
|
|
|
|
} |
90
|
14
|
|
|
|
|
17
|
$pi = "${i}\$"; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
21
|
100
|
|
|
|
32
|
if (defined $nwidth) { |
94
|
5
|
|
|
|
|
7
|
$width = $hash->{$nwidth}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
21
|
100
|
|
|
|
31
|
if (defined $nprec) { |
98
|
5
|
|
|
|
|
7
|
$prec = $hash->{$nprec}; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
168
|
|
|
|
|
201
|
$res = join("", |
102
|
21
|
|
|
|
|
26
|
grep {defined} ( |
103
|
|
|
|
|
|
|
"%", |
104
|
|
|
|
|
|
|
$pi, $flags, $vflag, |
105
|
|
|
|
|
|
|
$width, $dot, $prec, $conv) |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
} else { |
108
|
35
|
|
|
|
|
39
|
my $i = @args + 1; |
109
|
35
|
|
|
|
|
45
|
push @args, $all; |
110
|
35
|
|
|
|
|
50
|
$res = "\%${i}\$s"; |
111
|
|
|
|
|
|
|
} |
112
|
56
|
|
|
|
|
222
|
$res; |
113
|
|
|
|
|
|
|
}xego; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# DEBUG |
116
|
|
|
|
|
|
|
#use Data::Dump; dd [$format, @args]; |
117
|
|
|
|
|
|
|
|
118
|
14
|
|
|
|
|
152
|
sprintf $format, @args; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub printfn { |
122
|
0
|
|
|
0
|
1
|
|
print sprintfn @_; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |
126
|
|
|
|
|
|
|
# ABSTRACT: Drop-in replacement for sprintf(), with named parameter support |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__END__ |