Tidy/VerticalAligner.pm

filter: Statementstmt 91.4% 2428 / 2656 Branchbran 79.6% 886 / 1112 Conditioncond 76.4% 570 / 746 MC/DCmcdc 52.5% 214 / 407 Subroutinesub 90.8% 109 / 120 Podpod 0.0% 0 / 79 totaltotal 82.1% 4207 / 5120 CC 913 SCAR 35.4
CC
913
Cov
86%
is_marginal_match
86.2
valign_input
70.7
pad_signed_number_columns
64.9
CRAP 34.5
« Tidy/Logger.pm ↑ Summary Tidy/Diagnostics.pm »
1 package Perl::Tidy::VerticalAligner;
2 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 { #<<< A non-indenting brace to contain all lexical variables
7  
8 45 our $VERSION = '20260808';
9 45 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 45 ▶use Scalar::Util 'refaddr'; # perl 5.8.1 and later
Subroutine BEGIN: called CC=1 SCAR=0.0
11 45 ▶use Perl::Tidy::VerticalAligner::Alignment;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use Perl::Tidy::VerticalAligner::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 45 ▶use constant DEVEL_MODE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 45 ▶use constant EMPTY_STRING => q{};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 45 ▶use constant SPACE => q{ };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 45 ▶use constant COMMA => q{,};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # The Perl::Tidy::VerticalAligner package collects output lines and
20 # attempts to line up certain common tokens, such as => and #, which are
21 # identified by the calling routine.
22 #
23 # Usage:
24 # - Initiate an object with a call to new().
25 # - Write lines one-by-one with calls to valign_input().
26 # - Make a final call to flush() to empty the pipeline.
27 #
28 # The sub valign_input collects lines into groups. When a group reaches
29 # the maximum possible size it is processed for alignment and output.
30 # The maximum group size is reached whenever there is a change in indentation
31 # level, a blank line, a block comment, or an external flush call. The calling
32 # routine may also force a break in alignment at any time.
33 #
34 # If the calling routine needs to interrupt the output and send other text to
35 # the output, it must first call flush() to empty the output pipeline. This
36 # might occur for example if a block of pod text needs to be sent to the output
37 # between blocks of code.
38  
39 # It is essential that a final call to flush() be made. Otherwise some
40 # final lines of text will be lost.
41  
42 # Index...
43 # CODE SECTION 1: Preliminary code, global definitions and sub new
44 # sub new
45 # CODE SECTION 2: Some Basic Utilities
46 # CODE SECTION 3: Code to accept input and form groups
47 # sub valign_input
48 # CODE SECTION 4: Code to process comment lines
49 # sub _flush_comment_lines
50 # CODE SECTION 5: Code to process groups of code lines
51 # sub _flush_group_lines
52 # CODE SECTION 6: Pad Signed Number Columns
53 # sub pad_signed_number_columns
54 # CODE SECTION 7: Pad Wide Equals Columns
55 # sub pad_wide_equals_columns
56 # CODE SECTION 8: Output Step A
57 # sub valign_output_step_A
58 # CODE SECTION 9: Output Step B
59 # sub valign_output_step_B
60 # CODE SECTION 10: Output Step C
61 # sub valign_output_step_C
62 # CODE SECTION 11: Output Step D
63 # sub valign_output_step_D
64 # CODE SECTION 12: Summary
65 # sub report_anything_unusual
66  
67 ##################################################################
68 # CODE SECTION 1: Preliminary code, global definitions and sub new
69 ##################################################################
70  
71 sub AUTOLOAD {
72  
73 # Catch any undefined sub calls so that we are sure to get
74 # some diagnostic information. This sub should never be called
75 # except for a programming error.
76 0 ▶ our $AUTOLOAD;
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
77 0 ▶ return if ( $AUTOLOAD =~ /\bDESTROY$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::Tidy::VerticalAligner::AUTOLOAD =~ /\bDESTROY$/ 0 0 0
78 0 my ( $pkg, $fname, $lno ) = caller();
79 0 my $my_package = __PACKAGE__;
80 0 print {*STDERR} <<EOM;
81 ======================================================================
82 Error detected in package '$my_package', version $VERSION
83 Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
84 Called from package: '$pkg'
85 Called from File '$fname' at line '$lno'
86 This error is probably due to a recent programming change
87 ======================================================================
88 EOM
89 0 exit 1;
90 } ## end sub AUTOLOAD
91  
92 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
93  
94 # required to avoid call to AUTOLOAD in some versions of perl
95 }
96  
97 sub Die {
98 0 ▶ my ($msg) = @_;
Subroutine Die: not called CC=1 SCAR=6.9
Pod undocumented
99 0 Perl::Tidy::Die($msg);
100 0 croak "unexpected return from Perl::Tidy::Die";
101 }
102  
103 sub Warn {
104 0 ▶ my ($msg) = @_;
Subroutine Warn: not called CC=1 SCAR=6.9
Pod undocumented
105 0 Perl::Tidy::Warn($msg);
106 0 return;
107 }
108  
109 sub Fault {
110  
111 0 ▶ my ($msg) = @_;
Subroutine Fault: not called CC=1 SCAR=6.9
Pod undocumented
112  
113 # This routine is called for errors that really should not occur
114 # except if there has been a bug introduced by a recent program change.
115 # Please add comments at calls to Fault to explain why the call
116 # should not occur, and where to look to fix it.
117 0 my ( $package0_uu, $filename0_uu, $line0, $subroutine0_uu ) = caller(0);
118 0 my ( $package1_uu, $filename1, $line1, $subroutine1 ) = caller(1);
119 0 my ( $package2_uu, $filename2_uu, $line2_uu, $subroutine2 ) = caller(2);
120 0 my $pkg = __PACKAGE__;
121 0 my $input_stream_name = Perl::Tidy::get_input_stream_name();
122  
123 0 Die(<<EOM);
124 ==============================================================================
125 While operating on input stream with name: '$input_stream_name'
126 A fault was detected at line $line0 of sub '$subroutine1'
127 in file '$filename1'
128 which was called from line $line1 of sub '$subroutine2'
129 Message: '$msg'
130 This is probably an error introduced by a recent programming change.
131 $pkg reports VERSION='$VERSION'.
132 ==============================================================================
133 EOM
134 0 croak "unexpected return from sub Die";
135 } ## end sub Fault
136  
137 45 my %valid_LINE_keys;
138  
139 BEGIN {
140  
141 # define valid keys in a line object
142 45 ▶ my @q = qw(
Subroutine BEGIN: called CC=2 SCAR=6.9
143 jmax
144 rtokens
145 rfields
146 rfield_lengths
147 rpatterns
148 indentation
149 leading_space_count
150 outdent_long_lines
151 list_type
152 list_seqno
153 is_hanging_side_comment
154 maximum_line_length
155 rvertical_tightness_flags
156 is_terminal_ternary
157 j_terminal_match
158 end_group
159 Kend
160 ci_level
161 level
162 level_end
163 imax_pair
164  
165 ralignments
166 );
167  
168 45 $valid_LINE_keys{$_} = 1 for @q;
169  
170 } ## end BEGIN
171  
172 BEGIN {
173  
174 # Define the fixed indexes for variables in $self, which is an array
175 # reference. Note the convention of leading and trailing underscores to
176 # keep them unique.
177 # Do not combine with other BEGIN blocks (c101).
178 45 ▶ my $i = 0;
Subroutine BEGIN: called CC=1 SCAR=0.3
179 use constant {
180 45 _file_writer_object_ => $i++,
181 _logger_object_ => $i++,
182 _diagnostics_object_ => $i++,
183  
184 _rOpts_ => $i++,
185  
186 _last_level_written_ => $i++,
187 _last_side_comment_column_ => $i++,
188 _last_side_comment_line_number_ => $i++,
189 _last_side_comment_length_ => $i++,
190 _last_side_comment_level_ => $i++,
191 _outdented_line_count_ => $i++,
192 _first_outdented_line_at_ => $i++,
193 _last_outdented_line_at_ => $i++,
194 _consecutive_block_comments_ => $i++,
195  
196 _rgroup_lines_ => $i++,
197 _group_level_ => $i++,
198 _group_type_ => $i++,
199 _group_maximum_line_length_ => $i++,
200 _zero_count_ => $i++,
201 _last_leading_space_count_ => $i++,
202 _comment_leading_space_count_ => $i++,
203 45 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
204  
205 # Debug flag. This is a relic from the original program development
206 # looking for problems with tab characters. Caution: this debug flag can
207 # produce a lot of output It should be 0 except when debugging small
208 # scripts.
209  
210 45 ▶ use constant DEBUG_TABS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
211  
212 my $debug_warning = sub {
213 0 ▶ my $msg = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 print {*STDOUT} "VALIGN_DEBUGGING with key $msg\n";
215 0 return;
216 45 };
217  
218 45 DEBUG_TABS && $debug_warning->('TABS');
219 } ## end BEGIN
220  
221 # GLOBAL variables
222 my (
223  
224 45 %valign_control_hash,
225 $valign_control_default,
226  
227 $rOpts_indent_columns,
228 $rOpts_tabs,
229 $rOpts_entab_leading_whitespace,
230 $rOpts_fixed_position_side_comment,
231 $rOpts_maximum_line_length,
232 $rOpts_minimum_space_to_comment,
233 $rOpts_valign_code,
234 $rOpts_valign_block_comments,
235 $rOpts_valign_side_comments,
236 $rOpts_valign_signed_numbers,
237 $rOpts_valign_signed_numbers_limit,
238 $rOpts_valign_wide_equals,
239  
240 $require_tabs,
241  
242 );
243  
244 sub check_valign_list_items {
245 4 ▶ my ( $rlist, ( $option_name, $die_on_error ) ) = @_;
Subroutine check_valign_list_items: called CC=8 SCAR=32.2
Pod undocumented
246  
247 # Warn if obviously invalid token types or keywords occur in one of the
248 # --valign lists. This is a crude check for valid token types and valid
249 # keywords.
250  
251 # Given:
252 # $rlist = ref to list of input items
253 # $option_name = (optional) name of option for a warning message
254 # Return:
255 # nothing if no errors, or
256 # ref to list of unknown token types
257 4 ▶ return if ( !defined($rlist) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rlist 0 4 4
258  
259 4 my @unknown_items;
260 4 foreach my $item ( @{$rlist} ) {
261 5 ▶ next if ( Perl::Tidy::Tokenizer::is_valid_token_type($item) );
Branch: 1 branch
BranchTrueFalseTotal
if Perl::Tidy::Tokenizer::is_valid_token_type($item) 5 0 5
262 0 ▶ next if ( Perl::Tidy::Tokenizer::is_keyword($item) );
Branch: 1 branch
BranchTrueFalseTotal
if Perl::Tidy::Tokenizer::is_keyword($item) 0 0 0
263 0 push @unknown_items, $item;
264 }
265 4 ▶ return if ( !@unknown_items );
Branch: 1 branch
BranchTrueFalseTotal
unless @unknown_items 4 0 4
266  
267 0 ▶ if ($option_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option_name) 0 0 0
268 0 my $num = @unknown_items;
269 0 local $LIST_SEPARATOR = SPACE;
270 0 my $msg = <<EOM;
271 $num unrecognized items input with $option_name :
272 @unknown_items
273 EOM
274 0 ▶ Die($msg) if ($die_on_error);
Branch: 1 branch
BranchTrueFalseTotal
if $die_on_error 0 0 0
275 0 Warn($msg);
276 }
277 0 return \@unknown_items;
278 } ## end sub check_valign_list_items
279  
280 sub check_options {
281  
282 660 ▶ my ($rOpts) = @_;
Subroutine check_options: called CC=12 SCAR=25.2
Pod undocumented
283  
284 # This routine is called to check the user-supplied run parameters
285 # in $rOpts and to configure the control hashes to them.
286  
287 # All alignments are done by default
288 660 %valign_control_hash = ();
289 660 $valign_control_default = 1;
290  
291 # If -vil=s is entered without -vxl, assume -vxl='*'
292 660 ▶ if ( !$rOpts->{'valign-exclusion-list'}
Branch: 1 branch
BranchTrueFalseTotal
if (not $rOpts->{"valign-exclusion-list"} and $rOpts->{"valign-inclusion-list"}) 0 660 660
Condition66%2/3
not $rOpts->{"valign-exclusion-list"} and $rOpts->{"valign-inclusion-list"}
!ll&&!rl&&r
36570
MC/DC0%0/2
not $rOpts->{"valign-exclusion-list"} and $rOpts->{"valign-inclusion-list"}
not $rOpts->{"valign-exclusion-list"} $rOpts->{"valign-inclusion-list"}
Truth table66%2/3
not(A) and B
A= not $rOpts->{"valign-exclusion-list"}B= $rOpts->{"valign-inclusion-list"}
ABresult
0X0
100
111
293 && $rOpts->{'valign-inclusion-list'} )
294 {
295 0 $rOpts->{'valign-exclusion-list'} = '*';
296 }
297  
298 # See if the user wants to exclude any alignment types ...
299 660 ▶ if ( $rOpts->{'valign-exclusion-list'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"valign-exclusion-list"}) 3 657 660
300  
301 # The inclusion list is only relevant if there is an exclusion list
302 3 ▶ if ( $rOpts->{'valign-inclusion-list'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"valign-inclusion-list"}) 1 2 3
303 1 my @vil = split /\s+/, $rOpts->{'valign-inclusion-list'};
304 1 check_valign_list_items( \@vil, 'valign-inclusion-list', 1 );
305 1 $valign_control_hash{$_} = 1 for @vil;
306 }
307  
308 # Note that the -vxl list is done after -vil, so -vxl has priority
309 # in the event of duplicate entries.
310 3 my @vxl = split /\s+/, $rOpts->{'valign-exclusion-list'};
311 3 check_valign_list_items( \@vxl, 'valign-exclusion-list', 1 );
312 3 $valign_control_hash{$_} = 0 for @vxl;
313  
314 # Optimization: revert to defaults if no exclusions.
315 # This could happen with -vxl=' ' and any -vil list
316 3 ▶ if ( !@vxl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@vxl) 0 3 3
317 0 %valign_control_hash = ();
318 }
319  
320 # '$valign_control_default' applies to types not in the hash:
321 # - If a '*' was entered then set it to be that default type
322 # - Otherwise, leave it set it to 1
323 3 ▶ if ( defined( $valign_control_hash{'*'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valign_control_hash{"*"}) 1 2 3
324 1 $valign_control_default = $valign_control_hash{'*'};
325 }
326  
327 # Side comments are controlled separately and must be removed
328 # if given in a list.
329 3 ▶ if (%valign_control_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (%valign_control_hash) 3 0 3
330 3 $valign_control_hash{'#'} = 1;
331 }
332 }
333  
334 # Initialize some global options
335 660 $rOpts_indent_columns = $rOpts->{'indent-columns'};
336 660 $rOpts_tabs = $rOpts->{'tabs'};
337 660 $rOpts_entab_leading_whitespace = $rOpts->{'entab-leading-whitespace'};
338 660 ▶ $require_tabs = ( $rOpts_tabs || $rOpts_entab_leading_whitespace )
Condition66%4/6
($rOpts_tabs || $rOpts_entab_leading_whitespace) && $rOpts_indent_columns > 0
!ll&&!rl&&r
65802
$rOpts_tabs or $rOpts_entab_leading_whitespace
l!l&&r!l&&!r
02658
MC/DC33%1/3
($rOpts_tabs || $rOpts_entab_leading_whitespace) && $rOpts_indent_columns > 0
$rOpts_tabs $rOpts_entab_leading_whitespace $rOpts_indent_columns > 0
Truth table40%2/5
A or B && C
A= $rOpts_tabsB= $rOpts_entab_leading_whitespaceC= $rOpts_indent_columns > 0
ABCresult
00X0
1X00
0100
1X11
0111
339 && $rOpts_indent_columns > 0;
340  
341 $rOpts_fixed_position_side_comment =
342 660 $rOpts->{'fixed-position-side-comment'};
343  
344 660 $rOpts_maximum_line_length = $rOpts->{'maximum-line-length'};
345 660 $rOpts_minimum_space_to_comment = $rOpts->{'minimum-space-to-comment'};
346 660 $rOpts_valign_code = $rOpts->{'valign-code'};
347 660 $rOpts_valign_block_comments = $rOpts->{'valign-block-comments'};
348 660 $rOpts_valign_side_comments = $rOpts->{'valign-side-comments'};
349 660 $rOpts_valign_signed_numbers = $rOpts->{'valign-signed-numbers'};
350 $rOpts_valign_signed_numbers_limit =
351 660 $rOpts->{'valign-signed-numbers-limit'};
352 660 $rOpts_valign_wide_equals = $rOpts->{'valign-wide-equals'};
353  
354 660 return;
355 } ## end sub check_options
356  
357 sub check_keys {
358  
359 0 ▶ my ( $rtest, $rvalid, $msg, $exact_match ) = @_;
Subroutine check_keys: not called CC=4 SCAR=30.0
Pod undocumented
360  
361 # Check the keys of a hash:
362 # $rtest = ref to hash to test
363 # $rvalid = ref to hash with valid keys
364  
365 # $msg = a message to write in case of error
366 # $exact_match defines the type of check:
367 # = false: test hash must not have unknown key
368 # = true: test hash must have exactly same keys as known hash
369 my @unknown_keys =
370 0 grep { !exists $rvalid->{$_} } keys %{$rtest};
371 my @missing_keys =
372 0 grep { !exists $rtest->{$_} } keys %{$rvalid};
373 0 my $error = @unknown_keys;
374 0 ▶ if ($exact_match) { $error ||= @missing_keys }
Branch: 1 branch
BranchTrueFalseTotal
if ($exact_match) 0 0 0
Condition0%0/3
$error ||= @missing_keys
l!l&&r!l&&!r
000
MC/DC0%0/2
$error ||= @missing_keys
$error @missing_keys
Truth table0%0/3
A ||= B
A= $errorB= @missing_keys
ABresult
1X1
011
000
375 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
376 0 local $LIST_SEPARATOR = ')(';
377 0 my @expected_keys = sort keys %{$rvalid};
378 0 @unknown_keys = sort @unknown_keys;
379 0 Fault(<<EOM);
380 ------------------------------------------------------------------------
381 Program error detected checking hash keys
382 Message is: '$msg'
383 Expected keys: (@expected_keys)
384 Unknown key(s): (@unknown_keys)
385 Missing key(s): (@missing_keys)
386 ------------------------------------------------------------------------
387 EOM
388 }
389 0 return;
390 } ## end sub check_keys
391  
392 sub vertical_alignment_ok {
393 1322 ▶ my ($tok) = @_;
Subroutine vertical_alignment_ok: called CC=3 SCAR=11.0
Pod undocumented
394  
395 # Return 1 if alignment ok
396 # Return 0 if do not align
397 1322 ▶ return 1 if ( !%valign_control_hash );
Branch: 1 branch
BranchTrueFalseTotal
unless %valign_control_hash 1316 6 1322
398 6 my $align_ok = $valign_control_hash{$tok};
399 6 ▶ $align_ok = $valign_control_default unless ( defined($align_ok) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $align_ok 6 0 6
400 6 return $align_ok;
401 } ## end sub vertical_alignment_ok
402  
403 sub new {
404  
405 661 ▶ my ( $class, @arglist ) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
406  
407 # Create a VerticalAligner object
408 # Given:
409 # @arglist is the hash of values shown below in %defaults
410  
411 661 ▶ if ( @arglist % 2 ) { croak "Odd number of items in arg hash list\n" }
Branch: 1 branch
BranchTrueFalseTotal
if (@arglist % 2) 0 661 661
412  
413 661 my %defaults = (
414 rOpts => undef,
415 file_writer_object => undef,
416 logger_object => undef,
417 diagnostics_object => undef,
418 );
419 661 my %args = ( %defaults, @arglist );
420  
421 # Initialize other caches and buffers
422 661 initialize_step_B_cache();
423 661 initialize_valign_buffer();
424 661 initialize_decode();
425  
426 # Initialize all variables in $self.
427 # To add an item to $self, first define a new constant index in the BEGIN
428 # section.
429 661 my $self = [];
430  
431 # objects
432 661 $self->[_file_writer_object_] = $args{file_writer_object};
433 661 $self->[_logger_object_] = $args{logger_object};
434 661 $self->[_diagnostics_object_] = $args{diagnostics_object};
435  
436 # shortcut to user options
437 661 my $rOpts = $args{rOpts};
438 661 $self->[_rOpts_] = $rOpts;
439  
440 # Batch of lines being collected
441 661 $self->[_rgroup_lines_] = [];
442 661 $self->[_group_level_] = 0;
443 661 $self->[_group_type_] = EMPTY_STRING;
444 661 $self->[_group_maximum_line_length_] = undef;
445 661 $self->[_zero_count_] = 0;
446 661 $self->[_comment_leading_space_count_] = 0;
447 661 $self->[_last_leading_space_count_] = 0;
448  
449 # Memory of what has been processed
450 661 $self->[_last_level_written_] = -1;
451 661 $self->[_last_side_comment_column_] = 0;
452 661 $self->[_last_side_comment_line_number_] = 0;
453 661 $self->[_last_side_comment_length_] = 0;
454 661 $self->[_last_side_comment_level_] = -1;
455 661 $self->[_outdented_line_count_] = 0;
456 661 $self->[_first_outdented_line_at_] = 0;
457 661 $self->[_last_outdented_line_at_] = 0;
458 661 $self->[_consecutive_block_comments_] = 0;
459  
460 661 bless $self, $class;
461 661 return $self;
462 } ## end sub new
463  
464 #################################
465 # CODE SECTION 2: Basic Utilities
466 #################################
467  
468 sub flush {
469  
470 2328 ▶ my ($self) = @_;
Subroutine flush: called CC=2 SCAR=6.9
Pod undocumented
471  
472 # flush() is the external call to completely empty the pipeline.
473  
474 # push out any current group lines
475 $self->_flush_group_lines()
476 2328 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 544 1784 2328
477  
478 # then anything left in the cache of step_B
479 2328 $self->_flush_step_B_cache();
480  
481 # then anything left in the buffer of step_C
482 2328 $self->dump_valign_buffer();
483  
484 2328 return;
485 } ## end sub flush
486  
487 sub initialize_for_new_group {
488  
489 2663 ▶ my ($self) = @_;
Subroutine initialize_for_new_group: called CC=1 SCAR=0.0
Pod undocumented
490  
491 # initialize for a new group of lines to be aligned vertically
492  
493 2663 $self->[_rgroup_lines_] = [];
494 2663 $self->[_group_type_] = EMPTY_STRING;
495 2663 $self->[_zero_count_] = 0;
496 2663 $self->[_comment_leading_space_count_] = 0;
497 2663 $self->[_last_leading_space_count_] = 0;
498 2663 $self->[_group_maximum_line_length_] = undef;
499  
500 # Note that the value for _group_level_ is
501 # handled separately in sub valign_input
502 2663 return;
503 } ## end sub initialize_for_new_group
504  
505 sub group_line_count {
506 94 ▶ my $self = shift;
Subroutine group_line_count: called CC=1 SCAR=0.0
Pod undocumented
507 94 return +@{ $self->[_rgroup_lines_] };
508 }
509  
510 sub write_diagnostics {
511  
512 0 ▶ my ( $self, $msg ) = @_;
Subroutine write_diagnostics: not called CC=2 SCAR=17.9
Pod undocumented
513  
514 # Interface to Perl::Tidy::Diagnostics routines
515 # For debugging; not currently used
516 0 my $diagnostics_object = $self->[_diagnostics_object_];
517 0 ▶ if ($diagnostics_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diagnostics_object) 0 0 0
518 0 $diagnostics_object->write_diagnostics($msg);
519 }
520 0 return;
521 } ## end sub write_diagnostics
522  
523 sub warning {
524 0 ▶ my ( $self, $msg ) = @_;
Subroutine warning: not called CC=2 SCAR=17.9
Pod undocumented
525 0 my $logger_object = $self->[_logger_object_];
526 0 ▶ if ($logger_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger_object) 0 0 0
527 0 $logger_object->warning($msg);
528 }
529 0 return;
530 } ## end sub warning
531  
532 sub get_cached_line_count {
533 1 ▶ my $self = shift;
Subroutine get_cached_line_count: called CC=2 SCAR=7.2
Pod undocumented
534 1 ▶ return $self->group_line_count() + ( get_cached_line_type() ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
get_cached_line_type() ? : 0 1 1
535 }
536  
537 sub get_recoverable_spaces {
538  
539 4664 ▶ my $indentation = shift;
Subroutine get_recoverable_spaces: called CC=2 SCAR=6.9
Pod undocumented
540  
541 # Return the number of spaces (+ means shift right, - means shift left)
542 # that we would like to shift a group of lines with the same indentation
543 # to get them to line up with their opening parens
544  
545 4664 ▶ return ref($indentation) ? $indentation->get_recoverable_spaces() : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $indentation ? : 264 4400 4664
546 } ## end sub get_recoverable_spaces
547  
548 ######################################################
549 # CODE SECTION 3: Code to accept input and form groups
550 ######################################################
551  
552 45 ▶use constant DEBUG_VALIGN => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
553 45 ▶use constant SC_LONG_LINE_DIFF => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
554  
555 45 my %is_opening_token;
556 45 my %is_closing_token;
557 45 my %is_digit_char;
558 45 my %is_plus_or_minus;
559 45 my %is_if_or;
560 45 my %is_comma_token;
561 45 my %is_assignment;
562 45 my %is_good_marginal_alignment;
563  
564 BEGIN {
565  
566 45 ▶ $is_opening_token{$_} = 1 for qw# { ( [ #;
Subroutine BEGIN: called CC=9 SCAR=22.0
567 45 $is_closing_token{$_} = 1 for qw# } ) ] #;
568 45 $is_digit_char{$_} = 1 for qw# 0 1 2 3 4 5 6 7 8 9 #;
569 45 $is_plus_or_minus{$_} = 1 for qw# + - #;
570 45 $is_if_or{$_} = 1 for qw# if unless or || #;
571 45 $is_comma_token{$_} = 1 for ( '=>', COMMA );
572 $is_assignment{$_} = 1
573 45 for qw# = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x= ^^= #;
574  
575 # We can be less restrictive in marginal cases at certain "good" alignments
576 45 $is_good_marginal_alignment{$_} = 1 for ( COMMA, qw# { ? => = # );
577 }
578  
579 #--------------------------------------------
580 # VTFLAGS: Vertical tightness types and flags
581 #--------------------------------------------
582 # Vertical tightness is controlled by a 'type' and associated 'flags' for each
583 # line. These values are set by sub Formatter::set_vertical_tightness_flags.
584 # These are defined as follows:
585  
586 # Vertical Tightness Line Type Codes:
587 # Type 0, no vertical tightness condition
588 # Type 1, last token of this line is a non-block opening token
589 # Type 2, first token of next line is a non-block closing
590 # Type 3, isolated opening block brace
591 # type 4, isolated closing block brace
592  
593 # Opening token flag values are the vertical tightness flags
594 # 0 do not join with next line
595 # 1 just one join per line
596 # 2 any number of joins
597  
598 # Closing token flag values indicate spacing:
599 # 0 = no space added before closing token
600 # 1 = single space added before closing token
601  
602 sub valign_input {
603  
604 8541 ▶ my ( $self, $rcall_hash ) = @_;
Subroutine valign_input: called CC=69 SCAR=42.6
Pod undocumented
605  
606 #---------------------------------------------------------------------
607 # This is the front door of the vertical aligner. On each call
608 # we receive one line of specially marked text for vertical alignment.
609 # We compare the line with the current group, and either:
610 # - the line joins the current group if alignments match, or
611 # - the current group is flushed and a new group is started
612 #---------------------------------------------------------------------
613 #
614 # The key input parameters describing each line are:
615 # $level = indentation level of this line
616 # $rfields = ref to array of fields
617 # $rpatterns = ref to array of patterns, one per field
618 # $rtokens = ref to array of tokens starting fields 1,2,..
619 # $rfield_lengths = ref to array of field display widths
620 #
621 # Here is an example of what this package does. In this example,
622 # we are trying to line up both the '=>' and the '#'.
623 #
624 # '18' => 'grave', # \`
625 # '19' => 'acute', # `'
626 # '20' => 'caron', # \v
627 # <-tabs-><f1-><--field 2 ---><-f3->
628 # | | | |
629 # | | | |
630 # col1 col2 col3 col4
631 #
632 # The calling routine has already broken the entire line into 3 fields as
633 # indicated. (So the work of identifying promising common tokens has
634 # already been done).
635 #
636 # In this example, there will be 2 tokens being matched: '=>' and '#'.
637 # They are the leading parts of fields 2 and 3, but we do need to know
638 # what they are so that we can dump a group of lines when these tokens
639 # change.
640 #
641 # The fields contain the actual characters of each field. The patterns
642 # are like the fields, but they contain mainly token types instead
643 # of tokens, so they have fewer characters. They are used to be
644 # sure we are matching fields of similar type.
645 #
646 # In this example, there will be 4 column indexes being adjusted. The
647 # first one is always at zero. The interior columns are at the start of
648 # the matching tokens, and the last one tracks the maximum line length.
649 #
650 # Each time a new line comes in, it joins the current vertical
651 # group if possible. Otherwise it causes the current group to be flushed
652 # and a new group is started.
653 #
654 # For each new group member, the column locations are increased, as
655 # necessary, to make room for the new fields. When the group is finally
656 # output, these column numbers are used to compute the amount of spaces of
657 # padding needed for each field.
658 #
659 # Programming note: the fields are assumed not to have any tab characters.
660 # Tabs have been previously removed except for tabs in quoted strings and
661 # side comments. Tabs in these fields can mess up the column counting.
662 # The log file warns the user if there are any such tabs.
663  
664 # Unpack the call args. This form is significantly faster than getting them
665 # one-by-one.
666 my (
667  
668 $Kend,
669 $break_alignment_after,
670 $break_alignment_before,
671 $ci_level,
672 $forget_side_comment,
673 $indentation,
674 $is_terminal_ternary,
675 $level,
676 $level_end,
677 $list_seqno,
678 $maximum_line_length,
679 $outdent_long_lines,
680 $rline_alignment,
681 $rvertical_tightness_flags,
682  
683 ) =
684  
685 8541 @{$rcall_hash}{
686 8541 qw(
687 Kend
688 break_alignment_after
689 break_alignment_before
690 ci_level
691 forget_side_comment
692 indentation
693 is_terminal_ternary
694 level
695 level_end
696 list_seqno
697 maximum_line_length
698 outdent_long_lines
699 rline_alignment
700 rvertical_tightness_flags
701 )
702 };
703  
704 my ( $rtokens, $rfields, $rpatterns, $rfield_lengths ) =
705 8541 @{$rline_alignment};
706  
707 # The index '$Kend' is a value which passed along with the line text to sub
708 # 'write_code_line' for a convergence check.
709  
710 # number of fields is $jmax
711 # number of tokens between fields is $jmax-1
712 8541 my $jmax = @{$rfields} - 1;
713  
714 8541 ▶ my $leading_space_count =
Branch: 1 branch
BranchTrueFalseTotal
ref $indentation ? : 392 8149 8541
715 ref($indentation) ? $indentation->get_spaces() : $indentation;
716  
717 # set outdented flag to be sure we either align within statements or
718 # across statement boundaries, but not both.
719 8541 my $is_outdented =
720 $self->[_last_leading_space_count_] > $leading_space_count;
721 8541 $self->[_last_leading_space_count_] = $leading_space_count;
722  
723 # Identify a hanging side comment. Hanging side comments have an empty
724 # initial field.
725 8541 ▶ my $is_hanging_side_comment =
Condition100%6/6
$jmax == 1 && $rtokens->[0] eq '#' && $rfields->[0] =~ /^\s*$/
!ll&&!rl&&r
831817350
$jmax == 1 and $rtokens->[0] eq "#"
!ll&&!rl&&r
61892129223
MC/DC100%3/3
$jmax == 1 && $rtokens->[0] eq '#' && $rfields->[0] =~ /^\s*$/
$jmax == 1 $rtokens->[0] eq "#" $rfields->[0] =~ /^\s*$/
Truth table100%4/4
A and B && C
A= $jmax == 1B= $rtokens->[0] eq "#"C= $rfields->[0] =~ /^\s*$/
ABCresult
0XX0
10X0
1100
1111
726 ( $jmax == 1 && $rtokens->[0] eq '#' && $rfields->[0] =~ /^\s*$/ );
727  
728 # Undo outdented flag for a hanging side comment
729 8541 ▶ $is_outdented = 0 if ($is_hanging_side_comment);
Branch: 1 branch
BranchTrueFalseTotal
if $is_hanging_side_comment 50 8491 8541
730  
731 # Identify a block comment.
732 8541 ▶ my $is_block_comment = $jmax == 0 && substr( $rfields->[0], 0, 1 ) eq '#';
Condition100%3/3
$jmax == 0 && substr($rfields->[0], 0, 1) eq '#'
!ll&&!rl&&r
35444274723
MC/DC100%2/2
$jmax == 0 && substr($rfields->[0], 0, 1) eq '#'
$jmax == 0 substr($rfields->[0], 0, 1) eq '#'
Truth table100%3/3
A && B
A= $jmax == 0B= substr($rfields->[0], 0, 1) eq '#'
ABresult
0X0
100
111
733  
734 # Block comment .. update count
735 8541 ▶ if ($is_block_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_block_comment) { } 723 7818 8541
736 723 $self->[_consecutive_block_comments_]++;
737 }
738  
739 # Not a block comment ..
740 # Forget side comment column if we saw 2 or more block comments,
741 # and reset the count
742 else {
743  
744 7818 ▶ if ( $self->[_consecutive_block_comments_] > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[12] > 1) 78 7740 7818
745 78 $self->forget_side_comment();
746 }
747 7818 $self->[_consecutive_block_comments_] = 0;
748 }
749  
750 # Reset side comment location if we are entering a new block from level 0.
751 # This is intended to keep them from drifting too far to the right.
752 8541 ▶ if ($forget_side_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forget_side_comment) 46 8495 8541
753 46 $self->forget_side_comment();
754 }
755  
756 8541 my $is_balanced_line = $level_end == $level;
757  
758 8541 my $group_level = $self->[_group_level_];
759 8541 my $group_maximum_line_length = $self->[_group_maximum_line_length_];
760  
761 8541 DEBUG_VALIGN && do {
762 my $nlines = $self->group_line_count();
763 print {*STDOUT}
764 "Entering valign_input: lines=$nlines new #fields= $jmax, leading_count=$leading_space_count, level=$level, group_level=$group_level, level_end=$level_end\n";
765 };
766  
767 # Validate cached line if necessary: If we can produce a container
768 # with just 2 lines total by combining an existing cached opening
769 # token with the closing token to follow, then we will mark both
770 # cached flags as valid.
771 8541 my $cached_line_type = get_cached_line_type();
772 8541 ▶ if ($cached_line_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_line_type) 227 8314 8541
773 227 my $cached_line_opening_flag = get_cached_line_opening_flag();
774 227 ▶ if ($rvertical_tightness_flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rvertical_tightness_flags) 157 70 227
775 157 my $cached_seqno = get_cached_seqno();
776 157 ▶ if ( $cached_seqno
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_seqno and $rvertical_tightness_flags->{"_vt_seqno"} and $rvertical_tightness_flags->{"_vt_seqno"} == $cached_seqno) 4 153 157
Condition100%6/6
$cached_seqno and $rvertical_tightness_flags->{"_vt_seqno"} and $rvertical_tightness_flags->{"_vt_seqno"} == $cached_seqno
!ll&&!rl&&r
126274
$cached_seqno and $rvertical_tightness_flags->{"_vt_seqno"}
!ll&&!rl&&r
1611031
MC/DC100%3/3
$cached_seqno and $rvertical_tightness_flags->{"_vt_seqno"} and $rvertical_tightness_flags->{"_vt_seqno"} == $cached_seqno
$cached_seqno $rvertical_tightness_flags->{"_vt_seqno"} $rvertical_tightness_flags->{"_vt_seqno"} == $cached_seqno
Truth table100%4/4
A and B and C
A= $cached_seqnoB= $rvertical_tightness_flags->{"_vt_seqno"}C= $rvertical_tightness_flags->{"_vt_seqno"} == $cached_seqno
ABCresult
0XX0
10X0
1100
1111
777 && $rvertical_tightness_flags->{_vt_seqno}
778 && $rvertical_tightness_flags->{_vt_seqno} == $cached_seqno )
779 {
780  
781 # Fix for b1187 and b1188: Normally this step is only done
782 # if the number of existing lines is 0 or 1. But to prevent
783 # blinking, this range can be controlled by the caller.
784 # If zero values are given we fall back on the range 0 to 1.
785 4 my $line_count = $self->group_line_count();
786 4 my $min_lines = $rvertical_tightness_flags->{_vt_min_lines};
787 4 my $max_lines = $rvertical_tightness_flags->{_vt_max_lines};
788 4 ▶ $min_lines = 0 if ( !$min_lines );
Branch: 1 branch
BranchTrueFalseTotal
unless $min_lines 4 0 4
789 4 ▶ $max_lines = 1 if ( !$max_lines );
Branch: 1 branch
BranchTrueFalseTotal
unless $max_lines 4 0 4
790 4 ▶ if ( ( $line_count >= $min_lines )
Branch: 1 branch
BranchTrueFalseTotal
if ($line_count >= $min_lines and $line_count <= $max_lines) 3 1 4
Condition66%2/3
$line_count >= $min_lines and $line_count <= $max_lines
!ll&&!rl&&r
013
MC/DC50%1/2
$line_count >= $min_lines and $line_count <= $max_lines
$line_count >= $min_lines $line_count <= $max_lines
Truth table66%2/3
A and B
A= $line_count >= $min_linesB= $line_count <= $max_lines
ABresult
0X0
100
111
791 && ( $line_count <= $max_lines ) )
792 {
793 3 ▶ $rvertical_tightness_flags->{_vt_valid_flag} ||= 1;
Condition50%1/2
$rvertical_tightness_flags->{"_vt_valid_flag"} ||= 1
l!l
03
MC/DC0%0/1
$rvertical_tightness_flags->{"_vt_valid_flag"} ||= 1
$rvertical_tightness_flags->{"_vt_valid_flag"}
Truth table50%1/2
A
A= $rvertical_tightness_flags->{"_vt_valid_flag"}
Aresult
11
00
794 3 set_cached_line_valid(1);
795 }
796 }
797 }
798  
799 # do not join an opening block brace (type 3, see VTFLAGS)
800 # with an unbalanced line unless requested with a flag value of 2
801 227 ▶ if ( $cached_line_type == 3
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_line_type == 3 and not $self->group_line_count and $cached_line_opening_flag < 2 and not $is_balanced_line) 0 227 227
Condition77%7/9
$cached_line_type == 3 and not $self->group_line_count and $cached_line_opening_flag < 2 and not $is_balanced_line
!ll&&!rl&&r
216110
$cached_line_type == 3 and not $self->group_line_count and $cached_line_opening_flag < 2
!ll&&!rl&&r
216011
$cached_line_type == 3 and not $self->group_line_count
!ll&&!rl&&r
208811
MC/DC0%0/4
$cached_line_type == 3 and not $self->group_line_count and $cached_line_opening_flag < 2 and not $is_balanced_line
$cached_line_type == 3 not $self->group_line_count $cached_line_opening_flag < 2 not $is_balanced_line
Truth table60%3/5
A and not(B) and C and not(D)
A= $cached_line_type == 3B= not $self->group_line_countC= $cached_line_opening_flag < 2D= not $is_balanced_line
ABCDresult
0XXX0
10XX0
110X0
11100
11111
802 && !$self->group_line_count()
803 && $cached_line_opening_flag < 2
804 && !$is_balanced_line )
805 {
806 0 set_cached_line_valid(0);
807 }
808 }
809  
810 # shouldn't happen:
811 8541 ▶ if ( $level < 0 ) { $level = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($level < 0) 0 8541 8541
812  
813 # do not align code across indentation level changes
814 # or changes in the maximum line length
815 # or if vertical alignment is turned off
816 8541 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($level != $group_level or $group_maximum_line_length and $maximum_line_length != $group_maximum_line_length or $is_outdented or $is_block_comment and not $rOpts_valign_block_comments or not $is_block_comment || $rOpts_valign_side_comments || $rOpts_valign_code) 15 5266 5281
Condition91%22/24
$level != $group_level or $group_maximum_line_length and $maximum_line_length != $group_maximum_line_length or $is_outdented or $is_block_comment and not $rOpts_valign_block_comments or not $is_block_comment || $rOpts_valign_side_comments || $rOpts_valign_code
l!l&&r!l&&!r
3262135266
$level != $group_level or $group_maximum_line_length and $maximum_line_length != $group_maximum_line_length or $is_outdented or $is_block_comment and not $rOpts_valign_block_comments
l!l&&r!l&&!r
326025279
$level != $group_level or $group_maximum_line_length and $maximum_line_length != $group_maximum_line_length or $is_outdented
l!l&&r!l&&!r
30961645281
$level != $group_level or $group_maximum_line_length and $maximum_line_length != $group_maximum_line_length
l!l&&r!l&&!r
309605445
$group_maximum_line_length and $maximum_line_length != $group_maximum_line_length
!ll&&!rl&&r
160438410
$is_block_comment and not $rOpts_valign_block_comments
!ll&&!rl&&r
46706092
$is_block_comment || $rOpts_valign_side_comments || $rOpts_valign_code
l!l&&r!l&&!r
5257913
$is_block_comment or $rOpts_valign_side_comments
l!l&&r!l&&!r
609464822
MC/DC77%7/9
$level != $group_level or $group_maximum_line_length and $maximum_line_length != $group_maximum_line_length or $is_outdented or $is_block_comment and not $rOpts_valign_block_comments or not $is_block_comment || $rOpts_valign_side_comments || $rOpts_valign_code
$level != $group_level $group_maximum_line_length $maximum_line_length != $group_maximum_line_length $is_outdented $is_block_comment not $rOpts_valign_block_comments $is_block_comment $rOpts_valign_side_comments $rOpts_valign_code
Truth table54%12/22
A or B and C or D or E and not(F) or not(G or H || I)
A= $level != $group_levelB= $group_maximum_line_lengthC= $maximum_line_length != $group_maximum_line_lengthD= $is_outdentedE= $is_block_commentF= not $rOpts_valign_block_commentsG= $is_block_commentH= $rOpts_valign_side_commentsI= $rOpts_valign_code
ABCDEFGHIresult
1XXXXXXXX1
011XXXXXX1
00X1XXXXX1
0101XXXXX1
00X011XXX1
010011XXX1
00X00X0001
00X0100001
01000X0001
0100100001
00X00X1XX0
00X00X01X0
00X00X0010
00X0101XX0
00X01001X0
00X0100010
01000X1XX0
01000X01X0
01000X0010
0100101XX0
01001001X0
0100100010
817 $level != $group_level
818 || ( $group_maximum_line_length
819 && $maximum_line_length != $group_maximum_line_length )
820 || $is_outdented
821 || ( $is_block_comment && !$rOpts_valign_block_comments )
822 || ( !$is_block_comment
823 && !$rOpts_valign_side_comments
824 && !$rOpts_valign_code )
825 )
826 {
827  
828 $self->_flush_group_lines( $level - $group_level )
829 3275 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 1087 2188 3275
830  
831 3275 $group_level = $level;
832 3275 $self->[_group_level_] = $group_level;
833 3275 $self->[_group_maximum_line_length_] = $maximum_line_length;
834  
835 # Update leading spaces after the above flush because the leading space
836 # count may have been changed if the -icp flag is in effect
837 3275 ▶ $leading_space_count =
Branch: 1 branch
BranchTrueFalseTotal
ref $indentation ? : 187 3088 3275
838 ref($indentation) ? $indentation->get_spaces() : $indentation;
839 }
840  
841 # --------------------------------------------------------------------
842 # Collect outdentable block COMMENTS
843 # --------------------------------------------------------------------
844 8541 ▶ if ( $self->[_group_type_] eq 'COMMENT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[15] eq "COMMENT") 605 7936 8541
845 605 ▶ if ( $is_block_comment
Branch: 1 branch
BranchTrueFalseTotal
if ($is_block_comment and $outdent_long_lines and $leading_space_count == $self->[19]) { } 90 515 605
Condition66%4/6
$is_block_comment and $outdent_long_lines and $leading_space_count == $self->[19]
!ll&&!rl&&r
515090
$is_block_comment and $outdent_long_lines
!ll&&!rl&&r
515090
MC/DC33%1/3
$is_block_comment and $outdent_long_lines and $leading_space_count == $self->[19]
$is_block_comment $outdent_long_lines $leading_space_count == $self->[19]
Truth table50%2/4
A and B and C
A= $is_block_commentB= $outdent_long_linesC= $leading_space_count == $self->[19]
ABCresult
0XX0
10X0
1100
1111
846 && $outdent_long_lines
847 && $leading_space_count == $self->[_comment_leading_space_count_] )
848 {
849  
850 # Note that for a comment group we are not storing a line
851 # but rather just the text and its length.
852 90 push @{ $self->[_rgroup_lines_] },
853 [ $rfields->[0], $rfield_lengths->[0], $Kend ];
854 90 return;
855 }
856 else {
857 $self->_flush_group_lines()
858 515 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 515 0 515
859 }
860 }
861  
862 8451 my $rgroup_lines = $self->[_rgroup_lines_];
863 8451 ▶ if ( $break_alignment_before && @{$rgroup_lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($break_alignment_before and @{$rgroup_lines;}) 30 8421 8451
Condition100%3/3
$break_alignment_before and @{$rgroup_lines;}
!ll&&!rl&&r
83229930
MC/DC100%2/2
$break_alignment_before and @{$rgroup_lines;}
$break_alignment_before @{$rgroup_lines;}
Truth table100%3/3
A and B
A= $break_alignment_beforeB= @{$rgroup_lines;}
ABresult
0X0
100
111
864 30 $rgroup_lines->[-1]->{'end_group'} = 1;
865 }
866  
867 # --------------------------------------------------------------------
868 # add dummy fields for terminal ternary
869 # --------------------------------------------------------------------
870 8451 my $j_terminal_match;
871  
872 8451 ▶ if ( $is_terminal_ternary && @{$rgroup_lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_terminal_ternary and @{$rgroup_lines;}) 13 8438 8451
Condition100%3/3
$is_terminal_ternary and @{$rgroup_lines;}
!ll&&!rl&&r
8435313
MC/DC100%2/2
$is_terminal_ternary and @{$rgroup_lines;}
$is_terminal_ternary @{$rgroup_lines;}
Truth table100%3/3
A and B
A= $is_terminal_ternaryB= @{$rgroup_lines;}
ABresult
0X0
100
111
873 13 $j_terminal_match = fix_terminal_ternary(
874 {
875 old_line => $rgroup_lines->[-1],
876 rfields => $rfields,
877 rtokens => $rtokens,
878 rpatterns => $rpatterns,
879 rfield_lengths => $rfield_lengths,
880 group_level => $group_level,
881 }
882 );
883 13 $jmax = @{$rfields} - 1;
884 }
885  
886 # --------------------------------------------------------------------
887 # add dummy fields for else statement
888 # --------------------------------------------------------------------
889  
890 # Note the trailing space after 'else' here. If there were no space between
891 # the else and the next '{' then we would not be able to do vertical
892 # alignment of the '{'.
893 8451 ▶ if ( $rfields->[0] eq 'else '
Branch: 1 branch
BranchTrueFalseTotal
if ($rfields->[0] eq "else " and @{$rgroup_lines;} and $is_balanced_line) 9 8442 8451
Condition83%5/6
$rfields->[0] eq "else " and @{$rgroup_lines;} and $is_balanced_line
!ll&&!rl&&r
844209
$rfields->[0] eq "else " and @{$rgroup_lines;}
!ll&&!rl&&r
843939
MC/DC66%2/3
$rfields->[0] eq "else " and @{$rgroup_lines;} and $is_balanced_line
$rfields->[0] eq "else " @{$rgroup_lines;} $is_balanced_line
Truth table75%3/4
A and B and C
A= $rfields->[0] eq "else "B= @{$rgroup_lines;}C= $is_balanced_line
ABCresult
0XX0
10X0
1100
1111
894 12 && @{$rgroup_lines}
895 && $is_balanced_line )
896 {
897 9 $j_terminal_match = fix_terminal_else(
898 {
899 old_line => $rgroup_lines->[-1],
900 rfields => $rfields,
901 rtokens => $rtokens,
902 rpatterns => $rpatterns,
903 rfield_lengths => $rfield_lengths,
904 }
905 );
906 9 $jmax = @{$rfields} - 1;
907 }
908  
909 # --------------------------------------------------------------------
910 # Handle simple line of code with no fields to match.
911 # --------------------------------------------------------------------
912 8451 ▶ if ( $jmax <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax <= 0) { } 4895 3556 8451
913 4895 $self->[_zero_count_]++;
914  
915 # VSN PATCH for a single number, part 1.
916 4895 ▶ my $is_numeric =
Condition100%3/3
$rOpts_valign_signed_numbers && $rpatterns->[0] eq 'n,'
!ll&&!rl&&r
1485341
MC/DC100%2/2
$rOpts_valign_signed_numbers && $rpatterns->[0] eq 'n,'
$rOpts_valign_signed_numbers $rpatterns->[0] eq 'n,'
Truth table100%3/3
A && B
A= $rOpts_valign_signed_numbersB= $rpatterns->[0] eq 'n,'
ABresult
0X0
100
111
917 $rOpts_valign_signed_numbers && $rpatterns->[0] eq 'n,';
918  
919 4895 ▶ if ( !$is_numeric
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_numeric and @{$rgroup_lines;} and not get_recoverable_spaces($rgroup_lines->[0]{"indentation"})) 389 4506 4895
Condition100%6/6
not $is_numeric and @{$rgroup_lines;} and not get_recoverable_spaces($rgroup_lines->[0]{"indentation"})
!ll&&!rl&&r
449313389
not $is_numeric and @{$rgroup_lines;}
!ll&&!rl&&r
414452402
MC/DC100%3/3
not $is_numeric and @{$rgroup_lines;} and not get_recoverable_spaces($rgroup_lines->[0]{"indentation"})
not $is_numeric @{$rgroup_lines;} not get_recoverable_spaces($rgroup_lines->[0]{"indentation"})
Truth table100%4/4
not(A) and B and not(C)
A= not $is_numericB= @{$rgroup_lines;}C= not get_recoverable_spaces($rgroup_lines->[0]{"indentation"})
ABCresult
0XX0
10X0
1100
1111
920 4854 && @{$rgroup_lines}
921 && !get_recoverable_spaces( $rgroup_lines->[0]->{'indentation'} ) )
922 {
923  
924 # flush the current group if it has some aligned columns..
925 # or we haven't seen a comment lately
926 389 ▶ if ( $rgroup_lines->[0]->{'jmax'} > 1
Branch: 1 branch
BranchTrueFalseTotal
if ($rgroup_lines->[0]{"jmax"} > 1 or $self->[17] > 3) 353 36 389
Condition100%3/3
$rgroup_lines->[0]{"jmax"} > 1 or $self->[17] > 3
l!l&&r!l&&!r
3381536
MC/DC100%2/2
$rgroup_lines->[0]{"jmax"} > 1 or $self->[17] > 3
$rgroup_lines->[0]{"jmax"} > 1 $self->[17] > 3
Truth table100%3/3
A or B
A= $rgroup_lines->[0]{"jmax"} > 1B= $self->[17] > 3
ABresult
1X1
011
000
927 || $self->[_zero_count_] > 3 )
928 {
929 $self->_flush_group_lines()
930 353 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 353 0 353
931  
932 # Update '$rgroup_lines' - it will become a ref to empty array.
933 # This allows avoiding a call to get_group_line_count below.
934 353 $rgroup_lines = $self->[_rgroup_lines_];
935 }
936 }
937  
938 # start new COMMENT group if this comment may be outdented
939 4895 ▶ if ( $is_block_comment
Branch: 1 branch
BranchTrueFalseTotal
if ($is_block_comment and $outdent_long_lines and not @{$rgroup_lines;}) 602 4293 4895
Condition83%5/6
$is_block_comment and $outdent_long_lines and not @{$rgroup_lines;}
!ll&&!rl&&r
42930602
$is_block_comment and $outdent_long_lines
!ll&&!rl&&r
426231602
MC/DC66%2/3
$is_block_comment and $outdent_long_lines and not @{$rgroup_lines;}
$is_block_comment $outdent_long_lines not @{$rgroup_lines;}
Truth table75%3/4
A and B and not(C)
A= $is_block_commentB= $outdent_long_linesC= not @{$rgroup_lines;}
ABCresult
0XX0
10X0
1100
1111
940 && $outdent_long_lines
941 602 && !@{$rgroup_lines} )
942 {
943 602 $self->[_group_type_] = 'COMMENT';
944 602 $self->[_comment_leading_space_count_] = $leading_space_count;
945 602 $self->[_group_maximum_line_length_] = $maximum_line_length;
946 602 push @{$rgroup_lines},
947 [ $rfields->[0], $rfield_lengths->[0], $Kend ];
948 602 return;
949 }
950  
951 # just write this line directly if no current group, no side comment,
952 # and no space recovery is needed,
953 # and not numeric - VSN PATCH for a single number, part 4.
954 4293 ▶ if ( !@{$rgroup_lines}
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$rgroup_lines;} or $is_numeric or get_recoverable_spaces($indentation)) 4202 91 4293
Condition100%6/6
@{$rgroup_lines;} or $is_numeric or get_recoverable_spaces($indentation)
l!l&&r!l&&!r
9014202
@{$rgroup_lines;} or $is_numeric
l!l&&r!l&&!r
76144203
MC/DC100%3/3
@{$rgroup_lines;} or $is_numeric or get_recoverable_spaces($indentation)
@{$rgroup_lines;} $is_numeric get_recoverable_spaces($indentation)
Truth table100%4/4
A or B or C
A= @{$rgroup_lines;}B= $is_numericC= get_recoverable_spaces($indentation)
ABCresult
1XX1
01X1
0011
0000
955 && !$is_numeric
956 && !get_recoverable_spaces($indentation) )
957 {
958  
959 4202 $self->valign_output_step_B(
960 {
961 leading_space_count => $leading_space_count,
962 line => $rfields->[0],
963 line_length => $rfield_lengths->[0],
964 side_comment_length => 0,
965 outdent_long_lines => $outdent_long_lines,
966 rvertical_tightness_flags => $rvertical_tightness_flags,
967 level => $level,
968 level_end => $level_end,
969 Kend => $Kend,
970 maximum_line_length => $maximum_line_length,
971 }
972 );
973 4202 return;
974 }
975 }
976 else {
977 3556 $self->[_zero_count_] = 0;
978 }
979  
980 # --------------------------------------------------------------------
981 # It simplifies things to create a zero length side comment
982 # if none exists.
983 # --------------------------------------------------------------------
984 3647 ▶ if ( ( $jmax == 0 ) || ( $rtokens->[ $jmax - 1 ] ne '#' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax == 0 or $$rtokens[$jmax - 1] ne "#") 3299 348 3647
Condition100%3/3
$jmax == 0 or $$rtokens[$jmax - 1] ne "#"
l!l&&r!l&&!r
913208348
MC/DC100%2/2
$jmax == 0 or $$rtokens[$jmax - 1] ne "#"
$jmax == 0 $$rtokens[$jmax - 1] ne "#"
Truth table100%3/3
A or B
A= $jmax == 0B= $$rtokens[$jmax - 1] ne "#"
ABresult
1X1
011
000
985 3299 $jmax += 1;
986 3299 $rtokens->[ $jmax - 1 ] = '#';
987 3299 $rfields->[$jmax] = EMPTY_STRING;
988 3299 $rfield_lengths->[$jmax] = 0;
989 3299 $rpatterns->[$jmax] = '#';
990 }
991  
992 # --------------------------------------------------------------------
993 # create an object to hold this line
994 # --------------------------------------------------------------------
995  
996 # The hash keys below must match the list of keys in %valid_LINE_keys.
997 # Values in this hash are accessed directly, except for 'ralignments',
998 # rather than with get/set calls for efficiency.
999 3647 my $new_line = Perl::Tidy::VerticalAligner::Line->new(
1000 {
1001 jmax => $jmax,
1002 rtokens => $rtokens,
1003 rfields => $rfields,
1004 rpatterns => $rpatterns,
1005 rfield_lengths => $rfield_lengths,
1006 indentation => $indentation,
1007 leading_space_count => $leading_space_count,
1008 outdent_long_lines => $outdent_long_lines,
1009 list_seqno => $list_seqno,
1010 list_type => EMPTY_STRING,
1011 is_hanging_side_comment => $is_hanging_side_comment,
1012 rvertical_tightness_flags => $rvertical_tightness_flags,
1013 is_terminal_ternary => $is_terminal_ternary,
1014 j_terminal_match => $j_terminal_match,
1015 end_group => $break_alignment_after,
1016 Kend => $Kend,
1017 ci_level => $ci_level,
1018 level => $level,
1019 level_end => $level_end,
1020 imax_pair => -1,
1021 maximum_line_length => $maximum_line_length,
1022  
1023 ralignments => [],
1024 }
1025 );
1026  
1027 3647 DEVEL_MODE
1028 && check_keys( $new_line, \%valid_LINE_keys,
1029 "Checking line keys at line definition", 1 );
1030  
1031 # --------------------------------------------------------------------
1032 # Decide if this is a simple list of items.
1033 # We use this to be less restrictive in deciding what to align.
1034 # --------------------------------------------------------------------
1035 3647 ▶ decide_if_list($new_line) if ($list_seqno);
Branch: 1 branch
BranchTrueFalseTotal
if $list_seqno 1161 2486 3647
1036  
1037 # --------------------------------------------------------------------
1038 # Append this line to the current group (or start new group)
1039 # --------------------------------------------------------------------
1040  
1041 3647 push @{ $self->[_rgroup_lines_] }, $new_line;
1042 3647 $self->[_group_maximum_line_length_] = $maximum_line_length;
1043  
1044 # output this group if it ends in a terminal else or ternary line
1045 3647 ▶ if ( defined($j_terminal_match) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $j_terminal_match) { } 20 3627 3647
elsif ($level_end < $level or $is_closing_token{substr $rfields->[0], 0, 1}) { } 144 3483 3627
1046 $self->_flush_group_lines()
1047 20 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 20 0 20
1048 }
1049  
1050 # Force break after jump to lower level
1051 ▶ elsif ($level_end < $level
Condition100%3/3
$level_end < $level or $is_closing_token{substr $rfields->[0], 0, 1}
l!l&&r!l&&!r
46983483
MC/DC100%2/2
$level_end < $level or $is_closing_token{substr $rfields->[0], 0, 1}
$level_end < $level $is_closing_token{substr $rfields->[0], 0, 1}
Truth table100%3/3
A or B
A= $level_end < $levelB= $is_closing_token{substr $rfields->[0], 0, 1}
ABresult
1X1
011
000
1052 || $is_closing_token{ substr( $rfields->[0], 0, 1 ) } )
1053 {
1054 $self->_flush_group_lines(-1)
1055 144 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 144 0 144
1056 }
1057  
1058 else {
1059 ## ok: no output needed
1060 }
1061  
1062 # --------------------------------------------------------------------
1063 # Some old debugging stuff
1064 # --------------------------------------------------------------------
1065 3647 DEBUG_VALIGN && do {
1066 print {*STDOUT} "exiting valign_input fields:";
1067 dump_array( @{$rfields} );
1068 print {*STDOUT} "exiting valign_input tokens:";
1069 dump_array( @{$rtokens} );
1070 print {*STDOUT} "exiting valign_input patterns:";
1071 dump_array( @{$rpatterns} );
1072 };
1073  
1074 3647 return;
1075 } ## end sub valign_input
1076  
1077 sub join_hanging_comment {
1078  
1079 38 ▶ my ( $new_line, $old_line ) = @_;
Subroutine join_hanging_comment: called CC=6 SCAR=18.0
Pod undocumented
1080  
1081 # Add dummy fields to a hanging side comment to make it look
1082 # like the first line in its potential group. This simplifies
1083 # the coding.
1084  
1085 # Given:
1086 # $new_line = ref to hash of the line to be possibly changed
1087 # $old_line = ref to hash of the previous reference line
1088 # Return:
1089 # true if new line modified
1090 # false otherwise
1091  
1092 38 my $jmax = $new_line->{'jmax'};
1093  
1094 # must be 2 fields
1095 38 ▶ return 0 unless ( $jmax == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless $jmax == 1 0 38 38
1096 38 my $rtokens = $new_line->{'rtokens'};
1097  
1098 # the second field must be a comment
1099 38 ▶ return 0 unless ( $rtokens->[0] eq '#' );
Branch: 1 branch
BranchTrueFalseTotal
unless $rtokens->[0] eq "#" 0 38 38
1100 38 my $rfields = $new_line->{'rfields'};
1101  
1102 # the first field must be empty
1103 38 ▶ return 0 if ( $rfields->[0] !~ /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $rfields->[0] =~ /^\s*$/ 0 38 38
1104  
1105 # the current line must have fewer fields
1106 38 my $maximum_field_index = $old_line->{'jmax'};
1107 38 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $maximum_field_index <= $jmax 35 3 38
1108 if ( $maximum_field_index <= $jmax );
1109  
1110 # looks ok..
1111 3 my $rpatterns = $new_line->{'rpatterns'};
1112 3 my $rfield_lengths = $new_line->{'rfield_lengths'};
1113  
1114 3 $new_line->{'is_hanging_side_comment'} = 1;
1115  
1116 3 $jmax = $maximum_field_index;
1117 3 $new_line->{'jmax'} = $jmax;
1118 3 $rfields->[$jmax] = $rfields->[1];
1119 3 $rfield_lengths->[$jmax] = $rfield_lengths->[1];
1120 3 $rtokens->[ $jmax - 1 ] = $rtokens->[0];
1121 3 $rpatterns->[ $jmax - 1 ] = $rpatterns->[0];
1122  
1123 3 foreach my $j ( 1 .. $jmax - 1 ) {
1124 3 $rfields->[$j] = EMPTY_STRING;
1125 3 $rfield_lengths->[$j] = 0;
1126 3 $rtokens->[ $j - 1 ] = EMPTY_STRING;
1127 3 $rpatterns->[ $j - 1 ] = EMPTY_STRING;
1128 }
1129 3 return 1;
1130 } ## end sub join_hanging_comment
1131  
1132 sub decide_if_list {
1133  
1134 1205 ▶ my $line = shift;
Subroutine decide_if_list: called CC=4 SCAR=13.9
Pod undocumented
1135  
1136 # Given:
1137 # $line = ref to hash of values for a line
1138 # Task:
1139 # Set 'list_type' property
1140  
1141 # A list will be taken to be a line with a forced break in which all
1142 # of the field separators are commas or comma-arrows (except for the
1143 # trailing #)
1144  
1145 1205 my $rtokens = $line->{'rtokens'};
1146 1205 my $test_token = $rtokens->[0];
1147 1205 my ( $raw_tok, $lev, $tag, $tok_count ) =
1148 decode_alignment_token($test_token);
1149 1205 ▶ if ( $is_comma_token{$raw_tok} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_comma_token{$raw_tok}) 1040 165 1205
1150 1040 my $list_type = $test_token;
1151 1040 my $jmax = $line->{'jmax'};
1152  
1153 1040 foreach ( 1 .. $jmax - 2 ) {
1154 1077 ( $raw_tok, $lev, $tag, $tok_count ) =
1155 decode_alignment_token( $rtokens->[$_] );
1156 1077 ▶ if ( !$is_comma_token{$raw_tok} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_comma_token{$raw_tok}) 26 1051 1077
1157 26 $list_type = EMPTY_STRING;
1158 26 last;
1159 }
1160 }
1161 1040 $line->{'list_type'} = $list_type;
1162 }
1163 1205 return;
1164 } ## end sub decide_if_list
1165  
1166 sub fix_terminal_ternary {
1167  
1168 # Add empty fields as necessary to align a ternary term
1169 # like this:
1170 #
1171 # my $leapyear =
1172 # $year % 4 ? 0
1173 # : $year % 100 ? 1
1174 # : $year % 400 ? 0
1175 # : 1;
1176 #
1177 # returns the index of the terminal question token, if any
1178  
1179 13 ▶ my ($rcall_hash) = @_;
Subroutine fix_terminal_ternary: called CC=21 SCAR=37.9
Pod undocumented
1180  
1181 13 my $old_line = $rcall_hash->{old_line};
1182 13 my $rfields = $rcall_hash->{rfields};
1183 13 my $rtokens = $rcall_hash->{rtokens};
1184 13 my $rpatterns = $rcall_hash->{rpatterns};
1185 13 my $rfield_lengths = $rcall_hash->{rfield_lengths};
1186 13 my $group_level = $rcall_hash->{group_level};
1187  
1188 13 ▶ return if ( !$old_line );
Branch: 1 branch
BranchTrueFalseTotal
unless $old_line 0 13 13
1189 45 ▶ use constant EXPLAIN_TERNARY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1190  
1191 13 ▶ if (%valign_control_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (%valign_control_hash) 0 13 13
1192 0 my $align_ok = $valign_control_hash{'?'};
1193 0 ▶ $align_ok = $valign_control_default unless ( defined($align_ok) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $align_ok 0 0 0
1194 0 ▶ return if ( !$align_ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $align_ok 0 0 0
1195 }
1196  
1197 13 my $jmax = @{$rfields} - 1;
1198 13 my $rfields_old = $old_line->{'rfields'};
1199  
1200 13 my $rpatterns_old = $old_line->{'rpatterns'};
1201 13 my $rtokens_old = $old_line->{'rtokens'};
1202 13 my $maximum_field_index = $old_line->{'jmax'};
1203  
1204 # look for the question mark after the :
1205 13 my ($jquestion);
1206 my $depth_question;
1207 13 my $pad = EMPTY_STRING;
1208 13 my $pad_length = 0;
1209 13 foreach my $j ( 0 .. $maximum_field_index - 1 ) {
1210 14 my $tok = $rtokens_old->[$j];
1211 14 my ( $raw_tok, $lev, $tag_uu, $tok_count_uu ) =
1212 decode_alignment_token($tok);
1213 14 ▶ if ( $raw_tok eq '?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok eq "?") 13 1 14
1214 13 $depth_question = $lev;
1215  
1216 # depth must be correct
1217 13 ▶ next if ( $depth_question ne $group_level );
Branch: 1 branch
BranchTrueFalseTotal
if $depth_question ne $group_level 0 13 13
1218  
1219 13 $jquestion = $j;
1220 13 ▶ if ( $rfields_old->[ $j + 1 ] =~ /^(\?\s*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rfields_old[$j + 1] =~ /^(\?\s*)/) { } 13 0 13
1221 13 $pad_length = length($1);
1222 13 $pad = SPACE x $pad_length;
1223 }
1224 else {
1225 0 return; # shouldn't happen
1226 }
1227 13 last;
1228 }
1229 }
1230 13 ▶ return if ( !defined($jquestion) ); # shouldn't happen
Branch: 1 branch
BranchTrueFalseTotal
unless defined $jquestion 0 13 13
1231  
1232 # Now splice the tokens and patterns of the previous line
1233 # into the else line to insure a match. Add empty fields
1234 # as necessary.
1235 13 my $jadd = $jquestion;
1236  
1237 # Work on copies of the actual arrays in case we have
1238 # to return due to an error
1239 13 my @fields = @{$rfields};
1240 13 my @patterns = @{$rpatterns};
1241 13 my @tokens = @{$rtokens};
1242 13 my @field_lengths = @{$rfield_lengths};
1243  
1244 13 EXPLAIN_TERNARY && do {
1245 local $LIST_SEPARATOR = '><';
1246 print {*STDOUT} "CURRENT FIELDS=<@{$rfields_old}>\n";
1247 print {*STDOUT} "CURRENT TOKENS=<@{$rtokens_old}>\n";
1248 print {*STDOUT} "CURRENT PATTERNS=<@{$rpatterns_old}>\n";
1249 print {*STDOUT} "UNMODIFIED FIELDS=<@{$rfields}>\n";
1250 print {*STDOUT} "UNMODIFIED TOKENS=<@{$rtokens}>\n";
1251 print {*STDOUT} "UNMODIFIED PATTERNS=<@{$rpatterns}>\n";
1252 };
1253  
1254 # handle cases of leading colon on this line
1255 13 ▶ if ( $fields[0] =~ /^(:\s*)(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[0] =~ /^(:\s*)(.*)$/) { } 13 0 13
1256  
1257 13 my ( $colon, $therest ) = ( $1, $2 );
1258  
1259 # Handle sub-case of first field with leading colon plus additional code
1260 # This is the usual situation as at the '1' below:
1261 # ...
1262 # : $year % 400 ? 0
1263 # : 1;
1264 13 ▶ if ($therest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($therest) { } 13 0 13
1265  
1266 # Split the first field after the leading colon and insert padding.
1267 # Note that this padding will remain even if the terminal value goes
1268 # out on a separate line. This does not seem to look to bad, so no
1269 # mechanism has been included to undo it.
1270 13 my $field1_uu = shift @fields;
1271 13 my $field_length1 = shift @field_lengths;
1272 13 my $len_colon = length($colon);
1273 13 unshift @fields, ( $colon, $pad . $therest );
1274 13 unshift @field_lengths,
1275 ( $len_colon, $pad_length + $field_length1 - $len_colon );
1276  
1277 # change the leading pattern from : to ?
1278 13 ▶ return if ( $patterns[0] !~ s/^\:/?/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $patterns[0] =~ s/^\:/?/ 0 13 13
1279  
1280 # install leading tokens and patterns of existing line
1281 13 unshift @tokens, @{$rtokens_old}[ 0 .. $jquestion ];
1282 13 unshift @patterns, @{$rpatterns_old}[ 0 .. $jquestion ];
1283  
1284 # insert appropriate number of empty fields
1285 13 ▶ splice( @fields, 1, 0, (EMPTY_STRING) x $jadd ) if ($jadd);
Branch: 1 branch
BranchTrueFalseTotal
if $jadd 1 12 13
1286 13 ▶ splice( @field_lengths, 1, 0, (0) x $jadd ) if ($jadd);
Branch: 1 branch
BranchTrueFalseTotal
if $jadd 1 12 13
1287 }
1288  
1289 # handle sub-case of first field just equal to leading colon.
1290 # This can happen for example in the example below where
1291 # the leading '(' would create a new alignment token
1292 # : ( $name =~ /[]}]$/ ) ? ( $mname = $name )
1293 # : ( $mname = $name . '->' );
1294 else {
1295  
1296 0 ▶ return if ( $jmax <= 0 || $tokens[0] eq '#' ); # shouldn't happen
Branch: 1 branch
BranchTrueFalseTotal
if $jmax <= 0 or $tokens[0] eq "#" 0 0 0
Condition0%0/3
$jmax <= 0 or $tokens[0] eq "#"
l!l&&r!l&&!r
000
MC/DC0%0/2
$jmax <= 0 or $tokens[0] eq "#"
$jmax <= 0 $tokens[0] eq "#"
Truth table0%0/3
A or B
A= $jmax <= 0B= $tokens[0] eq "#"
ABresult
1X1
011
000
1297  
1298 # prepend a leading ? onto the second pattern
1299 0 $patterns[1] = "?b" . $patterns[1];
1300  
1301 # pad the second field
1302 0 $fields[1] = $pad . $fields[1];
1303 0 $field_lengths[1] = $pad_length + $field_lengths[1];
1304  
1305 # install leading tokens and patterns of existing line, replacing
1306 # leading token and inserting appropriate number of empty fields
1307 0 splice( @tokens, 0, 1, @{$rtokens_old}[ 0 .. $jquestion ] );
1308 0 splice( @patterns, 1, 0, @{$rpatterns_old}[ 1 .. $jquestion ] );
1309 0 ▶ splice( @fields, 1, 0, (EMPTY_STRING) x $jadd ) if ($jadd);
Branch: 1 branch
BranchTrueFalseTotal
if $jadd 0 0 0
1310 0 ▶ splice( @field_lengths, 1, 0, (0) x $jadd ) if ($jadd);
Branch: 1 branch
BranchTrueFalseTotal
if $jadd 0 0 0
1311 }
1312 }
1313  
1314 # Handle case of no leading colon on this line. This will
1315 # be the case when -wba=':' is used. For example,
1316 # $year % 400 ? 0 :
1317 # 1;
1318 else {
1319  
1320 # install leading tokens and patterns of existing line
1321 0 $patterns[0] = '?' . 'b' . $patterns[0];
1322 0 unshift @tokens, @{$rtokens_old}[ 0 .. $jquestion ];
1323 0 unshift @patterns, @{$rpatterns_old}[ 0 .. $jquestion ];
1324  
1325 # insert appropriate number of empty fields
1326 0 $jadd = $jquestion + 1;
1327 0 $fields[0] = $pad . $fields[0];
1328 0 $field_lengths[0] = $pad_length + $field_lengths[0];
1329 0 ▶ splice( @fields, 0, 0, (EMPTY_STRING) x $jadd ) if ($jadd);
Branch: 1 branch
BranchTrueFalseTotal
if $jadd 0 0 0
1330 0 ▶ splice( @field_lengths, 0, 0, (0) x $jadd ) if ($jadd);
Branch: 1 branch
BranchTrueFalseTotal
if $jadd 0 0 0
1331 }
1332  
1333 13 EXPLAIN_TERNARY && do {
1334 local $LIST_SEPARATOR = '><';
1335 print {*STDOUT} "MODIFIED TOKENS=<@tokens>\n";
1336 print {*STDOUT} "MODIFIED PATTERNS=<@patterns>\n";
1337 print {*STDOUT} "MODIFIED FIELDS=<@fields>\n";
1338 };
1339  
1340 # all ok .. update the arrays
1341 13 @{$rfields} = @fields;
1342 13 @{$rtokens} = @tokens;
1343 13 @{$rpatterns} = @patterns;
1344 13 @{$rfield_lengths} = @field_lengths;
1345  
1346 # force a flush after this line
1347 13 return $jquestion;
1348 } ## end sub fix_terminal_ternary
1349  
1350 sub fix_terminal_else {
1351  
1352 9 ▶ my ($rcall_hash) = @_;
Subroutine fix_terminal_else: called CC=13 SCAR=27.4
Pod undocumented
1353  
1354 # Add empty fields as necessary to align a balanced terminal
1355 # else block to a previous if/elsif/unless block,
1356 # like this:
1357 #
1358 # if ( 1 || $x ) { print "ok 13\n"; }
1359 # else { print "not ok 13\n"; }
1360 #
1361 # returns a positive value if the else block should be indented
1362 #
1363  
1364 9 my $old_line = $rcall_hash->{old_line};
1365 9 my $rfields = $rcall_hash->{rfields};
1366 9 my $rtokens = $rcall_hash->{rtokens};
1367 9 my $rpatterns = $rcall_hash->{rpatterns};
1368 9 my $rfield_lengths = $rcall_hash->{rfield_lengths};
1369  
1370 9 ▶ return if ( !$old_line );
Branch: 1 branch
BranchTrueFalseTotal
unless $old_line 0 9 9
1371 9 my $jmax = @{$rfields} - 1;
1372 9 ▶ return if ( $jmax <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $jmax <= 0 0 9 9
1373  
1374 9 ▶ if (%valign_control_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (%valign_control_hash) 0 9 9
1375 0 my $align_ok = $valign_control_hash{'{'};
1376 0 ▶ $align_ok = $valign_control_default unless ( defined($align_ok) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $align_ok 0 0 0
1377 0 ▶ return if ( !$align_ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $align_ok 0 0 0
1378 }
1379  
1380 # check for balanced else block following if/elsif/unless
1381 9 my $rfields_old = $old_line->{'rfields'};
1382  
1383 # TBD: add handling for 'case'
1384 9 ▶ return if ( $rfields_old->[0] !~ /^(?:if|elsif|unless)\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $rfields_old->[0] =~ /^(?:if|elsif|unless)\s*$/ 2 7 9
1385  
1386 # look for the opening brace after the else, and extract the depth
1387 7 my $tok_brace = $rtokens->[0];
1388 7 my $depth_brace;
1389 7 ▶ if ( $tok_brace =~ /^\{(\d+)/ ) { $depth_brace = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($tok_brace =~ /^\{(\d+)/) { } 7 0 7
1390  
1391 # probably: "else # side_comment"
1392 0 else { return }
1393  
1394 7 my $rpatterns_old = $old_line->{'rpatterns'};
1395 7 my $rtokens_old = $old_line->{'rtokens'};
1396 7 my $maximum_field_index = $old_line->{'jmax'};
1397  
1398 # be sure the previous if/elsif is followed by an opening paren
1399 7 my $jparen = 0;
1400 7 my $tok_paren = '(' . $depth_brace;
1401 7 my $tok_test = $rtokens_old->[$jparen];
1402 7 ▶ if ( $tok_test ne $tok_paren ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok_test ne $tok_paren) 0 7 7
1403 ## no opening paren - possible syntax error - give up.
1404 0 return;
1405 }
1406  
1407 # Now find the opening block brace
1408 7 my ($jbrace);
1409 7 foreach my $j ( 1 .. $maximum_field_index - 1 ) {
1410 8 my $tok = $rtokens_old->[$j];
1411 8 ▶ if ( $tok eq $tok_brace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok eq $tok_brace) 7 1 8
1412 7 $jbrace = $j;
1413 7 last;
1414 }
1415 }
1416 7 ▶ if ( !defined($jbrace) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $jbrace) 0 7 7
1417 ## no opening brace - possible syntax error - give up.
1418 0 return;
1419 }
1420  
1421 # Now splice the tokens and patterns of the previous line
1422 # into the else line to insure a match. Add empty fields
1423 # as necessary.
1424 7 my $jadd = $jbrace - $jparen;
1425 7 splice( @{$rtokens}, 0, 0, @{$rtokens_old}[ $jparen .. $jbrace - 1 ] );
1426 7 splice( @{$rpatterns}, 1, 0, @{$rpatterns_old}[ $jparen + 1 .. $jbrace ] );
1427 7 splice( @{$rfields}, 1, 0, (EMPTY_STRING) x $jadd );
1428 7 splice( @{$rfield_lengths}, 1, 0, (0) x $jadd );
1429  
1430 # force a flush after this line if it does not follow a case
1431 7 ▶ if ( $rfields_old->[0] =~ /^case\s*$/ ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($rfields_old->[0] =~ /^case\s*$/) { } 0 7 7
1432 7 else { return $jbrace }
1433 } ## end sub fix_terminal_else
1434  
1435 45 my %is_closing_block_type;
1436  
1437 BEGIN {
1438 45 ▶ $is_closing_block_type{$_} = 1 for qw# } ] #;
Subroutine BEGIN: called CC=2 SCAR=6.9
1439 }
1440  
1441 # This is a flag for testing alignment by sub sweep_left_to_right only.
1442 # This test can help find problems with the alignment logic.
1443 # This flag should normally be zero.
1444 45 ▶use constant TEST_SWEEP_ONLY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1445  
1446 45 ▶use constant EXPLAIN_CHECK_MATCH => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1447  
1448 sub check_match {
1449  
1450 1331 ▶ my ( $self, $new_line, $base_line, $prev_line, $group_line_count ) = @_;
Subroutine check_match: called CC=13 SCAR=26.0
Pod undocumented
1451  
1452 # See if the current line matches the current vertical alignment group.
1453  
1454 # Given:
1455 # $new_line = the line being considered for group inclusion
1456 # $base_line = the first line of the current group
1457 # $prev_line = the line just before $new_line
1458 # $group_line_count = number of lines in the current group
1459  
1460 # Returns: a flag and a value as follows:
1461 # return (0, $imax_align) if the line does not match
1462 # return (1, $imax_align) if the line matches but does not fit
1463 # return (2, $imax_align) if the line matches and fits
1464  
1465 45 ▶ use constant NO_MATCH => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1466 45 ▶ use constant MATCH_NO_FIT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1467 45 ▶ use constant MATCH_AND_FIT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1468  
1469 # Return value '$return_value' describes the match with 3 possible values
1470 1331 my $return_value;
1471  
1472 # Return value '$imax_align' is the index of the maximum matching token.
1473 # It will be used in the subsequent left-to-right sweep to align as many
1474 # tokens as possible for lines which partially match.
1475 1331 my $imax_align = -1;
1476  
1477 # variable $GoToMsg explains reason for no match, for debugging
1478 1331 my $GoToMsg = EMPTY_STRING;
1479  
1480 1331 my $jmax = $new_line->{'jmax'};
1481 1331 my $maximum_field_index = $base_line->{'jmax'};
1482  
1483 1331 my $jlimit = $jmax - 2;
1484 1331 ▶ if ( $jmax > $maximum_field_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax > $maximum_field_index) 103 1228 1331
1485 103 $jlimit = $maximum_field_index - 2;
1486 }
1487  
1488 1331 ▶ if ( $new_line->{'is_hanging_side_comment'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_line->{'is_hanging_side_comment'}) { } 38 1293 1331
1489  
1490 # HSC's can join the group if they fit
1491 }
1492  
1493 # Everything else
1494 else {
1495  
1496 # A group with hanging side comments ends with the first non hanging
1497 # side comment.
1498 1293 ▶ if ( $base_line->{'is_hanging_side_comment'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_line->{'is_hanging_side_comment'}) { } 0 1293 1293
1499 0 $GoToMsg = "end of hanging side comments";
1500 0 $return_value = NO_MATCH;
1501 }
1502 else {
1503  
1504 # The number of tokens that this line shares with the previous
1505 # line has been stored with the previous line. This value was
1506 # calculated and stored by sub 'match_line_pair'.
1507 1293 $imax_align = $prev_line->{'imax_pair'};
1508  
1509 # Only the following ci sequences are accepted (issue c225):
1510 # 0 0 0 ... OK
1511 # 0 1 1 ... OK but marginal*
1512 # 1 1 1 ... OK
1513 # This check is rarely activated, but for example we want
1514 # to avoid something like this 'tail wag dog' situation:
1515 # $tag =~ s/\b([a-z]+)/\L\u$1/gio;
1516 # $tag =~ s/\b([b-df-hj-np-tv-z]+)\b/\U$1/gio
1517 # if $tag =~ /-/;
1518 # *Note: we could set a flag for the 0 1 marginal case and
1519 # use it to prevent alignment of selected token types.
1520 1293 my $ci_prev = $prev_line->{'ci_level'};
1521 1293 my $ci_new = $new_line->{'ci_level'};
1522 1293 ▶ if ( $ci_prev != $ci_new
Branch: 2 branches
BranchTrueFalseTotal
if ($ci_prev != $ci_new and $imax_align >= 0 and $ci_new == 0 || $group_line_count > 1) { } 0 1293 1293
elsif ($imax_align != $jlimit) { } 39 1254 1293
Condition66%6/9
$ci_prev != $ci_new and $imax_align >= 0 and $ci_new == 0 || $group_line_count > 1
!ll&&!rl&&r
128670
$ci_prev != $ci_new and $imax_align >= 0
!ll&&!rl&&r
127887
$ci_new == 0 or $group_line_count > 1
l!l&&r!l&&!r
007
MC/DC0%0/4
$ci_prev != $ci_new and $imax_align >= 0 and $ci_new == 0 || $group_line_count > 1
$ci_prev != $ci_new $imax_align >= 0 $ci_new == 0 $group_line_count > 1
Truth table60%3/5
A and B and C or D
A= $ci_prev != $ci_newB= $imax_align >= 0C= $ci_new == 0D= $group_line_count > 1
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1523 && $imax_align >= 0
1524 && ( $ci_new == 0 || $group_line_count > 1 ) )
1525 {
1526 0 $imax_align = -1;
1527 0 $GoToMsg =
1528 "Rejected ci: ci_prev=$ci_prev ci_new=$ci_new num=$group_line_count\n";
1529 0 $return_value = NO_MATCH;
1530 }
1531 elsif ( $imax_align != $jlimit ) {
1532 39 $GoToMsg = "Not all tokens match: $imax_align != $jlimit\n";
1533 39 $return_value = NO_MATCH;
1534 }
1535 else {
1536 ## ok: continue
1537 }
1538 }
1539 }
1540  
1541 1331 ▶ if ( !defined($return_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $return_value) 1292 39 1331
1542  
1543 # The tokens match, but the lines must have identical number of
1544 # tokens to join the group.
1545 1292 ▶ if ( $maximum_field_index != $jmax ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($maximum_field_index != $jmax) { } 143 1149 1292
elsif ($self->check_fit($new_line, $base_line) and !0) { } 1135 14 1149
1546 143 $GoToMsg = "token count differs";
1547 143 $return_value = NO_MATCH;
1548 }
1549  
1550 # The tokens match. Now See if there is space for this line in the
1551 # current group.
1552 ▶ elsif ( $self->check_fit( $new_line, $base_line ) && !TEST_SWEEP_ONLY )
Condition100%2/2
$self->check_fit($new_line, $base_line) and
!ll
141135
MC/DC100%1/1
$self->check_fit($new_line, $base_line) and
$self->check_fit($new_line, $base_line)
Truth table100%2/2
A
A= $self->check_fit($new_line, $base_line)
Aresult
00
11
1553 {
1554  
1555 1135 $GoToMsg = "match and fit, imax_align=$imax_align, jmax=$jmax\n";
1556 1135 $return_value = MATCH_AND_FIT;
1557 1135 $imax_align = $jlimit;
1558 }
1559 else {
1560 14 $GoToMsg = "match but no fit, imax_align=$imax_align, jmax=$jmax\n";
1561 14 $return_value = MATCH_NO_FIT;
1562 14 $imax_align = $jlimit;
1563 }
1564 }
1565  
1566 EXPLAIN_CHECK_MATCH
1567 1331 && print
1568 "returning $return_value because $GoToMsg, max match index =i $imax_align, jmax=$jmax\n";
1569  
1570 1331 return ( $return_value, $imax_align );
1571 } ## end sub check_match
1572  
1573 sub check_fit {
1574  
1575 1149 ▶ my ( $self, $new_line, $old_line ) = @_;
Subroutine check_fit: called CC=8 SCAR=20.9
Pod undocumented
1576  
1577 # The new line has alignments identical to the current group. Now we have
1578 # to fit the new line into the group without causing a field to exceed the
1579 # line length limit.
1580  
1581 # Given:
1582 # $new_line = ref to hash of the new line values
1583 # $old_line = ref to hash of the previous line values
1584 # Returns:
1585 # true if the new line alignments fit the old line
1586 # false otherwise
1587  
1588 1149 my $jmax = $new_line->{'jmax'};
1589 1149 my $leading_space_count = $new_line->{'leading_space_count'};
1590 1149 my $rfield_lengths = $new_line->{'rfield_lengths'};
1591 1149 my $padding_available = $old_line->get_available_space_on_right();
1592 1149 my $jmax_old = $old_line->{'jmax'};
1593  
1594 # Safety check ... only lines with equal array sizes should arrive here
1595 # from sub check_match. So if this error occurs, look at recent changes in
1596 # sub check_match. It is only supposed to check the fit of lines with
1597 # identical numbers of alignment tokens.
1598 1149 ▶ if ( $jmax_old ne $jmax ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax_old ne $jmax) 0 1149 1149
1599  
1600 0 $self->warning(<<EOM);
1601 Program bug detected in Perl::Tidy::VerticalAligner sub check_fit
1602 unexpected difference in array lengths: $jmax != $jmax_old
1603 EOM
1604 0 return;
1605 }
1606  
1607 # Save current columns in case this line does not fit.
1608 1149 my @alignments = @{ $old_line->{'ralignments'} };
1609 1149 foreach my $alignment (@alignments) {
1610 4062 $alignment->save_column();
1611 }
1612  
1613 # Loop over all alignments ...
1614 1149 for my $j ( 0 .. $jmax ) {
1615  
1616 4043 my $pad = $rfield_lengths->[$j] - $old_line->current_field_width($j);
1617  
1618 4043 ▶ if ( $j == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 0) 1149 2894 4043
1619 1149 $pad += $leading_space_count;
1620 }
1621  
1622 # Keep going if this field does not need any space.
1623 4043 ▶ next if ( $pad < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pad < 0 1330 2713 4043
1624  
1625 # Revert to the starting state if does not fit
1626 2713 ▶ if ( $pad > $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > $padding_available) 14 2699 2713
1627  
1628 #----------------------------------------------
1629 # Line does not fit -- revert to starting state
1630 #----------------------------------------------
1631 14 foreach my $alignment (@alignments) {
1632 43 $alignment->restore_column();
1633 }
1634 14 return;
1635 }
1636  
1637 # make room for this field
1638 2699 $old_line->increase_field_width( $j, $pad );
1639 2699 $padding_available -= $pad;
1640 }
1641  
1642 #-------------------------------------
1643 # The line fits, the match is accepted
1644 #-------------------------------------
1645 1135 return 1;
1646  
1647 } ## end sub check_fit
1648  
1649 sub install_new_alignments {
1650  
1651 2512 ▶ my ($new_line) = @_;
Subroutine install_new_alignments: called CC=2 SCAR=6.9
Pod undocumented
1652  
1653 # Given:
1654 # $new_line = ref to hash of a line starting a new group
1655 # Task:
1656 # setup alignment fields for this line
1657  
1658 2512 my $jmax = $new_line->{'jmax'};
1659 2512 my $rfield_lengths = $new_line->{'rfield_lengths'};
1660 2512 my $col = $new_line->{'leading_space_count'};
1661  
1662 2512 my @alignments;
1663 2512 for my $j ( 0 .. $jmax ) {
1664 8434 $col += $rfield_lengths->[$j];
1665  
1666 # create initial alignments for the new group
1667 8434 my $alignment =
1668 Perl::Tidy::VerticalAligner::Alignment->new( { column => $col } );
1669 8434 push @alignments, $alignment;
1670 }
1671 2512 $new_line->{'ralignments'} = \@alignments;
1672 2512 return;
1673 } ## end sub install_new_alignments
1674  
1675 sub copy_old_alignments {
1676 1135 ▶ my ( $new_line, $old_line ) = @_;
Subroutine copy_old_alignments: called CC=1 SCAR=0.0
Pod undocumented
1677 1135 my @new_alignments = @{ $old_line->{'ralignments'} };
1678 1135 $new_line->{'ralignments'} = \@new_alignments;
1679 1135 return;
1680 } ## end sub copy_old_alignments
1681  
1682 sub dump_array {
1683  
1684 # debug routine to dump array contents
1685 0 ▶ local $LIST_SEPARATOR = ')(';
Subroutine dump_array: not called CC=1 SCAR=6.9
Pod undocumented
1686 0 print {*STDOUT} "(@_)\n";
1687 0 return;
1688 } ## end sub dump_array
1689  
1690 sub level_change {
1691  
1692 10 ▶ my ( $self, $leading_space_count, $diff, $level ) = @_;
Subroutine level_change: called CC=3 SCAR=11.4
Pod undocumented
1693  
1694 # compute decrease in level when we remove $diff spaces from the
1695 # leading spaces
1696  
1697 # Given:
1698 # $leading_space_count = current leading line spaces
1699 # $diff = number of spaces to remove
1700 # $level = current indentation level
1701 # Return:
1702 # $level = updated level accounting for the loss of spaces
1703  
1704 10 ▶ if ($rOpts_indent_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts_indent_columns) 10 0 10
1705 10 my $olev =
1706 int( ( $leading_space_count + $diff ) / $rOpts_indent_columns );
1707 10 my $nlev = int( $leading_space_count / $rOpts_indent_columns );
1708 10 $level -= ( $olev - $nlev );
1709 10 ▶ if ( $level < 0 ) { $level = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($level < 0) 0 10 10
1710 }
1711 10 return $level;
1712 } ## end sub level_change
1713  
1714 ###############################################
1715 # CODE SECTION 4: Code to process comment lines
1716 ###############################################
1717  
1718 sub _flush_comment_lines {
1719  
1720 602 ▶ my ($self) = @_;
Subroutine _flush_comment_lines: called CC=8 SCAR=20.8
1721  
1722 # Output a group consisting of COMMENT lines
1723  
1724 602 my $rgroup_lines = $self->[_rgroup_lines_];
1725 602 ▶ return if ( !@{$rgroup_lines} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rgroup_lines;} 0 602 602
1726 602 my $group_level = $self->[_group_level_];
1727 602 my $group_maximum_line_length = $self->[_group_maximum_line_length_];
1728 602 my $leading_space_count = $self->[_comment_leading_space_count_];
1729  
1730 # look for excessively long lines
1731 602 my $max_excess = 0;
1732 602 foreach my $item ( @{$rgroup_lines} ) {
1733 692 my ( $str_uu, $str_len ) = @{$item};
1734 692 my $excess =
1735 $str_len + $leading_space_count - $group_maximum_line_length;
1736 692 ▶ if ( $excess > $max_excess ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess > $max_excess) 39 653 692
1737 39 $max_excess = $excess;
1738 }
1739 }
1740  
1741 # zero leading space count if any lines are too long
1742 602 ▶ if ( $max_excess > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_excess > 0) 37 565 602
1743 37 $leading_space_count -= $max_excess;
1744 37 ▶ if ( $leading_space_count < 0 ) { $leading_space_count = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count < 0) 37 0 37
1745 37 my $file_writer_object = $self->[_file_writer_object_];
1746 37 my $last_outdented_line_at =
1747 $file_writer_object->get_output_line_number();
1748 37 my $nlines = @{$rgroup_lines};
1749 37 $self->[_last_outdented_line_at_] =
1750 $last_outdented_line_at + $nlines - 1;
1751 37 my $outdented_line_count = $self->[_outdented_line_count_];
1752 37 ▶ if ( !$outdented_line_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($outdented_line_count) 19 18 37
1753 19 $self->[_first_outdented_line_at_] = $last_outdented_line_at;
1754 }
1755 37 $outdented_line_count += $nlines;
1756 37 $self->[_outdented_line_count_] = $outdented_line_count;
1757 }
1758  
1759 # write the lines
1760 602 my $outdent_long_lines = 0;
1761  
1762 602 foreach my $item ( @{$rgroup_lines} ) {
1763 692 my ( $str, $str_len, $Kend ) = @{$item};
1764 692 $self->valign_output_step_B(
1765 {
1766 leading_space_count => $leading_space_count,
1767 line => $str,
1768 line_length => $str_len,
1769 side_comment_length => 0,
1770 outdent_long_lines => $outdent_long_lines,
1771 rvertical_tightness_flags => undef,
1772 level => $group_level,
1773 level_end => $group_level,
1774 Kend => $Kend,
1775 maximum_line_length => $group_maximum_line_length,
1776 }
1777 );
1778 }
1779  
1780 602 $self->initialize_for_new_group();
1781 602 return;
1782 } ## end sub _flush_comment_lines
1783  
1784 ######################################################
1785 # CODE SECTION 5: Code to process groups of code lines
1786 ######################################################
1787  
1788 sub _flush_group_lines {
1789  
1790 2663 ▶ my ( $self, ($level_jump) ) = @_;
Subroutine _flush_group_lines: called CC=13 SCAR=25.7
1791  
1792 # This is the vertical aligner internal flush, which leaves the cache
1793 # intact
1794  
1795 # $level_jump = $next_level-$group_level, if known
1796 # = undef if not known
1797 # Note: only the sign of the jump is needed
1798  
1799 2663 my $rgroup_lines = $self->[_rgroup_lines_];
1800 2663 ▶ return if ( !@{$rgroup_lines} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rgroup_lines;} 0 2663 2663
1801 2663 my $group_type = $self->[_group_type_];
1802 2663 my $group_level = $self->[_group_level_];
1803  
1804 # Debug
1805 2663 0 && do {
1806 my ( $a, $b, $c ) = caller();
1807 my $nlines = @{$rgroup_lines};
1808 print {*STDOUT}
1809 "APPEND0: _flush_group_lines called from $a $b $c lines=$nlines, type=$group_type \n";
1810 };
1811  
1812 #-------------------------------------------
1813 # Section 1: Handle a group of COMMENT lines
1814 #-------------------------------------------
1815 2663 ▶ if ( $group_type eq 'COMMENT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_type eq "COMMENT") 602 2061 2663
1816 602 $self->_flush_comment_lines();
1817 602 return;
1818 }
1819  
1820 #------------------------------------------------------------------------
1821 # Section 2: Handle line(s) of CODE. Most of the actual work of vertical
1822 # aligning happens here in the following steps:
1823 #------------------------------------------------------------------------
1824  
1825 # STEP 1: Remove most unmatched tokens. They block good alignments.
1826 2061 my ( $max_lev_diff_uu, $saw_side_comment, $saw_signed_number ) =
1827 delete_unmatched_tokens( $rgroup_lines, $group_level );
1828  
1829 # STEP 2: Sweep top to bottom, forming subgroups of lines with exactly
1830 # matching common alignments. The indexes of these subgroups are in the
1831 # return variable.
1832 2061 my $rgroups = $self->sweep_top_down( $rgroup_lines, $group_level );
1833  
1834 # STEP 3: Sweep left to right through the lines, looking for leading
1835 # alignment tokens shared by groups.
1836 sweep_left_to_right( $rgroup_lines, $rgroups, $group_level )
1837 2061 ▶ if ( @{$rgroups} > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @{$rgroups;} > 1 302 1759 2061
1838  
1839 # STEP 4: Move side comments to a common column if possible.
1840 2061 ▶ if ($saw_side_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_side_comment) 222 1839 2061
1841 222 $self->align_side_comments( $rgroup_lines, $rgroups );
1842 }
1843  
1844 # STEP 5: For the -lp option, increase the indentation of lists
1845 # to the desired amount, but do not exceed the line length limit.
1846  
1847 # We are allowed to shift a group of lines to the right if:
1848 # (1) its level is greater than the level of the previous group, and
1849 # (2) its level is greater than the level of the next line to be written.
1850  
1851 2061 my $extra_indent_ok;
1852 2061 ▶ if ( $group_level > $self->[_last_level_written_] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_level > $self->[4]) 1034 1027 2061
1853  
1854 # Use the level jump to next line to come, if given
1855 1034 ▶ if ( defined($level_jump) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level_jump) { } 681 353 1034
1856 681 $extra_indent_ok = $level_jump < 0;
1857 }
1858  
1859 # Otherwise, assume the next line has the level of the end of last line.
1860 # This fixes case c008.
1861 else {
1862 353 my $level_end = $rgroup_lines->[-1]->{'level_end'};
1863 353 $extra_indent_ok = $group_level > $level_end;
1864 }
1865 }
1866  
1867 2061 ▶ my $extra_leading_spaces =
Branch: 1 branch
BranchTrueFalseTotal
$extra_indent_ok ? : 447 1614 2061
1868 $extra_indent_ok
1869 ? get_extra_leading_spaces( $rgroup_lines, $rgroups )
1870 : 0;
1871  
1872 # STEP 6: add sign padding to columns numbers if needed
1873 2061 ▶ pad_signed_number_columns($rgroup_lines)
Branch: 1 branch
BranchTrueFalseTotal
if $saw_signed_number and $rOpts_valign_signed_numbers 70 1991 2061
Condition100%3/3
$saw_signed_number and $rOpts_valign_signed_numbers
!ll&&!rl&&r
1989270
MC/DC100%2/2
$saw_signed_number and $rOpts_valign_signed_numbers
$saw_signed_number $rOpts_valign_signed_numbers
Truth table100%3/3
A and B
A= $saw_signed_numberB= $rOpts_valign_signed_numbers
ABresult
0X0
100
111
1874 if ( $saw_signed_number && $rOpts_valign_signed_numbers );
1875  
1876 # STEP 7: pad wide equals
1877 2061 ▶ pad_wide_equals_columns($rgroup_lines)
Branch: 1 branch
BranchTrueFalseTotal
if $rOpts_valign_wide_equals 12 2049 2061
1878 if ($rOpts_valign_wide_equals);
1879  
1880 # STEP 8: Output the lines.
1881 # All lines in this group have the same leading spacing and maximum line
1882 # length
1883 2061 my $group_leader_length = $rgroup_lines->[0]->{'leading_space_count'};
1884 2061 my $group_maximum_line_length = $rgroup_lines->[0]->{'maximum_line_length'};
1885  
1886 2061 foreach my $line ( @{$rgroup_lines} ) {
1887 3647 $self->valign_output_step_A(
1888 {
1889 line => $line,
1890 min_ci_gap => 0,
1891 do_not_align => 0,
1892 group_leader_length => $group_leader_length,
1893 extra_leading_spaces => $extra_leading_spaces,
1894 level => $group_level,
1895 maximum_line_length => $group_maximum_line_length,
1896 }
1897 );
1898 }
1899  
1900 # Let the formatter know that this object has been processed and any
1901 # recoverable spaces have been handled. This is needed for setting the
1902 # closing paren location in -lp mode.
1903 2061 my $object = $rgroup_lines->[0]->{'indentation'};
1904 2061 ▶ if ( ref($object) ) { $object->set_recoverable_spaces(0) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object) 94 1967 2061
1905  
1906 2061 $self->initialize_for_new_group();
1907 2061 return;
1908 } ## end sub _flush_group_lines
1909  
1910 { ## closure for sub sweep_top_down
1911  
1912 45 my $rall_lines; # all of the lines
1913 my $grp_level; # level of all lines
1914 45 my $rgroups; # describes the partition of lines we will make here
1915 45 my $group_line_count; # number of lines in current partition
1916  
1917 45 ▶ BEGIN { $rgroups = [] }
Subroutine BEGIN: called CC=1 SCAR=0.0
1918  
1919 sub initialize_for_new_rgroup {
1920 4573 ▶ $group_line_count = 0;
Subroutine initialize_for_new_rgroup: called CC=1 SCAR=0.0
Pod undocumented
1921 4573 return;
1922 }
1923  
1924 sub add_to_rgroup {
1925  
1926 3647 ▶ my ($jend) = @_;
Subroutine add_to_rgroup: called CC=2 SCAR=6.9
Pod undocumented
1927  
1928 # Include the line at index $jend in the current alignment group
1929  
1930 3647 my $rline = $rall_lines->[$jend];
1931  
1932 3647 my $jbeg = $jend;
1933 3647 ▶ if ( $group_line_count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_line_count == 0) { } 2512 1135 3647
1934 2512 install_new_alignments($rline);
1935 }
1936 else {
1937 1135 my $rvals = pop @{$rgroups};
1938 1135 $jbeg = $rvals->[0];
1939 1135 copy_old_alignments( $rline, $rall_lines->[$jbeg] );
1940 }
1941 3647 push @{$rgroups}, [ $jbeg, $jend, undef ];
1942 3647 $group_line_count++;
1943 3647 return;
1944 } ## end sub add_to_rgroup
1945  
1946 sub get_rgroup_jrange {
1947  
1948 1507 ▶ return if ( !@{$rgroups} );
Subroutine get_rgroup_jrange: called CC=3 SCAR=11.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rgroups;} 0 1507 1507
1949 1507 ▶ return if ( $group_line_count <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $group_line_count <= 0 0 1507 1507
1950 1507 my ( $jbeg, $jend ) = @{ $rgroups->[-1] };
1951 1507 return ( $jbeg, $jend );
1952 } ## end sub get_rgroup_jrange
1953  
1954 sub end_rgroup {
1955  
1956 2531 ▶ my ($imax_align) = @_;
Subroutine end_rgroup: called CC=7 SCAR=19.5
Pod undocumented
1957  
1958 # End the current alignment group and set its maximum alignment field
1959 # Given:
1960 # $imax_align = maximum field to be vertically aligned
1961  
1962 2531 ▶ return if ( !@{$rgroups} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rgroups;} 0 2531 2531
1963 2531 ▶ return if ( $group_line_count <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $group_line_count <= 0 19 2512 2531
1964  
1965 2512 my ( $jbeg, $jend ) = @{ pop @{$rgroups} };
1966 2512 push @{$rgroups}, [ $jbeg, $jend, $imax_align ];
1967  
1968 # Undo some alignments of poor two-line combinations.
1969 # We had to wait until now to know the line count.
1970 2512 ▶ if ( $jend - $jbeg == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jend - $jbeg == 1) 295 2217 2512
1971 295 my $line_0 = $rall_lines->[$jbeg];
1972 295 my $line_1 = $rall_lines->[$jend];
1973  
1974 295 my $imax_pair = $line_1->{'imax_pair'};
1975 295 ▶ if ( $imax_pair > $imax_align ) { $imax_align = $imax_pair }
Branch: 1 branch
BranchTrueFalseTotal
if ($imax_pair > $imax_align) 0 295 295
1976  
1977 ## flag for possible future use:
1978 ## my $is_isolated_pair = $imax_pair < 0
1979 ## && ( $jbeg == 0
1980 ## || $rall_lines->[ $jbeg - 1 ]->{'imax_pair'} < 0 );
1981  
1982 my $imax_prev =
1983 295 ▶ $jbeg > 0 ? $rall_lines->[ $jbeg - 1 ]->{'imax_pair'} : -1;
Branch: 1 branch
BranchTrueFalseTotal
$jbeg > 0 ? : 76 219 295
1984  
1985 295 my ( $is_marginal, $imax_align_fix ) =
1986 is_marginal_match( $line_0, $line_1, $grp_level, $imax_align,
1987 $imax_prev );
1988 295 ▶ if ($is_marginal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_marginal) 16 279 295
1989 16 combine_fields( $line_0, $line_1, $imax_align_fix );
1990 }
1991 }
1992  
1993 2512 initialize_for_new_rgroup();
1994 2512 return;
1995 } ## end sub end_rgroup
1996  
1997 sub block_penultimate_match {
1998  
1999 # emergency reset to prevent sweep_left_to_right from trying to match a
2000 # failed terminal else match
2001 1 ▶ return if ( @{$rgroups} <= 1 );
Subroutine block_penultimate_match: called CC=2 SCAR=7.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @{$rgroups;} <= 1 0 1 1
2002 1 $rgroups->[-2]->[2] = -1;
2003 1 return;
2004 } ## end sub block_penultimate_match
2005  
2006 sub sweep_top_down {
2007  
2008 2061 ▶ my ( $self, $rlines, $group_level ) = @_;
Subroutine sweep_top_down: called CC=31 SCAR=34.8
Pod undocumented
2009  
2010 # This is the first of two major sweeps to find alignments.
2011 # The other is sweep_left_to_right.
2012  
2013 # Given:
2014 # $rlines = ref to hash of lines in this main alignment group
2015 # $group_level = common indentation level of these lines
2016 # Return:
2017 # $rgroups = ref to hash of subgroups created
2018  
2019 # Partition the set of lines into final alignment subgroups
2020 # and store the alignments with the lines.
2021  
2022 # The alignment subgroups we are making here are groups of consecutive
2023 # lines which have (1) identical alignment tokens and (2) do not
2024 # exceed the allowable maximum line length. A later sweep from
2025 # left-to-right ('sweep_lr') will handle additional alignments.
2026  
2027 # transfer args to closure variables
2028 2061 $rall_lines = $rlines;
2029 2061 $grp_level = $group_level;
2030 2061 $rgroups = [];
2031 2061 initialize_for_new_rgroup();
2032 2061 ▶ if ( !@{$rlines} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$rlines;}) 0 2061 2061
2033 0 DEVEL_MODE && Fault("Unexpected empty alignment group\n");
2034 0 return;
2035 }
2036  
2037 # Unset the _end_group flag for the last line if it set because it
2038 # is not needed and can causes problems for -lp formatting
2039 2061 $rall_lines->[-1]->{'end_group'} = 0;
2040  
2041 # Loop over all lines ...
2042 2061 my $jline = -1;
2043 2061 foreach my $new_line ( @{$rall_lines} ) {
2044 3647 $jline++;
2045  
2046 # Start a new subgroup if necessary
2047 3647 ▶ if ( !$group_line_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($group_line_count) 2140 1507 3647
2048 2140 add_to_rgroup($jline);
2049 2140 ▶ if ( $new_line->{'end_group'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_line->{"end_group"}) 26 2114 2140
2050 26 end_rgroup(-1);
2051 }
2052 2140 next;
2053 }
2054  
2055 1507 my $j_terminal_match = $new_line->{'j_terminal_match'};
2056 1507 my ( $jbeg, $jend_uu ) = get_rgroup_jrange();
2057 1507 ▶ if ( !defined($jbeg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $jbeg) 0 1507 1507
2058  
2059 # safety check, shouldn't happen
2060 0 $self->warning(<<EOM);
2061 Program bug detected in Perl::Tidy::VerticalAligner sub sweep_top_down
2062 undefined index for group line count $group_line_count
2063 EOM
2064 0 $jbeg = $jline;
2065 }
2066 1507 my $base_line = $rall_lines->[$jbeg];
2067  
2068 # Initialize a global flag saying if the last line of the group
2069 # should match end of group and also terminate the group. There
2070 # should be no returns between here and where the flag is handled
2071 # at the bottom.
2072 1507 my $col_matching_terminal = 0;
2073 1507 ▶ if ( defined($j_terminal_match) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $j_terminal_match) 19 1488 1507
2074  
2075 # remember the column of the terminal ? or { to match with
2076 19 $col_matching_terminal =
2077 $base_line->get_column($j_terminal_match);
2078  
2079 # Ignore an undefined value as a defensive step; shouldn't
2080 # normally happen.
2081 19 ▶ $col_matching_terminal = 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_matching_terminal 0 19 19
2082 unless ( defined($col_matching_terminal) );
2083 }
2084  
2085 # -------------------------------------------------------------
2086 # Allow hanging side comment to join current group, if any. The
2087 # only advantage is to keep the other tokens in the same group. For
2088 # example, this would make the '=' align here:
2089 # $ax = 1; # side comment
2090 # # hanging side comment
2091 # $boondoggle = 5; # side comment
2092 # $beetle = 5; # side comment
2093  
2094 # here is another example..
2095  
2096 # _rtoc_name_count => {}, # hash to track ..
2097 # _rpackage_stack => [], # stack to check ..
2098 # # name changes
2099 # _rlast_level => \$last_level, # brace indentation
2100 #
2101 #
2102 # If this were not desired, the next step could be skipped.
2103 # -------------------------------------------------------------
2104 1507 ▶ if ( $new_line->{'is_hanging_side_comment'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_line->{'is_hanging_side_comment'}) { } 38 1469 1507
elsif ($new_line->{'jmax'} == 1) { } 248 1221 1469
2105 38 join_hanging_comment( $new_line, $base_line );
2106 }
2107  
2108 # If this line has no matching tokens, then flush out the lines
2109 # BEFORE this line unless both it and the previous line have side
2110 # comments. This prevents this line from pushing side comments out
2111 # to the right.
2112 elsif ( $new_line->{'jmax'} == 1 ) {
2113  
2114 # There are no matching tokens, so now check side comments.
2115 # Programming note: accessing arrays with index -1 is
2116 # risky in Perl, but we have verified there is at least one
2117 # line in the group and that there is at least one field,
2118 my $prev_comment =
2119 248 $rall_lines->[ $jline - 1 ]->{'rfields'}->[-1];
2120 248 my $side_comment = $new_line->{'rfields'}->[-1];
2121  
2122 # do not end group if both lines have side comments
2123 248 ▶ if ( !$side_comment || !$prev_comment ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($side_comment and $prev_comment) 203 45 248
Condition100%3/3
$side_comment and $prev_comment
!ll&&!rl&&r
1822145
MC/DC100%2/2
$side_comment and $prev_comment
$side_comment $prev_comment
Truth table100%3/3
A and B
A= $side_commentB= $prev_comment
ABresult
0X0
100
111
2124  
2125 # Otherwise - VSN PATCH for a single number:
2126 # - do not end group if numeric and no side comment, or
2127 # - end if !numeric or side comment
2128 203 my $pat = $new_line->{'rpatterns'}->[0];
2129 203 ▶ my $is_numeric = $rOpts_valign_signed_numbers
Condition83%5/6
$rOpts_valign_signed_numbers && ($pat eq 'n,' || $pat eq 'n,b')
!ll&&!rl&&r
017528
$pat eq "n," or $pat eq "n,b"
l!l&&r!l&&!r
271175
MC/DC66%2/3
$rOpts_valign_signed_numbers && ($pat eq 'n,' || $pat eq 'n,b')
$rOpts_valign_signed_numbers $pat eq "n," $pat eq "n,b"
Truth table75%3/4
A && B or C
A= $rOpts_valign_signed_numbersB= $pat eq "n,"C= $pat eq "n,b"
ABCresult
0XX0
1000
11X1
1011
2130 && ( $pat eq 'n,'
2131 || $pat eq 'n,b' );
2132 203 ▶ end_rgroup(-1) if ( !$is_numeric || $side_comment );
Branch: 1 branch
BranchTrueFalseTotal
if not $is_numeric or $side_comment 176 27 203
Condition100%3/3
not $is_numeric or $side_comment
l!l&&r!l&&!r
175127
MC/DC100%2/2
not $is_numeric or $side_comment
not $is_numeric $side_comment
Truth table100%3/3
not(A) or B
A= not $is_numericB= $side_comment
ABresult
1X1
011
000
2133 }
2134 }
2135 else {
2136 ## ok: continue
2137 }
2138  
2139 # See if the new line matches and fits the current group,
2140 # if it still exists. Flush the current group if not.
2141 1507 my $match_code;
2142 1507 ▶ if ($group_line_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_line_count) 1331 176 1507
2143 1331 ( $match_code, my $imax_align ) =
2144 $self->check_match( $new_line, $base_line,
2145 $rall_lines->[ $jline - 1 ],
2146 $group_line_count );
2147 1331 ▶ if ( $match_code != 2 ) { end_rgroup($imax_align) }
Branch: 1 branch
BranchTrueFalseTotal
if ($match_code != 2) 196 1135 1331
2148 }
2149  
2150 # Store the new line
2151 1507 add_to_rgroup($jline);
2152  
2153 1507 ▶ if ( defined($j_terminal_match) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $j_terminal_match) { } 19 1488 1507
elsif ($new_line->{'end_group'}) { } 53 1435 1488
2154  
2155 # Decide if we should fix a terminal match. We can either:
2156 # 1. fix it and prevent the sweep_lr from changing it, or
2157 # 2. leave it alone and let sweep_lr try to fix it.
2158  
2159 # The current logic is to fix it if:
2160 # -it has not joined to previous lines,
2161 # -and either the previous subgroup has just 1 line, or
2162 # -this line matched but did not fit (so sweep won't work)
2163 19 my $fixit;
2164 19 ▶ if ( $group_line_count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_line_count == 1) 3 16 19
2165 3 ▶ $fixit ||= $match_code;
Condition66%2/3
$fixit ||= $match_code
l!l&&r!l&&!r
012
MC/DC50%1/2
$fixit ||= $match_code
$fixit $match_code
Truth table66%2/3
A ||= B
A= $fixitB= $match_code
ABresult
1X1
011
000
2166 3 ▶ if ( !$fixit ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fixit) 2 1 3
2167 2 ▶ if ( @{$rgroups} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rgroups;} > 1) 2 0 2
2168 2 my ( $jbegx, $jendx ) = @{ $rgroups->[-2] };
2169 2 my $nlines = $jendx - $jbegx + 1;
2170 2 ▶ $fixit ||= $nlines <= 1;
Condition66%2/3
$fixit ||= $nlines <= 1
l!l&&r!l&&!r
011
MC/DC50%1/2
$fixit ||= $nlines <= 1
$fixit $nlines <= 1
Truth table66%2/3
A ||= B
A= $fixitB= $nlines <= 1
ABresult
1X1
011
000
2171 }
2172 }
2173 }
2174  
2175 19 ▶ if ($fixit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixit) 2 17 19
2176 2 $base_line = $new_line;
2177 2 my $col_now = $base_line->get_column($j_terminal_match);
2178  
2179 # Ignore an undefined value as a defensive step; shouldn't
2180 # normally happen.
2181 2 ▶ $col_now = 0 unless ( defined($col_now) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_now 0 2 2
2182  
2183 2 my $pad = $col_matching_terminal - $col_now;
2184 2 my $padding_available =
2185 $base_line->get_available_space_on_right();
2186 2 ▶ if ( $col_now && $pad > 0 && $pad <= $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_now and $pad > 0 and $pad <= $padding_available) 1 1 2
Condition50%3/6
$col_now and $pad > 0 and $pad <= $padding_available
!ll&&!rl&&r
011
$col_now and $pad > 0
!ll&&!rl&&r
002
MC/DC33%1/3
$col_now and $pad > 0 and $pad <= $padding_available
$col_now $pad > 0 $pad <= $padding_available
Truth table50%2/4
A and B and C
A= $col_nowB= $pad > 0C= $pad <= $padding_available
ABCresult
0XX0
10X0
1100
1111
2187 1 $base_line->increase_field_width( $j_terminal_match,
2188 $pad );
2189 }
2190  
2191 # do not let sweep_left_to_right change an isolated 'else'
2192 2 ▶ if ( !$new_line->{'is_terminal_ternary'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_line->{"is_terminal_ternary"}) 1 1 2
2193 1 block_penultimate_match();
2194 }
2195 }
2196 19 end_rgroup(-1);
2197 }
2198  
2199 # end the group if we know we cannot match next line.
2200 elsif ( $new_line->{'end_group'} ) {
2201 53 end_rgroup(-1);
2202 }
2203  
2204 else {
2205 ## ok: continue
2206 }
2207 } ## end loop over lines
2208  
2209 2061 end_rgroup(-1);
2210 2061 return ($rgroups);
2211 } ## end sub sweep_top_down
2212 }
2213  
2214 45 sub two_line_pad {
2215  
2216 21 ▶ my ( $line_m, $line, $imax_min ) = @_;
Subroutine two_line_pad: called CC=13 SCAR=26.7
Pod undocumented
2217  
2218 # Decide if two adjacent, isolated lines should be aligned
2219  
2220 # Given:
2221 # $line_m, $line = two isolated (list) lines
2222 # imax_min = number of common alignment tokens
2223 # Return:
2224 # $pad_max = maximum suggested pad distance
2225 # = 0 if alignment not recommended
2226  
2227 # Allow alignment if the difference in the two unpadded line lengths
2228 # is not more than either line length. The idea is to avoid
2229 # aligning lines with very different field lengths, like these two:
2230  
2231 # [
2232 # 'VARCHAR', DBI::SQL_VARCHAR, undef, "'", "'", undef, 0, 1,
2233 # 1, 0, 0, 0, undef, 0, 0
2234 # ];
2235  
2236 # Note that this is only for two lines which do not have alignment tokens
2237 # in common with any other lines. It is intended for lists, but it might
2238 # also be used for two non-list lines with a common leading '='.
2239  
2240 21 my $rfield_lengths = $line->{'rfield_lengths'};
2241 21 my $rfield_lengths_m = $line_m->{'rfield_lengths'};
2242  
2243 # Safety check - shouldn't happen
2244 return 0
2245 21 if ( $imax_min >= @{$rfield_lengths}
2246 21 ▶ || $imax_min >= @{$rfield_lengths_m} );
Branch: 1 branch
BranchTrueFalseTotal
if $imax_min >= @{$rfield_lengths;} or $imax_min >= @{$rfield_lengths_m;} 0 21 21
Condition33%1/3
$imax_min >= @{$rfield_lengths;} or $imax_min >= @{$rfield_lengths_m;}
l!l&&r!l&&!r
0021
MC/DC0%0/2
$imax_min >= @{$rfield_lengths;} or $imax_min >= @{$rfield_lengths_m;}
$imax_min >= @{$rfield_lengths;} $imax_min >= @{$rfield_lengths_m;}
Truth table33%1/3
A or B
A= $imax_min >= @{$rfield_lengths;}B= $imax_min >= @{$rfield_lengths_m;}
ABresult
1X1
011
000
2247  
2248 21 my $lensum_m = 0;
2249 21 my $lensum = 0;
2250 21 foreach my $i ( 0 .. $imax_min ) {
2251 54 $lensum_m += $rfield_lengths_m->[$i];
2252 54 $lensum += $rfield_lengths->[$i];
2253 }
2254  
2255 21 ▶ my ( $lenmin, $lenmax ) =
Branch: 1 branch
BranchTrueFalseTotal
$lensum >= $lensum_m ? : 12 9 21
2256 $lensum >= $lensum_m ? ( $lensum_m, $lensum ) : ( $lensum, $lensum_m );
2257  
2258 21 my $patterns_match;
2259 21 ▶ if ( $line_m->{'list_type'} && $line->{'list_type'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_m->{"list_type"} and $line->{"list_type"}) 17 4 21
Condition66%2/3
$line_m->{"list_type"} and $line->{"list_type"}
!ll&&!rl&&r
4017
MC/DC50%1/2
$line_m->{"list_type"} and $line->{"list_type"}
$line_m->{"list_type"} $line->{"list_type"}
Truth table66%2/3
A and B
A= $line_m->{"list_type"}B= $line->{"list_type"}
ABresult
0X0
100
111
2260 17 $patterns_match = 1;
2261 17 my $rpatterns_m = $line_m->{'rpatterns'};
2262 17 my $rpatterns = $line->{'rpatterns'};
2263 17 foreach my $i ( 0 .. $imax_min ) {
2264 47 my $pat = $rpatterns->[$i];
2265 47 my $pat_m = $rpatterns_m->[$i];
2266  
2267 # VSN PATCH: allow numbers to match quotes
2268 47 ▶ if ( $pat_m ne $pat && length($pat_m) eq length($pat) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_m ne $pat and length $pat_m eq length $pat) 0 47 47
Condition66%2/3
$pat_m ne $pat and length $pat_m eq length $pat
!ll&&!rl&&r
4520
MC/DC0%0/2
$pat_m ne $pat and length $pat_m eq length $pat
$pat_m ne $pat length $pat_m eq length $pat
Truth table66%2/3
A and B
A= $pat_m ne $patB= length $pat_m eq length $pat
ABresult
0X0
100
111
2269 0 $pat =~ tr/n/Q/;
2270 0 $pat_m =~ tr/n/Q/;
2271 }
2272  
2273 47 ▶ if ( $pat ne $pat_m ) { $patterns_match = 0; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pat ne $pat_m) 2 45 47
2274 }
2275 }
2276  
2277 21 my $pad_max = $lenmax;
2278 21 ▶ if ( !$patterns_match && $lenmax > 2 * $lenmin ) { $pad_max = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if (not $patterns_match and $lenmax > 2 * $lenmin) 0 21 21
Condition66%2/3
not $patterns_match and $lenmax > 2 * $lenmin
!ll&&!rl&&r
1560
MC/DC0%0/2
not $patterns_match and $lenmax > 2 * $lenmin
not $patterns_match $lenmax > 2 * $lenmin
Truth table66%2/3
not(A) and B
A= not $patterns_matchB= $lenmax > 2 * $lenmin
ABresult
0X0
100
111
2279  
2280 21 return $pad_max;
2281 } ## end sub two_line_pad
2282  
2283 sub sweep_left_to_right {
2284  
2285 302 ▶ my ( $rlines, $rgroups, $group_level ) = @_;
Subroutine sweep_left_to_right: called CC=30 SCAR=34.0
Pod undocumented
2286  
2287 # This is the second of two major sweeps to find alignments.
2288 # The other is sweep_top_down.
2289  
2290 # Given:
2291 # $rlines = ref to hash of lines in this main alignment group
2292 # $rgroups = ref to hash of subgroups
2293 # $group_level = common indentation level of these lines
2294 # Task:
2295 # add leading alignments where possible
2296  
2297 # So far we have divided the lines into groups having an equal number of
2298 # identical alignments. Here we are going to look for common leading
2299 # alignments between the different groups and align them when possible.
2300  
2301 # For example, the three lines below are in three groups because each line
2302 # has a different number of commas. In this routine we will sweep from
2303 # left to right, aligning the leading commas as we go, but stopping if we
2304 # hit the line length limit.
2305  
2306 # my ( $num, $numi, $numj, $xyza, $ka, $xyzb, $kb, $aff, $error );
2307 # my ( $i, $j, $error, $aff, $asum, $avec );
2308 # my ( $km, $area, $varea );
2309  
2310 # nothing to do if just one group
2311 302 my $ng_max = @{$rgroups} - 1;
2312 302 ▶ return if ( $ng_max <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $ng_max <= 0 0 302 302
2313  
2314 #---------------------------------------------------------------------
2315 # Step 1: Loop over groups to find all common leading alignment tokens
2316 #---------------------------------------------------------------------
2317  
2318 302 my $line;
2319 my $rtokens;
2320 302 my $imax; # index of maximum non-side-comment alignment token
2321 302 my $istop; # an optional stopping index
2322 302 my $jbeg; # starting line index
2323 302 my $jend; # ending line index
2324  
2325 302 my $line_m;
2326 302 my $rtokens_m;
2327 302 my $imax_m;
2328 302 my $istop_m;
2329 302 my $jbeg_m;
2330 302 my $jend_m;
2331  
2332 302 my $istop_mm;
2333  
2334 # Look at neighboring pairs of groups and form a simple list
2335 # of all common leading alignment tokens. Foreach such match we
2336 # store [$i, $ng], where
2337 # $i = index of the token in the line (0,1,...)
2338 # $ng is the second of the two groups with this common token
2339 302 my @icommon;
2340  
2341 # Hash to hold the maximum alignment change for any group
2342 302 my %max_move;
2343  
2344 # a small number of columns
2345 302 my $short_pad = 4;
2346  
2347 302 my $ng = -1;
2348 302 foreach my $item ( @{$rgroups} ) {
2349 753 $ng++;
2350  
2351 753 $istop_mm = $istop_m;
2352  
2353 # save _m values of previous group
2354 753 $line_m = $line;
2355 753 $rtokens_m = $rtokens;
2356 753 $imax_m = $imax;
2357 753 $istop_m = $istop;
2358 753 $jbeg_m = $jbeg;
2359 753 $jend_m = $jend;
2360  
2361 # Get values for this group. Note that we just have to use values for
2362 # one of the lines of the group since all members have the same
2363 # alignments.
2364 753 ( $jbeg, $jend, $istop ) = @{$item};
2365  
2366 753 $line = $rlines->[$jbeg];
2367 753 $rtokens = $line->{'rtokens'};
2368 753 $imax = $line->{'jmax'} - 2;
2369 753 ▶ $istop = -1 if ( !defined($istop) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $istop 0 753 753
2370 753 ▶ $istop = $imax if ( $istop > $imax );
Branch: 1 branch
BranchTrueFalseTotal
if $istop > $imax 0 753 753
2371  
2372 # Initialize on first group
2373 753 ▶ next if ( $ng == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $ng == 0 302 451 753
2374  
2375 # Use the minimum index limit of the two groups
2376 451 ▶ my $imax_min = $imax > $imax_m ? $imax_m : $imax;
Branch: 1 branch
BranchTrueFalseTotal
$imax > $imax_m ? : 108 343 451
2377  
2378 # Also impose a limit if given.
2379 451 ▶ if ( $istop_m < $imax_min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($istop_m < $imax_min) 65 386 451
2380 65 $imax_min = $istop_m;
2381 }
2382  
2383 # Special treatment of two one-line groups isolated from other lines,
2384 # unless they form a simple list or a terminal match. Otherwise the
2385 # alignment can look strange in some cases.
2386 451 my $list_type = $rlines->[$jbeg]->{'list_type'};
2387 451 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($jend == $jbeg and $jend_m == $jbeg_m and $ng == 1 || $istop_mm < 0 and $ng == $ng_max || $istop < 0 and not $line->{"j_terminal_match"} and $imax != $imax_m || $istop_m != $imax_m) 80 371 451
Condition100%24/24
$jend == $jbeg and $jend_m == $jbeg_m and $ng == 1 || $istop_mm < 0 and $ng == $ng_max || $istop < 0 and not $line->{"j_terminal_match"} and $imax != $imax_m || $istop_m != $imax_m
!ll&&!rl&&r
1982449
$jend == $jbeg and $jend_m == $jbeg_m and $ng == 1 || $istop_mm < 0 and $ng == $ng_max || $istop < 0 and not $line->{"j_terminal_match"}
!ll&&!rl&&r
1971253
$jend == $jbeg and $jend_m == $jbeg_m and $ng == 1 || $istop_mm < 0 and $ng == $ng_max || $istop < 0
!ll&&!rl&&r
1889254
$jend == $jbeg and $jend_m == $jbeg_m and $ng == 1 || $istop_mm < 0
!ll&&!rl&&r
17612263
$jend == $jbeg and $jend_m == $jbeg_m
!ll&&!rl&&r
9878275
$ng == 1 or $istop_mm < 0
l!l&&r!l&&!r
1778612
$ng == $ng_max or $istop < 0
l!l&&r!l&&!r
174809
$imax != $imax_m or $istop_m != $imax_m
l!l&&r!l&&!r
746173
MC/DC77%7/9
$jend == $jbeg and $jend_m == $jbeg_m and $ng == 1 || $istop_mm < 0 and $ng == $ng_max || $istop < 0 and not $line->{"j_terminal_match"} and $imax != $imax_m || $istop_m != $imax_m
$jend == $jbeg $jend_m == $jbeg_m $ng == 1 $istop_mm < 0 $ng == $ng_max $istop < 0 not $line->{"j_terminal_match"} $imax != $imax_m $istop_m != $imax_m
Truth table57%12/21
A and B and C or D and E or F and not(G) and H or I
A= $jend == $jbegB= $jend_m == $jbeg_mC= $ng == 1D= $istop_mm < 0E= $ng == $ng_maxF= $istop < 0G= not $line->{"j_terminal_match"}H= $imax != $imax_mI= $istop_m != $imax_m
ABCDEFGHIresult
0XXXXXXXX0
10XXXXXXX0
1100XXXXX0
111X00XXX0
110100XXX0
111X1X0XX0
111X010XX0
11011X0XX0
1101010XX0
111X1X1000
111X011000
11011X1000
1101011000
111X1X11X1
111X1X1011
111X0111X1
111X011011
11011X11X1
11011X1011
11010111X1
1101011011
2388 $jend == $jbeg
2389 && $jend_m == $jbeg_m
2390 && ( $ng == 1 || $istop_mm < 0 )
2391 && ( $ng == $ng_max || $istop < 0 )
2392 && !$line->{'j_terminal_match'}
2393  
2394 # Only do this for imperfect matches. This is normally true except
2395 # when two perfect matches cannot form a group because the line
2396 # length limit would be exceeded. In that case we can still try
2397 # to match as many alignments as possible.
2398 && ( $imax != $imax_m || $istop_m != $imax_m )
2399 )
2400 {
2401  
2402 # We will just align assignments and simple lists
2403 80 ▶ next if ( $imax_min < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $imax_min < 0 54 26 80
2404 next
2405 26 ▶ if ( $rtokens->[0] !~ /^=\d/
Branch: 1 branch
BranchTrueFalseTotal
unless $rtokens->[0] =~ /^=\d/ or $list_type 5 21 26
Condition100%3/3
$rtokens->[0] =~ /^=\d/ or $list_type
l!l&&r!l&&!r
4175
MC/DC100%2/2
$rtokens->[0] =~ /^=\d/ or $list_type
$rtokens->[0] =~ /^=\d/ $list_type
Truth table100%3/3
A or B
A= $rtokens->[0] =~ /^=\d/B= $list_type
ABresult
1X1
011
000
2406 && !$list_type );
2407  
2408 # In this case we will limit padding to a short distance. This
2409 # is a compromise to keep some vertical alignment but prevent large
2410 # gaps, which do not look good for just two lines.
2411 21 my $pad_max =
2412 two_line_pad( $rlines->[$jbeg], $rlines->[$jbeg_m], $imax_min );
2413 21 ▶ next if ( !$pad_max );
Branch: 1 branch
BranchTrueFalseTotal
unless $pad_max 0 21 21
2414 21 my $ng_m = $ng - 1;
2415 21 $max_move{"$ng_m"} = $pad_max;
2416 21 $max_move{"$ng"} = $pad_max;
2417 }
2418  
2419 # Loop to find all common leading tokens.
2420 392 ▶ if ( $imax_min >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imax_min >= 0) 98 294 392
2421 98 foreach my $i ( 0 .. $imax_min ) {
2422 175 my $tok = $rtokens->[$i];
2423 175 my $tok_m = $rtokens_m->[$i];
2424 175 ▶ last if ( $tok ne $tok_m );
Branch: 1 branch
BranchTrueFalseTotal
if $tok ne $tok_m 0 175 175
2425 175 push @icommon, [ $i, $ng, $tok ];
2426 }
2427 }
2428 }
2429 302 ▶ return unless (@icommon);
Branch: 1 branch
BranchTrueFalseTotal
unless @icommon 227 75 302
2430  
2431 #----------------------------------------------------------
2432 # Step 2: Reorder and consolidate the list into a task list
2433 #----------------------------------------------------------
2434  
2435 # We have to work first from lowest token index to highest, then by group,
2436 # sort our list first on token index then group number
2437 75 ▶ @icommon = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @icommon;
Branch: 1 branch
BranchTrueFalseTotal
$Perl::Tidy::VerticalAligner::a->[0] <=> $Perl::Tidy::VerticalAligner::b->[0] or $Perl::Tidy::VerticalAligner::a->[1] <=> $Perl::Tidy::VerticalAligner::b->[1] 53 146 199
2438  
2439 # Make a task list of the form
2440 # [$i, ng_beg, $ng_end, $tok], ..
2441 # where
2442 # $i is the index of the token to be aligned
2443 # $ng_beg..$ng_end is the group range for this action
2444 75 my @todo;
2445 75 my ( $i, $ng_end, $tok );
2446 75 foreach my $item (@icommon) {
2447 175 my $ng_last = $ng_end;
2448 175 my $i_last = $i;
2449 175 ( $i, $ng_end, $tok ) = @{$item};
2450 175 my $ng_beg = $ng_end - 1;
2451 175 ▶ if ( defined($ng_last) && $ng_beg == $ng_last && $i == $i_last ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ng_last and $ng_beg == $ng_last and $i == $i_last) 38 137 175
Condition83%5/6
defined $ng_last and $ng_beg == $ng_last and $i == $i_last
!ll&&!rl&&r
137038
defined $ng_last and $ng_beg == $ng_last
!ll&&!rl&&r
756238
MC/DC66%2/3
defined $ng_last and $ng_beg == $ng_last and $i == $i_last
defined $ng_last $ng_beg == $ng_last $i == $i_last
Truth table75%3/4
A and B and C
A= defined $ng_lastB= $ng_beg == $ng_lastC= $i == $i_last
ABCresult
0XX0
10X0
1100
1111
2452 38 my $var = pop @todo;
2453 38 $ng_beg = $var->[1];
2454 }
2455 175 my ( $raw_tok, $lev, $tag_uu, $tok_count_uu ) =
2456 decode_alignment_token($tok);
2457 175 push @todo, [ $i, $ng_beg, $ng_end, $raw_tok, $lev ];
2458 }
2459  
2460 #------------------------------
2461 # Step 3: Execute the task list
2462 #------------------------------
2463 tap_dancer(
2464 {
2465 75 rlines => $rlines,
2466 rgroups => $rgroups,
2467 rtodo => \@todo,
2468 rmax_move => \%max_move,
2469 short_pad => $short_pad,
2470 group_level => $group_level,
2471 }
2472 );
2473 75 return;
2474 } ## end sub sweep_left_to_right
2475  
2476 { ## closure for sub tap_dancer
2477  
2478 45 my %is_good_alignment_token;
2479  
2480 BEGIN {
2481  
2482 # One of the most difficult aspects of vertical alignment is knowing
2483 # when not to align. Alignment can go from looking very nice to very
2484 # bad when overdone. In the sweep algorithm there are two special
2485 # cases where we may need to limit padding to a '$short_pad' distance
2486 # to avoid some very ugly formatting:
2487  
2488 # 1. Two isolated lines with partial alignment
2489 # 2. A 'tail-wag-dog' situation, in which a single terminal
2490 # line with partial alignment could cause a significant pad
2491 # increase in many previous lines if allowed to join the alignment.
2492  
2493 # For most alignment tokens, we will allow only a small pad to be
2494 # introduced (the hardwired $short_pad variable) . But for some 'good'
2495 # alignments we can be less restrictive.
2496  
2497 # The hash values are set so that:
2498 # if ($is_good_alignment_token{$raw_tok}) => best
2499 # if defined ($is_good_alignment_token{$raw_tok}) => good or best
2500  
2501 # Start by defining these 'good' alignments, which are allowed more
2502 # padding (so note the '0' hash value here):
2503 $is_good_alignment_token{$_} = 0
2504 45 ▶ for ( COMMA, qw# => = ? if unless or || { # );
Subroutine BEGIN: called CC=2 SCAR=6.9
2505  
2506 # Promote a few of these to 'best', with essentially no pad limit:
2507 45 $is_good_alignment_token{'='} = 1;
2508 45 $is_good_alignment_token{'if'} = 1;
2509 45 $is_good_alignment_token{'unless'} = 1;
2510 45 $is_good_alignment_token{'=>'} = 1;
2511  
2512 } ## end BEGIN
2513  
2514 sub move_to_common_column {
2515  
2516 140 ▶ my ($rcall_hash) = @_;
Subroutine move_to_common_column: called CC=9 SCAR=22.3
Pod undocumented
2517  
2518 # This is a sub called by sub tap_dancer to
2519 # move the alignment column of token $itok to $col_want for a
2520 # sequence of groups.
2521  
2522 140 my $rlines = $rcall_hash->{rlines};
2523 140 my $rgroups = $rcall_hash->{rgroups};
2524 140 my $rmax_move = $rcall_hash->{rmax_move};
2525 140 my $ngb = $rcall_hash->{ngb};
2526 140 my $nge = $rcall_hash->{nge};
2527 140 my $itok = $rcall_hash->{itok};
2528 140 my $col_want = $rcall_hash->{col_want};
2529 140 my $raw_tok = $rcall_hash->{raw_tok};
2530  
2531 140 ▶ return if ( !defined($ngb) || $nge <= $ngb );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ngb or $nge <= $ngb 12 128 140
Condition66%2/3
not defined $ngb or $nge <= $ngb
l!l&&r!l&&!r
012128
MC/DC50%1/2
not defined $ngb or $nge <= $ngb
not defined $ngb $nge <= $ngb
Truth table66%2/3
not(A) or B
A= not defined $ngbB= $nge <= $ngb
ABresult
1X1
011
000
2532 128 foreach my $ng ( $ngb .. $nge ) {
2533  
2534 291 my ( $jbeg, $jend_uu ) = @{ $rgroups->[$ng] };
2535 291 my $line = $rlines->[$jbeg];
2536 291 my $col = $line->get_column($itok);
2537 291 my $move = $col_want - $col;
2538 291 ▶ if ( $move > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($move > 0) { } 93 198 291
elsif ($move < 0) { } 0 198 198
2539  
2540 # limit padding increase in isolated two lines
2541 next
2542 if ( defined( $rmax_move->{$ng} )
2543 && $move > $rmax_move->{$ng}
2544 93 ▶ && !$is_good_alignment_token{$raw_tok} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rmax_move->{$ng} and $move > $rmax_move->{$ng} and not $is_good_alignment_token{$raw_tok} 0 93 93
Condition50%3/6
defined $rmax_move->{$ng} and $move > $rmax_move->{$ng} and not $is_good_alignment_token{$raw_tok}
!ll&&!rl&&r
9300
defined $rmax_move->{$ng} and $move > $rmax_move->{$ng}
!ll&&!rl&&r
71220
MC/DC0%0/3
defined $rmax_move->{$ng} and $move > $rmax_move->{$ng} and not $is_good_alignment_token{$raw_tok}
defined $rmax_move->{$ng} $move > $rmax_move->{$ng} not $is_good_alignment_token{$raw_tok}
Truth table50%2/4
A and B and not(C)
A= defined $rmax_move->{$ng}B= $move > $rmax_move->{$ng}C= not $is_good_alignment_token{$raw_tok}
ABCresult
0XX0
10X0
1100
1111
2545  
2546 93 $line->increase_field_width( $itok, $move );
2547 }
2548 elsif ( $move < 0 ) {
2549  
2550 # spot to take special action on failure to move
2551 }
2552 else {
2553 ## ok: (move==0)
2554 }
2555 }
2556 128 return;
2557 } ## end sub move_to_common_column
2558  
2559 sub tap_dancer {
2560  
2561 75 ▶ my ($rcall_hash) = @_;
Subroutine tap_dancer: called CC=33 SCAR=35.2
Pod undocumented
2562  
2563 # This is the worker routine for sub 'sweep_left_to_right'. It makes
2564 # vertical alignments as it sweeps from left to right over groups
2565 # of lines which have been located and prepared by the caller.
2566  
2567 75 my $rlines = $rcall_hash->{rlines};
2568 75 my $rgroups = $rcall_hash->{rgroups};
2569 75 my $rtodo = $rcall_hash->{rtodo};
2570 75 my $rmax_move = $rcall_hash->{rmax_move};
2571 75 my $short_pad = $rcall_hash->{short_pad};
2572 75 my $group_level = $rcall_hash->{group_level};
2573  
2574 # $blocking_level[$nj is the level at a match failure between groups
2575 # $ng-1 and $ng
2576 75 my @blocking_level;
2577 75 my $group_list_type = $rlines->[0]->{'list_type'};
2578  
2579 75 foreach my $task ( @{$rtodo} ) {
2580 137 my ( $itok, $ng_beg, $ng_end, $raw_tok, $lev ) = @{$task};
2581  
2582 # Nothing to do for a single group
2583 137 ▶ next if ( $ng_end <= $ng_beg );
Branch: 1 branch
BranchTrueFalseTotal
if $ng_end <= $ng_beg 0 137 137
2584  
2585 137 my $ng_first; # index of the first group of a continuous sequence
2586 my $col_want; # the common alignment column of a sequence of groups
2587 137 my $col_limit; # maximum column before bumping into max line length
2588 137 my $line_count_ng_m = 0;
2589 137 my $jmax_m;
2590 my $it_stop_m;
2591  
2592 # Loop over the groups
2593 # 'ix_' = index in the array of lines
2594 # 'ng_' = index in the array of groups
2595 # 'it_' = index in the array of tokens
2596 137 my $ix_min = $rgroups->[$ng_beg]->[0];
2597 137 my $ix_max = $rgroups->[$ng_end]->[1];
2598 137 my $lines_total = $ix_max - $ix_min + 1;
2599 137 foreach my $ng ( $ng_beg .. $ng_end ) {
2600 312 my ( $ix_beg, $ix_end, $it_stop ) = @{ $rgroups->[$ng] };
2601 312 my $line_count_ng = $ix_end - $ix_beg + 1;
2602  
2603 # Important: note that since all lines in a group have a common
2604 # alignments object, we just have to work on one of the lines
2605 # (the first line). All of the rest will be changed
2606 # automatically.
2607 312 my $line = $rlines->[$ix_beg];
2608 312 my $jmax = $line->{'jmax'};
2609  
2610 # the maximum space without exceeding the line length:
2611 312 my $avail = $line->get_available_space_on_right();
2612 312 my $col = $line->get_column($itok);
2613 312 my $col_max = $col + $avail;
2614  
2615 # Initialize on first group
2616 312 ▶ if ( !defined($col_want) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col_want) 137 175 312
2617 137 $ng_first = $ng;
2618 137 $col_want = $col;
2619 137 $col_limit = $col_max;
2620 137 $line_count_ng_m = $line_count_ng;
2621 137 $jmax_m = $jmax;
2622 137 $it_stop_m = $it_stop;
2623 137 next;
2624 }
2625  
2626 # RULE: Throw a blocking flag upon encountering a token level
2627 # different from the level of the first blocking token. For
2628 # example, in the following example, if the = matches get
2629 # blocked between two groups as shown, then we want to start
2630 # blocking matches at the commas, which are at deeper level, so
2631 # that we do not get the big gaps shown here:
2632  
2633 # my $unknown3 = pack( "v", -2 );
2634 # my $unknown4 = pack( "v", 0x09 );
2635 # my $unknown5 = pack( "VVV", 0x06, 0x00, 0x00 );
2636 # my $num_bbd_blocks = pack( "V", $num_lists );
2637 # my $root_startblock = pack( "V", $root_start );
2638 # my $unknown6 = pack( "VV", 0x00, 0x1000 );
2639  
2640 # On the other hand, it is okay to keep matching at the same
2641 # level such as in a simple list of commas and/or fat commas.
2642  
2643 175 ▶ my $is_blocked = defined( $blocking_level[$ng] )
Condition66%2/3
defined $blocking_level[$ng] && $lev > $blocking_level[$ng]
!ll&&!rl&&r
17140
MC/DC0%0/2
defined $blocking_level[$ng] && $lev > $blocking_level[$ng]
defined $blocking_level[$ng] $lev > $blocking_level[$ng]
Truth table66%2/3
A && B
A= defined $blocking_level[$ng]B= $lev > $blocking_level[$ng]
ABresult
0X0
100
111
2644 && $lev > $blocking_level[$ng];
2645  
2646 # TAIL-WAG-DOG RULE: prevent a 'tail-wag-dog' syndrome, meaning:
2647 # Do not let one or two lines with a **different number of
2648 # alignments** open up a big gap in a large block. For
2649 # example, we will prevent something like this, where the first
2650 # line pries open the rest:
2651  
2652 # $worksheet->write( "B7", "http://www.perl.com", undef, $format );
2653 # $worksheet->write( "C7", "", $format );
2654 # $worksheet->write( "D7", "", $format );
2655 # $worksheet->write( "D8", "", $format );
2656 # $worksheet->write( "D8", "", $format );
2657  
2658 # We should exclude from consideration two groups which are
2659 # effectively the same but separated because one does not
2660 # fit in the maximum allowed line length.
2661 175 ▶ my $is_same_group =
Condition100%3/3
$jmax == $jmax_m && $it_stop_m == $jmax_m - 2
!ll&&!rl&&r
139729
MC/DC100%2/2
$jmax == $jmax_m && $it_stop_m == $jmax_m - 2
$jmax == $jmax_m $it_stop_m == $jmax_m - 2
Truth table100%3/3
A && B
A= $jmax == $jmax_mB= $it_stop_m == $jmax_m - 2
ABresult
0X0
100
111
2662 $jmax == $jmax_m && $it_stop_m == $jmax_m - 2;
2663  
2664 175 my $lines_above = $ix_beg - $ix_min;
2665 175 my $lines_below = $lines_total - $lines_above;
2666  
2667 # Increase the tolerable gap for certain favorable factors
2668 175 my $factor = 1;
2669 175 my $top_level = $lev == $group_level;
2670  
2671 # Align best top level alignment tokens like '=', 'if', ...
2672 # A factor of 10 allows a gap of up to 40 spaces
2673 175 ▶ if ( $top_level && $is_good_alignment_token{$raw_tok} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top_level and $is_good_alignment_token{$raw_tok}) { } 56 119 175
Condition100%3/3
$top_level and $is_good_alignment_token{$raw_tok}
!ll&&!rl&&r
417856
MC/DC100%2/2
$top_level and $is_good_alignment_token{$raw_tok}
$top_level $is_good_alignment_token{$raw_tok}
Truth table100%3/3
A and B
A= $top_levelB= $is_good_alignment_token{$raw_tok}
ABresult
0X0
100
111
2674 56 $factor = 10;
2675 }
2676  
2677 # Otherwise allow some minimal padding of good alignments
2678 else {
2679  
2680 119 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $is_good_alignment_token{$raw_tok} and $lines_total > 2 || $group_list_type && $jmax < $jmax_m && $top_level) 107 12 119
Condition91%11/12
defined $is_good_alignment_token{$raw_tok} and $lines_total > 2 || $group_list_type && $jmax < $jmax_m && $top_level
!ll&&!rl&&r
36155
$lines_total > 2 or $group_list_type and $jmax < $jmax_m and $top_level
l!l&&r!l&&!r
64439
$group_list_type and $jmax < $jmax_m and $top_level
!ll&&!rl&&r
9043
$group_list_type and $jmax < $jmax_m
!ll&&!rl&&r
2743
MC/DC20%1/5
defined $is_good_alignment_token{$raw_tok} and $lines_total > 2 || $group_list_type && $jmax < $jmax_m && $top_level
defined $is_good_alignment_token{$raw_tok} $lines_total > 2 $group_list_type $jmax < $jmax_m $top_level
Truth table33%2/6
A and B or C and D and E
A= defined $is_good_alignment_token{$raw_tok}B= $lines_total > 2C= $group_list_typeD= $jmax < $jmax_mE= $top_level
ABCDEresult
0XXXX0
100XX0
1010X0
101100
11XXX1
101111
2681  
2682 defined( $is_good_alignment_token{$raw_tok} )
2683  
2684 # We have to be careful if there are just 2 lines.
2685 # This two-line factor allows large gaps only for 2
2686 # lines which are simple lists with fewer items on the
2687 # second line. It gives results similar to previous
2688 # versions of perltidy.
2689 && (
2690 $lines_total > 2
2691 || ( $group_list_type
2692 && $jmax < $jmax_m
2693 && $top_level )
2694 )
2695 )
2696 {
2697 107 $factor += 1;
2698 107 ▶ if ($top_level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top_level) 71 36 107
2699 71 $factor += 1;
2700 }
2701 }
2702 }
2703  
2704 175 my $is_big_gap;
2705 175 ▶ if ( !$is_same_group ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_same_group) 146 29 175
2706 146 ▶ $is_big_gap ||=
Condition75%9/12
$is_big_gap ||= ($lines_above == 1 || $lines_above == 2 && $lines_below >= 4) && $col_want > $col + $short_pad * $factor
l!l&&r!l&&!r
00146
($lines_above == 1 || $lines_above == 2 && $lines_below >= 4) && $col_want > $col + $short_pad * $factor
!ll&&!rl&&r
56900
$lines_above == 1 or $lines_above == 2 and $lines_below >= 4
l!l&&r!l&&!r
823034
$lines_above == 2 and $lines_below >= 4
!ll&&!rl&&r
30268
MC/DC0%0/5
$is_big_gap ||= ($lines_above == 1 || $lines_above == 2 && $lines_below >= 4) && $col_want > $col + $short_pad * $factor
$is_big_gap $lines_above == 1 $lines_above == 2 $lines_below >= 4 $col_want > $col + $short_pad * $factor
Truth table0%0/7
A ||= B or C and D && E
A= $is_big_gapB= $lines_above == 1C= $lines_above == 2D= $lines_below >= 4E= $col_want > $col + $short_pad * $factor
ABCDEresult
1XXXX1
01XX11
001111
000XX0
0010X0
01XX00
001100
2707 ( $lines_above == 1
2708 || $lines_above == 2 && $lines_below >= 4 )
2709 && $col_want > $col + $short_pad * $factor;
2710 146 ▶ $is_big_gap ||=
Condition75%9/12
$is_big_gap ||= ($lines_below == 1 || $lines_below == 2 && $lines_above >= 4) && $col > $col_want + $short_pad * $factor
l!l&&r!l&&!r
00146
($lines_below == 1 || $lines_below == 2 && $lines_above >= 4) && $col > $col_want + $short_pad * $factor
!ll&&!rl&&r
53930
$lines_below == 1 or $lines_below == 2 and $lines_above >= 4
l!l&&r!l&&!r
921935
$lines_below == 2 and $lines_above >= 4
!ll&&!rl&&r
33201
MC/DC0%0/5
$is_big_gap ||= ($lines_below == 1 || $lines_below == 2 && $lines_above >= 4) && $col > $col_want + $short_pad * $factor
$is_big_gap $lines_below == 1 $lines_below == 2 $lines_above >= 4 $col > $col_want + $short_pad * $factor
Truth table0%0/7
A ||= B or C and D && E
A= $is_big_gapB= $lines_below == 1C= $lines_below == 2D= $lines_above >= 4E= $col > $col_want + $short_pad * $factor
ABCDEresult
1XXXX1
01XX11
001111
000XX0
0010X0
01XX00
001100
2711 ( $lines_below == 1
2712 || $lines_below == 2 && $lines_above >= 4 )
2713 && $col > $col_want + $short_pad * $factor;
2714 }
2715  
2716 # if match is limited by gap size, stop aligning at this level
2717 175 ▶ if ($is_big_gap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_big_gap) 0 175 175
2718 0 $blocking_level[$ng] = $lev - 1;
2719 }
2720  
2721 # quit and restart if it cannot join this batch
2722 175 ▶ if ( $col_want > $col_max
Branch: 1 branch
BranchTrueFalseTotal
if ($col_want > $col_max or $col > $col_limit or $is_big_gap or $is_blocked) 0 163 163
Condition77%7/9
$col_want > $col_max or $col > $col_limit or $is_big_gap or $is_blocked
l!l&&r!l&&!r
120163
$col_want > $col_max or $col > $col_limit or $is_big_gap
l!l&&r!l&&!r
120163
$col_want > $col_max or $col > $col_limit
l!l&&r!l&&!r
57163
MC/DC50%2/4
$col_want > $col_max or $col > $col_limit or $is_big_gap or $is_blocked
$col_want > $col_max $col > $col_limit $is_big_gap $is_blocked
Truth table60%3/5
A or B or C or D
A= $col_want > $col_maxB= $col > $col_limitC= $is_big_gapD= $is_blocked
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2723 || $col > $col_limit
2724 || $is_big_gap
2725 || $is_blocked )
2726 {
2727  
2728 # remember the level of the first blocking token
2729 12 ▶ if ( !defined( $blocking_level[$ng] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $blocking_level[$ng]) 10 2 12
2730 10 $blocking_level[$ng] = $lev;
2731 }
2732  
2733 move_to_common_column(
2734 {
2735 12 rlines => $rlines,
2736 rgroups => $rgroups,
2737 rmax_move => $rmax_move,
2738 ngb => $ng_first,
2739 nge => $ng - 1,
2740 itok => $itok,
2741 col_want => $col_want,
2742 raw_tok => $raw_tok,
2743 }
2744 );
2745 12 $ng_first = $ng;
2746 12 $col_want = $col;
2747 12 $col_limit = $col_max;
2748 12 $line_count_ng_m = $line_count_ng;
2749 12 $jmax_m = $jmax;
2750 12 $it_stop_m = $it_stop;
2751 12 next;
2752 }
2753  
2754 163 $line_count_ng_m += $line_count_ng;
2755  
2756 # update the common column and limit
2757 163 ▶ if ( $col > $col_want ) { $col_want = $col }
Branch: 1 branch
BranchTrueFalseTotal
if ($col > $col_want) 49 114 163
2758 163 ▶ if ( $col_max < $col_limit ) { $col_limit = $col_max }
Branch: 1 branch
BranchTrueFalseTotal
if ($col_max < $col_limit) 47 116 163
2759  
2760 } ## end loop over groups
2761  
2762 137 ▶ if ( $ng_end > $ng_first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ng_end > $ng_first) 128 9 137
2763 128 move_to_common_column(
2764 {
2765 rlines => $rlines,
2766 rgroups => $rgroups,
2767 rmax_move => $rmax_move,
2768 ngb => $ng_first,
2769 nge => $ng_end,
2770 itok => $itok,
2771 col_want => $col_want,
2772 raw_tok => $raw_tok,
2773 }
2774 );
2775 }
2776 } ## end loop over tasks
2777  
2778 75 return;
2779 } ## end sub tap_dancer
2780 }
2781  
2782 45 sub delete_selected_tokens {
2783  
2784 545 ▶ my ( $line_obj, $ridel ) = @_;
Subroutine delete_selected_tokens: called CC=13 SCAR=25.7
Pod undocumented
2785  
2786 # Given:
2787 # $line_obj = the line to be modified
2788 # $ridel = a ref to list of indexes to be deleted
2789  
2790 # remove unused alignment token(s) to improve alignment chances
2791  
2792 545 ▶ return if ( !defined($line_obj) || !defined($ridel) || !@{$ridel} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line_obj and defined $ridel and @{$ridel;} 0 545 545
Condition33%2/6
defined $line_obj and defined $ridel and @{$ridel;}
!ll&&!rl&&r
00545
defined $line_obj and defined $ridel
!ll&&!rl&&r
00545
MC/DC0%0/3
defined $line_obj and defined $ridel and @{$ridel;}
defined $line_obj defined $ridel @{$ridel;}
Truth table25%1/4
A and B and C
A= defined $line_objB= defined $ridelC= @{$ridel;}
ABCresult
0XX0
10X0
1100
1111
2793  
2794 545 my $jmax_old = $line_obj->{'jmax'};
2795 545 my $rfields_old = $line_obj->{'rfields'};
2796 545 my $rfield_lengths_old = $line_obj->{'rfield_lengths'};
2797 545 my $rpatterns_old = $line_obj->{'rpatterns'};
2798 545 my $rtokens_old = $line_obj->{'rtokens'};
2799 545 my $j_terminal_match = $line_obj->{'j_terminal_match'};
2800  
2801 45 ▶ use constant EXPLAIN_DELETE_SELECTED => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2802  
2803 545 local $LIST_SEPARATOR = '> <';
2804 545 EXPLAIN_DELETE_SELECTED && print <<EOM;
2805 delete indexes: <@{$ridel}>
2806 old jmax: $jmax_old
2807 old tokens: <@{$rtokens_old}>
2808 old patterns: <@{$rpatterns_old}>
2809 old fields: <@{$rfields_old}>
2810 old field_lengths: <@{$rfield_lengths_old}>
2811 EOM
2812  
2813 545 my $rfields_new = [];
2814 545 my $rpatterns_new = [];
2815 545 my $rtokens_new = [];
2816 545 my $rfield_lengths_new = [];
2817  
2818 # Convert deletion list to a hash to allow any order, multiple entries,
2819 # and avoid problems with index values out of range
2820 545 my %delete_me = map { $_ => 1 } @{$ridel};
2821  
2822 545 my $pattern_0 = $rpatterns_old->[0];
2823 545 my $field_0 = $rfields_old->[0];
2824 545 my $field_length_0 = $rfield_lengths_old->[0];
2825 545 push @{$rfields_new}, $field_0;
2826 545 push @{$rfield_lengths_new}, $field_length_0;
2827 545 push @{$rpatterns_new}, $pattern_0;
2828  
2829 # Loop to either copy items or concatenate fields and patterns
2830 545 my $jmin_del;
2831 545 foreach my $j ( 0 .. $jmax_old - 1 ) {
2832 1745 my $token = $rtokens_old->[$j];
2833 1745 my $field = $rfields_old->[ $j + 1 ];
2834 1745 my $field_length = $rfield_lengths_old->[ $j + 1 ];
2835 1745 my $pattern = $rpatterns_old->[ $j + 1 ];
2836 1745 ▶ if ( !$delete_me{$j} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $delete_me{$j}) { } 850 895 1745
2837 850 push @{$rtokens_new}, $token;
2838 850 push @{$rfields_new}, $field;
2839 850 push @{$rpatterns_new}, $pattern;
2840 850 push @{$rfield_lengths_new}, $field_length;
2841 }
2842 else {
2843 895 ▶ if ( !defined($jmin_del) ) { $jmin_del = $j }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $jmin_del) 545 350 895
2844 895 $rfields_new->[-1] .= $field;
2845 895 $rfield_lengths_new->[-1] += $field_length;
2846 895 $rpatterns_new->[-1] .= $pattern;
2847 }
2848 }
2849  
2850 # ----- x ------ x ------ x ------
2851 #t 0 1 2 <- token indexing
2852 #f 0 1 2 3 <- field and pattern
2853  
2854 545 my $jmax_new = @{$rfields_new} - 1;
2855 545 $line_obj->{'rtokens'} = $rtokens_new;
2856 545 $line_obj->{'rpatterns'} = $rpatterns_new;
2857 545 $line_obj->{'rfields'} = $rfields_new;
2858 545 $line_obj->{'rfield_lengths'} = $rfield_lengths_new;
2859 545 $line_obj->{'jmax'} = $jmax_new;
2860  
2861 # The value of j_terminal_match will be incorrect if we delete tokens prior
2862 # to it. We will have to give up on aligning the terminal tokens if this
2863 # happens.
2864 545 ▶ if ( defined($j_terminal_match) && $jmin_del <= $j_terminal_match ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $j_terminal_match and $jmin_del <= $j_terminal_match) 1 544 545
Condition100%3/3
defined $j_terminal_match and $jmin_del <= $j_terminal_match
!ll&&!rl&&r
54221
MC/DC100%2/2
defined $j_terminal_match and $jmin_del <= $j_terminal_match
defined $j_terminal_match $jmin_del <= $j_terminal_match
Truth table100%3/3
A and B
A= defined $j_terminal_matchB= $jmin_del <= $j_terminal_match
ABresult
0X0
100
111
2865 1 $line_obj->{'j_terminal_match'} = undef;
2866 }
2867  
2868 # update list type -
2869 545 ▶ if ( $line_obj->{'list_seqno'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_obj->{"list_seqno"}) 76 469 545
2870  
2871 ## This works, but for efficiency see if we need to make a change:
2872 ## decide_if_list($line_obj);
2873  
2874 # An existing list will still be a list but with possibly different
2875 # leading token
2876 76 my $old_list_type = $line_obj->{'list_type'};
2877 76 my $new_list_type = EMPTY_STRING;
2878 76 ▶ if ( $rtokens_new->[0] =~ /^(=>|,)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtokens_new->[0] =~ /^(=>|,)/) 49 27 76
2879 49 $new_list_type = $rtokens_new->[0];
2880 }
2881 76 ▶ if ( !$old_list_type || $old_list_type ne $new_list_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $old_list_type or $old_list_type ne $new_list_type) 44 32 76
Condition100%3/3
not $old_list_type or $old_list_type ne $new_list_type
l!l&&r!l&&!r
192532
MC/DC100%2/2
not $old_list_type or $old_list_type ne $new_list_type
not $old_list_type $old_list_type ne $new_list_type
Truth table100%3/3
not(A) or B
A= not $old_list_typeB= $old_list_type ne $new_list_type
ABresult
1X1
011
000
2882 44 decide_if_list($line_obj);
2883 }
2884 }
2885  
2886 545 EXPLAIN_DELETE_SELECTED && print <<EOM;
2887  
2888 new jmax: $jmax_new
2889 new tokens: <@{$rtokens_new}>
2890 new patterns: <@{$rpatterns_new}>
2891 new fields: <@{$rfields_new}>
2892 EOM
2893 545 return;
2894 } ## end sub delete_selected_tokens
2895  
2896 { ## closure for sub decode_alignment_token
2897  
2898 # This routine is called repeatedly for each token, so it needs to be
2899 # efficient. We can speed things up by remembering the inputs and outputs
2900 # in a hash.
2901 45 my %decoded_token;
2902  
2903 sub initialize_decode {
2904  
2905 # We will re-initialize the hash for each file. Otherwise, there is
2906 # a danger that the hash can become arbitrarily large if a very large
2907 # number of files is processed at once.
2908 661 ▶ %decoded_token = ();
Subroutine initialize_decode: called CC=1 SCAR=0.0
Pod undocumented
2909 661 return;
2910 } ## end sub initialize_decode
2911  
2912 sub decode_alignment_token {
2913  
2914 10973 ▶ my ($tok) = @_;
Subroutine decode_alignment_token: called CC=5 SCAR=16.1
Pod undocumented
2915  
2916 # Unpack the values packed in an alignment token
2917  
2918 # Given:
2919 # $tok = an alignment token
2920 # Returns:
2921 # ( $raw_tok, $lev, $tag, $tok_count )
2922 #
2923 # Usage:
2924 # my ( $raw_tok, $lev, $tag, $tok_count ) =
2925 # decode_alignment_token($token);
2926  
2927 # Alignment tokens have a trailing decimal level and optional tag (for
2928 # commas):
2929 # For example, the first comma in the following line
2930 # sub banner { crlf; report( shift, '/', shift ); crlf }
2931 # is decorated as follows:
2932 # ,2+report-6 => (tok,lev,tag) =qw( , 2 +report-6)
2933  
2934 # An optional token count may be appended with a leading dot.
2935 # Currently this is only done for '=' tokens but this could change.
2936 # For example, consider the following line:
2937 # $nport = $port = shift || $name;
2938 # The first '=' may either be '=0' or '=0.1' [level 0, first equals]
2939 # The second '=' will be '=0.2' [level 0, second equals]
2940  
2941 10973 ▶ if ( defined( $decoded_token{$tok} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded_token{$tok}) 9286 1687 10973
2942 9286 return @{ $decoded_token{$tok} };
2943 }
2944  
2945 1687 my ( $raw_tok, $lev, $tag, $tok_count ) = ( $tok, 0, EMPTY_STRING, 1 );
2946 1687 ▶ if ( $tok =~ /^(\D+)(\d+)([^\.]*)(\.(\d+))?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok =~ /^(\D+)(\d+)([^\.]*)(\.(\d+))?$/) 1325 362 1687
2947 1325 $raw_tok = $1;
2948 1325 $lev = $2;
2949 1325 ▶ $tag = $3 if ($3);
Branch: 1 branch
BranchTrueFalseTotal
if $3 511 814 1325
2950 1325 ▶ $tok_count = $5 if ($5);
Branch: 1 branch
BranchTrueFalseTotal
if $5 71 1254 1325
2951 }
2952 1687 my @vals = ( $raw_tok, $lev, $tag, $tok_count );
2953 1687 $decoded_token{$tok} = \@vals;
2954 1687 return @vals;
2955 } ## end sub decode_alignment_token
2956 }
2957  
2958 45 sub delete_unmatched_tokens {
2959  
2960 2061 ▶ my ( $rlines, $group_level ) = @_;
Subroutine delete_unmatched_tokens: called CC=7 SCAR=19.5
Pod undocumented
2961  
2962 # Remove as many obviously un-needed alignment tokens as possible.
2963 # This will prevent them from interfering with the final alignment.
2964  
2965 # Given:
2966 # $rlines = ref to hash of all lines in this alignment group
2967 # $group_level = their comment indentation level
2968  
2969 # Return:
2970 2061 my $max_lev_diff = 0; # used to avoid a call to prune_tree
2971 2061 my $saw_side_comment = 0; # used to avoid a call for side comments
2972 2061 my $saw_signed_number = 0; # used to avoid a call for -vsn
2973  
2974 # Handle no lines -- shouldn't happen
2975 2061 ▶ return unless ( @{$rlines} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rlines;} 0 2061 2061
2976  
2977 # Handle a single line
2978 2061 ▶ if ( @{$rlines} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rlines;} == 1) 1380 681 2061
2979 1380 my $line = $rlines->[0];
2980 1380 my $jmax = $line->{'jmax'};
2981 1380 my $length = $line->{'rfield_lengths'}->[$jmax];
2982 1380 $saw_side_comment = $length > 0;
2983 1380 return ( $max_lev_diff, $saw_side_comment, $saw_signed_number );
2984 }
2985  
2986 # ignore hanging side comments in these operations
2987 681 my @filtered = grep { !$_->{'is_hanging_side_comment'} } @{$rlines};
2988 681 my $rnew_lines = \@filtered;
2989  
2990 681 $saw_side_comment = @filtered != @{$rlines};
2991 681 $max_lev_diff = 0;
2992  
2993 # nothing to do if all lines were hanging side comments
2994 681 my $jmax = @{$rnew_lines} - 1;
2995 681 ▶ return ( $max_lev_diff, $saw_side_comment, $saw_signed_number )
Branch: 1 branch
BranchTrueFalseTotal
if $jmax < 0 1 680 681
2996 if ( $jmax < 0 );
2997  
2998 #----------------------------------------------------
2999 # Create a hash of alignment token info for each line
3000 #----------------------------------------------------
3001 680 ( my $rline_hashes, my $requals_info, $saw_side_comment, $max_lev_diff ) =
3002 make_alignment_info( $group_level, $rnew_lines, $saw_side_comment );
3003  
3004 #------------------------------------------------------------
3005 # Find independent subgroups of lines. Neighboring subgroups
3006 # do not have a common alignment token.
3007 #------------------------------------------------------------
3008 680 my @subgroups;
3009 680 push @subgroups, [ 0, $jmax ];
3010 680 foreach my $jl ( 0 .. $jmax - 1 ) {
3011 1539 ▶ if ( $rnew_lines->[$jl]->{'end_group'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rnew_lines->[$jl]{"end_group"}) 79 1460 1539
3012 79 $subgroups[-1]->[1] = $jl;
3013 79 push @subgroups, [ $jl + 1, $jmax ];
3014 }
3015 }
3016  
3017 #-----------------------------------------------------------
3018 # PASS 1 over subgroups to remove unmatched alignment tokens
3019 #-----------------------------------------------------------
3020 delete_unmatched_tokens_main_loop(
3021 680 $group_level, $rnew_lines, \@subgroups,
3022 $rline_hashes, $requals_info
3023 );
3024  
3025 #----------------------------------------------------------------
3026 # PASS 2: Construct a tree of matched lines and delete some small
3027 # deeper levels of tokens. They also block good alignments.
3028 #----------------------------------------------------------------
3029 680 ▶ prune_alignment_tree($rnew_lines) if ($max_lev_diff);
Branch: 1 branch
BranchTrueFalseTotal
if $max_lev_diff 174 506 680
3030  
3031 #--------------------------------------------
3032 # PASS 3: compare all lines for common tokens
3033 #--------------------------------------------
3034 680 $saw_signed_number =
3035 match_line_pairs( $rlines, $rnew_lines, \@subgroups, $group_level );
3036  
3037 680 return ( $max_lev_diff, $saw_side_comment, $saw_signed_number );
3038 } ## end sub delete_unmatched_tokens
3039  
3040 sub make_alignment_info {
3041  
3042 680 ▶ my ( $group_level, $rnew_lines, $saw_side_comment ) = @_;
Subroutine make_alignment_info: called CC=27 SCAR=33.0
Pod undocumented
3043  
3044 # Create a hash of alignment token info for each line
3045 # This info will be used to find common alignments
3046  
3047 # Given:
3048 # $group_level = common indentation level
3049 # $rnew_lines = ref to hash of line info
3050 # $saw_side_comment = true if there is a side comment
3051 # Return:
3052 # $rline_hashes = ref to hash with new line vars
3053 # \@equals_info = ref to array with info on any '=' tokens
3054 # $saw_side_comment = updated side comment flag
3055 # $max_lev_diff = maximum level change seen
3056  
3057 #----------------
3058 # Loop over lines
3059 #----------------
3060 680 my $rline_hashes = [];
3061 680 my @equals_info;
3062 680 my $jmax = @{$rnew_lines} - 1;
3063 680 my $max_lev_diff = 0;
3064 680 foreach my $line ( @{$rnew_lines} ) {
3065 2219 my $rhash = {};
3066 2219 my $rtokens = $line->{'rtokens'};
3067 2219 my $rpatterns = $line->{'rpatterns'};
3068 2219 my $i = 0;
3069 2219 my ( $i_eq, $tok_eq, $pat_eq );
3070 2219 my ( $lev_min, $lev_max );
3071 2219 foreach my $tok ( @{$rtokens} ) {
3072 6109 my ( $raw_tok, $lev, $tag, $tok_count ) =
3073 decode_alignment_token($tok);
3074  
3075 6109 ▶ if ( $tok ne '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok ne '#') { } 3890 2219 6109
3076 3890 ▶ if ( !defined($lev_min) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lev_min) { } 2051 1839 3890
3077 2051 $lev_min = $lev;
3078 2051 $lev_max = $lev;
3079 }
3080 else {
3081 1839 ▶ if ( $lev < $lev_min ) { $lev_min = $lev }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev < $lev_min) 84 1755 1839
3082 1839 ▶ if ( $lev > $lev_max ) { $lev_max = $lev }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev > $lev_max) 305 1534 1839
3083 }
3084 }
3085 else {
3086 2219 ▶ if ( !$saw_side_comment ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_side_comment) 2012 207 2219
3087 2012 my $length = $line->{'rfield_lengths'}->[ $i + 1 ];
3088 2012 ▶ $saw_side_comment ||= $length;
Condition66%2/3
$saw_side_comment ||= $length
l!l&&r!l&&!r
0681944
MC/DC50%1/2
$saw_side_comment ||= $length
$saw_side_comment $length
Truth table66%2/3
A ||= B
A= $saw_side_commentB= $length
ABresult
1X1
011
000
3089 }
3090 }
3091  
3092 # Possible future upgrade: for multiple matches,
3093 # record [$i1, $i2, ..] instead of $i
3094 6109 $rhash->{$tok} =
3095 [ $i, undef, undef, $raw_tok, $lev, $tag, $tok_count ];
3096  
3097 # remember the first equals at line level
3098 6109 ▶ if ( !defined($i_eq) && $raw_tok eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $i_eq and $raw_tok eq "=") 638 5471 6109
Condition100%3/3
not defined $i_eq and $raw_tok eq "="
!ll&&!rl&&r
8714600638
MC/DC100%2/2
not defined $i_eq and $raw_tok eq "="
not defined $i_eq $raw_tok eq "="
Truth table100%3/3
not(A) and B
A= not defined $i_eqB= $raw_tok eq "="
ABresult
0X0
100
111
3099  
3100 638 ▶ if ( $lev eq $group_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lev eq $group_level) 521 117 638
3101 521 $i_eq = $i;
3102 521 $tok_eq = $tok;
3103 521 $pat_eq = $rpatterns->[$i];
3104 }
3105 }
3106 6109 $i++;
3107 }
3108 2219 push @{$rline_hashes}, $rhash;
3109 2219 push @equals_info, [ $i_eq, $tok_eq, $pat_eq ];
3110 2219 ▶ if ( defined($lev_min) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lev_min) 2051 168 2219
3111 2051 my $lev_diff = $lev_max - $lev_min;
3112 2051 ▶ if ( $lev_diff > $max_lev_diff ) { $max_lev_diff = $lev_diff }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev_diff > $max_lev_diff) 182 1869 2051
3113 }
3114 }
3115  
3116 #----------------------------------------------------
3117 # Loop to compare each line pair and remember matches
3118 #----------------------------------------------------
3119 680 my $rtok_hash = {};
3120 680 my $nr = 0;
3121 680 foreach my $jl ( 0 .. $jmax - 1 ) {
3122 1539 my $nl = $nr;
3123 1539 $nr = 0;
3124 1539 my $jr = $jl + 1;
3125 1539 my $rhash_l = $rline_hashes->[$jl];
3126 1539 my $rhash_r = $rline_hashes->[$jr];
3127 1539 foreach my $tok ( keys %{$rhash_l} ) {
3128 3683 ▶ if ( defined( $rhash_r->{$tok} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rhash_r->{$tok}) 3120 563 3683
3129 3120 my $il = $rhash_l->{$tok}->[0];
3130 3120 my $ir = $rhash_r->{$tok}->[0];
3131 3120 $rhash_l->{$tok}->[2] = $ir;
3132 3120 $rhash_r->{$tok}->[1] = $il;
3133 3120 ▶ if ( $tok ne '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok ne "#") 1581 1539 3120
3134 1581 push @{ $rtok_hash->{$tok} }, ( $jl, $jr );
3135 1581 $nr++;
3136 }
3137 }
3138 }
3139  
3140 # Set a line break if no matching tokens between these lines
3141 # (this is not strictly necessary now but does not hurt)
3142 1539 ▶ if ( $nr == 0 && $nl > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nr == 0 and $nl > 0) 39 1500 1539
Condition100%3/3
$nr == 0 and $nl > 0
!ll&&!rl&&r
119430639
MC/DC100%2/2
$nr == 0 and $nl > 0
$nr == 0 $nl > 0
Truth table100%3/3
A and B
A= $nr == 0B= $nl > 0
ABresult
0X0
100
111
3143 39 $rnew_lines->[$jl]->{'end_group'} = 1;
3144 }
3145  
3146 # Also set a line break if both lines have simple equals but with
3147 # different leading characters in patterns. This check is similar
3148 # to one in sub check_match, and will prevent sub
3149 # prune_alignment_tree from removing alignments which otherwise
3150 # should be kept. This fix is rarely needed, but it can
3151 # occasionally improve formatting.
3152 # For example:
3153 # my $name = $this->{Name};
3154 # $type = $this->ctype($genlooptype) if defined $genlooptype;
3155 # my $declini = ( $asgnonly ? "" : "\t$type *" );
3156 # my $cast = ( $type ? "($type *)" : "" );
3157 # The last two lines start with 'my' and will not match the
3158 # previous line starting with $type, so we do not want
3159 # prune_alignment tree to delete their ? : alignments at a deeper
3160 # level.
3161 1539 my ( $i_eq_l, $tok_eq_l, $pat_eq_l ) = @{ $equals_info[$jl] };
3162 1539 my ( $i_eq_r, $tok_eq_r, $pat_eq_r ) = @{ $equals_info[$jr] };
3163 1539 ▶ if ( defined($i_eq_l) && defined($i_eq_r) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i_eq_l and defined $i_eq_r) 268 1271 1539
Condition100%3/3
defined $i_eq_l and defined $i_eq_r
!ll&&!rl&&r
1165106268
MC/DC100%2/2
defined $i_eq_l and defined $i_eq_r
defined $i_eq_l defined $i_eq_r
Truth table100%3/3
A and B
A= defined $i_eq_lB= defined $i_eq_r
ABresult
0X0
100
111
3164  
3165 # Also, do not align equals across a change in ci level
3166 my $ci_jump = $rnew_lines->[$jl]->{'ci_level'} !=
3167 268 $rnew_lines->[$jr]->{'ci_level'};
3168  
3169 268 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($tok_eq_l eq $tok_eq_r and $i_eq_l == 0 and $i_eq_r == 0 and substr($pat_eq_l, 0, 1) ne substr($pat_eq_r, 0, 1) || $ci_jump) 13 255 268
Condition83%10/12
$tok_eq_l eq $tok_eq_r and $i_eq_l == 0 and $i_eq_r == 0 and substr($pat_eq_l, 0, 1) ne substr($pat_eq_r, 0, 1) || $ci_jump
!ll&&!rl&&r
52621
$tok_eq_l eq $tok_eq_r and $i_eq_l == 0 and $i_eq_r == 0
!ll&&!rl&&r
50263
$tok_eq_l eq $tok_eq_r and $i_eq_l == 0
!ll&&!rl&&r
05263
substr($pat_eq_l, 0, 1) ne substr($pat_eq_r, 0, 1) or $ci_jump
l!l&&r!l&&!r
121250
MC/DC40%2/5
$tok_eq_l eq $tok_eq_r and $i_eq_l == 0 and $i_eq_r == 0 and substr($pat_eq_l, 0, 1) ne substr($pat_eq_r, 0, 1) || $ci_jump
$tok_eq_l eq $tok_eq_r $i_eq_l == 0 $i_eq_r == 0 substr($pat_eq_l, 0, 1) ne substr($pat_eq_r, 0, 1) $ci_jump
Truth table50%3/6
A and B and C and D or E
A= $tok_eq_l eq $tok_eq_rB= $i_eq_l == 0C= $i_eq_r == 0D= substr($pat_eq_l, 0, 1) ne substr($pat_eq_r, 0, 1)E= $ci_jump
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
3170 $tok_eq_l eq $tok_eq_r
3171 && $i_eq_l == 0
3172 && $i_eq_r == 0
3173 && ( substr( $pat_eq_l, 0, 1 ) ne substr( $pat_eq_r, 0, 1 )
3174 || $ci_jump )
3175 )
3176 {
3177 13 $rnew_lines->[$jl]->{'end_group'} = 1;
3178 }
3179 }
3180 }
3181 680 return ( $rline_hashes, \@equals_info, $saw_side_comment, $max_lev_diff );
3182 } ## end sub make_alignment_info
3183  
3184 sub delete_unmatched_tokens_main_loop {
3185  
3186 680 ▶ my ( $group_level, $rnew_lines, $rsubgroups, $rline_hashes, $requals_info )
Subroutine delete_unmatched_tokens_main_loop: called CC=51 SCAR=39.3
Pod undocumented
3187 = @_;
3188  
3189 #--------------------------------------------------------------
3190 # Main loop over subgroups to remove unmatched alignment tokens
3191 #--------------------------------------------------------------
3192  
3193 # flag to allow skipping pass 2 - not currently used
3194 680 my $saw_large_group;
3195  
3196 680 my $has_terminal_match = $rnew_lines->[-1]->{'j_terminal_match'};
3197  
3198 680 foreach my $item ( @{$rsubgroups} ) {
3199 759 my ( $jbeg, $jend ) = @{$item};
3200  
3201 759 my $nlines = $jend - $jbeg + 1;
3202  
3203 #---------------------------------------------------
3204 # Look for complete if/elsif/else and ternary blocks
3205 #---------------------------------------------------
3206  
3207 # We are looking for a common '$dividing_token' like these:
3208  
3209 # if ( $b and $s ) { $p->{'type'} = 'a'; }
3210 # elsif ($b) { $p->{'type'} = 'b'; }
3211 # elsif ($s) { $p->{'type'} = 's'; }
3212 # else { $p->{'type'} = ''; }
3213 # ^----------- dividing_token
3214  
3215 # my $severity =
3216 # !$routine ? '[PFX]'
3217 # : $routine =~ /warn.*_d\z/ ? '[DS]'
3218 # : $routine =~ /ck_warn/ ? 'W'
3219 # : $routine =~ /ckWARN\d*reg_d/ ? 'S'
3220 # : $routine =~ /ckWARN\d*reg/ ? 'W'
3221 # : $routine =~ /vWARN\d/ ? '[WDS]'
3222 # : '[PFX]';
3223 # ^----------- dividing_token
3224  
3225 # Only look for groups which are more than 2 lines long. Two lines
3226 # can get messed up doing this, probably due to the various
3227 # two-line rules.
3228  
3229 759 my $dividing_token;
3230 my %token_line_count;
3231 759 ▶ if ( $nlines > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nlines > 2) 358 401 759
3232  
3233 358 foreach my $jj ( $jbeg .. $jend ) {
3234 1518 my %seen;
3235 1518 my $line = $rnew_lines->[$jj];
3236 1518 my $rtokens = $line->{'rtokens'};
3237 1518 foreach my $tok ( @{$rtokens} ) {
3238 4299 ▶ if ( !$seen{$tok} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$tok}) 3624 675 4299
3239 3624 $seen{$tok}++;
3240 3624 $token_line_count{$tok}++;
3241 }
3242 }
3243 }
3244  
3245 358 foreach my $tok ( keys %token_line_count ) {
3246 1105 ▶ if ( $token_line_count{$tok} == $nlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_line_count{$tok} == $nlines) 663 442 1105
3247 663 ▶ if ( substr( $tok, 0, 1 ) eq '?'
Branch: 1 branch
BranchTrueFalseTotal
if (substr($tok, 0, 1) eq "?" or substr($tok, 0, 1) eq "{" and $tok =~ /^\{\d+if/) 23 640 663
Condition100%6/6
substr($tok, 0, 1) eq "?" or substr($tok, 0, 1) eq "{" and $tok =~ /^\{\d+if/
l!l&&r!l&&!r
1738608
substr($tok, 0, 1) eq "{" and $tok =~ /^\{\d+if/
!ll&&!rl&&r
626146
MC/DC66%2/3
substr($tok, 0, 1) eq "?" or substr($tok, 0, 1) eq "{" and $tok =~ /^\{\d+if/
substr($tok, 0, 1) eq "?" substr($tok, 0, 1) eq "{" $tok =~ /^\{\d+if/
Truth table75%3/4
A or B and C
A= substr($tok, 0, 1) eq "?"B= substr($tok, 0, 1) eq "{"C= $tok =~ /^\{\d+if/
ABCresult
1XX1
0111
00X0
0100
3248 || substr( $tok, 0, 1 ) eq '{' && $tok =~ /^\{\d+if/ )
3249 {
3250 23 $dividing_token = $tok;
3251 23 last;
3252 }
3253 }
3254 }
3255 }
3256  
3257 #-------------------------------------------------------------
3258 # Loop over subgroup lines to remove unwanted alignment tokens
3259 #-------------------------------------------------------------
3260 759 foreach my $jj ( $jbeg .. $jend ) {
3261 2219 my $line = $rnew_lines->[$jj];
3262 2219 my $rtokens = $line->{'rtokens'};
3263 2219 my $rhash = $rline_hashes->[$jj];
3264 2219 my $i_eq = $requals_info->[$jj]->[0];
3265 2219 my @idel;
3266 2219 my $imax = @{$rtokens} - 2;
3267 2219 my $delete_above_level;
3268 my $deleted_assignment_token;
3269  
3270 2219 my $saw_dividing_token = EMPTY_STRING;
3271 2219 ▶ $saw_large_group ||= $nlines > 2 && $imax > 1;
Condition100%6/6
$saw_large_group ||= $nlines > 2 && $imax > 1
l!l&&r!l&&!r
4301301659
$nlines > 2 && $imax > 1
!ll&&!rl&&r
693966130
MC/DC0%0/3
$saw_large_group ||= $nlines > 2 && $imax > 1
$saw_large_group $nlines > 2 $imax > 1
Truth table0%0/4
A ||= B && C
A= $saw_large_groupB= $nlines > 2C= $imax > 1
ABCresult
1XX1
0111
00X0
0100
3272  
3273 # Loop over all alignment tokens
3274 2219 foreach my $i ( 0 .. $imax ) {
3275 3890 my $tok = $rtokens->[$i];
3276 3890 ▶ next if ( $tok eq '#' ); # shouldn't happen
Branch: 1 branch
BranchTrueFalseTotal
if $tok eq "#" 0 3890 3890
3277 my ( $iii_uu, $il, $ir, $raw_tok, $lev, $tag_uu, $tok_count ) =
3278 3890 @{ $rhash->{$tok} };
3279  
3280 #------------------------------------------------------
3281 # Here is the basic RULE: remove an unmatched alignment
3282 # which does not occur in the surrounding lines.
3283 #------------------------------------------------------
3284 3890 ▶ my $delete_me = !defined($il) && !defined($ir);
Condition100%3/3
defined $il || defined $ir
l!l&&r!l&&!r
2111937842
MC/DC100%2/2
defined $il || defined $ir
defined $il defined $ir
Truth table100%3/3
A || B
A= defined $ilB= defined $ir
ABresult
1X1
011
000
3285  
3286 # Apply any user controls. Note that not all lines pass
3287 # this way so they have to be applied elsewhere too.
3288 3890 my $align_ok = 1;
3289 3890 ▶ if (%valign_control_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (%valign_control_hash) 49 3841 3890
3290 49 $align_ok = $valign_control_hash{$raw_tok};
3291 49 ▶ $align_ok = $valign_control_default
Branch: 1 branch
BranchTrueFalseTotal
unless defined $align_ok 32 17 49
3292 unless ( defined($align_ok) );
3293 49 ▶ $delete_me ||= !$align_ok;
Condition100%3/3
$delete_me ||= !$align_ok
l!l&&r!l&&!r
12028
MC/DC100%2/2
$delete_me ||= !$align_ok
$delete_me !$align_ok
Truth table100%3/3
A ||= not(B)
A= $delete_meB= !$align_ok
ABresult
1X1
011
000
3294 }
3295  
3296 # But now we modify this with exceptions...
3297  
3298 # EXCEPTION 1: If we are in a complete ternary or
3299 # if/elsif/else group, and this token is not on every line
3300 # of the group, should we delete it to preserve overall
3301 # alignment?
3302 3890 ▶ if ($dividing_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dividing_token) 163 3727 3890
3303 163 ▶ if ( $token_line_count{$tok} >= $nlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_line_count{$tok} >= $nlines) { } 132 31 163
3304 132 ▶ $saw_dividing_token ||= $tok eq $dividing_token;
Condition100%3/3
$saw_dividing_token ||= $tok eq $dividing_token
l!l&&r!l&&!r
159324
MC/DC100%2/2
$saw_dividing_token ||= $tok eq $dividing_token
$saw_dividing_token $tok eq $dividing_token
Truth table100%3/3
A ||= B
A= $saw_dividing_tokenB= $tok eq $dividing_token
ABresult
1X1
011
000
3305 }
3306 else {
3307  
3308 # For shorter runs, delete toks to save alignment.
3309 # For longer runs, keep toks after the '{' or '?'
3310 # to allow sub-alignments within braces. The
3311 # number 5 lines is arbitrary but seems to work ok.
3312 31 ▶ $delete_me ||= ( $nlines < 5 || !$saw_dividing_token );
Condition83%5/6
$delete_me ||= $nlines < 5 || !$saw_dividing_token
l!l&&r!l&&!r
2137
$nlines < 5 || !$saw_dividing_token
l!l&&r!l&&!r
307
MC/DC0%0/3
$delete_me ||= $nlines < 5 || !$saw_dividing_token
$delete_me $nlines < 5 !$saw_dividing_token
Truth table0%0/4
A ||= B || not(C)
A= $delete_meB= $nlines < 5C= !$saw_dividing_token
ABCresult
1XX1
01X1
0011
0000
3313 }
3314 }
3315  
3316 # EXCEPTION 2: Remove all tokens above a certain level
3317 # following a previous deletion. For example, we have to
3318 # remove tagged higher level alignment tokens following a
3319 # '=>' deletion because the tags of higher level tokens
3320 # will now be incorrect. For example, this will prevent
3321 # aligning commas as follows after deleting the second '=>'
3322 # $w->insert(
3323 # ListBox => origin => [ 270, 160 ],
3324 # size => [ 200, 55 ],
3325 # );
3326 3890 ▶ if ( defined($delete_above_level) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $delete_above_level) 334 3556 3890
3327 334 ▶ if ( $lev > $delete_above_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lev > $delete_above_level) { } 160 174 334
3328 160 ▶ $delete_me ||= 1;
Condition100%2/2
$delete_me ||= 1
l!l
15010
MC/DC100%1/1
$delete_me ||= 1
$delete_me
Truth table100%2/2
A
A= $delete_me
Aresult
11
00
3329 }
3330 else {
3331 174 $delete_above_level = undef;
3332 }
3333 }
3334  
3335 # EXCEPTION 3: Remove all but certain tokens after an
3336 # assignment deletion.
3337 3890 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($deleted_assignment_token and $lev > $group_level || !$is_if_or{$raw_tok}) 62 3828 3890
Condition100%6/6
$deleted_assignment_token and $lev > $group_level || !$is_if_or{$raw_tok}
!ll&&!rl&&r
38113742
$lev > $group_level or not $is_if_or{$raw_tok}
l!l&&r!l&&!r
243817
MC/DC66%2/3
$deleted_assignment_token and $lev > $group_level || !$is_if_or{$raw_tok}
$deleted_assignment_token $lev > $group_level not $is_if_or{$raw_tok}
Truth table75%3/4
A and B or not(C)
A= $deleted_assignment_tokenB= $lev > $group_levelC= not $is_if_or{$raw_tok}
ABCresult
0XX0
1000
11X1
1011
3338 $deleted_assignment_token
3339 && ( $lev > $group_level
3340 || !$is_if_or{$raw_tok} )
3341 )
3342 {
3343 62 ▶ $delete_me ||= 1;
Condition100%2/2
$delete_me ||= 1
l!l
4715
MC/DC100%1/1
$delete_me ||= 1
$delete_me
Truth table100%2/2
A
A= $delete_me
Aresult
11
00
3344 }
3345  
3346 # EXCEPTION 4: Do not touch the first line of a 2 line
3347 # terminal match, such as below, because j_terminal has
3348 # already been set.
3349 # if ($tag) { $tago = "<$tag>"; $tagc = "</$tag>"; }
3350 # else { $tago = $tagc = ''; }
3351 # But see snippets 'else1.t' and 'else2.t'
3352 3890 ▶ $delete_me = 0
Branch: 1 branch
BranchTrueFalseTotal
if $jj == $jbeg and $has_terminal_match and $nlines == 2 18 3872 3890
Condition100%6/6
$jj == $jbeg and $has_terminal_match and $nlines == 2
!ll&&!rl&&r
38601218
$jj == $jbeg and $has_terminal_match
!ll&&!rl&&r
2578128230
MC/DC100%3/3
$jj == $jbeg and $has_terminal_match and $nlines == 2
$jj == $jbeg $has_terminal_match $nlines == 2
Truth table100%4/4
A and B and C
A= $jj == $jbegB= $has_terminal_matchC= $nlines == 2
ABCresult
0XX0
10X0
1100
1111
3353 if ( $jj == $jbeg
3354 && $has_terminal_match
3355 && $nlines == 2 );
3356  
3357 # EXCEPTION 5: misc additional rules for commas and equals
3358 3890 ▶ if ( $delete_me && $tok_count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_me and $tok_count == 1) 843 3047 3890
Condition100%3/3
$delete_me and $tok_count == 1
!ll&&!rl&&r
300146843
MC/DC100%2/2
$delete_me and $tok_count == 1
$delete_me $tok_count == 1
Truth table100%3/3
A and B
A= $delete_meB= $tok_count == 1
ABresult
0X0
100
111
3359  
3360 # okay to delete second and higher copies of a token
3361  
3362 # for a comma...
3363 843 ▶ if ( $raw_tok eq COMMA ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok eq ",") 306 537 843
3364  
3365 # Do not delete commas before an equals
3366 306 ▶ $delete_me = 0
Branch: 1 branch
BranchTrueFalseTotal
if defined $i_eq and $i < $i_eq 15 291 306
Condition100%3/3
defined $i_eq and $i < $i_eq
!ll&&!rl&&r
2167515
MC/DC100%2/2
defined $i_eq and $i < $i_eq
defined $i_eq $i < $i_eq
Truth table100%3/3
A and B
A= defined $i_eqB= $i < $i_eq
ABresult
0X0
100
111
3367 if ( defined($i_eq) && $i < $i_eq );
3368  
3369 # Do not delete line-level commas
3370 306 ▶ $delete_me = 0 if ( $lev <= $group_level );
Branch: 1 branch
BranchTrueFalseTotal
if $lev <= $group_level 81 225 306
3371 }
3372  
3373 # For an assignment at group level..
3374 843 ▶ if ( $is_assignment{$raw_tok}
Branch: 1 branch
BranchTrueFalseTotal
if ($is_assignment{$raw_tok} and $lev == $group_level) 137 706 843
Condition100%3/3
$is_assignment{$raw_tok} and $lev == $group_level
!ll&&!rl&&r
67036137
MC/DC100%2/2
$is_assignment{$raw_tok} and $lev == $group_level
$is_assignment{$raw_tok} $lev == $group_level
Truth table100%3/3
A and B
A= $is_assignment{$raw_tok}B= $lev == $group_level
ABresult
0X0
100
111
3375 && $lev == $group_level )
3376 {
3377  
3378 # Do not delete if it is the last alignment of
3379 # multiple tokens; this will prevent some
3380 # undesirable alignments
3381 137 ▶ if ( $imax > 0 && $i == $imax ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imax > 0 and $i == $imax) { } 13 124 137
Condition100%3/3
$imax > 0 and $i == $imax
!ll&&!rl&&r
804413
MC/DC100%2/2
$imax > 0 and $i == $imax
$imax > 0 $i == $imax
Truth table100%3/3
A and B
A= $imax > 0B= $i == $imax
ABresult
0X0
100
111
3382 13 $delete_me = 0;
3383 }
3384  
3385 # Otherwise, set a flag to delete most
3386 # remaining tokens
3387 124 else { $deleted_assignment_token = $raw_tok }
3388 }
3389 }
3390  
3391 # Do not let a user exclusion be reactivated by above rules
3392 3890 ▶ $delete_me ||= !$align_ok;
Condition66%2/3
$delete_me ||= !$align_ok
l!l&&r!l&&!r
78003110
MC/DC50%1/2
$delete_me ||= !$align_ok
$delete_me !$align_ok
Truth table66%2/3
A ||= not(B)
A= $delete_meB= !$align_ok
ABresult
1X1
011
000
3393  
3394 #------------------------------------
3395 # Add this token to the deletion list
3396 #------------------------------------
3397 3890 ▶ if ($delete_me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_me) 780 3110 3890
3398 780 push @idel, $i;
3399  
3400 # update deletion propagation flags
3401 780 ▶ if ( !defined($delete_above_level)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $delete_above_level or $lev < $delete_above_level) 620 160 780
Condition66%2/3
not defined $delete_above_level or $lev < $delete_above_level
l!l&&r!l&&!r
6200160
MC/DC50%1/2
not defined $delete_above_level or $lev < $delete_above_level
not defined $delete_above_level $lev < $delete_above_level
Truth table66%2/3
not(A) or B
A= not defined $delete_above_levelB= $lev < $delete_above_level
ABresult
1X1
011
000
3402 || $lev < $delete_above_level )
3403 {
3404  
3405 # delete all following higher level alignments
3406 620 $delete_above_level = $lev;
3407  
3408 # but keep deleting after => to next lower level
3409 # to avoid some bizarre alignments
3410 620 ▶ if ( $raw_tok eq '=>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok eq "=>") 56 564 620
3411 56 $delete_above_level = $lev - 1;
3412 }
3413 }
3414 }
3415 } ## End loop over alignment tokens
3416  
3417 # Process all deletion requests for this line
3418 2219 ▶ if (@idel) {
Branch: 1 branch
BranchTrueFalseTotal
if (@idel) 485 1734 2219
3419 485 delete_selected_tokens( $line, \@idel );
3420 }
3421 } ## End loop over lines
3422 } ## End main loop over subgroups
3423  
3424 680 return;
3425 } ## end sub delete_unmatched_tokens_main_loop
3426  
3427 sub match_line_pairs {
3428  
3429 680 ▶ my ( $rlines, $rnew_lines, $rsubgroups, $group_level ) = @_;
Subroutine match_line_pairs: called CC=27 SCAR=33.0
Pod undocumented
3430  
3431 # Compare each pair of lines and save information about common matches
3432  
3433 # Given:
3434 # $rlines = list of lines including hanging side comments
3435 # $rnew_lines = list of lines without any hanging side comments
3436 # $rsubgroups = list of subgroups of the new lines
3437 # Return:
3438 # $saw_signed_number = true if a field has a signed number
3439 # (needed for --valign-signed-numbers)
3440  
3441 # NOTE: A possible future generalization would be to change
3442 # imax_pair => $imax_align into a ref with additional information:
3443 # imax_pair => [$imax_align, $rMsg, ... ]
3444 # This could eventually hold multi-level match info
3445  
3446 # Previous line vars
3447 680 my ( $line_m, $rtokens_m, $rpatterns_m, $rfield_lengths_m, $imax_m,
3448 $list_type_m, $ci_level_m );
3449  
3450 # Current line vars
3451 680 my ( $line, $rtokens, $rpatterns, $rfield_lengths, $imax, $list_type,
3452 $ci_level );
3453  
3454 # Return parameter to avoid calls to sub pad_signed_number_columns
3455 680 my $saw_signed_number;
3456  
3457 # loop over subgroups
3458 680 foreach my $item ( @{$rsubgroups} ) {
3459 759 my ( $jbeg, $jend ) = @{$item};
3460 759 my $nlines = $jend - $jbeg + 1;
3461 759 ▶ next if ( $nlines <= 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $nlines <= 1 101 658 759
3462  
3463 # loop over lines in a subgroup
3464 658 foreach my $jj ( $jbeg .. $jend ) {
3465  
3466 2118 $line_m = $line;
3467 2118 $rtokens_m = $rtokens;
3468 2118 $rpatterns_m = $rpatterns;
3469 2118 $rfield_lengths_m = $rfield_lengths;
3470 2118 $imax_m = $imax;
3471 2118 $list_type_m = $list_type;
3472 2118 $ci_level_m = $ci_level;
3473  
3474 2118 $line = $rnew_lines->[$jj];
3475 2118 $rtokens = $line->{'rtokens'};
3476 2118 $rpatterns = $line->{'rpatterns'};
3477 2118 $rfield_lengths = $line->{'rfield_lengths'};
3478 2118 $imax = @{$rtokens} - 2;
3479 2118 $list_type = $line->{'list_type'};
3480 2118 $ci_level = $line->{'ci_level'};
3481  
3482 # Quick approximate check for signed numbers in this line.
3483 # This speeds up large runs by about 0.5%
3484 2118 ▶ if ( !$saw_signed_number ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_signed_number) 1945 173 2118
3485  
3486 1945 my $rfields = $line->{'rfields'};
3487 1945 foreach my $i ( 0 .. $imax + 1 ) {
3488 4641 ▶ next if ( index( $rpatterns->[$i], 'n' ) < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if (index($rpatterns->[$i], "n") == -1) 3685 956 4641
3489 956 my $field = $rfields->[$i];
3490 956 ▶ if ( index( $field, '-' ) >= 0
Branch: 1 branch
BranchTrueFalseTotal
if (index($field, "-") != -1 or index($field, "+") != -1) 72 884 956
Condition100%3/3
index($field, "-") != -1 or index($field, "+") != -1
l!l&&r!l&&!r
5220884
MC/DC100%2/2
index($field, "-") != -1 or index($field, "+") != -1
index($field, "-") != -1 index($field, "+") != -1
Truth table100%3/3
A or B
A= index($field, "-") != -1B= index($field, "+") != -1
ABresult
1X1
011
000
3491 || index( $field, '+' ) >= 0 )
3492 {
3493 72 $saw_signed_number = 1;
3494 72 last;
3495 }
3496 }
3497 }
3498  
3499 # nothing to do for first line
3500 2118 ▶ next if ( $jj == $jbeg );
Branch: 1 branch
BranchTrueFalseTotal
if $jj == $jbeg 658 1460 2118
3501  
3502 1460 my $ci_jump = $ci_level - $ci_level_m;
3503  
3504 1460 ▶ my $imax_min = $imax_m < $imax ? $imax_m : $imax;
Branch: 1 branch
BranchTrueFalseTotal
$imax_m < $imax ? : 103 1357 1460
3505  
3506 1460 my $imax_align = -1;
3507  
3508 # find number of leading common tokens
3509  
3510 #---------------------------------
3511 # No match to hanging side comment
3512 #---------------------------------
3513 1460 ▶ if ( $line->{'is_hanging_side_comment'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line->{'is_hanging_side_comment'}) { } 0 1460 1460
elsif ($list_type and $list_type eq $list_type_m) { } 526 934 1460
3514  
3515 # Should not get here; HSC's have been filtered out
3516 0 $imax_align = -1;
3517 }
3518  
3519 #-----------------------------
3520 # Handle comma-separated lists
3521 #-----------------------------
3522 ▶ elsif ( $list_type && $list_type eq $list_type_m ) {
Condition100%3/3
$list_type and $list_type eq $list_type_m
!ll&&!rl&&r
91024526
MC/DC100%2/2
$list_type and $list_type eq $list_type_m
$list_type $list_type eq $list_type_m
Truth table100%3/3
A and B
A= $list_typeB= $list_type eq $list_type_m
ABresult
0X0
100
111
3523  
3524 # do not align lists across a ci jump with new list method
3525 526 ▶ if ($ci_jump) { $imax_min = -1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ci_jump) 0 526 526
3526  
3527 526 my $i_nomatch = $imax_min + 1;
3528 526 foreach my $i ( 0 .. $imax_min ) {
3529 1049 my $tok = $rtokens->[$i];
3530 1049 my $tok_m = $rtokens_m->[$i];
3531 1049 ▶ if ( $tok ne $tok_m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok ne $tok_m) 0 1049 1049
3532 0 $i_nomatch = $i;
3533 0 last;
3534 }
3535 }
3536  
3537 526 $imax_align = $i_nomatch - 1;
3538 }
3539  
3540 #-----------------
3541 # Handle non-lists
3542 #-----------------
3543 else {
3544 934 my $i_nomatch = $imax_min + 1;
3545 934 foreach my $i ( 0 .. $imax_min ) {
3546 923 my $tok = $rtokens->[$i];
3547 923 my $tok_m = $rtokens_m->[$i];
3548 923 ▶ if ( $tok ne $tok_m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok ne $tok_m) 32 891 923
3549 32 $i_nomatch = $i;
3550 32 last;
3551 }
3552  
3553 891 my $pat = $rpatterns->[$i];
3554 891 my $pat_m = $rpatterns_m->[$i];
3555  
3556 # VSN PATCH: allow numbers to match quotes
3557 891 ▶ if ( $pat_m ne $pat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_m ne $pat) 200 691 891
3558 200 $pat =~ tr/n/Q/;
3559 200 $pat_m =~ tr/n/Q/;
3560 }
3561  
3562 # If patterns don't match, we have to be careful...
3563 891 ▶ if ( $pat_m ne $pat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_m ne $pat) 188 703 891
3564 188 my $pad =
3565 $rfield_lengths->[$i] - $rfield_lengths_m->[$i];
3566 188 my $match_code = compare_patterns(
3567 {
3568 group_level => $group_level,
3569 tok => $tok,
3570 pat => $pat,
3571 pat_m => $pat_m,
3572 pad => $pad,
3573 ## tok_m => $tok_m,
3574 }
3575 );
3576 188 ▶ if ($match_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_code) 7 181 188
3577 7 ▶ if ( $match_code == 1 ) { $i_nomatch = $i }
Branch: 2 branches
BranchTrueFalseTotal
if ($match_code == 1) { } 6 1 7
elsif ($match_code == 2) { } 1 0 1
3578 1 elsif ( $match_code == 2 ) { $i_nomatch = 0 }
3579 else { } ##ok
3580 7 last;
3581 }
3582 }
3583 }
3584 934 $imax_align = $i_nomatch - 1;
3585 }
3586  
3587 1460 $line_m->{'imax_pair'} = $imax_align;
3588  
3589 } ## end loop over lines
3590  
3591 # Put fence at end of subgroup
3592 658 $line->{'imax_pair'} = -1;
3593  
3594 } ## end loop over subgroups
3595  
3596 # if there are hanging side comments, propagate the pair info down to them
3597 # so that lines can just look back one line for their pair info.
3598 680 ▶ if ( @{$rlines} > @{$rnew_lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rlines;} > @{$rnew_lines;}) 26 654 680
3599 26 my $last_pair_info = -1;
3600 26 foreach my $line_t ( @{$rlines} ) {
3601 99 ▶ if ( $line_t->{'is_hanging_side_comment'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_t->{'is_hanging_side_comment'}) { } 41 58 99
3602 41 $line_t->{'imax_pair'} = $last_pair_info;
3603 }
3604 else {
3605 58 $last_pair_info = $line_t->{'imax_pair'};
3606 }
3607 }
3608 }
3609 680 return $saw_signed_number;
3610 } ## end sub match_line_pairs
3611  
3612 sub compare_patterns {
3613  
3614 188 ▶ my ($rcall_hash) = @_;
Subroutine compare_patterns: called CC=9 SCAR=22.1
Pod undocumented
3615  
3616 # This is a helper routine for sub match_line_pairs to decide if patterns
3617 # in two lines match well enough
3618 # Given these values in $rcall_hash:
3619 # $tok_m, $pat_m = token and pattern of first line
3620 # $tok, $pat = token and pattern of second line
3621 # $pad = 0 if no padding is needed, !=0 otherwise
3622 # Return code:
3623 # 0 = patterns match, continue
3624 # 1 = no match
3625 # 2 = no match, and lines do not match at all
3626  
3627 188 my $group_level = $rcall_hash->{group_level};
3628 188 my $tok = $rcall_hash->{tok};
3629 ## my $tok_m = $rcall_hash->{tok_m};
3630 188 my $pat = $rcall_hash->{pat};
3631 188 my $pat_m = $rcall_hash->{pat_m};
3632 188 my $pad = $rcall_hash->{pad};
3633  
3634 188 my $GoToMsg = EMPTY_STRING;
3635 188 my $return_code = 0;
3636  
3637 45 ▶ use constant EXPLAIN_COMPARE_PATTERNS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3638  
3639 188 my ( $alignment_token, $lev, $tag_uu, $tok_count_uu ) =
3640 decode_alignment_token($tok);
3641  
3642 # We have to be very careful about aligning commas
3643 # when the pattern's don't match, because it can be
3644 # worse to create an alignment where none is needed
3645 # than to omit one. Here's an example where the ','s
3646 # are not in named containers. The first line below
3647 # should not match the next two:
3648 # ( $a, $b ) = ( $b, $r );
3649 # ( $x1, $x2 ) = ( $x2 - $q * $x1, $x1 );
3650 # ( $y1, $y2 ) = ( $y2 - $q * $y1, $y1 );
3651 188 ▶ if ( $alignment_token eq COMMA ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($alignment_token eq ',') { } 26 162 188
elsif ($alignment_token eq '(') { } 3 159 162
elsif ($alignment_token eq '=') { } 20 139 159
3652  
3653 # do not align commas unless they are in named
3654 # containers
3655 26 ▶ if ( $tok !~ /[A-Za-z]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tok =~ /[A-Za-z]/) { } 3 23 26
3656 3 $return_code = 1;
3657 3 $GoToMsg = "do not align commas in unnamed containers";
3658 }
3659 else {
3660 23 $return_code = 0;
3661 }
3662 }
3663  
3664 # do not align parens unless patterns match;
3665 # large ugly spaces can occur in math expressions.
3666 elsif ( $alignment_token eq '(' ) {
3667  
3668 # But we can allow a match if the parens don't
3669 # require any padding.
3670 3 ▶ if ( $pad != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad != 0) { } 3 0 3
3671 3 $return_code = 1;
3672 3 $GoToMsg = "do not align '(' unless patterns match or pad=0";
3673 }
3674 else {
3675 0 $return_code = 0;
3676 }
3677 }
3678  
3679 # Handle an '=' alignment with different patterns to
3680 # the left.
3681 elsif ( $alignment_token eq '=' ) {
3682  
3683 # It is best to be a little restrictive when
3684 # aligning '=' tokens. Here is an example of
3685 # two lines that we will not align:
3686 # my $variable=6;
3687 # $bb=4;
3688 # The problem is that one is a 'my' declaration,
3689 # and the other isn't, so they're not very similar.
3690 # We will filter these out by comparing the first
3691 # letter of the pattern. This is crude, but works
3692 # well enough.
3693 20 ▶ if ( substr( $pat_m, 0, 1 ) ne substr( $pat, 0, 1 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($pat_m, 0, 1) ne substr($pat, 0, 1)) { } 0 20 20
elsif ((index($pat_m, ',') != -1) ne (index($pat, ',') != -1)) { } 1 19 20
3694 0 $GoToMsg = "first character before equals differ";
3695 0 $return_code = 1;
3696 }
3697  
3698 # The introduction of sub 'prune_alignment_tree'
3699 # enabled alignment of lists left of the equals with
3700 # other scalar variables. For example:
3701 # my ( $D, $s, $e ) = @_;
3702 # my $d = length $D;
3703 # my $c = $e - $s - $d;
3704  
3705 # But this would change formatting of a lot of scripts,
3706 # so for now we prevent alignment of comma lists on the
3707 # left with scalars on the left. We will also prevent
3708 # any partial alignments.
3709  
3710 # set return code 2 if the = is at line level, but
3711 # set return code 1 if the = is below line level, i.e.
3712 # sub new { my ( $p, $v ) = @_; bless \$v, $p }
3713 # sub iter { my ($x) = @_; return undef if $$x < 0; return $$x--; }
3714  
3715 elsif (
3716 ( index( $pat_m, COMMA ) >= 0 ) ne ( index( $pat, COMMA ) >= 0 ) )
3717 {
3718 1 $GoToMsg = "mixed commas/no-commas before equals";
3719 1 $return_code = 1;
3720 1 ▶ if ( $lev eq $group_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lev eq $group_level) 1 0 1
3721 1 $return_code = 2;
3722 }
3723 }
3724 else {
3725 19 $return_code = 0;
3726 }
3727 }
3728 else {
3729 139 $return_code = 0;
3730 }
3731  
3732 EXPLAIN_COMPARE_PATTERNS
3733 && $return_code
3734 188 && print {*STDOUT} "no match because $GoToMsg\n";
3735  
3736 188 return $return_code;
3737  
3738 } ## end sub compare_patterns
3739  
3740 sub fat_comma_to_comma {
3741  
3742 845 ▶ my ($str) = @_;
Subroutine fat_comma_to_comma: called CC=2 SCAR=6.9
Pod undocumented
3743  
3744 # Given:
3745 # $str = a decorated fat comma alignment token
3746  
3747 # Change '=>' to ','
3748 # and remove any trailing decimal count because currently fat commas have a
3749 # count and commas do not.
3750  
3751 # For example, change '=>2+{-3.2' into ',2+{-3'
3752 845 ▶ if ( $str =~ /^=>([^\.]*)/ ) { $str = COMMA . $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^=>([^\.]*)/) 181 664 845
3753 845 return $str;
3754 } ## end sub fat_comma_to_comma
3755  
3756 sub get_line_token_info {
3757  
3758 174 ▶ my ($rlines) = @_;
Subroutine get_line_token_info: called CC=27 SCAR=33.0
Pod undocumented
3759  
3760 # Given:
3761 # $rlines = ref to array of lines in this group
3762  
3763 # Scan lines of tokens and return summary information about the range of
3764 # levels and patterns.
3765  
3766 # First scan to check monotonicity. Here is an example of several
3767 # lines which are monotonic. The = is the lowest level, and
3768 # the commas are all one level deeper. So this is not nonmonotonic.
3769 # $$d{"weeks"} = [ "w", "wk", "wks", "week", "weeks" ];
3770 # $$d{"days"} = [ "d", "day", "days" ];
3771 # $$d{"hours"} = [ "h", "hr", "hrs", "hour", "hours" ];
3772 174 my @all_token_info;
3773 174 my $all_monotonic = 1;
3774 174 foreach my $jj ( 0 .. @{$rlines} - 1 ) {
3775 703 my ($line) = $rlines->[$jj];
3776 703 my $rtokens = $line->{'rtokens'};
3777 703 my $last_lev;
3778 703 my $is_monotonic = 1;
3779 703 my $i = -1;
3780 703 foreach my $tok ( @{$rtokens} ) {
3781 1838 $i++;
3782 1838 my ( $raw_tok, $lev, $tag, $tok_count ) =
3783 decode_alignment_token($tok);
3784 1838 push @{ $all_token_info[$jj] },
3785 [ $raw_tok, $lev, $tag, $tok_count ];
3786 1838 ▶ last if ( $tok eq '#' );
Branch: 1 branch
BranchTrueFalseTotal
if $tok eq "#" 703 1135 1838
3787 1135 ▶ if ( $i > 0 && $lev < $last_lev ) { $is_monotonic = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 and $lev < $last_lev) 90 1045 1135
Condition100%3/3
$i > 0 and $lev < $last_lev
!ll&&!rl&&r
59145490
MC/DC100%2/2
$i > 0 and $lev < $last_lev
$i > 0 $lev < $last_lev
Truth table100%3/3
A and B
A= $i > 0B= $lev < $last_lev
ABresult
0X0
100
111
3788 1135 $last_lev = $lev;
3789 }
3790 703 ▶ if ( !$is_monotonic ) { $all_monotonic = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_monotonic) 87 616 703
3791 }
3792  
3793 174 my $rline_values = [];
3794 174 foreach my $jj ( 0 .. @{$rlines} - 1 ) {
3795 703 my ($line) = $rlines->[$jj];
3796  
3797 703 my $rtokens = $line->{'rtokens'};
3798 703 my $i = -1;
3799 703 my ( $lev_min, $lev_max );
3800 703 my $token_pattern_max = EMPTY_STRING;
3801 703 my %saw_level;
3802 703 my $is_monotonic = 1;
3803  
3804 # find the index of the last token before the side comment
3805 703 my $imax = @{$rtokens} - 2;
3806 703 my $imax_true = $imax;
3807  
3808 # If the entire group is monotonic, and the line ends in a comma list,
3809 # walk it back to the first such comma. this will have the effect of
3810 # making all trailing ragged comma lists match in the prune tree
3811 # routine. these trailing comma lists can better be handled by later
3812 # alignment rules.
3813  
3814 # Treat fat commas the same as commas here by converting them to
3815 # commas. This will improve the chance of aligning the leading parts
3816 # of ragged lists.
3817  
3818 703 my $tok_end = fat_comma_to_comma( $rtokens->[$imax] );
3819 703 ▶ if ( $all_monotonic && $tok_end =~ /^,/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_monotonic and $tok_end =~ /^,/) 146 557 703
Condition100%3/3
$all_monotonic and $tok_end =~ /^,/
!ll&&!rl&&r
246311146
MC/DC100%2/2
$all_monotonic and $tok_end =~ /^,/
$all_monotonic $tok_end =~ /^,/
Truth table100%3/3
A and B
A= $all_monotonicB= $tok_end =~ /^,/
ABresult
0X0
100
111
3820 146 my $ii = $imax - 1;
3821 146 ▶ while ( $ii >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($ii >= 0 and fat_comma_to_comma($rtokens->[$ii]) eq $tok_end) 93 146 239
Condition100%3/3
$ii >= 0 and fat_comma_to_comma($rtokens->[$ii]) eq $tok_end
!ll&&!rl&&r
974993
MC/DC100%2/2
$ii >= 0 and fat_comma_to_comma($rtokens->[$ii]) eq $tok_end
$ii >= 0 fat_comma_to_comma($rtokens->[$ii]) eq $tok_end
Truth table100%3/3
A and B
A= $ii >= 0B= fat_comma_to_comma($rtokens->[$ii]) eq $tok_end
ABresult
0X0
100
111
3822 && fat_comma_to_comma( $rtokens->[$ii] ) eq $tok_end )
3823 {
3824 93 $imax = $ii;
3825 93 $ii--;
3826 } ## end while ( $ii >= 0 && fat_comma_to_comma...)
3827 }
3828  
3829 # make a first pass to find level range
3830 703 my $last_lev;
3831 703 foreach my $tok ( @{$rtokens} ) {
3832 1745 $i++;
3833 1745 ▶ last if ( $i > $imax );
Branch: 1 branch
BranchTrueFalseTotal
if $i > $imax 703 1042 1745
3834 1042 ▶ last if ( $tok eq '#' );
Branch: 1 branch
BranchTrueFalseTotal
if $tok eq "#" 0 1042 1042
3835 my ( $raw_tok_uu, $lev, $tag_uu, $tok_count_uu ) =
3836 1042 @{ $all_token_info[$jj]->[$i] };
3837  
3838 1042 ▶ last if ( $tok eq '#' );
Branch: 1 branch
BranchTrueFalseTotal
if $tok eq "#" 0 1042 1042
3839 1042 $token_pattern_max .= $tok;
3840 1042 $saw_level{$lev}++;
3841 1042 ▶ if ( !defined($lev_min) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lev_min) { } 591 451 1042
3842 591 $lev_min = $lev;
3843 591 $lev_max = $lev;
3844 }
3845 else {
3846 451 ▶ if ( $lev < $lev_min ) { $lev_min = $lev; }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev < $lev_min) 60 391 451
3847 451 ▶ if ( $lev > $lev_max ) { $lev_max = $lev; }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev > $lev_max) 140 311 451
3848 451 ▶ if ( $lev < $last_lev ) { $is_monotonic = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev < $last_lev) 90 361 451
3849 }
3850 1042 $last_lev = $lev;
3851 }
3852  
3853 # handle no levels
3854 703 my $rtoken_patterns = {};
3855 703 my $rtoken_indexes = {};
3856 703 my @levs = sort { $a <=> $b } keys %saw_level;
3857 703 ▶ if ( !defined($lev_min) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $lev_min) { } 112 591 703
elsif ($lev_max == $lev_min) { } 400 191 591
3858 112 $lev_min = -1;
3859 112 $lev_max = -1;
3860 112 $levs[0] = -1;
3861 112 $rtoken_patterns->{$lev_min} = EMPTY_STRING;
3862 112 $rtoken_indexes->{$lev_min} = [];
3863 }
3864  
3865 # handle one level
3866 elsif ( $lev_max == $lev_min ) {
3867 400 $rtoken_patterns->{$lev_max} = $token_pattern_max;
3868 400 $rtoken_indexes->{$lev_max} = [ ( 0 .. $imax ) ];
3869 }
3870  
3871 # handle multiple levels
3872 else {
3873 191 $rtoken_patterns->{$lev_max} = $token_pattern_max;
3874 191 $rtoken_indexes->{$lev_max} = [ ( 0 .. $imax ) ];
3875  
3876 191 my $lev_top = pop @levs; # already did max level
3877 191 my $itok = -1;
3878 191 foreach my $tok ( @{$rtokens} ) {
3879 789 $itok++;
3880 789 ▶ last if ( $itok > $imax );
Branch: 1 branch
BranchTrueFalseTotal
if $itok > $imax 191 598 789
3881 my ( $raw_tok, $lev, $tag_uu, $tok_count_uu ) =
3882 598 @{ $all_token_info[$jj]->[$itok] };
3883 598 ▶ last if ( $raw_tok eq '#' );
Branch: 1 branch
BranchTrueFalseTotal
if $raw_tok eq "#" 0 598 598
3884 598 foreach my $lev_test (@levs) {
3885 638 ▶ next if ( $lev > $lev_test );
Branch: 1 branch
BranchTrueFalseTotal
if $lev > $lev_test 323 315 638
3886 315 $rtoken_patterns->{$lev_test} .= $tok;
3887 315 push @{ $rtoken_indexes->{$lev_test} }, $itok;
3888 }
3889 }
3890 191 push @levs, $lev_top;
3891 }
3892  
3893 703 push @{$rline_values},
3894 [
3895 $lev_min, $lev_max, $rtoken_patterns, \@levs,
3896 $rtoken_indexes, $is_monotonic, $imax_true, $imax,
3897 ];
3898  
3899 # debug
3900 703 0 && do {
3901 local $LIST_SEPARATOR = ')(';
3902 print "lev_min=$lev_min, lev_max=$lev_max, levels=(@levs)\n";
3903 foreach my $key ( sort keys %{$rtoken_patterns} ) {
3904 print "$key => $rtoken_patterns->{$key}\n";
3905 print "$key => @{$rtoken_indexes->{$key}}\n";
3906 }
3907 };
3908 } ## end loop over lines
3909 174 return ( $rline_values, $all_monotonic );
3910 } ## end sub get_line_token_info
3911  
3912 sub prune_alignment_tree {
3913  
3914 174 ▶ my ($rlines) = @_;
Subroutine prune_alignment_tree: called CC=40 SCAR=36.9
Pod undocumented
3915  
3916 # Given:
3917 # $rlines = ref to array of lines in this group
3918  
3919 # Prune the tree of alignments to limit depth of alignments
3920  
3921 174 my $jmax = @{$rlines} - 1;
3922 174 ▶ return if ( $jmax <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $jmax <= 0 0 174 174
3923  
3924 # Vertical alignment in perltidy is done as an iterative process. The
3925 # starting point is to mark all possible alignment tokens ('=', ',', '=>',
3926 # etc) for vertical alignment. Then we have to delete all alignments
3927 # which, if actually made, would detract from overall alignment. This
3928 # is done in several phases of which this is one.
3929  
3930 # In this routine we look at the alignments of a group of lines as a
3931 # hierarchical tree. We will 'prune' the tree to limited depths if that
3932 # will improve overall alignment at the lower depths.
3933 # For each line we will be looking at its alignment patterns down to
3934 # different fixed depths. For each depth, we include all lower depths and
3935 # ignore all higher depths. We want to see if we can get alignment of a
3936 # larger group of lines if we ignore alignments at some lower depth.
3937 # Here is an # example:
3938  
3939 # for (
3940 # [ '$var', sub { join $_, "bar" }, 0, "bar" ],
3941 # [ 'CONSTANT', sub { join "foo", "bar" }, 0, "bar" ],
3942 # [ 'CONSTANT', sub { join "foo", "bar", 3 }, 1, "barfoo3" ],
3943 # [ '$myvar', sub { my $var; join $var, "bar" }, 0, "bar" ],
3944 # );
3945  
3946 # In the above example, all lines have three commas at the lowest depth
3947 # (zero), so if there were no other alignments, these lines would all
3948 # align considering only the zero depth alignment token. But some lines
3949 # have additional comma alignments at the next depth, so we need to decide
3950 # if we should drop those to keep the top level alignments, or keep those
3951 # for some additional low level alignments at the expense losing some top
3952 # level alignments. In this case we will drop the deeper level commas to
3953 # keep the entire collection aligned. But in some cases the decision could
3954 # go the other way.
3955  
3956 # The tree for this example at the zero depth has one node containing
3957 # all four lines, since they are identical at zero level (three commas).
3958 # At depth one, there are three 'children' nodes, namely:
3959 # - lines 1 and 2, which have a single comma in the 'sub' at depth 1
3960 # - line 3, which has 2 commas at depth 1
3961 # - line4, which has a ';' and a ',' at depth 1
3962 # There are no deeper alignments in this example.
3963 # so the tree structure for this example is:
3964 #
3965 # depth 0 depth 1 depth 2
3966 # [lines 1-4] -- [line 1-2] - (empty)
3967 # | [line 3] - (empty)
3968 # | [line 4] - (empty)
3969  
3970 # We can carry this to any depth, but it is not really useful to go below
3971 # depth 2. To cleanly stop there, we will consider depth 2 to contain all
3972 # alignments at depth >=2.
3973  
3974 45 ▶ use constant EXPLAIN_PRUNE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3975  
3976 #-------------------------------------------------------------------
3977 # Prune Tree Step 1. Start by scanning the lines and collecting info
3978 #-------------------------------------------------------------------
3979  
3980 # Note that the caller had this info but we have to redo this now because
3981 # alignment tokens may have been deleted.
3982 174 my ( $rline_values, $all_monotonic ) = get_line_token_info($rlines);
3983  
3984 # If all the lines have levels which increase monotonically from left to
3985 # right, then the sweep-left-to-right pass can do a better job of alignment
3986 # than pruning, and without deleting alignments.
3987 174 ▶ return if ($all_monotonic);
Branch: 1 branch
BranchTrueFalseTotal
if $all_monotonic 138 36 174
3988  
3989 # Contents of $rline_values
3990 # [
3991 # $lev_min, $lev_max, $rtoken_patterns, \@levs,
3992 # $rtoken_indexes, $is_monotonic, $imax_true, $imax,
3993 # ];
3994  
3995 # We can work to any depth, but there is little advantage to working
3996 # to a depth greater than 2
3997 36 my $MAX_DEPTH = 2;
3998  
3999 # This arrays will hold the tree of alignment tokens at different depths
4000 # for these lines.
4001 36 my @match_tree;
4002  
4003 # Tree nodes contain these values:
4004 # $match_tree[$depth] = [$jbeg, $jend, $n_parent, $level, $pattern,
4005 # $nc_beg_p, $nc_end_p, $rindexes];
4006 # where
4007 # $depth = 0,1,2 = index of depth of the match
4008  
4009 # $jbeg beginning index j of the range of lines in this match
4010 # $jend ending index j of the range of lines in this match
4011 # $n_parent = index of the containing group at $depth-1, if it exists
4012 # $level = actual level of code being matched in this group
4013 # $pattern = alignment pattern being matched
4014 # $nc_beg_p = first child
4015 # $nc_end_p = last child
4016 # $rindexes = ref to token indexes
4017  
4018 # the patterns and levels of the current group being formed at each depth
4019 36 my ( @token_patterns_current, @levels_current, @token_indexes_current );
4020  
4021 # the patterns and levels of the next line being tested at each depth
4022 36 my ( @token_patterns_next, @levels_next, @token_indexes_next );
4023  
4024 #-----------------------------------------------------------
4025 # define a recursive worker subroutine for tree construction
4026 #-----------------------------------------------------------
4027  
4028 # This is a recursive routine which is called if a match condition changes
4029 # at any depth when a new line is encountered. It ends the match node
4030 # which changed plus all deeper nodes attached to it.
4031 36 my $end_node;
4032 $end_node = sub {
4033  
4034 345 ▶ my ( $depth, $jl, $n_parent ) = @_;
Subroutine __ANON__: called CC=10 SCAR=23.1
4035  
4036 # $depth is the tree depth
4037 # $jl is the index of the line
4038 # $n_parent is index of the parent node of this node
4039  
4040 345 ▶ return if ( $depth > $MAX_DEPTH );
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $MAX_DEPTH 93 252 345
4041  
4042 # end any current group at this depth
4043 252 ▶ if ( $jl >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($jl >= 0 and defined $match_tree[$depth] and @{$match_tree[$depth];} and defined $levels_current[$depth]) 71 181 252
Condition88%8/9
$jl >= 0 and defined $match_tree[$depth] and @{$match_tree[$depth];} and defined $levels_current[$depth]
!ll&&!rl&&r
175671
$jl >= 0 and defined $match_tree[$depth] and @{$match_tree[$depth];}
!ll&&!rl&&r
175077
$jl >= 0 and defined $match_tree[$depth]
!ll&&!rl&&r
1086777
MC/DC75%3/4
$jl >= 0 and defined $match_tree[$depth] and @{$match_tree[$depth];} and defined $levels_current[$depth]
$jl >= 0 defined $match_tree[$depth] @{$match_tree[$depth];} defined $levels_current[$depth]
Truth table80%4/5
A and B and C and D
A= $jl >= 0B= defined $match_tree[$depth]C= @{$match_tree[$depth];}D= defined $levels_current[$depth]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
4044 && defined( $match_tree[$depth] )
4045 77 && @{ $match_tree[$depth] }
4046 && defined( $levels_current[$depth] ) )
4047 {
4048 71 $match_tree[$depth]->[-1]->[1] = $jl;
4049 }
4050  
4051 # Define the index of the node we will create below
4052 252 my $ng_self = 0;
4053 252 ▶ if ( defined( $match_tree[$depth] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match_tree[$depth]) 77 175 252
4054 77 $ng_self = @{ $match_tree[$depth] };
4055 }
4056  
4057 # end any next deeper child node(s)
4058 252 $end_node->( $depth + 1, $jl, $ng_self );
4059  
4060 # update the levels being matched
4061 252 $token_patterns_current[$depth] = $token_patterns_next[$depth];
4062 252 $token_indexes_current[$depth] = $token_indexes_next[$depth];
4063 252 $levels_current[$depth] = $levels_next[$depth];
4064  
4065 # Do not start a new group at this level if it is not being used
4066 252 ▶ if ( !defined( $levels_next[$depth] )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $levels_next[$depth] or $depth > 0 and $levels_next[$depth] <= $levels_next[$depth - 1]) 127 125 252
Condition66%4/6
not defined $levels_next[$depth] or $depth > 0 and $levels_next[$depth] <= $levels_next[$depth - 1]
l!l&&r!l&&!r
1270125
$depth > 0 and $levels_next[$depth] <= $levels_next[$depth - 1]
!ll&&!rl&&r
70550
MC/DC33%1/3
not defined $levels_next[$depth] or $depth > 0 and $levels_next[$depth] <= $levels_next[$depth - 1]
not defined $levels_next[$depth] $depth > 0 $levels_next[$depth] <= $levels_next[$depth - 1]
Truth table75%3/4
not(A) or B and C
A= not defined $levels_next[$depth]B= $depth > 0C= $levels_next[$depth] <= $levels_next[$depth - 1]
ABCresult
1XX1
0111
00X0
0100
4067 || $depth > 0
4068 && $levels_next[$depth] <= $levels_next[ $depth - 1 ] )
4069 {
4070 127 return;
4071 }
4072  
4073 # Create a node for the next group at this depth. We initially assume
4074 # that it will continue to $jmax, and correct that later if the node
4075 # ends earlier.
4076 125 push @{ $match_tree[$depth] },
4077 [
4078 $jl + 1, $jmax, $n_parent, $levels_current[$depth],
4079 $token_patterns_current[$depth],
4080 undef, undef, $token_indexes_current[$depth],
4081 ];
4082  
4083 125 return;
4084 36 }; ## end $end_node = sub
4085  
4086 #-----------------------------------------------------
4087 # Prune Tree Step 2. Loop to form the tree of matches.
4088 #-----------------------------------------------------
4089 36 foreach my $jp ( 0 .. $jmax ) {
4090  
4091 # working with two adjacent line indexes, 'm'=minus, 'p'=plus
4092 246 my $jm = $jp - 1;
4093  
4094 # Pull out needed values for the next line
4095 my ( $lev_min_uu, $lev_max_uu, $rtoken_patterns, $rlevs,
4096 $rtoken_indexes, $is_monotonic_uu, $imax_true_uu, $imax_uu )
4097 246 = @{ $rline_values->[$jp] };
4098  
4099 # Transfer levels and patterns for this line to the working arrays.
4100 # If the number of levels differs from our chosen MAX_DEPTH ...
4101 # if fewer than MAX_DEPTH: leave levels at missing depths undefined
4102 # if more than MAX_DEPTH: set the MAX_DEPTH level to be the maximum
4103 246 @levels_next = @{$rlevs}[ 0 .. $MAX_DEPTH ];
4104 246 ▶ if ( @{$rlevs} > $MAX_DEPTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rlevs;} > $MAX_DEPTH) 5 241 246
4105 5 $levels_next[$MAX_DEPTH] = $rlevs->[-1];
4106 }
4107 246 my $depth = 0;
4108 246 foreach my $item (@levels_next) {
4109 $token_patterns_next[$depth] =
4110 738 ▶ defined($item) ? $rtoken_patterns->{$item} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $item ? : 340 398 738
4111 $token_indexes_next[$depth] =
4112 738 ▶ defined($item) ? $rtoken_indexes->{$item} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $item ? : 340 398 738
4113 738 $depth++;
4114 }
4115  
4116 # Look for a change in match groups...
4117  
4118 # Initialize on the first line
4119 246 ▶ if ( $jp == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($jp == 0) { } 36 210 246
elsif ($rlines->[$jm]{'end_group'}) { } 11 199 210
elsif ($rlines->[$jp]{'is_hanging_side_comment'}) { } 0 199 199
4120 36 my $n_parent;
4121 36 $end_node->( 0, $jm, $n_parent );
4122 }
4123  
4124 # End groups if a hard flag has been set
4125 elsif ( $rlines->[$jm]->{'end_group'} ) {
4126 11 my $n_parent;
4127 11 $end_node->( 0, $jm, $n_parent );
4128 }
4129  
4130 # Continue at hanging side comment
4131 elsif ( $rlines->[$jp]->{'is_hanging_side_comment'} ) {
4132 0 next;
4133 }
4134  
4135 # Otherwise see if anything changed and update the tree if so
4136 else {
4137 199 foreach my $dep ( 0 .. $MAX_DEPTH ) {
4138  
4139 413 my $def_current = defined( $token_patterns_current[$dep] );
4140 413 my $def_next = defined( $token_patterns_next[$dep] );
4141 413 ▶ last if ( !$def_current && !$def_next );
Branch: 1 branch
BranchTrueFalseTotal
unless $def_current or $def_next 152 261 413
Condition100%3/3
$def_current or $def_next
l!l&&r!l&&!r
2574152
MC/DC100%2/2
$def_current or $def_next
$def_current $def_next
Truth table100%3/3
A or B
A= $def_currentB= $def_next
ABresult
1X1
011
000
4142 261 ▶ if ( !$def_current
Branch: 1 branch
BranchTrueFalseTotal
if (not $def_current && $def_next or $token_patterns_current[$dep] ne $token_patterns_next[$dep]) 46 215 261
Condition100%6/6
not $def_current && $def_next or $token_patterns_current[$dep] ne $token_patterns_next[$dep]
l!l&&r!l&&!r
838215
$def_current && $def_next
!ll&&!rl&&r
44253
MC/DC100%3/3
not $def_current && $def_next or $token_patterns_current[$dep] ne $token_patterns_next[$dep]
$def_current $def_next $token_patterns_current[$dep] ne $token_patterns_next[$dep]
Truth table100%4/4
not(A && B) or C
A= $def_currentB= $def_nextC= $token_patterns_current[$dep] ne $token_patterns_next[$dep]
ABCresult
0XX1
10X1
1111
1100
4143 || !$def_next
4144 || $token_patterns_current[$dep] ne
4145 $token_patterns_next[$dep] )
4146 {
4147 46 my $n_parent;
4148 46 ▶ if ( $dep > 0 && defined( $match_tree[ $dep - 1 ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep > 0 and defined $match_tree[$dep - 1]) 23 23 46
Condition66%2/3
$dep > 0 and defined $match_tree[$dep - 1]
!ll&&!rl&&r
23023
MC/DC50%1/2
$dep > 0 and defined $match_tree[$dep - 1]
$dep > 0 defined $match_tree[$dep - 1]
Truth table66%2/3
A and B
A= $dep > 0B= defined $match_tree[$dep - 1]
ABresult
0X0
100
111
4149 23 $n_parent = @{ $match_tree[ $dep - 1 ] } - 1;
4150 }
4151 46 $end_node->( $dep, $jm, $n_parent );
4152 46 last;
4153 }
4154 }
4155 }
4156 } ## end loop to form tree of matches
4157  
4158 #---------------------------------------------------------
4159 # Prune Tree Step 3. Make links from parent to child nodes
4160 #---------------------------------------------------------
4161  
4162 # It seemed cleaner to do this as a separate step rather than during tree
4163 # construction. The children nodes have links up to the parent node which
4164 # created them. Now make links in the opposite direction, so the parents
4165 # can find the children. We store the range of children nodes ($nc_beg,
4166 # $nc_end) of each parent with two additional indexes in the original array.
4167 # These will be undef if no children.
4168 36 foreach my $depth ( reverse( 1 .. $MAX_DEPTH ) ) {
4169 72 ▶ next unless ( defined( $match_tree[$depth] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match_tree[$depth] 35 37 72
4170 37 my $nc_max = @{ $match_tree[$depth] } - 1;
4171 37 my $np_now;
4172 37 foreach my $nc ( 0 .. $nc_max ) {
4173 55 my $np = $match_tree[$depth]->[$nc]->[2];
4174 55 ▶ if ( !defined($np) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $np) 0 55 55
4175  
4176 # shouldn't happen
4177 #print STDERR "lost child $np at depth $depth\n";
4178 0 next;
4179 }
4180 55 ▶ if ( !defined($np_now) || $np != $np_now ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $np_now or $np != $np_now) 40 15 55
Condition100%3/3
not defined $np_now or $np != $np_now
l!l&&r!l&&!r
37315
MC/DC100%2/2
not defined $np_now or $np != $np_now
not defined $np_now $np != $np_now
Truth table100%3/3
not(A) or B
A= not defined $np_nowB= $np != $np_now
ABresult
1X1
011
000
4181 40 $np_now = $np;
4182 40 $match_tree[ $depth - 1 ]->[$np]->[5] = $nc;
4183 }
4184 55 $match_tree[ $depth - 1 ]->[$np]->[6] = $nc;
4185 }
4186 } ## end loop to make links down to the child nodes
4187  
4188 36 EXPLAIN_PRUNE > 0 && do {
4189 print "Tree complete. Found these groups:\n";
4190 foreach my $depth ( 0 .. $MAX_DEPTH ) {
4191 Dump_tree_groups( \@{ $match_tree[$depth] }, "depth=$depth" );
4192 }
4193 };
4194  
4195 #------------------------------------------------------
4196 # Prune Tree Step 4. Make a list of nodes to be deleted
4197 #------------------------------------------------------
4198  
4199 # list of lines with tokens to be deleted:
4200 # [$jbeg, $jend, $level_keep]
4201 # $jbeg..$jend is the range of line indexes,
4202 # $level_keep is the minimum level to keep
4203 36 my @delete_list;
4204  
4205 # We work with a list of nodes to visit at the next deeper depth.
4206 my @todo_list;
4207 36 ▶ if ( defined( $match_tree[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match_tree[0]) 36 0 36
4208 36 @todo_list = ( 0 .. @{ $match_tree[0] } - 1 );
4209 }
4210  
4211 36 foreach my $depth ( 0 .. $MAX_DEPTH ) {
4212 101 ▶ last if ( !@todo_list );
Branch: 1 branch
BranchTrueFalseTotal
unless @todo_list 36 65 101
4213 65 my @todo_next;
4214 65 foreach my $np (@todo_list) {
4215 my ( $jbeg_p, $jend_p, $np_p_uu, $lev_p, $pat_p_uu, $nc_beg_p,
4216 $nc_end_p, $rindexes_p_uu )
4217 103 = @{ $match_tree[$depth]->[$np] };
4218 103 my $nlines_p = $jend_p - $jbeg_p + 1;
4219  
4220 # nothing to do if no children
4221 103 ▶ next unless ( defined($nc_beg_p) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nc_beg_p 63 40 103
4222  
4223 # Define the number of lines to either keep or delete a child node.
4224 # This is the key decision we have to make. We want to delete
4225 # short runs of matched lines, and keep long runs. It seems easier
4226 # for the eye to follow breaks in monotonic level changes than
4227 # non-monotonic level changes. For example, the following looks
4228 # best if we delete the lower level alignments:
4229  
4230 # [1] ~~ [];
4231 # [ ["foo"], ["bar"] ] ~~ [ qr/o/, qr/a/ ];
4232 # [ qr/o/, qr/a/ ] ~~ [ ["foo"], ["bar"] ];
4233 # [ "foo", "bar" ] ~~ [ qr/o/, qr/a/ ];
4234 # [ qr/o/, qr/a/ ] ~~ [ "foo", "bar" ];
4235 # $deep1 ~~ $deep1;
4236  
4237 # So we will use two thresholds.
4238 40 my $nmin_mono = $depth + 2;
4239 40 my $nmin_non_mono = $depth + 6;
4240 40 ▶ if ( $nmin_mono > $nlines_p - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmin_mono > $nlines_p - 1) 26 14 40
4241 26 $nmin_mono = $nlines_p - 1;
4242 }
4243 40 ▶ if ( $nmin_non_mono > $nlines_p - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmin_non_mono > $nlines_p - 1) 36 4 40
4244 36 $nmin_non_mono = $nlines_p - 1;
4245 }
4246  
4247 # loop to keep or delete each child node
4248 40 foreach my $nc ( $nc_beg_p .. $nc_end_p ) {
4249 my ( $jbeg_c, $jend_c, $np_c_uu, $lev_c_uu, $pat_c_uu,
4250 $nc_beg_c_uu, $nc_end_c_uu )
4251 55 = @{ $match_tree[ $depth + 1 ]->[$nc] };
4252 55 my $nlines_c = $jend_c - $jbeg_c + 1;
4253 55 my $is_monotonic = $rline_values->[$jbeg_c]->[5];
4254 55 ▶ my $nmin = $is_monotonic ? $nmin_mono : $nmin_non_mono;
Branch: 1 branch
BranchTrueFalseTotal
$is_monotonic ? : 1 54 55
4255 55 ▶ if ( $nlines_c < $nmin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nlines_c < $nmin) { } 22 33 55
4256 ##print "deleting child, nlines=$nlines_c, nmin=$nmin\n";
4257 22 push @delete_list, [ $jbeg_c, $jend_c, $lev_p ];
4258 }
4259 else {
4260 ##print "keeping child, nlines=$nlines_c, nmin=$nmin\n";
4261 33 push @todo_next, $nc;
4262 }
4263 }
4264 }
4265 65 @todo_list = @todo_next;
4266 } ## end loop to mark nodes to delete
4267  
4268 #------------------------------------------------------------
4269 # Prune Tree Step 5. Loop to delete selected alignment tokens
4270 #------------------------------------------------------------
4271 36 foreach my $item (@delete_list) {
4272 22 my ( $jbeg, $jend, $level_keep ) = @{$item};
4273 22 foreach my $jj ( $jbeg .. $jend ) {
4274 28 my $line = $rlines->[$jj];
4275 28 my @idel;
4276 28 my $rtokens = $line->{'rtokens'};
4277 28 my $imax = @{$rtokens} - 2;
4278 28 foreach my $i ( 0 .. $imax ) {
4279 152 my $tok = $rtokens->[$i];
4280 152 my ( $raw_tok_uu, $lev, $tag_uu, $tok_count_uu ) =
4281 decode_alignment_token($tok);
4282 152 ▶ if ( $lev > $level_keep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lev > $level_keep) 83 69 152
4283 83 push @idel, $i;
4284 }
4285 }
4286 28 ▶ if (@idel) {
Branch: 1 branch
BranchTrueFalseTotal
if (@idel) 28 0 28
4287 28 delete_selected_tokens( $line, \@idel );
4288 }
4289 }
4290 } ## end loop to delete selected alignment tokens
4291  
4292 36 return;
4293 } ## end sub prune_alignment_tree
4294  
4295 sub Dump_tree_groups {
4296  
4297 0 ▶ my ( $rgroup, $msg ) = @_;
Subroutine Dump_tree_groups: not called CC=4 SCAR=30.0
Pod undocumented
4298  
4299 # Debug routine
4300 0 print "$msg\n";
4301 0 local $LIST_SEPARATOR = ')(';
4302 0 foreach my $item ( @{$rgroup} ) {
4303 0 my @fix = @{$item};
4304 0 ▶ foreach my $val (@fix) { $val = "undef" unless ( defined($val) ); }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
4305 0 $fix[4] = "...";
4306 0 print "(@fix)\n";
4307 }
4308 0 return;
4309 } ## end sub Dump_tree_groups
4310  
4311 # This test did not give sufficiently better results to use as an update,
4312 # but the flag is kept as a starting point for future testing.
4313 45 ▶use constant TEST_MARGINAL_EQ_ALIGNMENT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4314  
4315 sub is_marginal_match {
4316  
4317 295 ▶ my ( $line_0, $line_1, $group_level, $imax_align, $imax_prev ) = @_;
Subroutine is_marginal_match: called CC=63 SCAR=44.6
Pod undocumented
4318  
4319 # Decide if we should undo some or all of the common alignments of a
4320 # group of just two lines.
4321  
4322 # Given:
4323 # $line_0 and $line_1 - the two lines
4324 # $group_level = the indentation level of the group being processed
4325 # $imax_align = the maximum index of the common alignment tokens
4326 # of the two lines
4327 # $imax_prev = the maximum index of the common alignment tokens
4328 # with the line before $line_0 (=-1 of does not exist)
4329  
4330 # Return:
4331 # $is_marginal = true if the two lines should NOT be fully aligned
4332 # = false if the two lines can remain fully aligned
4333 # $imax_align = the index of the highest alignment token shared by
4334 # these two lines to keep if the match is marginal.
4335  
4336 # When we have an alignment group of just two lines like this, we are
4337 # working in the twilight zone of what looks good and what looks bad.
4338 # This routine is a collection of rules which work have been found to
4339 # work fairly well, but it will need to be updated from time to time.
4340  
4341 295 my $is_marginal = 0;
4342  
4343 #---------------------------------------
4344 # Always align certain special cases ...
4345 #---------------------------------------
4346 295 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line_1->{"j_terminal_match"} or $line_0->{"list_type"} or $line_1->{"is_hanging_side_comment"}) 127 162 289
Condition100%6/6
defined $line_1->{"j_terminal_match"} or $line_0->{"list_type"} or $line_1->{"is_hanging_side_comment"}
l!l&&r!l&&!r
1258162
defined $line_1->{"j_terminal_match"} or $line_0->{"list_type"}
l!l&&r!l&&!r
6119170
MC/DC100%3/3
defined $line_1->{"j_terminal_match"} or $line_0->{"list_type"} or $line_1->{"is_hanging_side_comment"}
defined $line_1->{"j_terminal_match"} $line_0->{"list_type"} $line_1->{"is_hanging_side_comment"}
Truth table100%4/4
A or B or C
A= defined $line_1->{"j_terminal_match"}B= $line_0->{"list_type"}C= $line_1->{"is_hanging_side_comment"}
ABCresult
1XX1
01X1
0011
0000
4347  
4348 # always keep alignments of a terminal else or ternary
4349 defined( $line_1->{'j_terminal_match'} )
4350  
4351 # always align lists
4352 || $line_0->{'list_type'}
4353  
4354 # always align hanging side comments
4355 || $line_1->{'is_hanging_side_comment'}
4356  
4357 )
4358 {
4359 133 return ( $is_marginal, $imax_align );
4360 }
4361  
4362 162 my $jmax_0 = $line_0->{'jmax'};
4363 162 my $jmax_1 = $line_1->{'jmax'};
4364 162 my $rtokens_1 = $line_1->{'rtokens'};
4365 ## my $rtokens_0 = $line_0->{'rtokens'};
4366 162 my $rfield_lengths_0 = $line_0->{'rfield_lengths'};
4367 162 my $rfield_lengths_1 = $line_1->{'rfield_lengths'};
4368 162 my $rpatterns_0 = $line_0->{'rpatterns'};
4369 162 my $rpatterns_1 = $line_1->{'rpatterns'};
4370 162 my $imax_next = $line_1->{'imax_pair'};
4371  
4372 # We will scan the alignment tokens and set a flag '$is_marginal' if
4373 # it seems that the an alignment would look bad.
4374 162 my $max_pad = 0;
4375 162 my $saw_good_alignment = 0;
4376 162 my $saw_if_or; # if we saw an 'if' or 'or' at group level
4377 162 my $raw_tokb = EMPTY_STRING; # first token seen at group level
4378 162 my $jfirst_bad;
4379 my $line_ending_fat_comma; # is last token just a '=>' ?
4380 162 my $j0_eq_pad;
4381 162 my $j0_max_pad = 0;
4382  
4383 162 foreach my $j ( 0 .. $jmax_1 - 2 ) {
4384 215 my ( $raw_tok, $lev, $tag_uu, $tok_count_uu ) =
4385 decode_alignment_token( $rtokens_1->[$j] );
4386 215 ▶ if ( $raw_tok && $lev == $group_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok and $lev == $group_level) 185 30 215
Condition66%2/3
$raw_tok and $lev == $group_level
!ll&&!rl&&r
030185
MC/DC50%1/2
$raw_tok and $lev == $group_level
$raw_tok $lev == $group_level
Truth table66%2/3
A and B
A= $raw_tokB= $lev == $group_level
ABresult
0X0
100
111
4387 185 ▶ if ( !$raw_tokb ) { $raw_tokb = $raw_tok }
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw_tokb) 149 36 185
4388 185 ▶ $saw_if_or ||= $is_if_or{$raw_tok};
Condition100%3/3
$saw_if_or ||= $is_if_or{$raw_tok}
l!l&&r!l&&!r
418163
MC/DC100%2/2
$saw_if_or ||= $is_if_or{$raw_tok}
$saw_if_or $is_if_or{$raw_tok}
Truth table100%3/3
A ||= B
A= $saw_if_orB= $is_if_or{$raw_tok}
ABresult
1X1
011
000
4389 }
4390  
4391 # When the first of the two lines ends in a bare '=>' this will
4392 # probably be marginal match. (For a bare =>, the next field length
4393 # will be 2 or 3, depending on side comment)
4394 $line_ending_fat_comma =
4395 215 ▶ $j == $jmax_1 - 2
Condition100%6/6
$j == $jmax_1 - 2 && $raw_tok eq '=>' && $$rfield_lengths_0[$j + 1] <= 3
!ll&&!rl&&r
21041
$j == $jmax_1 - 2 and $raw_tok eq "=>"
!ll&&!rl&&r
611495
MC/DC100%3/3
$j == $jmax_1 - 2 && $raw_tok eq '=>' && $$rfield_lengths_0[$j + 1] <= 3
$j == $jmax_1 - 2 $raw_tok eq "=>" $$rfield_lengths_0[$j + 1] <= 3
Truth table100%4/4
A and B && C
A= $j == $jmax_1 - 2B= $raw_tok eq "=>"C= $$rfield_lengths_0[$j + 1] <= 3
ABCresult
0XX0
10X0
1100
1111
4396 && $raw_tok eq '=>'
4397 && $rfield_lengths_0->[ $j + 1 ] <= 3;
4398  
4399 215 my $pad = $rfield_lengths_1->[$j] - $rfield_lengths_0->[$j];
4400 215 ▶ if ( $j == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 0) 154 61 215
4401 $pad += $line_1->{'leading_space_count'} -
4402 154 $line_0->{'leading_space_count'};
4403  
4404 # Remember the pad at a leading equals
4405 154 ▶ if ( $raw_tok eq '=' && $lev == $group_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok eq "=" and $lev == $group_level) 92 62 154
Condition66%2/3
$raw_tok eq "=" and $lev == $group_level
!ll&&!rl&&r
62092
MC/DC50%1/2
$raw_tok eq "=" and $lev == $group_level
$raw_tok eq "=" $lev == $group_level
Truth table66%2/3
A and B
A= $raw_tok eq "="B= $lev == $group_level
ABresult
0X0
100
111
4406 92 $j0_eq_pad = $pad;
4407 92 $j0_max_pad =
4408 0.5 * ( $rfield_lengths_1->[0] + $rfield_lengths_0->[0] );
4409 92 ▶ $j0_max_pad = 4 if ( $j0_max_pad < 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $j0_max_pad < 4 5 87 92
4410 }
4411 }
4412  
4413 215 ▶ if ( $pad < 0 ) { $pad = -$pad }
Branch: 1 branch
BranchTrueFalseTotal
if ($pad < 0) 52 163 215
4414 215 ▶ if ( $pad > $max_pad ) { $max_pad = $pad }
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > $max_pad) 116 99 215
4415 215 ▶ if ( $is_good_marginal_alignment{$raw_tok}
Branch: 1 branch
BranchTrueFalseTotal
if ($is_good_marginal_alignment{$raw_tok} and not $line_ending_fat_comma) { } 157 58 215
Condition100%3/3
$is_good_marginal_alignment{$raw_tok} and not $line_ending_fat_comma
!ll&&!rl&&r
571157
MC/DC100%2/2
$is_good_marginal_alignment{$raw_tok} and not $line_ending_fat_comma
$is_good_marginal_alignment{$raw_tok} not $line_ending_fat_comma
Truth table100%3/3
A and not(B)
A= $is_good_marginal_alignment{$raw_tok}B= not $line_ending_fat_comma
ABresult
0X0
100
111
4416 && !$line_ending_fat_comma )
4417 {
4418 157 $saw_good_alignment = 1;
4419 }
4420 else {
4421 58 ▶ $jfirst_bad = $j unless ( defined($jfirst_bad) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $jfirst_bad 49 9 58
4422 }
4423 215 my $pat_0 = $rpatterns_0->[$j];
4424 215 my $pat_1 = $rpatterns_1->[$j];
4425 215 ▶ if ( $pat_0 ne $pat_1 && length($pat_0) eq length($pat_1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_0 ne $pat_1 and length $pat_0 eq length $pat_1) 3 212 215
Condition100%3/3
$pat_0 ne $pat_1 and length $pat_0 eq length $pat_1
!ll&&!rl&&r
170423
MC/DC100%2/2
$pat_0 ne $pat_1 and length $pat_0 eq length $pat_1
$pat_0 ne $pat_1 length $pat_0 eq length $pat_1
Truth table100%3/3
A and B
A= $pat_0 ne $pat_1B= length $pat_0 eq length $pat_1
ABresult
0X0
100
111
4426 3 $pat_0 =~ tr/n/Q/;
4427 3 $pat_1 =~ tr/n/Q/;
4428 }
4429 215 ▶ if ( $pat_0 ne $pat_1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_0 ne $pat_1) 45 170 215
4430  
4431 # Flag this as a marginal match since patterns differ.
4432 # Normally, we will not allow just two lines to match if
4433 # marginal. But we can allow matching in some specific cases.
4434  
4435 45 ▶ $jfirst_bad = $j if ( !defined($jfirst_bad) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $jfirst_bad 24 21 45
4436 45 ▶ $is_marginal = 1 if ( $is_marginal == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $is_marginal == 0 44 1 45
4437 45 ▶ if ( $raw_tok eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok eq "=") 13 32 45
4438  
4439 # Here is an example of a marginal match:
4440 # $done{$$op} = 1;
4441 # $op = compile_bblock($op);
4442 # The left tokens are both identifiers, but
4443 # one accesses a hash and the other doesn't.
4444 # We'll let this be a tentative match and undo
4445 # it later if we don't find more than 2 lines
4446 # in the group.
4447 13 $is_marginal = 2;
4448 }
4449 }
4450 }
4451  
4452 162 ▶ $is_marginal = 1 if ( $is_marginal == 0 && $line_ending_fat_comma );
Branch: 1 branch
BranchTrueFalseTotal
if $is_marginal == 0 and $line_ending_fat_comma 0 162 162
Condition66%2/3
$is_marginal == 0 and $line_ending_fat_comma
!ll&&!rl&&r
441180
MC/DC0%0/2
$is_marginal == 0 and $line_ending_fat_comma
$is_marginal == 0 $line_ending_fat_comma
Truth table66%2/3
A and B
A= $is_marginal == 0B= $line_ending_fat_comma
ABresult
0X0
100
111
4453  
4454 # Turn off the "marginal match" flag in some cases...
4455 # A "marginal match" occurs when the alignment tokens agree
4456 # but there are differences in the other tokens (patterns).
4457 # If we leave the marginal match flag set, then the rule is that we
4458 # will align only if there are more than two lines in the group.
4459 # We will turn of the flag if we almost have a match
4460 # and either we have seen a good alignment token or we
4461 # just need a small pad (2 spaces) to fit. These rules are
4462 # the result of experimentation. Tokens which misaligned by just
4463 # one or two characters are annoying. On the other hand,
4464 # large gaps to less important alignment tokens are also annoying.
4465 162 ▶ if ( $is_marginal == 1
Branch: 1 branch
BranchTrueFalseTotal
if ($is_marginal == 1 and $saw_good_alignment || $max_pad < 3) 25 137 162
Condition100%6/6
$is_marginal == 1 and $saw_good_alignment || $max_pad < 3
!ll&&!rl&&r
131274
$saw_good_alignment or $max_pad < 3
l!l&&r!l&&!r
2146
MC/DC66%2/3
$is_marginal == 1 and $saw_good_alignment || $max_pad < 3
$is_marginal == 1 $saw_good_alignment $max_pad < 3
Truth table75%3/4
A and B or C
A= $is_marginal == 1B= $saw_good_alignmentC= $max_pad < 3
ABCresult
0XX0
1000
11X1
1011
4466 && ( $saw_good_alignment || $max_pad < 3 ) )
4467 {
4468 25 $is_marginal = 0;
4469 }
4470  
4471 # We will use the line endings to help decide on alignments...
4472 # See if the lines end with semicolons...
4473 162 my $sc_term0;
4474 my $sc_term1;
4475 162 ▶ if ( $jmax_0 < 1 || $jmax_1 < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax_0 < 1 or $jmax_1 < 1) { } 0 162 162
Condition33%1/3
$jmax_0 < 1 or $jmax_1 < 1
l!l&&r!l&&!r
00162
MC/DC0%0/2
$jmax_0 < 1 or $jmax_1 < 1
$jmax_0 < 1 $jmax_1 < 1
Truth table33%1/3
A or B
A= $jmax_0 < 1B= $jmax_1 < 1
ABresult
1X1
011
000
4476  
4477 # shouldn't happen
4478 }
4479 else {
4480 162 my $pat0 = $rpatterns_0->[ $jmax_0 - 1 ];
4481 162 my $pat1 = $rpatterns_1->[ $jmax_1 - 1 ];
4482 162 $sc_term0 = $pat0 =~ /;b?$/;
4483 162 $sc_term1 = $pat1 =~ /;b?$/;
4484 }
4485  
4486 162 ▶ if ( !$is_marginal && !$sc_term0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_marginal or $sc_term0) 39 123 162
Condition100%3/3
$is_marginal or $sc_term0
l!l&&r!l&&!r
1910439
MC/DC100%2/2
$is_marginal or $sc_term0
$is_marginal $sc_term0
Truth table100%3/3
A or B
A= $is_marginalB= $sc_term0
ABresult
1X1
011
000
4487  
4488 # First line of assignment should be semicolon terminated.
4489 # For example, do not align here:
4490 # $$href{-NUM_TEXT_FILES} = $$href{-NUM_BINARY_FILES} =
4491 # $$href{-NUM_DIRS} = 0;
4492 39 ▶ if ( $is_assignment{$raw_tokb} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_assignment{$raw_tokb}) 1 38 39
4493 1 $is_marginal = 1;
4494 }
4495 }
4496  
4497 # Try to avoid some undesirable alignments of opening tokens
4498 # for example, the space between grep and { here:
4499 # return map { ( $_ => $_ ) }
4500 # grep { /$handles/ } $self->_get_delegate_method_list;
4501 $is_marginal ||=
4502 162 ▶ ( $raw_tokb eq '(' || $raw_tokb eq '{' )
Condition100%12/12
$is_marginal ||= ($raw_tokb eq "(" || $raw_tokb eq "{") && $jmax_1 == 2 && $sc_term0 ne $sc_term1
l!l&&r!l&&!r
201141
($raw_tokb eq '(' || $raw_tokb eq '{') && $jmax_1 == 2 && $sc_term0 ne $sc_term1
!ll&&!rl&&r
130111
$raw_tokb eq "(" || $raw_tokb eq "{" and $jmax_1 == 2
!ll&&!rl&&r
1171312
$raw_tokb eq "(" or $raw_tokb eq "{"
l!l&&r!l&&!r
1213117
MC/DC0%0/5
$is_marginal ||= ($raw_tokb eq "(" || $raw_tokb eq "{") && $jmax_1 == 2 && $sc_term0 ne $sc_term1
$is_marginal $raw_tokb eq "(" $raw_tokb eq "{" $jmax_1 == 2 $sc_term0 ne $sc_term1
Truth table0%0/8
A ||= B or C and D && E
A= $is_marginalB= $raw_tokb eq "("C= $raw_tokb eq "{"D= $jmax_1 == 2E= $sc_term0 ne $sc_term1
ABCDEresult
1XXXX1
01X111
001111
000XX0
01X0X0
0010X0
01X100
001100
4503 && $jmax_1 == 2
4504 && $sc_term0 ne $sc_term1;
4505  
4506 #---------------------------------------
4507 # return if this is not a marginal match
4508 #---------------------------------------
4509 162 ▶ if ( !$is_marginal ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_marginal) 141 21 162
4510 141 return ( $is_marginal, $imax_align );
4511 }
4512  
4513 # Undo the marginal match flag in certain cases,
4514  
4515 # Two lines with a leading equals-like operator are allowed to
4516 # align if the patterns to the left of the equals are the same.
4517 # For example the following two lines are a marginal match but have
4518 # the same left side patterns, so we will align the equals.
4519 # my $orig = my $format = "^<<<<< ~~\n";
4520 # my $abc = "abc";
4521 # But these have a different left pattern so they will not be
4522 # aligned
4523 # $xmldoc .= $`;
4524 # $self->{'leftovers'} .= "<bx-seq:seq" . $';
4525  
4526 # First line semicolon terminated but second not, usually ok:
4527 # my $want = "'ab', 'a', 'b'";
4528 # my $got = join( ", ",
4529 # map { defined($_) ? "'$_'" : "undef" }
4530 # @got );
4531 # First line not semicolon terminated, Not OK to match:
4532 # $$href{-NUM_TEXT_FILES} = $$href{-NUM_BINARY_FILES} =
4533 # $$href{-NUM_DIRS} = 0;
4534 21 my $pat0 = $rpatterns_0->[0];
4535 21 my $pat1 = $rpatterns_1->[0];
4536  
4537 #---------------------------------------------------------
4538 # Turn off the marginal flag for some types of assignments
4539 #---------------------------------------------------------
4540 21 ▶ if ( $is_assignment{$raw_tokb} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_assignment{$raw_tokb}) { } 14 7 21
elsif ($raw_tokb eq '=>') { } 0 7 7
elsif ($raw_tokb eq '=~') { } 0 7 7
4541  
4542 # undo marginal flag if first line is semicolon terminated
4543 # and leading patterns match
4544 14 ▶ if ($sc_term0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sc_term0) 13 1 14
4545 13 $is_marginal = $pat0 ne $pat1;
4546 }
4547 }
4548 elsif ( $raw_tokb eq '=>' ) {
4549  
4550 # undo marginal flag if patterns match
4551 0 ▶ $is_marginal = $pat0 ne $pat1 || $line_ending_fat_comma;
Condition0%0/3
$pat0 ne $pat1 || $line_ending_fat_comma
l!l&&r!l&&!r
000
MC/DC0%0/2
$pat0 ne $pat1 || $line_ending_fat_comma
$pat0 ne $pat1 $line_ending_fat_comma
Truth table0%0/3
A || B
A= $pat0 ne $pat1B= $line_ending_fat_comma
ABresult
1X1
011
000
4552 }
4553 elsif ( $raw_tokb eq '=~' ) {
4554  
4555 # undo marginal flag if both lines are semicolon terminated
4556 # and leading patters match
4557 0 ▶ if ( $sc_term1 && $sc_term0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sc_term1 and $sc_term0) 0 0 0
Condition0%0/3
$sc_term1 and $sc_term0
!ll&&!rl&&r
000
MC/DC0%0/2
$sc_term1 and $sc_term0
$sc_term1 $sc_term0
Truth table0%0/3
A and B
A= $sc_term1B= $sc_term0
ABresult
0X0
100
111
4558 0 $is_marginal = $pat0 ne $pat1;
4559 }
4560 }
4561 else {
4562 ## ok: (none of the above)
4563 }
4564  
4565 #-----------------------------------------------------
4566 # Turn off the marginal flag if we saw an 'if' or 'or'
4567 #-----------------------------------------------------
4568  
4569 # A trailing 'if' and 'or' often gives a good alignment
4570 # For example, we can align these:
4571 # return -1 if $_[0] =~ m/^CHAPT|APPENDIX/;
4572 # return $1 + 0 if $_[0] =~ m/^SECT(\d*)$/;
4573  
4574 # or
4575 # $d_in_m[2] = 29 if ( &Date_LeapYear($y) );
4576 # $d = $d_in_m[$m] if ( $d > $d_in_m[$m] );
4577  
4578 21 ▶ if ($saw_if_or) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_if_or) 5 16 21
4579  
4580 # undo marginal flag if both lines are semicolon terminated
4581 5 ▶ if ( $sc_term0 && $sc_term1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sc_term0 and $sc_term1) 5 0 5
Condition33%1/3
$sc_term0 and $sc_term1
!ll&&!rl&&r
005
MC/DC0%0/2
$sc_term0 and $sc_term1
$sc_term0 $sc_term1
Truth table33%1/3
A and B
A= $sc_term0B= $sc_term1
ABresult
0X0
100
111
4582 5 $is_marginal = 0;
4583 }
4584 }
4585  
4586 # For a marginal match, only keep matches before the first 'bad' match
4587 21 ▶ if ( $is_marginal
Branch: 1 branch
BranchTrueFalseTotal
if ($is_marginal and defined $jfirst_bad and $imax_align > $jfirst_bad - 1) 0 21 21
Condition83%5/6
$is_marginal and defined $jfirst_bad and $imax_align > $jfirst_bad - 1
!ll&&!rl&&r
6150
$is_marginal and defined $jfirst_bad
!ll&&!rl&&r
5115
MC/DC0%0/3
$is_marginal and defined $jfirst_bad and $imax_align > $jfirst_bad - 1
$is_marginal defined $jfirst_bad $imax_align > $jfirst_bad - 1
Truth table75%3/4
A and B and C
A= $is_marginalB= defined $jfirst_badC= $imax_align > $jfirst_bad - 1
ABCresult
0XX0
10X0
1100
1111
4588 && defined($jfirst_bad)
4589 && $imax_align > $jfirst_bad - 1 )
4590 {
4591 0 $imax_align = $jfirst_bad - 1;
4592 }
4593  
4594 #----------------------------------------------------------
4595 # Allow sweep to match lines with leading '=' in some cases
4596 #----------------------------------------------------------
4597 21 ▶ if ( $imax_align < 0 && defined($j0_eq_pad) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imax_align < 0 and defined $j0_eq_pad) 14 7 21
Condition66%2/3
$imax_align < 0 and defined $j0_eq_pad
!ll&&!rl&&r
0714
MC/DC50%1/2
$imax_align < 0 and defined $j0_eq_pad
$imax_align < 0 defined $j0_eq_pad
Truth table66%2/3
A and B
A= $imax_align < 0B= defined $j0_eq_pad
ABresult
0X0
100
111
4598  
4599 14 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($imax_next >= 0 || $imax_prev >= 0 || 0 and $j0_eq_pad >= -$j0_max_pad and $j0_eq_pad <= $j0_max_pad) 0 14 14
Condition36%4/11
$imax_next >= 0 || $imax_prev >= 0 || 0 and $j0_eq_pad >= -$j0_max_pad and $j0_eq_pad <= $j0_max_pad
!ll&&!rl&&r
1400
$imax_next >= 0 || $imax_prev >= 0 || 0 and $j0_eq_pad >= -$j0_max_pad
!ll&&!rl&&r
1400
$imax_next >= 0 or $imax_prev >= 0 or 0
l!l
014
$imax_next >= 0 or $imax_prev >= 0
l!l&&r!l&&!r
0014
MC/DC0%0/4
$imax_next >= 0 || $imax_prev >= 0 || 0 and $j0_eq_pad >= -$j0_max_pad and $j0_eq_pad <= $j0_max_pad
$imax_next >= 0 $imax_prev >= 0 $j0_eq_pad >= -$j0_max_pad $j0_eq_pad <= $j0_max_pad
Truth table14%1/7
A or B and C and D
A= $imax_next >= 0B= $imax_prev >= 0C= $j0_eq_pad >= -$j0_max_padD= $j0_eq_pad <= $j0_max_pad
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
4600  
4601 # If there is a following line with leading equals, or
4602 # preceding line with leading equals, then let the sweep align
4603 # them without restriction. For example, the first two lines
4604 # here are a marginal match, but they are followed by a line
4605 # with leading equals, so the sweep-lr logic can align all of
4606 # the lines:
4607  
4608 # $date[1] = $month_to_num{ $date[1] }; # <--line_0
4609 # @xdate = split( /[:\/\s]/, $log->field('t') ); # <--line_1
4610 # $day = sprintf( "%04d/%02d/%02d", @date[ 2, 1, 0 ] );
4611 # $time = sprintf( "%02d:%02d:%02d", @date[ 3 .. 5 ] );
4612  
4613 # Likewise, if we reverse the two pairs we want the same result
4614  
4615 # $day = sprintf( "%04d/%02d/%02d", @date[ 2, 1, 0 ] );
4616 # $time = sprintf( "%02d:%02d:%02d", @date[ 3 .. 5 ] );
4617 # $date[1] = $month_to_num{ $date[1] }; # <--line_0
4618 # @xdate = split( /[:\/\s]/, $log->field('t') ); # <--line_1
4619  
4620 (
4621 $imax_next >= 0
4622 || $imax_prev >= 0
4623 || TEST_MARGINAL_EQ_ALIGNMENT
4624 )
4625 && $j0_eq_pad >= -$j0_max_pad
4626 && $j0_eq_pad <= $j0_max_pad
4627 )
4628 {
4629  
4630 # But do not do this if there is a comma before the '='.
4631 # For example, the first two lines below have commas and
4632 # therefore are not allowed to align with lines 3 & 4:
4633  
4634 # my ( $x, $y ) = $self->Size(); #<--line_0
4635 # my ( $left, $top, $right, $bottom ) = $self->Window(); #<--l_1
4636 # my $vx = $right - $left;
4637 # my $vy = $bottom - $top;
4638  
4639 0 ▶ if ( $rpatterns_0->[0] !~ /,/ && $rpatterns_1->[0] !~ /,/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rpatterns_0->[0] =~ /,/ or $rpatterns_1->[0] =~ /,/) 0 0 0
Condition0%0/3
$rpatterns_0->[0] =~ /,/ or $rpatterns_1->[0] =~ /,/
l!l&&r!l&&!r
000
MC/DC0%0/2
$rpatterns_0->[0] =~ /,/ or $rpatterns_1->[0] =~ /,/
$rpatterns_0->[0] =~ /,/ $rpatterns_1->[0] =~ /,/
Truth table0%0/3
A or B
A= $rpatterns_0->[0] =~ /,/B= $rpatterns_1->[0] =~ /,/
ABresult
1X1
011
000
4640 0 $imax_align = 0;
4641 }
4642 }
4643 }
4644  
4645 21 return ( $is_marginal, $imax_align );
4646 } ## end sub is_marginal_match
4647  
4648 sub get_extra_leading_spaces {
4649  
4650 447 ▶ my ( $rlines, $rgroups ) = @_;
Subroutine get_extra_leading_spaces: called CC=15 SCAR=27.4
Pod undocumented
4651  
4652 #----------------------------------------------------------
4653 # Define any extra indentation space (for the -lp option).
4654 # Here is why:
4655 # If a list has side comments, sub scan_list must dump the
4656 # list before it sees everything. When this happens, it sets
4657 # the indentation to the standard scheme, but notes how
4658 # many spaces it would have liked to use. We may be able
4659 # to recover that space here in the event that all of the
4660 # lines of a list are back together again.
4661 #----------------------------------------------------------
4662  
4663 447 ▶ return 0 if ( !@{$rlines} || !@{$rgroups} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rlines;} and @{$rgroups;} 0 447 447
Condition33%1/3
@{$rlines;} and @{$rgroups;}
!ll&&!rl&&r
00447
MC/DC0%0/2
@{$rlines;} and @{$rgroups;}
@{$rlines;} @{$rgroups;}
Truth table33%1/3
A and B
A= @{$rlines;}B= @{$rgroups;}
ABresult
0X0
100
111
4664  
4665 447 my $object = $rlines->[0]->{'indentation'};
4666 447 ▶ return 0 if ( !ref($object) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $object 388 59 447
4667 59 my $extra_leading_spaces = 0;
4668 59 my $extra_indentation_spaces_wanted = get_recoverable_spaces($object);
4669 59 ▶ return ($extra_leading_spaces) if ( !$extra_indentation_spaces_wanted );
Branch: 1 branch
BranchTrueFalseTotal
unless $extra_indentation_spaces_wanted 46 13 59
4670  
4671 13 my $min_spaces = $extra_indentation_spaces_wanted;
4672 13 ▶ if ( $min_spaces > 0 ) { $min_spaces = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($min_spaces > 0) 13 0 13
4673  
4674 # loop over all groups
4675 13 my $ng = -1;
4676 13 my $ngroups = @{$rgroups};
4677 13 foreach my $item ( @{$rgroups} ) {
4678 32 $ng++;
4679 32 my ( $jbeg, $jend ) = @{$item};
4680 32 foreach my $j ( $jbeg .. $jend ) {
4681 44 ▶ next if ( $j == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $j == 0 13 31 44
4682  
4683 # all indentation objects must be the same
4684 31 ▶ if ( $object != $rlines->[$j]->{'indentation'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object != $rlines->[$j]{"indentation"}) 1 30 31
4685 1 return 0;
4686 }
4687 }
4688  
4689 # find the maximum space without exceeding the line length for this group
4690 31 my $avail = $rlines->[$jbeg]->get_available_space_on_right();
4691 31 ▶ my $spaces =
Branch: 1 branch
BranchTrueFalseTotal
$avail > $extra_indentation_spaces_wanted ? : 30 1 31
4692 ( $avail > $extra_indentation_spaces_wanted )
4693 ? $extra_indentation_spaces_wanted
4694 : $avail;
4695  
4696 #--------------------------------------------------------
4697 # Note: min spaces can be negative; for example with -gnu
4698 # f(
4699 # do { 1; !!(my $x = bless []); }
4700 # );
4701 #--------------------------------------------------------
4702 # The following rule is needed to match older formatting:
4703 # For multiple groups, we will keep spaces non-negative.
4704 # For a single group, we will allow a negative space.
4705 31 ▶ if ( $ngroups > 1 && $spaces < 0 ) { $spaces = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ngroups > 1 and $spaces < 0) 0 31 31
Condition66%2/3
$ngroups > 1 and $spaces < 0
!ll&&!rl&&r
4270
MC/DC0%0/2
$ngroups > 1 and $spaces < 0
$ngroups > 1 $spaces < 0
Truth table66%2/3
A and B
A= $ngroups > 1B= $spaces < 0
ABresult
0X0
100
111
4706  
4707 # update the minimum spacing
4708 31 ▶ if ( $ng == 0 || $spaces < $extra_leading_spaces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ng == 0 or $spaces < $extra_leading_spaces) 13 18 31
Condition66%2/3
$ng == 0 or $spaces < $extra_leading_spaces
l!l&&r!l&&!r
13018
MC/DC50%1/2
$ng == 0 or $spaces < $extra_leading_spaces
$ng == 0 $spaces < $extra_leading_spaces
Truth table66%2/3
A or B
A= $ng == 0B= $spaces < $extra_leading_spaces
ABresult
1X1
011
000
4709 13 $extra_leading_spaces = $spaces;
4710 }
4711 }
4712  
4713 # update the indentation object because with -icp the terminal
4714 # ');' will use the same adjustment.
4715 12 $object->permanently_decrease_available_spaces( -$extra_leading_spaces );
4716 12 return $extra_leading_spaces;
4717 } ## end sub get_extra_leading_spaces
4718  
4719 sub forget_side_comment {
4720 124 ▶ my ($self) = @_;
Subroutine forget_side_comment: called CC=1 SCAR=0.0
Pod undocumented
4721 124 $self->[_last_side_comment_column_] = 0;
4722 124 return;
4723 }
4724  
4725 sub is_good_side_comment_column {
4726  
4727 222 ▶ my ( $self, $line, $line_number, $level, $num5 ) = @_;
Subroutine is_good_side_comment_column: called CC=10 SCAR=23.2
Pod undocumented
4728  
4729 # Upon encountering the first side comment of a group, decide if
4730 # a previous side comment should be forgotten. This involves
4731 # checking several rules.
4732  
4733 # Given:
4734 # $line = ref to info hash for the line of interest
4735 # $line_number = number of this line in the output stream
4736 # $level = indentation level of this line
4737 # $num5 = ..see comments below
4738  
4739 # Return:
4740 # true to KEEP old comment location
4741 # false to FORGET old comment location
4742 222 my $KEEP = 1;
4743 222 my $FORGET = 0;
4744  
4745 222 my $rfields = $line->{'rfields'};
4746 222 my $is_hanging_side_comment = $line->{'is_hanging_side_comment'};
4747  
4748 # RULE1: Never forget comment before a hanging side comment
4749 222 ▶ return $KEEP if ($is_hanging_side_comment);
Branch: 1 branch
BranchTrueFalseTotal
if $is_hanging_side_comment 12 210 222
4750  
4751 # RULE2: Forget a side comment after a short line difference,
4752 # where 'short line difference' is computed from a formula.
4753 # Using a smooth formula helps minimize sudden large changes.
4754 210 my $line_diff = $line_number - $self->[_last_side_comment_line_number_];
4755 210 my $alev_diff = abs( $level - $self->[_last_side_comment_level_] );
4756  
4757 # '$num5' is the number of comments in the first 5 lines after the first
4758 # comment. It is needed to keep a compact group of side comments from
4759 # being influenced by a more distant side comment.
4760 210 ▶ $num5 = 1 if ( !$num5 );
Branch: 1 branch
BranchTrueFalseTotal
unless $num5 0 210 210
4761  
4762 # Some values:
4763  
4764 # $adiff $num5 $short_diff
4765 # 0 * 12
4766 # 1 1 6
4767 # 1 2 4
4768 # 1 3 3
4769 # 1 4 2
4770 # 2 1 4
4771 # 2 2 2
4772 # 2 3 1
4773 # 3 1 3
4774 # 3 2 1
4775  
4776 210 my $short_diff = SC_LONG_LINE_DIFF / ( 1 + $alev_diff * $num5 );
4777  
4778 210 ▶ return $FORGET
Branch: 1 branch
BranchTrueFalseTotal
if $line_diff > $short_diff or not $rOpts_valign_side_comments 82 128 210
Condition100%3/3
$line_diff > $short_diff or not $rOpts_valign_side_comments
l!l&&r!l&&!r
757128
MC/DC100%2/2
$line_diff > $short_diff or not $rOpts_valign_side_comments
$line_diff > $short_diff not $rOpts_valign_side_comments
Truth table100%3/3
A or not(B)
A= $line_diff > $short_diffB= not $rOpts_valign_side_comments
ABresult
1X1
011
000
4779 if ( $line_diff > $short_diff
4780 || !$rOpts_valign_side_comments );
4781  
4782 # RULE3: Forget a side comment if this line is at lower level and
4783 # ends a block
4784 128 my $last_sc_level = $self->[_last_side_comment_level_];
4785 return $FORGET
4786 if ( $level < $last_sc_level
4787 128 ▶ && $is_closing_block_type{ substr( $rfields->[0], 0, 1 ) } );
Branch: 1 branch
BranchTrueFalseTotal
if $level < $last_sc_level and $is_closing_block_type{substr $rfields->[0], 0, 1} 18 110 128
Condition100%3/3
$level < $last_sc_level and $is_closing_block_type{substr $rfields->[0], 0, 1}
!ll&&!rl&&r
105518
MC/DC100%2/2
$level < $last_sc_level and $is_closing_block_type{substr $rfields->[0], 0, 1}
$level < $last_sc_level $is_closing_block_type{substr $rfields->[0], 0, 1}
Truth table100%3/3
A and B
A= $level < $last_sc_levelB= $is_closing_block_type{substr $rfields->[0], 0, 1}
ABresult
0X0
100
111
4788  
4789 # RULE 4: Forget the last side comment if this comment might join a cached
4790 # line ...
4791 110 ▶ if ( my $cached_line_type = get_cached_line_type() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached_line_type = get_cached_line_type()) 4 106 110
4792  
4793 # ... otherwise side comment alignment will get messed up.
4794 # For example, in the following test script
4795 # with using 'perltidy -sct -act=2', the last comment would try to
4796 # align with the previous and then be in the wrong column when
4797 # the lines are combined:
4798  
4799 # foreach $line (
4800 # [0, 1, 2], [3, 4, 5], [6, 7, 8], # rows
4801 # [0, 3, 6], [1, 4, 7], [2, 5, 8], # columns
4802 # [0, 4, 8], [2, 4, 6]
4803 # ) # diagonals
4804 4 ▶ return $FORGET
Branch: 1 branch
BranchTrueFalseTotal
if $cached_line_type == 2 or $cached_line_type == 4 0 4 4
Condition33%1/3
$cached_line_type == 2 or $cached_line_type == 4
l!l&&r!l&&!r
004
MC/DC0%0/2
$cached_line_type == 2 or $cached_line_type == 4
$cached_line_type == 2 $cached_line_type == 4
Truth table33%1/3
A or B
A= $cached_line_type == 2B= $cached_line_type == 4
ABresult
1X1
011
000
4805 if ( $cached_line_type == 2 || $cached_line_type == 4 );
4806 }
4807  
4808 # Otherwise, keep it alive
4809 110 return $KEEP;
4810 } ## end sub is_good_side_comment_column
4811  
4812 sub align_side_comments {
4813  
4814 222 ▶ my ( $self, $rlines, $rgroups ) = @_;
Subroutine align_side_comments: called CC=33 SCAR=35.0
Pod undocumented
4815  
4816 # Align any side comments in this batch of lines
4817  
4818 # Given:
4819 # $rlines - the lines
4820 # $rgroups - the partition of the lines into groups
4821 #
4822 # We will be working group-by-group because all side comments
4823 # (real or fake) in each group are already aligned. So we just have
4824 # to make alignments between groups wherever possible.
4825  
4826 # An unusual aspect is that within each group we have aligned both real
4827 # and fake side comments. This has the consequence that the lengths of
4828 # long lines without real side comments can cause 'push' all side comments
4829 # to the right. This seems unusual, but testing with and without this
4830 # feature shows that it is usually better this way. Otherwise, side
4831 # comments can be hidden between long lines without side comments and
4832 # thus be harder to read.
4833  
4834 222 my $group_level = $self->[_group_level_];
4835 222 ▶ my $continuing_sc_flow = $self->[_last_side_comment_length_] > 0
Condition100%3/3
$self->[7] > 0 && $group_level == $self->[4]
!ll&&!rl&&r
1644315
MC/DC100%2/2
$self->[7] > 0 && $group_level == $self->[4]
$self->[7] > 0 $group_level == $self->[4]
Truth table100%3/3
A && B
A= $self->[7] > 0B= $group_level == $self->[4]
ABresult
0X0
100
111
4836 && $group_level == $self->[_last_level_written_];
4837  
4838 # Find groups with side comments, and remember the first nonblank comment
4839 222 my $j_sc_beg;
4840 my @todo;
4841 222 my $ng = -1;
4842 222 foreach my $item ( @{$rgroups} ) {
4843 348 $ng++;
4844 348 my ( $jbeg, $jend ) = @{$item};
4845 348 foreach my $j ( $jbeg .. $jend ) {
4846 403 my $line = $rlines->[$j];
4847 403 my $jmax = $line->{'jmax'};
4848 403 ▶ if ( $line->{'rfield_lengths'}->[$jmax] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"rfield_lengths"}[$jmax]) 251 152 403
4849  
4850 # this group has a line with a side comment
4851 251 push @todo, $ng;
4852 251 ▶ if ( !defined($j_sc_beg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $j_sc_beg) 222 29 251
4853 222 $j_sc_beg = $j;
4854 }
4855 251 last;
4856 }
4857 }
4858 }
4859  
4860 # done if no groups with side comments
4861 222 ▶ return unless (@todo);
Branch: 1 branch
BranchTrueFalseTotal
unless @todo 0 222 222
4862  
4863 # Count $num5 = number of comments in the 5 lines after the first comment
4864 # This is an important factor in a decision formula
4865 222 my $num5 = 1;
4866 222 foreach my $jj ( $j_sc_beg + 1 .. @{$rlines} - 1 ) {
4867 210 my $ldiff = $jj - $j_sc_beg;
4868 210 ▶ last if ( $ldiff > 5 );
Branch: 1 branch
BranchTrueFalseTotal
if $ldiff > 5 6 204 210
4869 204 my $line = $rlines->[$jj];
4870 204 my $jmax = $line->{'jmax'};
4871 204 my $sc_len = $line->{'rfield_lengths'}->[$jmax];
4872 204 ▶ next if ( !$sc_len );
Branch: 1 branch
BranchTrueFalseTotal
unless $sc_len 83 121 204
4873 121 $num5++;
4874 }
4875  
4876 # Forget the old side comment location if necessary
4877 222 my $line_0 = $rlines->[$j_sc_beg];
4878 222 my $lnum =
4879 $j_sc_beg + $self->[_file_writer_object_]->get_output_line_number();
4880 222 my $keep_it =
4881 $self->is_good_side_comment_column( $line_0, $lnum, $group_level, $num5 );
4882 222 ▶ my $last_side_comment_column =
Branch: 1 branch
BranchTrueFalseTotal
$keep_it ? : 122 100 222
4883 $keep_it ? $self->[_last_side_comment_column_] : 0;
4884  
4885 # If there are multiple groups we will do two passes
4886 # so that we can find a common alignment for all groups.
4887 222 ▶ my $MAX_PASS = @todo > 1 ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@todo > 1 ? : 24 198 222
4888  
4889 # Loop over passes
4890 222 my $max_comment_column = $last_side_comment_column;
4891 222 foreach my $PASS ( 1 .. $MAX_PASS ) {
4892  
4893 # If there are two passes, then on the last pass make the old column
4894 # equal to the largest of the group. This will result in the comments
4895 # being aligned if possible.
4896 246 ▶ if ( $PASS == $MAX_PASS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PASS == $MAX_PASS) 222 24 246
4897 222 $last_side_comment_column = $max_comment_column;
4898 }
4899  
4900 # Loop over the groups with side comments
4901 246 my $column_limit;
4902 246 foreach my $ngr (@todo) {
4903 304 my ( $jbeg, $jend_uu ) = @{ $rgroups->[$ngr] };
4904  
4905 # Note that since all lines in a group have common alignments, we
4906 # just have to work on one of the lines (the first line).
4907 304 my $line = $rlines->[$jbeg];
4908 304 my $jmax = $line->{'jmax'};
4909 304 my $is_hanging_side_comment = $line->{'is_hanging_side_comment'};
4910 last
4911 304 ▶ if ( $PASS < $MAX_PASS && $is_hanging_side_comment );
Branch: 1 branch
BranchTrueFalseTotal
if $PASS < $MAX_PASS and $is_hanging_side_comment 4 300 304
Condition100%3/3
$PASS < $MAX_PASS and $is_hanging_side_comment
!ll&&!rl&&r
251494
MC/DC100%2/2
$PASS < $MAX_PASS and $is_hanging_side_comment
$PASS < $MAX_PASS $is_hanging_side_comment
Truth table100%3/3
A and B
A= $PASS < $MAX_PASSB= $is_hanging_side_comment
ABresult
0X0
100
111
4912  
4913 # the maximum space without exceeding the line length:
4914 300 my $avail = $line->get_available_space_on_right();
4915  
4916 # try to use the previous comment column
4917 300 my $side_comment_column = $line->get_column( $jmax - 1 );
4918 300 my $move = $last_side_comment_column - $side_comment_column;
4919  
4920 # Remember the maximum possible column of the first line with
4921 # side comment
4922 300 ▶ if ( !defined($column_limit) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $column_limit) 246 54 300
4923 246 $column_limit = $side_comment_column + $avail;
4924 }
4925  
4926 300 ▶ next if ( $jmax <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $jmax <= 0 0 300 300
4927  
4928 # but if this doesn't work, give up and use the minimum space
4929 300 my $min_move = $rOpts_minimum_space_to_comment - 1;
4930 300 ▶ if ( $move > $avail ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move > $avail) 13 287 300
4931 13 $move = $min_move;
4932 }
4933  
4934 # but we want some minimum space to the comment
4935 300 ▶ if ( $move >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($move >= 0 and $j_sc_beg == 0 and $continuing_sc_flow) 5 295 300
Condition100%6/6
$move >= 0 and $j_sc_beg == 0 and $continuing_sc_flow
!ll&&!rl&&r
227685
$move >= 0 and $j_sc_beg == 0
!ll&&!rl&&r
2131473
MC/DC100%3/3
$move >= 0 and $j_sc_beg == 0 and $continuing_sc_flow
$move >= 0 $j_sc_beg == 0 $continuing_sc_flow
Truth table100%4/4
A and B and C
A= $move >= 0B= $j_sc_beg == 0C= $continuing_sc_flow
ABCresult
0XX0
10X0
1100
1111
4936 && $j_sc_beg == 0
4937 && $continuing_sc_flow )
4938 {
4939 5 $min_move = 0;
4940 }
4941  
4942 # remove constraints on hanging side comments
4943 300 ▶ if ($is_hanging_side_comment) { $min_move = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hanging_side_comment) 16 284 300
4944  
4945 300 ▶ if ( $move < $min_move ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move < $min_move) 215 85 300
4946 215 $move = $min_move;
4947 }
4948  
4949 # don't exceed the available space
4950 300 ▶ if ( $move > $avail ) { $move = $avail }
Branch: 1 branch
BranchTrueFalseTotal
if ($move > $avail) 11 289 300
4951  
4952 # We can only increase space, never decrease.
4953 300 ▶ if ( $move < 0 ) { $move = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($move < 0) 8 292 300
4954  
4955 # Discover the largest column on the preliminary pass
4956 300 ▶ if ( $PASS < $MAX_PASS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PASS < $MAX_PASS) { } 49 251 300
4957 49 my $col = $line->get_column( $jmax - 1 ) + $move;
4958  
4959 # but ignore columns too large for the starting line
4960 49 ▶ if ( $col > $max_comment_column && $col < $column_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col > $max_comment_column and $col < $column_limit) 23 26 49
Condition66%2/3
$col > $max_comment_column and $col < $column_limit
!ll&&!rl&&r
26023
MC/DC50%1/2
$col > $max_comment_column and $col < $column_limit
$col > $max_comment_column $col < $column_limit
Truth table66%2/3
A and B
A= $col > $max_comment_columnB= $col < $column_limit
ABresult
0X0
100
111
4961 23 $max_comment_column = $col;
4962 }
4963 }
4964  
4965 # Make the changes on the final pass
4966 else {
4967 251 $line->increase_field_width( $jmax - 1, $move );
4968  
4969 # remember this column for the next group
4970 251 $last_side_comment_column = $line->get_column( $jmax - 1 );
4971 }
4972 } ## end loop over groups
4973 } ## end loop over passes
4974  
4975 # Find the last side comment
4976 222 my $j_sc_last;
4977 222 my $ng_last = $todo[-1];
4978 222 my ( $jbeg, $jend ) = @{ $rgroups->[$ng_last] };
4979 222 foreach my $jj ( reverse( $jbeg .. $jend ) ) {
4980 225 my $line = $rlines->[$jj];
4981 225 my $jmax = $line->{'jmax'};
4982 225 ▶ if ( $line->{'rfield_lengths'}->[$jmax] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"rfield_lengths"}[$jmax]) 222 3 225
4983 222 $j_sc_last = $jj;
4984 222 last;
4985 }
4986 }
4987  
4988 # Save final side comment info for possible use by the next batch
4989 222 ▶ if ( defined($j_sc_last) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $j_sc_last) 222 0 222
4990 222 my $line_number =
4991 $self->[_file_writer_object_]->get_output_line_number() + $j_sc_last;
4992 222 $self->[_last_side_comment_column_] = $last_side_comment_column;
4993 222 $self->[_last_side_comment_line_number_] = $line_number;
4994 222 $self->[_last_side_comment_level_] = $group_level;
4995 }
4996 222 return;
4997 } ## end sub align_side_comments
4998  
4999 ###########################################
5000 # CODE SECTION 6: Pad Signed Number Columns
5001 ###########################################
5002  
5003 45 ▶use constant DEBUG_VSN => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5004  
5005 45 my %is_leading_sign_pattern;
5006  
5007 BEGIN {
5008  
5009 # PATTERNS: A pattern is basically the concatenation of all token types in
5010 # the field, with keywords converted to their actual text. The formatter
5011 # has changed things like 'print' to 'priNt' so that all 'n's are numbers.
5012 # The following patterns 'n' can match a signed number of interest.
5013 # Thus 'n'=a signed or unsigned number, 'b'=a space, '}'=one of ) ] }
5014 $is_leading_sign_pattern{$_} = 1
5015 45 ▶ for ( 'n,', 'n,b', 'nb', 'nb}', 'nb},', 'n},', 'n};' );
Subroutine BEGIN: called CC=2 SCAR=6.9
5016 }
5017  
5018 sub min_max_median {
5019  
5020 42 ▶ my ($rvalues) = @_;
Subroutine min_max_median: called CC=3 SCAR=11.0
Pod undocumented
5021  
5022 # Given: $rvalues = ref to an array of numbers
5023 # Return: the min, max, and median
5024 42 my $num = @{$rvalues};
5025 42 ▶ return unless ($num);
Branch: 1 branch
BranchTrueFalseTotal
unless $num 0 42 42
5026  
5027 42 my @sorted = sort { $a <=> $b } @{$rvalues};
5028  
5029 42 my $min = $sorted[0];
5030 42 my $max = $sorted[-1];
5031 42 my $imid = int( $num / 2 );
5032 42 ▶ my $median =
Branch: 1 branch
BranchTrueFalseTotal
@sorted % 2 ? : 26 16 42
5033 @sorted % 2
5034 ? $sorted[$imid]
5035 : ( $sorted[ $imid - 1 ] + $sorted[$imid] ) / 2;
5036  
5037 42 return ( $min, $max, $median );
5038 } ## end sub min_max_median
5039  
5040 sub end_signed_number_column {
5041  
5042 30 ▶ my ( $rgroup_lines, $rcol_hash, $ix_last ) = @_;
Subroutine end_signed_number_column: called CC=46 SCAR=40.2
Pod undocumented
5043  
5044 # Finish formatting a column of unsigned numbers
5045 # Given:
5046 # $rgroup_lines - the current vertical alignment group of lines
5047 # $rcol_hash - a hash of information about this vertical column
5048 # $ix_last - index of the last line of this vertical column
5049 # Task:
5050 # If this is a mixture of signed and unsigned numbers, then add a
5051 # single space before the unsigned numbers to improve appearance.
5052 30 ▶ return unless ($rcol_hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $rcol_hash 0 30 30
5053 30 my $jcol = $rcol_hash->{jcol};
5054 30 my $unsigned = $rcol_hash->{unsigned_count};
5055 30 my $signed = $rcol_hash->{signed_count};
5056 30 my $rsigned_lines = $rcol_hash->{rsigned_lines};
5057  
5058 30 ▶ if ( !$signed && $unsigned ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $signed and $unsigned) 0 30 30
Condition33%1/3
not $signed and $unsigned
!ll&&!rl&&r
3000
MC/DC0%0/2
not $signed and $unsigned
not $signed $unsigned
Truth table33%1/3
not(A) and B
A= not $signedB= $unsigned
ABresult
0X0
100
111
5059 0 DEVEL_MODE
5060 && Fault("avoid calling without mixed signed and unsigned\n");
5061 0 return;
5062 }
5063  
5064 30 my $pos_start_number = $rcol_hash->{pos_start_number};
5065 30 my $char_end_part1 = $rcol_hash->{char_end_part1};
5066 30 my $ix_first = $rcol_hash->{ix_first};
5067 30 my $nlines = $ix_last - $ix_first + 1;
5068  
5069 # check for skipped lines, shouldn't happen
5070 30 ▶ if ( $signed + $unsigned != $nlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signed + $unsigned != $nlines) 0 30 30
5071 0 my $line = $rgroup_lines->[$ix_last];
5072 0 my $rfields = $line->{'rfields'};
5073 0 my $text = join( EMPTY_STRING, @{$rfields} );
5074 0 DEVEL_MODE && Fault(<<EOM);
5075 We seem to have miscounted lines, please check:
5076 signed=$signed
5077 j=$jcol
5078 unsigned=$unsigned
5079 ix_first=$ix_first
5080 ix_last=$ix_last
5081 nlines=$nlines
5082 text=$text
5083 EOM
5084 0 return;
5085 }
5086  
5087 #-----------------------------------------------------------------
5088 # Form groups of unsigned numbers from the list of signed numbers.
5089 #-----------------------------------------------------------------
5090 30 my @unsigned_subgroups;
5091 30 my $ix_last_negative = $ix_first - 1;
5092 30 my %is_signed;
5093 30 foreach my $ix ( @{$rsigned_lines} ) {
5094 45 $is_signed{$ix} = 1;
5095 45 my $Nu = $ix - $ix_last_negative - 1;
5096 45 ▶ if ( $Nu > 0 && $Nu <= $rOpts_valign_signed_numbers_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Nu > 0 and $Nu <= $rOpts_valign_signed_numbers_limit) 18 27 45
Condition100%3/3
$Nu > 0 and $Nu <= $rOpts_valign_signed_numbers_limit
!ll&&!rl&&r
20718
MC/DC100%2/2
$Nu > 0 and $Nu <= $rOpts_valign_signed_numbers_limit
$Nu > 0 $Nu <= $rOpts_valign_signed_numbers_limit
Truth table100%3/3
A and B
A= $Nu > 0B= $Nu <= $rOpts_valign_signed_numbers_limit
ABresult
0X0
100
111
5097 18 push @unsigned_subgroups, [ $ix_last_negative + 1, $ix - 1 ];
5098 }
5099 45 $ix_last_negative = $ix;
5100 }
5101  
5102 # Exclude groups with more than about 20 consecutive numbers. Little
5103 # visual improvement is gained by padding more than this, and this avoids
5104 # large numbers of differences in a file when a single line is changed.
5105 30 my $Nu = $ix_last - $ix_last_negative;
5106 30 ▶ if ( $Nu > 0 && $Nu <= $rOpts_valign_signed_numbers_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Nu > 0 and $Nu <= $rOpts_valign_signed_numbers_limit) 18 12 30
Condition100%3/3
$Nu > 0 and $Nu <= $rOpts_valign_signed_numbers_limit
!ll&&!rl&&r
4818
MC/DC100%2/2
$Nu > 0 and $Nu <= $rOpts_valign_signed_numbers_limit
$Nu > 0 $Nu <= $rOpts_valign_signed_numbers_limit
Truth table100%3/3
A and B
A= $Nu > 0B= $Nu <= $rOpts_valign_signed_numbers_limit
ABresult
0X0
100
111
5107 18 push @unsigned_subgroups, [ $ix_last_negative + 1, $ix_last ];
5108 }
5109  
5110 30 ▶ if ( !@unsigned_subgroups ) { return } # shouldn't happen
Branch: 1 branch
BranchTrueFalseTotal
unless (@unsigned_subgroups) 9 21 30
5111  
5112 #--------------------------------------------
5113 # Find number lengths for irregularity checks
5114 #--------------------------------------------
5115 # Padding signed numbers looks best when the numbers, excluding signs,
5116 # all have about the same length. When the lengths are irregular, with
5117 # mostly longer unsigned numbers, it doesn't look good to do this. So
5118 # we need to filter out these bad-looking cases.
5119  
5120 # The 'field_lengths' are unreliable because they may include some
5121 # arbitrary trailing text; see 'substr.t' So we must look for the end of
5122 # the number at a space, comma, or closing container token. Note that these
5123 # lengths include the length of any signs.
5124 21 my @len_unsigned;
5125 my @len_signed;
5126 21 my @lengths;
5127 21 foreach my $ix ( $ix_first .. $ix_last ) {
5128 102 my $line = $rgroup_lines->[$ix];
5129 102 my $rfield = $line->{'rfields'};
5130 102 my $str = substr( $rfield->[$jcol], $pos_start_number );
5131 102 ▶ if ( $str =~ /^([^\s\,\)\]\}]*)/ ) { $str = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([^\s\,\)\]\}]*)/) 102 0 102
5132 102 my $len = length($str);
5133 102 ▶ if ( $is_signed{$ix} ) { push @len_signed, $len }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_signed{$ix}) { } 33 69 102
5134 69 else { push @len_unsigned, $len }
5135 102 push @lengths, [ $len, $ix ];
5136 }
5137  
5138 21 my ( $min_unsigned_length, $max_unsigned_length, $median_unsigned_length )
5139 = min_max_median( \@len_unsigned );
5140 21 my ( $min_signed_length_uu, $max_signed_length, $median_signed_length ) =
5141 min_max_median( \@len_signed );
5142  
5143 # Skip padding if no signed numbers exceed unsigned numbers in length
5144 21 ▶ if ( $max_signed_length <= $min_unsigned_length ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max_signed_length <= $min_unsigned_length) { } 0 21 21
elsif ($max_signed_length > $max_unsigned_length) { } 10 11 21
5145 0 return;
5146 }
5147  
5148 # If max signed length is greatest - all unsigned values can be padded
5149 elsif ( $max_signed_length > $max_unsigned_length ) {
5150  
5151 # Example:
5152 # %wind_dir = (
5153 # 'n' => [ 1, 0 ],
5154 # 'ne' => [ 1, 1 ],
5155 # 'e' => [ 0, 1 ],
5156 # 'se' => [ -1, 1 ],
5157 # 's' => [ -1, 0 ],
5158 # 'sw' => [ -1, -1 ],
5159 # 'w' => [ 0, -1 ],
5160 # 'nw' => [ 1, -1 ],
5161 # '' => [ 0, 0 ],
5162 # );
5163  
5164 # This is the ideal case - ok to continue and pad
5165 }
5166  
5167 # intermediate case: some signed numbers cannot be padded ...
5168 else {
5169  
5170 # We have to take a closer look.
5171 # Here is an example which looks bad if we do padding like this:
5172 # my %hash = (
5173 # X0 => -12867.098241163,
5174 # X1 => 2.31694338671684, # unsigned w/ excess>0
5175 # X2 => 0.0597726714860419, # max length => excess=0
5176 # Y0 => 30043.1335503155, # unsigned w/ excess>0
5177 # Y1 => 0.0525784981597044, # max length => excess=0
5178 # Y2 => -2.32447131600783,
5179 # );
5180  
5181 # To decide what looks okay, we count 'good' and 'bad' line interfaces:
5182 # X0 - X1 = good (X0 is signed and X1 can move)
5183 # X1 - X2 = bad (x1 can move but x2 cannot)
5184 # X2 - Y0 = bad (x2 cannot move but Y0 can move)
5185 # Y0 - Y1 = bad (Y0 can move but Y1 cannot move)
5186 # Y1 - Y2 = bad (Y1 cannot move and Y2 is signed)
5187 # Result: 4 bad interfaces and 1 good => so we will skip this
5188 11 my $good_count = 0;
5189 11 my $bad_count = 0;
5190 11 foreach my $item (@lengths) {
5191 54 $item->[0] = $max_unsigned_length - $item->[0];
5192 }
5193 11 my $item0 = shift @lengths;
5194 11 my ( $excess, $ix ) = @{$item0};
5195 11 ▶ my $immobile_count = $excess ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$excess ? : 11 0 11
5196 11 foreach my $item (@lengths) {
5197 43 my $excess_m = $excess;
5198 43 my $ix_m = $ix;
5199 43 ( $excess, $ix ) = @{$item};
5200 43 ▶ if ( !$excess ) { $immobile_count++ }
Branch: 1 branch
BranchTrueFalseTotal
unless ($excess) 13 30 43
5201  
5202 43 ▶ if ( $is_signed{$ix_m} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_signed{$ix_m}) { } 15 28 43
5203  
5204 # signed-unsigned interface
5205 15 ▶ if ( !$is_signed{$ix} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_signed{$ix}) 13 2 15
5206 13 ▶ if ($excess) { $good_count++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($excess) { } 8 5 13
5207 5 else { $bad_count++ }
5208 }
5209  
5210 # signed-signed: ok, not good or bad
5211 }
5212 else {
5213  
5214 # unsigned-signed interface
5215 28 ▶ if ( $is_signed{$ix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_signed{$ix}) { } 9 19 28
5216 9 ▶ if ($excess_m) { $good_count++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($excess_m) { } 9 0 9
5217 0 else { $bad_count++ }
5218 }
5219  
5220 # unsigned-unsigned: bad if different
5221 else {
5222 19 ▶ if ( $excess_m xor $excess ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess_m xor $excess) 7 12 19
Condition100%4/4
$excess_m xor $excess
l&&rl&&!r!l&&r!l&&!r
10612
MC/DC100%2/2
$excess_m xor $excess
$excess_m $excess
Truth table100%4/4
A xor B
A= $excess_mB= $excess
ABresult
110
101
011
000
5223 7 $bad_count++;
5224 }
5225 }
5226 }
5227 }
5228  
5229 # Filter 1: skip if more interfaces are 'bad' than 'good'
5230 11 ▶ if ( $bad_count > $good_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad_count > $good_count) 1 10 11
5231 1 return;
5232 }
5233  
5234 # Filter 2: skip in a table with multiple 'bad' interfaces and where
5235 # 'most' of the unsigned lengths are shorter than the signed lengths.
5236 # Using the median value makes this insensitive to small changes.
5237 10 ▶ if ( $median_unsigned_length >= $median_signed_length
Branch: 1 branch
BranchTrueFalseTotal
if ($median_unsigned_length >= $median_signed_length and $bad_count > 1 and $immobile_count > 1) 0 10 10
Condition50%3/6
$median_unsigned_length >= $median_signed_length and $bad_count > 1 and $immobile_count > 1
!ll&&!rl&&r
1000
$median_unsigned_length >= $median_signed_length and $bad_count > 1
!ll&&!rl&&r
460
MC/DC0%0/3
$median_unsigned_length >= $median_signed_length and $bad_count > 1 and $immobile_count > 1
$median_unsigned_length >= $median_signed_length $bad_count > 1 $immobile_count > 1
Truth table50%2/4
A and B and C
A= $median_unsigned_length >= $median_signed_lengthB= $bad_count > 1C= $immobile_count > 1
ABCresult
0XX0
10X0
1100
1111
5238 && $bad_count > 1
5239 && $immobile_count > 1 )
5240 {
5241 0 return;
5242 }
5243  
5244 # Anything that gets past these filters should look ok if padded
5245 }
5246  
5247 #---------------------------------------------
5248 # Compute actual available space for each line
5249 #---------------------------------------------
5250 20 my %excess_space;
5251 20 my $movable_count = 0;
5252 20 foreach my $item (@unsigned_subgroups) {
5253 34 my ( $ix_min, $ix_max ) = @{$item};
5254 34 foreach my $ix ( $ix_min .. $ix_max ) {
5255 56 my $line = $rgroup_lines->[$ix];
5256 56 my $leading_space_count = $line->{'leading_space_count'};
5257 56 my $jmax = $line->{'jmax'};
5258 56 my $rfield_lengths = $line->{'rfield_lengths'};
5259 56 ▶ if ( $jcol >= $jmax ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jcol >= $jmax) 0 56 56
5260  
5261 # shouldn't happen
5262 0 DEVEL_MODE && Fault("jcol=$jcol >= jmax=$jmax\n");
5263 0 return;
5264 }
5265 56 my @alignments = @{ $line->{'ralignments'} };
5266 56 my $col = $alignments[$jcol]->{'column'};
5267 my $col_start =
5268 $jcol == 0
5269 ? $leading_space_count
5270 56 ▶ : $alignments[ $jcol - 1 ]->{'column'};
Branch: 1 branch
BranchTrueFalseTotal
$jcol == 0 ? : 16 40 56
5271 56 my $avail = $col - $col_start;
5272 56 my $field_length = $rfield_lengths->[$jcol];
5273 56 my $excess = $avail - $field_length;
5274 56 $excess_space{$ix} = $excess;
5275 56 ▶ if ( $excess > 0 ) { $movable_count++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($excess > 0) 56 0 56
5276 }
5277 }
5278  
5279 20 ▶ return unless ($movable_count);
Branch: 1 branch
BranchTrueFalseTotal
unless $movable_count 0 20 20
5280  
5281 # Count the number of signed-unsigned interfaces that would change
5282 # if we do the padding
5283 20 my $Nc = 0;
5284 20 foreach my $item (@unsigned_subgroups) {
5285 34 my ( $ix_min, $ix_max ) = @{$item};
5286 34 ▶ $Nc++ if ( $excess_space{$ix_min} > 0 && $ix_min != $ix_first );
Branch: 1 branch
BranchTrueFalseTotal
if $excess_space{$ix_min} > 0 and $ix_min != $ix_first 22 12 34
Condition66%2/3
$excess_space{$ix_min} > 0 and $ix_min != $ix_first
!ll&&!rl&&r
01222
MC/DC50%1/2
$excess_space{$ix_min} > 0 and $ix_min != $ix_first
$excess_space{$ix_min} > 0 $ix_min != $ix_first
Truth table66%2/3
A and B
A= $excess_space{$ix_min} > 0B= $ix_min != $ix_first
ABresult
0X0
100
111
5287 34 ▶ $Nc++ if ( $excess_space{$ix_max} > 0 && $ix_max != $ix_last );
Branch: 1 branch
BranchTrueFalseTotal
if $excess_space{$ix_max} > 0 and $ix_max != $ix_last 17 17 34
Condition66%2/3
$excess_space{$ix_max} > 0 and $ix_max != $ix_last
!ll&&!rl&&r
01717
MC/DC50%1/2
$excess_space{$ix_max} > 0 and $ix_max != $ix_last
$excess_space{$ix_max} > 0 $ix_max != $ix_last
Truth table66%2/3
A and B
A= $excess_space{$ix_max} > 0B= $ix_max != $ix_last
ABresult
0X0
100
111
5288 }
5289  
5290 #--------------------------------------------------------------------
5291 # Sparsity check:
5292 # Give up if the number of interface changes will be below the cutoff
5293 #--------------------------------------------------------------------
5294 20 ▶ if ( $unsigned > $Nc * $rOpts_valign_signed_numbers_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsigned > $Nc * $rOpts_valign_signed_numbers_limit) 0 20 20
5295 0 return;
5296 }
5297  
5298 #------------------------------------------------------------------------
5299 # Insert an extra space before the unsigned numbers if space is available
5300 #------------------------------------------------------------------------
5301 20 foreach my $item (@unsigned_subgroups) {
5302 34 my ( $ix_min, $ix_max ) = @{$item};
5303  
5304 34 foreach my $ix ( $ix_min .. $ix_max ) {
5305 56 ▶ next if ( $excess_space{$ix} <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $excess_space{$ix} <= 0 0 56 56
5306 56 my $line = $rgroup_lines->[$ix];
5307 56 my $rfields = $line->{'rfields'};
5308 56 my $rfield_lengths = $line->{'rfield_lengths'};
5309 56 pad_signed_field(
5310 \$rfields->[$jcol], \$rfield_lengths->[$jcol],
5311 $pos_start_number, $char_end_part1
5312 );
5313 }
5314 }
5315 20 return;
5316 } ## end sub end_signed_number_column
5317  
5318 sub pad_signed_field {
5319  
5320 56 ▶ my ( $rstr, $rstr_len, $pos_start_number, $char_end_part1 ) = @_;
Subroutine pad_signed_field: called CC=5 SCAR=16.8
Pod undocumented
5321  
5322 # Insert an extra space before a number to highlight algebraic signs
5323 # in a column of numbers.
5324 # Given:
5325 # $rstr = ref to string
5326 # $rstr_len = ref to display width of string (could include wide chars)
5327 # $pos_start_number = string position of the leading digit
5328 # $char_end_part1 = character at $pos_start_number - 1
5329 # Task: update $rstr and $rstr_len with a single space
5330  
5331 # First partition the string into $part1 and $part2, so that the
5332 # number starts at the beginning of part2.
5333 56 my $part1 = EMPTY_STRING;
5334 56 my $part2 = ${$rstr};
5335 56 my $str = ${$rstr};
5336 56 ▶ if ( $pos_start_number > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_start_number > 0) 16 40 56
5337 16 my $len = length($str);
5338 16 ▶ if ( $pos_start_number >= $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_start_number >= $len) 0 16 16
5339 0 DEVEL_MODE && Fault(<<EOM);
5340 Expection position '$pos_start_number' < length $len of string '$str'
5341 EOM
5342 0 return;
5343 }
5344 16 $part1 = substr( $str, 0, $pos_start_number );
5345 16 $part2 = substr( $str, $pos_start_number );
5346  
5347 # VERIFY that we are inserting a new space after either
5348 # (1) an existing space or
5349 # (2) an opening token.
5350 # Otherwise disaster can occur. An error here implies a programming
5351 # error in defining '$pos_start_number'.
5352  
5353 16 my $test_char1 = substr( $part1, -1, 1 );
5354 16 ▶ if ( $test_char1 ne $char_end_part1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_char1 ne $char_end_part1) 0 16 16
5355 0 DEVEL_MODE && Fault(<<EOM);
5356 Expecting '$char_end_part1' but saw '$test_char1' in string '$str'
5357 Probably bad position '$pos_start_number'
5358 EOM
5359 0 return;
5360 }
5361 }
5362  
5363 # VERIFY we are inserting a space before a digit character
5364 56 my $test_char2 = substr( $part2, 0, 1 );
5365 56 ▶ if ( $is_digit_char{$test_char2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_digit_char{$test_char2}) { } 56 0 56
5366 56 ${$rstr} = $part1 . SPACE . $part2;
5367 56 ${$rstr_len} += 1;
5368 }
5369 else {
5370 0 DEVEL_MODE && Fault(<<EOM);
5371 Expecting test char2 as leading digit but saw '$test_char2' in string '$str'
5372 May be bad position '$pos_start_number'
5373 EOM
5374 }
5375 56 return;
5376 } ## end sub pad_signed_field
5377  
5378 sub split_field {
5379  
5380 124 ▶ my ( $pat1, $field, $pattern ) = @_;
Subroutine split_field: called CC=27 SCAR=35.5
Pod undocumented
5381  
5382 # Given;
5383 # $pat1 = first part of a pattern before a numeric type 'n'
5384 # $field = corresponding text field
5385 # $pattern = full pattern
5386 # Return:
5387 # $pos_start_number = position in $field where the number should start
5388 # = 0 if cannot find
5389 # $char_end_part1 = the character preceding $pos_start_number
5390 # $ch_opening = the preceding opening container character, if any
5391  
5392 # We have to find where the possible number starts in this field.
5393 # The safe thing to do is return @fail if anything does not look right.
5394  
5395 124 my $pos_start_number = 0;
5396 124 my $char_end_part1 = EMPTY_STRING;
5397 124 my $ch_opening = EMPTY_STRING;
5398 124 my @fail = ( $pos_start_number, $char_end_part1, $ch_opening );
5399  
5400 # Be sure there is just 'n' in the pattern. Multiple terms can occur
5401 # when fields are joined, but since we are jumping into the middle
5402 # of a field it is safest not to try to handle them.
5403 124 my $n_count = ( $pattern =~ tr/n/n/ );
5404 124 ▶ if ( $n_count && $n_count > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_count and $n_count > 1) 20 104 124
Condition66%2/3
$n_count and $n_count > 1
!ll&&!rl&&r
010420
MC/DC50%1/2
$n_count and $n_count > 1
$n_count $n_count > 1
Truth table66%2/3
A and B
A= $n_countB= $n_count > 1
ABresult
0X0
100
111
5405 20 return @fail;
5406 }
5407  
5408 # Same thing for commas
5409 104 my $comma_count = ( $pattern =~ tr/,/,/ );
5410 104 ▶ if ( $comma_count && $comma_count > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comma_count and $comma_count > 1) 0 104 104
Condition66%2/3
$comma_count and $comma_count > 1
!ll&&!rl&&r
31730
MC/DC0%0/2
$comma_count and $comma_count > 1
$comma_count $comma_count > 1
Truth table66%2/3
A and B
A= $comma_countB= $comma_count > 1
ABresult
0X0
100
111
5411 0 return @fail;
5412 }
5413  
5414 # Require 0 or 1 braces
5415 104 my $len_field = length($field);
5416 104 my $len_pat1 = length($pat1);
5417 104 ▶ return @fail unless ( $len_pat1 && $len_field );
Branch: 1 branch
BranchTrueFalseTotal
unless $len_pat1 and $len_field 0 104 104
Condition33%1/3
$len_pat1 and $len_field
!ll&&!rl&&r
00104
MC/DC0%0/2
$len_pat1 and $len_field
$len_pat1 $len_field
Truth table33%1/3
A and B
A= $len_pat1B= $len_field
ABresult
0X0
100
111
5418  
5419 # Look at the pattern ending
5420 104 my $ending_b = 0;
5421 104 my $ch = substr( $pat1, -1, 1 );
5422 104 ▶ if ( $ch eq 'b' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "b") 79 25 104
5423 79 $ending_b = 1;
5424 79 $ch = substr( $pat1, -2, 1 );
5425 79 $char_end_part1 = SPACE;
5426 }
5427  
5428 # handle either '{b' or '{'
5429 104 ▶ if ( $ch eq '{' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq '{') { } 56 48 104
5430  
5431 # Only one brace
5432 56 my $brace_count = ( $pat1 =~ tr/\{/\{/ );
5433 56 ▶ return @fail if ( $brace_count != 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $brace_count != 1 0 56 56
5434  
5435 56 my $i_paren = index( $field, '(' );
5436 56 my $i_bracket = index( $field, '[' );
5437 56 my $i_brace = index( $field, '{' );
5438 56 my $i_opening = length($field);
5439 56 ▶ if ( $i_paren >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_paren >= 0) 18 38 56
5440 18 $i_opening = $i_paren;
5441 18 $ch_opening = '(';
5442 }
5443 56 ▶ if ( $i_bracket >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($i_bracket >= 0 and $i_bracket < $i_opening) 38 18 56
Condition66%2/3
$i_bracket >= 0 and $i_bracket < $i_opening
!ll&&!rl&&r
18038
MC/DC50%1/2
$i_bracket >= 0 and $i_bracket < $i_opening
$i_bracket >= 0 $i_bracket < $i_opening
Truth table66%2/3
A and B
A= $i_bracket >= 0B= $i_bracket < $i_opening
ABresult
0X0
100
111
5444 && $i_bracket < $i_opening )
5445 {
5446 38 $i_opening = $i_bracket;
5447 38 $ch_opening = '[';
5448 }
5449 56 ▶ if ( $i_brace >= 0 && $i_brace < $i_opening ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_brace >= 0 and $i_brace < $i_opening) 0 56 56
Condition33%1/3
$i_brace >= 0 and $i_brace < $i_opening
!ll&&!rl&&r
5600
MC/DC0%0/2
$i_brace >= 0 and $i_brace < $i_opening
$i_brace >= 0 $i_brace < $i_opening
Truth table33%1/3
A and B
A= $i_brace >= 0B= $i_brace < $i_opening
ABresult
0X0
100
111
5450 0 $i_opening = $i_brace;
5451 0 $ch_opening = '{';
5452 }
5453 56 ▶ if ( $i_opening >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($i_opening >= 0 and $i_opening < length($field) - 1) { } 56 0 56
Condition33%1/3
$i_opening >= 0 and $i_opening < length($field) - 1
!ll&&!rl&&r
0056
MC/DC0%0/2
$i_opening >= 0 and $i_opening < length($field) - 1
$i_opening >= 0 $i_opening < length($field) - 1
Truth table33%1/3
A and B
A= $i_opening >= 0B= $i_opening < length($field) - 1
ABresult
0X0
100
111
5454 && $i_opening < length($field) - 1 )
5455 {
5456 56 $pos_start_number = $i_opening + 1 + $ending_b;
5457 56 ▶ $char_end_part1 = $ch_opening
Branch: 1 branch
BranchTrueFalseTotal
unless $ending_b 25 31 56
5458 if ( !$ending_b );
5459 }
5460 else {
5461 ## strange - could not find the opening token
5462 }
5463 }
5464  
5465 # no braces: maybe '=>b'
5466 else {
5467  
5468 # looking for patterns ending in '=b' or '=>b'
5469 48 ▶ if ( !$ending_b ) { return @fail }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ending_b) 0 48 48
5470  
5471 # find the = in the text
5472 48 my $pos_equals = index( $field, '=' );
5473 48 ▶ return @fail if ( $pos_equals < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pos_equals < 0 11 37 48
5474  
5475 # be sure there are no other '=' in the pattern
5476 37 my $equals_count = ( $pat1 =~ tr/=/=/ );
5477 37 ▶ return @fail if ( $equals_count != 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $equals_count != 1 3 34 37
5478  
5479 34 ▶ if ( $len_pat1 >= 2 && substr( $pat1, -2, 2 ) eq '=b' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len_pat1 >= 2 and substr($pat1, -2, 2) eq '=b') { } 8 26 34
elsif ($len_pat1 >= 3 and substr($pat1, -3, 3) eq '=>b') { } 16 10 26
Condition66%2/3
$len_pat1 >= 2 and substr($pat1, -2, 2) eq "=b"
!ll&&!rl&&r
0268
MC/DC50%1/2
$len_pat1 >= 2 and substr($pat1, -2, 2) eq "=b"
$len_pat1 >= 2 substr($pat1, -2, 2) eq "=b"
Truth table66%2/3
A and B
A= $len_pat1 >= 2B= substr($pat1, -2, 2) eq "=b"
ABresult
0X0
100
111
5480 8 $pos_start_number = $pos_equals + 2;
5481 }
5482 ▶ elsif ( $len_pat1 >= 3 && substr( $pat1, -3, 3 ) eq '=>b' ) {
Condition66%2/3
$len_pat1 >= 3 and substr($pat1, -3, 3) eq "=>b"
!ll&&!rl&&r
01016
MC/DC50%1/2
$len_pat1 >= 3 and substr($pat1, -3, 3) eq "=>b"
$len_pat1 >= 3 substr($pat1, -3, 3) eq "=>b"
Truth table66%2/3
A and B
A= $len_pat1 >= 3B= substr($pat1, -3, 3) eq "=>b"
ABresult
0X0
100
111
5483 16 $pos_start_number = $pos_equals + 3;
5484 }
5485 else {
5486  
5487 # cannot handle this pattern
5488 10 return @fail;
5489 }
5490 }
5491  
5492 80 ▶ if ( $pos_start_number <= 0 || $pos_start_number >= $len_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_start_number <= 0 or $pos_start_number >= $len_field) 0 80 80
Condition33%1/3
$pos_start_number <= 0 or $pos_start_number >= $len_field
l!l&&r!l&&!r
0080
MC/DC0%0/2
$pos_start_number <= 0 or $pos_start_number >= $len_field
$pos_start_number <= 0 $pos_start_number >= $len_field
Truth table33%1/3
A or B
A= $pos_start_number <= 0B= $pos_start_number >= $len_field
ABresult
1X1
011
000
5493 0 return @fail;
5494 }
5495  
5496 80 return ( $pos_start_number, $char_end_part1, $ch_opening );
5497 } ## end sub split_field
5498  
5499 sub field_matches_end_pattern {
5500  
5501 264 ▶ my ( $field2, $pat2 ) = @_;
Subroutine field_matches_end_pattern: called CC=9 SCAR=23.2
Pod undocumented
5502  
5503 # Check that a possible numeric field matches the ending pattern
5504  
5505 # Given:
5506 # $field2 = the rest of the field after removing any sign
5507 # $pat2 = the end pattern of this field
5508 # Return:
5509 # false if field is definitely non-numeric
5510 # true otherwise
5511  
5512 264 my $next_char = substr( $pat2, 1, 1 );
5513 264 my $field2_trim = EMPTY_STRING;
5514  
5515 # if pattern is one of: 'n,', 'n,b'
5516 264 ▶ if ( $next_char eq COMMA ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($next_char eq ',') { } 192 72 264
elsif ($next_char eq 'b') { } 47 25 72
elsif ($next_char eq '}') { } 25 0 25
5517 192 my $icomma = index( $field2, COMMA );
5518 192 ▶ if ( $icomma >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($icomma >= 0) 192 0 192
5519 192 $field2_trim = substr( $field2, 0, $icomma );
5520 }
5521 }
5522  
5523 # if pattern is one of: 'nb', 'nb}', 'nb},'
5524 elsif ( $next_char eq 'b' ) {
5525 47 my $ispace = index( $field2, SPACE );
5526 47 ▶ if ( $ispace >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ispace >= 0) 47 0 47
5527 47 $field2_trim = substr( $field2, 0, $ispace );
5528 }
5529 }
5530  
5531 # if pattern is one of 'n},', 'n};'
5532 elsif ( $next_char eq '}' ) {
5533 25 ▶ if ( $field2 =~ /^([^\)\}\]]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field2 =~ /^([^\)\}\]]+)/) 25 0 25
5534 25 $field2_trim = $1;
5535 }
5536 }
5537  
5538 # unrecognized pattern
5539 else {
5540 0 DEVEL_MODE && Fault(<<EOM);
5541 Unexpected ending pattern '$pat2' next='$next_char' field2='$field2'
5542 The hash 'is_leading_sign_pattern' seems to have changed but the code
5543 has not been updated to handle it. Please fix.
5544 EOM
5545 0 return;
5546 }
5547  
5548 264 ▶ if ( !length($field2_trim) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $field2_trim) 0 264 264
5549 0 DEVEL_MODE
5550 && Fault(
5551 "STRANGE: cannot find end of field=$field2 for pat=$pat2 \n");
5552 0 return;
5553 }
5554  
5555 # Reject obviously non-numeric fields just to be sure we did not
5556 # jump into a quote of some kind
5557 264 ▶ if ( $field2_trim !~ /^[\d\.\+\-abcdefpx_]+$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field2_trim =~ /^[\d\.\+\-abcdefpx_]+$/i) 2 262 264
5558 DEBUG_VSN
5559 2 && print {*STDERR}
5560 "Rejecting match to pat2='$pat2' with next=$next_char field2=$field2 trimmed='$field2_trim'\n";
5561 2 return;
5562 }
5563 262 return 1;
5564 } ## end sub field_matches_end_pattern
5565  
5566 sub pad_signed_number_columns {
5567  
5568 70 ▶ my ($rgroup_lines) = @_;
Subroutine pad_signed_number_columns: called CC=52 SCAR=41.7
Pod undocumented
5569  
5570 # Given:
5571 # $rgroup_lines = the current vertical alignment group of lines
5572 # Task:
5573 # Look for columns of aligned numeric values, some of whose numbers
5574 # have algebraic signs. Add a leading space to the unsigned
5575 # numbers, if possible, so that the just the signs appear as the first
5576 # character. Example of what we want to do:
5577  
5578 # my @correct = (
5579 # [ 123456.79, 86753090000.868, 11 ],
5580 # [ -123456.79, -86753090000.868, -11 ],
5581 # [ 123456.001, 80.080, 10 ],
5582 # [ -123456.001, -80.080, 0 ],
5583 # [ 10.9, 10.9, 11 ],
5584 # );
5585  
5586 # The logic here is complex because we are working with bits of text
5587 # which have been broken into patterns which are convenient for the
5588 # vertical aligner, but we no longer have the original tokenization
5589 # which would have indicated the precise bounds of numbers. So we
5590 # have to proceed very carefully with lots of checks. There are
5591 # more checks than really necessary now because originally numbers
5592 # and quotes were both indicated with pattern 'Q'. But now numbers are
5593 # uniquely marked as pattern 'n', so there is less risk of an error.
5594 # The extra checks take very little time so they are retained.
5595  
5596 70 ▶ return unless ($rOpts_valign_signed_numbers);
Branch: 1 branch
BranchTrueFalseTotal
unless $rOpts_valign_signed_numbers 0 70 70
5597  
5598 70 my %column_info;
5599 my @columns;
5600  
5601 #----------------
5602 # loop over lines
5603 #----------------
5604 70 my $ix_line = -1;
5605 70 my $jmax = -1;
5606 70 foreach my $line ( @{$rgroup_lines} ) {
5607 349 $ix_line++;
5608 349 my $jmax_last = $jmax;
5609 349 $jmax = $line->{'jmax'};
5610 349 my $jmax_change = $jmax ne $jmax_last;
5611  
5612 349 my @alignments = @{ $line->{'ralignments'} };
5613 349 my $rfields = $line->{'rfields'};
5614 349 my $rpatterns = $line->{'rpatterns'};
5615 349 my $rtokens = $line->{'rtokens'};
5616  
5617 #-----------------------------------------------
5618 # Check for a reduction in the number of columns
5619 #-----------------------------------------------
5620 349 ▶ if ( $jmax < $jmax_last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax < $jmax_last) 11 338 349
5621  
5622 11 foreach my $jcol ( keys %column_info ) {
5623  
5624 # end any stranded columns on the right
5625 6 ▶ next if ( $jcol < $jmax );
Branch: 1 branch
BranchTrueFalseTotal
if $jcol < $jmax 4 2 6
5626 2 my $rcol_hash = $column_info{$jcol};
5627 2 ▶ next unless ($rcol_hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $rcol_hash 0 2 2
5628 2 ▶ if ( $rcol_hash->{signed_count}
Branch: 1 branch
BranchTrueFalseTotal
if ($rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}) 0 2 2
Condition33%1/3
$rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}
!ll&&!rl&&r
200
MC/DC0%0/2
$rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}
$rcol_hash->{"signed_count"} $rcol_hash->{"unsigned_count"}
Truth table33%1/3
A and B
A= $rcol_hash->{"signed_count"}B= $rcol_hash->{"unsigned_count"}
ABresult
0X0
100
111
5629 && $rcol_hash->{unsigned_count} )
5630 {
5631 0 end_signed_number_column( $rgroup_lines, $rcol_hash,
5632 $ix_line - 1 );
5633 }
5634 2 delete $column_info{$jcol};
5635 }
5636  
5637 # Try to keep the end data column running; test case 'rfc.in'
5638 # The last item in a list will still need a trailing comma.
5639 11 my $jcol = $jmax - 1;
5640 11 ▶ if ( $jcol >= 0 && $column_info{$jcol} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jcol >= 0 and $column_info{$jcol}) 2 9 11
Condition66%2/3
$jcol >= 0 and $column_info{$jcol}
!ll&&!rl&&r
092
MC/DC50%1/2
$jcol >= 0 and $column_info{$jcol}
$jcol >= 0 $column_info{$jcol}
Truth table66%2/3
A and B
A= $jcol >= 0B= $column_info{$jcol}
ABresult
0X0
100
111
5641 2 my $alignment = $alignments[$jcol];
5642 2 my $old_col = $columns[$jcol];
5643 2 my $col = $alignment->{column};
5644  
5645 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($col < $old_col and $rfields->[$jcol] =~ /^([+-]?)\d/ and $1 || $column_info{$jcol}{"signed_count"}) 0 2 2
Condition22%2/9
$col < $old_col and $rfields->[$jcol] =~ /^([+-]?)\d/ and $1 || $column_info{$jcol}{"signed_count"}
!ll&&!rl&&r
200
$col < $old_col and $rfields->[$jcol] =~ /^([+-]?)\d/
!ll&&!rl&&r
200
$1 or $column_info{$jcol}{"signed_count"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$col < $old_col and $rfields->[$jcol] =~ /^([+-]?)\d/ and $1 || $column_info{$jcol}{"signed_count"}
$col < $old_col $rfields->[$jcol] =~ /^([+-]?)\d/ $1 $column_info{$jcol}{"signed_count"}
Truth table20%1/5
A and B and C or D
A= $col < $old_colB= $rfields->[$jcol] =~ /^([+-]?)\d/C= $1D= $column_info{$jcol}{"signed_count"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
5646 $col < $old_col
5647  
5648 # only do this if the text has a leading digit
5649 && $rfields->[$jcol] =~ /^([+-]?)\d/
5650  
5651 # and a signed number has been seen - issue c375
5652 && ( $1 || $column_info{$jcol}->{signed_count} )
5653 )
5654 {
5655 0 my $spaces_needed = $old_col - $col;
5656 0 my $spaces_available =
5657 $line->get_available_space_on_right();
5658 0 ▶ if ( $spaces_available >= $spaces_needed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spaces_available >= $spaces_needed) 0 0 0
5659 0 $line->increase_field_width( $jcol, $spaces_needed );
5660 }
5661 }
5662 }
5663 }
5664  
5665 #--------------------------------------------
5666 # Loop over fields except last (side comment)
5667 #--------------------------------------------
5668 349 for my $jcol ( 0 .. $jmax - 1 ) {
5669  
5670 #-----------------------------------------
5671 # Decide if this is a new alignment column
5672 #-----------------------------------------
5673 785 my $alignment = $alignments[$jcol];
5674 785 my $old_col = $columns[$jcol];
5675 785 my $col = $alignment->{column};
5676 785 $columns[$jcol] = $col;
5677 785 ▶ if ( defined($old_col) && $old_col != $col ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_col and $old_col != $col) 55 730 785
Condition100%3/3
defined $old_col and $old_col != $col
!ll&&!rl&&r
14958155
MC/DC100%2/2
defined $old_col and $old_col != $col
defined $old_col $old_col != $col
Truth table100%3/3
A and B
A= defined $old_colB= $old_col != $col
ABresult
0X0
100
111
5678 55 foreach my $jcol_old ( keys %column_info ) {
5679 17 ▶ next if ( $jcol_old < $jcol );
Branch: 1 branch
BranchTrueFalseTotal
if $jcol_old < $jcol 6 11 17
5680 11 my $rcol_hash = $column_info{$jcol_old};
5681 11 ▶ if ( $rcol_hash->{signed_count}
Branch: 1 branch
BranchTrueFalseTotal
if ($rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}) 1 10 11
Condition66%2/3
$rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}
!ll&&!rl&&r
1001
MC/DC50%1/2
$rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}
$rcol_hash->{"signed_count"} $rcol_hash->{"unsigned_count"}
Truth table66%2/3
A and B
A= $rcol_hash->{"signed_count"}B= $rcol_hash->{"unsigned_count"}
ABresult
0X0
100
111
5682 && $rcol_hash->{unsigned_count} )
5683 {
5684 1 end_signed_number_column( $rgroup_lines, $rcol_hash,
5685 $ix_line - 1 );
5686 }
5687 11 delete $column_info{$jcol_old};
5688 }
5689 }
5690  
5691 # A new padded sign column can only start at an alignment change
5692 785 my $rcol_hash = $column_info{$jcol};
5693  
5694 #------------------------------------------------------------
5695 # Examine this field, looking for signed and unsigned numbers
5696 #------------------------------------------------------------
5697 785 my $field = $rfields->[$jcol];
5698 785 my $pattern = $rpatterns->[$jcol];
5699  
5700 785 my $is_signed_number = 0;
5701 785 my $is_unsigned_number = 0;
5702  
5703 #--------------------------------------------------------
5704 # set $pos_start_number = index in field of digit or sign
5705 #--------------------------------------------------------
5706 785 my $pos_start_number = 0;
5707 785 my $char_end_part1 = EMPTY_STRING;
5708 785 my $ch_opening = EMPTY_STRING;
5709  
5710 # Set $field_ok to false on encountering any problem
5711 # Do not pad signed and unsigned hash keys
5712 785 ▶ my $field_ok = length($field) > 0
Condition66%2/3
length $field > 0 && substr($rtokens->[$jcol], 0, 2) ne '=>'
!ll&&!rl&&r
030755
MC/DC50%1/2
length $field > 0 && substr($rtokens->[$jcol], 0, 2) ne '=>'
length $field > 0 substr($rtokens->[$jcol], 0, 2) ne '=>'
Truth table66%2/3
A && B
A= length $field > 0B= substr($rtokens->[$jcol], 0, 2) ne '=>'
ABresult
0X0
100
111
5713 && substr( $rtokens->[$jcol], 0, 2 ) ne '=>';
5714  
5715 785 ▶ if ( $field_ok && $pattern ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_ok and $pattern) 755 30 785
Condition66%2/3
$field_ok and $pattern
!ll&&!rl&&r
300755
MC/DC50%1/2
$field_ok and $pattern
$field_ok $pattern
Truth table66%2/3
A and B
A= $field_okB= $pattern
ABresult
0X0
100
111
5716  
5717 # Split the pattern at the first 'n'
5718 # $pat1 = pattern before the 'n' (if any)
5719 # $pat2 = pattern starting at the 'n'
5720 755 my ( $pat1, $pat2 );
5721 755 my $posq = index( $pattern, 'n' );
5722 755 ▶ if ( $posq < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posq < 0) { } 333 422 755
5723 333 $field_ok = 0;
5724 }
5725 else {
5726 # Just look at up to 3 of the pattern characters
5727 # We require $pat2 to have one of the known patterns
5728 422 $pat1 = substr( $pattern, 0, $posq );
5729 422 $pat2 = substr( $pattern, $posq, 3 );
5730 422 $field_ok = $is_leading_sign_pattern{$pat2};
5731 }
5732  
5733 755 ▶ if ($field_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_ok) 310 445 755
5734  
5735 # If the number starts within the field then we must
5736 # find its offset position.
5737 310 ▶ if ($pat1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat1) 124 186 310
5738  
5739 # Note: an optimization would be to remember previous
5740 # calls for each column and use them if possible, but
5741 # benchmarking shows that this is not necessary.
5742 # See .ba54 for example coding.
5743 124 ( $pos_start_number, $char_end_part1, $ch_opening ) =
5744 split_field( $pat1, $field, $pattern );
5745  
5746 124 ▶ $field_ok ||= $pos_start_number;
Condition33%1/3
$field_ok ||= $pos_start_number
l!l&&r!l&&!r
12400
MC/DC0%0/2
$field_ok ||= $pos_start_number
$field_ok $pos_start_number
Truth table33%1/3
A ||= B
A= $field_okB= $pos_start_number
ABresult
1X1
011
000
5747 }
5748  
5749 310 ▶ if ($field_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_ok) 310 0 310
5750  
5751 # look for an optional + or - sign
5752 310 my $test_char = substr( $field, $pos_start_number, 1 );
5753 310 my $sign;
5754 310 ▶ if ( $is_plus_or_minus{$test_char} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_plus_or_minus{$test_char}) 49 261 310
5755 49 $sign = $test_char;
5756 49 $test_char =
5757 substr( $field, $pos_start_number + 1, 1 );
5758 }
5759  
5760 # and a digit
5761 310 ▶ if ( $is_digit_char{$test_char} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_digit_char{$test_char}) { } 264 46 310
5762 264 my $field2;
5763 264 ▶ if ($sign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign) { } 47 217 264
5764 47 $is_signed_number = 1;
5765 47 $field2 =
5766 substr( $field, $pos_start_number + 1 );
5767 }
5768 else {
5769 217 $is_unsigned_number = 1;
5770 217 ▶ $field2 =
Branch: 1 branch
BranchTrueFalseTotal
$pos_start_number ? : 67 150 217
5771 $pos_start_number
5772 ? substr( $field, $pos_start_number )
5773 : $field;
5774 }
5775  
5776 # Check for match to ending pattern
5777 264 $field_ok =
5778 field_matches_end_pattern( $field2, $pat2 );
5779 }
5780 else {
5781 46 $field_ok = 0;
5782 }
5783 }
5784 }
5785 }
5786  
5787 #----------------------
5788 # Figure out what to do
5789 #----------------------
5790  
5791 # we require a signed or unsigned number field
5792 # which is not a hash key
5793 785 ▶ $field_ok &&= ( $is_signed_number || $is_unsigned_number );
Condition66%4/6
$field_ok &&= $is_signed_number || $is_unsigned_number
!ll&&!rl&&r
5230262
$is_signed_number || $is_unsigned_number
l!l&&r!l&&!r
472150
MC/DC0%0/3
$field_ok &&= $is_signed_number || $is_unsigned_number
$field_ok $is_signed_number $is_unsigned_number
Truth table0%0/4
A &&= B || C
A= $field_okB= $is_signed_numberC= $is_unsigned_number
ABCresult
0XX0
1000
11X1
1011
5794  
5795 # if a column has not started..
5796 785 ▶ if ( !$rcol_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rcol_hash) { } 584 201 785
5797  
5798 # give up if this is cannot start a new column
5799 584 ▶ next if ( !$field_ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $field_ok 500 84 584
5800  
5801 # otherwise continue on to start a new column
5802  
5803 }
5804  
5805 # if a column has been started...
5806 else {
5807  
5808 # and this cannot be added to it
5809 201 ▶ if ( !$field_ok
Branch: 1 branch
BranchTrueFalseTotal
if (not $field_ok or $rcol_hash->{"pos_start_number"} ne $pos_start_number or $rcol_hash->{"char_end_part1"} ne $char_end_part1 or $rcol_hash->{"col"} ne $col) 0 167 167
Condition77%7/9
not $field_ok or $rcol_hash->{"pos_start_number"} ne $pos_start_number or $rcol_hash->{"char_end_part1"} ne $char_end_part1 or $rcol_hash->{"col"} ne $col
l!l&&r!l&&!r
340167
not $field_ok or $rcol_hash->{"pos_start_number"} ne $pos_start_number or $rcol_hash->{"char_end_part1"} ne $char_end_part1
l!l&&r!l&&!r
340167
not $field_ok or $rcol_hash->{"pos_start_number"} ne $pos_start_number
l!l&&r!l&&!r
2311167
MC/DC50%2/4
not $field_ok or $rcol_hash->{"pos_start_number"} ne $pos_start_number or $rcol_hash->{"char_end_part1"} ne $char_end_part1 or $rcol_hash->{"col"} ne $col
not $field_ok $rcol_hash->{"pos_start_number"} ne $pos_start_number $rcol_hash->{"char_end_part1"} ne $char_end_part1 $rcol_hash->{"col"} ne $col
Truth table60%3/5
not(A) or B or C or D
A= not $field_okB= $rcol_hash->{"pos_start_number"} ne $pos_start_numberC= $rcol_hash->{"char_end_part1"} ne $char_end_part1D= $rcol_hash->{"col"} ne $col
ABCDresult
1XXX1
01XX1
001X1
00011
00000
5810 || $rcol_hash->{pos_start_number} ne $pos_start_number
5811 || $rcol_hash->{char_end_part1} ne $char_end_part1
5812 || $rcol_hash->{col} ne $col )
5813 {
5814  
5815 # then end the current column and start over
5816 34 ▶ if ( $rcol_hash->{signed_count}
Branch: 1 branch
BranchTrueFalseTotal
if ($rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}) 0 34 34
Condition33%1/3
$rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}
!ll&&!rl&&r
3400
MC/DC0%0/2
$rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}
$rcol_hash->{"signed_count"} $rcol_hash->{"unsigned_count"}
Truth table33%1/3
A and B
A= $rcol_hash->{"signed_count"}B= $rcol_hash->{"unsigned_count"}
ABresult
0X0
100
111
5817 && $rcol_hash->{unsigned_count} )
5818 {
5819 0 end_signed_number_column( $rgroup_lines, $rcol_hash,
5820 $ix_line - 1 );
5821 }
5822 34 delete $column_info{$jcol};
5823 34 $rcol_hash = undef;
5824 }
5825 }
5826  
5827 285 if (DEBUG_VSN) {
5828 my $exists = defined($rcol_hash);
5829 print
5830 "VSN: line=$ix_line change=$jmax_change jcol=$jcol field=$field exists?=$exists unsigned?=$is_unsigned_number signed?=$is_signed_number\n";
5831 }
5832  
5833 #---------------------------------------
5834 # Either start a new column, if possible
5835 #---------------------------------------
5836 285 ▶ if ( !defined($rcol_hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rcol_hash) { } 118 167 285
5837  
5838 118 ▶ next if ( !$field_ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $field_ok 23 95 118
5839  
5840 95 ▶ my $rsigned_lines = $is_signed_number ? [$ix_line] : [];
Branch: 1 branch
BranchTrueFalseTotal
$is_signed_number ? : 12 83 95
5841 95 $column_info{$jcol} = {
5842 unsigned_count => $is_unsigned_number,
5843 signed_count => $is_signed_number,
5844 pos_start_number => $pos_start_number,
5845 char_end_part1 => $char_end_part1,
5846 ix_first => $ix_line,
5847 col => $col,
5848 jcol => $jcol,
5849 rsigned_lines => $rsigned_lines,
5850 };
5851 }
5852  
5853 #------------------------------
5854 # or extend the existing column
5855 #------------------------------
5856 else {
5857 167 $rcol_hash->{unsigned_count} += $is_unsigned_number;
5858 167 $rcol_hash->{signed_count} += $is_signed_number;
5859 167 ▶ if ($is_signed_number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_signed_number) 35 132 167
5860 35 push @{ $rcol_hash->{rsigned_lines} }, $ix_line;
5861 }
5862 }
5863 }
5864 }
5865  
5866 #-------------------------------------
5867 # Loop to finish any remaining columns
5868 #-------------------------------------
5869 70 foreach my $jcol ( keys %column_info ) {
5870 48 my $rcol_hash = $column_info{$jcol};
5871 48 ▶ if ( $rcol_hash->{signed_count} && $rcol_hash->{unsigned_count} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}) 29 19 48
Condition100%3/3
$rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}
!ll&&!rl&&r
18129
MC/DC100%2/2
$rcol_hash->{"signed_count"} and $rcol_hash->{"unsigned_count"}
$rcol_hash->{"signed_count"} $rcol_hash->{"unsigned_count"}
Truth table100%3/3
A and B
A= $rcol_hash->{"signed_count"}B= $rcol_hash->{"unsigned_count"}
ABresult
0X0
100
111
5872 29 end_signed_number_column( $rgroup_lines, $rcol_hash, $ix_line );
5873 }
5874 }
5875 70 return;
5876 } ## end sub pad_signed_number_columns
5877  
5878 #########################################
5879 # CODE SECTION 7: Pad Wide Equals Columns
5880 #########################################
5881  
5882 45 ▶use constant DEBUG_WEC => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5883  
5884 sub end_wide_equals_column {
5885  
5886 12 ▶ my ( $rgroup_lines, $rcol_hash, $ix_last ) = @_;
Subroutine end_wide_equals_column: called CC=15 SCAR=27.3
Pod undocumented
5887  
5888 # Finish formatting a column of wide equals
5889 # Given:
5890 # $rgroup_lines - the current vertical alignment group of lines
5891 # $rcol_hash - a hash of information about this vertical column
5892 # $ix_last - index of the last line of this vertical column
5893  
5894 12 ▶ return unless ($rcol_hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $rcol_hash 0 12 12
5895 12 my $jcol = $rcol_hash->{jcol};
5896 12 my $col = $rcol_hash->{col};
5897 12 my $min_width = $rcol_hash->{min_width};
5898 12 my $max_width = $rcol_hash->{max_width};
5899 12 my $rwidths = $rcol_hash->{rwidths};
5900 12 my $ix_first = $rcol_hash->{ix_first};
5901  
5902 # check for skipped lines, shouldn't happen
5903 12 my $nlines = $ix_last - $ix_first + 1;
5904 12 my $num = @{$rwidths};
5905 12 ▶ if ( $num != $nlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num != $nlines) 0 12 12
5906 0 my $line = $rgroup_lines->[$ix_last];
5907 0 my $rfields = $line->{'rfields'};
5908 0 my $text = join( EMPTY_STRING, @{$rfields} );
5909 0 DEVEL_MODE && Fault(<<EOM);
5910 We seem to have miscounted lines, please check:
5911 nlines=$nlines
5912 num saved=$num
5913 min width=$min_width
5914 max width=$max_width
5915 j=$jcol
5916 ix_first=$ix_first
5917 ix_last=$ix_last
5918 text=$text
5919 EOM
5920 0 return;
5921 }
5922  
5923 #------------------------------------------------------
5924 # loop over all lines of this vertical alignment column
5925 #------------------------------------------------------
5926  
5927 my (
5928 12 $current_alignment, $starting_colp,
5929 $current_line, @previous_linked_lines
5930 );
5931 12 foreach my $item ( @{$rwidths} ) {
5932 50 my ( $ix, $width ) = @{$item};
5933 50 my $line = $rgroup_lines->[$ix];
5934  
5935 # add leading spaces to the shorter equality tokens to get
5936 # vertical alignment of the '=' signs
5937 50 my $jmax = $line->{'jmax'};
5938 50 my $jcolp = $jcol + 1;
5939  
5940 50 my @alignments = @{ $line->{'ralignments'} };
5941 50 my $alignment = $alignments[$jcolp];
5942 50 my $colp = $alignment->{column};
5943  
5944 #------------------------------------------------------------
5945 # Transfer column width changes between equivalent alignments
5946 #------------------------------------------------------------
5947  
5948 # This step keeps alignments to the right correct in case the
5949 # alignment object changes but the actual alignment col does not.
5950 # It is extremely rare for this to occur. Issue c353.
5951  
5952 # nothing to do if no more real alignments on right
5953 50 ▶ if ( $jcolp >= $jmax - 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($jcolp >= $jmax - 1) { } 18 32 50
elsif (not $current_alignment) { } 9 23 32
elsif (builtin::refaddr($alignment) != builtin::refaddr($current_alignment)) { } 6 17 23
5954 18 $current_alignment = undef;
5955 18 $current_line = undef;
5956 18 @previous_linked_lines = ();
5957 }
5958  
5959 # handle new rhs alignment
5960 elsif ( !$current_alignment ) {
5961 9 $current_alignment = $alignment;
5962 9 $current_line = $line;
5963 9 $starting_colp = $colp;
5964 9 @previous_linked_lines = ();
5965 }
5966  
5967 # handle change in existing alignment
5968 elsif ( refaddr($alignment) != refaddr($current_alignment) ) {
5969  
5970 # change rhs alignment column - new vertical group on right
5971 6 ▶ if ( $starting_colp != $colp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($starting_colp != $colp) { } 1 5 6
5972 1 $starting_colp = $colp;
5973 1 @previous_linked_lines = ();
5974 }
5975 else {
5976  
5977 # Same starting alignment col on right, but different alignment
5978 # object. See if we must increase width of this new alignment
5979 # object.
5980 5 my $current_colp = $current_alignment->{column};
5981 5 ▶ if ( $current_colp > $colp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_colp > $colp) 3 2 5
5982 3 my $excess = $current_colp - $colp;
5983 3 my $padding_available =
5984 $line->get_available_space_on_right();
5985 3 ▶ if ( $excess <= $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess <= $padding_available) 3 0 3
5986 3 $line->increase_field_width( $jcolp, $excess );
5987 3 $colp = $alignment->{column};
5988 }
5989 }
5990  
5991 # remember the previous line in case we have to go back and
5992 # increase its width
5993 5 push @previous_linked_lines, $current_line;
5994 }
5995 6 $current_alignment = $alignment;
5996 6 $current_line = $line;
5997 }
5998 else {
5999 ## continuing with same alignment
6000 }
6001  
6002 #-----------------------
6003 # add any needed padding
6004 #-----------------------
6005 50 my $pad = $max_width - $width;
6006 50 ▶ if ( $pad > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > 0) 33 17 50
6007  
6008 33 my $rfields = $line->{'rfields'};
6009 33 my $rfield_lengths = $line->{'rfield_lengths'};
6010  
6011 33 my $lenp = $rfield_lengths->[$jcolp];
6012 33 my $avail = $colp - $col;
6013 33 my $excess = $lenp + $pad - $avail;
6014  
6015 33 ▶ if ( $excess > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess > 0) 13 20 33
6016  
6017 13 my $padding_available = $line->get_available_space_on_right();
6018 13 ▶ if ( $excess <= $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess <= $padding_available) { } 12 1 13
6019 12 $line->increase_field_width( $jcolp, $excess );
6020  
6021 # Increase space of any previous linked lines
6022 12 foreach my $line_prev (@previous_linked_lines) {
6023 1 $padding_available =
6024 $line_prev->get_available_space_on_right();
6025 1 ▶ if ( $excess <= $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess <= $padding_available) { } 1 0 1
6026 1 $line_prev->increase_field_width( $jcolp, $excess );
6027 }
6028 else {
6029 0 last;
6030 }
6031 }
6032 }
6033 else {
6034 1 $pad = 0;
6035 }
6036  
6037 }
6038  
6039 # Add spaces
6040 33 $rfields->[$jcolp] = ( SPACE x $pad ) . $rfields->[$jcolp];
6041 33 $rfield_lengths->[$jcolp] += $pad;
6042 }
6043 }
6044 12 return;
6045 } ## end sub end_wide_equals_column
6046  
6047 sub pad_wide_equals_columns {
6048  
6049 12 ▶ my ($rgroup_lines) = @_;
Subroutine pad_wide_equals_columns: called CC=30 SCAR=38.7
Pod undocumented
6050  
6051 # Given:
6052 # $rgroup_lines = the current vertical alignment group of lines
6053 # Task:
6054 # Look for columns of aligned equals tokens, some of which may be
6055 # things like '-=', '&&=', etc. Increase the field length of the
6056 # previous field by 1 or 2 spaces where necessary and possible so
6057 # that alignment of all '=' occurs. For example, given
6058  
6059 # $j /= 2;
6060 # $pow2 = $pow2 * $pow2;
6061  
6062 # In this case we want to add a leading space '=' term to get
6063 # $j /= 2;
6064 # $pow2 = $pow2 * $pow2;
6065  
6066 # The logic here is somewhat similar to sub pad_signed_number_columns
6067  
6068 12 ▶ return unless ($rOpts_valign_wide_equals);
Branch: 1 branch
BranchTrueFalseTotal
unless $rOpts_valign_wide_equals 0 12 12
6069  
6070 12 my %column_info;
6071 my @columns;
6072  
6073 #----------------
6074 # loop over lines
6075 #----------------
6076 12 my $ix_line = -1;
6077 12 my $jmax = -1;
6078 12 foreach my $line ( @{$rgroup_lines} ) {
6079 50 $ix_line++;
6080 50 my $jmax_last = $jmax;
6081 50 $jmax = $line->{'jmax'};
6082 50 my $jmax_change = $jmax ne $jmax_last;
6083  
6084 50 my @alignments = @{ $line->{'ralignments'} };
6085 50 my $rfields = $line->{'rfields'};
6086 50 my $rtokens = $line->{'rtokens'};
6087  
6088 #-----------------------------------------------
6089 # Check for a reduction in the number of columns
6090 #-----------------------------------------------
6091 50 ▶ if ( $jmax < $jmax_last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax < $jmax_last) 6 44 50
6092  
6093 6 foreach my $jcol ( keys %column_info ) {
6094  
6095 # end any stranded columns on the right
6096 6 ▶ next if ( $jcol < $jmax );
Branch: 1 branch
BranchTrueFalseTotal
if $jcol < $jmax 6 0 6
6097 0 my $rcol_hash = $column_info{$jcol};
6098 0 ▶ next unless ($rcol_hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $rcol_hash 0 0 0
6099 0 ▶ if ( $rcol_hash->{max_width} > $rcol_hash->{min_width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rcol_hash->{"max_width"} > $rcol_hash->{"min_width"}) 0 0 0
6100 0 end_wide_equals_column( $rgroup_lines, $rcol_hash,
6101 $ix_line - 1 );
6102 }
6103 0 delete $column_info{$jcol};
6104 }
6105 }
6106  
6107 #--------------------------------------------------
6108 # Loop over fields except last field (side comment)
6109 #--------------------------------------------------
6110 50 for my $jcol ( 0 .. $jmax - 1 ) {
6111  
6112 #-----------------------------------------
6113 # Decide if this is a new alignment column
6114 #-----------------------------------------
6115 147 my $alignment = $alignments[$jcol];
6116 147 my $old_col = $columns[$jcol];
6117 147 my $col = $alignment->{column};
6118 147 $columns[$jcol] = $col;
6119 147 ▶ if ( defined($old_col) && $old_col != $col ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_col and $old_col != $col) 14 133 147
Condition100%3/3
defined $old_col and $old_col != $col
!ll&&!rl&&r
379614
MC/DC100%2/2
defined $old_col and $old_col != $col
defined $old_col $old_col != $col
Truth table100%3/3
A and B
A= defined $old_colB= $old_col != $col
ABresult
0X0
100
111
6120 14 foreach my $jcol_old ( keys %column_info ) {
6121 14 ▶ next if ( $jcol_old < $jcol );
Branch: 1 branch
BranchTrueFalseTotal
if $jcol_old < $jcol 14 0 14
6122 0 my $rcol_hash = $column_info{$jcol_old};
6123 0 ▶ if ( $rcol_hash->{max_width} > $rcol_hash->{min_width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rcol_hash->{"max_width"} > $rcol_hash->{"min_width"}) 0 0 0
6124 0 end_wide_equals_column( $rgroup_lines, $rcol_hash,
6125 $ix_line - 1 );
6126 }
6127 0 delete $column_info{$jcol_old};
6128 }
6129 }
6130  
6131 # A new wide equals column can only start at an alignment change
6132 147 my $rcol_hash = $column_info{$jcol};
6133  
6134 #------------------------------------------------------
6135 # Examine this field, looking for equals or wide equals
6136 #------------------------------------------------------
6137 147 my $field_next = $rfields->[ $jcol + 1 ];
6138 147 my $token = $rtokens->[$jcol];
6139  
6140 # See if this is an equals alignment group;
6141 # indicated by alignment token of '=' followed by a digit
6142 147 my $len_equals_symbol = 0;
6143 147 ▶ if ( length($token) > 1
Branch: 1 branch
BranchTrueFalseTotal
if (length $token > 1 and substr($token, 0, 1) eq "=" and $is_digit_char{substr $token, 1, 1}) 50 97 147
Condition100%6/6
length $token > 1 and substr($token, 0, 1) eq "=" and $is_digit_char{substr $token, 1, 1}
!ll&&!rl&&r
93450
length $token > 1 and substr($token, 0, 1) eq "="
!ll&&!rl&&r
504354
MC/DC100%3/3
length $token > 1 and substr($token, 0, 1) eq "=" and $is_digit_char{substr $token, 1, 1}
length $token > 1 substr($token, 0, 1) eq "=" $is_digit_char{substr $token, 1, 1}
Truth table100%4/4
A and B and C
A= length $token > 1B= substr($token, 0, 1) eq "="C= $is_digit_char{substr $token, 1, 1}
ABCresult
0XX0
10X0
1100
1111
6144 && substr( $token, 0, 1 ) eq '='
6145 && $is_digit_char{ substr( $token, 1, 1 ) } )
6146 {
6147  
6148 # find the actual equality symbol which starts the next field
6149 # i.e. '=' or '**=' or '-=' etc. We just need its length.
6150 50 my $pos = index( $field_next, '=' );
6151 50 ▶ if ( $pos >= 0 && $pos <= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0 and $pos <= 2) 50 0 50
Condition33%1/3
$pos >= 0 and $pos <= 2
!ll&&!rl&&r
0050
MC/DC0%0/2
$pos >= 0 and $pos <= 2
$pos >= 0 $pos <= 2
Truth table33%1/3
A and B
A= $pos >= 0B= $pos <= 2
ABresult
0X0
100
111
6152 50 $len_equals_symbol = $pos + 1;
6153 }
6154 }
6155  
6156 # if a column has not started..
6157 147 ▶ if ( !$rcol_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rcol_hash) { } 109 38 147
6158  
6159 # give up if this is cannot start a new column
6160 109 ▶ next if ( !$len_equals_symbol );
Branch: 1 branch
BranchTrueFalseTotal
unless $len_equals_symbol 97 12 109
6161  
6162 # otherwise continue on to start a new column
6163  
6164 }
6165  
6166 # if a column has been started...
6167 else {
6168  
6169 # and this cannot be added to it
6170 38 ▶ if ( !$len_equals_symbol || $rcol_hash->{col} ne $col ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $len_equals_symbol or $rcol_hash->{"col"} ne $col) 0 38 38
Condition33%1/3
not $len_equals_symbol or $rcol_hash->{"col"} ne $col
l!l&&r!l&&!r
0038
MC/DC0%0/2
not $len_equals_symbol or $rcol_hash->{"col"} ne $col
not $len_equals_symbol $rcol_hash->{"col"} ne $col
Truth table33%1/3
not(A) or B
A= not $len_equals_symbolB= $rcol_hash->{"col"} ne $col
ABresult
1X1
011
000
6171  
6172 # then end the current column and start over
6173 0 ▶ if ( $rcol_hash->{max_width} > $rcol_hash->{min_width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rcol_hash->{"max_width"} > $rcol_hash->{"min_width"}) 0 0 0
6174 0 end_wide_equals_column( $rgroup_lines, $rcol_hash,
6175 $ix_line - 1 );
6176 }
6177 0 delete $column_info{$jcol};
6178 0 $rcol_hash = undef;
6179 }
6180 }
6181  
6182 50 if (DEBUG_WEC) {
6183 my $exists = defined($rcol_hash);
6184 print
6185 "WEA: line=$ix_line change=$jmax_change jcol=$jcol field=$field_next exists?=$exists equals?=$len_equals_symbol\n";
6186 }
6187  
6188 #---------------------------------------
6189 # Either start a new column, if possible
6190 #---------------------------------------
6191 50 ▶ if ( !defined($rcol_hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rcol_hash) { } 12 38 50
6192  
6193 12 ▶ next if ( !$len_equals_symbol );
Branch: 1 branch
BranchTrueFalseTotal
unless $len_equals_symbol 0 12 12
6194  
6195 12 $column_info{$jcol} = {
6196 ix_first => $ix_line,
6197 col => $col,
6198 jcol => $jcol,
6199 min_width => $len_equals_symbol,
6200 max_width => $len_equals_symbol,
6201 rwidths => [ [ $ix_line, $len_equals_symbol ] ],
6202 };
6203 }
6204  
6205 #------------------------------
6206 # or extend the existing column
6207 #------------------------------
6208 else {
6209 38 ▶ if ( $len_equals_symbol > $rcol_hash->{max_width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_equals_symbol > $rcol_hash->{"max_width"}) 9 29 38
6210 9 $rcol_hash->{max_width} = $len_equals_symbol;
6211 }
6212 38 ▶ if ( $len_equals_symbol < $rcol_hash->{min_width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_equals_symbol < $rcol_hash->{"min_width"}) 4 34 38
6213 4 $rcol_hash->{min_width} = $len_equals_symbol;
6214 }
6215 38 push @{ $rcol_hash->{rwidths} },
6216 [ $ix_line, $len_equals_symbol ];
6217 }
6218 }
6219 }
6220  
6221 #-------------------------------------
6222 # Loop to finish any remaining columns
6223 #-------------------------------------
6224 12 foreach my $jcol ( keys %column_info ) {
6225 12 my $rcol_hash = $column_info{$jcol};
6226 12 ▶ if ( $rcol_hash->{max_width} > $rcol_hash->{min_width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rcol_hash->{"max_width"} > $rcol_hash->{"min_width"}) 12 0 12
6227 12 end_wide_equals_column( $rgroup_lines, $rcol_hash, $ix_line );
6228 }
6229 }
6230 12 return;
6231 } ## end sub pad_wide_equals_columns
6232  
6233 ###############################
6234 # CODE SECTION 8: Output Step A
6235 ###############################
6236  
6237 sub valign_output_step_A {
6238  
6239 3647 ▶ my ( $self, $rinput_hash ) = @_;
Subroutine valign_output_step_A: called CC=14 SCAR=26.9
Pod undocumented
6240  
6241 #------------------------------------------------------------
6242 # This is Step A in writing vertically aligned lines.
6243 # The line is prepared according to the alignments which have
6244 # been found. Then it is shipped to the next step.
6245 #------------------------------------------------------------
6246  
6247 my (
6248  
6249 $line,
6250 $min_ci_gap,
6251 $do_not_align,
6252 $group_leader_length,
6253 $extra_leading_spaces,
6254 $level,
6255 $maximum_line_length,
6256  
6257 ) =
6258  
6259 3647 @{$rinput_hash}{
6260 3647 qw(
6261 line
6262 min_ci_gap
6263 do_not_align
6264 group_leader_length
6265 extra_leading_spaces
6266 level
6267 maximum_line_length
6268 )
6269 };
6270  
6271 my (
6272  
6273 $rfields,
6274 $rfield_lengths,
6275 $leading_space_count,
6276 $outdent_long_lines,
6277 $maximum_field_index,
6278 $rvertical_tightness_flags,
6279 $Kend,
6280 $level_end,
6281  
6282 ) =
6283  
6284 3647 @{$line}{
6285 3647 qw(
6286 rfields
6287 rfield_lengths
6288 leading_space_count
6289 outdent_long_lines
6290 jmax
6291 rvertical_tightness_flags
6292 Kend
6293 level_end
6294 )
6295 };
6296  
6297 # Check for valid hash keys at end of lifetime of $line during development
6298 3647 DEVEL_MODE
6299 && check_keys( $line, \%valid_LINE_keys,
6300 "Checking line keys at valign_output_step_A", 1 );
6301  
6302 # add any extra spaces
6303 3647 ▶ if ( $leading_space_count > $group_leader_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count > $group_leader_length) 49 3598 3647
6304 49 $leading_space_count += $min_ci_gap;
6305 }
6306  
6307 3647 my $str = $rfields->[0];
6308 3647 my $str_len = $rfield_lengths->[0];
6309  
6310 3647 my @alignments = @{ $line->{'ralignments'} };
6311 3647 ▶ if ( @alignments != $maximum_field_index + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@alignments != $maximum_field_index + 1) 0 3647 3647
6312  
6313 # Shouldn't happen: sub install_new_alignments makes jmax alignments
6314 0 my $jmax_alignments = @alignments - 1;
6315 0 if (DEVEL_MODE) {
6316 Fault(
6317 "alignment jmax=$jmax_alignments should equal $maximum_field_index\n"
6318 );
6319 }
6320 0 $do_not_align = 1;
6321 }
6322  
6323 # loop to concatenate all fields of this line and needed padding
6324 3647 my $total_pad_count = 0;
6325 3647 for my $j ( 1 .. $maximum_field_index ) {
6326  
6327 # skip zero-length side comments
6328 last
6329 if (
6330 8774 ▶ ( $j == $maximum_field_index )
Branch: 1 branch
BranchTrueFalseTotal
if $j == $maximum_field_index and !defined($rfields->[$j]) || $rfield_lengths->[$j] == 0 3299 5475 8774
Condition83%5/6
$j == $maximum_field_index and !defined($rfields->[$j]) || $rfield_lengths->[$j] == 0
!ll&&!rl&&r
51273483299
not defined $rfields->[$j] or $rfield_lengths->[$j] == 0
l!l&&r!l&&!r
03299348
MC/DC66%2/3
$j == $maximum_field_index and !defined($rfields->[$j]) || $rfield_lengths->[$j] == 0
$j == $maximum_field_index not defined $rfields->[$j] $rfield_lengths->[$j] == 0
Truth table75%3/4
A and not(B) or C
A= $j == $maximum_field_indexB= not defined $rfields->[$j]C= $rfield_lengths->[$j] == 0
ABCresult
0XX0
1000
11X1
1011
6331 && ( !defined( $rfields->[$j] )
6332 || ( $rfield_lengths->[$j] == 0 ) )
6333 );
6334  
6335 # compute spaces of padding before this field
6336 5475 my $col = $alignments[ $j - 1 ]->{'column'};
6337 5475 my $pad = $col - ( $str_len + $leading_space_count );
6338  
6339 5475 ▶ if ($do_not_align) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_not_align) 0 5475 5475
6340 0 ▶ $pad =
Branch: 1 branch
BranchTrueFalseTotal
$j < $maximum_field_index ? : 0 0 0
6341 ( $j < $maximum_field_index )
6342 ? 0
6343 : $rOpts_minimum_space_to_comment - 1;
6344 }
6345  
6346 # if the -fpsc flag is set, move the side comment to the selected
6347 # column if and only if it is possible, ignoring constraints on
6348 # line length and minimum space to comment
6349 5475 ▶ if ( $rOpts_fixed_position_side_comment
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts_fixed_position_side_comment and $j == $maximum_field_index) 9 5466 5475
Condition100%3/3
$rOpts_fixed_position_side_comment and $j == $maximum_field_index
!ll&&!rl&&r
5434329
MC/DC100%2/2
$rOpts_fixed_position_side_comment and $j == $maximum_field_index
$rOpts_fixed_position_side_comment $j == $maximum_field_index
Truth table100%3/3
A and B
A= $rOpts_fixed_position_side_commentB= $j == $maximum_field_index
ABresult
0X0
100
111
6350 && $j == $maximum_field_index )
6351 {
6352 9 my $newpad = $pad + $rOpts_fixed_position_side_comment - $col - 1;
6353 9 ▶ if ( $newpad >= 0 ) { $pad = $newpad; }
Branch: 1 branch
BranchTrueFalseTotal
if ($newpad >= 0) 9 0 9
6354 }
6355  
6356 # accumulate the padding
6357 5475 ▶ if ( $pad > 0 ) { $total_pad_count += $pad; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > 0) 1604 3871 5475
6358  
6359 # only add padding when we have a finite field;
6360 # this avoids extra terminal spaces if we have empty fields
6361 5475 ▶ if ( $rfield_lengths->[$j] > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rfield_lengths->[$j] > 0) { } 5464 11 5475
6362 5464 $str .= SPACE x $total_pad_count;
6363 5464 $str_len += $total_pad_count;
6364 5464 $total_pad_count = 0;
6365 5464 $str .= $rfields->[$j];
6366 5464 $str_len += $rfield_lengths->[$j];
6367 }
6368 else {
6369 11 $total_pad_count = 0;
6370 }
6371 }
6372  
6373 3647 my $side_comment_length = $rfield_lengths->[$maximum_field_index];
6374  
6375 # ship this line off
6376 3647 $self->valign_output_step_B(
6377 {
6378 leading_space_count => $leading_space_count + $extra_leading_spaces,
6379 line => $str,
6380 line_length => $str_len,
6381 side_comment_length => $side_comment_length,
6382 outdent_long_lines => $outdent_long_lines,
6383 rvertical_tightness_flags => $rvertical_tightness_flags,
6384 level => $level,
6385 level_end => $level_end,
6386 Kend => $Kend,
6387 maximum_line_length => $maximum_line_length,
6388 }
6389 );
6390 3647 return;
6391 } ## end sub valign_output_step_A
6392  
6393 sub combine_fields {
6394  
6395 16 ▶ my ( $line_0, $line_1, $imax_align ) = @_;
Subroutine combine_fields: called CC=5 SCAR=16.5
Pod undocumented
6396  
6397 # Given:
6398 # $line_0, $line_1 = two adjacent lines
6399 # $imax_align = index of last alignment wanted
6400  
6401 # Task:
6402 # We have a group of two lines for which we do not want to align tokens
6403 # between index $imax_align and the side comment. So we will delete fields
6404 # between $imax_align and the side comment. Alignments have already
6405 # been set so we have to adjust them.
6406  
6407 16 ▶ if ( !defined($imax_align) ) { $imax_align = -1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $imax_align) 0 16 16
6408  
6409 # First delete the unwanted tokens
6410 16 my $jmax_old = $line_0->{'jmax'};
6411 16 my @idel = ( $imax_align + 1 .. $jmax_old - 2 );
6412 16 ▶ return if ( !@idel );
Branch: 1 branch
BranchTrueFalseTotal
unless @idel 0 16 16
6413  
6414 # Get old alignments before any changes are made
6415 16 my @old_alignments = @{ $line_0->{'ralignments'} };
6416  
6417 16 foreach my $line ( $line_0, $line_1 ) {
6418 32 delete_selected_tokens( $line, \@idel );
6419 }
6420  
6421 # Now adjust the alignments. Note that the side comment alignment
6422 # is always at jmax-1, and there is an ending alignment at jmax.
6423 16 my @new_alignments;
6424 16 ▶ if ( $imax_align >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imax_align >= 0) 0 16 16
6425 0 @new_alignments[ 0 .. $imax_align ] =
6426 @old_alignments[ 0 .. $imax_align ];
6427 }
6428  
6429 16 my $jmax_new = $line_0->{'jmax'};
6430  
6431 16 $new_alignments[ $jmax_new - 1 ] = $old_alignments[ $jmax_old - 1 ];
6432 16 $new_alignments[$jmax_new] = $old_alignments[$jmax_old];
6433 16 $line_0->{'ralignments'} = \@new_alignments;
6434 16 $line_1->{'ralignments'} = \@new_alignments;
6435 16 return;
6436 } ## end sub combine_fields
6437  
6438 sub get_output_line_number {
6439  
6440 # Return the output line number to external modules.
6441 # The output line number reported to a caller =
6442 # the number of items still in the buffer +
6443 # the number of items written.
6444 70 ▶ my $self = shift;
Subroutine get_output_line_number: called CC=1 SCAR=0.0
Pod undocumented
6445 70 return $self->group_line_count() +
6446 $self->[_file_writer_object_]->get_output_line_number();
6447 } ## end sub get_output_line_number
6448  
6449 ###############################
6450 # CODE SECTION 9: Output Step B
6451 ###############################
6452  
6453 { ## closure for sub valign_output_step_B
6454  
6455 # These are values for a cache used by valign_output_step_B.
6456 45 my $cached_line_text;
6457 my $cached_line_text_length;
6458 45 my $cached_line_type;
6459 45 my $cached_line_opening_flag;
6460 45 my $cached_line_closing_flag;
6461 45 my $cached_seqno;
6462 45 my $cached_line_valid;
6463 45 my $cached_line_leading_space_count;
6464 45 my $cached_seqno_string;
6465 45 my $cached_line_Kend;
6466 45 my $cached_line_maximum_length;
6467  
6468 # These are passed to step_C:
6469 45 my $seqno_string;
6470 45 my $last_nonblank_seqno_string;
6471  
6472 sub set_last_nonblank_seqno_string {
6473 398 ▶ my ($val) = @_;
Subroutine set_last_nonblank_seqno_string: called CC=1 SCAR=0.0
Pod undocumented
6474 398 $last_nonblank_seqno_string = $val;
6475 398 return;
6476 }
6477  
6478 sub get_cached_line_opening_flag {
6479 227 ▶ return $cached_line_opening_flag;
Subroutine get_cached_line_opening_flag: called CC=1 SCAR=0.0
Pod undocumented
6480 }
6481  
6482 sub get_cached_line_type {
6483 8652 ▶ return $cached_line_type;
Subroutine get_cached_line_type: called CC=1 SCAR=0.0
Pod undocumented
6484 }
6485  
6486 sub set_cached_line_valid {
6487 3 ▶ my ($val) = @_;
Subroutine set_cached_line_valid: called CC=1 SCAR=0.0
Pod undocumented
6488 3 $cached_line_valid = $val;
6489 3 return;
6490 }
6491  
6492 sub get_cached_seqno {
6493 157 ▶ return $cached_seqno;
Subroutine get_cached_seqno: called CC=1 SCAR=0.0
Pod undocumented
6494 }
6495  
6496 sub initialize_step_B_cache {
6497  
6498 # valign_output_step_B cache:
6499 661 ▶ $cached_line_text = EMPTY_STRING;
Subroutine initialize_step_B_cache: called CC=1 SCAR=0.0
Pod undocumented
6500 661 $cached_line_text_length = 0;
6501 661 $cached_line_type = 0;
6502 661 $cached_line_opening_flag = 0;
6503 661 $cached_line_closing_flag = 0;
6504 661 $cached_seqno = 0;
6505 661 $cached_line_valid = 0;
6506 661 $cached_line_leading_space_count = 0;
6507 661 $cached_seqno_string = EMPTY_STRING;
6508 661 $cached_line_Kend = undef;
6509 661 $cached_line_maximum_length = undef;
6510  
6511 # These vars hold a string of sequence numbers joined together used by
6512 # the cache
6513 661 $seqno_string = EMPTY_STRING;
6514 661 $last_nonblank_seqno_string = EMPTY_STRING;
6515 661 return;
6516 } ## end sub initialize_step_B_cache
6517  
6518 sub _flush_step_B_cache {
6519  
6520 2328 ▶ my ($self) = @_;
Subroutine _flush_step_B_cache: called CC=2 SCAR=6.9
6521  
6522 # Send any text in the step_B cache on to step_C
6523 2328 ▶ if ($cached_line_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_line_type) 1 2327 2328
6524 1 $seqno_string = $cached_seqno_string;
6525 1 $self->valign_output_step_C(
6526 $seqno_string,
6527 $last_nonblank_seqno_string,
6528  
6529 $cached_line_text,
6530 $cached_line_leading_space_count,
6531 $self->[_last_level_written_],
6532 $cached_line_Kend,
6533 );
6534 1 $cached_line_type = 0;
6535 1 $cached_line_text = EMPTY_STRING;
6536 1 $cached_line_text_length = 0;
6537 1 $cached_seqno_string = EMPTY_STRING;
6538 1 $cached_line_Kend = undef;
6539 1 $cached_line_maximum_length = undef;
6540 }
6541 2328 return;
6542 } ## end sub _flush_step_B_cache
6543  
6544 sub handle_cached_line {
6545  
6546 162 ▶ my ( $self, $rinput, $leading_string, $leading_string_length ) = @_;
Subroutine handle_cached_line: called CC=33 SCAR=38.6
Pod undocumented
6547  
6548 # handle a cached line ..
6549 # either append the current line to it or write it out
6550  
6551 # The cached line will either be:
6552 # - passed along to step_C, or
6553 # - or combined with the current line
6554  
6555 162 my $last_level_written = $self->[_last_level_written_];
6556  
6557 162 my $leading_space_count = $rinput->{leading_space_count};
6558 162 my $str = $rinput->{line};
6559 162 my $str_length = $rinput->{line_length};
6560 162 my $rvertical_tightness_flags = $rinput->{rvertical_tightness_flags};
6561 162 my $level = $rinput->{level};
6562 162 my $level_end = $rinput->{level_end};
6563 162 my $maximum_line_length = $rinput->{maximum_line_length};
6564  
6565 162 my ( $open_or_close, $seqno_beg );
6566 162 ▶ if ($rvertical_tightness_flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rvertical_tightness_flags) 162 0 162
6567  
6568 162 $open_or_close = $rvertical_tightness_flags->{_vt_type};
6569 162 $seqno_beg = $rvertical_tightness_flags->{_vt_seqno_beg};
6570 }
6571  
6572 # Dump an invalid cached line
6573 162 ▶ if ( !$cached_line_valid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $cached_line_valid) { } 93 69 162
elsif ($cached_line_type == 1 or $cached_line_type == 3) { } 32 37 69
6574 93 $self->valign_output_step_C(
6575 $seqno_string,
6576 $last_nonblank_seqno_string,
6577  
6578 $cached_line_text,
6579 $cached_line_leading_space_count,
6580 $last_level_written,
6581 $cached_line_Kend,
6582 );
6583 }
6584  
6585 # Handle cached line ending in OPENING tokens
6586 ▶ elsif ( $cached_line_type == 1 || $cached_line_type == 3 ) {
Condition100%3/3
$cached_line_type == 1 or $cached_line_type == 3
l!l&&r!l&&!r
211137
MC/DC100%2/2
$cached_line_type == 1 or $cached_line_type == 3
$cached_line_type == 1 $cached_line_type == 3
Truth table100%3/3
A or B
A= $cached_line_type == 1B= $cached_line_type == 3
ABresult
1X1
011
000
6587  
6588 32 my $gap = $leading_space_count - $cached_line_text_length;
6589  
6590 # handle option of just one tight opening per line:
6591 32 ▶ if ( $cached_line_opening_flag == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_line_opening_flag == 1) 14 18 32
6592 14 ▶ if ( defined($open_or_close) && $open_or_close == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $open_or_close and $open_or_close == 1) 0 14 14
Condition33%1/3
defined $open_or_close and $open_or_close == 1
!ll&&!rl&&r
0140
MC/DC0%0/2
defined $open_or_close and $open_or_close == 1
defined $open_or_close $open_or_close == 1
Truth table33%1/3
A and B
A= defined $open_or_closeB= $open_or_close == 1
ABresult
0X0
100
111
6593 0 $gap = -1;
6594 }
6595 }
6596  
6597 # Do not join the lines if this might produce a one-line
6598 # container which exceeds the maximum line length. This is
6599 # necessary prevent blinking, particularly with the combination
6600 # -xci -pvt=2. In that case a one-line block alternately forms
6601 # and breaks, causing -xci to alternately turn on and off (case
6602 # b765).
6603 # Patched to fix cases b656 b862 b971 b972: always do the check
6604 # if the maximum line length changes (due to -vmll).
6605 32 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($gap >= 0 and $maximum_line_length != $cached_line_maximum_length || defined $level_end && $level > $level_end) 0 32 32
Condition44%4/9
$gap >= 0 and $maximum_line_length != $cached_line_maximum_length || defined $level_end && $level > $level_end
!ll&&!rl&&r
12200
$maximum_line_length != $cached_line_maximum_length or defined $level_end and $level > $level_end
l!l&&r!l&&!r
0020
defined $level_end and $level > $level_end
!ll&&!rl&&r
0200
MC/DC0%0/4
$gap >= 0 and $maximum_line_length != $cached_line_maximum_length || defined $level_end && $level > $level_end
$gap >= 0 $maximum_line_length != $cached_line_maximum_length defined $level_end $level > $level_end
Truth table40%2/5
A and B or C and D
A= $gap >= 0B= $maximum_line_length != $cached_line_maximum_lengthC= defined $level_endD= $level > $level_end
ABCDresult
0XXX0
100X0
10100
11XX1
10111
6606 $gap >= 0
6607 && ( $maximum_line_length != $cached_line_maximum_length
6608 || ( defined($level_end) && $level > $level_end ) )
6609 )
6610 {
6611 0 my $test_line_length =
6612 $cached_line_text_length + $gap + $str_length;
6613  
6614 # Add a small tolerance in the length test (fixes case b862)
6615 0 ▶ if ( $test_line_length > $cached_line_maximum_length - 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_line_length > $cached_line_maximum_length - 2) 0 0 0
6616 0 $gap = -1;
6617 }
6618 }
6619  
6620 # NOTE: using defined() since $seqno_beg can be 0 for -bbvt
6621 32 ▶ if ( $gap >= 0 && defined($seqno_beg) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gap >= 0 and defined $seqno_beg) { } 20 12 32
Condition66%2/3
$gap >= 0 and defined $seqno_beg
!ll&&!rl&&r
12020
MC/DC50%1/2
$gap >= 0 and defined $seqno_beg
$gap >= 0 defined $seqno_beg
Truth table66%2/3
A and B
A= $gap >= 0B= defined $seqno_beg
ABresult
0X0
100
111
6622 20 $maximum_line_length = $cached_line_maximum_length;
6623 20 $leading_string = $cached_line_text . SPACE x $gap;
6624 20 $leading_string_length = $cached_line_text_length + $gap;
6625 20 $leading_space_count = $cached_line_leading_space_count;
6626 20 $seqno_string = $cached_seqno_string . ':' . $seqno_beg;
6627 20 $level = $last_level_written;
6628 }
6629 else {
6630 12 $self->valign_output_step_C(
6631 $seqno_string,
6632 $last_nonblank_seqno_string,
6633  
6634 $cached_line_text,
6635 $cached_line_leading_space_count,
6636 $last_level_written,
6637 $cached_line_Kend,
6638 );
6639 }
6640 }
6641  
6642 # Handle cached line ending in CLOSING tokens
6643 else {
6644 37 my $test_line =
6645 $cached_line_text . SPACE x $cached_line_closing_flag . $str;
6646 37 my $test_line_length =
6647 $cached_line_text_length +
6648 $cached_line_closing_flag +
6649 $str_length;
6650 37 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($seqno_beg and $open_or_close == $cached_line_type || $cached_line_type == 2 && $open_or_close == 4 || !$open_or_close && $cached_line_type == 2 and $test_line_length <= $cached_line_maximum_length) { } 33 4 37
Condition77%14/18
$seqno_beg and $open_or_close == $cached_line_type || $cached_line_type == 2 && $open_or_close == 4 || !$open_or_close && $cached_line_type == 2 and $test_line_length <= $cached_line_maximum_length
!ll&&!rl&&r
1333
$seqno_beg and $open_or_close == $cached_line_type || $cached_line_type == 2 && $open_or_close == 4 || !$open_or_close && $cached_line_type == 2
!ll&&!rl&&r
0136
$open_or_close == $cached_line_type or $cached_line_type == 2 and $open_or_close == 4 or not $open_or_close and $cached_line_type == 2
l!l&&r!l&&!r
9271
$open_or_close == $cached_line_type or $cached_line_type == 2 and $open_or_close == 4
l!l&&r!l&&!r
9028
$cached_line_type == 2 and $open_or_close == 4
!ll&&!rl&&r
1270
not $open_or_close and $cached_line_type == 2
!ll&&!rl&&r
0127
MC/DC57%4/7
$seqno_beg and $open_or_close == $cached_line_type || $cached_line_type == 2 && $open_or_close == 4 || !$open_or_close && $cached_line_type == 2 and $test_line_length <= $cached_line_maximum_length
$seqno_beg $open_or_close == $cached_line_type $cached_line_type == 2 $open_or_close == 4 not $open_or_close $cached_line_type == 2 $test_line_length <= $cached_line_maximum_length
Truth table30%4/13
A and B or C and D or not(E) and F and G
A= $seqno_begB= $open_or_close == $cached_line_typeC= $cached_line_type == 2D= $open_or_close == 4E= not $open_or_closeF= $cached_line_type == 2G= $test_line_length <= $cached_line_maximum_length
ABCDEFGresult
0XXXXXX0
100X0XX0
100X10X0
10100XX0
101010X0
11XXXX00
1011XX00
100X1100
10101100
11XXXX11
1011XX11
100X1111
10101111
6651  
6652 # The new line must start with container
6653 $seqno_beg
6654  
6655 # The container combination must be okay..
6656 && (
6657  
6658 # okay to combine like types
6659 ( $open_or_close == $cached_line_type )
6660  
6661 # closing block brace may append to non-block
6662 || ( $cached_line_type == 2 && $open_or_close == 4 )
6663  
6664 # something like ');'
6665 || ( !$open_or_close && $cached_line_type == 2 )
6666  
6667 )
6668  
6669 # The combined line must fit
6670 && ( $test_line_length <= $cached_line_maximum_length )
6671 )
6672 {
6673  
6674 33 $seqno_string = $cached_seqno_string . ':' . $seqno_beg;
6675  
6676 # Patch to outdent closing tokens ending # in ');' If we
6677 # are joining a line like ');' to a previous stacked set of
6678 # closing tokens, then decide if we may outdent the
6679 # combined stack to the indentation of the ');'. Since we
6680 # should not normally outdent any of the other tokens more
6681 # than the indentation of the lines that contained them, we
6682 # will only do this if all of the corresponding opening
6683 # tokens were on the same line. This can happen with -sot
6684 # and -sct.
6685  
6686 # For example, it is ok here:
6687 # __PACKAGE__->load_components( qw(
6688 # PK::Auto
6689 # Core
6690 # ));
6691 #
6692 # But, for example, we do not outdent in this example
6693 # because that would put the closing sub brace out farther
6694 # than the opening sub brace:
6695 #
6696 # perltidy -sot -sct
6697 # $c->Tk::bind(
6698 # '<Control-f>' => sub {
6699 # my ($c) = @_;
6700 # my $e = $c->XEvent;
6701 # itemsUnderArea $c;
6702 # } );
6703 #
6704 33 ▶ if ( $str =~ /^\);/
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\);/ and $cached_line_text =~ /^[\)\}\]\s]*$/) 4 29 33
Condition100%3/3
$str =~ /^\);/ and $cached_line_text =~ /^[\)\}\]\s]*$/
!ll&&!rl&&r
16134
MC/DC100%2/2
$str =~ /^\);/ and $cached_line_text =~ /^[\)\}\]\s]*$/
$str =~ /^\);/ $cached_line_text =~ /^[\)\}\]\s]*$/
Truth table100%3/3
A and B
A= $str =~ /^\);/B= $cached_line_text =~ /^[\)\}\]\s]*$/
ABresult
0X0
100
111
6705 && $cached_line_text =~ /^[\)\}\]\s]*$/ )
6706 {
6707  
6708 # The way to tell this is if the stacked sequence
6709 # numbers of this output line are the reverse of the
6710 # stacked sequence numbers of the previous non-blank
6711 # line of sequence numbers. So we can join if the
6712 # previous nonblank string of tokens is the mirror
6713 # image. For example if stack )}] is 13:8:6 then we
6714 # are looking for a leading stack like [{( which
6715 # is 6:8:13. We only need to check the two ends,
6716 # because the intermediate tokens must fall in order.
6717 # Note on speed: having to split on colons and
6718 # eliminate multiple colons might appear to be slow,
6719 # but it's not an issue because we almost never come
6720 # through here. In a typical file we don't.
6721  
6722 4 $seqno_string =~ s/^:+//;
6723 4 $last_nonblank_seqno_string =~ s/^:+//;
6724 4 $seqno_string =~ s/:+/:/g;
6725 4 $last_nonblank_seqno_string =~ s/:+/:/g;
6726  
6727 # how many spaces can we outdent?
6728 4 my $diff =
6729 $cached_line_leading_space_count - $leading_space_count;
6730 4 ▶ if ( $diff > 0
Branch: 1 branch
BranchTrueFalseTotal
if ($diff > 0 and length $seqno_string and length $last_nonblank_seqno_string == length $seqno_string) 3 1 4
Condition50%3/6
$diff > 0 and length $seqno_string and length $last_nonblank_seqno_string == length $seqno_string
!ll&&!rl&&r
013
$diff > 0 and length $seqno_string
!ll&&!rl&&r
004
MC/DC33%1/3
$diff > 0 and length $seqno_string and length $last_nonblank_seqno_string == length $seqno_string
$diff > 0 length $seqno_string length $last_nonblank_seqno_string == length $seqno_string
Truth table50%2/4
A and B and C
A= $diff > 0B= length $seqno_stringC= length $last_nonblank_seqno_string == length $seqno_string
ABCresult
0XX0
10X0
1100
1111
6731 && length($seqno_string)
6732 && length($last_nonblank_seqno_string) ==
6733 length($seqno_string) )
6734 {
6735 3 my @seqno_last =
6736 ( split /:/, $last_nonblank_seqno_string );
6737 3 my @seqno_now = ( split /:/, $seqno_string );
6738 3 ▶ if ( @seqno_now
Branch: 1 branch
BranchTrueFalseTotal
if (@seqno_now and @seqno_last and $seqno_now[-1] == $seqno_last[0] and $seqno_now[0] == $seqno_last[-1]) 3 0 3
Condition33%3/9
@seqno_now and @seqno_last and $seqno_now[-1] == $seqno_last[0] and $seqno_now[0] == $seqno_last[-1]
!ll&&!rl&&r
003
@seqno_now and @seqno_last and $seqno_now[-1] == $seqno_last[0]
!ll&&!rl&&r
003
@seqno_now and @seqno_last
!ll&&!rl&&r
003
MC/DC0%0/4
@seqno_now and @seqno_last and $seqno_now[-1] == $seqno_last[0] and $seqno_now[0] == $seqno_last[-1]
@seqno_now @seqno_last $seqno_now[-1] == $seqno_last[0] $seqno_now[0] == $seqno_last[-1]
Truth table20%1/5
A and B and C and D
A= @seqno_nowB= @seqno_lastC= $seqno_now[-1] == $seqno_last[0]D= $seqno_now[0] == $seqno_last[-1]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
6739 && @seqno_last
6740 && $seqno_now[-1] == $seqno_last[0]
6741 && $seqno_now[0] == $seqno_last[-1] )
6742 {
6743  
6744 # OK to outdent ..
6745 # for absolute safety, be sure we only remove
6746 # whitespace
6747 3 my $ws = substr( $test_line, 0, $diff );
6748 3 ▶ if ( ( length($ws) == $diff )
Branch: 1 branch
BranchTrueFalseTotal
if (length $ws == $diff and $ws =~ /^\s+$/) 3 0 3
Condition33%1/3
length $ws == $diff and $ws =~ /^\s+$/
!ll&&!rl&&r
003
MC/DC0%0/2
length $ws == $diff and $ws =~ /^\s+$/
length $ws == $diff $ws =~ /^\s+$/
Truth table33%1/3
A and B
A= length $ws == $diffB= $ws =~ /^\s+$/
ABresult
0X0
100
111
6749 && $ws =~ /^\s+$/ )
6750 {
6751  
6752 3 $test_line = substr( $test_line, $diff );
6753 3 $cached_line_leading_space_count -= $diff;
6754 3 $last_level_written =
6755 $self->level_change(
6756 $cached_line_leading_space_count,
6757 $diff, $last_level_written );
6758 3 $self->reduce_valign_buffer_indentation($diff);
6759 }
6760  
6761 # shouldn't happen, but not critical:
6762 ##else {
6763 ## ERROR transferring indentation here
6764 ##}
6765 }
6766 }
6767 }
6768  
6769 # Change the args to look like we received the combined line
6770 33 $str = $test_line;
6771 33 $str_length = $test_line_length;
6772 33 $leading_string = EMPTY_STRING;
6773 33 $leading_string_length = 0;
6774 33 $leading_space_count = $cached_line_leading_space_count;
6775 33 $level = $last_level_written;
6776 33 $maximum_line_length = $cached_line_maximum_length;
6777 }
6778 else {
6779 4 $self->valign_output_step_C(
6780 $seqno_string,
6781 $last_nonblank_seqno_string,
6782  
6783 $cached_line_text,
6784 $cached_line_leading_space_count,
6785 $last_level_written,
6786 $cached_line_Kend,
6787 );
6788 }
6789 }
6790 162 return ( $str, $str_length, $leading_string, $leading_string_length,
6791 $leading_space_count, $level, $maximum_line_length );
6792  
6793 } ## end sub handle_cached_line
6794  
6795 sub valign_output_step_B {
6796  
6797 8541 ▶ my ( $self, $rinput ) = @_;
Subroutine valign_output_step_B: called CC=17 SCAR=28.5
Pod undocumented
6798  
6799 #---------------------------------------------------------
6800 # This is Step B in writing vertically aligned lines.
6801 # Vertical tightness is applied according to preset flags.
6802 # In particular this routine handles stacking of opening
6803 # and closing tokens.
6804 #---------------------------------------------------------
6805  
6806 ##Note: key 'level_end' not needed
6807 my (
6808  
6809 $leading_space_count,
6810 $str,
6811 $str_length,
6812 $side_comment_length,
6813 $outdent_long_lines,
6814 $rvertical_tightness_flags,
6815 $level,
6816 $Kend,
6817 $maximum_line_length,
6818  
6819 ) =
6820  
6821 8541 @{$rinput}{
6822 8541 qw(
6823 leading_space_count
6824 line
6825 line_length
6826 side_comment_length
6827 outdent_long_lines
6828 rvertical_tightness_flags
6829 level
6830 Kend
6831 maximum_line_length
6832 )
6833 };
6834  
6835 # Useful -gcs test cases for wide characters are
6836 # perl527/(method.t.2, reg_mesg.t, mime-header.t)
6837  
6838 # handle outdenting of long lines:
6839 8541 my $is_outdented_line;
6840 8541 ▶ if ($outdent_long_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outdent_long_lines) 372 8169 8541
6841 372 my $excess =
6842 $str_length -
6843 $side_comment_length +
6844 $leading_space_count -
6845 $maximum_line_length;
6846 372 ▶ if ( $excess > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess > 0) 11 361 372
6847 11 $leading_space_count = 0;
6848 11 my $file_writer_object = $self->[_file_writer_object_];
6849 11 my $last_outdented_line_at =
6850 $file_writer_object->get_output_line_number();
6851 11 $self->[_last_outdented_line_at_] = $last_outdented_line_at;
6852  
6853 11 my $outdented_line_count = $self->[_outdented_line_count_];
6854 11 ▶ if ( !$outdented_line_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($outdented_line_count) 4 7 11
6855 4 $self->[_first_outdented_line_at_] =
6856 $last_outdented_line_at;
6857 }
6858 11 $outdented_line_count++;
6859 11 $self->[_outdented_line_count_] = $outdented_line_count;
6860 11 $is_outdented_line = 1;
6861 }
6862 }
6863  
6864 # Make preliminary leading whitespace. It could get changed
6865 # later by entabbing, so we have to keep track of any changes
6866 # to the leading_space_count from here on.
6867 8541 ▶ my $leading_string =
Branch: 1 branch
BranchTrueFalseTotal
$leading_space_count > 0 ? : 4984 3557 8541
6868 $leading_space_count > 0
6869 ? ( SPACE x $leading_space_count )
6870 : EMPTY_STRING;
6871 8541 my $leading_string_length = length($leading_string);
6872  
6873 # Unpack any recombination data; it was packed by
6874 # sub 'Formatter::set_vertical_tightness_flags'
6875  
6876 # old hash Meaning
6877 # index key
6878 #
6879 # 0 _vt_type: 1=opening non-block 2=closing non-block
6880 # 3=opening block brace 4=closing block brace
6881 #
6882 # 1a _vt_opening_flag: 1=no multiple steps, 2=multiple steps ok
6883 # 1b _vt_closing_flag: spaces of padding to use if closing
6884 # 2 _vt_seqno: sequence number of container
6885 # 3 _vt_valid flag: do not append if this flag is false. Will be
6886 # true if appropriate -vt flag is set. Otherwise, Will be
6887 # made true only for 2 line container in parens with -lp
6888 # 4 _vt_seqno_beg: sequence number of first token of line
6889 # 5 _vt_seqno_end: sequence number of last token of line
6890 # 6 _vt_min_lines: min number of lines for joining opening cache,
6891 # 0=no constraint
6892 # 7 _vt_max_lines: max number of lines for joining opening cache,
6893 # 0=no constraint
6894  
6895 8541 my ( $open_or_close, $opening_flag, $closing_flag, $seqno, $valid,
6896 $seqno_beg, $seqno_end );
6897 8541 ▶ if ($rvertical_tightness_flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rvertical_tightness_flags) 796 7745 8541
6898  
6899 796 $open_or_close = $rvertical_tightness_flags->{_vt_type};
6900 796 $opening_flag = $rvertical_tightness_flags->{_vt_opening_flag};
6901 796 $closing_flag = $rvertical_tightness_flags->{_vt_closing_flag};
6902 796 $seqno = $rvertical_tightness_flags->{_vt_seqno};
6903 796 $valid = $rvertical_tightness_flags->{_vt_valid_flag};
6904 796 $seqno_beg = $rvertical_tightness_flags->{_vt_seqno_beg};
6905 796 $seqno_end = $rvertical_tightness_flags->{_vt_seqno_end};
6906 }
6907  
6908 8541 ▶ $seqno_string = defined($seqno_end) ? $seqno_end : EMPTY_STRING;
Branch: 1 branch
BranchTrueFalseTotal
defined $seqno_end ? : 796 7745 8541
6909  
6910 # handle any cached line ..
6911 # either append this line to it or write it out
6912 # Note: the function length() is used in this next test out of caution.
6913 # All testing has shown that the variable $cached_line_text_length is
6914 # correct, but its calculation is complex and a loss of cached text
6915 # would be a disaster.
6916 8541 ▶ if ( length($cached_line_text) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $cached_line_text) 162 8379 8541
6917  
6918 (
6919 162 $str,
6920 $str_length,
6921 $leading_string,
6922 $leading_string_length,
6923 $leading_space_count,
6924 $level,
6925 $maximum_line_length
6926  
6927 ) = $self->handle_cached_line( $rinput, $leading_string,
6928 $leading_string_length );
6929  
6930 162 $cached_line_type = 0;
6931 162 $cached_line_text = EMPTY_STRING;
6932 162 $cached_line_text_length = 0;
6933 162 $cached_line_Kend = undef;
6934 162 $cached_line_maximum_length = undef;
6935  
6936 }
6937  
6938 # make the line to be written
6939 8541 my $line = $leading_string . $str;
6940 8541 my $line_length = $leading_string_length + $str_length;
6941  
6942 # Safety check: be sure that a line to be cached as a stacked block
6943 # brace line ends in the appropriate opening or closing block brace.
6944 # This should always be the case if the caller set flags correctly.
6945 # Code '3' is for -sobb, code '4' is for -scbb.
6946 8541 ▶ if ($open_or_close) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open_or_close) 163 8378 8541
6947 163 ▶ if ( $open_or_close == 3 && $line !~ /\{\s*$/
Branch: 1 branch
BranchTrueFalseTotal
if ($open_or_close == 3 and not $line =~ /\{\s*$/ or $open_or_close == 4 and not $line =~ /\}\s*$/) 0 163 163
Condition55%5/9
$open_or_close == 3 and not $line =~ /\{\s*$/ or $open_or_close == 4 and not $line =~ /\}\s*$/
l!l&&r!l&&!r
00163
$open_or_close == 3 and not $line =~ /\{\s*$/
!ll&&!rl&&r
152110
$open_or_close == 4 and not $line =~ /\}\s*$/
!ll&&!rl&&r
15850
MC/DC0%0/4
$open_or_close == 3 and not $line =~ /\{\s*$/ or $open_or_close == 4 and not $line =~ /\}\s*$/
$open_or_close == 3 not $line =~ /\{\s*$/ $open_or_close == 4 not $line =~ /\}\s*$/
Truth table14%1/7
A and not(B) or C and not(D)
A= $open_or_close == 3B= not $line =~ /\{\s*$/C= $open_or_close == 4D= not $line =~ /\}\s*$/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
6948 || $open_or_close == 4 && $line !~ /\}\s*$/ )
6949 {
6950 0 $open_or_close = 0;
6951 }
6952 }
6953  
6954 # write or cache this line ...
6955 # fix for case b999: do not cache an outdented line
6956 # fix for b1378: do not cache an empty line
6957 8541 ▶ if ( !$open_or_close
Branch: 1 branch
BranchTrueFalseTotal
if (not $open_or_close or $side_comment_length > 0 or $is_outdented_line or not $line_length) { } 8378 163 8541
Condition66%6/9
not $open_or_close or $side_comment_length > 0 or $is_outdented_line or not $line_length
l!l&&r!l&&!r
83780163
not $open_or_close or $side_comment_length > 0 or $is_outdented_line
l!l&&r!l&&!r
83780163
not $open_or_close or $side_comment_length > 0
l!l&&r!l&&!r
83780163
MC/DC25%1/4
not $open_or_close or $side_comment_length > 0 or $is_outdented_line or not $line_length
not $open_or_close $side_comment_length > 0 $is_outdented_line not $line_length
Truth table40%2/5
not(A) or B or C or not(D)
A= not $open_or_closeB= $side_comment_length > 0C= $is_outdented_lineD= not $line_length
ABCDresult
1XXX1
01XX1
001X1
00011
00000
6958 || $side_comment_length > 0
6959 || $is_outdented_line
6960 || !$line_length )
6961 {
6962 8378 $self->valign_output_step_C(
6963 $seqno_string,
6964 $last_nonblank_seqno_string,
6965  
6966 $line,
6967 $leading_space_count,
6968 $level,
6969 $Kend,
6970 );
6971 }
6972 else {
6973 163 $cached_line_text = $line;
6974 163 $cached_line_text_length = $line_length;
6975 163 $cached_line_type = $open_or_close;
6976 163 $cached_line_opening_flag = $opening_flag;
6977 163 $cached_line_closing_flag = $closing_flag;
6978 163 $cached_seqno = $seqno;
6979 163 $cached_line_valid = $valid;
6980 163 $cached_line_leading_space_count = $leading_space_count;
6981 163 $cached_seqno_string = $seqno_string;
6982 163 $cached_line_Kend = $Kend;
6983 163 $cached_line_maximum_length = $maximum_line_length;
6984 }
6985  
6986 8541 $self->[_last_level_written_] = $level;
6987 8541 $self->[_last_side_comment_length_] = $side_comment_length;
6988 8541 return;
6989 } ## end sub valign_output_step_B
6990 }
6991  
6992 ################################
6993 # CODE SECTION 10: Output Step C
6994 ################################
6995  
6996 { ## closure for sub valign_output_step_C
6997  
6998 # Vertical alignment buffer used by valign_output_step_C
6999 45 my $valign_buffer_filling;
7000 45 my @valign_buffer;
7001  
7002 sub initialize_valign_buffer {
7003 661 ▶ @valign_buffer = ();
Subroutine initialize_valign_buffer: called CC=1 SCAR=0.0
Pod undocumented
7004 661 $valign_buffer_filling = EMPTY_STRING;
7005 661 return;
7006 }
7007  
7008 sub dump_valign_buffer {
7009  
7010 2330 ▶ my ($self) = @_;
Subroutine dump_valign_buffer: called CC=3 SCAR=11.0
Pod undocumented
7011  
7012 # Send all lines in the current buffer on to step_D
7013 2330 ▶ if (@valign_buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (@valign_buffer) 2 2328 2330
7014 2 foreach (@valign_buffer) {
7015 7 $self->valign_output_step_D( @{$_} );
7016 }
7017 2 @valign_buffer = ();
7018 }
7019 2330 $valign_buffer_filling = EMPTY_STRING;
7020 2330 return;
7021 } ## end sub dump_valign_buffer
7022  
7023 sub reduce_valign_buffer_indentation {
7024  
7025 3 ▶ my ( $self, $diff ) = @_;
Subroutine reduce_valign_buffer_indentation: called CC=7 SCAR=19.9
Pod undocumented
7026  
7027 # Reduce the leading indentation of lines in the current
7028 # buffer by $diff spaces
7029 3 ▶ if ( $valign_buffer_filling && $diff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valign_buffer_filling and $diff) 2 1 3
Condition66%2/3
$valign_buffer_filling and $diff
!ll&&!rl&&r
102
MC/DC50%1/2
$valign_buffer_filling and $diff
$valign_buffer_filling $diff
Truth table66%2/3
A and B
A= $valign_buffer_fillingB= $diff
ABresult
0X0
100
111
7030 2 my $max_valign_buffer = @valign_buffer;
7031 2 foreach my $i ( 0 .. $max_valign_buffer - 1 ) {
7032 my ( $line, $leading_space_count, $level, $Kend ) =
7033 7 @{ $valign_buffer[$i] };
7034 7 my $ws = substr( $line, 0, $diff );
7035 7 ▶ if ( ( length($ws) == $diff ) && $ws =~ /^\s+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ws == $diff and $ws =~ /^\s+$/) 7 0 7
Condition33%1/3
length $ws == $diff and $ws =~ /^\s+$/
!ll&&!rl&&r
007
MC/DC0%0/2
length $ws == $diff and $ws =~ /^\s+$/
length $ws == $diff $ws =~ /^\s+$/
Truth table33%1/3
A and B
A= length $ws == $diffB= $ws =~ /^\s+$/
ABresult
0X0
100
111
7036 7 $line = substr( $line, $diff );
7037 }
7038 7 ▶ if ( $leading_space_count >= $diff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count >= $diff) 7 0 7
7039 7 $leading_space_count -= $diff;
7040 7 $level =
7041 $self->level_change( $leading_space_count, $diff,
7042 $level );
7043 }
7044 7 $valign_buffer[$i] =
7045 [ $line, $leading_space_count, $level, $Kend ];
7046 }
7047 }
7048 3 return;
7049 } ## end sub reduce_valign_buffer_indentation
7050  
7051 sub valign_output_step_C {
7052  
7053 my (
7054 8488 ▶ $self,
Subroutine valign_output_step_C: called CC=8 SCAR=20.8
Pod undocumented
7055 $seqno_string,
7056 $last_nonblank_seqno_string,
7057  
7058 @args_to_D,
7059 ) = @_;
7060  
7061 #-----------------------------------------------------------------------
7062 # This is Step C in writing vertically aligned lines.
7063 # Lines are either stored in a buffer or passed along to the next step.
7064 # The reason for storing lines is that we may later want to reduce their
7065 # indentation when -sot and -sct are both used.
7066 #-----------------------------------------------------------------------
7067  
7068 # Dump any saved lines if we see a line with an unbalanced opening or
7069 # closing token.
7070 8488 ▶ $self->dump_valign_buffer()
Branch: 1 branch
BranchTrueFalseTotal
if $seqno_string and $valign_buffer_filling 2 8486 8488
Condition100%3/3
$seqno_string and $valign_buffer_filling
!ll&&!rl&&r
80903962
MC/DC100%2/2
$seqno_string and $valign_buffer_filling
$seqno_string $valign_buffer_filling
Truth table100%3/3
A and B
A= $seqno_stringB= $valign_buffer_filling
ABresult
0X0
100
111
7071 if ( $seqno_string && $valign_buffer_filling );
7072  
7073 # Either store or write this line
7074 8488 ▶ if ($valign_buffer_filling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valign_buffer_filling) { } 7 8481 8488
7075 7 push @valign_buffer, [@args_to_D];
7076 }
7077 else {
7078 8481 $self->valign_output_step_D(@args_to_D);
7079 }
7080  
7081 # For lines starting or ending with opening or closing tokens..
7082 8488 ▶ if ($seqno_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqno_string) 398 8090 8488
7083 398 $last_nonblank_seqno_string = $seqno_string;
7084 398 set_last_nonblank_seqno_string($seqno_string);
7085  
7086 # Start storing lines when we see a line with multiple stacked
7087 # opening tokens.
7088 # patch for RT #94354, requested by Colin Williams
7089 398 ▶ if ( index( $seqno_string, ':' ) >= 0
Branch: 1 branch
BranchTrueFalseTotal
if (index($seqno_string, ":") != -1 and $seqno_string =~ /^\d+(\:+\d+)+$/ and not $args_to_D[0] =~ /^[\}\)\]\:\?]/) 8 390 398
Condition100%6/6
index($seqno_string, ":") != -1 and $seqno_string =~ /^\d+(\:+\d+)+$/ and not $args_to_D[0] =~ /^[\}\)\]\:\?]/
!ll&&!rl&&r
38828
index($seqno_string, ":") != -1 and $seqno_string =~ /^\d+(\:+\d+)+$/
!ll&&!rl&&r
3553310
MC/DC100%3/3
index($seqno_string, ":") != -1 and $seqno_string =~ /^\d+(\:+\d+)+$/ and not $args_to_D[0] =~ /^[\}\)\]\:\?]/
index($seqno_string, ":") != -1 $seqno_string =~ /^\d+(\:+\d+)+$/ not $args_to_D[0] =~ /^[\}\)\]\:\?]/
Truth table100%4/4
A and B and not(C)
A= index($seqno_string, ":") != -1B= $seqno_string =~ /^\d+(\:+\d+)+$/C= not $args_to_D[0] =~ /^[\}\)\]\:\?]/
ABCresult
0XX0
10X0
1100
1111
7090 && $seqno_string =~ /^\d+(\:+\d+)+$/
7091 && $args_to_D[0] !~ /^[\}\)\]\:\?]/ )
7092 {
7093  
7094 # This test is efficient but a little subtle: The first test
7095 # says that we have multiple sequence numbers and hence
7096 # multiple opening or closing tokens in this line. The second
7097 # part of the test rejects stacked closing and ternary tokens.
7098 # So if we get here then we should have stacked unbalanced
7099 # opening tokens.
7100  
7101 # Here is a complex example:
7102  
7103 # Foo($Bar[0], { # (side comment)
7104 # baz => 1,
7105 # });
7106  
7107 # The first line has sequence 6::4. It does not begin with
7108 # a closing token or ternary, so it passes the test and must be
7109 # stacked opening tokens.
7110  
7111 # The last line has sequence 4:6 but is a stack of closing
7112 # tokens, so it gets rejected.
7113  
7114 # Note that the sequence number of an opening token for a qw
7115 # quote is a negative number and will be rejected. For
7116 # example, for the following line: skip_symbols([qw(
7117 # $seqno_string='10:5:-1'. It would be okay to accept it but I
7118 # decided not to do this after testing.
7119  
7120 8 $valign_buffer_filling = $seqno_string;
7121  
7122 }
7123 }
7124 8488 return;
7125 } ## end sub valign_output_step_C
7126 }
7127  
7128 ###############################
7129 # CODE SECTION 11: Output Step D
7130 ###############################
7131  
7132 45 sub add_leading_tabs {
7133  
7134 45 ▶ my ( $self, $line, $leading_space_count, $level ) = @_;
Subroutine add_leading_tabs: called CC=9 SCAR=36.9
Pod undocumented
7135  
7136 # Convert leading whitespace to use tabs if -et or -t are set
7137  
7138 # Given:
7139 # $line = the line of text to be written, without any tabs
7140 # $leading_whitespace = expected number of leading blank spaces
7141 # $level = indentation level (needed for -t)
7142  
7143 # Return:
7144 # $line = the line with possible leading tabs
7145  
7146 45 my $trimmed_line = $line;
7147 45 $trimmed_line =~ s/^ [^\S\n]+ //gxm;
7148  
7149 # Check for discrepancy in actual leading white spaces with estimate
7150 45 ▶ if ( length($line) != length($trimmed_line) + $leading_space_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line != length($trimmed_line) + $leading_space_count) 0 45 45
7151  
7152 # If $leading_space_count is zero, then this routine must not
7153 # be called because we might be in a quote of some kind
7154 0 ▶ if ( $leading_space_count <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count <= 0) 0 0 0
7155 0 DEVEL_MODE && Fault(<<EOM);
7156 should not be here with leading space count = $leading_space_count
7157 EOM
7158 0 return $line;
7159 }
7160  
7161 0 my $leading_space_count_test = length($line) - length($trimmed_line);
7162  
7163 # Skip tabbing if actual whitespace is less than expected
7164 0 ▶ if ( $leading_space_count_test < $leading_space_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count_test < $leading_space_count) 0 0 0
7165 0 DEBUG_TABS
7166 && $self->warning(<<EOM);
7167 Error entabbing: expected count=$leading_space_count but only found $leading_space_count_test for line:
7168 '$line'
7169 EOM
7170 0 return $line;
7171 }
7172  
7173 # Use actual whitespace if it exceeds prediction. This mainly
7174 # occurs at hanging side comments.
7175 0 $leading_space_count = $leading_space_count_test;
7176 }
7177  
7178 #----------------------------------
7179 # Handle --entab-leading-whitespace
7180 #----------------------------------
7181 45 ▶ if ($rOpts_entab_leading_whitespace) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rOpts_entab_leading_whitespace) { } 45 0 45
elsif ($rOpts_tabs and $level) { } 0 0 0
7182  
7183 45 my $space_count =
7184 $leading_space_count % $rOpts_entab_leading_whitespace;
7185 45 my $tab_count =
7186 int( $leading_space_count / $rOpts_entab_leading_whitespace );
7187 45 my $leading_string = "\t" x $tab_count . SPACE x $space_count;
7188 45 $line = $leading_string . $trimmed_line;
7189 }
7190  
7191 #-----------------------------------------------
7192 # Handle -t (one tab per level; not recommended)
7193 #-----------------------------------------------
7194 ▶ elsif ( $rOpts_tabs && $level ) {
Condition0%0/3
$rOpts_tabs and $level
!ll&&!rl&&r
000
MC/DC0%0/2
$rOpts_tabs and $level
$rOpts_tabs $level
Truth table0%0/3
A and B
A= $rOpts_tabsB= $level
ABresult
0X0
100
111
7195  
7196 0 my $leading_string = ( "\t" x $level );
7197 0 my $space_count = $leading_space_count - $level * $rOpts_indent_columns;
7198  
7199 # shouldn't happen:
7200 0 ▶ if ( $space_count < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space_count < 0) { } 0 0 0
7201  
7202 # But it could be an outdented comment
7203 0 ▶ if ( $line !~ /^\s*#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*#/) 0 0 0
7204 0 DEBUG_TABS
7205 && $self->warning(
7206 "Error entabbing in valign_output_step_D: for level=$level count=$leading_space_count\n"
7207 );
7208 }
7209 0 $leading_string = ( SPACE x $leading_space_count );
7210 }
7211 else {
7212 0 $leading_string .= ( SPACE x $space_count );
7213 }
7214 0 $line = $leading_string . $trimmed_line;
7215 }
7216  
7217 # nothing to do; we should have skipped a call to this sub
7218 else {
7219 0 if (DEVEL_MODE) {
7220 Fault(
7221 "in tab sub but neither -t nor -et set: check flag 'require_tabs'\n"
7222 );
7223 }
7224 }
7225 45 return $line;
7226 } ## end sub add_leading_tabs
7227  
7228 sub valign_output_step_D {
7229  
7230 8488 ▶ my ( $self, $line, $leading_space_count, $level, $Kend ) = @_;
Subroutine valign_output_step_D: called CC=3 SCAR=11.0
Pod undocumented
7231  
7232 #----------------------------------------------------------------
7233 # This is Step D in writing vertically aligned lines.
7234 # It is the end of the vertical alignment pipeline.
7235 # Write one vertically aligned line of code to the output object.
7236 #----------------------------------------------------------------
7237  
7238 # Convert leading whitespace to use tabs if requested.
7239 8488 ▶ if ( $require_tabs && $leading_space_count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require_tabs and $leading_space_count > 0) 45 8443 8488
Condition100%3/3
$require_tabs and $leading_space_count > 0
!ll&&!rl&&r
8438545
MC/DC100%2/2
$require_tabs and $leading_space_count > 0
$require_tabs $leading_space_count > 0
Truth table100%3/3
A and B
A= $require_tabsB= $leading_space_count > 0
ABresult
0X0
100
111
7240 45 $line = $self->add_leading_tabs( $line, $leading_space_count, $level );
7241 }
7242  
7243 8488 my $file_writer_object = $self->[_file_writer_object_];
7244 8488 $file_writer_object->write_code_line( $line . "\n", $Kend );
7245  
7246 8488 return;
7247 } ## end sub valign_output_step_D
7248  
7249 ##########################
7250 # CODE SECTION 12: Summary
7251 ##########################
7252  
7253 sub report_anything_unusual {
7254 661 ▶ my $self = shift;
Subroutine report_anything_unusual: called CC=4 SCAR=13.9
Pod undocumented
7255 661 my $logger_object = $self->[_logger_object_];
7256 661 ▶ return if ( !$logger_object );
Branch: 1 branch
BranchTrueFalseTotal
unless $logger_object 2 659 661
7257  
7258 659 my $outdented_line_count = $self->[_outdented_line_count_];
7259 659 ▶ if ( $outdented_line_count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outdented_line_count > 0) 23 636 659
7260 23 $logger_object->write_logfile_entry(
7261 "$outdented_line_count long lines were outdented:\n");
7262 23 my $first_outdented_line_at = $self->[_first_outdented_line_at_];
7263 23 $logger_object->write_logfile_entry(
7264 " First at output line $first_outdented_line_at\n");
7265  
7266 23 ▶ if ( $outdented_line_count > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outdented_line_count > 1) 7 16 23
7267 7 my $last_outdented_line_at = $self->[_last_outdented_line_at_];
7268 7 $logger_object->write_logfile_entry(
7269 " Last at output line $last_outdented_line_at\n");
7270 }
7271 $logger_object->write_logfile_entry(
7272 23 " use -noll to prevent outdenting, -l=n to increase line length\n"
7273 );
7274 23 $logger_object->write_logfile_entry("\n");
7275 }
7276 659 return;
7277 } ## end sub report_anything_unusual
7278  
7279 } ## end package Perl::Tidy::VerticalAligner
7280 45 1;
« Tidy/Logger.pm ↑ Summary Tidy/Diagnostics.pm »