Fold.pm

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