| 1 | package String::Format; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 4 | # Copyright (C) 2002,2009 darren chamberlain <darren@cpan.org> | ||||||||||||||||||||||||||||
| 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 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 6 | ▶ | use vars qw($VERSION @EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 6 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 6 | $VERSION = '1.18'; | |||||||||||||||||||||||||||
| 27 | 6 | @EXPORT = qw(stringf); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 31 | ▶ | sub _replace { | ||||||||||||||||||||||||||
|
Subroutine
_replace: called CC=11 SCAR=25.1
| |||||||||||||||||||||||||||||
| 30 | 31 | my ($args, $orig, $alignment, $min_width, | |||||||||||||||||||||||||||
| 31 | $max_width, $passme, $formchar) = @_; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # For unknown escapes, return the orignial | ||||||||||||||||||||||||||||
| 34 | 31 | ▶ | return $orig unless defined $args->{$formchar}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 27 | ▶ | $alignment = '+' unless defined $alignment; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 27 | my $replacement = $args->{$formchar}; | |||||||||||||||||||||||||||
| 39 | 27 | ▶ | if (ref $replacement eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | # $passme gets passed to subrefs. | ||||||||||||||||||||||||||||
| 41 | 3 | ▶ | $passme ||= ""; | ||||||||||||||||||||||||||
|
Condition100%2/2
$passme ||= ""
MC/DC100%1/1
$passme ||= ""
$passme
Truth table100%2/2
A
A= $passme
| |||||||||||||||||||||||||||||
| 42 | 3 | $passme =~ tr/{}//d; | |||||||||||||||||||||||||||
| 43 | 3 | $replacement = $replacement->($passme); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 27 | my $replength = length $replacement; | |||||||||||||||||||||||||||
| 47 | 27 | ▶ | $min_width ||= $replength; | ||||||||||||||||||||||||||
|
Condition66%2/3
$min_width ||= $replength
MC/DC0%0/2
$min_width ||= $replength
$min_width
$replength
Truth table66%2/3
A ||= B
A= $min_widthB= $replength
| |||||||||||||||||||||||||||||
| 48 | 27 | ▶ | $max_width ||= $replength; | ||||||||||||||||||||||||||
|
Condition66%2/3
$max_width ||= $replength
MC/DC0%0/2
$max_width ||= $replength
$max_width
$replength
Truth table66%2/3
A ||= B
A= $max_widthB= $replength
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # length of replacement is between min and max | ||||||||||||||||||||||||||||
| 51 | 27 | ▶ | if (($replength > $min_width) && ($replength < $max_width)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$replength > $min_width and $replength < $max_width
MC/DC0%0/2
$replength > $min_width and $replength < $max_width
$replength > $min_width
$replength < $max_width
Truth table33%1/3
A and B
A= $replength > $min_widthB= $replength < $max_width
| |||||||||||||||||||||||||||||
| 52 | 0 | return $replacement; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # length of replacement is longer than max; truncate | ||||||||||||||||||||||||||||
| 56 | 27 | ▶ | if ($replength > $max_width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 1 | return substr($replacement, 0, $max_width); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # length of replacement is less than min: pad | ||||||||||||||||||||||||||||
| 61 | 26 | ▶ | if ($alignment eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | # left align; pad in front | ||||||||||||||||||||||||||||
| 63 | 0 | return $replacement . " " x ($min_width - $replength); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # right align, pad at end | ||||||||||||||||||||||||||||
| 67 | 26 | return " " x ($min_width - $replength) . $replacement; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | 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 | 19 | ▶ | sub stringf { | ||||||||||||||||||||||||||
|
Subroutine
stringf: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 19 | ▶ | my $format = shift || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 80 | 19 | ▶ | my $args = UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 19 | ▶ | $args->{'n'} = "\n" unless exists $args->{'n'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 19 | ▶ | $args->{'t'} = "\t" unless exists $args->{'t'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 19 | ▶ | $args->{'%'} = "%" unless exists $args->{'%'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 19 | $format =~ s/$regex/_replace($args, $1, $2, $3, $4, $5, $6)/ge; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 19 | return $format; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub stringfactory { | ||||||||||||||||||||||||||
|
Subroutine
stringfactory: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 1 | shift; # It's a class method, but we don't actually want the class | |||||||||||||||||||||||||||
| 92 | 1 | ▶ | my $args = UNIVERSAL::isa($_[0], "HASH") ? shift : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | return sub { stringf($_[0], $args) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 6 | 1; | |||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||