blib/lib/No/Worries/String.pm

filter: Statementstmt 35.0% 47 / 134 Branchbran 20.0% 12 / 60 Conditioncond 23.0% 3 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 53.3% 8 / 15 Podpod 100% 6 / 6 totaltotal 33.0% 78 / 236 CC 39 SCAR 48.4
CC
39
Cov
30%
string_table
342.0
_strpad
42.0
string_bytefmt
30.0
CRAP 126.4
↑ Summary blib/lib/No/Worries/Die.pm »
1 #+##############################################################################
2 # #
3 # File: No/Worries/String.pm #
4 # #
5 # Description: string handling without worries #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package No::Worries::String;
14 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 our $VERSION = "1.7";
17 21 our $REVISION = sprintf("%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 21 ▶use No::Worries::Export qw(export_control);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 21 ▶use Params::Validate qw(validate validate_pos :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #
27 # global variables
28 #
29  
30 our(
31 21 @_ByteSuffix, # byte suffixes used by bytefmt
32 @_Map, # mapping of characters to escaped strings
33 %_Plural, # pluralization cache
34 );
35  
36 #
37 # format a number of bytes
38 #
39  
40 0 ▶sub string_bytefmt ($;$) {
Subroutine string_bytefmt: not called CC=5 SCAR=34.0
Pod documented
41 0 my($number, $precision) = @_;
42 0 my($index);
43  
44 0 ▶ $precision = 2 unless defined($precision);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $precision 0 0 0
45 0 $index = 0;
46 0 ▶ while ($_ByteSuffix[$index] and $number > 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if ($No::Worries::String::_ByteSuffix[$index] and $number > 1024) 0 0 0
Condition0%0/3
$No::Worries::String::_ByteSuffix[$index] and $number > 1024
!ll&&!rl&&r
000
MC/DC0%0/2
$No::Worries::String::_ByteSuffix[$index] and $number > 1024
$No::Worries::String::_ByteSuffix[$index] $number > 1024
Truth table0%0/3
A and B
A= $No::Worries::String::_ByteSuffix[$index]B= $number > 1024
ABresult
0X0
100
111
47 0 $index++;
48 0 $number /= 1024.0;
49 }
50 0 ▶ return("$number $_ByteSuffix[$index]") if $number =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $number =~ /^\d+$/ 0 0 0
51 0 return(sprintf("%.${precision}f %s", $number, $_ByteSuffix[$index]));
52 }
53  
54 #
55 # escape a string (quite compact, human friendly but not Perl eval()'able)
56 #
57  
58 6 ▶sub string_escape ($) {
Subroutine string_escape: called CC=3 SCAR=11.0
Pod documented
59 6 my($string) = @_;
60 6 my(@list);
61  
62 6 validate_pos(@_, { type => SCALAR });
63 6 foreach my $ord (map(ord($_), split(//, $string))) {
64 23 ▶ push(@list, $ord < 256 ? $_Map[$ord] : sprintf("\\x{%04x}", $ord));
Branch: 1 branch
BranchTrueFalseTotal
$ord < 256 ? : 22 1 23
65 }
66 6 return(join("", @list));
67 }
68  
69 #
70 # return the plural form of the given noun
71 #
72  
73 3 ▶sub string_plural ($) {
Subroutine string_plural: called CC=7 SCAR=22.4
Pod documented
74 3 my($noun) = @_;
75  
76 3 ▶ unless ($_Plural{$noun}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($No::Worries::String::_Plural{$noun}) 2 1 3
77 2 ▶ if ($noun =~ /(ch|s|sh|x|z)$/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($noun =~ /(ch|s|sh|x|z)$/) { } 0 2 2
elsif ($noun =~ /[bcdfghjklmnpqrstvwxz]y$/) { } 1 1 2
elsif ($noun =~ /f$/) { } 0 1 1
elsif ($noun =~ /fe$/) { } 0 1 1
elsif ($noun =~ /[bcdfghjklmnpqrstvwxz]o$/) { } 0 1 1
78 0 $_Plural{$noun} = $noun . "es";
79 } elsif ($noun =~ /[bcdfghjklmnpqrstvwxz]y$/) {
80 1 $_Plural{$noun} = substr($noun, 0, -1) . "ies";
81 } elsif ($noun =~ /f$/) {
82 0 $_Plural{$noun} = substr($noun, 0, -1) . "ves";
83 } elsif ($noun =~ /fe$/) {
84 0 $_Plural{$noun} = substr($noun, 0, -2) . "ves";
85 } elsif ($noun =~ /[bcdfghjklmnpqrstvwxz]o$/) {
86 0 $_Plural{$noun} = $noun . "es";
87 } else {
88 1 $_Plural{$noun} = $noun . "s";
89 }
90 }
91 3 return($_Plural{$noun});
92 }
93  
94 #
95 # quantify the given (count, noun) pair
96 #
97  
98 0 ▶sub string_quantify ($$) {
Subroutine string_quantify: not called CC=2 SCAR=17.9
Pod documented
99 0 my($count, $noun) = @_;
100  
101 0 ▶ return($count . " " . ($count == 1 ? $noun : string_plural($noun)));
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 0 0 0
102 }
103  
104 #
105 # return the real length of a string (removing ANSI Escape sequences)
106 #
107  
108 0 ▶sub _strlen ($) {
Subroutine _strlen: not called CC=2 SCAR=17.9
109 0 my($string) = @_;
110  
111 0 ▶ return(0) unless defined($string);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
112 0 $string =~ s/\x1b\[[0-9;]*[mGKH]//g;
113 0 return(length($string));
114 }
115  
116 #
117 # return an aligned and padded string
118 #
119  
120 0 ▶sub _strpad ($$$) {
Subroutine _strpad: not called CC=6 SCAR=37.4
121 0 my($string, $length, $align) = @_;
122 0 my($strlen, $before, $after);
123  
124 0 ▶ $string = "" unless defined($string);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
125 0 $strlen = _strlen($string);
126 0 ▶ $align ||= "left";
Condition0%0/2
$align ||= "left"
l!l
00
MC/DC0%0/1
$align ||= "left"
$align
Truth table0%0/2
A
A= $align
Aresult
11
00
127 0 ▶ if ($align eq "left") {
Branch: 3 branches
BranchTrueFalseTotal
if ($align eq 'left') { } 0 0 0
elsif ($align eq 'right') { } 0 0 0
elsif ($align eq 'center') { } 0 0 0
128 0 $before = 0;
129 0 $after = $length - $strlen;
130 } elsif ($align eq "right") {
131 0 $before = $length - $strlen;
132 0 $after = 0;
133 } elsif ($align eq "center") {
134 0 $before = ($length - $strlen) >> 1;
135 0 $after = $length - $strlen - $before;
136 } else {
137 0 die("unexpected alignment: $align\n");
138 }
139 0 return((" " x $before) . $string . (" " x $after));
140 }
141  
142 #
143 # return a string generated from a repeated pattern
144 #
145  
146 0 ▶sub _strgen ($$) {
Subroutine _strgen: not called CC=1 SCAR=6.9
147 0 my($pattern, $length) = @_;
148  
149 0 return(substr($pattern x $length, 0, $length));
150 }
151  
152 #
153 # return a formatted table line
154 #
155  
156 0 ▶sub _tblfmt ($$) {
Subroutine _tblfmt: not called CC=3 SCAR=24.8
157 0 my($column, $option) = @_;
158 0 my($line, $index);
159  
160 0 $line = $option->{indent};
161 0 $line .= $option->{lsep};
162 0 $index = 0;
163 0 ▶ while ($index < @{ $option->{collen} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @{$option->{"collen"};}) 0 0 0
164 0 ▶ $line .= $option->{colsep} if $index;
Branch: 1 branch
BranchTrueFalseTotal
if $index 0 0 0
165 $line .= _strpad($column->[$index],
166 $option->{collen}[$index],
167 0 $option->{align}[$index]);
168 0 $index++;
169 }
170 0 $line .= $option->{rsep};
171 0 $line .= "\n";
172 0 return($line);
173 }
174  
175 #
176 # transform a table into a string
177 #
178  
179 21 my %string_table_options = (
180 align => { optional => 1, type => ARRAYREF },
181 colsep => { optional => 1, type => SCALAR },
182 header => { optional => 1, type => ARRAYREF },
183 headsep => { optional => 1, type => SCALAR },
184 indent => { optional => 1, type => SCALAR },
185 markdown => { optional => 1, type => BOOLEAN },
186 );
187  
188 0 ▶sub string_table ($@) {
Subroutine string_table: not called CC=18 SCAR=58.3
Pod documented
189 0 my($lines, %option, @collen, @headsep, $index, $length, $result);
190  
191 # handle options
192 0 $lines = shift(@_);
193 0 ▶ %option = validate(@_, \%string_table_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
194 0 ▶ $option{align} ||= [];
Condition0%0/2
$option{"align"} ||= []
l!l
00
MC/DC0%0/1
$option{"align"} ||= []
$option{"align"}
Truth table0%0/2
A
A= $option{"align"}
Aresult
11
00
195 $option{colsep} = " | "
196 0 ▶ unless defined($option{colsep});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option{"colsep"} 0 0 0
197 $option{headsep} = $option{markdown} ? "-" : "="
198 0 ▶ unless defined($option{headsep});
Branch: 2 branches
BranchTrueFalseTotal
unless defined $option{"headsep"} 0 0 0
$option{'markdown'} ? : 0 0 0
199 $option{indent} = ""
200 0 ▶ unless defined($option{indent});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option{"indent"} 0 0 0
201 0 ▶ if ($option{markdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{'markdown'}) { } 0 0 0
202 0 $option{lsep} = $option{rsep} = $option{colsep};
203 0 $option{lsep} =~ s/^\s+//;
204 0 $option{rsep} =~ s/\s+$//;
205 } else {
206 0 $option{lsep} = "";
207 0 $option{rsep} = "";
208 }
209 # compute column lengths
210 0 ▶ foreach my $line ($option{header} ? ($option{header}) : (), @{ $lines }) {
Branch: 1 branch
BranchTrueFalseTotal
$option{'header'} ? : 0 0 0
211 0 $index = 0;
212 0 foreach my $entry (@{ $line }) {
213 0 $length = _strlen($entry);
214 0 ▶ $collen[$index] = $length
Branch: 1 branch
BranchTrueFalseTotal
unless defined $collen[$index] and $collen[$index] >= $length 0 0 0
Condition0%0/3
defined $collen[$index] and $collen[$index] >= $length
!ll&&!rl&&r
000
MC/DC0%0/2
defined $collen[$index] and $collen[$index] >= $length
defined $collen[$index] $collen[$index] >= $length
Truth table0%0/3
A and B
A= defined $collen[$index]B= $collen[$index] >= $length
ABresult
0X0
100
111
215 unless defined($collen[$index]) and $collen[$index] >= $length;
216 0 $index++;
217 }
218 }
219 # compute total length
220 0 $length = length($option{lsep}) + length($option{rsep});
221 0 $length += length($option{colsep}) * (@collen - 1);
222 0 foreach my $collen (@collen) {
223 0 $length += $collen;
224 }
225 0 $option{collen} = \@collen;
226 0 $result = "";
227 # format header
228 0 ▶ if ($option{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"header"}) 0 0 0
229 0 $result .= _tblfmt($option{header}, \%option);
230 0 ▶ if (length($option{headsep})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $option{"headsep"}) 0 0 0
231 0 ▶ if ($option{markdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{'markdown'}) { } 0 0 0
232 0 @headsep = map(_strgen($option{headsep}, $_), @collen);
233 0 $result .= _tblfmt(\@headsep, \%option);
234 } else {
235 0 $result .= $option{indent};
236 0 $result .= _strgen($option{headsep}, $length) . "\n";
237 }
238 }
239 }
240 # format lines
241 0 foreach my $line (@{ $lines }) {
242 0 $result .= _tblfmt($line, \%option);
243 }
244 0 return($result);
245 }
246  
247 #
248 # remove leading and trailing spaces
249 #
250  
251 37 ▶sub string_trim ($) {
Subroutine string_trim: called CC=1 SCAR=0.0
Pod documented
252 37 my($string) = @_;
253  
254 37 validate_pos(@_, { type => SCALAR });
255 37 $string =~ s/^\s+//;
256 37 $string =~ s/\s+$//;
257 37 return($string);
258 }
259  
260 #
261 # module initialization
262 #
263  
264 21 @_ByteSuffix = qw(B kB MB GB TB PB EB ZB YB);
265 21 foreach my $ord (0 .. 255) {
266 5376 ▶ $_Map[$ord] = 32 <= $ord && $ord < 127 ?
Branch: 1 branch
BranchTrueFalseTotal
32 <= $ord && $ord < 127 ? : 1995 3381 5376
Condition100%3/3
32 <= $ord and $ord < 127
!ll&&!rl&&r
67227091995
MC/DC100%2/2
32 <= $ord and $ord < 127
32 <= $ord $ord < 127
Truth table100%3/3
A and B
A= 32 <= $ordB= $ord < 127
ABresult
0X0
100
111
267 chr($ord) : sprintf("\\x%02x", $ord);
268 }
269 21 $_Map[ord("\t")] = "\\t";
270 21 $_Map[ord("\n")] = "\\n";
271 21 $_Map[ord("\r")] = "\\r";
272 21 $_Map[ord("\e")] = "\\e";
273 21 $_Map[ord("\\")] = "\\\\";
274 21 %_Plural = (
275 "child" => "children",
276 "data" => "data",
277 "foot" => "feet",
278 "index" => "indices",
279 "man" => "men",
280 "tooth" => "teeth",
281 "woman" => "women",
282 );
283  
284 #
285 # export control
286 #
287  
288 24 ▶sub import : method {
Subroutine import: called CC=1 SCAR=0.0
289 24 my($pkg, %exported);
290  
291 24 $pkg = shift(@_);
292 24 grep($exported{$_}++, map("string_$_",
293 qw(bytefmt escape plural quantify table trim)));
294 24 export_control(scalar(caller()), $pkg, \%exported, @_);
295 }
296  
297 21 1;
298  
299 __DATA__
↑ Summary blib/lib/No/Worries/Die.pm »