| 1 | package String::Pad; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 4 | 2 | our $DATE = '2021-08-01'; # DATE | |||||||||||||||||||||||||||
| 5 | 2 | our $DIST = 'String-Pad'; # DIST | |||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.021'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 14 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 15 | pad | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | ▶ | sub pad { | ||||||||||||||||||||||||||
|
Subroutine
pad: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 10 | my ($text0, $width, $which, $padchar, $is_trunc) = @_; | |||||||||||||||||||||||||||
| 20 | 10 | ▶ | if ($which) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 3 | $which = substr($which, 0, 1); | |||||||||||||||||||||||||||
| 22 | } else { | ||||||||||||||||||||||||||||
| 23 | 7 | $which = "r"; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 10 | ▶ | $padchar //= " "; | ||||||||||||||||||||||||||
|
Condition100%2/2
$padchar //= " "
MC/DC100%1/1
$padchar //= " "
$padchar
Truth table100%2/2
A
A= $padchar
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 10 | ▶ | my $texts = ref $text0 eq 'ARRAY' ? [@$text0] : [$text0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 10 | ▶ | if (!defined($width) || $width < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $width or $width < 0
MC/DC50%1/2
not defined $width or $width < 0
not defined $width
$width < 0
Truth table66%2/3
not(A) or B
A= not defined $widthB= $width < 0
| |||||||||||||||||||||||||||||
| 30 | 1 | my $longest = 0; | |||||||||||||||||||||||||||
| 31 | 1 | for (@$texts) { | |||||||||||||||||||||||||||
| 32 | 3 | my $len = length($_); | |||||||||||||||||||||||||||
| 33 | 3 | ▶ | $longest = $len if $longest < $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 1 | $width = $longest; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 10 | for my $text (@$texts) { | |||||||||||||||||||||||||||
| 39 | 16 | my $w = length($text); | |||||||||||||||||||||||||||
| 40 | 16 | ▶ | if ($is_trunc && $w > $width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_trunc and $w > $width
MC/DC100%2/2
$is_trunc and $w > $width
$is_trunc
$w > $width
Truth table100%3/3
A and B
A= $is_truncB= $w > $width
| |||||||||||||||||||||||||||||
| 41 | 3 | $text = substr($text, 0, $width, 1); | |||||||||||||||||||||||||||
| 42 | 3 | $w = $width; | |||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||
| 44 | 13 | ▶ | if ($which eq 'l') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | no warnings; # negative repeat count | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | $text = ($padchar x ($width-$w)) . $text; | |||||||||||||||||||||||||||
| 47 | } elsif ($which eq 'c') { | ||||||||||||||||||||||||||||
| 48 | 2 | my $n = int(($width-$w)/2); | |||||||||||||||||||||||||||
| 49 | 2 | $text = ($padchar x $n) . $text . ($padchar x ($width-$w-$n)); | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 2 | ▶ | no warnings; # negative repeat count | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 10 | $text .= ($padchar x ($width-$w)); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } # for $text | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | ref $text0 eq 'ARRAY' ? $texts : $texts->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | 1; | |||||||||||||||||||||||||||
| 61 | # ABSTRACT: String padding routines | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | __END__ | ||||||||||||||||||||||||||||