blib/lib/Text/WrapProp.pm

filter: Statementstmt 100% 46 / 46 Branchbran 100% 18 / 18 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 1 totaltotal 98.8% 86 / 87 CC 15 SCAR 25.9
CC
15
Cov
100%
wrap_prop
15.0
BEGIN
1.0
BEGIN
1.0
CRAP 13.4
↑ Summary
1 package Text::WrapProp;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7  
8 1 @ISA = qw(Exporter AutoLoader);
9 # Items to export into callers namespace by default. Note: do not export
10 # names by default without a very good reason. Use EXPORT_OK instead.
11 # Do not simply export all your public functions/methods/constants.
12  
13 1 @EXPORT_OK = qw( wrap_prop );
14 1 @EXPORT = qw();
15  
16 1 $VERSION = '0.05';
17  
18 1 1;
19  
20 14 ▶sub wrap_prop {
Subroutine wrap_prop: called CC=15 SCAR=27.1
Pod undocumented
21 14 my ($text, $width, $ref_width_table) = @_;
22  
23 14 ▶ if (not defined $text) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $text) { } 1 13 14
elsif (not defined $width or $width < 1e-07) { } 3 10 13
elsif (not defined $ref_width_table or ref $ref_width_table ne 'ARRAY' or scalar @{$ref_width_table;} <= 1) { } 3 7 10
elsif ($text eq '') { } 1 6 7
24 1 return('', 1);
25 }
26 ▶ elsif (not defined $width or $width < 0.0000001) {
Condition100%3/3
not defined $width or $width < "1e-07"
l!l&&r!l&&!r
1210
MC/DC100%2/2
not defined $width or $width < "1e-07"
not defined $width $width < "1e-07"
Truth table100%3/3
not(A) or B
A= not defined $widthB= $width < "1e-07"
ABresult
1X1
011
000
27 3 return('', 2);
28 }
29 8 ▶ elsif (not defined $ref_width_table or ref($ref_width_table) ne 'ARRAY' or scalar(@{$ref_width_table}) <= 1) {
Condition100%6/6
not defined $ref_width_table or ref $ref_width_table ne "ARRAY" or scalar @{$ref_width_table;} <= 1
l!l&&r!l&&!r
217
not defined $ref_width_table or ref $ref_width_table ne "ARRAY"
l!l&&r!l&&!r
118
MC/DC100%3/3
not defined $ref_width_table or ref $ref_width_table ne "ARRAY" or scalar @{$ref_width_table;} <= 1
not defined $ref_width_table ref $ref_width_table ne "ARRAY" scalar @{$ref_width_table;} <= 1
Truth table100%4/4
not(A) or B or C
A= not defined $ref_width_tableB= ref $ref_width_table ne "ARRAY"C= scalar @{$ref_width_table;} <= 1
ABCresult
1XX1
01X1
0011
0000
30 3 return('', 3);
31 }
32 elsif ($text eq '') {
33 1 return('', 0);
34 }
35  
36 6 my @width_table = @{$ref_width_table};
37  
38 # simplify whitespace, including newlines
39 6 $text =~ s/\s+/ /gs;
40  
41 6 my $c; # current character
42 6 my $ltext = length $text;
43 6 my $cursor = 0; # width so far of line
44 6 my $out = ''; # output buffer
45 6 my $nextline = '';
46  
47 6 my $i=0;
48  
49 6 ▶ while ($i < $ltext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ltext) 443 6 449
50 # pop off next character
51 443 $c = substr($text, $i++, 1);
52
53 # don't need leading spaces at start of line
54 443 ▶ next if $nextline eq '' and $c eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $nextline eq "" and $c eq " " 1 442 443
Condition100%3/3
$nextline eq "" and $c eq " "
!ll&&!rl&&r
417251
MC/DC100%2/2
$nextline eq "" and $c eq " "
$nextline eq "" $c eq " "
Truth table100%3/3
A and B
A= $nextline eq ""B= $c eq " "
ABresult
0X0
100
111
55  
56 # see if character will fit on line - but don't include if too long
57 442 ▶ if ($cursor + $width_table[ord $c] < $width + 0.0000001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor + $width_table[ord $c] < $width + 1e-07) { } 423 19 442
58 # another character fits
59 423 $nextline .= $c;
60 423 $cursor += $width_table[ord $c];
61 }
62 else {
63 # find where we can wrap by checking backwards for separator
64 19 my $j = length($nextline);
65 19 for (split //, reverse $nextline) { # find separator
66 196 $j--;
67 # last if /( |:|;|,|\.|-|\(|\)|\/)/o; # separator characters
68 196 ▶ last if /[ :;,.()\\\/-]/; # separator characters
Branch: 1 branch
BranchTrueFalseTotal
if m[[ :;,.()\\/-]] 13 183 196
69 }
70  
71 # see if no separator found
72 19 ▶ if (!$j) { # no separator, so just truncate line right here
Branch: 1 branch
BranchTrueFalseTotal
if (not $j) { } 6 13 19
73 6 $i--; # rerun on $c
74 6 $out .= $nextline."\n";
75 }
76 #
77 else { # separator found, so break line at separator
78 13 $i -= length($nextline) - $j; # rerun characters after separator
79 13 $out .= substr($nextline, 0, $j+1)."\n";
80 }
81  
82 19 $nextline = '';
83 19 $cursor = 0;
84 }
85 # print "i=$i, ltext=$ltext, cursor=$cursor, out=$out\n\n";
86 }
87  
88 6 return($out.$nextline, 0);
89 }
90  
91 __END__
↑ Summary