| 1 | package Text::Wrap::NoStrip; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 9 | 2 | our $DATE = '2023-02-18'; # DATE | |||||||||||||||||||||||||||
| 10 | 2 | our $DIST = 'Text-Wrap-NoStrip'; # DIST | |||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.003'; # VERSION | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 14 | wrap | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | our $columns = 76; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 1 | my $initial_indent = shift; | |||||||||||||||||||||||||||
| 21 | 1 | my $subsequent_indent = shift; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | my @res = ($initial_indent); | |||||||||||||||||||||||||||
| 24 | 1 | my $width = length($initial_indent); | |||||||||||||||||||||||||||
| 25 | 1 | my $si_len = length($subsequent_indent); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | for my $text (@_) { | |||||||||||||||||||||||||||
| 28 | 1 | my @chunks = split /(\R+|\s+)/, $text; | |||||||||||||||||||||||||||
| 29 | #use DD; dd \@chunks; | ||||||||||||||||||||||||||||
| 30 | 1 | for my $chunk (@chunks) { | |||||||||||||||||||||||||||
| 31 | 7 | ▶ | if ($chunk =~ /\R/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $width = 0; | |||||||||||||||||||||||||||
| 33 | 0 | push @res, $chunk; | |||||||||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||||||||
| 35 | 9 | L1: | |||||||||||||||||||||||||||
| 36 | my $len = length $chunk; | ||||||||||||||||||||||||||||
| 37 | #print "D:got chunk=<$chunk> ($len), width=$width, scalar(\@res)=".scalar(@res)."\n"; | ||||||||||||||||||||||||||||
| 38 | 9 | ▶ | if ($width + $len > $columns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # should we chop long word? | ||||||||||||||||||||||||||||
| 41 | 4 | ▶ | if ($chunk !~ /\s/ && $len > $columns - $si_len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $chunk =~ /\s/ and $len > $Text::Wrap::NoStrip::columns - $si_len
MC/DC100%2/2
not $chunk =~ /\s/ and $len > $Text::Wrap::NoStrip::columns - $si_len
not $chunk =~ /\s/
$len > $Text::Wrap::NoStrip::columns - $si_len
Truth table100%3/3
not(A) and B
A= not $chunk =~ /\s/B= $len > $Text::Wrap::NoStrip::columns - $si_len
| |||||||||||||||||||||||||||||
| 42 | 2 | my $s = substr($chunk, 0, $columns - $width); | |||||||||||||||||||||||||||
| 43 | #print "D:wrapping <$s>\n"; | ||||||||||||||||||||||||||||
| 44 | 2 | substr($chunk, 0, $columns - $width) = ""; | |||||||||||||||||||||||||||
| 45 | 2 | push @res, $s, "\n$subsequent_indent"; | |||||||||||||||||||||||||||
| 46 | 2 | $width = $si_len; | |||||||||||||||||||||||||||
| 47 | 2 | goto L1; | |||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 2 | push @res, "\n$subsequent_indent", $chunk; | |||||||||||||||||||||||||||
| 50 | 2 | $width = $len; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | #print "D:adding <$chunk>\n"; | ||||||||||||||||||||||||||||
| 54 | 5 | push @res, $chunk; | |||||||||||||||||||||||||||
| 55 | 5 | $width += $len; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | #print "D:width=$width\n"; | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | join("", @res); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | 1; | |||||||||||||||||||||||||||
| 66 | # ABSTRACT: Line wrapping without stripping the whitespace | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | __END__ | ||||||||||||||||||||||||||||