| 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
| |||||||||||||||||||||||||||||||||||
| 24 | 1 | return('', 1); | |||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||
| 26 | ▶ | elsif (not defined $width or $width < 0.0000001) { | |||||||||||||||||||||||||||||||||
|
Condition100%3/3
not defined $width or $width < "1e-07"
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"
| |||||||||||||||||||||||||||||||||||
| 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
not defined $ref_width_table or ref $ref_width_table ne "ARRAY"
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$nextline eq "" and $c eq " "
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 " "
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | # see if no separator found | ||||||||||||||||||||||||||||||||||
| 72 | 19 | ▶ | if (!$j) { # no separator, so just truncate line right here | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||