line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Sprintf::Length; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2018-03-17'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.001'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
48934
|
use 5.010001; |
|
1
|
|
|
|
|
9
|
|
7
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
15
|
|
8
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
29
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
13
|
use Exporter 'import'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
336
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(sprintf_length); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# XXX BEGIN COPIED FROM Text::sprintfn |
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
|
|
|
|
|
|
|
# XXX END COPIED FROM Text::sprintfn |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub sprintf_length { |
65
|
3
|
|
|
3
|
1
|
74
|
my $format = shift; |
66
|
|
|
|
|
|
|
|
67
|
3
|
|
|
|
|
6
|
my $sprintf_width = length($format); |
68
|
|
|
|
|
|
|
|
69
|
3
|
|
|
|
|
24
|
while ($format =~ /$regex/g) { |
70
|
7
|
|
|
|
|
28
|
my ($all, $fmt, $pi, $npi, $flags, |
71
|
|
|
|
|
|
|
$vflag, $width, $nwidth, $dot, $prec, |
72
|
|
|
|
|
|
|
$nprec, $conv) = |
73
|
|
|
|
|
|
|
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12); |
74
|
|
|
|
|
|
|
|
75
|
7
|
100
|
66
|
|
|
27
|
if ($fmt && defined $sprintf_width) { |
76
|
|
|
|
|
|
|
|
77
|
5
|
100
|
100
|
|
|
22
|
if ($conv eq '%' || $conv eq 'c') { |
|
|
50
|
33
|
|
|
|
|
|
|
50
|
|
|
|
|
|
78
|
2
|
|
50
|
|
|
7
|
$width //= 1; |
79
|
|
|
|
|
|
|
} elsif ($conv eq 'p' || $conv eq 'c') { |
80
|
0
|
|
0
|
|
|
0
|
$width //= 1; |
81
|
|
|
|
|
|
|
} elsif ($conv eq 'n') { |
82
|
0
|
|
|
|
|
0
|
$width = 0; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
5
|
100
|
|
|
|
8
|
if (defined $width) { |
86
|
4
|
|
|
|
|
17
|
$sprintf_width += $width - length($all); |
87
|
|
|
|
|
|
|
} else { |
88
|
1
|
|
|
|
|
4
|
$sprintf_width = undef; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
3
|
|
|
|
|
12
|
$sprintf_width; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
# ABSTRACT: Calculate length of sprintf()-formatted string |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__END__ |