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