line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::Pad; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2021-08-01'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'String-Pad'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.021'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
195702
|
use 5.010001; |
|
1
|
|
|
|
|
8
|
|
9
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
10
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
5
|
use Exporter; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
202
|
|
13
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
15
|
|
|
|
|
|
|
pad |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub pad { |
19
|
10
|
|
|
10
|
1
|
7485
|
my ($text0, $width, $which, $padchar, $is_trunc) = @_; |
20
|
10
|
100
|
|
|
|
23
|
if ($which) { |
21
|
3
|
|
|
|
|
7
|
$which = substr($which, 0, 1); |
22
|
|
|
|
|
|
|
} else { |
23
|
7
|
|
|
|
|
11
|
$which = "r"; |
24
|
|
|
|
|
|
|
} |
25
|
10
|
|
100
|
|
|
44
|
$padchar //= " "; |
26
|
|
|
|
|
|
|
|
27
|
10
|
100
|
|
|
|
31
|
my $texts = ref $text0 eq 'ARRAY' ? [@$text0] : [$text0]; |
28
|
|
|
|
|
|
|
|
29
|
10
|
100
|
66
|
|
|
40
|
if (!defined($width) || $width < 0) { |
30
|
1
|
|
|
|
|
2
|
my $longest = 0; |
31
|
1
|
|
|
|
|
3
|
for (@$texts) { |
32
|
3
|
|
|
|
|
4
|
my $len = length($_); |
33
|
3
|
100
|
|
|
|
8
|
$longest = $len if $longest < $len; |
34
|
|
|
|
|
|
|
} |
35
|
1
|
|
|
|
|
2
|
$width = $longest; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
10
|
|
|
|
|
19
|
for my $text (@$texts) { |
39
|
16
|
|
|
|
|
24
|
my $w = length($text); |
40
|
16
|
100
|
100
|
|
|
37
|
if ($is_trunc && $w > $width) { |
41
|
3
|
|
|
|
|
8
|
$text = substr($text, 0, $width, 1); |
42
|
3
|
|
|
|
|
6
|
$w = $width; |
43
|
|
|
|
|
|
|
} else { |
44
|
13
|
100
|
|
|
|
32
|
if ($which eq 'l') { |
|
|
100
|
|
|
|
|
|
45
|
1
|
|
|
1
|
|
8
|
no warnings; # negative repeat count |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
84
|
|
46
|
1
|
|
|
|
|
5
|
$text = ($padchar x ($width-$w)) . $text; |
47
|
|
|
|
|
|
|
} elsif ($which eq 'c') { |
48
|
2
|
|
|
|
|
8
|
my $n = int(($width-$w)/2); |
49
|
2
|
|
|
|
|
7
|
$text = ($padchar x $n) . $text . ($padchar x ($width-$w-$n)); |
50
|
|
|
|
|
|
|
} else { |
51
|
1
|
|
|
1
|
|
8
|
no warnings; # negative repeat count |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
103
|
|
52
|
10
|
|
|
|
|
26
|
$text .= ($padchar x ($width-$w)); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} # for $text |
56
|
|
|
|
|
|
|
|
57
|
10
|
100
|
|
|
|
79
|
ref $text0 eq 'ARRAY' ? $texts : $texts->[0]; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
# ABSTRACT: String padding routines |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
__END__ |