blib/lib/Lisp/Fmt.pm

filter: Statementstmt 83.2% 532 / 639 Branchbran 71.7% 373 / 520 Conditioncond 69.6% 62 / 89 MC/DCmcdc 46.4% 26 / 56 Subroutinesub 88.4% 23 / 26 Podpod 0.0% 0 / 22 totaltotal 75.1% 1016 / 1352 CC 295 SCAR 51.0
CC
295
Cov
77%
run
341.9
parser
38.4
reduce
35.0
CRAP 163.4
↑ Summary
1 # -*- perl -*-
2  
3 # Copyright (c) 1998 by Jeff Weisberg
4 # Author: Jeff Weisberg <jaw @ tcp4me.com>
5 # Function: Lisp format
6 #
7 # $Id: Fmt.pm,v 1.1 1998/08/31 23:50:12 jaw Exp jaw $
8 #
9 # LICENSE: at end
10 # DOCUMENTATION: at end
11 #
12  
13 package Lisp::Fmt;
14 1 require Exporter;
15 1 @ISA = qw(Exporter);
16 1 @EXPORT = qw(fmt pfmt);
17 1 $VERSION = "0.01";
18  
19 1 ▶use vars qw($accum $argno @arglist $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 ### config options
23  
24 # lisp's notion of false differs from that of perl
25 # you may or may not want 0 be considered false...
26 # this effects ~:[ and ~@[
27 1 my($config_zero_is_false) = 0;
28  
29 # X3J13 is ambiguous on how to align the middle columns of <>
30 # set this to 'r', 'l', or 'c'
31 1 my($config_align_middle) = 'l';
32  
33 # turn on verbose debugging output, most run 0 through 2
34 1 my($verbose_tok) = 0;
35 1 my($verbose_parse) = 0;
36 1 my($verbose_reduce) = 0;
37 1 my($verbose_run) = 0;
38 1 my($verbose_fmt) = 0;
39  
40 ### end of config options
41  
42 ################################################################
43 ### No user servicable parts below this point
44 ################################################################
45  
46 ### tokenize the format spec
47 213 ▶sub tok {
Subroutine tok: called CC=18 SCAR=29.4
Pod undocumented
48 213 my( $s ) = @_;
49 213 my( $fulls ) = $s;
50 213 my( @s );
51 my( @t );
52 213 my( $len ) = length($s);
53 213 my( $soff, $eoff );
54  
55 213 ▶ while( $s ){
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 896 213 1109
56 896 ▶ print STDERR "T: $s\n" if $verbose_tok;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_tok 0 896 896
57
58 896 ▶ if( $s =~ /^([^~]+)/ && $1 ){
Branch: 3 branches
BranchTrueFalseTotal
if ($s =~ /^([^~]+)/ and $1) { } 292 604 896
elsif ($s =~ /^~(<{2,}|>{2,}|\|{2,})/ and $1) { } 6 598 604
elsif ($s =~ /^~(((-?\d*|v|\#|\'.)(,(-?\d*|v|\#|\'.))*)? # comma sep. list of params ([\@:!]*) # optional @ ! and : ([^=\@:!\'v\#]|\n\s*|=.))/x and $1) { } 598 0 598
Condition66%2/3
$s =~ /^([^~]+)/ and $1
!ll&&!rl&&r
6040292
MC/DC50%1/2
$s =~ /^([^~]+)/ and $1
$s =~ /^([^~]+)/ $1
Truth table66%2/3
A and B
A= $s =~ /^([^~]+)/B= $1
ABresult
0X0
100
111
59 292 ▶ print STDERR "T-> $1 <$'>\n" if $verbose_tok > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_tok > 1 0 292 292
60 292 $soff = $len - length($s) - 1;
61 292 $s = $';
62 292 $eoff = $len - length($s) - 1;
63
64 # add literal
65 292 push @t, {
66 directive => 'literal',
67 text => $1,
68 fullfmt => $fulls,
69 fmtoffset => $eoff,
70 fmtstart => $soff,
71 };
72 }
73  
74 ▶ elsif( $s =~ /^~(<{2,}|>{2,}|\|{2,})/ && $1 ){
Condition66%2/3
$s =~ /^~(<{2,}|>{2,}|\|{2,})/ and $1
!ll&&!rl&&r
59806
MC/DC50%1/2
$s =~ /^~(<{2,}|>{2,}|\|{2,})/ and $1
$s =~ /^~(<{2,}|>{2,}|\|{2,})/ $1
Truth table66%2/3
A and B
A= $s =~ /^~(<{2,}|>{2,}|\|{2,})/B= $1
ABresult
0X0
100
111
75 # perl-esque format spec ~<<<<<< ~>>>>>>> ~||||||
76 # NB: must be at least 3 long (eg ~<<) to avoid confusion with ~|, ~< and ~>
77 6 my( $p ) = $1;
78 6 ▶ print STDERR "T-> $1 {$'}\n" if $verbose_tok > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_tok > 1 0 6 6
79 6 $soff = $len - length($s) - 1;
80 6 $s = $';
81 6 $eoff = $len - length($s) - 1;
82  
83 6 ▶ push @t, {
Branch: 2 branches
BranchTrueFalseTotal
$p =~ /</ ? : 2 4 6
$p =~ />/ ? : 2 2 4
84 directive => 'A',
85 numbers => [ length($1)+1 ],
86 gravity => ( $p =~ /</ ) ? 'l' : (( $p =~ />/ ) ? 'r' : 'c'),
87 fullfmt => $fulls,
88 fmtoffset => $eoff,
89 fmtstart => $soff,
90 };
91 }
92  
93 ▶ elsif( $s =~ /^~(((-?\d*|v|\#|\'.)(,(-?\d*|v|\#|\'.))*)? # comma sep. list of params
Condition33%1/3
$s =~ /^~(((-?\d*|v|\#|\'.)(,(-?\d*|v|\#|\'.))*)? # comma sep. list of params ([\@:!]*) # optional @ ! and : ([^=\@:!\'v\#]|\n\s*|=.))/x and $1
!ll&&!rl&&r
00598
MC/DC0%0/2
$s =~ /^~(((-?\d*|v|\#|\'.)(,(-?\d*|v|\#|\'.))*)? # comma sep. list of params ([\@:!]*) # optional @ ! and : ([^=\@:!\'v\#]|\n\s*|=.))/x and $1
$s =~ /^~(((-?\d*|v|\#|\'.)(,(-?\d*|v|\#|\'.))*)? # comma sep. list of params ([\@:!]*) # optional @ ! and : ([^=\@:!\'v\#]|\n\s*|=.))/x $1
Truth table33%1/3
A and B
A= $s =~ /^~(((-?\d*|v|\#|\'.)(,(-?\d*|v|\#|\'.))*)? # comma sep. list of params ([\@:!]*) # optional @ ! and : ([^=\@:!\'v\#]|\n\s*|=.))/xB= $1
ABresult
0X0
100
111
94 ([\@:!]*) # optional @ ! and :
95 ([^=\@:!\'v\#]|\n\s*|=.))/x # directive
96 && $1 ){
97 598 my($ns, $sp, $f) = ($2, $6, $7);
98 598 ▶ print STDERR "T-> $f; <$ns>; $sp <$'>\n" if $verbose_tok > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_tok > 1 0 598 598
99 598 $soff = $len - length($s) - 1;
100 598 $s = $';
101 598 $eoff = $len - length($s) - 1;
102 598 my(@n) = split ',', $ns;
103 598 foreach (@n){
104 244 ▶ if( /^\'(.*)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (/^\'(.*)$/) 27 217 244
105 27 $_ = $1;
106 }
107 }
108  
109 # add it
110 598 ▶ push @t, {
Branch: 2 branches
BranchTrueFalseTotal
$sp =~ /@|!/ ? : 68 530 598
$sp =~ /:/ ? : 66 532 598
111 numbers => [@n],
112 directive => uc($f),
113 atsign => ($sp =~ /@|!/) ? 1 : 0, # because we have to \@, we allow ! as a synonym
114 colon => ($sp =~ /:/) ? 1 : 0,
115 fullfmt => $fulls,
116 fmtoffset => $eoff,
117 fmtstart => $soff,
118 };
119 }
120 }
121  
122 213 @t;
123 }
124  
125  
126 ### parse the tokenized format string
127 213 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
128 213 my( @t ) = @_;
129 213 my( $t );
130 213 my( $i ) = 0;
131 213 my( $tnext );
132
133 1109 ▶ $tnext = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
134 1109 ▶ return undef if( $i >= @t );
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @t 213 896 1109
135 896 return $t[ $i ++ ];
136 213 };
137  
138 213 parser('', 1, $tnext);
139 }
140  
141 293 ▶sub parser {
Subroutine parser: called CC=30 SCAR=36.5
Pod undocumented
142 293 my( $term, $n, $tnext ) = @_;
143 293 my( $t, @tt );
144
145 293 while( 1 ){
146 1109 $t = &$tnext();
147
148 1109 ▶ if( $term && ! $t ){
Branch: 1 branch
BranchTrueFalseTotal
if ($term and not $t) 0 1109 1109
Condition66%2/3
$term and not $t
!ll&&!rl&&r
6754340
MC/DC0%0/2
$term and not $t
$term not $t
Truth table66%2/3
A and not(B)
A= $termB= not $t
ABresult
0X0
100
111
149 # error ESARAHCONNOR - no terminator
150 0 $t = $tt[0];
151 0 formaterror("I see no matching $term here", $t);
152 0 return ;
153 }
154
155 1109 ▶ return \@tt unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 213 896 1109
156  
157 896 ▶ print STDERR "PP", "<"x$n, ": $t->{directive}\n" if $verbose_parse;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse 0 896 896
158
159 896 ▶ return \@tt if( $t->{'directive'} eq $term );
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"directive"} eq $term 80 816 896
160
161 816 ▶ if( $t->{'directive'} eq '[' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"directive"} eq "[") 37 779 816
162 37 ▶ print STDERR "PP->[\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 37 37
163 37 my( $l ) = parser(']', $n+1, $tnext);
164 37 my( @l ) = @{$l};
165 37 my( @nn, @ll );
166 37 my( $n ) = 0;
167  
168 37 ▶ while( @l ){
Branch: 1 branch
BranchTrueFalseTotal
if (@l) 203 37 240
169 203 $l = shift @l;
170 203 ▶ if( $l->{'directive'} eq ";" ){
Branch: 1 branch
BranchTrueFalseTotal
if ($l->{'directive'} eq ';') { } 58 145 203
171 58 push @nn, [@ll];
172 58 ▶ $t->{'default_item'} = ($n+1) if $l->{'colon'};
Branch: 1 branch
BranchTrueFalseTotal
if $l->{"colon"} 10 48 58
173 58 @ll = ();
174 58 $n++;
175 }else{
176 145 push @ll, $l;
177 }
178 }
179 37 push @nn, [@ll];
180
181 37 $t->{'subparts'} = \@nn;
182 37 ▶ print STDERR "PP->]\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 37 37
183 }
184
185 816 ▶ if( $t->{'directive'} eq '<' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"directive"} eq "<") 11 805 816
186 11 ▶ print STDERR "PP-><\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 11 11
187 11 my( $l ) = parser('>', $n+1, $tnext);
188 11 my( @l ) = @{$l};
189 11 my( @nn, @ll );
190 11 my( $n ) = 0;
191  
192 11 ▶ while( @l ){
Branch: 1 branch
BranchTrueFalseTotal
if (@l) 35 11 46
193 35 $l = shift @l;
194 35 ▶ if( $l->{'directive'} eq ";" ){
Branch: 1 branch
BranchTrueFalseTotal
if ($l->{'directive'} eq ';') { } 12 23 35
195 12 push @nn, [@ll];
196 12 @ll = ();
197 12 $n++;
198 }else{
199 23 push @ll, $l;
200 }
201 }
202 11 push @nn, [@ll];
203
204 11 $t->{'subparts'} = \@nn;
205 11 ▶ print STDERR "PP->>\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 11 11
206 }
207  
208 816 ▶ if( $t->{'directive'} eq '{' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"directive"} eq "{") 24 792 816
209 24 ▶ print STDERR "PP->{\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 24 24
210 24 my( $b ) = parser('}', $n+1, $tnext);
211  
212 24 ▶ if( @{$b} != 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$b;} != 0) 23 1 24
213 23 $t->{'body'} = $b;
214 }
215 24 ▶ print STDERR "PP->}\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 24 24
216 }
217  
218 816 ▶ if( $t->{'directive'} eq '(' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"directive"} eq "(") 7 809 816
219 7 ▶ print STDERR "PP->(\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 7 7
220 7 $t->{'body'} = parser(')', $n+1, $tnext);
221 7 ▶ print STDERR "PP->)\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 7 7
222 }
223
224 816 ▶ if( $t->{'directive'} eq '/' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"directive"} eq "/") 0 816 816
225 0 my( $tt );
226 0 $tt = &$tnext();
227 0 ▶ $tt->{'directive'} eq 'literal' || return formaterror("I was expecting a function name. Pity.", $tt);
Branch: 1 branch
BranchTrueFalseTotal
$tt->{"directive"} eq "literal" or return formaterror("I was expecting a function name. Pity.", $tt) 0 0 0
228 0 $t->{'funcname'} = $tt->{'text'};
229 0 $tt = &$tnext();
230 0 ▶ $tt->{'directive'} eq '/' || return formaterror("I see no matching / here", $tt);
Branch: 1 branch
BranchTrueFalseTotal
$tt->{"directive"} eq "/" or return formaterror("I see no matching / here", $tt) 0 0 0
231 }
232  
233 816 ▶ if( $t->{'directive'} eq '=(' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"directive"} eq "=(") 1 815 816
234 1 ▶ print STDERR "PP->=(\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 1 1
235 1 $t->{'body'} = parser('=)', $n+1, $tnext);
236 1 ▶ print STDERR "PP->=)\n" if $verbose_parse > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_parse > 1 0 1 1
237 }
238
239 816 push @tt, reduce($t);
240
241 }
242 }
243  
244 ### the optimizer - simplify parse tree
245 816 ▶sub reduce {
Subroutine reduce: called CC=26 SCAR=35.6
Pod undocumented
246 816 my( $t ) = @_;
247 816 my( @n );
248 my( $d );
249  
250 816 $d = $t->{'directive'};
251 816 ▶ print STDERR "R: $d\n" if $verbose_reduce;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_reduce 0 816 816
252
253 # collapse all numbers together
254 816 ▶ if( $d =~ /^[DOXBR]$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /^[DOXBR]$/) 47 769 816
255 47 $t->{'directive'} = 'number';
256 47 ▶ $t->{'radix'} = 10 if $d eq "D";
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "D" 20 27 47
257 47 ▶ $t->{'radix'} = 8 if $d eq "O";
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "O" 2 45 47
258 47 ▶ $t->{'radix'} = 16 if $d eq "X";
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "X" 2 45 47
259 47 ▶ $t->{'radix'} = 2 if $d eq "B";
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "B" 2 45 47
260
261 # NB: radix could end up 'v' or '#'
262
263 47 ▶ if( $d eq "R" ){
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq "R") 21 26 47
264 21 @n = @{$t->{'numbers'}};
265 21 $t->{'radix'} = shift @n;
266 21 $t->{'numbers'} = [ @n ];
267 21 ▶ if( ! $t->{'radix'} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"radix"}) 16 5 21
268 16 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 11 5 16
269 11 $t->{'directive'} = 'roman';
270 }else{
271 5 $t->{'directive'} = 'english';
272 }
273 }
274 }
275 47 ▶ print STDERR "R-> number, $t->{'radix'}\n" if $verbose_reduce > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_reduce > 1 0 47 47
276 }
277  
278 816 ▶ if( $d =~ /^[ASW]$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /^[ASW]$/) 213 603 816
279 213 $t->{'directive'} = 'A';
280 213 ▶ $t->{'how'} = $d eq 'A' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$d eq 'A' ? : 213 0 213
281 }
282
283 # convert to literal or repeated text
284 816 ▶ if( $d =~ /^[%_|~]$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /^[%_|~]$/) 3 813 816
285 3 my( $c, $n );
286
287 3 @n = @{$t->{'numbers'}};
288 3 ▶ $n = shift @n || "";
Condition100%2/2
shift @n || ''
l!l
12
MC/DC100%1/1
shift @n || ''
shift @n
Truth table100%2/2
A
A= shift @n
Aresult
11
00
289 3 ▶ $n = 1 if $n eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "" 2 1 3
290  
291 3 $c = "";
292 3 ▶ $c = " " if $d eq "_";
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "_" 0 3 3
293 3 ▶ $c = "~" if $d eq "~";
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "~" 3 0 3
294 3 ▶ $c = "\n" if $d eq "%";
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "%" 0 3 3
295 3 ▶ $c = "\f" if $d eq "|";
Branch: 1 branch
BranchTrueFalseTotal
if $d eq "|" 0 3 3
296
297 3 ▶ if( $n !~ /\d/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $n =~ /\d/) { } 0 3 3
298 0 $t->{'directive'} = "repeat";
299 0 $t->{'text'} = $c;
300 }else{
301 3 $t->{'text'} = $c x $n;
302 3 $t->{'directive'} = "literal";
303 }
304 3 ▶ print STDERR "R-> $d rewrite $t->{'directive'}\n" if $verbose_reduce > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_reduce > 1 0 3 3
305 }
306
307 # convert to literal text
308 816 ▶ if( $d =~ /^\n/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /^\n/) 0 816 816
309 0 my( $sp ) = $d;
310
311 0 $sp =~ s/\n//;
312 0 ▶ $t->{'text'} = ($t->{'atsign'} ? "\n" : "") . ($t->{'colon'} ? $sp : "");
Branch: 2 branches
BranchTrueFalseTotal
$t->{'atsign'} ? : 0 0 0
$t->{'colon'} ? : 0 0 0
313 0 $t->{'directive'} = "literal";
314
315 0 ▶ print STDERR "R-> whitespace literal\n" if $verbose_reduce > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_reduce > 1 0 0 0
316 }
317
318 816 $t;
319 }
320  
321 0 ▶sub formaterror {
Subroutine formaterror: not called CC=1 SCAR=6.9
Pod undocumented
322 0 my( $msg, $t ) = @_;
323 0 my( $fmt );
324  
325 0 $fmt = $t->{'fullfmt'};
326 0 $fmt =~ s/\n/\$/g;
327 0 print STDERR "\n## FORMAT ERROR: $msg\n";
328 0 print STDERR "## \t\"", $fmt, "\"\n";
329 0 print STDERR "## \t ", " " x $t->{'fmtoffset'}, "^\n";
330
331 0 "error";
332 }
333  
334 1 my($_fmtobja, $_fmtobjs) = ("","");
335 291 ▶sub objecttostring {
Subroutine objecttostring: called CC=14 SCAR=34.6
Pod undocumented
336 291 my( $how, $obj ) = @_;
337  
338 291 ▶ if( ref($obj) ){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $obj) { } 1 290 291
elsif ($obj =~ /^[+-]?\d+$/) { } 111 179 290
elsif ($obj =~ /^[+-]\d*\.\d*$/) { } 0 179 179
339 1 ▶ if( ref($obj) eq "SCALAR"){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $obj eq 'SCALAR') { } 0 1 1
elsif (ref $obj eq 'CODE') { } 0 1 1
elsif (ref $obj eq 'GLOB') { } 0 1 1
340 0 ▶ if( $how ){
Branch: 1 branch
BranchTrueFalseTotal
if ($how) { } 0 0 0
341 0 fmt( "\\~s", $$obj );
342 }else{
343 0 fmt( "\\~a", $$obj );
344 }
345 }elsif( ref($obj) eq "CODE"){
346 # is it possible to do anything more useful?
347 0 "CODE";
348 }elsif( ref($obj) eq "GLOB"){
349 0 "GLOB";
350 }else{
351 1 my( $fmta, $fmtb );
352 # recursively call fmt, to nicely render lists (refs)
353
354 1 ▶ if( $how ){
Branch: 1 branch
BranchTrueFalseTotal
if ($how) { } 0 1 1
355 0 ▶ $_fmtobjs = compile( "~#[~;~s~:;~!{~#[~;~s~:;~s, ~]~}~]" ) unless $_fmtobjs;
Branch: 1 branch
BranchTrueFalseTotal
unless $_fmtobjs 0 0 0
356 0 $fmtb = $_fmtobjs;
357 }else{
358 1 ▶ $_fmtobja = compile( "~#[~;~a~:;~!{~#[~;~a~:;~a, ~]~}~]" ) unless $_fmtobja;
Branch: 1 branch
BranchTrueFalseTotal
unless $_fmtobja 1 0 1
359 1 $fmtb = $_fmtobja;
360 }
361  
362 # should I do anything with the package name?
363 1 $fmta = "<~?>";
364 1 ▶ $fmta = "[~?]" if ref($obj) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj eq "ARRAY" 1 0 1
365 1 ▶ $fmta = "{~?}" if ref($obj) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj eq "HASH" 0 1 1
366 1 fmt($fmta, $fmtb, $obj);
367 }
368 }elsif( $obj =~ /^[+-]?\d+$/ ){
369 111 "$obj";
370 }elsif( $obj =~ /^[+-]\d*\.\d*$/ ){
371 0 "$obj";
372 }else{
373 179 ▶ if( $how ){
Branch: 1 branch
BranchTrueFalseTotal
if ($how) { } 0 179 179
374 0 qq("$obj");
375 }else{
376 179 "$obj";
377 }
378 }
379 }
380  
381 291 ▶sub formatstring {
Subroutine formatstring: called CC=15 SCAR=28.1
Pod undocumented
382 291 my( $how, $mincol, $colinc, $minpad, $padchar, $ovchar, $gravity, $obj ) = @_;
383 291 my( $str ) = objecttostring($how, $obj);
384 291 my( $l, $w, $padamt, $maxcolp );
385  
386 291 ▶ $padchar = chr($padchar) if( $padchar =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $padchar =~ /^\d+$/ 2 289 291
387 291 ▶ $ovchar = chr($ovchar) if( $ovchar =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $ovchar =~ /^\d+$/ 0 291 291
388
389 291 ▶ print STDERR "F: $how, MC:$mincol, C:$colinc, MP:$minpad, P:$padchar, OV:$ovchar, G:$gravity, O:$obj\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_fmt 0 291 291
390 if $verbose_fmt;
391  
392 291 $l = length($str) + $minpad;
393 291 ▶ $w = abs($mincol?$mincol:0);
Branch: 1 branch
BranchTrueFalseTotal
$mincol ? : 66 225 291
394 291 $padamt = $colinc * int((($w - $l) + $colinc - 1) / $colinc);
395 291 ▶ $maxcolp = ($mincol ne "") && ($mincol < 0);
Condition66%2/3
$mincol ne '' && $mincol < 0
!ll&&!rl&&r
225660
MC/DC0%0/2
$mincol ne '' && $mincol < 0
$mincol ne '' $mincol < 0
Truth table66%2/3
A && B
A= $mincol ne ''B= $mincol < 0
ABresult
0X0
100
111
396  
397 291 ▶ print STDERR "F: $l, $w, $padamt, $maxcolp, $str\n" if $verbose_fmt;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_fmt 0 291 291
398  
399 # and minimum padding
400 291 ▶ if( $gravity eq "r" ){
Branch: 2 branches
BranchTrueFalseTotal
if ($gravity eq 'r') { } 63 228 291
elsif ($gravity eq 'l') { } 225 3 228
401 63 $str = $padchar x $minpad . $str;
402 }elsif( $gravity eq "l" ){
403 225 $str .= $padchar x $minpad;
404 }
405
406 291 ▶ if( $l == $w ){
Branch: 2 branches
BranchTrueFalseTotal
if ($l == $w) { } 2 289 291
elsif ($l > $w) { } 232 57 289
407 # Happy Happy, Joy Joy!
408 }elsif( $l > $w ){
409 # You can't fit this five-foot clam through that little passage!
410 232 ▶ if( $maxcolp ){
Branch: 1 branch
BranchTrueFalseTotal
if ($maxcolp) 0 232 232
411 0 my( $fl ) = $w - 1;
412 0 ▶ if( $gravity eq "r" ){
Branch: 1 branch
BranchTrueFalseTotal
if ($gravity eq 'r') { } 0 0 0
413 0 $str =~ s/^(.{$fl}).*$/$1$ovchar/;
414 }else{
415 0 $str =~ s/^(.{$fl}).*$/$ovchar$1/;
416 }
417 }
418 }else{
419 # too short - pad
420 57 ▶ if( $gravity eq "r" ){
Branch: 2 branches
BranchTrueFalseTotal
if ($gravity eq 'r') { } 20 37 57
elsif ($gravity eq 'l') { } 34 3 37
421 20 $str = $padchar x $padamt . $str;
422 }elsif( $gravity eq "l" ){
423 34 $str .= $padchar x $padamt;
424 }else{
425 3 my($rp, $lp);
426 3 $rp = int( ($w - $l) / 2 );
427 3 $lp = $w - $l - $rp;
428 3 $str = $padchar x $lp . $str . $padchar x $rp;
429 }
430 }
431 291 $str;
432 }
433  
434 31 ▶sub formatnumber {
Subroutine formatnumber: called CC=7 SCAR=19.5
Pod undocumented
435 31 my( $radix, $mincol, $padchar, $commachar,
436 $commawidth, $ovchar, $withsign, $withcommas, $val ) = @_;
437 31 my( $str, $sign, @cs );
438  
439 31 $str = "";
440 31 $val = int($val);
441 31 ▶ if( $val < 0 ){
Branch: 2 branches
BranchTrueFalseTotal
if ($val < 0) { } 7 24 31
elsif ($withsign) { } 5 19 24
442 7 $val = - $val;
443 7 $sign = "-";
444 }elsif( $withsign ){
445 5 $sign = "+";
446 }else{
447 19 $sign = "";
448 }
449  
450 # convert to desired radix
451 31 ▶ if( $radix == 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($radix == 1) { } 0 31 31
452 # special case
453 0 $str = "1" x $val;
454 }else{
455 31 @cs = split //,"0123456789abcdefghijklmnopqrstuvwxyz";
456 31 ▶ while( $val ){
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 140 31 171
457 140 $str = $cs[ $val % $radix ] . $str;
458 140 $val = int( $val / $radix );
459 }
460 }
461  
462 # add commas
463 31 ▶ if( $withcommas ){
Branch: 1 branch
BranchTrueFalseTotal
if ($withcommas) 15 16 31
464 15 ▶ 1 while $str =~ s/^(-?\d+)(\d{$commawidth})/$1$commachar$2/;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ s/^(-?\d+)(\d{$commawidth})/$1$commachar$2/ and do { '???' } 18 15 33
465 }
466  
467 31 formatstring(0, $mincol, 1, 0, $padchar, $ovchar, "r", "$sign$str");
468 }
469  
470 11 ▶sub roman {
Subroutine roman: called CC=10 SCAR=23.0
Pod undocumented
471 11 my( $oldway, $val ) = @_;
472 11 my( $rc, $rv, $i, $str );
473 11 my( @rc, @rv );
474
475 11 @rc = qw(/M /D /C /L /X /V M D C L X V I);
476 11 @rv = qw(1000000 500000 100000 50000 10000 5000 1000 500 100 50 10 5 1 0 0);
477 11 $i = 0;
478 11 $str = '';
479
480 11 ▶ while($val){
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 133 11 144
481
482 133 ▶ if( $val >= $rv[$i] ){
Branch: 4 branches
BranchTrueFalseTotal
if ($val >= $rv[$i]) { } 16 117 133
elsif ($val <= $rv[$i + 1]) { } 105 12 117
elsif (not $oldway and ($i & 1) == 0 and $val >= $rv[$i] - $rv[$i + 2]) { } 5 7 12
elsif (not $oldway and ($i & 1) != 0 and $val >= $rv[$i] - $rv[$i + 1]) { } 2 5 7
483 16 $str .= $rc[$i];
484 16 $val -= $rv[$i];
485  
486 }elsif( $val <= $rv[$i+1] ){
487 105 $i++;
488  
489 ▶ }elsif( !$oldway && ($i&1)==0 && ($val >= ($rv[$i] - $rv[$i+2])) ){
Condition83%5/6
not $oldway and ($i & 1) == 0 and $val >= $rv[$i] - $rv[$i + 2]
!ll&&!rl&&r
705
not $oldway and ($i & 1) == 0
!ll&&!rl&&r
345
MC/DC66%2/3
not $oldway and ($i & 1) == 0 and $val >= $rv[$i] - $rv[$i + 2]
not $oldway ($i & 1) == 0 $val >= $rv[$i] - $rv[$i + 2]
Truth table75%3/4
not(A) and B and C
A= not $oldwayB= ($i & 1) == 0C= $val >= $rv[$i] - $rv[$i + 2]
ABCresult
0XX0
10X0
1100
1111
490 5 $str .= $rc[$i+2];
491 5 $str .= $rc[$i];
492 5 $val -= $rv[$i] - $rv[$i+2];
493  
494 ▶ }elsif( !$oldway && ($i&1)!=0 && ($val >= ($rv[$i] - $rv[$i+1])) ){
Condition83%5/6
not $oldway and ($i & 1) != 0 and $val >= $rv[$i] - $rv[$i + 1]
!ll&&!rl&&r
322
not $oldway and ($i & 1) != 0
!ll&&!rl&&r
304
MC/DC66%2/3
not $oldway and ($i & 1) != 0 and $val >= $rv[$i] - $rv[$i + 1]
not $oldway ($i & 1) != 0 $val >= $rv[$i] - $rv[$i + 1]
Truth table75%3/4
not(A) and B and C
A= not $oldwayB= ($i & 1) != 0C= $val >= $rv[$i] - $rv[$i + 1]
ABCresult
0XX0
10X0
1100
1111
495 2 $str .= $rc[$i+1];
496 2 $str .= $rc[$i];
497 2 $val -= $rv[$i] - $rv[$i+1];
498  
499 }else{
500 5 $i ++;
501 }
502 }
503 11 $str;
504 }
505  
506 7 ▶sub englishsmall {
Subroutine englishsmall: called CC=10 SCAR=23.4
Pod undocumented
507 7 my($ordinal, $val) = @_;
508 7 my($n, $str);
509 7 my(@units, @ounits, @tens, @otens);
510  
511 7 @units = qw(zero one two three four five six seven eight nine
512 ten eleven twelve thirteen fourteen fifteen sixteen
513 seventeen eighteen nineteen);
514 7 @ounits = qw(zeroth first second third fourth fifth sixth seventh
515 eighth ninth tenth eleventh twelfth);
516 7 @tens = qw(twenty thirty forty fifty sixty seventy eighty ninety);
517 7 @otens = qw(twentieth thirtieth fortieth fiftieth sixtieth seventieth eightieth ninetieth);
518 7 unshift @tens, ""; unshift @tens, "";
519 7 unshift @otens, ""; unshift @otens, "";
520  
521 7 $str = "";
522 7 ▶ if( $val >= 100 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= 100) 2 5 7
523 2 $str .= " " . $units[ $val / 100 ] . " hundred";
524 2 $val %= 100;
525 2 ▶ $str .= "th" if $ordinal && !$val;
Branch: 1 branch
BranchTrueFalseTotal
if $ordinal and not $val 0 2 2
Condition66%2/3
$ordinal and not $val
!ll&&!rl&&r
110
MC/DC0%0/2
$ordinal and not $val
$ordinal not $val
Truth table66%2/3
A and not(B)
A= $ordinalB= not $val
ABresult
0X0
100
111
526 }
527  
528 7 ▶ if( $val >= 20 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= 20) 2 5 7
529 2 $n = $val % 10;
530 2 ▶ if( $ordinal && !$n ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ordinal and not $n) { } 0 2 2
Condition66%2/3
$ordinal and not $n
!ll&&!rl&&r
110
MC/DC0%0/2
$ordinal and not $n
$ordinal not $n
Truth table66%2/3
A and not(B)
A= $ordinalB= not $n
ABresult
0X0
100
111
531 0 $str .= " " . $otens[ $val / 10 ];
532 }else{
533 2 $str .= " " . $tens[ $val / 10 ];
534 }
535 2 $val = $n;
536 }
537  
538 7 ▶ if( $val ){
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 7 0 7
539 7 ▶ if( $ordinal ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ordinal) { } 1 6 7
540 1 ▶ if( $val < @ounits ){
Branch: 1 branch
BranchTrueFalseTotal
if ($val < @ounits) { } 1 0 1
541 1 $str .= " " . $ounits[$val];
542 }else{
543 0 $str .= " " . $units[$val] . "th";
544 }
545 }else{
546 6 $str .= " " . $units[$val];
547 }
548 }
549  
550 7 $str =~ s/^ //;
551 7 $str;
552 }
553  
554 6 ▶sub english {
Subroutine english: called CC=6 SCAR=18.8
Pod undocumented
555 6 my($ordinal, $val) = @_;
556 6 my(@illions);
557 6 my($ordd, $f);
558
559 6 @illions = qw(thousand million billion trillion quadrillion quintillion
560 sextillion septillion octillion nonillion decillion undecillion
561 duodecillion tredecillion quattuordecillion quindecillion
562 sexdecillion septdecillion octodecillion novemdecillion vigintillion);
563 6 unshift @illions, "";
564
565 7 ▶ $f = sub {
Subroutine __ANON__: called CC=11 SCAR=24.8
566 7 my($val, $k) = @_;
567 7 my($n, $r, $str);
568  
569 7 $str = "";
570 7 $n = $val % 1000;
571 7 $r = int($val / 1000);
572  
573 7 ▶ if( $r ){
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 2 5 7
574 2 $str .= &$f($r, $k + 1);
575 2 ▶ if( $n ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 2 0 2
576 2 ▶ if( !$k && ($n < 100) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $k and $n < 100) { } 0 2 2
Condition33%1/3
not $k and $n < 100
!ll&&!rl&&r
020
MC/DC0%0/2
not $k and $n < 100
not $k $n < 100
Truth table33%1/3
not(A) and B
A= not $kB= $n < 100
ABresult
0X0
100
111
577 0 $str .= " and ";
578 }else{
579 2 $str .= ", ";
580 }
581 }
582 }
583
584 7 ▶ if( $n ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 7 0 7
585 7 my($o);
586 7 ▶ $o = $ordinal && ($k==0);
Condition100%3/3
$ordinal && $k == 0
!ll&&!rl&&r
511
MC/DC100%2/2
$ordinal && $k == 0
$ordinal $k == 0
Truth table100%3/3
A && B
A= $ordinalB= $k == 0
ABresult
0X0
100
111
587 7 ▶ $ordd = 1 if($o);
Branch: 1 branch
BranchTrueFalseTotal
if $o 1 6 7
588 7 $str .= englishsmall($o , $n);
589 }
590
591 7 ▶ if( $k && $n ){
Branch: 1 branch
BranchTrueFalseTotal
if ($k and $n) 2 5 7
Condition66%2/3
$k and $n
!ll&&!rl&&r
502
MC/DC50%1/2
$k and $n
$k $n
Truth table66%2/3
A and B
A= $kB= $n
ABresult
0X0
100
111
592 2 $str .= " ";
593 2 ▶ if( $k > @illions ){
Branch: 1 branch
BranchTrueFalseTotal
if ($k > @illions) { } 0 2 2
594 0 $str .= "times ten to the " . english(1, $k * 3);
595 }else{
596 2 $str .= $illions[ $k ];
597 }
598 }
599 7 $str;
600 6 };
601  
602 6 ▶ if( ! $val ){
Branch: 2 branches
BranchTrueFalseTotal
if (not $val) { } 0 6 6
elsif ($val < 0) { } 1 5 6
603 0 ▶ if( $ordinal ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ordinal) { } 0 0 0
604 0 return "zeroth";
605 }else{
606 0 return "zero";
607 }
608 }elsif( $val < 0 ){
609 1 return "minus " . english($ordinal, - $val);
610 }else{
611 5 ▶ &$f($val, 0) . (($ordinal && !$ordd) ? "th" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ordinal && !$ordd ? : 0 5 5
Condition66%2/3
$ordinal and not $ordd
!ll&&!rl&&r
410
MC/DC0%0/2
$ordinal and not $ordd
$ordinal not $ordd
Truth table66%2/3
A and not(B)
A= $ordinalB= not $ordd
ABresult
0X0
100
111
612 }
613 }
614
615 18 ▶sub falsep {
Subroutine falsep: called CC=3 SCAR=14.2
Pod undocumented
616 18 my($val) = @_;
617  
618 18 ▶ if( $config_zero_is_false ){
Branch: 1 branch
BranchTrueFalseTotal
if ($config_zero_is_false) { } 0 18 18
619 0 ▶ $val ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
620 }else{
621 18 $val eq "";
622 }
623 }
624  
625 20 ▶sub mkarray {
Subroutine mkarray: called CC=3 SCAR=13.6
Pod undocumented
626 20 my( $a ) = @_;
627 20 my( @a );
628  
629 20 ▶ if( $a =~ /ARRAY/ ){
Branch: 2 branches
BranchTrueFalseTotal
if ($a =~ /ARRAY/) { } 20 0 20
elsif ($a =~ /HASH/) { } 0 0 0
630 20 @a = @{$a};
631 }elsif( $a =~ /HASH/ ){
632 0 @a = %{$a};
633 }else{
634 0 @a = ( $a );
635 }
636 20 @a;
637 }
638  
639 2 ▶sub capitalize {
Subroutine capitalize: called CC=1 SCAR=0.0
Pod undocumented
640 2 my( $s ) = @_;
641  
642 2 $s = ucfirst(lc($s));
643 2 $s =~ s/\b(\w)/\U$1/g;
644 2 $s;
645 }
646
647 335 ▶sub nextarg {
Subroutine nextarg: called CC=2 SCAR=7.2
Pod undocumented
648 335 ▶ return "" if( $argno >= @arglist );
Branch: 1 branch
BranchTrueFalseTotal
if $Lisp::Fmt::argno >= @Lisp::Fmt::arglist 0 335 335
649 335 return $arglist[ $argno ++ ];
650 }
651  
652 20 ▶sub pound {
Subroutine pound: called CC=2 SCAR=6.9
Pod undocumented
653 20 ▶ return 0 if( $argno >= @arglist );
Branch: 1 branch
BranchTrueFalseTotal
if $Lisp::Fmt::argno >= @Lisp::Fmt::arglist 2 18 20
654 18 return @arglist - $argno;
655 }
656  
657 1543 ▶sub param {
Subroutine param: called CC=5 SCAR=16.1
Pod undocumented
658 1543 my( $t, $nth, $dfl ) = @_;
659 1543 my( $n, @n );
660  
661 1543 @n = @{$t->{'numbers'}};
662 1543 ▶ return $dfl if( $nth >= @n );
Branch: 1 branch
BranchTrueFalseTotal
if $nth >= @n 1291 252 1543
663 252 $n = $n[ $nth ];
664  
665 252 ▶ return nextarg() if( $n eq "v" );
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "v" 3 249 252
666 249 ▶ return pound() if( $n eq "#" );
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "#" 20 229 249
667  
668 229 ▶ return $dfl if $n eq ""; # no n, dfl
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "" 28 201 229
669  
670 201 $n;
671 }
672  
673 ### run the compiled format
674 325 ▶sub run {
Subroutine run: called CC=142 SCAR=58.3
Pod undocumented
675 325 my( $t ) = @_;
676 325 my( $d, @t );
677  
678 325 @t = @{$t};
679
680 325 ▶ while( @t ){
Branch: 1 branch
BranchTrueFalseTotal
if (@t) 756 316 1072
681 756 $t = shift @t;
682 756 $d = $t->{'directive'};
683  
684 756 ▶ print STDERR "U: $d\n" if $verbose_run;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose_run 0 756 756
685  
686 756 ▶ if( $d eq 'literal' ){
Branch: 25 branches
BranchTrueFalseTotal
if ($d eq 'literal') { } 297 459 756
elsif ($d eq 'repeat') { } 0 459 459
elsif ($d eq '&') { } 20 439 459
elsif ($d eq 'T') { } 24 415 439
elsif ($d eq 'A') { } 221 194 415
elsif ($d eq '*') { } 6 188 194
elsif ($d eq '?') { } 3 185 188
elsif ($d eq 'P') { } 15 170 185
elsif ($d eq 'C') { } 17 153 170
elsif ($d eq '(') { } 7 146 153
elsif ($d eq '/') { } 0 146 146
elsif ($d eq '<') { } 11 135 146
elsif ($d eq '{') { } 22 113 135
elsif ($d eq '[') { } 38 75 113
elsif ($d eq '^') { } 27 48 75
elsif ($d eq 'number') { } 31 17 48
elsif ($d eq 'roman') { } 11 6 17
elsif ($d eq 'english') { } 5 1 6
elsif ($d eq '=V') { } 0 1 1
elsif ($d eq '=(') { } 1 0 1
elsif ($d eq '=F') { } 0 0 0
elsif ($d eq ')') { } 0 0 0
elsif ($d eq '}') { } 0 0 0
elsif ($d eq ']') { } 0 0 0
elsif ($d eq ';') { } 0 0 0
687 297 $accum .= $t->{'text'};
688
689 }elsif( $d eq 'repeat' ){
690 0 $accum .= $t->{'text'} x param($t, 0, 1);
691  
692 }elsif( $d eq "&" ){
693 20 my($n) = param($t, 0, 1);
694  
695 20 ▶ next unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 20 0 20
696 0 ▶ $accum .= "\n" unless( $accum =~ /\n$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $Lisp::Fmt::accum =~ /\n$/ 0 0 0
697 0 ▶ if( $n > 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 1) 0 0 0
698 0 $accum .= "\n" x ($n - 1);
699 }
700
701 }elsif( $d eq "T" ){
702 24 my($colnum, $colinc, $tabchar);
703 24 my($l) = $accum;
704 24 my($cp, $mp);
705  
706 24 $colnum = param($t,0,1);
707 24 $colinc = param($t,1,1);
708 24 $tabchar = param($t,2," ");
709
710 24 $l =~ s/.*\n$//;
711 24 $cp = length $l;
712 24 $mp = 0;
713
714 24 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 4 20 24
715 4 ▶ if( $colinc ){
Branch: 1 branch
BranchTrueFalseTotal
if ($colinc) 4 0 4
716 4 $mp = $colnum + $colinc - (($cp + $colnum) % $colinc);
717 }
718 }else{
719 20 $mp = $colnum - $cp;
720 20 ▶ if( $mp < 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($mp < 0) 15 5 20
721 15 ▶ if( $colinc ){
Branch: 1 branch
BranchTrueFalseTotal
if ($colinc) 15 0 15
722 15 $mp = $colnum + $colinc - ($cp % $colinc);
723 15 ▶ $mp = $mp - $colinc if $mp >= $colinc;
Branch: 1 branch
BranchTrueFalseTotal
if $mp >= $colinc 8 7 15
724 }
725 }
726 }
727  
728 24 $accum .= $tabchar x $mp;
729
730 }elsif( $d eq 'A' ){
731 # mincol, colinc, minpad, padchr, ovchr, gravity
732 $accum .=
733 formatstring( $t->{'how'}, param($t,0,""), param($t,1,1),
734 param($t,2,0), param($t,3," "),
735 param($t,4,"*"),
736 221 ▶ $t->{'gravity'} || ($t->{'atsign'} ? "r" : "l"),
Branch: 1 branch
BranchTrueFalseTotal
$t->{'atsign'} ? : 5 210 215
Condition66%2/3
$t->{'gravity'} || ($t->{'atsign'} ? 'r' : 'l')
l!l&&r!l&&!r
62150
MC/DC0%0/2
$t->{'gravity'} || ($t->{'atsign'} ? 'r' : 'l')
$t->{'gravity'} ($t->{'atsign'} ? 'r' : 'l')
Truth table66%2/3
A || B
A= $t->{'gravity'}B= ($t->{'atsign'} ? 'r' : 'l')
ABresult
1X1
011
000
737 nextarg() );
738  
739 }elsif( $d eq '*' ){
740 6 ▶ my( $n ) = param($t, 0, $t->{'atsign'} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$t->{'atsign'} ? : 2 4 6
741  
742 6 ▶ $n = -$n if( $t->{'colon'} );
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"colon"} 2 4 6
743  
744 6 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 2 4 6
745 2 $argno = $n;
746 }else{
747 4 $argno += $n;
748 }
749 6 ▶ $argno = 0 if( $argno < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $Lisp::Fmt::argno < 0 0 6 6
750  
751 }elsif( $d eq '?' ){
752 3 my( $fmt ) = nextarg();
753 3 my( $rv );
754  
755 3 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 1 2 3
756 # use current arglist
757 1 ▶ $rv = run( ref($fmt) ? $fmt : compile( $fmt ));
Branch: 1 branch
BranchTrueFalseTotal
ref $fmt ? : 0 1 1
758 }else{
759 # nextarg is list of args
760 2 my( $a ) = nextarg();
761 2 local( $argno ) = 0;
762 2 local( @arglist );
763  
764 2 @arglist = mkarray( $a );
765
766 2 ▶ $rv = run( ref($fmt) ? $fmt : compile( $fmt ));
Branch: 1 branch
BranchTrueFalseTotal
ref $fmt ? : 1 1 2
767 }
768 3 ▶ return $rv if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 3 3
769
770 }elsif( $d eq 'P' ){
771 15 my( $n );
772  
773 15 ▶ if( $t->{'colon'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'colon'}) { } 8 7 15
774 8 $n = $arglist[ $argno - 1 ];
775 }else{
776 7 $n = nextarg();
777 }
778  
779 15 ▶ if( $n == 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) { } 5 10 15
780 5 ▶ $accum .= "y" if $t->{'atsign'};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"atsign"} 1 4 5
781 }else{
782 10 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 2 8 10
783 2 $accum .= "ies";
784 }else{
785 8 $accum .= "s";
786 }
787 }
788
789 }elsif( $d eq 'C' ){
790 17 my( @cv ) = qw(NUL SOH STX ETX EOT ENQ ACK BEL BS HT NL
791 VT NP CR SO SI DLE DC1 DC2 DC3 DC4 NAK
792 SYN ETB CAN EM SUB ESC FS GS RS US SP);
793 17 my( $n ) = param($t, 0, "");
794 17 my( $c, $str );
795 17 ▶ $n = nextarg() if $n eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "" 14 3 17
796 17 ▶ $n = ord($n) unless $n =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /^\d+$/ 3 14 17
797 17 $c = chr($n);
798  
799 17 ▶ if( $t->{'colon'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'colon'}) { } 7 10 17
800 7 ▶ if( $t->{'atsign'} && $n && $n < 27 ){
Branch: 3 branches
BranchTrueFalseTotal
if ($t->{'atsign'} and $n and $n < 27) { } 0 7 7
elsif ($n < @cv) { } 3 4 7
elsif ($n >= 127) { } 3 1 4
Condition33%2/6
$t->{"atsign"} and $n and $n < 27
!ll&&!rl&&r
700
$t->{"atsign"} and $n
!ll&&!rl&&r
700
MC/DC0%0/3
$t->{"atsign"} and $n and $n < 27
$t->{"atsign"} $n $n < 27
Truth table25%1/4
A and B and C
A= $t->{"atsign"}B= $nC= $n < 27
ABCresult
0XX0
10X0
1100
1111
801 0 $str = "Control-" . chr($n+ord('A'));
802 }elsif( $n < @cv ){
803 3 $str = $cv[$n];
804 }elsif( $n >= 127 ){
805 3 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 0 3 3
806 # <shrug>...
807 0 $str = "Meta-" . fmt("~:!C", $n & 127);
808 }else{
809 3 $str = sprintf "\\0%o", $n;
810 }
811 }else{
812 1 $str = $c;
813 }
814 }else{
815 10 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 8 2 10
816 8 ▶ if( ($n >= 127) || ($n < @cv) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 127 or $n < @cv) { } 7 1 8
Condition100%3/3
$n >= 127 or $n < @cv
l!l&&r!l&&!r
341
MC/DC100%2/2
$n >= 127 or $n < @cv
$n >= 127 $n < @cv
Truth table100%3/3
A or B
A= $n >= 127B= $n < @cv
ABresult
1X1
011
000
817 7 $str = sprintf "\"\\0%o\"", $n;
818 }else{
819 1 $str = "\"$c\"";
820 }
821 }else{
822 2 $str = $c;
823 }
824 }
825 17 $accum .= $str;
826
827 }elsif( $d eq '(' ){
828 7 my( $str, $rv );
829 7 do {
830 7 local( $accum ) = ("");
831 7 $rv = run( $t->{'body'} );
832 7 $str = $accum;
833  
834 7 ▶ if( $t->{'colon'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'colon'}) { } 4 3 7
835 4 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 2 2 4
836 2 $str = uc($str);
837 }else{
838 2 $str = capitalize($str);
839 }
840 }else{
841 3 ▶ if( $t->{'atsign'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 2 1 3
842 2 $str = ucfirst(lc($str));
843 }else{
844 1 $str = lc($str);
845 }
846 }
847 };
848 7 $accum .= $str;
849 7 ▶ return $rv if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 7 7
850  
851 }elsif( $d eq '/' ){
852 # this is implemented as ~/funcname~/ and not as ~/funcname/
853 # so sue me...
854 0 my( $func, $str, $p, @p );
855  
856 0 $func = $t->{'funcname'};
857 0 foreach $p ( @{$t->{'numbers'}} ){
858 0 ▶ $p = pound() if( $p eq "#" );
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "#" 0 0 0
859 0 ▶ $p = nextarg() if( $p eq "v" );
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "v" 0 0 0
860 0 push @p, $p;
861 }
862 0 $str = nextarg();
863 0 $str = "$func($str, $t->{'colon'}, t->{'atsign'}";
864 0 ▶ $str .= ", " . join(", ", @p) if( @p );
Branch: 1 branch
BranchTrueFalseTotal
if @p 0 0 0
865 0 $str .= ")";
866  
867 0 $accum .= eval( $str );
868
869 }elsif( $d eq '<' ){
870 # XXX
871 11 my( $str, $rv, $n, $s );
872 11 my( @str );
873 11 my( $mincol, $colinc, $minpad, $padchar ) =
874 (param($t,0,""), param($t,1,1),
875 param($t,2,0), param($t,3," "));
876
877
878 11 do {
879 11 local( $accum );
880 # $rv = run( $t->{'subparts'}[0] );
881 # $str = $accum;
882
883 11 foreach $s ( @{$t->{'subparts'}} ){
884 23 $accum = "";
885 23 $rv = run( $s );
886 23 ▶ last if $rv =~ /hat/;
Branch: 1 branch
BranchTrueFalseTotal
if $rv =~ /hat/ 0 23 23
887 23 push @str, $accum;
888 23 $n ++;
889 }
890 };
891  
892 11 ▶ if( $n == 1 ){
Branch: 2 branches
BranchTrueFalseTotal
if ($n == 1) { } 3 8 11
elsif ($n >= 2) { } 8 0 8
893 $str = formatstring( 0, $mincol, $colinc, $minpad, $padchar, "*",
894 3 ▶ $t->{'atsign'} ? ($t->{'colon'} ? 'c' : 'l') : 'r',
Branch: 2 branches
BranchTrueFalseTotal
$t->{'atsign'} ? : 2 1 3
$t->{'colon'} ? : 1 1 2
895 $str[0] );
896  
897 }elsif( $n >= 2 ){
898 8 my( $rspace, $lspace, $space, $m );
899  
900 8 $rspace = $mincol;
901 8 $m = 0;
902 8 $str = '';
903 8 foreach $s (@str){
904 20 $space = $rspace / ( $n - $m );
905 20 $rspace -= $space;
906
907 $str .= formatstring( 0, $space, $colinc, $m?$minpad:0, $padchar, "*",
908 $m==0 ? ($t->{'colon'} ? 'r' : 'l') :
909 20 ▶ ($m==$n-1 ? ($t->{'atsign'} ? 'l' : 'r') :
Branch: 5 branches
BranchTrueFalseTotal
$m ? : 12 8 20
$m == 0 ? : 8 12 20
$t->{'colon'} ? : 4 4 8
$m == $n - 1 ? : 8 4 12
$t->{'atsign'} ? : 4 4 8
910 $config_align_middle),
911 $str[$m]);
912 20 $m ++;
913 }
914 }
915
916
917 11 $accum .= $str;
918
919 }elsif( $d eq '{' ){
920 22 my( $maxiter ) = param($t, 0, "");
921 22 ▶ my( $maxiterp ) = $maxiter ne "" ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$maxiter ne '' ? : 6 16 22
922 22 my( $retv );
923 22 my( $body ) = $t->{'body'};
924  
925 22 ▶ if( !$body ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($body) 1 21 22
926 1 $body = nextarg();
927 1 ▶ return formaterror("An empty {} may be less filling, but it won't work", $t) unless $body;
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 1 1
928 1 $body = compile($body);
929 }
930  
931 22 ▶ if( $t->{'colon'} && $t->{'atsign'} ){
Branch: 3 branches
BranchTrueFalseTotal
if ($t->{'colon'} and $t->{'atsign'}) { } 2 20 22
elsif ($t->{'colon'}) { } 2 18 20
elsif ($t->{'atsign'}) { } 5 13 18
Condition100%3/3
$t->{"colon"} and $t->{"atsign"}
!ll&&!rl&&r
1822
MC/DC100%2/2
$t->{"colon"} and $t->{"atsign"}
$t->{"colon"} $t->{"atsign"}
Truth table100%3/3
A and B
A= $t->{"colon"}B= $t->{"atsign"}
ABresult
0X0
100
111
932 # use remaining args, which are sublists
933  
934 2 ▶ while( !$maxiterp || $maxiter-- ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $maxiterp or $maxiter--) 6 1 7
Condition100%3/3
not $maxiterp or $maxiter--
l!l&&r!l&&!r
421
MC/DC100%2/2
not $maxiterp or $maxiter--
not $maxiterp $maxiter--
Truth table100%3/3
not(A) or B
A= not $maxiterpB= $maxiter--
ABresult
1X1
011
000
935 6 my( $a, @a );
936 6 ▶ last if $argno >= @arglist;
Branch: 1 branch
BranchTrueFalseTotal
if $Lisp::Fmt::argno >= @Lisp::Fmt::arglist 1 5 6
937
938 5 $a = nextarg();
939 5 @a = mkarray($a);
940 5 do {
941 5 local($argno) = 0;
942 5 local(@arglist) = @a;
943  
944 5 $retv = run( $body );
945 5 ▶ last if( $retv =~ /colon/ ); # otherwise just this iter
Branch: 1 branch
BranchTrueFalseTotal
if $retv =~ /colon/ 0 5 5
946 };
947 }
948
949 }elsif( $t->{'colon'} ){
950 # next arg is list of sublists
951 2 my( $a, @a );
952
953 2 $a = nextarg();
954 2 @a = @{$a};
955  
956 2 ▶ while( !$maxiterp || $maxiter-- ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $maxiterp or $maxiter--) 6 1 7
Condition100%3/3
not $maxiterp or $maxiter--
l!l&&r!l&&!r
421
MC/DC100%2/2
not $maxiterp or $maxiter--
not $maxiterp $maxiter--
Truth table100%3/3
not(A) or B
A= not $maxiterpB= $maxiter--
ABresult
1X1
011
000
957 6 ▶ last unless @a;
Branch: 1 branch
BranchTrueFalseTotal
unless @a 1 5 6
958 5 do {
959 5 local($argno) = 0;
960 5 local(@arglist) = @{shift @a};
961  
962 5 $retv = run( $body );
963 5 ▶ last if( $retv =~ /colon/ ); # otherwise just this iter
Branch: 1 branch
BranchTrueFalseTotal
if $retv =~ /colon/ 0 5 5
964 };
965 }
966  
967 }elsif( $t->{'atsign'} ){
968 # use remaining args
969  
970 5 ▶ while( !$maxiterp || $maxiter-- ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $maxiterp or $maxiter--) 16 1 17
Condition100%3/3
not $maxiterp or $maxiter--
l!l&&r!l&&!r
1421
MC/DC100%2/2
not $maxiterp or $maxiter--
not $maxiterp $maxiter--
Truth table100%3/3
not(A) or B
A= not $maxiterpB= $maxiter--
ABresult
1X1
011
000
971 16 ▶ last if $argno >= @arglist;
Branch: 1 branch
BranchTrueFalseTotal
if $Lisp::Fmt::argno >= @Lisp::Fmt::arglist 2 14 16
972 14 $retv = run( $body );
973 14 ▶ last if( $retv =~ /hat/ );
Branch: 1 branch
BranchTrueFalseTotal
if $retv =~ /hat/ 2 12 14
974 }
975  
976 }else{
977 # next arg is list of args
978 13 my( $a, @a );
979  
980 13 $a = nextarg();
981 13 @a = mkarray($a);
982 13 do {
983 13 local($argno) = 0;
984 13 local(@arglist) = @a;
985  
986 13 ▶ while( !$maxiterp || $maxiter-- ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $maxiterp or $maxiter--) 37 3 40
Condition100%3/3
not $maxiterp or $maxiter--
l!l&&r!l&&!r
3253
MC/DC100%2/2
not $maxiterp or $maxiter--
not $maxiterp $maxiter--
Truth table100%3/3
not(A) or B
A= not $maxiterpB= $maxiter--
ABresult
1X1
011
000
987 37 ▶ last if $argno >= @arglist;
Branch: 1 branch
BranchTrueFalseTotal
if $Lisp::Fmt::argno >= @Lisp::Fmt::arglist 9 28 37
988 28 $retv = run( $body );
989 28 ▶ last if( $retv =~ /hat/ );
Branch: 1 branch
BranchTrueFalseTotal
if $retv =~ /hat/ 1 27 28
990 }
991 };
992 }
993
994 }elsif( $d eq '[' ){
995 38 my( @ch ) = @{$t->{'subparts'}};
996 38 my( $ni ) = scalar @ch;
997 38 my( $n ) = param($t, 0, "");
998 38 my( $rv );
999 38 ▶ $n = nextarg() if $n eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "" 17 21 38
1000  
1001 38 ▶ if( $t->{'atsign'} ){
Branch: 2 branches
BranchTrueFalseTotal
if ($t->{'atsign'}) { } 8 30 38
elsif ($t->{'colon'}) { } 2 28 30
1002 8 ▶ $argno -- if( !falsep($n) );
Branch: 1 branch
BranchTrueFalseTotal
unless falsep($n) 5 3 8
1003 8 ▶ $n = !falsep($n) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
!falsep($n) ? : 5 3 8
1004 }elsif( $t->{'colon'} ){
1005 2 ▶ $n = falsep($n) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
falsep($n) ? : 1 1 2
1006 }
1007  
1008 38 ▶ if( $n >= $ni && (defined $t->{'default_item'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $ni and defined $t->{"default_item"}) 2 36 38
Condition100%3/3
$n >= $ni and defined $t->{"default_item"}
!ll&&!rl&&r
2882
MC/DC100%2/2
$n >= $ni and defined $t->{"default_item"}
$n >= $ni defined $t->{"default_item"}
Truth table100%3/3
A and B
A= $n >= $niB= defined $t->{"default_item"}
ABresult
0X0
100
111
1009 2 $n = $t->{'default_item'};
1010 }
1011  
1012 38 ▶ if( $n < $ni ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $ni) 30 8 38
1013 30 my( $str );
1014 30 do {
1015 30 local( $accum ) = ("");
1016 30 $rv = run( $t->{'subparts'}[$n] );
1017 30 $str = $accum;
1018 };
1019 30 $accum .= $str;
1020 }
1021 38 ▶ return $rv if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 38 38
1022
1023 }elsif( $d eq '^' ){
1024 27 my( $np, $out );
1025
1026 27 $np = @{$t->{'numbers'}};
1027  
1028 27 ▶ if( $np == 1 ){
Branch: 3 branches
BranchTrueFalseTotal
if ($np == 1) { } 2 25 27
elsif ($np == 2) { } 2 23 25
elsif ($np == 3) { } 3 20 23
1029 2 ▶ $out = 1 if param($t, 0, "") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if param($t, 0, "") == 0 1 1 2
1030 }elsif( $np == 2 ){
1031 2 ▶ $out = 1 if param($t, 0, "") == param($t, 1, "");
Branch: 1 branch
BranchTrueFalseTotal
if param($t, 0, "") == param($t, 1, "") 1 1 2
1032 }elsif( $np == 3 ){
1033 3 ▶ $out = 1 if (param($t, 0, "") <= param($t, 1, ""))
Branch: 1 branch
BranchTrueFalseTotal
if param($t, 0, "") <= param($t, 1, "") and param($t, 1, "") <= param($t, 2, "") 1 2 3
Condition100%3/3
param($t, 0, "") <= param($t, 1, "") and param($t, 1, "") <= param($t, 2, "")
!ll&&!rl&&r
111
MC/DC100%2/2
param($t, 0, "") <= param($t, 1, "") and param($t, 1, "") <= param($t, 2, "")
param($t, 0, "") <= param($t, 1, "") param($t, 1, "") <= param($t, 2, "")
Truth table100%3/3
A and B
A= param($t, 0, "") <= param($t, 1, "")B= param($t, 1, "") <= param($t, 2, "")
ABresult
0X0
100
111
1034 && (param($t, 1, "") <= param($t, 2, ""));
1035 }else{
1036 20 ▶ $out = 1 if $argno >= @arglist;
Branch: 1 branch
BranchTrueFalseTotal
if $Lisp::Fmt::argno >= @Lisp::Fmt::arglist 6 14 20
1037 }
1038  
1039 27 ▶ if( $out ){
Branch: 1 branch
BranchTrueFalseTotal
if ($out) 9 18 27
1040 9 ▶ return "hat/colon" if( $t->{'colon'} );
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"colon"} 0 9 9
1041 9 return "hat";
1042 }
1043
1044 }elsif( $d eq 'number' ){
1045 31 my( $r );
1046 # $radix, $mincol, $padchar, $commachar, $commawidth, $ovchar, $withsign, $withcommas, $val
1047  
1048 31 $r = $t->{'radix'};
1049 31 ▶ $r = pound() if( $r eq "#" );
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "#" 0 31 31
1050 31 ▶ $r = nextarg() if( $r eq "v" );
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "v" 0 31 31
1051 31 ▶ return formaterror("In base $r? I'm game. Would you care to explain how?", $t) if( $r<1 || $r>36 );
Branch: 1 branch
BranchTrueFalseTotal
if $r < 1 or $r > 36 0 31 31
Condition33%1/3
$r < 1 or $r > 36
l!l&&r!l&&!r
0031
MC/DC0%0/2
$r < 1 or $r > 36
$r < 1 $r > 36
Truth table33%1/3
A or B
A= $r < 1B= $r > 36
ABresult
1X1
011
000
1052
1053 $accum .=
1054 formatnumber( $r, param($t,0,""), param($t,1," "),
1055 param($t,2,","), param($t,3,3), param($t,4,"*"),
1056 31 ▶ ($t->{'atsign'}?1:0), ($t->{'colon'}?1:0),
Branch: 2 branches
BranchTrueFalseTotal
$t->{'atsign'} ? : 6 25 31
$t->{'colon'} ? : 15 16 31
1057 nextarg());
1058
1059 }elsif( $d eq 'roman' ){
1060 $accum .= formatstring(0, param($t,0,""), 1, 0, param($t,1, " "),
1061 param($t,4, "*"), "r",
1062 11 roman($t->{'colon'}, nextarg()));
1063
1064 }elsif( $d eq 'english' ){
1065 $accum .= formatstring(0, param($t,0,""), 1, 0, param($t,1, " "),
1066 param($t,4, "*"), "r",
1067 5 english($t->{'colon'}, nextarg()));
1068  
1069 ### Non-standard 2 character (=X) directives
1070 }elsif( $d eq '=V' ){
1071 # Version info
1072 # ~=:V - long form
1073 # ~=:@V - longer form
1074  
1075 0 ▶ $accum .= "Good morning Dr. Chandra, I am " if $t->{'atsign'};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"atsign"} 0 0 0
1076 0 ▶ $accum .= "Fmt Version " if $t->{'colon'};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"colon"} 0 0 0
1077 0 $accum .= "$VERSION";
1078
1079 }elsif( $d eq '=(' ){
1080 # "eval" - use results of formatting as a format spec
1081 # ~=(...~=)
1082
1083 1 my( $str, $rv, $fmt );
1084 1 do {
1085 1 local( $accum ) = ("");
1086 1 $rv = run( $t->{'body'} );
1087 1 $str = $accum;
1088  
1089 1 $accum = '';
1090 1 $fmt = compile($str);
1091 1 $rv = run( $fmt );
1092 1 $str = $accum;
1093
1094 };
1095 1 $accum .= $str;
1096 1 ▶ return $rv if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 1 1
1097  
1098 }elsif( $d eq '=F' ){
1099 # suck in line/lines from a file
1100 # ~=F - entire file
1101 # ~N=F - just line N
1102 # ~N,M=F lines N through M
1103  
1104 0 my( $null, $i, $n, $m, $f );
1105  
1106 0 $n = param($t,0,'');
1107 0 $m = param($t,1,'');
1108 0 $f = nextarg();
1109 0 $i = 1;
1110 0 ▶ open(FMT_FILE, $f) || return formaterror( "'$f' is stubborn and refuses to open: $!", $t);
Branch: 1 branch
BranchTrueFalseTotal
open FMT_FILE, $f or return formaterror("'${f}' is stubborn and refuses to open: $!", $t) 0 0 0
1111  
1112 0 ▶ if( $n ne '' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n ne '') { } 0 0 0
1113 0 ▶ while( $n != $i && !eof(FMT_FILE) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $i and not eof FMT_FILE) 0 0 0
Condition0%0/3
$n != $i and not eof FMT_FILE
!ll&&!rl&&r
000
MC/DC0%0/2
$n != $i and not eof FMT_FILE
$n != $i not eof FMT_FILE
Truth table0%0/3
A and not(B)
A= $n != $iB= not eof FMT_FILE
ABresult
0X0
100
111
1114 0 $null = <FMT_FILE>;
1115 0 $i++;
1116 }
1117 0 ▶ if( $m ne '' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($m ne '') { } 0 0 0
1118 0 ▶ while( $m + 1 != $i++ && !eof(FMT_FILE) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($m + 1 != $i++ and not eof FMT_FILE) 0 0 0
Condition0%0/3
$m + 1 != $i++ and not eof FMT_FILE
!ll&&!rl&&r
000
MC/DC0%0/2
$m + 1 != $i++ and not eof FMT_FILE
$m + 1 != $i++ not eof FMT_FILE
Truth table0%0/3
A and not(B)
A= $m + 1 != $i++B= not eof FMT_FILE
ABresult
0X0
100
111
1119 0 $accum .= <FMT_FILE>;
1120 }
1121 }else{
1122 0 $accum .= <FMT_FILE>;
1123 }
1124 }else{
1125 0 ▶ $accum .= $_ while( <FMT_FILE> );
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline FMT_FILE) and do { $Lisp::Fmt::accum .= $_ } 0 0 0
1126 }
1127 0 close(FMT_FILE);
1128
1129  
1130 ### directives that should not happen (errors)
1131 }elsif( $d eq ')' ){
1132 # error - no start
1133 0 return formaterror( "I see no matching ( here", $t );
1134  
1135 }elsif( $d eq '}' ){
1136 # error - no start
1137 0 return formaterror( "I see no matching { here", $t );
1138
1139 }elsif( $d eq ']' ){
1140 # error - no start
1141 0 return formaterror( "I see no matching [ here", $t );
1142  
1143 }elsif( $d eq ';' ){
1144 # no enclosing [] or <>
1145 0 return formaterror( "I see no enclosing [] or <> here", $t );
1146
1147 }else{
1148 # error - unknown
1149 0 return formaterror( "I don't know how to apply that word ($d) here.", $t);
1150 }
1151 }
1152 316 "";
1153 }
1154  
1155 ### for debugging
1156 0 ▶sub tree {
Subroutine tree: not called CC=5 SCAR=34.0
Pod undocumented
1157 0 my( $n, $t ) = @_;
1158 0 my( $nn, @t, @nn );
1159  
1160 0 @t = @{$t};
1161 0 ▶ while( @t ){
Branch: 1 branch
BranchTrueFalseTotal
if (@t) 0 0 0
1162 0 $t = shift @t;
1163
1164 0 print " " x $n, "$t->{'directive'}\n";
1165 0 ▶ tree($n+1, $t->{'body'} ) if( $t->{'body'} );
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"body"} 0 0 0
1166  
1167 0 ▶ if( $t->{'subparts'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"subparts"}) 0 0 0
1168 0 @nn = @{$t->{'subparts'}};
1169 0 ▶ while( @nn ){
Branch: 1 branch
BranchTrueFalseTotal
if (@nn) 0 0 0
1170 0 $nn = shift @nn;
1171 0 tree($n+1, $nn);
1172 0 print " " x $n, " ;\n";
1173 }
1174 }
1175 }
1176 }
1177
1178 ### compile the format spec
1179 213 ▶sub compile {
Subroutine compile: called CC=1 SCAR=0.0
Pod undocumented
1180 213 my( $fmt ) = @_;
1181  
1182 213 parse(tok( $fmt ));
1183 }
1184  
1185 ### the main entry point
1186 208 ▶sub fmt {
Subroutine fmt: called CC=2 SCAR=7.0
Pod undocumented
1187 208 my( $fmt ) = shift;
1188 208 local( @arglist ) = @_; # our arglist
1189 208 local( $argno ) = 0; # index into above
1190 208 local( $accum ) = ""; # accumulator for output string
1191
1192 208 ▶ run( ref($fmt) ? $fmt : compile($fmt));
Branch: 1 branch
BranchTrueFalseTotal
ref $fmt ? : 0 208 208
1193  
1194 208 $accum;
1195 }
1196  
1197 ### format, print to stdout
1198 0 ▶sub pfmt {
Subroutine pfmt: not called CC=1 SCAR=6.9
Pod undocumented
1199 0 print fmt(@_);
1200 }
1201  
1202 ################################################################
1203 ################################################################
1204 ################################################################
1205  
1206 =head1 NAME
1207  
1208 Lisp::Fmt - Perl module for Common Lisp like formatting
1209  
1210 =head1 SYNOPSIS
1211  
1212 use Lisp::Fmt;
1213 $str = fmt("~{~a ~5,,,'*a~}", $a,$b,$c,$d); # store result in $str
1214 pfmt("~{ ~a~5,,,'*a~}", $a,$b,$c,$d); # print to stdout
1215  
1216 =head1 DESCRIPTION
1217  
1218 The Common Lisp "format" function provides an extremely rich set of formatting
1219 directives. This module brings this to Perl.
1220  
1221 The formatting directives all begin with a C<~> and take the form:
1222 C<~[N]{,N}[@][:]X>
1223  
1224 where C<N> is a number, C<X> is a formatting directive, and C<@> and C<:> are
1225 optional modifiers. Recognized directives are: A, S, W, D, O, B, X, R, C, P,
1226 T, ~, %, |, _, ?, *, \n, {, }, (, ), [, ], <, >, ^
1227  
1228 examples:
1229
1230 C<~A> - simplest format spec, prints the arg
1231 C<~D> - prints a number in base 10
1232 C<~X> - prints a number in base 16
1233 C<~12R> - prints a number in base 12
1234 C<~@R> - prints a number in roman numerals
1235 C<~#[ none~; ~a~; ~a and ~a~:;~!{~#[~; and~] ~a~^,~}~].">
1236 - prints a list in nice readable english
1237  
1238 =head1 FORMAT SPEC
1239  
1240 as a param, a v will read the param from the arglist
1241 a # will interpolate to the number of remaining args
1242  
1243 the directive can be one of:
1244  
1245 A print the arg
1246 S print the arg in a readable form (strings are quotes,...)
1247 @ will pad on left
1248 params are: mincols (maxcols if <0), colinc, minpad, padchar, overflowchar
1249  
1250 ~ print a ~ [N ~s]
1251 % print a newline [N newlines]
1252 | print a formfeed [N formfeeds]
1253 _ print a space [N spaces]
1254 & print a newline unless already at the beginning of a line
1255 T tabulate
1256 @ relative
1257 params are: colnum, colinc
1258
1259 n ignore the newline and any following whitespace
1260 : newline is ignored, whitespace is left
1261 @ newline is printed, following whitespce is ignored
1262 * next arg is ignored, with param, next N args are ignored
1263 : back up in arg list, with param, backup N args
1264 @ goto 0th arg, or with a param, Nth arg
1265 ? indirect - 2 args are a format string and list of args
1266 @ - 1 arg - is a format string, use args from current arglist
1267
1268 P pluralize
1269 @ use y/ies
1270 : use previous arg
1271
1272 D a number in base 10
1273 O a number in base 8
1274 X a number in base 16
1275 B a number in base 2
1276 R a number in specified radix (ie. ~7R)
1277 @ print leading sign
1278 : print with commas
1279 params are: mincol, padchar, commachar, commawidth, overflowchar
1280
1281 without a radix specifier:
1282 in english "four"
1283 : in english, ordinal "fourth"
1284 @ in roman "IV"
1285 :@ in old roman "IIII"
1286  
1287  
1288 C a character
1289 @ as with write
1290 : spell out control chars
1291
1292 ( downcase until ~) - hello world
1293 @ capitalize the first word - Hello world
1294 : capitalize - Hello World
1295 :@ uppercase - HELLO WORLD
1296
1297 { iteration spec until ~}
1298 @ use remaining args
1299 : arg is list of sublists
1300 :@ use remaining args, which are sublists
1301
1302 [ conditional spec, separated with ~; ending with ~]
1303 choose item specified by arg ~:; is default item
1304 with a param, chhose with it instead of arg
1305 @ choose if not false
1306 : use first item if false, second otherwise
1307
1308 ^ abort ? {} or <> if no args remain,
1309 or if a param is given, it is 0
1310 or if 2 params are given, they are equal
1311 or if 3 params are given, the 1st is <= 2nd <= 3rd
1312 : terminate an entire :{ or :@{, not just this iteration
1313  
1314  
1315 For a more complete description of the various formatting directives, parameters, etc.
1316 see your favorite lisp reference, such as
1317 http://www.harlequin.com/education/books/HyperSpec/Body/sec_22-3.html.
1318  
1319 =head1 NOTES
1320  
1321 ! is a synonym for @
1322  
1323 Often used format strings can be pre-compiled:
1324 C<$f = Fmt::compile("~{ ~a ~5,,,'*a~}");>
1325 C<$str = fmt( $f, ...);>
1326  
1327 when lisp says an arg is a "list", we translate that as a reference to a list (or hash)
1328  
1329 lisp: (format () "~{ ~A~}\n" '(a b c d e))
1330 perl: fmt( "~{ ~A~}\n", ["a", "b", "c", "d"])
1331 fmt( "~{ key ~A value ~A\n~}", {foo=>1, bar=>2, baz=>3})
1332  
1333 =head1 BUGS
1334  
1335 Floating-point output is not yet supported.
1336  
1337 the <> formatting support is incomplete.
1338  
1339 the radix for ~R is restricted to the range 1-36
1340  
1341 no test is performed to detect circular data structures
1342  
1343 many other bugs not listed here
1344  
1345 =head1 CHANGES
1346  
1347 none.
1348  
1349 =head1 TO DO
1350  
1351 see BUGS.
1352  
1353 =head1 SEE ALSO
1354  
1355 Common Lisp - The Language 2nd. ed.
1356 L<http://www.harlequin.com/education/books/HyperSpec/Body/sec_22-3.html>
1357 Yellowstone National Park.
1358  
1359 =head1 AUTHOR
1360  
1361 Jeff Weisberg - http://www.tcp4me.com/code/
1362  
1363 =head1 COPYRIGHT
1364  
1365 This software is Copyright (c) 1998 Jeff Weisberg
1366 Permission is granted to use, copy and distribute this software
1367 under the following conditions:
1368 - This license covers the original software, as well as
1369 modified or derived works.
1370 - All modified or derived works must contain this notice
1371 unmodified and in its entirety.
1372 - This software is not to be used for any purpose which
1373 may be considered illegal, immoral, or unethical.
1374 - This software is provided as is and without warranty.
1375  
1376 =cut
1377 ;
1378  
1379 1 1;
↑ Summary