Fold.pm

filter: Statementstmt 96.7% 358 / 370 Branchbran 89.5% 188 / 210 Conditioncond 83.4% 91 / 109 MC/DCmcdc 67.2% 41 / 61 Subroutinesub 97.6% 41 / 42 Podpod 36.8% 7 / 19 totaltotal 89.5% 726 / 811 CC 145 SCAR 40.6
CC
145
Cov
92%
fold
104.5
configure
12.9
chops
8.0
CRAP 57.9
↑ Summary Fold/Japanese/Wikipedia.pm »
1 package Text::ANSI::Fold;
2  
3 19 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 19 our $VERSION = "2.40";
8  
9 19 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 {
11 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶ *Data::Dumper::qquote = sub { qq["${\(shift)}"] };
Subroutine __ANON__: not called CC=1 SCAR=2.6
13 19 $Data::Dumper::Useperl = 1;
14 19 $Data::Dumper::Sortkeys = 1;
15 }
16 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use List::Util qw(pairmap pairgrep);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 ▶use Text::VisualWidth::PP 'vwidth';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 ######################################################################
22 19 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 19 our %EXPORT_TAGS = (
24 constants => [ qw(
25 &LINEBREAK_NONE
26 &LINEBREAK_ALL
27 &LINEBREAK_RUNIN
28 &LINEBREAK_RUNOUT
29 ) ],
30 regex => [ qw(
31 $reset_re
32 $color_re
33 $erase_re
34 $csi_re
35 $osc_re
36 $ctrl_string_re
37 ) ],
38 );
39  
40 our @EXPORT_OK = ( qw(&ansi_fold),
41 19 @{$EXPORT_TAGS{constants}},
42 19 @{$EXPORT_TAGS{regex}},
43 );
44  
45 472 ▶sub ansi_fold {
Subroutine ansi_fold: called CC=1 SCAR=0.0
Pod documented
46 472 my($text, $width, @option) = @_;
47 472 __PACKAGE__->fold($text, width => $width, @option);
48 }
49 ######################################################################
50  
51 # \p{Hangul} is Script_Extensions, which takes CJK punctuation as well
52 19 our $alphanum_re = qr{ [_\d\p{Latin}\p{Greek}\p{Cyrillic}\p{sc=Hangul}] }x;
53 19 our $nonspace_re = qr{ \p{IsPrintableLatin} }x;
54 19 our $reset_re = qr{ \e \[ [0;]* m }x;
55 19 our $color_re = qr{ \e \[ [\d:;]* m }x;
56 19 our $erase_re = qr{ \e \[ [\d:;]* K }x;
57  
58 # see ECMA-48 5.4 Control sequences
59 19 my $csi_start = qr{ (?: \e\[ | \x9b ) }x;
60 19 my $csi_parameter = qr/[\x30-\x3f]*+/;
61 19 my $csi_itermidiate = qr/[\x20-\x2f]*+/;
62 19 my $csi_final = qr/[\x40-\x7e]/;
63 19 our $csi_body_re = qr/${csi_start}${csi_parameter}${csi_itermidiate}/;
64 19 our $csi_re = qr/${csi_start}${csi_parameter}${csi_itermidiate}${csi_final}/;
65 # CSI other than SGR (cursor movement and the like)
66 19 my $csi_other_re = qr/${csi_body_re}[\x40-\x6c\x6e-\x7e]/;
67  
68 19 our $osc_re = qr{
69 # see ECMA-48 8.3.89 OSC - OPERATING SYSTEM COMMAND
70 # Extended to accept non-ASCII (undefined but tolerated)
71 (?: \e\] | \x9d ) # osc
72 [^\x00-\x07\x0e-\x1f\x7f-\x9f]*+ # command (excludes C1 controls)
73 (?: \e\\ | \x9c | \a ) # st: string terminator
74 }x;
75  
76 19 our $ctrl_string_re = qr{
77 # the control strings of ECMA-48 (5.6): DCS (8.3.27), OSC (8.3.89),
78 # SOS (8.3.128), PM (8.3.94) and APC (8.3.2)
79 (?: \e[P\]^_X] | [\x90\x98\x9d\x9e\x9f] ) # dcs, osc, pm, apc, sos
80 [^\x00-\x07\x0e-\x1f\x7f-\x9f]*+ # string (excludes C1 controls)
81 (?: \e\\ | \x9c | \a ) # st: string terminator
82 }x;
83  
84 19 ▶use constant SGR_RESET => "\e[m";
Subroutine BEGIN: called CC=1 SCAR=0.0
85 19 ▶use constant OSC8_RESET => "\e]8;;\e\\";
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 246 ▶sub pwidth { vwidth $_[0] =~ s/\X\cH{1,2}//gr =~ s/${ctrl_string_re}|${csi_re}//gr }
Subroutine pwidth: called CC=1 SCAR=0.0
Pod undocumented
88  
89 1 ▶sub IsPrintableLatin {
Subroutine IsPrintableLatin: called CC=1 SCAR=0.0
Pod undocumented
90 1 return <<"END";
91 +utf8::ASCII
92 +utf8::Latin
93 -utf8::White_Space
94 END
95 }
96  
97 19 ▶sub IsWideSpacing {
Subroutine IsWideSpacing: called CC=1 SCAR=0.0
Pod undocumented
98 19 return <<"END";
99 +utf8::East_Asian_Width=Wide
100 +utf8::East_Asian_Width=FullWidth
101 -utf8::Nonspacing_Mark
102 -utf8::Default_Ignorable_Code_Point
103 END
104 }
105  
106 19 ▶sub IsWideAmbiguousSpacing {
Subroutine IsWideAmbiguousSpacing: called CC=1 SCAR=0.0
Pod undocumented
107 19 return <<"END";
108 +utf8::East_Asian_Width=Wide
109 +utf8::East_Asian_Width=FullWidth
110 +utf8::East_Asian_Width=Ambiguous
111 -utf8::Nonspacing_Mark
112 -utf8::Default_Ignorable_Code_Point
113 END
114 }
115  
116 536 ▶sub _startWideSpacing {
Subroutine _startWideSpacing: called CC=2 SCAR=6.9
117 # look at $_
118 536 ▶ if ($Text::VisualWidth::PP::EastAsian) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::VisualWidth::PP::EastAsian) { } 2 534 536
119 2 /^\p{IsWideAmbiguousSpacing}/;
120 } else {
121 534 /^\p{IsWideSpacing}/;
122 }
123 }
124  
125 19 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
126 19 LINEBREAK_NONE => 0,
127 LINEBREAK_RUNIN => 1,
128 LINEBREAK_RUNOUT => 2,
129 LINEBREAK_ALL => 3,
130 19 };
131  
132 19 our $DEFAULT_LINEBREAK = LINEBREAK_NONE;
133 19 our $DEFAULT_RUNIN_WIDTH = 2;
134 19 our $DEFAULT_RUNOUT_WIDTH = 2;
135  
136 19 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.5
137 19 ▶ if ($] < 5.016) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.016") 0 19 19
138 0 require charnames;
139 0 charnames->import(':full');
140 }
141 }
142  
143 our %TABSTYLE = (
144 437 ▶ pairmap {
Subroutine __ANON__: called CC=2 SCAR=6.9
145 437 ▶ ( $a =~ s/_/-/gr => ref $b ? $b : [ $b, $b ] );
Branch: 1 branch
BranchTrueFalseTotal
ref $b ? : 114 323 437
146 }
147 19 ▶ symbol => [ "\N{SYMBOL FOR HORIZONTAL TABULATION}", # ␉
Subroutine BEGIN: called CC=1 SCAR=0.0
148 "\N{SYMBOL FOR SPACE}" ], # ␠
149 shade => [ "\N{MEDIUM SHADE}", # ▒
150 "\N{LIGHT SHADE}" ], # ░
151 block => [ "\N{LOWER ONE QUARTER BLOCK}", # ▂
152 "\N{LOWER ONE EIGHTH BLOCK}" ], # ▁
153 needle => [ "\N{BOX DRAWINGS HEAVY RIGHT}", # ╺
154 "\N{BOX DRAWINGS LIGHT HORIZONTAL}" ], # ─
155 dash => [ "\N{BOX DRAWINGS HEAVY RIGHT}", # ╺
156 "\N{BOX DRAWINGS LIGHT DOUBLE DASH HORIZONTAL}" ], # ╌
157 triangle => [ "\N{BLACK RIGHT-POINTING SMALL TRIANGLE}", # ▸
158 "\N{WHITE RIGHT-POINTING SMALL TRIANGLE}" ], # ▹
159  
160 dot => '.',
161 space => ' ',
162 emspace => "\N{EM SPACE}", #  
163 blank => "\N{OPEN BOX}", # ␣
164 middle_dot => "\N{MIDDLE DOT}", # ·
165 arrow => "\N{RIGHTWARDS ARROW}", # →
166 double_arrow => "\N{RIGHTWARDS DOUBLE ARROW}", # ⇒
167 triple_arrow => "\N{RIGHTWARDS TRIPLE ARROW}", # ⇛
168 white_arrow => "\N{RIGHTWARDS WHITE ARROW}", # ⇨
169 wave_arrow => "\N{RIGHTWARDS WAVE ARROW}", # ↝
170 circle_arrow => "\N{CIRCLED HEAVY WHITE RIGHTWARDS ARROW}", # ➲
171 curved_arrow => "\N{HEAVY BLACK CURVED DOWNWARDS AND RIGHTWARDS ARROW}",# ➥
172 shadow_arrow => "\N{HEAVY UPPER RIGHT-SHADOWED WHITE RIGHTWARDS ARROW}",# ➮
173 squat_arrow => "\N{SQUAT BLACK RIGHTWARDS ARROW}", # ➧
174 squiggle => "\N{RIGHTWARDS SQUIGGLE ARROW}", # ⇝
175 harpoon => "\N{RIGHTWARDS HARPOON WITH BARB UPWARDS}", # ⇀
176 cuneiform => "\N{CUNEIFORM SIGN TAB}", # 𒋰
177  
178 );
179  
180 19 for my $alias (
181 [ bar => 'needle'],
182 [ pin => 'needle']
183 ) {
184 38 $TABSTYLE{$alias->[0]} = $TABSTYLE{$alias->[1]};
185 }
186  
187 19 my @default = (
188 text => '',
189 width => undef,
190 padding => 0,
191 trim => 0,
192 boundary => '',
193 padchar => ' ',
194 prefix => '',
195 ambiguous => 'narrow',
196 margin => 0,
197 linebreak => $DEFAULT_LINEBREAK,
198 runin => $DEFAULT_RUNIN_WIDTH,
199 runout => $DEFAULT_RUNOUT_WIDTH,
200 expand => 0,
201 tabstop => 8,
202 tabhead => ' ',
203 tabspace => ' ',
204 discard => {},
205 splitwide => 0,
206 lefthalf => "\N{NO-BREAK SPACE}",
207 righthalf => "\N{NO-BREAK SPACE}",
208 );
209  
210 307 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
211 307 my $class = shift;
212 307 my $obj = bless { @default }, $class;
213 307 ▶ $obj->configure(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 292 15 307
214 307 $obj;
215 }
216  
217 INTERNAL_METHODS: {
218 1825 ▶ sub spawn {
Subroutine spawn: called CC=1 SCAR=0.0
Pod undocumented
219 1825 my $obj = shift;
220 1825 my $class = ref $obj;
221 1825 ▶ my %new = ( %$obj, pairgrep { defined $b } @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
222 1825 bless \%new, $class;
223 }
224 1825 ▶ sub do_runin { $_[0]->{linebreak} & LINEBREAK_RUNIN && $_[0]->{runin} > 0 }
Subroutine do_runin: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"linebreak"} & 1 and $_[0]{"runin"} > 0 159 1666 1825
225 1411 ▶ sub do_runout { $_[0]->{linebreak} & LINEBREAK_RUNOUT && $_[0]->{runout} > 0 }
Subroutine do_runout: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"linebreak"} & 2 and $_[0]{"runout"} > 0 138 1273 1411
226 }
227  
228 19 ▶use Text::ANSI::Fold::Japanese::W3C qw(%prohibition);
Subroutine BEGIN: called CC=1 SCAR=0.0
229  
230 38 ▶sub chars_to_regex {
Subroutine chars_to_regex: called CC=4 SCAR=13.9
Pod undocumented
231 38 my $chars = join '', @_;
232 38 my($c, @s);
233 38 for ($chars =~ /\X/g) {
234 2945 ▶ if (length == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) { } 2926 19 2945
235 2926 $c .= $_;
236 } else {
237 19 push @s, $_;
238 }
239 }
240 38 ▶ if (@s) {
Branch: 1 branch
BranchTrueFalseTotal
if (@s) { } 19 19 38
241 19 local $" = '|';
242 19 qr/(?:[\Q$c\E]|@s)/;
243 } else {
244 19 qr/[\Q$c\E]/;
245 }
246 }
247  
248 19 my %prohibition_re = do {
249 head => do {
250 19 my $re = chars_to_regex @prohibition{qw(head postfix)};
251 19 qr/(?: $re | \p{Space_Separator} )/x;
252 },
253 19 end => chars_to_regex @prohibition{qw(end prefix)},
254 };
255  
256 358 ▶sub configure {
Subroutine configure: called CC=12 SCAR=25.6
Pod documented
257 358 my $obj = shift;
258 358 ▶ if (not ref $obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $obj) 15 343 358
259 15 $obj = state $private = __PACKAGE__->new;
260 }
261 358 ▶ croak "invalid parameter" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 358 358
262 358 ▶ while (@_ >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 478 358 836
263 478 my($a, $b) = splice @_, 0, 2;
264  
265 478 ▶ if ($a eq 'tabstyle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($a eq "tabstyle") 1 477 478
266 1 ▶ $b // next;
Condition50%1/2
$b // (next)
l!l
10
MC/DC0%0/1
$b // (next)
$b
Truth table50%1/2
A
A= $b
Aresult
11
00
267 1 ▶ my($h, $s) = $b =~ /([-\w]+)/g or croak "$b: invalid tabstyle";
Branch: 1 branch
BranchTrueFalseTotal
() = $b =~ /([-\w]+)/gu or croak("${b}: invalid tabstyle") 1 0 1
268 1 ▶ $s ||= $h;
Condition33%1/3
$s ||= $h
l!l&&r!l&&!r
010
MC/DC0%0/2
$s ||= $h
$s $h
Truth table33%1/3
A ||= B
A= $sB= $h
ABresult
1X1
011
000
269 my %style = (
270 h => ($TABSTYLE{$h} or croak "$h: invalid tabstyle"),
271 1 ▶ s => ($TABSTYLE{$s} or croak "$s: invalid tabstyle"),
Condition50%2/4
$Text::ANSI::Fold::TABSTYLE{$h} || croak("${h}: invalid tabstyle")
l!l
10
$Text::ANSI::Fold::TABSTYLE{$s} || croak("${s}: invalid tabstyle")
l!l
10
MC/DC0%0/1
$Text::ANSI::Fold::TABSTYLE{$h} || croak("${h}: invalid tabstyle")
$Text::ANSI::Fold::TABSTYLE{$h}
MC/DC0%0/1
$Text::ANSI::Fold::TABSTYLE{$s} || croak("${s}: invalid tabstyle")
$Text::ANSI::Fold::TABSTYLE{$s}
Truth table50%1/2
A
A= $Text::ANSI::Fold::TABSTYLE{$h}
Aresult
11
00
Truth table50%1/2
A
A= $Text::ANSI::Fold::TABSTYLE{$s}
Aresult
11
00
272 );
273 unshift @_,
274 tabhead => $style{h}->[0],
275 1 tabspace => $style{s}->[1];
276 1 next;
277 }
278  
279 477 ▶ croak "$a: invalid parameter" if not exists $obj->{$a};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{$a} 0 477 477
280 477 $obj->{$a} = $b;
281 }
282 358 ▶ if (ref $obj->{discard} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{"discard"} eq "ARRAY") 2 356 358
283 2 $obj->{discard} = { map { uc $_ => 1 } @{$obj->{discard}} };
284 }
285 358 $obj;
286 }
287  
288 19 my @color_stack;
289 my @bg_stack;
290 19 my @reset;
291 19 my $osc8_link;
292 171 ▶sub put_reset { @reset = shift };
Subroutine put_reset: called CC=1 SCAR=0.0
Pod undocumented
293 215 ▶sub pop_reset {
Subroutine pop_reset: called CC=2 SCAR=6.9
Pod undocumented
294 215 ▶ @reset ? do { @color_stack = (); pop @reset } : '';
Branch: 1 branch
BranchTrueFalseTotal
@reset ? : 171 44 215
295 }
296  
297 19 ▶use constant MAX_INT => ~0 >> 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
298  
299 # grapheme cluster boundary, from perl 5.22
300 19 ▶my $gcb = $] >= 5.022 ? '\b{gcb}' : '';
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.022 ? : 19 0 19
301  
302 19 ▶sub IsEOL {
Subroutine IsEOL: called CC=1 SCAR=0.0
Pod undocumented
303 19 <<"END";
304 0000\t0000
305 000A\t000D
306 2028\t2029
307 END
308 }
309  
310 1825 ▶sub fold {
Subroutine fold: called CC=99 SCAR=46.5
Pod documented
311 1825 my $obj = shift;
312 1825 ▶ local $_ = shift // '';
Condition50%1/2
shift() // ""
l!l
18250
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
313  
314 1825 ▶ if (not ref $obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $obj) 472 1353 1825
315 472 $obj = state $private = configure();
316 }
317 1825 my $opt = $obj->spawn(splice @_);
318  
319 1825 my $width = $opt->{width};
320 1825 ▶ looks_like_number $width and $width == int($width)
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($width) and $width == int $width or croak("${width}: invalid width") 1825 0 1825
Condition33%1/3
&looks_like_number($width) and $width == int $width
!ll&&!rl&&r
001825
MC/DC0%0/2
&looks_like_number($width) and $width == int $width
&looks_like_number($width) $width == int $width
Truth table33%1/3
A and B
A= &looks_like_number($width)B= $width == int $width
ABresult
0X0
100
111
321 or croak "$width: invalid width";
322 1825 ▶ $opt->{padding} = 0 if $width <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $width <= 0 28 1797 1825
323 1825 my $unlimited = $width < 0;
324 1825 ▶ $width = MAX_INT if $unlimited;
Branch: 1 branch
BranchTrueFalseTotal
if $unlimited 28 1797 1825
325 1825 ▶ ($width -= $opt->{margin}) > 0 or croak "margin too big";
Branch: 1 branch
BranchTrueFalseTotal
($width -= $opt->{"margin"}) > 0 or croak("margin too big") 1825 0 1825
326  
327 my $word_char_re =
328 { word => $alphanum_re, space => $nonspace_re }
329 1825 ▶ ->{$opt->{boundary} // ''};
Condition50%1/2
$opt->{"boundary"} // ""
l!l
18250
MC/DC0%0/1
$opt->{"boundary"} // ""
$opt->{"boundary"}
Truth table50%1/2
A
A= $opt->{"boundary"}
Aresult
11
00
330  
331 1825 local $Text::VisualWidth::PP::EastAsian = $opt->{ambiguous} eq 'wide';
332  
333 # The prefix put at the head of this text by the previous call does
334 # not count as progress: there must be something after it.
335 1825 ▶ my $prefix_width = $opt->{prefix} eq '' ? 0 : pwidth($opt->{prefix});
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'prefix'} eq '' ? : 1780 45 1825
336 1825 ▶ my $head_width = (not ref $opt->{prefix} and index($_, $opt->{prefix}) == 0)
Branch: 1 branch
BranchTrueFalseTotal
!ref($opt->{'prefix'}) && index($_, $opt->{'prefix'}) == 0 ? : 1814 11 1825
Condition100%3/3
not ref $opt->{"prefix"} and index($_, $opt->{"prefix"}) == 0
!ll&&!rl&&r
561814
MC/DC100%2/2
not ref $opt->{"prefix"} and index($_, $opt->{"prefix"}) == 0
not ref $opt->{"prefix"} index($_, $opt->{"prefix"}) == 0
Truth table100%3/3
not(A) and B
A= not ref $opt->{"prefix"}B= index($_, $opt->{"prefix"}) == 0
ABresult
0X0
100
111
337 ? $prefix_width : 0;
338 # the room of a line with nothing after the prefix yet
339 1825 ▶ my $head_room = $width > $head_width ? $width - $head_width : $width;
Branch: 1 branch
BranchTrueFalseTotal
$width > $head_width ? : 1825 0 1825
340  
341 1825 my $folded = '';
342 1825 my $eol = '';
343 1825 my $room = $width;
344 1825 my $split = 0;
345 1825 @bg_stack = @color_stack = @reset = ();
346 1825 $osc8_link = undef;
347 # a character with no special handling, and not overstruck
348 my $unremarkable_re =
349 1825 ▶ $opt->{expand} ? qr/[^\p{IsEOL}\e\t](?!\cH)/
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'expand'} ? : 329 1496 1825
350 : qr/[^\p{IsEOL}\e](?!\cH)/;
351  
352 FOLD:
353 1825 ▶ while (length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 7540 404 7944
354  
355 # newline, null, vt
356 # U+2028: Line Separator
357 # U+2029: Paragraph Separator
358 7540 ▶ if (s/\A(\r*\n|[\0\x0b\N{U+2028}\N{U+2029}])//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A(\r*\n|[\0\x0b\N{U+2028}\N{U+2029}])//u) 22 7518 7540
359 22 $eol = $1;
360 22 last;
361 }
362 # form feed
363 # It is at the beginning when no text is before it: sequences
364 # such as colors do not count, or a form feed in a color would
365 # stay in the rest forever.
366 7518 ▶ if (m/\A(\f+)/p) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A(\f+)/pu) 11 7507 7518
367 11 my($ff, $post) = ($1, ${^POSTMATCH});
368 11 ▶ last if $folded =~ s/(?:${csi_re}|${ctrl_string_re}|\f)//gr ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $folded =~ s/(?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re|\f)//gru ne "" 3 8 11
369 8 ($folded, $_) = ($folded . $ff, $post);
370 8 next;
371 }
372 # carriage return
373 7507 ▶ if (s/\A(\r+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A(\r+)//u) 2 7505 7507
374 2 ▶ if (length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 0) { } 0 2 2
375 # this must be the part of CRNL
376 0 $eol = $1;
377 0 last;
378 } else {
379 2 $folded .= $1;
380 2 $room = $width;
381 2 next;
382 }
383 }
384 # ECMA-48 control strings: DCS, OSC, SOS, PM and APC
385 7505 ▶ if (s/\A($ctrl_string_re)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A($Text::ANSI::Fold::ctrl_string_re)//u) 88 7417 7505
386 88 my $str = $1;
387 88 my $osc = $str =~ /^(?:\e\]|\x9d)/;
388 88 ▶ unless ($obj->{discard}->{CONTROL} or $osc && $obj->{discard}->{OSC}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{"discard"}{"CONTROL"} or $osc and $obj->{"discard"}{"OSC"}) 76 12 88
Condition100%6/6
$obj->{"discard"}{"CONTROL"} or $osc and $obj->{"discard"}{"OSC"}
l!l&&r!l&&!r
7576
$osc and $obj->{"discard"}{"OSC"}
!ll&&!rl&&r
25515
MC/DC100%3/3
$obj->{"discard"}{"CONTROL"} or $osc and $obj->{"discard"}{"OSC"}
$obj->{"discard"}{"CONTROL"} $osc $obj->{"discard"}{"OSC"}
Truth table100%4/4
A or B and C
A= $obj->{"discard"}{"CONTROL"}B= $oscC= $obj->{"discard"}{"OSC"}
ABCresult
1XX1
0111
00X0
0100
389 76 $folded .= $str;
390 # OSC 8 hyperlink: reopened on the next line
391 76 ▶ if ($str =~ /^(?:\e\]|\x9d)8;[^;]*;(.*?)(?:\e\\|\x9c|\a)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(?:\e\]|\x9d)8;[^;]*;(.*?)(?:\e\\|\x9c|\a)$/u) 46 30 76
392 46 ▶ $osc8_link = $1 ne '' ? $str : undef;
Branch: 1 branch
BranchTrueFalseTotal
$1 ne '' ? : 31 15 46
393 }
394 }
395 88 next;
396 }
397 # erase line (assume 0)
398 7417 ▶ if (s/\A($erase_re)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A($Text::ANSI::Fold::erase_re)//u) 6 7411 7417
399 6 ▶ $folded .= $1 unless $obj->{discard}->{EL};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"discard"}{"EL"} 6 0 6
400 6 @bg_stack = grep { !/$erase_re/ } @color_stack;
401 6 push @color_stack, $1;
402 6 next;
403 }
404 # reset
405 7411 ▶ if (s/\A($reset_re+($erase_re*))//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A($Text::ANSI::Fold::reset_re+($Text::ANSI::Fold::erase_re*))//u) 171 7240 7411
406 171 put_reset($1);
407 171 ▶ @bg_stack = () if $2;
Branch: 1 branch
BranchTrueFalseTotal
if $2 0 171 171
408 171 next;
409 }
410  
411 # sequences with no visible text after them stay on this line
412 7240 ▶ last if $room < 1
Branch: 1 branch
BranchTrueFalseTotal
if $room < 1 and not /\A(?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)*+(?:\z|\p{IsEOL})/u 1094 6146 7240
Condition100%3/3
$room < 1 and not /\A(?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)*+(?:\z|\p{IsEOL})/u
!ll&&!rl&&r
614151094
MC/DC100%2/2
$room < 1 and not /\A(?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)*+(?:\z|\p{IsEOL})/u
$room < 1 not /\A(?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)*+(?:\z|\p{IsEOL})/u
Truth table100%3/3
A and not(B)
A= $room < 1B= not /\A(?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)*+(?:\z|\p{IsEOL})/u
ABresult
0X0
100
111
413 and not /\A(?:${csi_re}|${ctrl_string_re})*+(?:\z|\p{IsEOL})/;
414 6146 ▶ if ($room < 2 and !$opt->{splitwide}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($room < 2 and not $opt->{"splitwide"}) 605 5541 6146
Condition66%2/3
$room < 2 and not $opt->{"splitwide"}
!ll&&!rl&&r
55410605
MC/DC50%1/2
$room < 2 and not $opt->{"splitwide"}
$room < 2 not $opt->{"splitwide"}
Truth table66%2/3
A and not(B)
A= $room < 2B= not $opt->{"splitwide"}
ABresult
0X0
100
111
415 605 ▶ last if $room != $head_room and &_startWideSpacing;
Branch: 1 branch
BranchTrueFalseTotal
if $room != $head_room and &_startWideSpacing 282 323 605
Condition100%3/3
$room != $head_room and &_startWideSpacing
!ll&&!rl&&r
69254282
MC/DC100%2/2
$room != $head_room and &_startWideSpacing
$room != $head_room &_startWideSpacing
Truth table100%3/3
A and B
A= $room != $head_roomB= &_startWideSpacing
ABresult
0X0
100
111
416 }
417  
418 5864 ▶ if (@reset) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reset) 114 5750 5864
419 114 $folded .= pop_reset();
420 }
421  
422 # ANSI color sequence
423 5864 ▶ if (s/\A($color_re)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A($Text::ANSI::Fold::color_re)//u) 241 5623 5864
424 241 $folded .= $1;
425 241 push @color_stack, $1;
426 241 next;
427 }
428  
429 # other CSI: no width, the cursor movement is not counted; at
430 # the folding point it goes with the text after it, as color does
431 5623 ▶ if (s/\A($csi_other_re)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A($csi_other_re)//u) 15 5608 5623
432 15 $folded .= $1;
433 15 next;
434 }
435  
436 # imcomplete CSI
437 5608 ▶ if (s/\A(\e+|$csi_body_re)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A(\e+|$Text::ANSI::Fold::csi_body_re)\z//u) 3 5605 5608
438 3 $folded .= $1;
439 3 last;
440 }
441  
442 # tab
443 5605 ▶ if ($opt->{expand} and s/\A\t//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"expand"} and s/\A\t//u) 112 5493 5605
Condition100%3/3
$opt->{"expand"} and s/\A\t//u
!ll&&!rl&&r
5035458112
MC/DC100%2/2
$opt->{"expand"} and s/\A\t//u
$opt->{"expand"} s/\A\t//u
Truth table100%3/3
A and B
A= $opt->{"expand"}B= s/\A\t//u
ABresult
0X0
100
111
444 112 my $space = $opt->{tabstop} - ($width - $room) % $opt->{tabstop};
445 112 $_ = $opt->{tabhead} . $opt->{tabspace} x ($space - 1) . $_;
446 112 next;
447 }
448  
449 # backspace
450 5493 my $bs = 0;
451 5493 ▶ while (m/\A(?:\X\cH+)++(?<c>\X|\z)/p) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A(?:\X\cH+)++(?<c>\X|\z)/pu) 850 5440 6290
452 850 my $w = vwidth($+{c});
453 850 ▶ last FOLD if $w > $room and $room != $width;
Branch: 1 branch
BranchTrueFalseTotal
if $w > $room and $room != $width 4 846 850
Condition100%3/3
$w > $room and $room != $width
!ll&&!rl&&r
84064
MC/DC100%2/2
$w > $room and $room != $width
$w > $room $room != $width
Truth table100%3/3
A and B
A= $w > $roomB= $room != $width
ABresult
0X0
100
111
454 846 $folded .= ${^MATCH};
455 846 $_ = ${^POSTMATCH};
456 846 $room -= $w;
457 846 $bs++;
458 846 ▶ last FOLD if $room < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $room < 0 6 840 846
459 840 ▶ last if $room < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $room < 1 43 797 840
460 }
461 5483 ▶ next if $bs;
Branch: 1 branch
BranchTrueFalseTotal
if $bs 62 5421 5483
462  
463 # consume unremarkable characters
464 # no more than the room can take, so as not to scan the rest of
465 # a long paragraph for every line; no limit for an unlimited
466 # width, or beyond what a regex quantifier allows
467 5421 my $max = do {
468 5421 ▶ if ($unlimited) {
Branch: 2 branches
BranchTrueFalseTotal
if ($unlimited) { } 39 5382 5421
elsif ((my $n = $room * 2 + 8) > 32766) { } 0 5382 5382
469 39 '';
470 } elsif ((my $n = $room * 2 + 8) > 32766) {
471 0 '';
472 } else {
473 5382 $n;
474 }
475 };
476 5421 ▶ if (s{\A(
Branch: 1 branch
BranchTrueFalseTotal
if (s/\A( \e+ # escapes | # or ${unremarkable_re}{1,$max}+ # up to the limit $unremarkable_re*? $gcb # and to the end of the grapheme )//ux) { } 5421 0 5421
477 \e+ # escapes
478 | # or
479 ${unremarkable_re}{1,$max}+ # up to the limit
480 ${unremarkable_re}*? $gcb # and to the end of the grapheme
481 )}{}x) {
482 5421 my $s = $1;
483 # simple_fold looks only as far as the room, while vwidth
484 # would measure the whole run, which can be the rest of a
485 # long paragraph
486 5421 my($a, $b, $w) = simple_fold($s, $room);
487 5421 ▶ if ($b eq '' and $w <= $room) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b eq "" and $w <= $room) 4446 975 5421
Condition100%3/3
$b eq "" and $w <= $room
!ll&&!rl&&r
97144446
MC/DC100%2/2
$b eq "" and $w <= $room
$b eq "" $w <= $room
Truth table100%3/3
A and B
A= $b eq ""B= $w <= $room
ABresult
0X0
100
111
488 4446 $folded .= $s;
489 4446 $room -= $w;
490 4446 next;
491 }
492 975 ▶ if ($opt->{splitwide}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"splitwide"}) 3 972 975
493 3 ▶ if ($w == $room - 1 && $b =~ /\A\p{IsWideSpacing}/p) {
Branch: 2 branches
BranchTrueFalseTotal
if ($w == $room - 1 and $b =~ /\A\p{IsWideSpacing}/pu) { } 3 0 3
elsif ($w > $room) { } 0 0 0
Condition33%1/3
$w == $room - 1 and $b =~ /\A\p{IsWideSpacing}/pu
!ll&&!rl&&r
003
MC/DC0%0/2
$w == $room - 1 and $b =~ /\A\p{IsWideSpacing}/pu
$w == $room - 1 $b =~ /\A\p{IsWideSpacing}/pu
Truth table33%1/3
A and B
A= $w == $room - 1B= $b =~ /\A\p{IsWideSpacing}/pu
ABresult
0X0
100
111
494 3 $a .= $opt->{lefthalf};
495 3 $b = $opt->{righthalf} . ${^POSTMATCH};
496 3 $w++;
497 3 $split = 1;
498 }
499 elsif ($w > $room) {
500 0 $a = $opt->{lefthalf};
501 0 $b = $opt->{righthalf} . $b;
502 0 $w--;
503 0 $split = 1;
504 }
505 }
506 975 ▶ if ($w > $room && $room != $head_room) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w > $room and $room != $head_room) 7 968 975
Condition100%3/3
$w > $room and $room != $head_room
!ll&&!rl&&r
955137
MC/DC100%2/2
$w > $room and $room != $head_room
$w > $room $room != $head_room
Truth table100%3/3
A and B
A= $w > $roomB= $room != $head_room
ABresult
0X0
100
111
507 7 $_ = $s . $_;
508 7 last;
509 }
510 968 ($folded, $_) = ($folded . $a, $b . $_);
511 968 $room -= $w;
512 } else {
513 0 die "panic ($_)";
514 }
515 }
516  
517 ##
518 ## --boundary=word
519 ##
520 1825 ▶ if ($word_char_re
Branch: 1 branch
BranchTrueFalseTotal
if ($word_char_re and my($w2) = /\A( (?: $word_char_re \cH ? ) + )/ux and () = $folded =~ m[ \A ## avoid CSI/OSC final char making a word ( (?: [^\e\x90\x98\x9b\x9d\x9e\x9f]* (?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)++ ) *+ .*? ) ( (?: $word_char_re \cH ? ) + ) \z ]ux) 50 1775 1825
Condition100%6/6
$word_char_re and my($w2) = /\A( (?: $word_char_re \cH ? ) + )/ux and () = $folded =~ m[ \A ## avoid CSI/OSC final char making a word ( (?: [^\e\x90\x98\x9b\x9d\x9e\x9f]* (?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)++ ) *+ .*? ) ( (?: $word_char_re \cH ? ) + ) \z ]ux
!ll&&!rl&&r
1772350
$word_char_re and my($w2) = /\A( (?: $word_char_re \cH ? ) + )/ux
!ll&&!rl&&r
17502253
MC/DC100%3/3
$word_char_re and my($w2) = /\A( (?: $word_char_re \cH ? ) + )/ux and () = $folded =~ m[ \A ## avoid CSI/OSC final char making a word ( (?: [^\e\x90\x98\x9b\x9d\x9e\x9f]* (?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)++ ) *+ .*? ) ( (?: $word_char_re \cH ? ) + ) \z ]ux
$word_char_re my($w2) = /\A( (?: $word_char_re \cH ? ) + )/ux () = $folded =~ m[ \A ## avoid CSI/OSC final char making a word ( (?: [^\e\x90\x98\x9b\x9d\x9e\x9f]* (?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)++ ) *+ .*? ) ( (?: $word_char_re \cH ? ) + ) \z ]ux
Truth table100%4/4
A and B and C
A= $word_char_reB= my($w2) = /\A( (?: $word_char_re \cH ? ) + )/uxC= () = $folded =~ m[ \A ## avoid CSI/OSC final char making a word ( (?: [^\e\x90\x98\x9b\x9d\x9e\x9f]* (?:$Text::ANSI::Fold::csi_re|$Text::ANSI::Fold::ctrl_string_re)++ ) *+ .*? ) ( (?: $word_char_re \cH ? ) + ) \z ]ux
ABCresult
0XX0
10X0
1100
1111
521 and my($w2) = /\A( (?: ${word_char_re} \cH ? ) + )/x
522 and my($lead, $w1) = $folded =~ m{
523 \A ## avoid CSI/OSC final char making a word
524 ( (?: [^\e\x90\x98\x9b\x9d\x9e\x9f]* (?:${csi_re}|${ctrl_string_re})++ ) *+ .*? )
525 ( (?: ${word_char_re} \cH ? ) + )
526 \z }x
527 ) {
528 ## Break line before word only when enough space will be
529 ## provided for the word in the next turn.
530 50 my $l = pwidth($w1);
531 50 my $next = $width - $prefix_width;
532 50 ▶ if ($room + $l < $next and $l + pwidth($w2) <= $next
Branch: 1 branch
BranchTrueFalseTotal
if ($room + $l < $next and $l + pwidth($w2) <= $next and pwidth($lead) > $head_width) 44 6 50
Condition66%4/6
$room + $l < $next and $l + pwidth($w2) <= $next and pwidth($lead) > $head_width
!ll&&!rl&&r
6044
$room + $l < $next and $l + pwidth($w2) <= $next
!ll&&!rl&&r
0644
MC/DC33%1/3
$room + $l < $next and $l + pwidth($w2) <= $next and pwidth($lead) > $head_width
$room + $l < $next $l + pwidth($w2) <= $next pwidth($lead) > $head_width
Truth table50%2/4
A and B and C
A= $room + $l < $nextB= $l + pwidth($w2) <= $nextC= pwidth($lead) > $head_width
ABCresult
0XX0
10X0
1100
1111
533 and pwidth($lead) > $head_width) {
534 44 $folded = $lead;
535 44 $_ = $w1 . pop_reset() . $_;
536 44 $room += $l;
537 }
538 }
539  
540 ##
541 ## RUN-OUT
542 ##
543 1825 ▶ if ($_ ne '' and $opt->do_runout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ ne "" and $opt->do_runout) 138 1687 1825
Condition100%3/3
$_ ne "" and $opt->do_runout
!ll&&!rl&&r
4141273138
MC/DC100%2/2
$_ ne "" and $opt->do_runout
$_ ne "" $opt->do_runout
Truth table100%3/3
A and B
A= $_ ne ""B= $opt->do_runout
ABresult
0X0
100
111
544 138 ▶ if ($folded =~ m{ (?<color> (?! ${reset_re}) ${color_re}*+ )
Branch: 1 branch
BranchTrueFalseTotal
if ($folded =~ / (?<color> (?! $Text::ANSI::Fold::reset_re) $Text::ANSI::Fold::color_re*+ ) (?<runout> (?: ($prohibition_re{"end"}) (?: \cH{1,2} \g{-1})* )+ ) \z /pux and ${^PREMATCH} ne "" and pwidth(${^PREMATCH}) > $head_width and (my $w = pwidth($+{"runout"})) <= $opt->{"runout"}) 20 118 138
Condition88%8/9
$folded =~ / (?<color> (?! $Text::ANSI::Fold::reset_re) $Text::ANSI::Fold::color_re*+ ) (?<runout> (?: ($prohibition_re{"end"}) (?: \cH{1,2} \g{-1})* )+ ) \z /pux and ${^PREMATCH} ne "" and pwidth(${^PREMATCH}) > $head_width and (my $w = pwidth($+{"runout"})) <= $opt->{"runout"}
!ll&&!rl&&r
113520
$folded =~ / (?<color> (?! $Text::ANSI::Fold::reset_re) $Text::ANSI::Fold::color_re*+ ) (?<runout> (?: ($prohibition_re{"end"}) (?: \cH{1,2} \g{-1})* )+ ) \z /pux and ${^PREMATCH} ne "" and pwidth(${^PREMATCH}) > $head_width
!ll&&!rl&&r
106725
$folded =~ / (?<color> (?! $Text::ANSI::Fold::reset_re) $Text::ANSI::Fold::color_re*+ ) (?<runout> (?: ($prohibition_re{"end"}) (?: \cH{1,2} \g{-1})* )+ ) \z /pux and ${^PREMATCH} ne ""
!ll&&!rl&&r
106032
MC/DC75%3/4
$folded =~ / (?<color> (?! $Text::ANSI::Fold::reset_re) $Text::ANSI::Fold::color_re*+ ) (?<runout> (?: ($prohibition_re{"end"}) (?: \cH{1,2} \g{-1})* )+ ) \z /pux and ${^PREMATCH} ne "" and pwidth(${^PREMATCH}) > $head_width and (my $w = pwidth($+{"runout"})) <= $opt->{"runout"}
$folded =~ / (?<color> (?! $Text::ANSI::Fold::reset_re) $Text::ANSI::Fold::color_re*+ ) (?<runout> (?: ($prohibition_re{"end"}) (?: \cH{1,2} \g{-1})* )+ ) \z /pux ${^PREMATCH} ne "" pwidth(${^PREMATCH}) > $head_width (my $w = pwidth($+{"runout"})) <= $opt->{"runout"}
Truth table80%4/5
A and B and C and D
A= $folded =~ / (?<color> (?! $Text::ANSI::Fold::reset_re) $Text::ANSI::Fold::color_re*+ ) (?<runout> (?: ($prohibition_re{"end"}) (?: \cH{1,2} \g{-1})* )+ ) \z /puxB= ${^PREMATCH} ne ""C= pwidth(${^PREMATCH}) > $head_widthD= (my $w = pwidth($+{"runout"})) <= $opt->{"runout"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
545 (?<runout>
546 (?: ($prohibition_re{end}) (?: \cH{1,2} \g{-1})* )+
547 ) \z
548 }xp
549 and ${^PREMATCH} ne '' and pwidth(${^PREMATCH}) > $head_width
550 and (my $w = pwidth $+{runout}) <= $opt->{runout}) {
551  
552 20 $folded = ${^PREMATCH};
553 20 $_ = join '', ${^MATCH}, @reset, $_;
554 20 ▶ pop_reset() if $+{color};
Branch: 1 branch
BranchTrueFalseTotal
if $+{"color"} 2 18 20
555 20 $room += $w;
556 }
557 }
558  
559 1825 ▶ $folded .= pop_reset() if @reset;
Branch: 1 branch
BranchTrueFalseTotal
if @reset 55 1770 1825
560  
561 1825 $room += $opt->{margin};
562  
563 ##
564 ## RUN-IN
565 ##
566 1825 ▶ if ($opt->do_runin and not $split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->do_runin and not $split) 157 1668 1825
Condition100%3/3
$opt->do_runin and not $split
!ll&&!rl&&r
16662157
MC/DC100%2/2
$opt->do_runin and not $split
$opt->do_runin not $split
Truth table100%3/3
A and not(B)
A= $opt->do_runinB= not $split
ABresult
0X0
100
111
567 157 my @runin;
568 157 my $m = $opt->{runin};
569 157 ▶ while ($m > 0 and
Branch: 1 branch
BranchTrueFalseTotal
if ($m > 0 and /\A (?<color> $Text::ANSI::Fold::color_re*+) (?<runin> $prohibition_re{"head"} ) ( \cH{1,2} \g{runin} )* # multiple strike (?<reset> (?: $Text::ANSI::Fold::erase_re* $Text::ANSI::Fold::reset_re+ $Text::ANSI::Fold::erase_re* )? ) /pux) 49 157 206
Condition100%3/3
$m > 0 and /\A (?<color> $Text::ANSI::Fold::color_re*+) (?<runin> $prohibition_re{"head"} ) ( \cH{1,2} \g{runin} )* # multiple strike (?<reset> (?: $Text::ANSI::Fold::erase_re* $Text::ANSI::Fold::reset_re+ $Text::ANSI::Fold::erase_re* )? ) /pux
!ll&&!rl&&r
2313449
MC/DC100%2/2
$m > 0 and /\A (?<color> $Text::ANSI::Fold::color_re*+) (?<runin> $prohibition_re{"head"} ) ( \cH{1,2} \g{runin} )* # multiple strike (?<reset> (?: $Text::ANSI::Fold::erase_re* $Text::ANSI::Fold::reset_re+ $Text::ANSI::Fold::erase_re* )? ) /pux
$m > 0 /\A (?<color> $Text::ANSI::Fold::color_re*+) (?<runin> $prohibition_re{"head"} ) ( \cH{1,2} \g{runin} )* # multiple strike (?<reset> (?: $Text::ANSI::Fold::erase_re* $Text::ANSI::Fold::reset_re+ $Text::ANSI::Fold::erase_re* )? ) /pux
Truth table100%3/3
A and B
A= $m > 0B= /\A (?<color> $Text::ANSI::Fold::color_re*+) (?<runin> $prohibition_re{"head"} ) ( \cH{1,2} \g{runin} )* # multiple strike (?<reset> (?: $Text::ANSI::Fold::erase_re* $Text::ANSI::Fold::reset_re+ $Text::ANSI::Fold::erase_re* )? ) /pux
ABresult
0X0
100
111
570 m{\A (?<color> ${color_re}*+)
571 (?<runin> $prohibition_re{head} )
572 ( \cH{1,2} \g{runin} )* # multiple strike
573 (?<reset> (?: $erase_re* $reset_re+ $erase_re* )? )
574 }xp) {
575 49 my $w = vwidth $+{runin};
576 49 ▶ last if ($m -= $w) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($m -= $w) < 0 0 49 49
577 49 ▶ $+{color} and do { push @color_stack, $+{color} };
Branch: 1 branch
BranchTrueFalseTotal
$+{"color"} and do { push @color_stack, $+{"color"} } 2 47 49
578 49 ▶ $+{reset} and do { @color_stack = () };
Branch: 1 branch
BranchTrueFalseTotal
$+{"reset"} and do { @color_stack = () } 4 45 49
579 49 $room -= $w;
580 49 push @runin, ${^MATCH};
581 49 $_ = ${^POSTMATCH};
582 }
583 157 ▶ if (@runin) {
Branch: 1 branch
BranchTrueFalseTotal
if (@runin) 44 113 157
584 44 $folded .= join '', @runin;
585 # colors with no visible text after them stay on this line
586 44 ▶ $folded .= $1 if s/\A((?:${color_re}|${reset_re})++)\z//;
Branch: 1 branch
BranchTrueFalseTotal
if s/\A((?:$Text::ANSI::Fold::color_re|$Text::ANSI::Fold::reset_re)++)\z//u 4 40 44
587 }
588 }
589  
590 ##
591 ## TRIM: spaces at the end of a folded line, unless they are colored
592 ##
593 1825 ▶ if ($opt->{trim} and $eol eq '' and length and not /\A\p{IsEOL}/
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"trim"} and $eol eq "" and length $_ and not /\A\p{IsEOL}/u and not @color_stack and not defined $osc8_link and $folded =~ s/(\N{U+20}+)\z//u) 9 1816 1825
Condition94%17/18
$opt->{"trim"} and $eol eq "" and length $_ and not /\A\p{IsEOL}/u and not @color_stack and not defined $osc8_link and $folded =~ s/(\N{U+20}+)\z//u
!ll&&!rl&&r
181429
$opt->{"trim"} and $eol eq "" and length $_ and not /\A\p{IsEOL}/u and not @color_stack and not defined $osc8_link
!ll&&!rl&&r
1814011
$opt->{"trim"} and $eol eq "" and length $_ and not /\A\p{IsEOL}/u and not @color_stack
!ll&&!rl&&r
1813111
$opt->{"trim"} and $eol eq "" and length $_ and not /\A\p{IsEOL}/u
!ll&&!rl&&r
1812112
$opt->{"trim"} and $eol eq "" and length $_
!ll&&!rl&&r
1809313
$opt->{"trim"} and $eol eq ""
!ll&&!rl&&r
1808116
MC/DC85%6/7
$opt->{"trim"} and $eol eq "" and length $_ and not /\A\p{IsEOL}/u and not @color_stack and not defined $osc8_link and $folded =~ s/(\N{U+20}+)\z//u
$opt->{"trim"} $eol eq "" length $_ not /\A\p{IsEOL}/u not @color_stack not defined $osc8_link $folded =~ s/(\N{U+20}+)\z//u
Truth table87%7/8
A and B and C and not(D) and not(E) and not(F) and G
A= $opt->{"trim"}B= $eol eq ""C= length $_D= not /\A\p{IsEOL}/uE= not @color_stackF= not defined $osc8_linkG= $folded =~ s/(\N{U+20}+)\z//u
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
594 and not @color_stack and not defined $osc8_link
595 and $folded =~ s/(\N{SPACE}+)\z//) {
596 9 $room += vwidth(my $spaces = $1);
597 }
598  
599 1825 ▶ if (@color_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@color_stack) 74 1751 1825
600 74 $folded .= SGR_RESET;
601 74 ▶ $_ = join '', @color_stack, $_ if $_ ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne "" 68 6 74
602 }
603 1825 ▶ if (defined $osc8_link) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $osc8_link) 16 1809 1825
604 16 $folded .= OSC8_RESET;
605 16 ▶ $_ = $osc8_link . $_ if $_ ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne "" 16 0 16
606 }
607  
608 1825 ▶ if ($opt->{padding} and $room > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"padding"} and $room > 0) 15 1810 1825
Condition100%3/3
$opt->{"padding"} and $room > 0
!ll&&!rl&&r
17911915
MC/DC100%2/2
$opt->{"padding"} and $room > 0
$opt->{"padding"} $room > 0
Truth table100%3/3
A and B
A= $opt->{"padding"}B= $room > 0
ABresult
0X0
100
111
609 15 my $padding = $opt->{padchar} x $room;
610 15 ▶ if (@bg_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bg_stack) 1 14 15
611 1 $padding = join '', @bg_stack, $padding, SGR_RESET;
612 }
613 15 $folded .= $padding;
614 }
615  
616 1825 ▶ if (length and my $p = $opt->{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ and my $p = $opt->{"prefix"}) 36 1789 1825
Condition100%3/3
length $_ and my $p = $opt->{"prefix"}
!ll&&!rl&&r
419137036
MC/DC100%2/2
length $_ and my $p = $opt->{"prefix"}
length $_ my $p = $opt->{"prefix"}
Truth table100%3/3
A and B
A= length $_B= my $p = $opt->{"prefix"}
ABresult
0X0
100
111
617 36 ▶ my $s = ref $p eq 'CODE' ? &$p : $p;
Branch: 1 branch
BranchTrueFalseTotal
ref $p eq 'CODE' ? : 4 32 36
618 36 $_ = $s . $_;
619 }
620  
621 1825 ($folded . $eol, $_, $width + $opt->{margin} - $room);
622 }
623  
624 ##
625 ## Trim off one or more *logical* characters from the beginning of a line
626 ##
627 5421 ▶sub simple_fold {
Subroutine simple_fold: called CC=6 SCAR=18.0
Pod undocumented
628 5421 my $orig = shift;
629 5421 my $width = shift;
630 5421 ▶ $width <= 0 and croak "parameter error";
Branch: 1 branch
BranchTrueFalseTotal
$width <= 0 and croak("parameter error") 0 5421 5421
631  
632 # Printable ASCII is as wide as its length. The character after
633 # the width is checked too, as it may be a mark on the last one.
634 5421 ▶ if (substr($orig, 0, $width + 1) !~ /[^\x20-\x7e]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($orig, 0, ($width + 1)) =~ /[^\x20-\x7e]/u) 742 4679 5421
635 742 ▶ return ($orig, '', length $orig) if length $orig <= $width;
Branch: 1 branch
BranchTrueFalseTotal
if length $orig <= $width 337 405 742
636 405 return (substr($orig, 0, $width), substr($orig, $width), $width);
637 }
638  
639 4679 my $left = _trim($orig, $width);
640 4679 ▶ $left = $orig =~ /^(\X)/ ? $1 : '' if $left eq ''; # at least one
Branch: 2 branches
BranchTrueFalseTotal
if $left eq "" 20 4659 4679
$orig =~ /^(\X)/u ? : 20 0 20
641 4679 ($left, substr($orig, length $left), vwidth $left);
642 }
643  
644 ##
645 ## The longest leading part of a string within a width, in graphemes.
646 ## This is trim of Text::VisualWidth::PP, which counts a zero-width
647 ## character as 1. The classes are those of Text::VisualWidth::PP, so
648 ## that the result agrees with vwidth.
649 ##
650 4679 ▶sub _trim {
Subroutine _trim: called CC=8 SCAR=20.8
651 4679 my($str, $limit) = @_;
652  
653 4679 my $cnt = 0;
654 4679 my $ret = '';
655 4679 ▶ my $fullwidth = $Text::VisualWidth::PP::EastAsian
Branch: 1 branch
BranchTrueFalseTotal
$Text::VisualWidth::PP::EastAsian ? : 2 4677 4679
656 ? qr/\p{Text::VisualWidth::PP::InVWPP1Fullwidth}/
657 : qr/\p{Text::VisualWidth::PP::InVWPP0Fullwidth}/;
658 4679 ▶ my $halfwidth = $Text::VisualWidth::PP::EastAsian
Branch: 1 branch
BranchTrueFalseTotal
$Text::VisualWidth::PP::EastAsian ? : 2 4677 4679
659 ? qr/\p{Text::VisualWidth::PP::InVWPP1Halfwidth}/
660 : qr/\p{Text::VisualWidth::PP::InVWPP0Halfwidth}/;
661 4679 ▶ while ($str =~ /\G(\X)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G(\X)/gu) 16118 4109 20227
662 16118 my $ch = $1;
663 # a zero-width character is in neither class, and vwidth gives 0
664 16118 my $w = do {
665 16118 ▶ if ($ch =~ /\A[\x00-\x7f]\z/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ch =~ /\A[\x00-\x7f]\z/u) { } 56 16062 16118
elsif ($ch =~ /\A$fullwidth\z/u) { } 1005 15057 16062
elsif ($ch =~ /\A$halfwidth\z/u) { } 301 14756 15057
666 56 1;
667 } elsif ($ch =~ /\A$fullwidth\z/) {
668 1005 2;
669 } elsif ($ch =~ /\A$halfwidth\z/) {
670 301 1;
671 } else {
672 14756 vwidth($ch);
673 }
674 };
675 16118 ▶ last if $cnt + $w > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt + $w > $limit 570 15548 16118
676 15548 $ret .= $ch;
677 15548 $cnt += $w;
678 }
679 4679 $ret;
680 }
681  
682 ######################################################################
683 # EXTERNAL METHODS
684  
685 269 ▶sub text :lvalue {
Subroutine text: called CC=3 SCAR=11.0
Pod documented
686 269 my $obj = shift;
687 269 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) { } 6 263 269
688 6 $obj->{text};
689 } else {
690 263 ▶ croak "Invalid argument" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 263 263
691 263 $obj->{text} = shift;
692 263 $obj;
693 }
694 }
695  
696 33 ▶sub retrieve {
Subroutine retrieve: called CC=1 SCAR=0.0
Pod documented
697 33 (shift->_retrieve(@_))[0];
698 }
699  
700 # returns the folded string and its width
701 1432 ▶sub _retrieve {
Subroutine _retrieve: called CC=5 SCAR=16.2
702 1432 my $obj = shift;
703 1432 local *_ = \$obj->{text};
704 1432 ▶ return ('', 0) if not defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 286 1146 1432
705 1146 my($folded, $rest, $width) = $obj->fold($_, @_);
706 1146 ▶ die "panic: retrieve: no progress in fold"
Branch: 1 branch
BranchTrueFalseTotal
if length $rest and $rest eq $_ 0 1146 1146
Condition66%2/3
length $rest and $rest eq $_
!ll&&!rl&&r
3008460
MC/DC0%0/2
length $rest and $rest eq $_
length $rest $rest eq $_
Truth table66%2/3
A and B
A= length $restB= $rest eq $_
ABresult
0X0
100
111
707 if length $rest and $rest eq $_;
708 1146 ▶ $_ = length $rest ? $rest : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $rest ? : 846 300 1146
709 1146 ($folded, $width);
710 }
711  
712 292 ▶sub chops {
Subroutine chops: called CC=8 SCAR=20.8
Pod documented
713 292 my $obj = shift;
714 292 my %opt = @_;
715 292 ▶ my $width = $opt{width} // $obj->{width};
Condition66%2/3
$opt{"width"} // $obj->{"width"}
l!l&&r!l&&!r
82840
MC/DC0%0/2
$opt{"width"} // $obj->{"width"}
$opt{"width"} $obj->{"width"}
Truth table66%2/3
A // B
A= $opt{"width"}B= $obj->{"width"}
ABresult
1X1
011
000
716  
717 292 my(@chops, @length);
718  
719 292 ▶ if (ref $width eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $width eq 'ARRAY') { } 11 281 292
720 11 for my $w (@{$width}) {
721 53 ▶ if ($w == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($w == 0) { } 3 50 53
elsif ((() = $obj->_retrieve('width', $w))[0] ne '') { } 45 5 50
722 3 push @chops, '';
723 3 push @length, 0;
724 }
725 elsif ((my($chop, $l) = $obj->_retrieve(width => $w))[0] ne '') {
726 45 push @chops, $chop;
727 45 push @length, $l;
728 }
729 else {
730 5 last;
731 }
732 }
733 }
734 else {
735 281 ▶ while ((my($chop, $l) = $obj->_retrieve(width => $width))[0] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ((() = $obj->_retrieve("width", $width))[0] ne "") 1068 281 1349
736 1068 push @chops, $chop;
737 1068 push @length, $l;
738 }
739 }
740  
741 292 ▶ @{$opt{length}} = @length if $opt{length};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"length"} 3 289 292
742 292 @chops;
743 }
744  
745 19 1;
746  
747 __END__
↑ Summary Fold/Japanese/Wikipedia.pm »