Tidy/VerticalAligner.pm

filter: Statementstmt 91.4% 2434 / 2662 Branchbran 79.8% 886 / 1110 Conditioncond 76.3% 572 / 749 MC/DCmcdc 54.5% 223 / 409 Subroutinesub 90.8% 109 / 120 Podpod 0.0% 0 / 79 totaltotal 82.3% 4224 / 5129 CC 913 SCAR 35.4
CC
913
Cov
86%
is_marginal_match
86.2
valign_input
70.7
pad_signed_number_columns
63.8
CRAP 34.4
« 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 = '20260826';
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 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
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;
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 0 ▶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 0 ▶sub Die {
Subroutine Die: not called CC=1 SCAR=6.9
Pod undocumented
98 0 my ($msg) = @_;
99 0 Perl::Tidy::Die($msg);
100 0 croak "unexpected return from Perl::Tidy::Die";
101 }
102  
103 0 ▶sub Warn {
Subroutine Warn: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my ($msg) = @_;
105 0 Perl::Tidy::Warn($msg);
106 0 return;
107 }
108  
109 0 ▶sub Fault {
Subroutine Fault: not called CC=1 SCAR=6.9
Pod undocumented
110  
111 0 my ($msg) = @_;
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 45 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
140  
141 # define valid keys in a line object
142 45 my @q = qw(
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 45 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.3
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;
179 45 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
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 };
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 0 ▶ my $debug_warning = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 my $msg = shift;
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 4 ▶sub check_valign_list_items {
Subroutine check_valign_list_items: called CC=8 SCAR=32.2
Pod undocumented
245 4 my ( $rlist, ( $option_name, $die_on_error ) ) = @_;
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 664 ▶sub check_options {
Subroutine check_options: called CC=12 SCAR=25.2
Pod undocumented
281  
282 664 my ($rOpts) = @_;
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 664 %valign_control_hash = ();
289 664 $valign_control_default = 1;
290  
291 # If -vil=s is entered without -vxl, assume -vxl='*'
292 664 ▶ if ( !$rOpts->{'valign-exclusion-list'}
Branch: 1 branch
BranchTrueFalseTotal
if (not $rOpts->{"valign-exclusion-list"} and $rOpts->{"valign-inclusion-list"}) 0 664 664
Condition66%2/3
not $rOpts->{"valign-exclusion-list"} and $rOpts->{"valign-inclusion-list"}
!ll&&!rl&&r
36610
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 664 ▶ if ( $rOpts->{'valign-exclusion-list'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"valign-exclusion-list"}) 3 661 664
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 664 $rOpts_indent_columns = $rOpts->{'indent-columns'};
336 664 $rOpts_tabs = $rOpts->{'tabs'};
337 664 $rOpts_entab_leading_whitespace = $rOpts->{'entab-leading-whitespace'};
338 664 ▶ $require_tabs = ( $rOpts_tabs || $rOpts_entab_leading_whitespace )
Condition66%4/6
($rOpts_tabs || $rOpts_entab_leading_whitespace) && $rOpts_indent_columns > 0
!ll&&!rl&&r
66202
$rOpts_tabs or $rOpts_entab_leading_whitespace
l!l&&r!l&&!r
02662
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 664 $rOpts->{'fixed-position-side-comment'};
343  
344 664 $rOpts_maximum_line_length = $rOpts->{'maximum-line-length'};
345 664 $rOpts_minimum_space_to_comment = $rOpts->{'minimum-space-to-comment'};
346 664 $rOpts_valign_code = $rOpts->{'valign-code'};
347 664 $rOpts_valign_block_comments = $rOpts->{'valign-block-comments'};
348 664 $rOpts_valign_side_comments = $rOpts->{'valign-side-comments'};
349 664 $rOpts_valign_signed_numbers = $rOpts->{'valign-signed-numbers'};
350 $rOpts_valign_signed_numbers_limit =
351 664 $rOpts->{'valign-signed-numbers-limit'};
352 664 $rOpts_valign_wide_equals = $rOpts->{'valign-wide-equals'};
353  
354 664 return;
355 } ## end sub check_options
356  
357 0 ▶sub check_keys {
Subroutine check_keys: not called CC=4 SCAR=30.0
Pod undocumented
358  
359 0 my ( $rtest, $rvalid, $msg, $exact_match ) = @_;
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 1330 ▶sub vertical_alignment_ok {
Subroutine vertical_alignment_ok: called CC=3 SCAR=11.0
Pod undocumented
393 1330 my ($tok) = @_;
394  
395 # Return 1 if alignment ok
396 # Return 0 if do not align
397 1330 ▶ return 1 if ( !%valign_control_hash );
Branch: 1 branch
BranchTrueFalseTotal
unless %valign_control_hash 1324 6 1330
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 665 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
404  
405 665 my ( $class, @arglist ) = @_;
406  
407 # Create a VerticalAligner object
408 # Given:
409 # @arglist is the hash of values shown below in %defaults
410  
411 0 ▶ if ( @arglist % 2 ) { croak "Odd number of items in arg hash list\n" }
Branch: 1 branch
BranchTrueFalseTotal
if (@arglist % 2) 0 665 665
412  
413 665 my %defaults = (
414 rOpts => undef,
415 file_writer_object => undef,
416 logger_object => undef,
417 diagnostics_object => undef,
418 );
419 665 my %args = ( %defaults, @arglist );
420  
421 # Initialize other caches and buffers
422 665 initialize_step_B_cache();
423 665 initialize_valign_buffer();
424 665 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 665 my $self = [];
430  
431 # objects
432 665 $self->[_file_writer_object_] = $args{file_writer_object};
433 665 $self->[_logger_object_] = $args{logger_object};
434 665 $self->[_diagnostics_object_] = $args{diagnostics_object};
435  
436 # shortcut to user options
437 665 my $rOpts = $args{rOpts};
438 665 $self->[_rOpts_] = $rOpts;
439  
440 # Batch of lines being collected
441 665 $self->[_rgroup_lines_] = [];
442 665 $self->[_group_level_] = 0;
443 665 $self->[_group_type_] = EMPTY_STRING;
444 665 $self->[_group_maximum_line_length_] = undef;
445 665 $self->[_zero_count_] = 0;
446 665 $self->[_comment_leading_space_count_] = 0;
447 665 $self->[_last_leading_space_count_] = 0;
448  
449 # Memory of what has been processed
450 665 $self->[_last_level_written_] = -1;
451 665 $self->[_last_side_comment_column_] = 0;
452 665 $self->[_last_side_comment_line_number_] = 0;
453 665 $self->[_last_side_comment_length_] = 0;
454 665 $self->[_last_side_comment_level_] = -1;
455 665 $self->[_outdented_line_count_] = 0;
456 665 $self->[_first_outdented_line_at_] = 0;
457 665 $self->[_last_outdented_line_at_] = 0;
458 665 $self->[_consecutive_block_comments_] = 0;
459  
460 665 bless $self, $class;
461 665 return $self;
462 } ## end sub new
463  
464 #################################
465 # CODE SECTION 2: Basic Utilities
466 #################################
467  
468 2359 ▶sub flush {
Subroutine flush: called CC=2 SCAR=6.9
Pod undocumented
469  
470 2359 my ($self) = @_;
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 2359 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 546 1813 2359
477  
478 # then anything left in the cache of step_B
479 2359 $self->_flush_step_B_cache();
480  
481 # then anything left in the buffer of step_C
482 2359 $self->dump_valign_buffer();
483  
484 2359 return;
485 } ## end sub flush
486  
487 2671 ▶sub initialize_for_new_group {
Subroutine initialize_for_new_group: called CC=1 SCAR=0.0
Pod undocumented
488  
489 2671 my ($self) = @_;
490  
491 # initialize for a new group of lines to be aligned vertically
492  
493 2671 $self->[_rgroup_lines_] = [];
494 2671 $self->[_group_type_] = EMPTY_STRING;
495 2671 $self->[_zero_count_] = 0;
496 2671 $self->[_comment_leading_space_count_] = 0;
497 2671 $self->[_last_leading_space_count_] = 0;
498 2671 $self->[_group_maximum_line_length_] = undef;
499  
500 # Note that the value for _group_level_ is
501 # handled separately in sub valign_input
502 2671 return;
503 } ## end sub initialize_for_new_group
504  
505 94 ▶sub group_line_count {
Subroutine group_line_count: called CC=1 SCAR=0.0
Pod undocumented
506 94 my $self = shift;
507 94 return +@{ $self->[_rgroup_lines_] };
508 }
509  
510 0 ▶sub write_diagnostics {
Subroutine write_diagnostics: not called CC=2 SCAR=17.9
Pod undocumented
511  
512 0 my ( $self, $msg ) = @_;
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 0 ▶sub warning {
Subroutine warning: not called CC=2 SCAR=17.9
Pod undocumented
524 0 my ( $self, $msg ) = @_;
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 1 ▶sub get_cached_line_count {
Subroutine get_cached_line_count: called CC=2 SCAR=7.2
Pod undocumented
533 1 my $self = shift;
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 4678 ▶sub get_recoverable_spaces {
Subroutine get_recoverable_spaces: called CC=2 SCAR=6.9
Pod undocumented
538  
539 4678 my $indentation = shift;
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 4678 ▶ return ref($indentation) ? $indentation->get_recoverable_spaces() : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $indentation ? : 264 4414 4678
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 45 ▶BEGIN {
Subroutine BEGIN: called CC=9 SCAR=22.0
565  
566 45 $is_opening_token{$_} = 1 for qw# { ( [ #;
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 8561 ▶sub valign_input {
Subroutine valign_input: called CC=69 SCAR=42.6
Pod undocumented
603  
604 8561 my ( $self, $rcall_hash ) = @_;
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 8561 @{$rcall_hash}{
686 8561 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 8561 @{$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 8561 my $jmax = @{$rfields} - 1;
713  
714 8561 ▶ my $leading_space_count =
Branch: 1 branch
BranchTrueFalseTotal
ref $indentation ? : 392 8169 8561
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 8561 my $is_outdented =
720 $self->[_last_leading_space_count_] > $leading_space_count;
721 8561 $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 8561 ▶ my $is_hanging_side_comment =
Condition100%6/6
$jmax == 1 && $rtokens->[0] eq '#' && $rfields->[0] =~ /^\s*$/
!ll&&!rl&&r
833817350
$jmax == 1 and $rtokens->[0] eq "#"
!ll&&!rl&&r
62032135223
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 8561 ▶ $is_outdented = 0 if ($is_hanging_side_comment);
Branch: 1 branch
BranchTrueFalseTotal
if $is_hanging_side_comment 50 8511 8561
730  
731 # Identify a block comment.
732 8561 ▶ 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
35484288725
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 8561 ▶ if ($is_block_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_block_comment) { } 725 7836 8561
736 725 $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 7836 ▶ if ( $self->[_consecutive_block_comments_] > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[12] > 1) 78 7758 7836
745 78 $self->forget_side_comment();
746 }
747 7836 $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 8561 ▶ if ($forget_side_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forget_side_comment) 46 8515 8561
753 46 $self->forget_side_comment();
754 }
755  
756 8561 my $is_balanced_line = $level_end == $level;
757  
758 8561 my $group_level = $self->[_group_level_];
759 8561 my $group_maximum_line_length = $self->[_group_maximum_line_length_];
760  
761 8561 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 8561 my $cached_line_type = get_cached_line_type();
772 8561 ▶ if ($cached_line_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_line_type) 227 8334 8561
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 0 ▶ if ( $level < 0 ) { $level = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($level < 0) 0 8561 8561
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 8561 ▶ 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) 3287 5274 8561
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
3274135274
$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
327225287
$level != $group_level or $group_maximum_line_length and $maximum_line_length != $group_maximum_line_length or $is_outdented
l!l&&r!l&&!r
31081645289
$level != $group_level or $group_maximum_line_length and $maximum_line_length != $group_maximum_line_length
l!l&&r!l&&!r
310805453
$group_maximum_line_length and $maximum_line_length != $group_maximum_line_length
!ll&&!rl&&r
160838450
$is_block_comment and not $rOpts_valign_block_comments
!ll&&!rl&&r
46766112
$is_block_comment || $rOpts_valign_side_comments || $rOpts_valign_code
l!l&&r!l&&!r
5265913
$is_block_comment or $rOpts_valign_side_comments
l!l&&r!l&&!r
611465422
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 3287 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 1091 2196 3287
830  
831 3287 $group_level = $level;
832 3287 $self->[_group_level_] = $group_level;
833 3287 $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 3287 ▶ $leading_space_count =
Branch: 1 branch
BranchTrueFalseTotal
ref $indentation ? : 187 3100 3287
838 ref($indentation) ? $indentation->get_spaces() : $indentation;
839 }
840  
841 # --------------------------------------------------------------------
842 # Collect outdentable block COMMENTS
843 # --------------------------------------------------------------------
844 8561 ▶ if ( $self->[_group_type_] eq 'COMMENT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[15] eq "COMMENT") 607 7954 8561
845 607 ▶ if ( $is_block_comment
Branch: 1 branch
BranchTrueFalseTotal
if ($is_block_comment and $outdent_long_lines and $leading_space_count == $self->[19]) { } 90 517 607
Condition66%4/6
$is_block_comment and $outdent_long_lines and $leading_space_count == $self->[19]
!ll&&!rl&&r
517090
$is_block_comment and $outdent_long_lines
!ll&&!rl&&r
517090
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 517 ▶ if ( @{ $self->[_rgroup_lines_] } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[13];} 517 0 517
859 }
860 }
861  
862 8471 my $rgroup_lines = $self->[_rgroup_lines_];
863 8471 ▶ if ( $break_alignment_before && @{$rgroup_lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($break_alignment_before and @{$rgroup_lines;}) 30 8441 8471
Condition100%3/3
$break_alignment_before and @{$rgroup_lines;}
!ll&&!rl&&r
83429930
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 8471 my $j_terminal_match;
871  
872 8471 ▶ if ( $is_terminal_ternary && @{$rgroup_lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_terminal_ternary and @{$rgroup_lines;}) 13 8458 8471
Condition100%3/3
$is_terminal_ternary and @{$rgroup_lines;}
!ll&&!rl&&r
8455313
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 8471 ▶ if ( $rfields->[0] eq 'else '
Branch: 1 branch
BranchTrueFalseTotal
if ($rfields->[0] eq "else " and @{$rgroup_lines;} and $is_balanced_line) 9 8462 8471
Condition83%5/6
$rfields->[0] eq "else " and @{$rgroup_lines;} and $is_balanced_line
!ll&&!rl&&r
846209
$rfields->[0] eq "else " and @{$rgroup_lines;}
!ll&&!rl&&r
845939
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 8471 ▶ if ( $jmax <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax <= 0) { } 4911 3560 8471
913 4911 $self->[_zero_count_]++;
914  
915 # VSN PATCH for a single number, part 1.
916 4911 ▶ my $is_numeric =
Condition100%3/3
$rOpts_valign_signed_numbers && $rpatterns->[0] eq 'n,'
!ll&&!rl&&r
1486941
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 4911 ▶ if ( !$is_numeric
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_numeric and @{$rgroup_lines;} and not get_recoverable_spaces($rgroup_lines->[0]{"indentation"})) 389 4522 4911
Condition100%6/6
not $is_numeric and @{$rgroup_lines;} and not get_recoverable_spaces($rgroup_lines->[0]{"indentation"})
!ll&&!rl&&r
450913389
not $is_numeric and @{$rgroup_lines;}
!ll&&!rl&&r
414468402
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 4870 && @{$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 4911 ▶ if ( $is_block_comment
Branch: 1 branch
BranchTrueFalseTotal
if ($is_block_comment and $outdent_long_lines and not @{$rgroup_lines;}) 604 4307 4911
Condition83%5/6
$is_block_comment and $outdent_long_lines and not @{$rgroup_lines;}
!ll&&!rl&&r
43070604
$is_block_comment and $outdent_long_lines
!ll&&!rl&&r
427631604
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 604 && !@{$rgroup_lines} )
942 {
943 604 $self->[_group_type_] = 'COMMENT';
944 604 $self->[_comment_leading_space_count_] = $leading_space_count;
945 604 $self->[_group_maximum_line_length_] = $maximum_line_length;
946 604 push @{$rgroup_lines},
947 [ $rfields->[0], $rfield_lengths->[0], $Kend ];
948 604 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 4307 ▶ if ( !@{$rgroup_lines}
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$rgroup_lines;} or $is_numeric or get_recoverable_spaces($indentation)) 4216 91 4307
Condition100%6/6
@{$rgroup_lines;} or $is_numeric or get_recoverable_spaces($indentation)
l!l&&r!l&&!r
9014216
@{$rgroup_lines;} or $is_numeric
l!l&&r!l&&!r
76144217
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 4216 $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 4216 return;
974 }
975 }
976 else {
977 3560 $self->[_zero_count_] = 0;
978 }
979  
980 # --------------------------------------------------------------------
981 # It simplifies things to create a zero length side comment
982 # if none exists.
983 # --------------------------------------------------------------------
984 3651 ▶ if ( ( $jmax == 0 ) || ( $rtokens->[ $jmax - 1 ] ne '#' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax == 0 or $$rtokens[$jmax - 1] ne "#") 3303 348 3651
Condition100%3/3
$jmax == 0 or $$rtokens[$jmax - 1] ne "#"
l!l&&r!l&&!r
913212348
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 3303 $jmax += 1;
986 3303 $rtokens->[ $jmax - 1 ] = '#';
987 3303 $rfields->[$jmax] = EMPTY_STRING;
988 3303 $rfield_lengths->[$jmax] = 0;
989 3303 $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 3651 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 3651 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 3651 ▶ decide_if_list($new_line) if ($list_seqno);
Branch: 1 branch
BranchTrueFalseTotal
if $list_seqno 1161 2490 3651
1036  
1037 # --------------------------------------------------------------------
1038 # Append this line to the current group (or start new group)
1039 # --------------------------------------------------------------------
1040  
1041 3651 push @{ $self->[_rgroup_lines_] }, $new_line;
1042 3651 $self->[_group_maximum_line_length_] = $maximum_line_length;
1043  
1044 # output this group if it ends in a terminal else or ternary line
1045 3651 ▶ if ( defined($j_terminal_match) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $j_terminal_match) { } 20 3631 3651
elsif ($level_end < $level or $is_closing_token{substr $rfields->[0], 0, 1}) { } 144 3487 3631
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
46983487
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 3651 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 3651 return;
1075 } ## end sub valign_input
1076  
1077 38 ▶sub join_hanging_comment {
Subroutine join_hanging_comment: called CC=6 SCAR=18.0
Pod undocumented
1078  
1079 38 my ( $new_line, $old_line ) = @_;
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 1205 ▶sub decide_if_list {
Subroutine decide_if_list: called CC=4 SCAR=13.9
Pod undocumented
1133  
1134 1205 my $line = shift;
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 13 ▶sub fix_terminal_ternary {
Subroutine fix_terminal_ternary: called CC=21 SCAR=37.9
Pod undocumented
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) = @_;
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 9 ▶sub fix_terminal_else {
Subroutine fix_terminal_else: called CC=13 SCAR=27.4
Pod undocumented
1351  
1352 9 my ($rcall_hash) = @_;
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 0 ▶ 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 45 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
1438 45 $is_closing_block_type{$_} = 1 for qw# } ] #;
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 1327 ▶sub check_match {
Subroutine check_match: called CC=13 SCAR=26.0
Pod undocumented
1449  
1450 1327 my ( $self, $new_line, $base_line, $prev_line, $group_line_count ) = @_;
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 1327 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 1327 my $imax_align = -1;
1476  
1477 # variable $GoToMsg explains reason for no match, for debugging
1478 1327 my $GoToMsg = EMPTY_STRING;
1479  
1480 1327 my $jmax = $new_line->{'jmax'};
1481 1327 my $maximum_field_index = $base_line->{'jmax'};
1482  
1483 1327 my $jlimit = $jmax - 2;
1484 1327 ▶ if ( $jmax > $maximum_field_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax > $maximum_field_index) 103 1224 1327
1485 103 $jlimit = $maximum_field_index - 2;
1486 }
1487  
1488 1327 ▶ if ( $new_line->{'is_hanging_side_comment'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_line->{'is_hanging_side_comment'}) { } 38 1289 1327
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 1289 ▶ if ( $base_line->{'is_hanging_side_comment'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_line->{'is_hanging_side_comment'}) { } 0 1289 1289
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 1289 $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 1289 my $ci_prev = $prev_line->{'ci_level'};
1521 1289 my $ci_new = $new_line->{'ci_level'};
1522 1289 ▶ 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 1289 1289
elsif ($imax_align != $jlimit) { } 37 1252 1289
Condition66%6/9
$ci_prev != $ci_new and $imax_align >= 0 and $ci_new == 0 || $group_line_count > 1
!ll&&!rl&&r
128270
$ci_prev != $ci_new and $imax_align >= 0
!ll&&!rl&&r
127487
$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 37 $GoToMsg = "Not all tokens match: $imax_align != $jlimit\n";
1533 37 $return_value = NO_MATCH;
1534 }
1535 else {
1536 ## ok: continue
1537 }
1538 }
1539 }
1540  
1541 1327 ▶ if ( !defined($return_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $return_value) 1290 37 1327
1542  
1543 # The tokens match, but the lines must have identical number of
1544 # tokens to join the group.
1545 1290 ▶ if ( $maximum_field_index != $jmax ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($maximum_field_index != $jmax) { } 143 1147 1290
elsif ($self->check_fit($new_line, $base_line) and !0) { } 1133 14 1147
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
141133
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 1133 $GoToMsg = "match and fit, imax_align=$imax_align, jmax=$jmax\n";
1556 1133 $return_value = MATCH_AND_FIT;
1557 1133 $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 1327 && print
1568 "returning $return_value because $GoToMsg, max match index =i $imax_align, jmax=$jmax\n";
1569  
1570 1327 return ( $return_value, $imax_align );
1571 } ## end sub check_match
1572  
1573 1147 ▶sub check_fit {
Subroutine check_fit: called CC=8 SCAR=20.9
Pod undocumented
1574  
1575 1147 my ( $self, $new_line, $old_line ) = @_;
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 1147 my $jmax = $new_line->{'jmax'};
1589 1147 my $leading_space_count = $new_line->{'leading_space_count'};
1590 1147 my $rfield_lengths = $new_line->{'rfield_lengths'};
1591 1147 my $padding_available = $old_line->get_available_space_on_right();
1592 1147 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 1147 ▶ if ( $jmax_old ne $jmax ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax_old ne $jmax) 0 1147 1147
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 1147 my @alignments = @{ $old_line->{'ralignments'} };
1609 1147 foreach my $alignment (@alignments) {
1610 4056 $alignment->save_column();
1611 }
1612  
1613 # Loop over all alignments ...
1614 1147 for my $j ( 0 .. $jmax ) {
1615  
1616 4037 my $pad = $rfield_lengths->[$j] - $old_line->current_field_width($j);
1617  
1618 4037 ▶ if ( $j == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 0) 1147 2890 4037
1619 1147 $pad += $leading_space_count;
1620 }
1621  
1622 # Keep going if this field does not need any space.
1623 4037 ▶ next if ( $pad < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pad < 0 1328 2709 4037
1624  
1625 # Revert to the starting state if does not fit
1626 2709 ▶ if ( $pad > $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > $padding_available) 14 2695 2709
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 2695 $old_line->increase_field_width( $j, $pad );
1639 2695 $padding_available -= $pad;
1640 }
1641  
1642 #-------------------------------------
1643 # The line fits, the match is accepted
1644 #-------------------------------------
1645 1133 return 1;
1646  
1647 } ## end sub check_fit
1648  
1649 2518 ▶sub install_new_alignments {
Subroutine install_new_alignments: called CC=2 SCAR=6.9
Pod undocumented
1650  
1651 2518 my ($new_line) = @_;
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 2518 my $jmax = $new_line->{'jmax'};
1659 2518 my $rfield_lengths = $new_line->{'rfield_lengths'};
1660 2518 my $col = $new_line->{'leading_space_count'};
1661  
1662 2518 my @alignments;
1663 2518 for my $j ( 0 .. $jmax ) {
1664 8450 $col += $rfield_lengths->[$j];
1665  
1666 # create initial alignments for the new group
1667 8450 my $alignment =
1668 Perl::Tidy::VerticalAligner::Alignment->new( { column => $col } );
1669 8450 push @alignments, $alignment;
1670 }
1671 2518 $new_line->{'ralignments'} = \@alignments;
1672 2518 return;
1673 } ## end sub install_new_alignments
1674  
1675 1133 ▶sub copy_old_alignments {
Subroutine copy_old_alignments: called CC=1 SCAR=0.0
Pod undocumented
1676 1133 my ( $new_line, $old_line ) = @_;
1677 1133 my @new_alignments = @{ $old_line->{'ralignments'} };
1678 1133 $new_line->{'ralignments'} = \@new_alignments;
1679 1133 return;
1680 } ## end sub copy_old_alignments
1681  
1682 0 ▶sub dump_array {
Subroutine dump_array: not called CC=1 SCAR=6.9
Pod undocumented
1683  
1684 # debug routine to dump array contents
1685 0 local $LIST_SEPARATOR = ')(';
1686 0 print {*STDOUT} "(@_)\n";
1687 0 return;
1688 } ## end sub dump_array
1689  
1690 10 ▶sub level_change {
Subroutine level_change: called CC=3 SCAR=11.4
Pod undocumented
1691  
1692 10 my ( $self, $leading_space_count, $diff, $level ) = @_;
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 0 ▶ 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 604 ▶sub _flush_comment_lines {
Subroutine _flush_comment_lines: called CC=8 SCAR=20.8
1719  
1720 604 my ($self) = @_;
1721  
1722 # Output a group consisting of COMMENT lines
1723  
1724 604 my $rgroup_lines = $self->[_rgroup_lines_];
1725 604 ▶ return if ( !@{$rgroup_lines} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rgroup_lines;} 0 604 604
1726 604 my $group_level = $self->[_group_level_];
1727 604 my $group_maximum_line_length = $self->[_group_maximum_line_length_];
1728 604 my $leading_space_count = $self->[_comment_leading_space_count_];
1729  
1730 # look for excessively long lines
1731 604 my $max_excess = 0;
1732 604 foreach my $item ( @{$rgroup_lines} ) {
1733 694 my ( $str_uu, $str_len ) = @{$item};
1734 694 my $excess =
1735 $str_len + $leading_space_count - $group_maximum_line_length;
1736 694 ▶ if ( $excess > $max_excess ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess > $max_excess) 39 655 694
1737 39 $max_excess = $excess;
1738 }
1739 }
1740  
1741 # zero leading space count if any lines are too long
1742 604 ▶ if ( $max_excess > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_excess > 0) 37 567 604
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 604 my $outdent_long_lines = 0;
1761  
1762 604 foreach my $item ( @{$rgroup_lines} ) {
1763 694 my ( $str, $str_len, $Kend ) = @{$item};
1764 694 $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 604 $self->initialize_for_new_group();
1781 604 return;
1782 } ## end sub _flush_comment_lines
1783  
1784 ######################################################
1785 # CODE SECTION 5: Code to process groups of code lines
1786 ######################################################
1787  
1788 2671 ▶sub _flush_group_lines {
Subroutine _flush_group_lines: called CC=13 SCAR=25.7
1789  
1790 2671 my ( $self, ($level_jump) ) = @_;
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 2671 my $rgroup_lines = $self->[_rgroup_lines_];
1800 2671 ▶ return if ( !@{$rgroup_lines} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rgroup_lines;} 0 2671 2671
1801 2671 my $group_type = $self->[_group_type_];
1802 2671 my $group_level = $self->[_group_level_];
1803  
1804 # Debug
1805 2671 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 2671 ▶ if ( $group_type eq 'COMMENT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_type eq "COMMENT") 604 2067 2671
1816 604 $self->_flush_comment_lines();
1817 604 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 2067 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 2067 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 2067 ▶ if ( @{$rgroups} > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @{$rgroups;} > 1 302 1765 2067
1838  
1839 # STEP 4: Move side comments to a common column if possible.
1840 2067 ▶ if ($saw_side_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_side_comment) 222 1845 2067
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 2067 my $extra_indent_ok;
1852 2067 ▶ if ( $group_level > $self->[_last_level_written_] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_level > $self->[4]) 1036 1031 2067
1853  
1854 # Use the level jump to next line to come, if given
1855 1036 ▶ if ( defined($level_jump) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level_jump) { } 681 355 1036
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 355 my $level_end = $rgroup_lines->[-1]->{'level_end'};
1863 355 $extra_indent_ok = $group_level > $level_end;
1864 }
1865 }
1866  
1867 2067 ▶ my $extra_leading_spaces =
Branch: 1 branch
BranchTrueFalseTotal
$extra_indent_ok ? : 445 1622 2067
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 2067 ▶ pad_signed_number_columns($rgroup_lines)
Branch: 1 branch
BranchTrueFalseTotal
if $saw_signed_number and $rOpts_valign_signed_numbers 70 1997 2067
Condition100%3/3
$saw_signed_number and $rOpts_valign_signed_numbers
!ll&&!rl&&r
1995270
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 2067 ▶ pad_wide_equals_columns($rgroup_lines)
Branch: 1 branch
BranchTrueFalseTotal
if $rOpts_valign_wide_equals 12 2055 2067
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 2067 my $group_leader_length = $rgroup_lines->[0]->{'leading_space_count'};
1884 2067 my $group_maximum_line_length = $rgroup_lines->[0]->{'maximum_line_length'};
1885  
1886 2067 foreach my $line ( @{$rgroup_lines} ) {
1887 3651 $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 2067 my $object = $rgroup_lines->[0]->{'indentation'};
1904 2067 ▶ if ( ref($object) ) { $object->set_recoverable_spaces(0) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object) 94 1973 2067
1905  
1906 2067 $self->initialize_for_new_group();
1907 2067 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 4585 ▶ sub initialize_for_new_rgroup {
Subroutine initialize_for_new_rgroup: called CC=1 SCAR=0.0
Pod undocumented
1920 4585 $group_line_count = 0;
1921 4585 return;
1922 }
1923  
1924 3651 ▶ sub add_to_rgroup {
Subroutine add_to_rgroup: called CC=2 SCAR=6.9
Pod undocumented
1925  
1926 3651 my ($jend) = @_;
1927  
1928 # Include the line at index $jend in the current alignment group
1929  
1930 3651 my $rline = $rall_lines->[$jend];
1931  
1932 3651 my $jbeg = $jend;
1933 3651 ▶ if ( $group_line_count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_line_count == 0) { } 2518 1133 3651
1934 2518 install_new_alignments($rline);
1935 }
1936 else {
1937 1133 my $rvals = pop @{$rgroups};
1938 1133 $jbeg = $rvals->[0];
1939 1133 copy_old_alignments( $rline, $rall_lines->[$jbeg] );
1940 }
1941 3651 push @{$rgroups}, [ $jbeg, $jend, undef ];
1942 3651 $group_line_count++;
1943 3651 return;
1944 } ## end sub add_to_rgroup
1945  
1946 1503 ▶ sub get_rgroup_jrange {
Subroutine get_rgroup_jrange: called CC=3 SCAR=11.2
Pod undocumented
1947  
1948 1503 ▶ return if ( !@{$rgroups} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rgroups;} 0 1503 1503
1949 1503 ▶ return if ( $group_line_count <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $group_line_count <= 0 0 1503 1503
1950 1503 my ( $jbeg, $jend ) = @{ $rgroups->[-1] };
1951 1503 return ( $jbeg, $jend );
1952 } ## end sub get_rgroup_jrange
1953  
1954 2537 ▶ sub end_rgroup {
Subroutine end_rgroup: called CC=7 SCAR=19.5
Pod undocumented
1955  
1956 2537 my ($imax_align) = @_;
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 2537 ▶ return if ( !@{$rgroups} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rgroups;} 0 2537 2537
1963 2537 ▶ return if ( $group_line_count <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $group_line_count <= 0 19 2518 2537
1964  
1965 2518 my ( $jbeg, $jend ) = @{ pop @{$rgroups} };
1966 2518 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 2518 ▶ if ( $jend - $jbeg == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jend - $jbeg == 1) 293 2225 2518
1971 293 my $line_0 = $rall_lines->[$jbeg];
1972 293 my $line_1 = $rall_lines->[$jend];
1973  
1974 293 my $imax_pair = $line_1->{'imax_pair'};
1975 0 ▶ if ( $imax_pair > $imax_align ) { $imax_align = $imax_pair }
Branch: 1 branch
BranchTrueFalseTotal
if ($imax_pair > $imax_align) 0 293 293
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 293 ▶ $jbeg > 0 ? $rall_lines->[ $jbeg - 1 ]->{'imax_pair'} : -1;
Branch: 1 branch
BranchTrueFalseTotal
$jbeg > 0 ? : 74 219 293
1984  
1985 293 my ( $is_marginal, $imax_align_fix ) =
1986 is_marginal_match( $line_0, $line_1, $grp_level, $imax_align,
1987 $imax_prev );
1988 293 ▶ if ($is_marginal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_marginal) 16 277 293
1989 16 combine_fields( $line_0, $line_1, $imax_align_fix );
1990 }
1991 }
1992  
1993 2518 initialize_for_new_rgroup();
1994 2518 return;
1995 } ## end sub end_rgroup
1996  
1997 1 ▶ sub block_penultimate_match {
Subroutine block_penultimate_match: called CC=2 SCAR=7.0
Pod undocumented
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 );
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 2067 ▶ sub sweep_top_down {
Subroutine sweep_top_down: called CC=31 SCAR=34.8
Pod undocumented
2007  
2008 2067 my ( $self, $rlines, $group_level ) = @_;
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 2067 $rall_lines = $rlines;
2029 2067 $grp_level = $group_level;
2030 2067 $rgroups = [];
2031 2067 initialize_for_new_rgroup();
2032 2067 ▶ if ( !@{$rlines} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$rlines;}) 0 2067 2067
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 2067 $rall_lines->[-1]->{'end_group'} = 0;
2040  
2041 # Loop over all lines ...
2042 2067 my $jline = -1;
2043 2067 foreach my $new_line ( @{$rall_lines} ) {
2044 3651 $jline++;
2045  
2046 # Start a new subgroup if necessary
2047 3651 ▶ if ( !$group_line_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($group_line_count) 2148 1503 3651
2048 2148 add_to_rgroup($jline);
2049 2148 ▶ if ( $new_line->{'end_group'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_line->{"end_group"}) 26 2122 2148
2050 26 end_rgroup(-1);
2051 }
2052 2148 next;
2053 }
2054  
2055 1503 my $j_terminal_match = $new_line->{'j_terminal_match'};
2056 1503 my ( $jbeg, $jend_uu ) = get_rgroup_jrange();
2057 1503 ▶ if ( !defined($jbeg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $jbeg) 0 1503 1503
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 1503 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 1503 my $col_matching_terminal = 0;
2073 1503 ▶ if ( defined($j_terminal_match) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $j_terminal_match) 19 1484 1503
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 1503 ▶ if ( $new_line->{'is_hanging_side_comment'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_line->{'is_hanging_side_comment'}) { } 38 1465 1503
elsif ($new_line->{'jmax'} == 1) { } 248 1217 1465
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 1503 my $match_code;
2142 1503 ▶ if ($group_line_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_line_count) 1327 176 1503
2143 1327 ( $match_code, my $imax_align ) =
2144 $self->check_match( $new_line, $base_line,
2145 $rall_lines->[ $jline - 1 ],
2146 $group_line_count );
2147 1327 ▶ if ( $match_code != 2 ) { end_rgroup($imax_align) }
Branch: 1 branch
BranchTrueFalseTotal
if ($match_code != 2) 194 1133 1327
2148 }
2149  
2150 # Store the new line
2151 1503 add_to_rgroup($jline);
2152  
2153 1503 ▶ if ( defined($j_terminal_match) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $j_terminal_match) { } 19 1484 1503
elsif ($new_line->{'end_group'}) { } 55 1429 1484
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 55 end_rgroup(-1);
2202 }
2203  
2204 else {
2205 ## ok: continue
2206 }
2207 } ## end loop over lines
2208  
2209 2067 end_rgroup(-1);
2210 2067 return ($rgroups);
2211 } ## end sub sweep_top_down
2212 }
2213  
2214 45 ▶sub two_line_pad {
Subroutine two_line_pad: called CC=13 SCAR=26.6
Pod undocumented
2215  
2216 21 my ( $line_m, $line, $imax_min ) = @_;
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 0 ▶ 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 302 ▶sub sweep_left_to_right {
Subroutine sweep_left_to_right: called CC=30 SCAR=34.1
Pod undocumented
2284  
2285 302 my ( $rlines, $rgroups, $group_level ) = @_;
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) 63 388 451
2380 63 $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) 82 369 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
19617382
$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
1951255
$jend == $jbeg and $jend_m == $jbeg_m and $ng == 1 || $istop_mm < 0 and $ng == $ng_max || $istop < 0
!ll&&!rl&&r
1869256
$jend == $jbeg and $jend_m == $jbeg_m and $ng == 1 || $istop_mm < 0
!ll&&!rl&&r
17412265
$jend == $jbeg and $jend_m == $jbeg_m
!ll&&!rl&&r
9678277
$ng == 1 or $istop_mm < 0
l!l&&r!l&&!r
1778812
$ng == $ng_max or $istop < 0
l!l&&r!l&&!r
176809
$imax != $imax_m or $istop_m != $imax_m
l!l&&r!l&&!r
766173
MC/DC100%9/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 table76%16/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 82 ▶ next if ( $imax_min < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $imax_min < 0 56 26 82
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 390 ▶ if ( $imax_min >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imax_min >= 0) 98 292 390
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;
Condition66%2/3
$Perl::Tidy::VerticalAligner::a->[0] <=> $Perl::Tidy::VerticalAligner::b->[0] || $Perl::Tidy::VerticalAligner::a->[1] <=> $Perl::Tidy::VerticalAligner::b->[1]
l!l&&r!l&&!r
146530
MC/DC0%0/2
$Perl::Tidy::VerticalAligner::a->[0] <=> $Perl::Tidy::VerticalAligner::b->[0] || $Perl::Tidy::VerticalAligner::a->[1] <=> $Perl::Tidy::VerticalAligner::b->[1]
$Perl::Tidy::VerticalAligner::a->[0] <=> $Perl::Tidy::VerticalAligner::b->[0] $Perl::Tidy::VerticalAligner::a->[1] <=> $Perl::Tidy::VerticalAligner::b->[1]
Truth table66%2/3
A || B
A= $Perl::Tidy::VerticalAligner::a->[0] <=> $Perl::Tidy::VerticalAligner::b->[0]B= $Perl::Tidy::VerticalAligner::a->[1] <=> $Perl::Tidy::VerticalAligner::b->[1]
ABresult
1X1
011
000
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 45 ▶ BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
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 || { # );
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 140 ▶ sub move_to_common_column {
Subroutine move_to_common_column: called CC=9 SCAR=22.3
Pod undocumented
2515  
2516 140 my ($rcall_hash) = @_;
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 75 ▶ sub tap_dancer {
Subroutine tap_dancer: called CC=33 SCAR=35.2
Pod undocumented
2560  
2561 75 my ($rcall_hash) = @_;
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
39107
$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/DC80%4/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 table83%5/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
82856
$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
92153
$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) 12 163 175
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 {
Subroutine delete_selected_tokens: called CC=13 SCAR=25.7
Pod undocumented
2783  
2784 545 my ( $line_obj, $ridel ) = @_;
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 1743 my $token = $rtokens_old->[$j];
2833 1743 my $field = $rfields_old->[ $j + 1 ];
2834 1743 my $field_length = $rfield_lengths_old->[ $j + 1 ];
2835 1743 my $pattern = $rpatterns_old->[ $j + 1 ];
2836 1743 ▶ if ( !$delete_me{$j} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $delete_me{$j}) { } 848 895 1743
2837 848 push @{$rtokens_new}, $token;
2838 848 push @{$rfields_new}, $field;
2839 848 push @{$rpatterns_new}, $pattern;
2840 848 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 665 ▶ sub initialize_decode {
Subroutine initialize_decode: called CC=1 SCAR=0.0
Pod undocumented
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 665 %decoded_token = ();
2909 665 return;
2910 } ## end sub initialize_decode
2911  
2912 10963 ▶ sub decode_alignment_token {
Subroutine decode_alignment_token: called CC=5 SCAR=16.1
Pod undocumented
2913  
2914 10963 my ($tok) = @_;
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 10963 ▶ if ( defined( $decoded_token{$tok} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded_token{$tok}) 9276 1687 10963
2942 9276 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 {
Subroutine delete_unmatched_tokens: called CC=7 SCAR=19.5
Pod undocumented
2959  
2960 2067 my ( $rlines, $group_level ) = @_;
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 2067 my $max_lev_diff = 0; # used to avoid a call to prune_tree
2971 2067 my $saw_side_comment = 0; # used to avoid a call for side comments
2972 2067 my $saw_signed_number = 0; # used to avoid a call for -vsn
2973  
2974 # Handle no lines -- shouldn't happen
2975 2067 ▶ return unless ( @{$rlines} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rlines;} 0 2067 2067
2976  
2977 # Handle a single line
2978 2067 ▶ if ( @{$rlines} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rlines;} == 1) 1386 681 2067
2979 1386 my $line = $rlines->[0];
2980 1386 my $jmax = $line->{'jmax'};
2981 1386 my $length = $line->{'rfield_lengths'}->[$jmax];
2982 1386 $saw_side_comment = $length > 0;
2983 1386 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 1537 ▶ if ( $rnew_lines->[$jl]->{'end_group'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rnew_lines->[$jl]{"end_group"}) 81 1456 1537
3012 81 $subgroups[-1]->[1] = $jl;
3013 81 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 680 ▶sub make_alignment_info {
Subroutine make_alignment_info: called CC=27 SCAR=33.0
Pod undocumented
3041  
3042 680 my ( $group_level, $rnew_lines, $saw_side_comment ) = @_;
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 2217 my $rhash = {};
3066 2217 my $rtokens = $line->{'rtokens'};
3067 2217 my $rpatterns = $line->{'rpatterns'};
3068 2217 my $i = 0;
3069 2217 my ( $i_eq, $tok_eq, $pat_eq );
3070 2217 my ( $lev_min, $lev_max );
3071 2217 foreach my $tok ( @{$rtokens} ) {
3072 6103 my ( $raw_tok, $lev, $tag, $tok_count ) =
3073 decode_alignment_token($tok);
3074  
3075 6103 ▶ if ( $tok ne '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok ne '#') { } 3886 2217 6103
3076 3886 ▶ if ( !defined($lev_min) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lev_min) { } 2049 1837 3886
3077 2049 $lev_min = $lev;
3078 2049 $lev_max = $lev;
3079 }
3080 else {
3081 1837 ▶ if ( $lev < $lev_min ) { $lev_min = $lev }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev < $lev_min) 84 1753 1837
3082 1837 ▶ if ( $lev > $lev_max ) { $lev_max = $lev }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev > $lev_max) 305 1532 1837
3083 }
3084 }
3085 else {
3086 2217 ▶ if ( !$saw_side_comment ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_side_comment) 2010 207 2217
3087 2010 my $length = $line->{'rfield_lengths'}->[ $i + 1 ];
3088 2010 ▶ $saw_side_comment ||= $length;
Condition66%2/3
$saw_side_comment ||= $length
l!l&&r!l&&!r
0681942
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 6103 $rhash->{$tok} =
3095 [ $i, undef, undef, $raw_tok, $lev, $tag, $tok_count ];
3096  
3097 # remember the first equals at line level
3098 6103 ▶ if ( !defined($i_eq) && $raw_tok eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $i_eq and $raw_tok eq "=") 638 5465 6103
Condition100%3/3
not defined $i_eq and $raw_tok eq "="
!ll&&!rl&&r
8714594638
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 6103 $i++;
3107 }
3108 2217 push @{$rline_hashes}, $rhash;
3109 2217 push @equals_info, [ $i_eq, $tok_eq, $pat_eq ];
3110 2217 ▶ if ( defined($lev_min) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lev_min) 2049 168 2217
3111 2049 my $lev_diff = $lev_max - $lev_min;
3112 2049 ▶ if ( $lev_diff > $max_lev_diff ) { $max_lev_diff = $lev_diff }
Branch: 1 branch
BranchTrueFalseTotal
if ($lev_diff > $max_lev_diff) 182 1867 2049
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 1537 my $nl = $nr;
3123 1537 $nr = 0;
3124 1537 my $jr = $jl + 1;
3125 1537 my $rhash_l = $rline_hashes->[$jl];
3126 1537 my $rhash_r = $rline_hashes->[$jr];
3127 1537 foreach my $tok ( keys %{$rhash_l} ) {
3128 3677 ▶ if ( defined( $rhash_r->{$tok} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rhash_r->{$tok}) 3114 563 3677
3129 3114 my $il = $rhash_l->{$tok}->[0];
3130 3114 my $ir = $rhash_r->{$tok}->[0];
3131 3114 $rhash_l->{$tok}->[2] = $ir;
3132 3114 $rhash_r->{$tok}->[1] = $il;
3133 3114 ▶ if ( $tok ne '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok ne "#") 1577 1537 3114
3134 1577 push @{ $rtok_hash->{$tok} }, ( $jl, $jr );
3135 1577 $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 1537 ▶ if ( $nr == 0 && $nl > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nr == 0 and $nl > 0) 41 1496 1537
Condition100%3/3
$nr == 0 and $nl > 0
!ll&&!rl&&r
119030641
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 41 $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 1537 my ( $i_eq_l, $tok_eq_l, $pat_eq_l ) = @{ $equals_info[$jl] };
3162 1537 my ( $i_eq_r, $tok_eq_r, $pat_eq_r ) = @{ $equals_info[$jr] };
3163 1537 ▶ if ( defined($i_eq_l) && defined($i_eq_r) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i_eq_l and defined $i_eq_r) 268 1269 1537
Condition100%3/3
defined $i_eq_l and defined $i_eq_r
!ll&&!rl&&r
1163106268
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
525013
$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/DC60%3/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 table66%4/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 680 ▶sub delete_unmatched_tokens_main_loop {
Subroutine delete_unmatched_tokens_main_loop: called CC=51 SCAR=39.3
Pod undocumented
3185  
3186 680 my ( $group_level, $rnew_lines, $rsubgroups, $rline_hashes, $requals_info )
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 761 my ( $jbeg, $jend ) = @{$item};
3200  
3201 761 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 761 my $dividing_token;
3230 my %token_line_count;
3231 761 ▶ if ( $nlines > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nlines > 2) 358 403 761
3232  
3233 358 foreach my $jj ( $jbeg .. $jend ) {
3234 1514 my %seen;
3235 1514 my $line = $rnew_lines->[$jj];
3236 1514 my $rtokens = $line->{'rtokens'};
3237 1514 foreach my $tok ( @{$rtokens} ) {
3238 4289 ▶ if ( !$seen{$tok} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$tok}) 3614 675 4289
3239 3614 $seen{$tok}++;
3240 3614 $token_line_count{$tok}++;
3241 }
3242 }
3243 }
3244  
3245 358 foreach my $tok ( keys %token_line_count ) {
3246 1104 ▶ if ( $token_line_count{$tok} == $nlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_line_count{$tok} == $nlines) 663 441 1104
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
176640
substr($tok, 0, 1) eq "{" and $tok =~ /^\{\d+if/
!ll&&!rl&&r
626146
MC/DC100%3/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 table100%4/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 761 foreach my $jj ( $jbeg .. $jend ) {
3261 2217 my $line = $rnew_lines->[$jj];
3262 2217 my $rtokens = $line->{'rtokens'};
3263 2217 my $rhash = $rline_hashes->[$jj];
3264 2217 my $i_eq = $requals_info->[$jj]->[0];
3265 2217 my @idel;
3266 2217 my $imax = @{$rtokens} - 2;
3267 2217 my $delete_above_level;
3268 my $deleted_assignment_token;
3269  
3270 2217 my $saw_dividing_token = EMPTY_STRING;
3271 2217 ▶ $saw_large_group ||= $nlines > 2 && $imax > 1;
Condition100%6/6
$saw_large_group ||= $nlines > 2 && $imax > 1
l!l&&r!l&&!r
4301301657
$nlines > 2 && $imax > 1
!ll&&!rl&&r
695962130
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 2217 foreach my $i ( 0 .. $imax ) {
3275 3886 my $tok = $rtokens->[$i];
3276 3886 ▶ next if ( $tok eq '#' ); # shouldn't happen
Branch: 1 branch
BranchTrueFalseTotal
if $tok eq "#" 0 3886 3886
3277 my ( $iii_uu, $il, $ir, $raw_tok, $lev, $tag_uu, $tok_count ) =
3278 3886 @{ $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 3886 ▶ my $delete_me = !defined($il) && !defined($ir);
Condition100%3/3
defined $il || defined $ir
l!l&&r!l&&!r
2107937842
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 3886 my $align_ok = 1;
3289 3886 ▶ if (%valign_control_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (%valign_control_hash) 49 3837 3886
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 3886 ▶ if ($dividing_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dividing_token) 157 3729 3886
3303 157 ▶ if ( $token_line_count{$tok} >= $nlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_line_count{$tok} >= $nlines) { } 128 29 157
3304 128 ▶ $saw_dividing_token ||= $tok eq $dividing_token;
Condition100%3/3
$saw_dividing_token ||= $tok eq $dividing_token
l!l&&r!l&&!r
158924
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 29 ▶ $delete_me ||= ( $nlines < 5 || !$saw_dividing_token );
Condition83%5/6
$delete_me ||= $nlines < 5 || !$saw_dividing_token
l!l&&r!l&&!r
1937
$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 3886 ▶ if ( defined($delete_above_level) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $delete_above_level) 332 3554 3886
3327 332 ▶ if ( $lev > $delete_above_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lev > $delete_above_level) { } 160 172 332
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 172 $delete_above_level = undef;
3332 }
3333 }
3334  
3335 # EXCEPTION 3: Remove all but certain tokens after an
3336 # assignment deletion.
3337 3886 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($deleted_assignment_token and $lev > $group_level || !$is_if_or{$raw_tok}) 62 3824 3886
Condition100%6/6
$deleted_assignment_token and $lev > $group_level || !$is_if_or{$raw_tok}
!ll&&!rl&&r
38071762
$lev > $group_level or not $is_if_or{$raw_tok}
l!l&&r!l&&!r
243817
MC/DC100%3/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 table100%4/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 3886 ▶ $delete_me = 0
Branch: 1 branch
BranchTrueFalseTotal
if $jj == $jbeg and $has_terminal_match and $nlines == 2 18 3868 3886
Condition100%6/6
$jj == $jbeg and $has_terminal_match and $nlines == 2
!ll&&!rl&&r
38561218
$jj == $jbeg and $has_terminal_match
!ll&&!rl&&r
2572128430
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 3886 ▶ if ( $delete_me && $tok_count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_me and $tok_count == 1) 843 3043 3886
Condition100%3/3
$delete_me and $tok_count == 1
!ll&&!rl&&r
299746843
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 3886 ▶ $delete_me ||= !$align_ok;
Condition66%2/3
$delete_me ||= !$align_ok
l!l&&r!l&&!r
78003106
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 3886 ▶ if ($delete_me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_me) 780 3106 3886
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 2217 ▶ if (@idel) {
Branch: 1 branch
BranchTrueFalseTotal
if (@idel) 485 1732 2217
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 680 ▶sub match_line_pairs {
Subroutine match_line_pairs: called CC=27 SCAR=33.0
Pod undocumented
3428  
3429 680 my ( $rlines, $rnew_lines, $rsubgroups, $group_level ) = @_;
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 761 my ( $jbeg, $jend ) = @{$item};
3460 761 my $nlines = $jend - $jbeg + 1;
3461 761 ▶ next if ( $nlines <= 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $nlines <= 1 103 658 761
3462  
3463 # loop over lines in a subgroup
3464 658 foreach my $jj ( $jbeg .. $jend ) {
3465  
3466 2114 $line_m = $line;
3467 2114 $rtokens_m = $rtokens;
3468 2114 $rpatterns_m = $rpatterns;
3469 2114 $rfield_lengths_m = $rfield_lengths;
3470 2114 $imax_m = $imax;
3471 2114 $list_type_m = $list_type;
3472 2114 $ci_level_m = $ci_level;
3473  
3474 2114 $line = $rnew_lines->[$jj];
3475 2114 $rtokens = $line->{'rtokens'};
3476 2114 $rpatterns = $line->{'rpatterns'};
3477 2114 $rfield_lengths = $line->{'rfield_lengths'};
3478 2114 $imax = @{$rtokens} - 2;
3479 2114 $list_type = $line->{'list_type'};
3480 2114 $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 2114 ▶ if ( !$saw_signed_number ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_signed_number) 1941 173 2114
3485  
3486 1941 my $rfields = $line->{'rfields'};
3487 1941 foreach my $i ( 0 .. $imax + 1 ) {
3488 4633 ▶ next if ( index( $rpatterns->[$i], 'n' ) < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if (index($rpatterns->[$i], "n") == -1) 3679 954 4633
3489 954 my $field = $rfields->[$i];
3490 954 ▶ if ( index( $field, '-' ) >= 0
Branch: 1 branch
BranchTrueFalseTotal
if (index($field, "-") != -1 or index($field, "+") != -1) 72 882 954
Condition100%3/3
index($field, "-") != -1 or index($field, "+") != -1
l!l&&r!l&&!r
5220882
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 2114 ▶ next if ( $jj == $jbeg );
Branch: 1 branch
BranchTrueFalseTotal
if $jj == $jbeg 658 1456 2114
3501  
3502 1456 my $ci_jump = $ci_level - $ci_level_m;
3503  
3504 1456 ▶ my $imax_min = $imax_m < $imax ? $imax_m : $imax;
Branch: 1 branch
BranchTrueFalseTotal
$imax_m < $imax ? : 103 1353 1456
3505  
3506 1456 my $imax_align = -1;
3507  
3508 # find number of leading common tokens
3509  
3510 #---------------------------------
3511 # No match to hanging side comment
3512 #---------------------------------
3513 1456 ▶ if ( $line->{'is_hanging_side_comment'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line->{'is_hanging_side_comment'}) { } 0 1456 1456
elsif ($list_type and $list_type eq $list_type_m) { } 526 930 1456
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
90624526
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 0 ▶ 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 930 my $i_nomatch = $imax_min + 1;
3545 930 foreach my $i ( 0 .. $imax_min ) {
3546 919 my $tok = $rtokens->[$i];
3547 919 my $tok_m = $rtokens_m->[$i];
3548 919 ▶ if ( $tok ne $tok_m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok ne $tok_m) 30 889 919
3549 30 $i_nomatch = $i;
3550 30 last;
3551 }
3552  
3553 889 my $pat = $rpatterns->[$i];
3554 889 my $pat_m = $rpatterns_m->[$i];
3555  
3556 # VSN PATCH: allow numbers to match quotes
3557 889 ▶ if ( $pat_m ne $pat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_m ne $pat) 198 691 889
3558 198 $pat =~ tr/n/Q/;
3559 198 $pat_m =~ tr/n/Q/;
3560 }
3561  
3562 # If patterns don't match, we have to be careful...
3563 889 ▶ if ( $pat_m ne $pat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_m ne $pat) 186 703 889
3564 186 my $pad =
3565 $rfield_lengths->[$i] - $rfield_lengths_m->[$i];
3566 186 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 186 ▶ if ($match_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_code) 7 179 186
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 930 $imax_align = $i_nomatch - 1;
3585 }
3586  
3587 1456 $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 186 ▶sub compare_patterns {
Subroutine compare_patterns: called CC=9 SCAR=22.1
Pod undocumented
3613  
3614 186 my ($rcall_hash) = @_;
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 186 my $group_level = $rcall_hash->{group_level};
3628 186 my $tok = $rcall_hash->{tok};
3629 ## my $tok_m = $rcall_hash->{tok_m};
3630 186 my $pat = $rcall_hash->{pat};
3631 186 my $pat_m = $rcall_hash->{pat_m};
3632 186 my $pad = $rcall_hash->{pad};
3633  
3634 186 my $GoToMsg = EMPTY_STRING;
3635 186 my $return_code = 0;
3636  
3637 45 ▶ use constant EXPLAIN_COMPARE_PATTERNS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3638  
3639 186 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 186 ▶ if ( $alignment_token eq COMMA ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($alignment_token eq ',') { } 26 160 186
elsif ($alignment_token eq '(') { } 3 157 160
elsif ($alignment_token eq '=') { } 20 137 157
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 137 $return_code = 0;
3730 }
3731  
3732 EXPLAIN_COMPARE_PATTERNS
3733 && $return_code
3734 186 && print {*STDOUT} "no match because $GoToMsg\n";
3735  
3736 186 return $return_code;
3737  
3738 } ## end sub compare_patterns
3739  
3740 845 ▶sub fat_comma_to_comma {
Subroutine fat_comma_to_comma: called CC=2 SCAR=6.9
Pod undocumented
3741  
3742 845 my ($str) = @_;
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 174 ▶sub get_line_token_info {
Subroutine get_line_token_info: called CC=27 SCAR=33.0
Pod undocumented
3757  
3758 174 my ($rlines) = @_;
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 ## 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 _lv_rtoken_patterns => $rtoken_patterns,
3895 _lv_rlevels => \@levs,
3896 _lv_rtoken_indexes => $rtoken_indexes,
3897 _lv_is_monotonic => $is_monotonic,
3898 ## unused:
3899 ## _lv_level_min => $lev_min,
3900 ## _lv_level_max => $lev_max,
3901 ## _lv_imax_true => $imax_true,
3902 ## _lv_imax => $imax,
3903 };
3904  
3905 # debug
3906 703 0 && do {
3907 local $LIST_SEPARATOR = ')(';
3908 print "lev_min=$lev_min, lev_max=$lev_max, levels=(@levs)\n";
3909 foreach my $key ( sort keys %{$rtoken_patterns} ) {
3910 print "$key => $rtoken_patterns->{$key}\n";
3911 print "$key => @{$rtoken_indexes->{$key}}\n";
3912 }
3913 };
3914 } ## end loop over lines
3915 174 return ( $rline_values, $all_monotonic );
3916 } ## end sub get_line_token_info
3917  
3918 174 ▶sub prune_alignment_tree {
Subroutine prune_alignment_tree: called CC=40 SCAR=36.9
Pod undocumented
3919  
3920 174 my ($rlines) = @_;
3921  
3922 # Given:
3923 # $rlines = ref to array of lines in this group
3924  
3925 # Prune the tree of alignments to limit depth of alignments
3926  
3927 174 my $jmax = @{$rlines} - 1;
3928 174 ▶ return if ( $jmax <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $jmax <= 0 0 174 174
3929  
3930 # Vertical alignment in perltidy is done as an iterative process. The
3931 # starting point is to mark all possible alignment tokens ('=', ',', '=>',
3932 # etc) for vertical alignment. Then we have to delete all alignments
3933 # which, if actually made, would detract from overall alignment. This
3934 # is done in several phases of which this is one.
3935  
3936 # In this routine we look at the alignments of a group of lines as a
3937 # hierarchical tree. We will 'prune' the tree to limited depths if that
3938 # will improve overall alignment at the lower depths.
3939 # For each line we will be looking at its alignment patterns down to
3940 # different fixed depths. For each depth, we include all lower depths and
3941 # ignore all higher depths. We want to see if we can get alignment of a
3942 # larger group of lines if we ignore alignments at some lower depth.
3943 # Here is an # example:
3944  
3945 # for (
3946 # [ '$var', sub { join $_, "bar" }, 0, "bar" ],
3947 # [ 'CONSTANT', sub { join "foo", "bar" }, 0, "bar" ],
3948 # [ 'CONSTANT', sub { join "foo", "bar", 3 }, 1, "barfoo3" ],
3949 # [ '$myvar', sub { my $var; join $var, "bar" }, 0, "bar" ],
3950 # );
3951  
3952 # In the above example, all lines have three commas at the lowest depth
3953 # (zero), so if there were no other alignments, these lines would all
3954 # align considering only the zero depth alignment token. But some lines
3955 # have additional comma alignments at the next depth, so we need to decide
3956 # if we should drop those to keep the top level alignments, or keep those
3957 # for some additional low level alignments at the expense losing some top
3958 # level alignments. In this case we will drop the deeper level commas to
3959 # keep the entire collection aligned. But in some cases the decision could
3960 # go the other way.
3961  
3962 # The tree for this example at the zero depth has one node containing
3963 # all four lines, since they are identical at zero level (three commas).
3964 # At depth one, there are three 'children' nodes, namely:
3965 # - lines 1 and 2, which have a single comma in the 'sub' at depth 1
3966 # - line 3, which has 2 commas at depth 1
3967 # - line4, which has a ';' and a ',' at depth 1
3968 # There are no deeper alignments in this example.
3969 # so the tree structure for this example is:
3970 #
3971 # depth 0 depth 1 depth 2
3972 # [lines 1-4] -- [line 1-2] - (empty)
3973 # | [line 3] - (empty)
3974 # | [line 4] - (empty)
3975  
3976 # We can carry this to any depth, but it is not really useful to go below
3977 # depth 2. To cleanly stop there, we will consider depth 2 to contain all
3978 # alignments at depth >=2.
3979  
3980 45 ▶ use constant EXPLAIN_PRUNE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3981  
3982 #-------------------------------------------------------------------
3983 # Prune Tree Step 1. Start by scanning the lines and collecting info
3984 #-------------------------------------------------------------------
3985  
3986 # Note that the caller had this info but we have to redo this now because
3987 # alignment tokens may have been deleted.
3988 174 my ( $rline_values, $all_monotonic ) = get_line_token_info($rlines);
3989  
3990 # If all the lines have levels which increase monotonically from left to
3991 # right, then the sweep-left-to-right pass can do a better job of alignment
3992 # than pruning, and without deleting alignments.
3993 174 ▶ return if ($all_monotonic);
Branch: 1 branch
BranchTrueFalseTotal
if $all_monotonic 138 36 174
3994  
3995 # Contents of $rline_values:
3996 #
3997 # {
3998 ## _lv_level_min => $lev_min,
3999 ## _lv_level_max => $lev_max,
4000 # _lv_rtoken_patterns => $rtoken_patterns,
4001 # _lv_rlevels => \@levs,
4002 # _lv_rtoken_indexes => $rtoken_indexes,
4003 # _lv_is_monotonic => $is_monotonic,
4004 ## _lv_imax_true => $imax_true,
4005 # _lv_imax => $imax,
4006 # };
4007  
4008 # We can work to any depth, but there is little advantage to working
4009 # to a depth greater than 2
4010 36 my $MAX_DEPTH = 2;
4011  
4012 # This arrays will hold the tree of alignment tokens at different depths
4013 # for these lines.
4014 36 my @match_tree;
4015  
4016 # Tree nodes contain these values:
4017 # $match_tree[$depth] =
4018 # {
4019 # _tn_jbeg => $jbeg,
4020 # _tn_jend => $jend,
4021 # _tn_n_parent => $n_parent,
4022 # _tn_level => $level,
4023 ## _tn_pattern => $pattern,
4024 # _tn_nc_beg_p => $nc_beg_p,
4025 # _tn_nc_end_p => $nc_end_p,
4026 ## _tn_rindexes => $rindexes
4027 # };
4028 # where
4029 # $depth = 0,1,2 = index of depth of the match
4030  
4031 # $jbeg beginning index j of the range of lines in this match
4032 # $jend ending index j of the range of lines in this match
4033 # $n_parent = index of the containing group at $depth-1, if it exists
4034 # $level = actual level of code being matched in this group
4035 # $pattern = alignment pattern being matched
4036 # $nc_beg_p = first child
4037 # $nc_end_p = last child
4038 # $rindexes = ref to token indexes
4039  
4040 # the patterns and levels of the current group being formed at each depth
4041 36 my ( @token_patterns_current, @levels_current, @token_indexes_current );
4042  
4043 # the patterns and levels of the next line being tested at each depth
4044 36 my ( @token_patterns_next, @levels_next, @token_indexes_next );
4045  
4046 #-----------------------------------------------------------
4047 # define a recursive worker subroutine for tree construction
4048 #-----------------------------------------------------------
4049  
4050 # This is a recursive routine which is called if a match condition changes
4051 # at any depth when a new line is encountered. It ends the match node
4052 # which changed plus all deeper nodes attached to it.
4053 36 my $end_node;
4054 345 ▶ $end_node = sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
4055  
4056 345 my ( $depth, $jl, $n_parent ) = @_;
4057  
4058 # $depth is the tree depth
4059 # $jl is the index of the line
4060 # $n_parent is index of the parent node of this node
4061  
4062 345 ▶ return if ( $depth > $MAX_DEPTH );
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $MAX_DEPTH 93 252 345
4063  
4064 # end any current group at this depth
4065 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
4066 && defined( $match_tree[$depth] )
4067 77 && @{ $match_tree[$depth] }
4068 && defined( $levels_current[$depth] ) )
4069 {
4070 71 $match_tree[$depth]->[-1]->{_tn_jend} = $jl;
4071 }
4072  
4073 # Define the index of the node we will create below
4074 252 my $ng_self = 0;
4075 252 ▶ if ( defined( $match_tree[$depth] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match_tree[$depth]) 77 175 252
4076 77 $ng_self = @{ $match_tree[$depth] };
4077 }
4078  
4079 # end any next deeper child node(s)
4080 252 $end_node->( $depth + 1, $jl, $ng_self );
4081  
4082 # update the levels being matched
4083 252 $token_patterns_current[$depth] = $token_patterns_next[$depth];
4084 252 $token_indexes_current[$depth] = $token_indexes_next[$depth];
4085 252 $levels_current[$depth] = $levels_next[$depth];
4086  
4087 # Do not start a new group at this level if it is not being used
4088 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
4089 || $depth > 0
4090 && $levels_next[$depth] <= $levels_next[ $depth - 1 ] )
4091 {
4092 127 return;
4093 }
4094  
4095 # Create a node for the next group at this depth. We initially assume
4096 # that it will continue to $jmax, and correct that later if the node
4097 # ends earlier.
4098 125 push @{ $match_tree[$depth] }, {
4099 _tn_jbeg => $jl + 1,
4100 _tn_jend => $jmax,
4101 _tn_n_parent => $n_parent,
4102 _tn_level => $levels_current[$depth],
4103 ## _tn_pattern => $token_patterns_current[$depth],
4104 _tn_nc_beg_p => undef,
4105 _tn_nc_end_p => undef,
4106 ## _tn_rindexes => $token_indexes_current[$depth],
4107 };
4108  
4109 125 return;
4110 36 }; ## end $end_node = sub
4111  
4112 #-----------------------------------------------------
4113 # Prune Tree Step 2. Loop to form the tree of matches.
4114 #-----------------------------------------------------
4115 36 foreach my $jp ( 0 .. $jmax ) {
4116  
4117 # working with two adjacent line indexes, 'm'=minus, 'p'=plus
4118 246 my $jm = $jp - 1;
4119  
4120 # Pull out needed values for the next line
4121 246 my $item_jp = $rline_values->[$jp];
4122 246 my $rtoken_patterns = $item_jp->{_lv_rtoken_patterns};
4123 246 my $rlevs = $item_jp->{_lv_rlevels};
4124 246 my $rtoken_indexes = $item_jp->{_lv_rtoken_indexes};
4125  
4126 # Transfer levels and patterns for this line to the working arrays.
4127 # If the number of levels differs from our chosen MAX_DEPTH ...
4128 # if fewer than MAX_DEPTH: leave levels at missing depths undefined
4129 # if more than MAX_DEPTH: set the MAX_DEPTH level to be the maximum
4130 246 @levels_next = @{$rlevs}[ 0 .. $MAX_DEPTH ];
4131 246 ▶ if ( @{$rlevs} > $MAX_DEPTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rlevs;} > $MAX_DEPTH) 5 241 246
4132 5 $levels_next[$MAX_DEPTH] = $rlevs->[-1];
4133 }
4134 246 my $depth = 0;
4135 246 foreach my $item (@levels_next) {
4136 $token_patterns_next[$depth] =
4137 738 ▶ defined($item) ? $rtoken_patterns->{$item} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $item ? : 340 398 738
4138 $token_indexes_next[$depth] =
4139 738 ▶ defined($item) ? $rtoken_indexes->{$item} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $item ? : 340 398 738
4140 738 $depth++;
4141 }
4142  
4143 # Look for a change in match groups...
4144  
4145 # Initialize on the first line
4146 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
4147 36 my $n_parent;
4148 36 $end_node->( 0, $jm, $n_parent );
4149 }
4150  
4151 # End groups if a hard flag has been set
4152 elsif ( $rlines->[$jm]->{'end_group'} ) {
4153 11 my $n_parent;
4154 11 $end_node->( 0, $jm, $n_parent );
4155 }
4156  
4157 # Continue at hanging side comment
4158 elsif ( $rlines->[$jp]->{'is_hanging_side_comment'} ) {
4159 0 next;
4160 }
4161  
4162 # Otherwise see if anything changed and update the tree if so
4163 else {
4164 199 foreach my $dep ( 0 .. $MAX_DEPTH ) {
4165  
4166 413 my $def_current = defined( $token_patterns_current[$dep] );
4167 413 my $def_next = defined( $token_patterns_next[$dep] );
4168 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
4169 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
4170 || !$def_next
4171 || $token_patterns_current[$dep] ne
4172 $token_patterns_next[$dep] )
4173 {
4174 46 my $n_parent;
4175 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
4176 23 $n_parent = @{ $match_tree[ $dep - 1 ] } - 1;
4177 }
4178 46 $end_node->( $dep, $jm, $n_parent );
4179 46 last;
4180 }
4181 }
4182 }
4183 } ## end loop to form tree of matches
4184  
4185 #---------------------------------------------------------
4186 # Prune Tree Step 3. Make links from parent to child nodes
4187 #---------------------------------------------------------
4188  
4189 # It seemed cleaner to do this as a separate step rather than during tree
4190 # construction. The children nodes have links up to the parent node which
4191 # created them. Now make links in the opposite direction, so the parents
4192 # can find the children. We store the range of children nodes ($nc_beg,
4193 # $nc_end) of each parent with two additional indexes in the original array.
4194 # These will be undef if no children.
4195 36 foreach my $depth ( reverse( 1 .. $MAX_DEPTH ) ) {
4196 72 ▶ next unless ( defined( $match_tree[$depth] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match_tree[$depth] 35 37 72
4197 37 my $nc_max = @{ $match_tree[$depth] } - 1;
4198 37 my $np_now;
4199 37 foreach my $nc ( 0 .. $nc_max ) {
4200 55 my $np = $match_tree[$depth]->[$nc]->{_tn_n_parent};
4201 55 ▶ if ( !defined($np) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $np) 0 55 55
4202  
4203 # shouldn't happen
4204 #print STDERR "lost child $np at depth $depth\n";
4205 0 next;
4206 }
4207 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
4208 40 $np_now = $np;
4209 40 $match_tree[ $depth - 1 ]->[$np]->{_tn_nc_beg_p} = $nc;
4210 }
4211 55 $match_tree[ $depth - 1 ]->[$np]->{_tn_nc_end_p} = $nc;
4212 }
4213 } ## end loop to make links down to the child nodes
4214  
4215 36 EXPLAIN_PRUNE > 0 && do {
4216 print "Tree complete. Found these groups:\n";
4217 foreach my $depth ( 0 .. $MAX_DEPTH ) {
4218 Dump_tree_groups( \@{ $match_tree[$depth] }, "depth=$depth" );
4219 }
4220 };
4221  
4222 #------------------------------------------------------
4223 # Prune Tree Step 4. Make a list of nodes to be deleted
4224 #------------------------------------------------------
4225  
4226 # list of lines with tokens to be deleted:
4227 # [$jbeg, $jend, $level_keep]
4228 # $jbeg..$jend is the range of line indexes,
4229 # $level_keep is the minimum level to keep
4230 36 my @delete_list;
4231  
4232 # We work with a list of nodes to visit at the next deeper depth.
4233 my @todo_list;
4234 36 ▶ if ( defined( $match_tree[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match_tree[0]) 36 0 36
4235 36 @todo_list = ( 0 .. @{ $match_tree[0] } - 1 );
4236 }
4237  
4238 36 foreach my $depth ( 0 .. $MAX_DEPTH ) {
4239 101 ▶ last if ( !@todo_list );
Branch: 1 branch
BranchTrueFalseTotal
unless @todo_list 36 65 101
4240 65 my @todo_next;
4241 65 foreach my $np (@todo_list) {
4242  
4243 103 my $item = $match_tree[$depth]->[$np];
4244  
4245 103 my $jbeg_p = $item->{_tn_jbeg};
4246 103 my $jend_p = $item->{_tn_jend};
4247 103 my $lev_p = $item->{_tn_level};
4248 103 my $nc_beg_p = $item->{_tn_nc_beg_p};
4249 103 my $nc_end_p = $item->{_tn_nc_end_p};
4250  
4251 103 my $nlines_p = $jend_p - $jbeg_p + 1;
4252  
4253 # nothing to do if no children
4254 103 ▶ next unless ( defined($nc_beg_p) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nc_beg_p 63 40 103
4255  
4256 # Define the number of lines to either keep or delete a child node.
4257 # This is the key decision we have to make. We want to delete
4258 # short runs of matched lines, and keep long runs. It seems easier
4259 # for the eye to follow breaks in monotonic level changes than
4260 # non-monotonic level changes. For example, the following looks
4261 # best if we delete the lower level alignments:
4262  
4263 # [1] ~~ [];
4264 # [ ["foo"], ["bar"] ] ~~ [ qr/o/, qr/a/ ];
4265 # [ qr/o/, qr/a/ ] ~~ [ ["foo"], ["bar"] ];
4266 # [ "foo", "bar" ] ~~ [ qr/o/, qr/a/ ];
4267 # [ qr/o/, qr/a/ ] ~~ [ "foo", "bar" ];
4268 # $deep1 ~~ $deep1;
4269  
4270 # So we will use two thresholds.
4271 40 my $nmin_mono = $depth + 2;
4272 40 my $nmin_non_mono = $depth + 6;
4273 40 ▶ if ( $nmin_mono > $nlines_p - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmin_mono > $nlines_p - 1) 26 14 40
4274 26 $nmin_mono = $nlines_p - 1;
4275 }
4276 40 ▶ if ( $nmin_non_mono > $nlines_p - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmin_non_mono > $nlines_p - 1) 36 4 40
4277 36 $nmin_non_mono = $nlines_p - 1;
4278 }
4279  
4280 # loop to keep or delete each child node
4281 40 foreach my $nc ( $nc_beg_p .. $nc_end_p ) {
4282  
4283 55 my $item_c = $match_tree[ $depth + 1 ]->[$nc];
4284 55 my $jbeg_c = $item_c->{_tn_jbeg};
4285 55 my $jend_c = $item_c->{_tn_jend};
4286  
4287 55 my $nlines_c = $jend_c - $jbeg_c + 1;
4288 55 my $is_monotonic = $rline_values->[$jbeg_c]->{_lv_is_monotonic};
4289 55 ▶ my $nmin = $is_monotonic ? $nmin_mono : $nmin_non_mono;
Branch: 1 branch
BranchTrueFalseTotal
$is_monotonic ? : 1 54 55
4290 55 ▶ if ( $nlines_c < $nmin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nlines_c < $nmin) { } 22 33 55
4291 ##print "deleting child, nlines=$nlines_c, nmin=$nmin\n";
4292 22 push @delete_list, [ $jbeg_c, $jend_c, $lev_p ];
4293 }
4294 else {
4295 ##print "keeping child, nlines=$nlines_c, nmin=$nmin\n";
4296 33 push @todo_next, $nc;
4297 }
4298 }
4299 }
4300 65 @todo_list = @todo_next;
4301 } ## end loop to mark nodes to delete
4302  
4303 #------------------------------------------------------------
4304 # Prune Tree Step 5. Loop to delete selected alignment tokens
4305 #------------------------------------------------------------
4306 36 foreach my $item (@delete_list) {
4307 22 my ( $jbeg, $jend, $level_keep ) = @{$item};
4308 22 foreach my $jj ( $jbeg .. $jend ) {
4309 28 my $line = $rlines->[$jj];
4310 28 my @idel;
4311 28 my $rtokens = $line->{'rtokens'};
4312 28 my $imax = @{$rtokens} - 2;
4313 28 foreach my $i ( 0 .. $imax ) {
4314 152 my $tok = $rtokens->[$i];
4315 152 my ( $raw_tok_uu, $lev, $tag_uu, $tok_count_uu ) =
4316 decode_alignment_token($tok);
4317 152 ▶ if ( $lev > $level_keep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lev > $level_keep) 83 69 152
4318 83 push @idel, $i;
4319 }
4320 }
4321 28 ▶ if (@idel) {
Branch: 1 branch
BranchTrueFalseTotal
if (@idel) 28 0 28
4322 28 delete_selected_tokens( $line, \@idel );
4323 }
4324 }
4325 } ## end loop to delete selected alignment tokens
4326  
4327 36 return;
4328 } ## end sub prune_alignment_tree
4329  
4330 0 ▶sub Dump_tree_groups {
Subroutine Dump_tree_groups: not called CC=4 SCAR=30.0
Pod undocumented
4331  
4332 0 my ( $rgroup, $msg ) = @_;
4333  
4334 # Debug routine
4335 0 print "$msg\n";
4336 0 local $LIST_SEPARATOR = ')(';
4337 0 foreach my $item ( @{$rgroup} ) {
4338 0 my @fix;
4339 0 foreach my $key (
4340 qw(
4341 _tn_jbeg
4342 _tn_jend
4343 _tn_n_parent
4344 _tn_level
4345 _tn_nc_beg_p
4346 _tn_nc_end_p
4347 )
4348 )
4349 {
4350 0 my $val = $item->{$key};
4351 0 ▶ $val = "undef" unless ( defined($val) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
4352 0 push @fix, "$val";
4353 }
4354 0 print "(@fix)\n";
4355 }
4356 0 return;
4357 } ## end sub Dump_tree_groups
4358  
4359 # This test sometimes gave alignments where no alignment was better, so it is
4360 # not used, but the flag is kept as a starting point for future testing.
4361 45 ▶use constant TEST_MARGINAL_EQ_ALIGNMENT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4362  
4363 293 ▶sub is_marginal_match {
Subroutine is_marginal_match: called CC=63 SCAR=44.6
Pod undocumented
4364  
4365 293 my ( $line_0, $line_1, $group_level, $imax_align, $imax_prev ) = @_;
4366  
4367 # Decide if we should undo some or all of the common alignments of a
4368 # group of just two lines.
4369  
4370 # Given:
4371 # $line_0 and $line_1 - the two lines
4372 # $group_level = the indentation level of the group being processed
4373 # $imax_align = the maximum index of the common alignment tokens
4374 # of the two lines
4375 # $imax_prev = the maximum index of the common alignment tokens
4376 # with the line before $line_0 (=-1 of does not exist)
4377  
4378 # Return:
4379 # $is_marginal = true if the two lines should NOT be fully aligned
4380 # = false if the two lines can remain fully aligned
4381 # $imax_align = the index of the highest alignment token shared by
4382 # these two lines to keep if the match is marginal.
4383  
4384 # When we have an alignment group of just two lines like this, we are
4385 # working in the twilight zone of what looks good and what looks bad.
4386 # This routine is a collection of rules which work have been found to
4387 # work fairly well, but it will need to be updated from time to time.
4388  
4389 293 my $is_marginal = 0;
4390  
4391 #---------------------------------------
4392 # Always align certain special cases ...
4393 #---------------------------------------
4394 293 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line_1->{"j_terminal_match"} or $line_0->{"list_type"} or $line_1->{"is_hanging_side_comment"}) 133 160 293
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
1258160
defined $line_1->{"j_terminal_match"} or $line_0->{"list_type"}
l!l&&r!l&&!r
6119168
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
4395  
4396 # always keep alignments of a terminal else or ternary
4397 defined( $line_1->{'j_terminal_match'} )
4398  
4399 # always align lists
4400 || $line_0->{'list_type'}
4401  
4402 # always align hanging side comments
4403 || $line_1->{'is_hanging_side_comment'}
4404  
4405 )
4406 {
4407 133 return ( $is_marginal, $imax_align );
4408 }
4409  
4410 160 my $jmax_0 = $line_0->{'jmax'};
4411 160 my $jmax_1 = $line_1->{'jmax'};
4412 160 my $rtokens_1 = $line_1->{'rtokens'};
4413 ## my $rtokens_0 = $line_0->{'rtokens'};
4414 160 my $rfield_lengths_0 = $line_0->{'rfield_lengths'};
4415 160 my $rfield_lengths_1 = $line_1->{'rfield_lengths'};
4416 160 my $rpatterns_0 = $line_0->{'rpatterns'};
4417 160 my $rpatterns_1 = $line_1->{'rpatterns'};
4418 160 my $imax_next = $line_1->{'imax_pair'};
4419  
4420 # We will scan the alignment tokens and set a flag '$is_marginal' if
4421 # it seems that the an alignment would look bad.
4422 160 my $max_pad = 0;
4423 160 my $saw_good_alignment = 0;
4424 160 my $saw_if_or; # if we saw an 'if' or 'or' at group level
4425 160 my $raw_tokb = EMPTY_STRING; # first token seen at group level
4426 160 my $jfirst_bad;
4427 my $line_ending_fat_comma; # is last token just a '=>' ?
4428 160 my $j0_eq_pad;
4429 160 my $j0_max_pad = 0;
4430  
4431 160 foreach my $j ( 0 .. $jmax_1 - 2 ) {
4432 213 my ( $raw_tok, $lev, $tag_uu, $tok_count_uu ) =
4433 decode_alignment_token( $rtokens_1->[$j] );
4434 213 ▶ if ( $raw_tok && $lev == $group_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok and $lev == $group_level) 183 30 213
Condition66%2/3
$raw_tok and $lev == $group_level
!ll&&!rl&&r
030183
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
4435 183 ▶ if ( !$raw_tokb ) { $raw_tokb = $raw_tok }
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw_tokb) 147 36 183
4436 183 ▶ $saw_if_or ||= $is_if_or{$raw_tok};
Condition100%3/3
$saw_if_or ||= $is_if_or{$raw_tok}
l!l&&r!l&&!r
418161
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
4437 }
4438  
4439 # When the first of the two lines ends in a bare '=>' this will
4440 # probably be marginal match. (For a bare =>, the next field length
4441 # will be 2 or 3, depending on side comment)
4442 $line_ending_fat_comma =
4443 213 ▶ $j == $jmax_1 - 2
Condition100%6/6
$j == $jmax_1 - 2 && $raw_tok eq '=>' && $$rfield_lengths_0[$j + 1] <= 3
!ll&&!rl&&r
20841
$j == $jmax_1 - 2 and $raw_tok eq "=>"
!ll&&!rl&&r
611475
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
4444 && $raw_tok eq '=>'
4445 && $rfield_lengths_0->[ $j + 1 ] <= 3;
4446  
4447 213 my $pad = $rfield_lengths_1->[$j] - $rfield_lengths_0->[$j];
4448 213 ▶ if ( $j == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 0) 152 61 213
4449 $pad += $line_1->{'leading_space_count'} -
4450 152 $line_0->{'leading_space_count'};
4451  
4452 # Remember the pad at a leading equals
4453 152 ▶ if ( $raw_tok eq '=' && $lev == $group_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok eq "=" and $lev == $group_level) 92 60 152
Condition66%2/3
$raw_tok eq "=" and $lev == $group_level
!ll&&!rl&&r
60092
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
4454 92 $j0_eq_pad = $pad;
4455 92 $j0_max_pad =
4456 0.5 * ( $rfield_lengths_1->[0] + $rfield_lengths_0->[0] );
4457 92 ▶ $j0_max_pad = 4 if ( $j0_max_pad < 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $j0_max_pad < 4 5 87 92
4458 }
4459 }
4460  
4461 213 ▶ if ( $pad < 0 ) { $pad = -$pad }
Branch: 1 branch
BranchTrueFalseTotal
if ($pad < 0) 50 163 213
4462 213 ▶ if ( $pad > $max_pad ) { $max_pad = $pad }
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > $max_pad) 114 99 213
4463 213 ▶ if ( $is_good_marginal_alignment{$raw_tok}
Branch: 1 branch
BranchTrueFalseTotal
if ($is_good_marginal_alignment{$raw_tok} and not $line_ending_fat_comma) { } 155 58 213
Condition100%3/3
$is_good_marginal_alignment{$raw_tok} and not $line_ending_fat_comma
!ll&&!rl&&r
571155
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
4464 && !$line_ending_fat_comma )
4465 {
4466 155 $saw_good_alignment = 1;
4467 }
4468 else {
4469 58 ▶ $jfirst_bad = $j unless ( defined($jfirst_bad) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $jfirst_bad 49 9 58
4470 }
4471 213 my $pat_0 = $rpatterns_0->[$j];
4472 213 my $pat_1 = $rpatterns_1->[$j];
4473 213 ▶ 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 210 213
Condition100%3/3
$pat_0 ne $pat_1 and length $pat_0 eq length $pat_1
!ll&&!rl&&r
170403
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
4474 3 $pat_0 =~ tr/n/Q/;
4475 3 $pat_1 =~ tr/n/Q/;
4476 }
4477 213 ▶ if ( $pat_0 ne $pat_1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat_0 ne $pat_1) 43 170 213
4478  
4479 # Flag this as a marginal match since patterns differ.
4480 # Normally, we will not allow just two lines to match if
4481 # marginal. But we can allow matching in some specific cases.
4482  
4483 43 ▶ $jfirst_bad = $j if ( !defined($jfirst_bad) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $jfirst_bad 22 21 43
4484 43 ▶ $is_marginal = 1 if ( $is_marginal == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $is_marginal == 0 42 1 43
4485 43 ▶ if ( $raw_tok eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_tok eq "=") 13 30 43
4486  
4487 # Here is an example of a marginal match:
4488 # $done{$$op} = 1;
4489 # $op = compile_bblock($op);
4490 # The left tokens are both identifiers, but
4491 # one accesses a hash and the other doesn't.
4492 # We'll let this be a tentative match and undo
4493 # it later if we don't find more than 2 lines
4494 # in the group.
4495 13 $is_marginal = 2;
4496 }
4497 }
4498 }
4499  
4500 160 ▶ $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 160 160
Condition66%2/3
$is_marginal == 0 and $line_ending_fat_comma
!ll&&!rl&&r
421180
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
4501  
4502 # Turn off the "marginal match" flag in some cases...
4503 # A "marginal match" occurs when the alignment tokens agree
4504 # but there are differences in the other tokens (patterns).
4505 # If we leave the marginal match flag set, then the rule is that we
4506 # will align only if there are more than two lines in the group.
4507 # We will turn of the flag if we almost have a match
4508 # and either we have seen a good alignment token or we
4509 # just need a small pad (2 spaces) to fit. These rules are
4510 # the result of experimentation. Tokens which misaligned by just
4511 # one or two characters are annoying. On the other hand,
4512 # large gaps to less important alignment tokens are also annoying.
4513 160 ▶ if ( $is_marginal == 1
Branch: 1 branch
BranchTrueFalseTotal
if ($is_marginal == 1 and $saw_good_alignment || $max_pad < 3) 23 137 160
Condition100%6/6
$is_marginal == 1 and $saw_good_alignment || $max_pad < 3
!ll&&!rl&&r
131623
$saw_good_alignment or $max_pad < 3
l!l&&r!l&&!r
1946
MC/DC100%3/3
$is_marginal == 1 and $saw_good_alignment || $max_pad < 3
$is_marginal == 1 $saw_good_alignment $max_pad < 3
Truth table100%4/4
A and B or C
A= $is_marginal == 1B= $saw_good_alignmentC= $max_pad < 3
ABCresult
0XX0
1000
11X1
1011
4514 && ( $saw_good_alignment || $max_pad < 3 ) )
4515 {
4516 23 $is_marginal = 0;
4517 }
4518  
4519 # We will use the line endings to help decide on alignments...
4520 # See if the lines end with semicolons...
4521 160 my $sc_term0;
4522 my $sc_term1;
4523 160 ▶ if ( $jmax_0 < 1 || $jmax_1 < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax_0 < 1 or $jmax_1 < 1) { } 0 160 160
Condition33%1/3
$jmax_0 < 1 or $jmax_1 < 1
l!l&&r!l&&!r
00160
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
4524  
4525 # shouldn't happen
4526 }
4527 else {
4528 160 my $pat0 = $rpatterns_0->[ $jmax_0 - 1 ];
4529 160 my $pat1 = $rpatterns_1->[ $jmax_1 - 1 ];
4530 160 $sc_term0 = $pat0 =~ /;b?$/;
4531 160 $sc_term1 = $pat1 =~ /;b?$/;
4532 }
4533  
4534 160 ▶ if ( !$is_marginal && !$sc_term0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_marginal or $sc_term0) 37 123 160
Condition100%3/3
$is_marginal or $sc_term0
l!l&&r!l&&!r
1910437
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
4535  
4536 # First line of assignment should be semicolon terminated.
4537 # For example, do not align here:
4538 # $$href{-NUM_TEXT_FILES} = $$href{-NUM_BINARY_FILES} =
4539 # $$href{-NUM_DIRS} = 0;
4540 37 ▶ if ( $is_assignment{$raw_tokb} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_assignment{$raw_tokb}) 1 36 37
4541 1 $is_marginal = 1;
4542 }
4543 }
4544  
4545 # Try to avoid some undesirable alignments of opening tokens
4546 # for example, the space between grep and { here:
4547 # return map { ( $_ => $_ ) }
4548 # grep { /$handles/ } $self->_get_delegate_method_list;
4549 $is_marginal ||=
4550 160 ▶ ( $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
201139
($raw_tokb eq '(' || $raw_tokb eq '{') && $jmax_1 == 2 && $sc_term0 ne $sc_term1
!ll&&!rl&&r
13091
$raw_tokb eq "(" || $raw_tokb eq "{" and $jmax_1 == 2
!ll&&!rl&&r
1171310
$raw_tokb eq "(" or $raw_tokb eq "{"
l!l&&r!l&&!r
1211117
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
4551 && $jmax_1 == 2
4552 && $sc_term0 ne $sc_term1;
4553  
4554 #---------------------------------------
4555 # return if this is not a marginal match
4556 #---------------------------------------
4557 160 ▶ if ( !$is_marginal ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_marginal) 139 21 160
4558 139 return ( $is_marginal, $imax_align );
4559 }
4560  
4561 # Undo the marginal match flag in certain cases,
4562  
4563 # Two lines with a leading equals-like operator are allowed to
4564 # align if the patterns to the left of the equals are the same.
4565 # For example the following two lines are a marginal match but have
4566 # the same left side patterns, so we will align the equals.
4567 # my $orig = my $format = "^<<<<< ~~\n";
4568 # my $abc = "abc";
4569 # But these have a different left pattern so they will not be
4570 # aligned
4571 # $xmldoc .= $`;
4572 # $self->{'leftovers'} .= "<bx-seq:seq" . $';
4573  
4574 # First line semicolon terminated but second not, usually ok:
4575 # my $want = "'ab', 'a', 'b'";
4576 # my $got = join( ", ",
4577 # map { defined($_) ? "'$_'" : "undef" }
4578 # @got );
4579 # First line not semicolon terminated, Not OK to match:
4580 # $$href{-NUM_TEXT_FILES} = $$href{-NUM_BINARY_FILES} =
4581 # $$href{-NUM_DIRS} = 0;
4582 21 my $pat0 = $rpatterns_0->[0];
4583 21 my $pat1 = $rpatterns_1->[0];
4584  
4585 #---------------------------------------------------------
4586 # Turn off the marginal flag for some types of assignments
4587 #---------------------------------------------------------
4588 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
4589  
4590 # undo marginal flag if first line is semicolon terminated
4591 # and leading patterns match
4592 14 ▶ if ($sc_term0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sc_term0) 13 1 14
4593 13 $is_marginal = $pat0 ne $pat1;
4594 }
4595 }
4596 elsif ( $raw_tokb eq '=>' ) {
4597  
4598 # undo marginal flag if patterns match
4599 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
4600 }
4601 elsif ( $raw_tokb eq '=~' ) {
4602  
4603 # undo marginal flag if both lines are semicolon terminated
4604 # and leading patters match
4605 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
4606 0 $is_marginal = $pat0 ne $pat1;
4607 }
4608 }
4609 else {
4610 ## ok: (none of the above)
4611 }
4612  
4613 #-----------------------------------------------------
4614 # Turn off the marginal flag if we saw an 'if' or 'or'
4615 #-----------------------------------------------------
4616  
4617 # A trailing 'if' and 'or' often gives a good alignment
4618 # For example, we can align these:
4619 # return -1 if $_[0] =~ m/^CHAPT|APPENDIX/;
4620 # return $1 + 0 if $_[0] =~ m/^SECT(\d*)$/;
4621  
4622 # or
4623 # $d_in_m[2] = 29 if ( &Date_LeapYear($y) );
4624 # $d = $d_in_m[$m] if ( $d > $d_in_m[$m] );
4625  
4626 21 ▶ if ($saw_if_or) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_if_or) 5 16 21
4627  
4628 # undo marginal flag if both lines are semicolon terminated
4629 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
4630 5 $is_marginal = 0;
4631 }
4632 }
4633  
4634 # For a marginal match, only keep matches before the first 'bad' match
4635 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
4636 && defined($jfirst_bad)
4637 && $imax_align > $jfirst_bad - 1 )
4638 {
4639 0 $imax_align = $jfirst_bad - 1;
4640 }
4641  
4642 #----------------------------------------------------------
4643 # Allow sweep to match lines with leading '=' in some cases
4644 #----------------------------------------------------------
4645 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
4646  
4647 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
4648  
4649 # If there is a following line with leading equals, or
4650 # preceding line with leading equals, then let the sweep align
4651 # them without restriction. For example, the first two lines
4652 # here are a marginal match, but they are followed by a line
4653 # with leading equals, so the sweep-lr logic can align all of
4654 # the lines:
4655  
4656 # $date[1] = $month_to_num{ $date[1] }; # <--line_0
4657 # @xdate = split( /[:\/\s]/, $log->field('t') ); # <--line_1
4658 # $day = sprintf( "%04d/%02d/%02d", @date[ 2, 1, 0 ] );
4659 # $time = sprintf( "%02d:%02d:%02d", @date[ 3 .. 5 ] );
4660  
4661 # Likewise, if we reverse the two pairs we want the same result
4662  
4663 # $day = sprintf( "%04d/%02d/%02d", @date[ 2, 1, 0 ] );
4664 # $time = sprintf( "%02d:%02d:%02d", @date[ 3 .. 5 ] );
4665 # $date[1] = $month_to_num{ $date[1] }; # <--line_0
4666 # @xdate = split( /[:\/\s]/, $log->field('t') ); # <--line_1
4667  
4668 (
4669 $imax_next >= 0
4670 || $imax_prev >= 0
4671 || TEST_MARGINAL_EQ_ALIGNMENT
4672 )
4673 && $j0_eq_pad >= -$j0_max_pad
4674 && $j0_eq_pad <= $j0_max_pad
4675 )
4676 {
4677  
4678 # But do not do this if there is a comma before the '='.
4679 # For example, the first two lines below have commas and
4680 # therefore are not allowed to align with lines 3 & 4:
4681  
4682 # my ( $x, $y ) = $self->Size(); #<--line_0
4683 # my ( $left, $top, $right, $bottom ) = $self->Window(); #<--l_1
4684 # my $vx = $right - $left;
4685 # my $vy = $bottom - $top;
4686  
4687 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
4688 0 $imax_align = 0;
4689 }
4690 }
4691 }
4692  
4693 21 return ( $is_marginal, $imax_align );
4694 } ## end sub is_marginal_match
4695  
4696 445 ▶sub get_extra_leading_spaces {
Subroutine get_extra_leading_spaces: called CC=15 SCAR=27.4
Pod undocumented
4697  
4698 445 my ( $rlines, $rgroups ) = @_;
4699  
4700 #----------------------------------------------------------
4701 # Define any extra indentation space (for the -lp option).
4702 # Here is why:
4703 # If a list has side comments, sub scan_list must dump the
4704 # list before it sees everything. When this happens, it sets
4705 # the indentation to the standard scheme, but notes how
4706 # many spaces it would have liked to use. We may be able
4707 # to recover that space here in the event that all of the
4708 # lines of a list are back together again.
4709 #----------------------------------------------------------
4710  
4711 445 ▶ return 0 if ( !@{$rlines} || !@{$rgroups} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rlines;} and @{$rgroups;} 0 445 445
Condition33%1/3
@{$rlines;} and @{$rgroups;}
!ll&&!rl&&r
00445
MC/DC0%0/2
@{$rlines;} and @{$rgroups;}
@{$rlines;} @{$rgroups;}
Truth table33%1/3
A and B
A= @{$rlines;}B= @{$rgroups;}
ABresult
0X0
100
111
4712  
4713 445 my $object = $rlines->[0]->{'indentation'};
4714 445 ▶ return 0 if ( !ref($object) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $object 386 59 445
4715 59 my $extra_leading_spaces = 0;
4716 59 my $extra_indentation_spaces_wanted = get_recoverable_spaces($object);
4717 59 ▶ return ($extra_leading_spaces) if ( !$extra_indentation_spaces_wanted );
Branch: 1 branch
BranchTrueFalseTotal
unless $extra_indentation_spaces_wanted 46 13 59
4718  
4719 13 my $min_spaces = $extra_indentation_spaces_wanted;
4720 13 ▶ if ( $min_spaces > 0 ) { $min_spaces = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($min_spaces > 0) 13 0 13
4721  
4722 # loop over all groups
4723 13 my $ng = -1;
4724 13 my $ngroups = @{$rgroups};
4725 13 foreach my $item ( @{$rgroups} ) {
4726 32 $ng++;
4727 32 my ( $jbeg, $jend ) = @{$item};
4728 32 foreach my $j ( $jbeg .. $jend ) {
4729 44 ▶ next if ( $j == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $j == 0 13 31 44
4730  
4731 # all indentation objects must be the same
4732 31 ▶ if ( $object != $rlines->[$j]->{'indentation'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object != $rlines->[$j]{"indentation"}) 1 30 31
4733 1 return 0;
4734 }
4735 }
4736  
4737 # find the maximum space without exceeding the line length for this group
4738 31 my $avail = $rlines->[$jbeg]->get_available_space_on_right();
4739 31 ▶ my $spaces =
Branch: 1 branch
BranchTrueFalseTotal
$avail > $extra_indentation_spaces_wanted ? : 30 1 31
4740 ( $avail > $extra_indentation_spaces_wanted )
4741 ? $extra_indentation_spaces_wanted
4742 : $avail;
4743  
4744 #--------------------------------------------------------
4745 # Note: min spaces can be negative; for example with -gnu
4746 # f(
4747 # do { 1; !!(my $x = bless []); }
4748 # );
4749 #--------------------------------------------------------
4750 # The following rule is needed to match older formatting:
4751 # For multiple groups, we will keep spaces non-negative.
4752 # For a single group, we will allow a negative space.
4753 0 ▶ 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
4754  
4755 # update the minimum spacing
4756 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
4757 13 $extra_leading_spaces = $spaces;
4758 }
4759 }
4760  
4761 # update the indentation object because with -icp the terminal
4762 # ');' will use the same adjustment.
4763 12 $object->permanently_decrease_available_spaces( -$extra_leading_spaces );
4764 12 return $extra_leading_spaces;
4765 } ## end sub get_extra_leading_spaces
4766  
4767 124 ▶sub forget_side_comment {
Subroutine forget_side_comment: called CC=1 SCAR=0.0
Pod undocumented
4768 124 my ($self) = @_;
4769 124 $self->[_last_side_comment_column_] = 0;
4770 124 return;
4771 }
4772  
4773 222 ▶sub is_good_side_comment_column {
Subroutine is_good_side_comment_column: called CC=10 SCAR=23.2
Pod undocumented
4774  
4775 222 my ( $self, $line, $line_number, $level, $num5 ) = @_;
4776  
4777 # Upon encountering the first side comment of a group, decide if
4778 # a previous side comment should be forgotten. This involves
4779 # checking several rules.
4780  
4781 # Given:
4782 # $line = ref to info hash for the line of interest
4783 # $line_number = number of this line in the output stream
4784 # $level = indentation level of this line
4785 # $num5 = ..see comments below
4786  
4787 # Return:
4788 # true to KEEP old comment location
4789 # false to FORGET old comment location
4790 222 my $KEEP = 1;
4791 222 my $FORGET = 0;
4792  
4793 222 my $rfields = $line->{'rfields'};
4794 222 my $is_hanging_side_comment = $line->{'is_hanging_side_comment'};
4795  
4796 # RULE1: Never forget comment before a hanging side comment
4797 222 ▶ return $KEEP if ($is_hanging_side_comment);
Branch: 1 branch
BranchTrueFalseTotal
if $is_hanging_side_comment 12 210 222
4798  
4799 # RULE2: Forget a side comment after a short line difference,
4800 # where 'short line difference' is computed from a formula.
4801 # Using a smooth formula helps minimize sudden large changes.
4802 210 my $line_diff = $line_number - $self->[_last_side_comment_line_number_];
4803 210 my $alev_diff = abs( $level - $self->[_last_side_comment_level_] );
4804  
4805 # '$num5' is the number of comments in the first 5 lines after the first
4806 # comment. It is needed to keep a compact group of side comments from
4807 # being influenced by a more distant side comment.
4808 210 ▶ $num5 = 1 if ( !$num5 );
Branch: 1 branch
BranchTrueFalseTotal
unless $num5 0 210 210
4809  
4810 # Some values:
4811  
4812 # $adiff $num5 $short_diff
4813 # 0 * 12
4814 # 1 1 6
4815 # 1 2 4
4816 # 1 3 3
4817 # 1 4 2
4818 # 2 1 4
4819 # 2 2 2
4820 # 2 3 1
4821 # 3 1 3
4822 # 3 2 1
4823  
4824 210 my $short_diff = SC_LONG_LINE_DIFF / ( 1 + $alev_diff * $num5 );
4825  
4826 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
4827 if ( $line_diff > $short_diff
4828 || !$rOpts_valign_side_comments );
4829  
4830 # RULE3: Forget a side comment if this line is at lower level and
4831 # ends a block
4832 128 my $last_sc_level = $self->[_last_side_comment_level_];
4833 return $FORGET
4834 if ( $level < $last_sc_level
4835 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
4836  
4837 # RULE 4: Forget the last side comment if this comment might join a cached
4838 # line ...
4839 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
4840  
4841 # ... otherwise side comment alignment will get messed up.
4842 # For example, in the following test script
4843 # with using 'perltidy -sct -act=2', the last comment would try to
4844 # align with the previous and then be in the wrong column when
4845 # the lines are combined:
4846  
4847 # foreach $line (
4848 # [0, 1, 2], [3, 4, 5], [6, 7, 8], # rows
4849 # [0, 3, 6], [1, 4, 7], [2, 5, 8], # columns
4850 # [0, 4, 8], [2, 4, 6]
4851 # ) # diagonals
4852 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
4853 if ( $cached_line_type == 2 || $cached_line_type == 4 );
4854 }
4855  
4856 # Otherwise, keep it alive
4857 110 return $KEEP;
4858 } ## end sub is_good_side_comment_column
4859  
4860 222 ▶sub align_side_comments {
Subroutine align_side_comments: called CC=33 SCAR=35.0
Pod undocumented
4861  
4862 222 my ( $self, $rlines, $rgroups ) = @_;
4863  
4864 # Align any side comments in this batch of lines
4865  
4866 # Given:
4867 # $rlines - the lines
4868 # $rgroups - the partition of the lines into groups
4869 #
4870 # We will be working group-by-group because all side comments
4871 # (real or fake) in each group are already aligned. So we just have
4872 # to make alignments between groups wherever possible.
4873  
4874 # An unusual aspect is that within each group we have aligned both real
4875 # and fake side comments. This has the consequence that the lengths of
4876 # long lines without real side comments can cause 'push' all side comments
4877 # to the right. This seems unusual, but testing with and without this
4878 # feature shows that it is usually better this way. Otherwise, side
4879 # comments can be hidden between long lines without side comments and
4880 # thus be harder to read.
4881  
4882 222 my $group_level = $self->[_group_level_];
4883 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
4884 && $group_level == $self->[_last_level_written_];
4885  
4886 # Find groups with side comments, and remember the first nonblank comment
4887 222 my $j_sc_beg;
4888 my @todo;
4889 222 my $ng = -1;
4890 222 foreach my $item ( @{$rgroups} ) {
4891 348 $ng++;
4892 348 my ( $jbeg, $jend ) = @{$item};
4893 348 foreach my $j ( $jbeg .. $jend ) {
4894 403 my $line = $rlines->[$j];
4895 403 my $jmax = $line->{'jmax'};
4896 403 ▶ if ( $line->{'rfield_lengths'}->[$jmax] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"rfield_lengths"}[$jmax]) 251 152 403
4897  
4898 # this group has a line with a side comment
4899 251 push @todo, $ng;
4900 251 ▶ if ( !defined($j_sc_beg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $j_sc_beg) 222 29 251
4901 222 $j_sc_beg = $j;
4902 }
4903 251 last;
4904 }
4905 }
4906 }
4907  
4908 # done if no groups with side comments
4909 222 ▶ return unless (@todo);
Branch: 1 branch
BranchTrueFalseTotal
unless @todo 0 222 222
4910  
4911 # Count $num5 = number of comments in the 5 lines after the first comment
4912 # This is an important factor in a decision formula
4913 222 my $num5 = 1;
4914 222 foreach my $jj ( $j_sc_beg + 1 .. @{$rlines} - 1 ) {
4915 210 my $ldiff = $jj - $j_sc_beg;
4916 210 ▶ last if ( $ldiff > 5 );
Branch: 1 branch
BranchTrueFalseTotal
if $ldiff > 5 6 204 210
4917 204 my $line = $rlines->[$jj];
4918 204 my $jmax = $line->{'jmax'};
4919 204 my $sc_len = $line->{'rfield_lengths'}->[$jmax];
4920 204 ▶ next if ( !$sc_len );
Branch: 1 branch
BranchTrueFalseTotal
unless $sc_len 83 121 204
4921 121 $num5++;
4922 }
4923  
4924 # Forget the old side comment location if necessary
4925 222 my $line_0 = $rlines->[$j_sc_beg];
4926 222 my $lnum =
4927 $j_sc_beg + $self->[_file_writer_object_]->get_output_line_number();
4928 222 my $keep_it =
4929 $self->is_good_side_comment_column( $line_0, $lnum, $group_level, $num5 );
4930 222 ▶ my $last_side_comment_column =
Branch: 1 branch
BranchTrueFalseTotal
$keep_it ? : 122 100 222
4931 $keep_it ? $self->[_last_side_comment_column_] : 0;
4932  
4933 # If there are multiple groups we will do two passes
4934 # so that we can find a common alignment for all groups.
4935 222 ▶ my $MAX_PASS = @todo > 1 ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@todo > 1 ? : 24 198 222
4936  
4937 # Loop over passes
4938 222 my $max_comment_column = $last_side_comment_column;
4939 222 foreach my $PASS ( 1 .. $MAX_PASS ) {
4940  
4941 # If there are two passes, then on the last pass make the old column
4942 # equal to the largest of the group. This will result in the comments
4943 # being aligned if possible.
4944 246 ▶ if ( $PASS == $MAX_PASS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PASS == $MAX_PASS) 222 24 246
4945 222 $last_side_comment_column = $max_comment_column;
4946 }
4947  
4948 # Loop over the groups with side comments
4949 246 my $column_limit;
4950 246 foreach my $ngr (@todo) {
4951 304 my ( $jbeg, $jend_uu ) = @{ $rgroups->[$ngr] };
4952  
4953 # Note that since all lines in a group have common alignments, we
4954 # just have to work on one of the lines (the first line).
4955 304 my $line = $rlines->[$jbeg];
4956 304 my $jmax = $line->{'jmax'};
4957 304 my $is_hanging_side_comment = $line->{'is_hanging_side_comment'};
4958 last
4959 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
4960  
4961 # the maximum space without exceeding the line length:
4962 300 my $avail = $line->get_available_space_on_right();
4963  
4964 # try to use the previous comment column
4965 300 my $side_comment_column = $line->get_column( $jmax - 1 );
4966 300 my $move = $last_side_comment_column - $side_comment_column;
4967  
4968 # Remember the maximum possible column of the first line with
4969 # side comment
4970 300 ▶ if ( !defined($column_limit) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $column_limit) 246 54 300
4971 246 $column_limit = $side_comment_column + $avail;
4972 }
4973  
4974 300 ▶ next if ( $jmax <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $jmax <= 0 0 300 300
4975  
4976 # but if this doesn't work, give up and use the minimum space
4977 300 my $min_move = $rOpts_minimum_space_to_comment - 1;
4978 300 ▶ if ( $move > $avail ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move > $avail) 13 287 300
4979 13 $move = $min_move;
4980 }
4981  
4982 # but we want some minimum space to the comment
4983 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
4984 && $j_sc_beg == 0
4985 && $continuing_sc_flow )
4986 {
4987 5 $min_move = 0;
4988 }
4989  
4990 # remove constraints on hanging side comments
4991 300 ▶ if ($is_hanging_side_comment) { $min_move = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hanging_side_comment) 16 284 300
4992  
4993 300 ▶ if ( $move < $min_move ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move < $min_move) 215 85 300
4994 215 $move = $min_move;
4995 }
4996  
4997 # don't exceed the available space
4998 300 ▶ if ( $move > $avail ) { $move = $avail }
Branch: 1 branch
BranchTrueFalseTotal
if ($move > $avail) 11 289 300
4999  
5000 # We can only increase space, never decrease.
5001 300 ▶ if ( $move < 0 ) { $move = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($move < 0) 8 292 300
5002  
5003 # Discover the largest column on the preliminary pass
5004 300 ▶ if ( $PASS < $MAX_PASS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PASS < $MAX_PASS) { } 49 251 300
5005 49 my $col = $line->get_column( $jmax - 1 ) + $move;
5006  
5007 # but ignore columns too large for the starting line
5008 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
5009 23 $max_comment_column = $col;
5010 }
5011 }
5012  
5013 # Make the changes on the final pass
5014 else {
5015 251 $line->increase_field_width( $jmax - 1, $move );
5016  
5017 # remember this column for the next group
5018 251 $last_side_comment_column = $line->get_column( $jmax - 1 );
5019 }
5020 } ## end loop over groups
5021 } ## end loop over passes
5022  
5023 # Find the last side comment
5024 222 my $j_sc_last;
5025 222 my $ng_last = $todo[-1];
5026 222 my ( $jbeg, $jend ) = @{ $rgroups->[$ng_last] };
5027 222 foreach my $jj ( reverse( $jbeg .. $jend ) ) {
5028 225 my $line = $rlines->[$jj];
5029 225 my $jmax = $line->{'jmax'};
5030 225 ▶ if ( $line->{'rfield_lengths'}->[$jmax] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"rfield_lengths"}[$jmax]) 222 3 225
5031 222 $j_sc_last = $jj;
5032 222 last;
5033 }
5034 }
5035  
5036 # Save final side comment info for possible use by the next batch
5037 222 ▶ if ( defined($j_sc_last) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $j_sc_last) 222 0 222
5038 222 my $line_number =
5039 $self->[_file_writer_object_]->get_output_line_number() + $j_sc_last;
5040 222 $self->[_last_side_comment_column_] = $last_side_comment_column;
5041 222 $self->[_last_side_comment_line_number_] = $line_number;
5042 222 $self->[_last_side_comment_level_] = $group_level;
5043 }
5044 222 return;
5045 } ## end sub align_side_comments
5046  
5047 ###########################################
5048 # CODE SECTION 6: Pad Signed Number Columns
5049 ###########################################
5050  
5051 45 ▶use constant DEBUG_VSN => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5052  
5053 45 my %is_leading_sign_pattern;
5054  
5055 45 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
5056  
5057 # PATTERNS: A pattern is basically the concatenation of all token types in
5058 # the field, with keywords converted to their actual text. The formatter
5059 # has changed things like 'print' to 'priNt' so that all 'n's are numbers.
5060 # The following patterns 'n' can match a signed number of interest.
5061 # Thus 'n'=a signed or unsigned number, 'b'=a space, '}'=one of ) ] }
5062 $is_leading_sign_pattern{$_} = 1
5063 45 for ( 'n,', 'n,b', 'nb', 'nb}', 'nb},', 'n},', 'n};' );
5064 }
5065  
5066 42 ▶sub min_max_median {
Subroutine min_max_median: called CC=3 SCAR=11.0
Pod undocumented
5067  
5068 42 my ($rvalues) = @_;
5069  
5070 # Given: $rvalues = ref to an array of numbers
5071 # Return: the min, max, and median
5072 42 my $num = @{$rvalues};
5073 42 ▶ return unless ($num);
Branch: 1 branch
BranchTrueFalseTotal
unless $num 0 42 42
5074  
5075 42 my @sorted = sort { $a <=> $b } @{$rvalues};
5076  
5077 42 my $min = $sorted[0];
5078 42 my $max = $sorted[-1];
5079 42 my $imid = int( $num / 2 );
5080 42 ▶ my $median =
Branch: 1 branch
BranchTrueFalseTotal
@sorted % 2 ? : 26 16 42
5081 @sorted % 2
5082 ? $sorted[$imid]
5083 : ( $sorted[ $imid - 1 ] + $sorted[$imid] ) / 2;
5084  
5085 42 return ( $min, $max, $median );
5086 } ## end sub min_max_median
5087  
5088 30 ▶sub end_signed_number_column {
Subroutine end_signed_number_column: called CC=46 SCAR=40.2
Pod undocumented
5089  
5090 30 my ( $rgroup_lines, $rcol_hash, $ix_last ) = @_;
5091  
5092 # Finish formatting a column of unsigned numbers
5093 # Given:
5094 # $rgroup_lines - the current vertical alignment group of lines
5095 # $rcol_hash - a hash of information about this vertical column
5096 # $ix_last - index of the last line of this vertical column
5097 # Task:
5098 # If this is a mixture of signed and unsigned numbers, then add a
5099 # single space before the unsigned numbers to improve appearance.
5100 30 ▶ return unless ($rcol_hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $rcol_hash 0 30 30
5101 30 my $jcol = $rcol_hash->{jcol};
5102 30 my $unsigned = $rcol_hash->{unsigned_count};
5103 30 my $signed = $rcol_hash->{signed_count};
5104 30 my $rsigned_lines = $rcol_hash->{rsigned_lines};
5105  
5106 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
5107 0 DEVEL_MODE
5108 && Fault("avoid calling without mixed signed and unsigned\n");
5109 0 return;
5110 }
5111  
5112 30 my $pos_start_number = $rcol_hash->{pos_start_number};
5113 30 my $char_end_part1 = $rcol_hash->{char_end_part1};
5114 30 my $ix_first = $rcol_hash->{ix_first};
5115 30 my $nlines = $ix_last - $ix_first + 1;
5116  
5117 # check for skipped lines, shouldn't happen
5118 30 ▶ if ( $signed + $unsigned != $nlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signed + $unsigned != $nlines) 0 30 30
5119 0 my $line = $rgroup_lines->[$ix_last];
5120 0 my $rfields = $line->{'rfields'};
5121 0 my $text = join( EMPTY_STRING, @{$rfields} );
5122 0 DEVEL_MODE && Fault(<<EOM);
5123 We seem to have miscounted lines, please check:
5124 signed=$signed
5125 j=$jcol
5126 unsigned=$unsigned
5127 ix_first=$ix_first
5128 ix_last=$ix_last
5129 nlines=$nlines
5130 text=$text
5131 EOM
5132 0 return;
5133 }
5134  
5135 #-----------------------------------------------------------------
5136 # Form groups of unsigned numbers from the list of signed numbers.
5137 #-----------------------------------------------------------------
5138 30 my @unsigned_subgroups;
5139 30 my $ix_last_negative = $ix_first - 1;
5140 30 my %is_signed;
5141 30 foreach my $ix ( @{$rsigned_lines} ) {
5142 45 $is_signed{$ix} = 1;
5143 45 my $Nu = $ix - $ix_last_negative - 1;
5144 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
5145 18 push @unsigned_subgroups, [ $ix_last_negative + 1, $ix - 1 ];
5146 }
5147 45 $ix_last_negative = $ix;
5148 }
5149  
5150 # Exclude groups with more than about 20 consecutive numbers. Little
5151 # visual improvement is gained by padding more than this, and this avoids
5152 # large numbers of differences in a file when a single line is changed.
5153 30 my $Nu = $ix_last - $ix_last_negative;
5154 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
5155 18 push @unsigned_subgroups, [ $ix_last_negative + 1, $ix_last ];
5156 }
5157  
5158 30 ▶ if ( !@unsigned_subgroups ) { return } # shouldn't happen
Branch: 1 branch
BranchTrueFalseTotal
unless (@unsigned_subgroups) 9 21 30
5159  
5160 #--------------------------------------------
5161 # Find number lengths for irregularity checks
5162 #--------------------------------------------
5163 # Padding signed numbers looks best when the numbers, excluding signs,
5164 # all have about the same length. When the lengths are irregular, with
5165 # mostly longer unsigned numbers, it doesn't look good to do this. So
5166 # we need to filter out these bad-looking cases.
5167  
5168 # The 'field_lengths' are unreliable because they may include some
5169 # arbitrary trailing text; see 'substr.t' So we must look for the end of
5170 # the number at a space, comma, or closing container token. Note that these
5171 # lengths include the length of any signs.
5172 21 my @len_unsigned;
5173 my @len_signed;
5174 21 my @lengths;
5175 21 foreach my $ix ( $ix_first .. $ix_last ) {
5176 102 my $line = $rgroup_lines->[$ix];
5177 102 my $rfield = $line->{'rfields'};
5178 102 my $str = substr( $rfield->[$jcol], $pos_start_number );
5179 102 ▶ if ( $str =~ /^([^\s\,\)\]\}]*)/ ) { $str = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([^\s\,\)\]\}]*)/) 102 0 102
5180 102 my $len = length($str);
5181 102 ▶ if ( $is_signed{$ix} ) { push @len_signed, $len }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_signed{$ix}) { } 33 69 102
5182 69 else { push @len_unsigned, $len }
5183 102 push @lengths, [ $len, $ix ];
5184 }
5185  
5186 21 my ( $min_unsigned_length, $max_unsigned_length, $median_unsigned_length )
5187 = min_max_median( \@len_unsigned );
5188 21 my ( $min_signed_length_uu, $max_signed_length, $median_signed_length ) =
5189 min_max_median( \@len_signed );
5190  
5191 # Skip padding if no signed numbers exceed unsigned numbers in length
5192 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
5193 0 return;
5194 }
5195  
5196 # If max signed length is greatest - all unsigned values can be padded
5197 elsif ( $max_signed_length > $max_unsigned_length ) {
5198  
5199 # Example:
5200 # %wind_dir = (
5201 # 'n' => [ 1, 0 ],
5202 # 'ne' => [ 1, 1 ],
5203 # 'e' => [ 0, 1 ],
5204 # 'se' => [ -1, 1 ],
5205 # 's' => [ -1, 0 ],
5206 # 'sw' => [ -1, -1 ],
5207 # 'w' => [ 0, -1 ],
5208 # 'nw' => [ 1, -1 ],
5209 # '' => [ 0, 0 ],
5210 # );
5211  
5212 # This is the ideal case - ok to continue and pad
5213 }
5214  
5215 # intermediate case: some signed numbers cannot be padded ...
5216 else {
5217  
5218 # We have to take a closer look.
5219 # Here is an example which looks bad if we do padding like this:
5220 # my %hash = (
5221 # X0 => -12867.098241163,
5222 # X1 => 2.31694338671684, # unsigned w/ excess>0
5223 # X2 => 0.0597726714860419, # max length => excess=0
5224 # Y0 => 30043.1335503155, # unsigned w/ excess>0
5225 # Y1 => 0.0525784981597044, # max length => excess=0
5226 # Y2 => -2.32447131600783,
5227 # );
5228  
5229 # To decide what looks okay, we count 'good' and 'bad' line interfaces:
5230 # X0 - X1 = good (X0 is signed and X1 can move)
5231 # X1 - X2 = bad (x1 can move but x2 cannot)
5232 # X2 - Y0 = bad (x2 cannot move but Y0 can move)
5233 # Y0 - Y1 = bad (Y0 can move but Y1 cannot move)
5234 # Y1 - Y2 = bad (Y1 cannot move and Y2 is signed)
5235 # Result: 4 bad interfaces and 1 good => so we will skip this
5236 11 my $good_count = 0;
5237 11 my $bad_count = 0;
5238 11 foreach my $item (@lengths) {
5239 54 $item->[0] = $max_unsigned_length - $item->[0];
5240 }
5241 11 my $item0 = shift @lengths;
5242 11 my ( $excess, $ix ) = @{$item0};
5243 11 ▶ my $immobile_count = $excess ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$excess ? : 11 0 11
5244 11 foreach my $item (@lengths) {
5245 43 my $excess_m = $excess;
5246 43 my $ix_m = $ix;
5247 43 ( $excess, $ix ) = @{$item};
5248 43 ▶ if ( !$excess ) { $immobile_count++ }
Branch: 1 branch
BranchTrueFalseTotal
unless ($excess) 13 30 43
5249  
5250 43 ▶ if ( $is_signed{$ix_m} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_signed{$ix_m}) { } 15 28 43
5251  
5252 # signed-unsigned interface
5253 15 ▶ if ( !$is_signed{$ix} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_signed{$ix}) 13 2 15
5254 13 ▶ if ($excess) { $good_count++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($excess) { } 8 5 13
5255 5 else { $bad_count++ }
5256 }
5257  
5258 # signed-signed: ok, not good or bad
5259 }
5260 else {
5261  
5262 # unsigned-signed interface
5263 28 ▶ if ( $is_signed{$ix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_signed{$ix}) { } 9 19 28
5264 9 ▶ if ($excess_m) { $good_count++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($excess_m) { } 9 0 9
5265 0 else { $bad_count++ }
5266 }
5267  
5268 # unsigned-unsigned: bad if different
5269 else {
5270 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
5271 7 $bad_count++;
5272 }
5273 }
5274 }
5275 }
5276  
5277 # Filter 1: skip if more interfaces are 'bad' than 'good'
5278 11 ▶ if ( $bad_count > $good_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad_count > $good_count) 1 10 11
5279 1 return;
5280 }
5281  
5282 # Filter 2: skip in a table with multiple 'bad' interfaces and where
5283 # 'most' of the unsigned lengths are shorter than the signed lengths.
5284 # Using the median value makes this insensitive to small changes.
5285 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
5286 && $bad_count > 1
5287 && $immobile_count > 1 )
5288 {
5289 0 return;
5290 }
5291  
5292 # Anything that gets past these filters should look ok if padded
5293 }
5294  
5295 #---------------------------------------------
5296 # Compute actual available space for each line
5297 #---------------------------------------------
5298 20 my %excess_space;
5299 20 my $movable_count = 0;
5300 20 foreach my $item (@unsigned_subgroups) {
5301 34 my ( $ix_min, $ix_max ) = @{$item};
5302 34 foreach my $ix ( $ix_min .. $ix_max ) {
5303 56 my $line = $rgroup_lines->[$ix];
5304 56 my $leading_space_count = $line->{'leading_space_count'};
5305 56 my $jmax = $line->{'jmax'};
5306 56 my $rfield_lengths = $line->{'rfield_lengths'};
5307 56 ▶ if ( $jcol >= $jmax ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jcol >= $jmax) 0 56 56
5308  
5309 # shouldn't happen
5310 0 DEVEL_MODE && Fault("jcol=$jcol >= jmax=$jmax\n");
5311 0 return;
5312 }
5313 56 my @alignments = @{ $line->{'ralignments'} };
5314 56 my $col = $alignments[$jcol]->{'column'};
5315 my $col_start =
5316 $jcol == 0
5317 ? $leading_space_count
5318 56 ▶ : $alignments[ $jcol - 1 ]->{'column'};
Branch: 1 branch
BranchTrueFalseTotal
$jcol == 0 ? : 16 40 56
5319 56 my $avail = $col - $col_start;
5320 56 my $field_length = $rfield_lengths->[$jcol];
5321 56 my $excess = $avail - $field_length;
5322 56 $excess_space{$ix} = $excess;
5323 56 ▶ if ( $excess > 0 ) { $movable_count++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($excess > 0) 56 0 56
5324 }
5325 }
5326  
5327 20 ▶ return unless ($movable_count);
Branch: 1 branch
BranchTrueFalseTotal
unless $movable_count 0 20 20
5328  
5329 # Count the number of signed-unsigned interfaces that would change
5330 # if we do the padding
5331 20 my $Nc = 0;
5332 20 foreach my $item (@unsigned_subgroups) {
5333 34 my ( $ix_min, $ix_max ) = @{$item};
5334 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
5335 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
5336 }
5337  
5338 #--------------------------------------------------------------------
5339 # Sparsity check:
5340 # Give up if the number of interface changes will be below the cutoff
5341 #--------------------------------------------------------------------
5342 20 ▶ if ( $unsigned > $Nc * $rOpts_valign_signed_numbers_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsigned > $Nc * $rOpts_valign_signed_numbers_limit) 0 20 20
5343 0 return;
5344 }
5345  
5346 #------------------------------------------------------------------------
5347 # Insert an extra space before the unsigned numbers if space is available
5348 #------------------------------------------------------------------------
5349 20 foreach my $item (@unsigned_subgroups) {
5350 34 my ( $ix_min, $ix_max ) = @{$item};
5351  
5352 34 foreach my $ix ( $ix_min .. $ix_max ) {
5353 56 ▶ next if ( $excess_space{$ix} <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $excess_space{$ix} <= 0 0 56 56
5354 56 my $line = $rgroup_lines->[$ix];
5355 56 my $rfields = $line->{'rfields'};
5356 56 my $rfield_lengths = $line->{'rfield_lengths'};
5357 56 pad_signed_field(
5358 \$rfields->[$jcol], \$rfield_lengths->[$jcol],
5359 $pos_start_number, $char_end_part1
5360 );
5361 }
5362 }
5363 20 return;
5364 } ## end sub end_signed_number_column
5365  
5366 56 ▶sub pad_signed_field {
Subroutine pad_signed_field: called CC=5 SCAR=16.8
Pod undocumented
5367  
5368 56 my ( $rstr, $rstr_len, $pos_start_number, $char_end_part1 ) = @_;
5369  
5370 # Insert an extra space before a number to highlight algebraic signs
5371 # in a column of numbers.
5372 # Given:
5373 # $rstr = ref to string
5374 # $rstr_len = ref to display width of string (could include wide chars)
5375 # $pos_start_number = string position of the leading digit
5376 # $char_end_part1 = character at $pos_start_number - 1
5377 # Task: update $rstr and $rstr_len with a single space
5378  
5379 # First partition the string into $part1 and $part2, so that the
5380 # number starts at the beginning of part2.
5381 56 my $part1 = EMPTY_STRING;
5382 56 my $part2 = ${$rstr};
5383 56 my $str = ${$rstr};
5384 56 ▶ if ( $pos_start_number > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_start_number > 0) 16 40 56
5385 16 my $len = length($str);
5386 16 ▶ if ( $pos_start_number >= $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos_start_number >= $len) 0 16 16
5387 0 DEVEL_MODE && Fault(<<EOM);
5388 Expection position '$pos_start_number' < length $len of string '$str'
5389 EOM
5390 0 return;
5391 }
5392 16 $part1 = substr( $str, 0, $pos_start_number );
5393 16 $part2 = substr( $str, $pos_start_number );
5394  
5395 # VERIFY that we are inserting a new space after either
5396 # (1) an existing space or
5397 # (2) an opening token.
5398 # Otherwise disaster can occur. An error here implies a programming
5399 # error in defining '$pos_start_number'.
5400  
5401 16 my $test_char1 = substr( $part1, -1, 1 );
5402 16 ▶ if ( $test_char1 ne $char_end_part1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_char1 ne $char_end_part1) 0 16 16
5403 0 DEVEL_MODE && Fault(<<EOM);
5404 Expecting '$char_end_part1' but saw '$test_char1' in string '$str'
5405 Probably bad position '$pos_start_number'
5406 EOM
5407 0 return;
5408 }
5409 }
5410  
5411 # VERIFY we are inserting a space before a digit character
5412 56 my $test_char2 = substr( $part2, 0, 1 );
5413 56 ▶ if ( $is_digit_char{$test_char2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_digit_char{$test_char2}) { } 56 0 56
5414 56 ${$rstr} = $part1 . SPACE . $part2;
5415 56 ${$rstr_len} += 1;
5416 }
5417 else {
5418 0 DEVEL_MODE && Fault(<<EOM);
5419 Expecting test char2 as leading digit but saw '$test_char2' in string '$str'
5420 May be bad position '$pos_start_number'
5421 EOM
5422 }
5423 56 return;
5424 } ## end sub pad_signed_field
5425  
5426 124 ▶sub split_field {
Subroutine split_field: called CC=27 SCAR=35.5
Pod undocumented
5427  
5428 124 my ( $pat1, $field, $pattern ) = @_;
5429  
5430 # Given;
5431 # $pat1 = first part of a pattern before a numeric type 'n'
5432 # $field = corresponding text field
5433 # $pattern = full pattern
5434 # Return:
5435 # $pos_start_number = position in $field where the number should start
5436 # = 0 if cannot find
5437 # $char_end_part1 = the character preceding $pos_start_number
5438 # $ch_opening = the preceding opening container character, if any
5439  
5440 # We have to find where the possible number starts in this field.
5441 # The safe thing to do is return @fail if anything does not look right.
5442  
5443 124 my $pos_start_number = 0;
5444 124 my $char_end_part1 = EMPTY_STRING;
5445 124 my $ch_opening = EMPTY_STRING;
5446 124 my @fail = ( $pos_start_number, $char_end_part1, $ch_opening );
5447  
5448 # Be sure there is just 'n' in the pattern. Multiple terms can occur
5449 # when fields are joined, but since we are jumping into the middle
5450 # of a field it is safest not to try to handle them.
5451 124 my $n_count = ( $pattern =~ tr/n/n/ );
5452 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
5453 20 return @fail;
5454 }
5455  
5456 # Same thing for commas
5457 104 my $comma_count = ( $pattern =~ tr/,/,/ );
5458 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
5459 0 return @fail;
5460 }
5461  
5462 # Require 0 or 1 braces
5463 104 my $len_field = length($field);
5464 104 my $len_pat1 = length($pat1);
5465 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
5466  
5467 # Look at the pattern ending
5468 104 my $ending_b = 0;
5469 104 my $ch = substr( $pat1, -1, 1 );
5470 104 ▶ if ( $ch eq 'b' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "b") 79 25 104
5471 79 $ending_b = 1;
5472 79 $ch = substr( $pat1, -2, 1 );
5473 79 $char_end_part1 = SPACE;
5474 }
5475  
5476 # handle either '{b' or '{'
5477 104 ▶ if ( $ch eq '{' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq '{') { } 56 48 104
5478  
5479 # Only one brace
5480 56 my $brace_count = ( $pat1 =~ tr/\{/\{/ );
5481 56 ▶ return @fail if ( $brace_count != 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $brace_count != 1 0 56 56
5482  
5483 56 my $i_paren = index( $field, '(' );
5484 56 my $i_bracket = index( $field, '[' );
5485 56 my $i_brace = index( $field, '{' );
5486 56 my $i_opening = length($field);
5487 56 ▶ if ( $i_paren >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_paren >= 0) 18 38 56
5488 18 $i_opening = $i_paren;
5489 18 $ch_opening = '(';
5490 }
5491 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
5492 && $i_bracket < $i_opening )
5493 {
5494 38 $i_opening = $i_bracket;
5495 38 $ch_opening = '[';
5496 }
5497 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
5498 0 $i_opening = $i_brace;
5499 0 $ch_opening = '{';
5500 }
5501 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
5502 && $i_opening < length($field) - 1 )
5503 {
5504 56 $pos_start_number = $i_opening + 1 + $ending_b;
5505 56 ▶ $char_end_part1 = $ch_opening
Branch: 1 branch
BranchTrueFalseTotal
unless $ending_b 25 31 56
5506 if ( !$ending_b );
5507 }
5508 else {
5509 ## strange - could not find the opening token
5510 }
5511 }
5512  
5513 # no braces: maybe '=>b'
5514 else {
5515  
5516 # looking for patterns ending in '=b' or '=>b'
5517 0 ▶ if ( !$ending_b ) { return @fail }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ending_b) 0 48 48
5518  
5519 # find the = in the text
5520 48 my $pos_equals = index( $field, '=' );
5521 48 ▶ return @fail if ( $pos_equals < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pos_equals < 0 11 37 48
5522  
5523 # be sure there are no other '=' in the pattern
5524 37 my $equals_count = ( $pat1 =~ tr/=/=/ );
5525 37 ▶ return @fail if ( $equals_count != 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $equals_count != 1 3 34 37
5526  
5527 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
5528 8 $pos_start_number = $pos_equals + 2;
5529 }
5530 ▶ 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
5531 16 $pos_start_number = $pos_equals + 3;
5532 }
5533 else {
5534  
5535 # cannot handle this pattern
5536 10 return @fail;
5537 }
5538 }
5539  
5540 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
5541 0 return @fail;
5542 }
5543  
5544 80 return ( $pos_start_number, $char_end_part1, $ch_opening );
5545 } ## end sub split_field
5546  
5547 264 ▶sub field_matches_end_pattern {
Subroutine field_matches_end_pattern: called CC=9 SCAR=23.2
Pod undocumented
5548  
5549 264 my ( $field2, $pat2 ) = @_;
5550  
5551 # Check that a possible numeric field matches the ending pattern
5552  
5553 # Given:
5554 # $field2 = the rest of the field after removing any sign
5555 # $pat2 = the end pattern of this field
5556 # Return:
5557 # false if field is definitely non-numeric
5558 # true otherwise
5559  
5560 264 my $next_char = substr( $pat2, 1, 1 );
5561 264 my $field2_trim = EMPTY_STRING;
5562  
5563 # if pattern is one of: 'n,', 'n,b'
5564 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
5565 192 my $icomma = index( $field2, COMMA );
5566 192 ▶ if ( $icomma >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($icomma >= 0) 192 0 192
5567 192 $field2_trim = substr( $field2, 0, $icomma );
5568 }
5569 }
5570  
5571 # if pattern is one of: 'nb', 'nb}', 'nb},'
5572 elsif ( $next_char eq 'b' ) {
5573 47 my $ispace = index( $field2, SPACE );
5574 47 ▶ if ( $ispace >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ispace >= 0) 47 0 47
5575 47 $field2_trim = substr( $field2, 0, $ispace );
5576 }
5577 }
5578  
5579 # if pattern is one of 'n},', 'n};'
5580 elsif ( $next_char eq '}' ) {
5581 25 ▶ if ( $field2 =~ /^([^\)\}\]]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field2 =~ /^([^\)\}\]]+)/) 25 0 25
5582 25 $field2_trim = $1;
5583 }
5584 }
5585  
5586 # unrecognized pattern
5587 else {
5588 0 DEVEL_MODE && Fault(<<EOM);
5589 Unexpected ending pattern '$pat2' next='$next_char' field2='$field2'
5590 The hash 'is_leading_sign_pattern' seems to have changed but the code
5591 has not been updated to handle it. Please fix.
5592 EOM
5593 0 return;
5594 }
5595  
5596 264 ▶ if ( !length($field2_trim) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $field2_trim) 0 264 264
5597 0 DEVEL_MODE
5598 && Fault(
5599 "STRANGE: cannot find end of field=$field2 for pat=$pat2 \n");
5600 0 return;
5601 }
5602  
5603 # Reject obviously non-numeric fields just to be sure we did not
5604 # jump into a quote of some kind
5605 264 ▶ if ( $field2_trim !~ /^[\d\.\+\-abcdefpx_]+$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field2_trim =~ /^[\d\.\+\-abcdefpx_]+$/i) 2 262 264
5606 DEBUG_VSN
5607 2 && print {*STDERR}
5608 "Rejecting match to pat2='$pat2' with next=$next_char field2=$field2 trimmed='$field2_trim'\n";
5609 2 return;
5610 }
5611 262 return 1;
5612 } ## end sub field_matches_end_pattern
5613  
5614 70 ▶sub pad_signed_number_columns {
Subroutine pad_signed_number_columns: called CC=52 SCAR=41.6
Pod undocumented
5615  
5616 70 my ($rgroup_lines) = @_;
5617  
5618 # Given:
5619 # $rgroup_lines = the current vertical alignment group of lines
5620 # Task:
5621 # Look for columns of aligned numeric values, some of whose numbers
5622 # have algebraic signs. Add a leading space to the unsigned
5623 # numbers, if possible, so that the just the signs appear as the first
5624 # character. Example of what we want to do:
5625  
5626 # my @correct = (
5627 # [ 123456.79, 86753090000.868, 11 ],
5628 # [ -123456.79, -86753090000.868, -11 ],
5629 # [ 123456.001, 80.080, 10 ],
5630 # [ -123456.001, -80.080, 0 ],
5631 # [ 10.9, 10.9, 11 ],
5632 # );
5633  
5634 # The logic here is complex because we are working with bits of text
5635 # which have been broken into patterns which are convenient for the
5636 # vertical aligner, but we no longer have the original tokenization
5637 # which would have indicated the precise bounds of numbers. So we
5638 # have to proceed very carefully with lots of checks. There are
5639 # more checks than really necessary now because originally numbers
5640 # and quotes were both indicated with pattern 'Q'. But now numbers are
5641 # uniquely marked as pattern 'n', so there is less risk of an error.
5642 # The extra checks take very little time so they are retained.
5643  
5644 70 ▶ return unless ($rOpts_valign_signed_numbers);
Branch: 1 branch
BranchTrueFalseTotal
unless $rOpts_valign_signed_numbers 0 70 70
5645  
5646 70 my %column_info;
5647 my @columns;
5648  
5649 #----------------
5650 # loop over lines
5651 #----------------
5652 70 my $ix_line = -1;
5653 70 my $jmax = -1;
5654 70 foreach my $line ( @{$rgroup_lines} ) {
5655 349 $ix_line++;
5656 349 my $jmax_last = $jmax;
5657 349 $jmax = $line->{'jmax'};
5658 349 my $jmax_change = $jmax ne $jmax_last;
5659  
5660 349 my @alignments = @{ $line->{'ralignments'} };
5661 349 my $rfields = $line->{'rfields'};
5662 349 my $rpatterns = $line->{'rpatterns'};
5663 349 my $rtokens = $line->{'rtokens'};
5664  
5665 #-----------------------------------------------
5666 # Check for a reduction in the number of columns
5667 #-----------------------------------------------
5668 349 ▶ if ( $jmax < $jmax_last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax < $jmax_last) 11 338 349
5669  
5670 11 foreach my $jcol ( keys %column_info ) {
5671  
5672 # end any stranded columns on the right
5673 6 ▶ next if ( $jcol < $jmax );
Branch: 1 branch
BranchTrueFalseTotal
if $jcol < $jmax 4 2 6
5674 2 my $rcol_hash = $column_info{$jcol};
5675 2 ▶ next unless ($rcol_hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $rcol_hash 0 2 2
5676 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
5677 && $rcol_hash->{unsigned_count} )
5678 {
5679 0 end_signed_number_column( $rgroup_lines, $rcol_hash,
5680 $ix_line - 1 );
5681 }
5682 2 delete $column_info{$jcol};
5683 }
5684  
5685 # Try to keep the end data column running; test case 'rfc.in'
5686 # The last item in a list will still need a trailing comma.
5687 11 my $jcol = $jmax - 1;
5688 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
5689 2 my $alignment = $alignments[$jcol];
5690 2 my $old_col = $columns[$jcol];
5691 2 my $col = $alignment->{column};
5692  
5693 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
5694 $col < $old_col
5695  
5696 # only do this if the text has a leading digit
5697 && $rfields->[$jcol] =~ /^([+-]?)\d/
5698  
5699 # and a signed number has been seen - issue c375
5700 && ( $1 || $column_info{$jcol}->{signed_count} )
5701 )
5702 {
5703 0 my $spaces_needed = $old_col - $col;
5704 0 my $spaces_available =
5705 $line->get_available_space_on_right();
5706 0 ▶ if ( $spaces_available >= $spaces_needed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spaces_available >= $spaces_needed) 0 0 0
5707 0 $line->increase_field_width( $jcol, $spaces_needed );
5708 }
5709 }
5710 }
5711 }
5712  
5713 #--------------------------------------------
5714 # Loop over fields except last (side comment)
5715 #--------------------------------------------
5716 349 for my $jcol ( 0 .. $jmax - 1 ) {
5717  
5718 #-----------------------------------------
5719 # Decide if this is a new alignment column
5720 #-----------------------------------------
5721 785 my $alignment = $alignments[$jcol];
5722 785 my $old_col = $columns[$jcol];
5723 785 my $col = $alignment->{column};
5724 785 $columns[$jcol] = $col;
5725 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
5726 55 foreach my $jcol_old ( keys %column_info ) {
5727 17 ▶ next if ( $jcol_old < $jcol );
Branch: 1 branch
BranchTrueFalseTotal
if $jcol_old < $jcol 6 11 17
5728 11 my $rcol_hash = $column_info{$jcol_old};
5729 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
5730 && $rcol_hash->{unsigned_count} )
5731 {
5732 1 end_signed_number_column( $rgroup_lines, $rcol_hash,
5733 $ix_line - 1 );
5734 }
5735 11 delete $column_info{$jcol_old};
5736 }
5737 }
5738  
5739 # A new padded sign column can only start at an alignment change
5740 785 my $rcol_hash = $column_info{$jcol};
5741  
5742 #------------------------------------------------------------
5743 # Examine this field, looking for signed and unsigned numbers
5744 #------------------------------------------------------------
5745 785 my $field = $rfields->[$jcol];
5746 785 my $pattern = $rpatterns->[$jcol];
5747  
5748 785 my $is_signed_number = 0;
5749 785 my $is_unsigned_number = 0;
5750  
5751 #--------------------------------------------------------
5752 # set $pos_start_number = index in field of digit or sign
5753 #--------------------------------------------------------
5754 785 my $pos_start_number = 0;
5755 785 my $char_end_part1 = EMPTY_STRING;
5756 785 my $ch_opening = EMPTY_STRING;
5757  
5758 # Set $field_ok to false on encountering any problem
5759 # Do not pad signed and unsigned hash keys
5760 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
5761 && substr( $rtokens->[$jcol], 0, 2 ) ne '=>';
5762  
5763 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
5764  
5765 # Split the pattern at the first 'n'
5766 # $pat1 = pattern before the 'n' (if any)
5767 # $pat2 = pattern starting at the 'n'
5768 755 my ( $pat1, $pat2 );
5769 755 my $posq = index( $pattern, 'n' );
5770 755 ▶ if ( $posq < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posq < 0) { } 333 422 755
5771 333 $field_ok = 0;
5772 }
5773 else {
5774 # Just look at up to 3 of the pattern characters
5775 # We require $pat2 to have one of the known patterns
5776 422 $pat1 = substr( $pattern, 0, $posq );
5777 422 $pat2 = substr( $pattern, $posq, 3 );
5778 422 $field_ok = $is_leading_sign_pattern{$pat2};
5779 }
5780  
5781 755 ▶ if ($field_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_ok) 310 445 755
5782  
5783 # If the number starts within the field then we must
5784 # find its offset position.
5785 310 ▶ if ($pat1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat1) 124 186 310
5786  
5787 # Note: an optimization would be to remember previous
5788 # calls for each column and use them if possible, but
5789 # benchmarking shows that this is not necessary.
5790 # See .ba54 for example coding.
5791 124 ( $pos_start_number, $char_end_part1, $ch_opening ) =
5792 split_field( $pat1, $field, $pattern );
5793  
5794 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
5795 }
5796  
5797 310 ▶ if ($field_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_ok) 310 0 310
5798  
5799 # look for an optional + or - sign
5800 310 my $test_char = substr( $field, $pos_start_number, 1 );
5801 310 my $sign;
5802 310 ▶ if ( $is_plus_or_minus{$test_char} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_plus_or_minus{$test_char}) 49 261 310
5803 49 $sign = $test_char;
5804 49 $test_char =
5805 substr( $field, $pos_start_number + 1, 1 );
5806 }
5807  
5808 # and a digit
5809 310 ▶ if ( $is_digit_char{$test_char} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_digit_char{$test_char}) { } 264 46 310
5810 264 my $field2;
5811 264 ▶ if ($sign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign) { } 47 217 264
5812 47 $is_signed_number = 1;
5813 47 $field2 =
5814 substr( $field, $pos_start_number + 1 );
5815 }
5816 else {
5817 217 $is_unsigned_number = 1;
5818 217 ▶ $field2 =
Branch: 1 branch
BranchTrueFalseTotal
$pos_start_number ? : 67 150 217
5819 $pos_start_number
5820 ? substr( $field, $pos_start_number )
5821 : $field;
5822 }
5823  
5824 # Check for match to ending pattern
5825 264 $field_ok =
5826 field_matches_end_pattern( $field2, $pat2 );
5827 }
5828 else {
5829 46 $field_ok = 0;
5830 }
5831 }
5832 }
5833 }
5834  
5835 #----------------------
5836 # Figure out what to do
5837 #----------------------
5838  
5839 # we require a signed or unsigned number field
5840 # which is not a hash key
5841 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
5842  
5843 # if a column has not started..
5844 785 ▶ if ( !$rcol_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rcol_hash) { } 584 201 785
5845  
5846 # give up if this is cannot start a new column
5847 584 ▶ next if ( !$field_ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $field_ok 500 84 584
5848  
5849 # otherwise continue on to start a new column
5850  
5851 }
5852  
5853 # if a column has been started...
5854 else {
5855  
5856 # and this cannot be added to it
5857 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) 34 167 201
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
5858 || $rcol_hash->{pos_start_number} ne $pos_start_number
5859 || $rcol_hash->{char_end_part1} ne $char_end_part1
5860 || $rcol_hash->{col} ne $col )
5861 {
5862  
5863 # then end the current column and start over
5864 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
5865 && $rcol_hash->{unsigned_count} )
5866 {
5867 0 end_signed_number_column( $rgroup_lines, $rcol_hash,
5868 $ix_line - 1 );
5869 }
5870 34 delete $column_info{$jcol};
5871 34 $rcol_hash = undef;
5872 }
5873 }
5874  
5875 285 if (DEBUG_VSN) {
5876 my $exists = defined($rcol_hash);
5877 print
5878 "VSN: line=$ix_line change=$jmax_change jcol=$jcol field=$field exists?=$exists unsigned?=$is_unsigned_number signed?=$is_signed_number\n";
5879 }
5880  
5881 #---------------------------------------
5882 # Either start a new column, if possible
5883 #---------------------------------------
5884 285 ▶ if ( !defined($rcol_hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rcol_hash) { } 118 167 285
5885  
5886 118 ▶ next if ( !$field_ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $field_ok 23 95 118
5887  
5888 95 ▶ my $rsigned_lines = $is_signed_number ? [$ix_line] : [];
Branch: 1 branch
BranchTrueFalseTotal
$is_signed_number ? : 12 83 95
5889 95 $column_info{$jcol} = {
5890 unsigned_count => $is_unsigned_number,
5891 signed_count => $is_signed_number,
5892 pos_start_number => $pos_start_number,
5893 char_end_part1 => $char_end_part1,
5894 ix_first => $ix_line,
5895 col => $col,
5896 jcol => $jcol,
5897 rsigned_lines => $rsigned_lines,
5898 };
5899 }
5900  
5901 #------------------------------
5902 # or extend the existing column
5903 #------------------------------
5904 else {
5905 167 $rcol_hash->{unsigned_count} += $is_unsigned_number;
5906 167 $rcol_hash->{signed_count} += $is_signed_number;
5907 167 ▶ if ($is_signed_number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_signed_number) 35 132 167
5908 35 push @{ $rcol_hash->{rsigned_lines} }, $ix_line;
5909 }
5910 }
5911 }
5912 }
5913  
5914 #-------------------------------------
5915 # Loop to finish any remaining columns
5916 #-------------------------------------
5917 70 foreach my $jcol ( keys %column_info ) {
5918 48 my $rcol_hash = $column_info{$jcol};
5919 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
5920 29 end_signed_number_column( $rgroup_lines, $rcol_hash, $ix_line );
5921 }
5922 }
5923 70 return;
5924 } ## end sub pad_signed_number_columns
5925  
5926 #########################################
5927 # CODE SECTION 7: Pad Wide Equals Columns
5928 #########################################
5929  
5930 45 ▶use constant DEBUG_WEC => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5931  
5932 12 ▶sub end_wide_equals_column {
Subroutine end_wide_equals_column: called CC=15 SCAR=27.3
Pod undocumented
5933  
5934 12 my ( $rgroup_lines, $rcol_hash, $ix_last ) = @_;
5935  
5936 # Finish formatting a column of wide equals
5937 # Given:
5938 # $rgroup_lines - the current vertical alignment group of lines
5939 # $rcol_hash - a hash of information about this vertical column
5940 # $ix_last - index of the last line of this vertical column
5941  
5942 12 ▶ return unless ($rcol_hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $rcol_hash 0 12 12
5943 12 my $jcol = $rcol_hash->{jcol};
5944 12 my $col = $rcol_hash->{col};
5945 12 my $min_width = $rcol_hash->{min_width};
5946 12 my $max_width = $rcol_hash->{max_width};
5947 12 my $rwidths = $rcol_hash->{rwidths};
5948 12 my $ix_first = $rcol_hash->{ix_first};
5949  
5950 # check for skipped lines, shouldn't happen
5951 12 my $nlines = $ix_last - $ix_first + 1;
5952 12 my $num = @{$rwidths};
5953 12 ▶ if ( $num != $nlines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num != $nlines) 0 12 12
5954 0 my $line = $rgroup_lines->[$ix_last];
5955 0 my $rfields = $line->{'rfields'};
5956 0 my $text = join( EMPTY_STRING, @{$rfields} );
5957 0 DEVEL_MODE && Fault(<<EOM);
5958 We seem to have miscounted lines, please check:
5959 nlines=$nlines
5960 num saved=$num
5961 min width=$min_width
5962 max width=$max_width
5963 j=$jcol
5964 ix_first=$ix_first
5965 ix_last=$ix_last
5966 text=$text
5967 EOM
5968 0 return;
5969 }
5970  
5971 #------------------------------------------------------
5972 # loop over all lines of this vertical alignment column
5973 #------------------------------------------------------
5974  
5975 my (
5976 12 $current_alignment, $starting_colp,
5977 $current_line, @previous_linked_lines
5978 );
5979 12 foreach my $item ( @{$rwidths} ) {
5980 50 my ( $ix, $width ) = @{$item};
5981 50 my $line = $rgroup_lines->[$ix];
5982  
5983 # add leading spaces to the shorter equality tokens to get
5984 # vertical alignment of the '=' signs
5985 50 my $jmax = $line->{'jmax'};
5986 50 my $jcolp = $jcol + 1;
5987  
5988 50 my @alignments = @{ $line->{'ralignments'} };
5989 50 my $alignment = $alignments[$jcolp];
5990 50 my $colp = $alignment->{column};
5991  
5992 #------------------------------------------------------------
5993 # Transfer column width changes between equivalent alignments
5994 #------------------------------------------------------------
5995  
5996 # This step keeps alignments to the right correct in case the
5997 # alignment object changes but the actual alignment col does not.
5998 # It is extremely rare for this to occur. Issue c353.
5999  
6000 # nothing to do if no more real alignments on right
6001 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
6002 18 $current_alignment = undef;
6003 18 $current_line = undef;
6004 18 @previous_linked_lines = ();
6005 }
6006  
6007 # handle new rhs alignment
6008 elsif ( !$current_alignment ) {
6009 9 $current_alignment = $alignment;
6010 9 $current_line = $line;
6011 9 $starting_colp = $colp;
6012 9 @previous_linked_lines = ();
6013 }
6014  
6015 # handle change in existing alignment
6016 elsif ( refaddr($alignment) != refaddr($current_alignment) ) {
6017  
6018 # change rhs alignment column - new vertical group on right
6019 6 ▶ if ( $starting_colp != $colp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($starting_colp != $colp) { } 1 5 6
6020 1 $starting_colp = $colp;
6021 1 @previous_linked_lines = ();
6022 }
6023 else {
6024  
6025 # Same starting alignment col on right, but different alignment
6026 # object. See if we must increase width of this new alignment
6027 # object.
6028 5 my $current_colp = $current_alignment->{column};
6029 5 ▶ if ( $current_colp > $colp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_colp > $colp) 3 2 5
6030 3 my $excess = $current_colp - $colp;
6031 3 my $padding_available =
6032 $line->get_available_space_on_right();
6033 3 ▶ if ( $excess <= $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess <= $padding_available) 3 0 3
6034 3 $line->increase_field_width( $jcolp, $excess );
6035 3 $colp = $alignment->{column};
6036 }
6037 }
6038  
6039 # remember the previous line in case we have to go back and
6040 # increase its width
6041 5 push @previous_linked_lines, $current_line;
6042 }
6043 6 $current_alignment = $alignment;
6044 6 $current_line = $line;
6045 }
6046 else {
6047 ## continuing with same alignment
6048 }
6049  
6050 #-----------------------
6051 # add any needed padding
6052 #-----------------------
6053 50 my $pad = $max_width - $width;
6054 50 ▶ if ( $pad > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > 0) 33 17 50
6055  
6056 33 my $rfields = $line->{'rfields'};
6057 33 my $rfield_lengths = $line->{'rfield_lengths'};
6058  
6059 33 my $lenp = $rfield_lengths->[$jcolp];
6060 33 my $avail = $colp - $col;
6061 33 my $excess = $lenp + $pad - $avail;
6062  
6063 33 ▶ if ( $excess > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess > 0) 13 20 33
6064  
6065 13 my $padding_available = $line->get_available_space_on_right();
6066 13 ▶ if ( $excess <= $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess <= $padding_available) { } 12 1 13
6067 12 $line->increase_field_width( $jcolp, $excess );
6068  
6069 # Increase space of any previous linked lines
6070 12 foreach my $line_prev (@previous_linked_lines) {
6071 1 $padding_available =
6072 $line_prev->get_available_space_on_right();
6073 1 ▶ if ( $excess <= $padding_available ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess <= $padding_available) { } 1 0 1
6074 1 $line_prev->increase_field_width( $jcolp, $excess );
6075 }
6076 else {
6077 0 last;
6078 }
6079 }
6080 }
6081 else {
6082 1 $pad = 0;
6083 }
6084  
6085 }
6086  
6087 # Add spaces
6088 33 $rfields->[$jcolp] = ( SPACE x $pad ) . $rfields->[$jcolp];
6089 33 $rfield_lengths->[$jcolp] += $pad;
6090 }
6091 }
6092 12 return;
6093 } ## end sub end_wide_equals_column
6094  
6095 12 ▶sub pad_wide_equals_columns {
Subroutine pad_wide_equals_columns: called CC=30 SCAR=38.7
Pod undocumented
6096  
6097 12 my ($rgroup_lines) = @_;
6098  
6099 # Given:
6100 # $rgroup_lines = the current vertical alignment group of lines
6101 # Task:
6102 # Look for columns of aligned equals tokens, some of which may be
6103 # things like '-=', '&&=', etc. Increase the field length of the
6104 # previous field by 1 or 2 spaces where necessary and possible so
6105 # that alignment of all '=' occurs. For example, given
6106  
6107 # $j /= 2;
6108 # $pow2 = $pow2 * $pow2;
6109  
6110 # In this case we want to add a leading space '=' term to get
6111 # $j /= 2;
6112 # $pow2 = $pow2 * $pow2;
6113  
6114 # The logic here is somewhat similar to sub pad_signed_number_columns
6115  
6116 12 ▶ return unless ($rOpts_valign_wide_equals);
Branch: 1 branch
BranchTrueFalseTotal
unless $rOpts_valign_wide_equals 0 12 12
6117  
6118 12 my %column_info;
6119 my @columns;
6120  
6121 #----------------
6122 # loop over lines
6123 #----------------
6124 12 my $ix_line = -1;
6125 12 my $jmax = -1;
6126 12 foreach my $line ( @{$rgroup_lines} ) {
6127 50 $ix_line++;
6128 50 my $jmax_last = $jmax;
6129 50 $jmax = $line->{'jmax'};
6130 50 my $jmax_change = $jmax ne $jmax_last;
6131  
6132 50 my @alignments = @{ $line->{'ralignments'} };
6133 50 my $rfields = $line->{'rfields'};
6134 50 my $rtokens = $line->{'rtokens'};
6135  
6136 #-----------------------------------------------
6137 # Check for a reduction in the number of columns
6138 #-----------------------------------------------
6139 50 ▶ if ( $jmax < $jmax_last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jmax < $jmax_last) 6 44 50
6140  
6141 6 foreach my $jcol ( keys %column_info ) {
6142  
6143 # end any stranded columns on the right
6144 6 ▶ next if ( $jcol < $jmax );
Branch: 1 branch
BranchTrueFalseTotal
if $jcol < $jmax 6 0 6
6145 0 my $rcol_hash = $column_info{$jcol};
6146 0 ▶ next unless ($rcol_hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $rcol_hash 0 0 0
6147 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
6148 0 end_wide_equals_column( $rgroup_lines, $rcol_hash,
6149 $ix_line - 1 );
6150 }
6151 0 delete $column_info{$jcol};
6152 }
6153 }
6154  
6155 #--------------------------------------------------
6156 # Loop over fields except last field (side comment)
6157 #--------------------------------------------------
6158 50 for my $jcol ( 0 .. $jmax - 1 ) {
6159  
6160 #-----------------------------------------
6161 # Decide if this is a new alignment column
6162 #-----------------------------------------
6163 147 my $alignment = $alignments[$jcol];
6164 147 my $old_col = $columns[$jcol];
6165 147 my $col = $alignment->{column};
6166 147 $columns[$jcol] = $col;
6167 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
6168 14 foreach my $jcol_old ( keys %column_info ) {
6169 14 ▶ next if ( $jcol_old < $jcol );
Branch: 1 branch
BranchTrueFalseTotal
if $jcol_old < $jcol 14 0 14
6170 0 my $rcol_hash = $column_info{$jcol_old};
6171 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
6172 0 end_wide_equals_column( $rgroup_lines, $rcol_hash,
6173 $ix_line - 1 );
6174 }
6175 0 delete $column_info{$jcol_old};
6176 }
6177 }
6178  
6179 # A new wide equals column can only start at an alignment change
6180 147 my $rcol_hash = $column_info{$jcol};
6181  
6182 #------------------------------------------------------
6183 # Examine this field, looking for equals or wide equals
6184 #------------------------------------------------------
6185 147 my $field_next = $rfields->[ $jcol + 1 ];
6186 147 my $token = $rtokens->[$jcol];
6187  
6188 # See if this is an equals alignment group;
6189 # indicated by alignment token of '=' followed by a digit
6190 147 my $len_equals_symbol = 0;
6191 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
6192 && substr( $token, 0, 1 ) eq '='
6193 && $is_digit_char{ substr( $token, 1, 1 ) } )
6194 {
6195  
6196 # find the actual equality symbol which starts the next field
6197 # i.e. '=' or '**=' or '-=' etc. We just need its length.
6198 50 my $pos = index( $field_next, '=' );
6199 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
6200 50 $len_equals_symbol = $pos + 1;
6201 }
6202 }
6203  
6204 # if a column has not started..
6205 147 ▶ if ( !$rcol_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rcol_hash) { } 109 38 147
6206  
6207 # give up if this is cannot start a new column
6208 109 ▶ next if ( !$len_equals_symbol );
Branch: 1 branch
BranchTrueFalseTotal
unless $len_equals_symbol 97 12 109
6209  
6210 # otherwise continue on to start a new column
6211  
6212 }
6213  
6214 # if a column has been started...
6215 else {
6216  
6217 # and this cannot be added to it
6218 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
6219  
6220 # then end the current column and start over
6221 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
6222 0 end_wide_equals_column( $rgroup_lines, $rcol_hash,
6223 $ix_line - 1 );
6224 }
6225 0 delete $column_info{$jcol};
6226 0 $rcol_hash = undef;
6227 }
6228 }
6229  
6230 50 if (DEBUG_WEC) {
6231 my $exists = defined($rcol_hash);
6232 print
6233 "WEA: line=$ix_line change=$jmax_change jcol=$jcol field=$field_next exists?=$exists equals?=$len_equals_symbol\n";
6234 }
6235  
6236 #---------------------------------------
6237 # Either start a new column, if possible
6238 #---------------------------------------
6239 50 ▶ if ( !defined($rcol_hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rcol_hash) { } 12 38 50
6240  
6241 12 ▶ next if ( !$len_equals_symbol );
Branch: 1 branch
BranchTrueFalseTotal
unless $len_equals_symbol 0 12 12
6242  
6243 12 $column_info{$jcol} = {
6244 ix_first => $ix_line,
6245 col => $col,
6246 jcol => $jcol,
6247 min_width => $len_equals_symbol,
6248 max_width => $len_equals_symbol,
6249 rwidths => [ [ $ix_line, $len_equals_symbol ] ],
6250 };
6251 }
6252  
6253 #------------------------------
6254 # or extend the existing column
6255 #------------------------------
6256 else {
6257 38 ▶ if ( $len_equals_symbol > $rcol_hash->{max_width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_equals_symbol > $rcol_hash->{"max_width"}) 9 29 38
6258 9 $rcol_hash->{max_width} = $len_equals_symbol;
6259 }
6260 38 ▶ if ( $len_equals_symbol < $rcol_hash->{min_width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_equals_symbol < $rcol_hash->{"min_width"}) 4 34 38
6261 4 $rcol_hash->{min_width} = $len_equals_symbol;
6262 }
6263 38 push @{ $rcol_hash->{rwidths} },
6264 [ $ix_line, $len_equals_symbol ];
6265 }
6266 }
6267 }
6268  
6269 #-------------------------------------
6270 # Loop to finish any remaining columns
6271 #-------------------------------------
6272 12 foreach my $jcol ( keys %column_info ) {
6273 12 my $rcol_hash = $column_info{$jcol};
6274 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
6275 12 end_wide_equals_column( $rgroup_lines, $rcol_hash, $ix_line );
6276 }
6277 }
6278 12 return;
6279 } ## end sub pad_wide_equals_columns
6280  
6281 ###############################
6282 # CODE SECTION 8: Output Step A
6283 ###############################
6284  
6285 3651 ▶sub valign_output_step_A {
Subroutine valign_output_step_A: called CC=14 SCAR=26.9
Pod undocumented
6286  
6287 3651 my ( $self, $rinput_hash ) = @_;
6288  
6289 #------------------------------------------------------------
6290 # This is Step A in writing vertically aligned lines.
6291 # The line is prepared according to the alignments which have
6292 # been found. Then it is shipped to the next step.
6293 #------------------------------------------------------------
6294  
6295 my (
6296  
6297 $line,
6298 $min_ci_gap,
6299 $do_not_align,
6300 $group_leader_length,
6301 $extra_leading_spaces,
6302 $level,
6303 $maximum_line_length,
6304  
6305 ) =
6306  
6307 3651 @{$rinput_hash}{
6308 3651 qw(
6309 line
6310 min_ci_gap
6311 do_not_align
6312 group_leader_length
6313 extra_leading_spaces
6314 level
6315 maximum_line_length
6316 )
6317 };
6318  
6319 my (
6320  
6321 $rfields,
6322 $rfield_lengths,
6323 $leading_space_count,
6324 $outdent_long_lines,
6325 $maximum_field_index,
6326 $rvertical_tightness_flags,
6327 $Kend,
6328 $level_end,
6329  
6330 ) =
6331  
6332 3651 @{$line}{
6333 3651 qw(
6334 rfields
6335 rfield_lengths
6336 leading_space_count
6337 outdent_long_lines
6338 jmax
6339 rvertical_tightness_flags
6340 Kend
6341 level_end
6342 )
6343 };
6344  
6345 # Check for valid hash keys at end of lifetime of $line during development
6346 3651 DEVEL_MODE
6347 && check_keys( $line, \%valid_LINE_keys,
6348 "Checking line keys at valign_output_step_A", 1 );
6349  
6350 # add any extra spaces
6351 3651 ▶ if ( $leading_space_count > $group_leader_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count > $group_leader_length) 49 3602 3651
6352 49 $leading_space_count += $min_ci_gap;
6353 }
6354  
6355 3651 my $str = $rfields->[0];
6356 3651 my $str_len = $rfield_lengths->[0];
6357  
6358 3651 my @alignments = @{ $line->{'ralignments'} };
6359 3651 ▶ if ( @alignments != $maximum_field_index + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@alignments != $maximum_field_index + 1) 0 3651 3651
6360  
6361 # Shouldn't happen: sub install_new_alignments makes jmax alignments
6362 0 my $jmax_alignments = @alignments - 1;
6363 0 if (DEVEL_MODE) {
6364 Fault(
6365 "alignment jmax=$jmax_alignments should equal $maximum_field_index\n"
6366 );
6367 }
6368 0 $do_not_align = 1;
6369 }
6370  
6371 # loop to concatenate all fields of this line and needed padding
6372 3651 my $total_pad_count = 0;
6373 3651 for my $j ( 1 .. $maximum_field_index ) {
6374  
6375 # skip zero-length side comments
6376 last
6377 if (
6378 8780 ▶ ( $j == $maximum_field_index )
Branch: 1 branch
BranchTrueFalseTotal
if $j == $maximum_field_index and !defined($rfields->[$j]) || $rfield_lengths->[$j] == 0 3303 5477 8780
Condition83%5/6
$j == $maximum_field_index and !defined($rfields->[$j]) || $rfield_lengths->[$j] == 0
!ll&&!rl&&r
51293483303
not defined $rfields->[$j] or $rfield_lengths->[$j] == 0
l!l&&r!l&&!r
03303348
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
6379 && ( !defined( $rfields->[$j] )
6380 || ( $rfield_lengths->[$j] == 0 ) )
6381 );
6382  
6383 # compute spaces of padding before this field
6384 5477 my $col = $alignments[ $j - 1 ]->{'column'};
6385 5477 my $pad = $col - ( $str_len + $leading_space_count );
6386  
6387 5477 ▶ if ($do_not_align) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_not_align) 0 5477 5477
6388 0 ▶ $pad =
Branch: 1 branch
BranchTrueFalseTotal
$j < $maximum_field_index ? : 0 0 0
6389 ( $j < $maximum_field_index )
6390 ? 0
6391 : $rOpts_minimum_space_to_comment - 1;
6392 }
6393  
6394 # if the -fpsc flag is set, move the side comment to the selected
6395 # column if and only if it is possible, ignoring constraints on
6396 # line length and minimum space to comment
6397 5477 ▶ if ( $rOpts_fixed_position_side_comment
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts_fixed_position_side_comment and $j == $maximum_field_index) 9 5468 5477
Condition100%3/3
$rOpts_fixed_position_side_comment and $j == $maximum_field_index
!ll&&!rl&&r
5436329
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
6398 && $j == $maximum_field_index )
6399 {
6400 9 my $newpad = $pad + $rOpts_fixed_position_side_comment - $col - 1;
6401 9 ▶ if ( $newpad >= 0 ) { $pad = $newpad; }
Branch: 1 branch
BranchTrueFalseTotal
if ($newpad >= 0) 9 0 9
6402 }
6403  
6404 # accumulate the padding
6405 5477 ▶ if ( $pad > 0 ) { $total_pad_count += $pad; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pad > 0) 1602 3875 5477
6406  
6407 # only add padding when we have a finite field;
6408 # this avoids extra terminal spaces if we have empty fields
6409 5477 ▶ if ( $rfield_lengths->[$j] > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rfield_lengths->[$j] > 0) { } 5466 11 5477
6410 5466 $str .= SPACE x $total_pad_count;
6411 5466 $str_len += $total_pad_count;
6412 5466 $total_pad_count = 0;
6413 5466 $str .= $rfields->[$j];
6414 5466 $str_len += $rfield_lengths->[$j];
6415 }
6416 else {
6417 11 $total_pad_count = 0;
6418 }
6419 }
6420  
6421 3651 my $side_comment_length = $rfield_lengths->[$maximum_field_index];
6422  
6423 # ship this line off
6424 3651 $self->valign_output_step_B(
6425 {
6426 leading_space_count => $leading_space_count + $extra_leading_spaces,
6427 line => $str,
6428 line_length => $str_len,
6429 side_comment_length => $side_comment_length,
6430 outdent_long_lines => $outdent_long_lines,
6431 rvertical_tightness_flags => $rvertical_tightness_flags,
6432 level => $level,
6433 level_end => $level_end,
6434 Kend => $Kend,
6435 maximum_line_length => $maximum_line_length,
6436 }
6437 );
6438 3651 return;
6439 } ## end sub valign_output_step_A
6440  
6441 16 ▶sub combine_fields {
Subroutine combine_fields: called CC=5 SCAR=16.5
Pod undocumented
6442  
6443 16 my ( $line_0, $line_1, $imax_align ) = @_;
6444  
6445 # Given:
6446 # $line_0, $line_1 = two adjacent lines
6447 # $imax_align = index of last alignment wanted
6448  
6449 # Task:
6450 # We have a group of two lines for which we do not want to align tokens
6451 # between index $imax_align and the side comment. So we will delete fields
6452 # between $imax_align and the side comment. Alignments have already
6453 # been set so we have to adjust them.
6454  
6455 0 ▶ if ( !defined($imax_align) ) { $imax_align = -1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $imax_align) 0 16 16
6456  
6457 # First delete the unwanted tokens
6458 16 my $jmax_old = $line_0->{'jmax'};
6459 16 my @idel = ( $imax_align + 1 .. $jmax_old - 2 );
6460 16 ▶ return if ( !@idel );
Branch: 1 branch
BranchTrueFalseTotal
unless @idel 0 16 16
6461  
6462 # Get old alignments before any changes are made
6463 16 my @old_alignments = @{ $line_0->{'ralignments'} };
6464  
6465 16 foreach my $line ( $line_0, $line_1 ) {
6466 32 delete_selected_tokens( $line, \@idel );
6467 }
6468  
6469 # Now adjust the alignments. Note that the side comment alignment
6470 # is always at jmax-1, and there is an ending alignment at jmax.
6471 16 my @new_alignments;
6472 16 ▶ if ( $imax_align >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imax_align >= 0) 0 16 16
6473 0 @new_alignments[ 0 .. $imax_align ] =
6474 @old_alignments[ 0 .. $imax_align ];
6475 }
6476  
6477 16 my $jmax_new = $line_0->{'jmax'};
6478  
6479 16 $new_alignments[ $jmax_new - 1 ] = $old_alignments[ $jmax_old - 1 ];
6480 16 $new_alignments[$jmax_new] = $old_alignments[$jmax_old];
6481 16 $line_0->{'ralignments'} = \@new_alignments;
6482 16 $line_1->{'ralignments'} = \@new_alignments;
6483 16 return;
6484 } ## end sub combine_fields
6485  
6486 70 ▶sub get_output_line_number {
Subroutine get_output_line_number: called CC=1 SCAR=0.0
Pod undocumented
6487  
6488 # Return the output line number to external modules.
6489 # The output line number reported to a caller =
6490 # the number of items still in the buffer +
6491 # the number of items written.
6492 70 my $self = shift;
6493 70 return $self->group_line_count() +
6494 $self->[_file_writer_object_]->get_output_line_number();
6495 } ## end sub get_output_line_number
6496  
6497 ###############################
6498 # CODE SECTION 9: Output Step B
6499 ###############################
6500  
6501 { ## closure for sub valign_output_step_B
6502  
6503 # These are values for a cache used by valign_output_step_B.
6504 45 my $cached_line_text;
6505 my $cached_line_text_length;
6506 45 my $cached_line_type;
6507 45 my $cached_line_opening_flag;
6508 45 my $cached_line_closing_flag;
6509 45 my $cached_seqno;
6510 45 my $cached_line_valid;
6511 45 my $cached_line_leading_space_count;
6512 45 my $cached_seqno_string;
6513 45 my $cached_line_Kend;
6514 45 my $cached_line_maximum_length;
6515  
6516 # These are passed to step_C:
6517 45 my $seqno_string;
6518 45 my $last_nonblank_seqno_string;
6519  
6520 398 ▶ sub set_last_nonblank_seqno_string {
Subroutine set_last_nonblank_seqno_string: called CC=1 SCAR=0.0
Pod undocumented
6521 398 my ($val) = @_;
6522 398 $last_nonblank_seqno_string = $val;
6523 398 return;
6524 }
6525  
6526 227 ▶ sub get_cached_line_opening_flag {
Subroutine get_cached_line_opening_flag: called CC=1 SCAR=0.0
Pod undocumented
6527 227 return $cached_line_opening_flag;
6528 }
6529  
6530 8672 ▶ sub get_cached_line_type {
Subroutine get_cached_line_type: called CC=1 SCAR=0.0
Pod undocumented
6531 8672 return $cached_line_type;
6532 }
6533  
6534 3 ▶ sub set_cached_line_valid {
Subroutine set_cached_line_valid: called CC=1 SCAR=0.0
Pod undocumented
6535 3 my ($val) = @_;
6536 3 $cached_line_valid = $val;
6537 3 return;
6538 }
6539  
6540 157 ▶ sub get_cached_seqno {
Subroutine get_cached_seqno: called CC=1 SCAR=0.0
Pod undocumented
6541 157 return $cached_seqno;
6542 }
6543  
6544 665 ▶ sub initialize_step_B_cache {
Subroutine initialize_step_B_cache: called CC=1 SCAR=0.0
Pod undocumented
6545  
6546 # valign_output_step_B cache:
6547 665 $cached_line_text = EMPTY_STRING;
6548 665 $cached_line_text_length = 0;
6549 665 $cached_line_type = 0;
6550 665 $cached_line_opening_flag = 0;
6551 665 $cached_line_closing_flag = 0;
6552 665 $cached_seqno = 0;
6553 665 $cached_line_valid = 0;
6554 665 $cached_line_leading_space_count = 0;
6555 665 $cached_seqno_string = EMPTY_STRING;
6556 665 $cached_line_Kend = undef;
6557 665 $cached_line_maximum_length = undef;
6558  
6559 # These vars hold a string of sequence numbers joined together used by
6560 # the cache
6561 665 $seqno_string = EMPTY_STRING;
6562 665 $last_nonblank_seqno_string = EMPTY_STRING;
6563 665 return;
6564 } ## end sub initialize_step_B_cache
6565  
6566 2359 ▶ sub _flush_step_B_cache {
Subroutine _flush_step_B_cache: called CC=2 SCAR=6.9
6567  
6568 2359 my ($self) = @_;
6569  
6570 # Send any text in the step_B cache on to step_C
6571 2359 ▶ if ($cached_line_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_line_type) 1 2358 2359
6572 1 $seqno_string = $cached_seqno_string;
6573 1 $self->valign_output_step_C(
6574 $seqno_string,
6575 $last_nonblank_seqno_string,
6576  
6577 $cached_line_text,
6578 $cached_line_leading_space_count,
6579 $self->[_last_level_written_],
6580 $cached_line_Kend,
6581 );
6582 1 $cached_line_type = 0;
6583 1 $cached_line_text = EMPTY_STRING;
6584 1 $cached_line_text_length = 0;
6585 1 $cached_seqno_string = EMPTY_STRING;
6586 1 $cached_line_Kend = undef;
6587 1 $cached_line_maximum_length = undef;
6588 }
6589 2359 return;
6590 } ## end sub _flush_step_B_cache
6591  
6592 162 ▶ sub handle_cached_line {
Subroutine handle_cached_line: called CC=33 SCAR=38.6
Pod undocumented
6593  
6594 162 my ( $self, $rinput, $leading_string, $leading_string_length ) = @_;
6595  
6596 # handle a cached line ..
6597 # either append the current line to it or write it out
6598  
6599 # The cached line will either be:
6600 # - passed along to step_C, or
6601 # - or combined with the current line
6602  
6603 162 my $last_level_written = $self->[_last_level_written_];
6604  
6605 162 my $leading_space_count = $rinput->{leading_space_count};
6606 162 my $str = $rinput->{line};
6607 162 my $str_length = $rinput->{line_length};
6608 162 my $rvertical_tightness_flags = $rinput->{rvertical_tightness_flags};
6609 162 my $level = $rinput->{level};
6610 162 my $level_end = $rinput->{level_end};
6611 162 my $maximum_line_length = $rinput->{maximum_line_length};
6612  
6613 162 my ( $open_or_close, $seqno_beg );
6614 162 ▶ if ($rvertical_tightness_flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rvertical_tightness_flags) 162 0 162
6615  
6616 162 $open_or_close = $rvertical_tightness_flags->{_vt_type};
6617 162 $seqno_beg = $rvertical_tightness_flags->{_vt_seqno_beg};
6618 }
6619  
6620 # Dump an invalid cached line
6621 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
6622 93 $self->valign_output_step_C(
6623 $seqno_string,
6624 $last_nonblank_seqno_string,
6625  
6626 $cached_line_text,
6627 $cached_line_leading_space_count,
6628 $last_level_written,
6629 $cached_line_Kend,
6630 );
6631 }
6632  
6633 # Handle cached line ending in OPENING tokens
6634 ▶ 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
6635  
6636 32 my $gap = $leading_space_count - $cached_line_text_length;
6637  
6638 # handle option of just one tight opening per line:
6639 32 ▶ if ( $cached_line_opening_flag == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_line_opening_flag == 1) 14 18 32
6640 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
6641 0 $gap = -1;
6642 }
6643 }
6644  
6645 # Do not join the lines if this might produce a one-line
6646 # container which exceeds the maximum line length. This is
6647 # necessary prevent blinking, particularly with the combination
6648 # -xci -pvt=2. In that case a one-line block alternately forms
6649 # and breaks, causing -xci to alternately turn on and off (case
6650 # b765).
6651 # Patched to fix cases b656 b862 b971 b972: always do the check
6652 # if the maximum line length changes (due to -vmll).
6653 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
6654 $gap >= 0
6655 && ( $maximum_line_length != $cached_line_maximum_length
6656 || ( defined($level_end) && $level > $level_end ) )
6657 )
6658 {
6659 0 my $test_line_length =
6660 $cached_line_text_length + $gap + $str_length;
6661  
6662 # Add a small tolerance in the length test (fixes case b862)
6663 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
6664 0 $gap = -1;
6665 }
6666 }
6667  
6668 # NOTE: using defined() since $seqno_beg can be 0 for -bbvt
6669 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
6670 20 $maximum_line_length = $cached_line_maximum_length;
6671 20 $leading_string = $cached_line_text . SPACE x $gap;
6672 20 $leading_string_length = $cached_line_text_length + $gap;
6673 20 $leading_space_count = $cached_line_leading_space_count;
6674 20 $seqno_string = $cached_seqno_string . ':' . $seqno_beg;
6675 20 $level = $last_level_written;
6676 }
6677 else {
6678 12 $self->valign_output_step_C(
6679 $seqno_string,
6680 $last_nonblank_seqno_string,
6681  
6682 $cached_line_text,
6683 $cached_line_leading_space_count,
6684 $last_level_written,
6685 $cached_line_Kend,
6686 );
6687 }
6688 }
6689  
6690 # Handle cached line ending in CLOSING tokens
6691 else {
6692 37 my $test_line =
6693 $cached_line_text . SPACE x $cached_line_closing_flag . $str;
6694 37 my $test_line_length =
6695 $cached_line_text_length +
6696 $cached_line_closing_flag +
6697 $str_length;
6698 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
6699  
6700 # The new line must start with container
6701 $seqno_beg
6702  
6703 # The container combination must be okay..
6704 && (
6705  
6706 # okay to combine like types
6707 ( $open_or_close == $cached_line_type )
6708  
6709 # closing block brace may append to non-block
6710 || ( $cached_line_type == 2 && $open_or_close == 4 )
6711  
6712 # something like ');'
6713 || ( !$open_or_close && $cached_line_type == 2 )
6714  
6715 )
6716  
6717 # The combined line must fit
6718 && ( $test_line_length <= $cached_line_maximum_length )
6719 )
6720 {
6721  
6722 33 $seqno_string = $cached_seqno_string . ':' . $seqno_beg;
6723  
6724 # Patch to outdent closing tokens ending # in ');' If we
6725 # are joining a line like ');' to a previous stacked set of
6726 # closing tokens, then decide if we may outdent the
6727 # combined stack to the indentation of the ');'. Since we
6728 # should not normally outdent any of the other tokens more
6729 # than the indentation of the lines that contained them, we
6730 # will only do this if all of the corresponding opening
6731 # tokens were on the same line. This can happen with -sot
6732 # and -sct.
6733  
6734 # For example, it is ok here:
6735 # __PACKAGE__->load_components( qw(
6736 # PK::Auto
6737 # Core
6738 # ));
6739 #
6740 # But, for example, we do not outdent in this example
6741 # because that would put the closing sub brace out farther
6742 # than the opening sub brace:
6743 #
6744 # perltidy -sot -sct
6745 # $c->Tk::bind(
6746 # '<Control-f>' => sub {
6747 # my ($c) = @_;
6748 # my $e = $c->XEvent;
6749 # itemsUnderArea $c;
6750 # } );
6751 #
6752 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
6753 && $cached_line_text =~ /^[\)\}\]\s]*$/ )
6754 {
6755  
6756 # The way to tell this is if the stacked sequence
6757 # numbers of this output line are the reverse of the
6758 # stacked sequence numbers of the previous non-blank
6759 # line of sequence numbers. So we can join if the
6760 # previous nonblank string of tokens is the mirror
6761 # image. For example if stack )}] is 13:8:6 then we
6762 # are looking for a leading stack like [{( which
6763 # is 6:8:13. We only need to check the two ends,
6764 # because the intermediate tokens must fall in order.
6765 # Note on speed: having to split on colons and
6766 # eliminate multiple colons might appear to be slow,
6767 # but it's not an issue because we almost never come
6768 # through here. In a typical file we don't.
6769  
6770 4 $seqno_string =~ s/^:+//;
6771 4 $last_nonblank_seqno_string =~ s/^:+//;
6772 4 $seqno_string =~ s/:+/:/g;
6773 4 $last_nonblank_seqno_string =~ s/:+/:/g;
6774  
6775 # how many spaces can we outdent?
6776 4 my $diff =
6777 $cached_line_leading_space_count - $leading_space_count;
6778 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
6779 && length($seqno_string)
6780 && length($last_nonblank_seqno_string) ==
6781 length($seqno_string) )
6782 {
6783 3 my @seqno_last =
6784 ( split /:/, $last_nonblank_seqno_string );
6785 3 my @seqno_now = ( split /:/, $seqno_string );
6786 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
6787 && @seqno_last
6788 && $seqno_now[-1] == $seqno_last[0]
6789 && $seqno_now[0] == $seqno_last[-1] )
6790 {
6791  
6792 # OK to outdent ..
6793 # for absolute safety, be sure we only remove
6794 # whitespace
6795 3 my $ws = substr( $test_line, 0, $diff );
6796 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
6797 && $ws =~ /^\s+$/ )
6798 {
6799  
6800 3 $test_line = substr( $test_line, $diff );
6801 3 $cached_line_leading_space_count -= $diff;
6802 3 $last_level_written =
6803 $self->level_change(
6804 $cached_line_leading_space_count,
6805 $diff, $last_level_written );
6806 3 $self->reduce_valign_buffer_indentation($diff);
6807 }
6808  
6809 # shouldn't happen, but not critical:
6810 ##else {
6811 ## ERROR transferring indentation here
6812 ##}
6813 }
6814 }
6815 }
6816  
6817 # Change the args to look like we received the combined line
6818 33 $str = $test_line;
6819 33 $str_length = $test_line_length;
6820 33 $leading_string = EMPTY_STRING;
6821 33 $leading_string_length = 0;
6822 33 $leading_space_count = $cached_line_leading_space_count;
6823 33 $level = $last_level_written;
6824 33 $maximum_line_length = $cached_line_maximum_length;
6825 }
6826 else {
6827 4 $self->valign_output_step_C(
6828 $seqno_string,
6829 $last_nonblank_seqno_string,
6830  
6831 $cached_line_text,
6832 $cached_line_leading_space_count,
6833 $last_level_written,
6834 $cached_line_Kend,
6835 );
6836 }
6837 }
6838 162 return ( $str, $str_length, $leading_string, $leading_string_length,
6839 $leading_space_count, $level, $maximum_line_length );
6840  
6841 } ## end sub handle_cached_line
6842  
6843 8561 ▶ sub valign_output_step_B {
Subroutine valign_output_step_B: called CC=17 SCAR=28.5
Pod undocumented
6844  
6845 8561 my ( $self, $rinput ) = @_;
6846  
6847 #---------------------------------------------------------
6848 # This is Step B in writing vertically aligned lines.
6849 # Vertical tightness is applied according to preset flags.
6850 # In particular this routine handles stacking of opening
6851 # and closing tokens.
6852 #---------------------------------------------------------
6853  
6854 ##Note: key 'level_end' not needed
6855 my (
6856  
6857 $leading_space_count,
6858 $str,
6859 $str_length,
6860 $side_comment_length,
6861 $outdent_long_lines,
6862 $rvertical_tightness_flags,
6863 $level,
6864 $Kend,
6865 $maximum_line_length,
6866  
6867 ) =
6868  
6869 8561 @{$rinput}{
6870 8561 qw(
6871 leading_space_count
6872 line
6873 line_length
6874 side_comment_length
6875 outdent_long_lines
6876 rvertical_tightness_flags
6877 level
6878 Kend
6879 maximum_line_length
6880 )
6881 };
6882  
6883 # Useful -gcs test cases for wide characters are
6884 # perl527/(method.t.2, reg_mesg.t, mime-header.t)
6885  
6886 # handle outdenting of long lines:
6887 8561 my $is_outdented_line;
6888 8561 ▶ if ($outdent_long_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outdent_long_lines) 372 8189 8561
6889 372 my $excess =
6890 $str_length -
6891 $side_comment_length +
6892 $leading_space_count -
6893 $maximum_line_length;
6894 372 ▶ if ( $excess > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($excess > 0) 11 361 372
6895 11 $leading_space_count = 0;
6896 11 my $file_writer_object = $self->[_file_writer_object_];
6897 11 my $last_outdented_line_at =
6898 $file_writer_object->get_output_line_number();
6899 11 $self->[_last_outdented_line_at_] = $last_outdented_line_at;
6900  
6901 11 my $outdented_line_count = $self->[_outdented_line_count_];
6902 11 ▶ if ( !$outdented_line_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($outdented_line_count) 4 7 11
6903 4 $self->[_first_outdented_line_at_] =
6904 $last_outdented_line_at;
6905 }
6906 11 $outdented_line_count++;
6907 11 $self->[_outdented_line_count_] = $outdented_line_count;
6908 11 $is_outdented_line = 1;
6909 }
6910 }
6911  
6912 # Make preliminary leading whitespace. It could get changed
6913 # later by entabbing, so we have to keep track of any changes
6914 # to the leading_space_count from here on.
6915 8561 ▶ my $leading_string =
Branch: 1 branch
BranchTrueFalseTotal
$leading_space_count > 0 ? : 4992 3569 8561
6916 $leading_space_count > 0
6917 ? ( SPACE x $leading_space_count )
6918 : EMPTY_STRING;
6919 8561 my $leading_string_length = length($leading_string);
6920  
6921 # Unpack any recombination data; it was packed by
6922 # sub 'Formatter::set_vertical_tightness_flags'
6923  
6924 # old hash Meaning
6925 # index key
6926 #
6927 # 0 _vt_type: 1=opening non-block 2=closing non-block
6928 # 3=opening block brace 4=closing block brace
6929 #
6930 # 1a _vt_opening_flag: 1=no multiple steps, 2=multiple steps ok
6931 # 1b _vt_closing_flag: spaces of padding to use if closing
6932 # 2 _vt_seqno: sequence number of container
6933 # 3 _vt_valid flag: do not append if this flag is false. Will be
6934 # true if appropriate -vt flag is set. Otherwise, Will be
6935 # made true only for 2 line container in parens with -lp
6936 # 4 _vt_seqno_beg: sequence number of first token of line
6937 # 5 _vt_seqno_end: sequence number of last token of line
6938 # 6 _vt_min_lines: min number of lines for joining opening cache,
6939 # 0=no constraint
6940 # 7 _vt_max_lines: max number of lines for joining opening cache,
6941 # 0=no constraint
6942  
6943 8561 my ( $open_or_close, $opening_flag, $closing_flag, $seqno, $valid,
6944 $seqno_beg, $seqno_end );
6945 8561 ▶ if ($rvertical_tightness_flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rvertical_tightness_flags) 796 7765 8561
6946  
6947 796 $open_or_close = $rvertical_tightness_flags->{_vt_type};
6948 796 $opening_flag = $rvertical_tightness_flags->{_vt_opening_flag};
6949 796 $closing_flag = $rvertical_tightness_flags->{_vt_closing_flag};
6950 796 $seqno = $rvertical_tightness_flags->{_vt_seqno};
6951 796 $valid = $rvertical_tightness_flags->{_vt_valid_flag};
6952 796 $seqno_beg = $rvertical_tightness_flags->{_vt_seqno_beg};
6953 796 $seqno_end = $rvertical_tightness_flags->{_vt_seqno_end};
6954 }
6955  
6956 8561 ▶ $seqno_string = defined($seqno_end) ? $seqno_end : EMPTY_STRING;
Branch: 1 branch
BranchTrueFalseTotal
defined $seqno_end ? : 796 7765 8561
6957  
6958 # handle any cached line ..
6959 # either append this line to it or write it out
6960 # Note: the function length() is used in this next test out of caution.
6961 # All testing has shown that the variable $cached_line_text_length is
6962 # correct, but its calculation is complex and a loss of cached text
6963 # would be a disaster.
6964 8561 ▶ if ( length($cached_line_text) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $cached_line_text) 162 8399 8561
6965  
6966 (
6967 162 $str,
6968 $str_length,
6969 $leading_string,
6970 $leading_string_length,
6971 $leading_space_count,
6972 $level,
6973 $maximum_line_length
6974  
6975 ) = $self->handle_cached_line( $rinput, $leading_string,
6976 $leading_string_length );
6977  
6978 162 $cached_line_type = 0;
6979 162 $cached_line_text = EMPTY_STRING;
6980 162 $cached_line_text_length = 0;
6981 162 $cached_line_Kend = undef;
6982 162 $cached_line_maximum_length = undef;
6983  
6984 }
6985  
6986 # make the line to be written
6987 8561 my $line = $leading_string . $str;
6988 8561 my $line_length = $leading_string_length + $str_length;
6989  
6990 # Safety check: be sure that a line to be cached as a stacked block
6991 # brace line ends in the appropriate opening or closing block brace.
6992 # This should always be the case if the caller set flags correctly.
6993 # Code '3' is for -sobb, code '4' is for -scbb.
6994 8561 ▶ if ($open_or_close) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open_or_close) 163 8398 8561
6995 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 table42%3/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
6996 || $open_or_close == 4 && $line !~ /\}\s*$/ )
6997 {
6998 0 $open_or_close = 0;
6999 }
7000 }
7001  
7002 # write or cache this line ...
7003 # fix for case b999: do not cache an outdented line
7004 # fix for b1378: do not cache an empty line
7005 8561 ▶ 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) { } 8398 163 8561
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
83980163
not $open_or_close or $side_comment_length > 0 or $is_outdented_line
l!l&&r!l&&!r
83980163
not $open_or_close or $side_comment_length > 0
l!l&&r!l&&!r
83980163
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
7006 || $side_comment_length > 0
7007 || $is_outdented_line
7008 || !$line_length )
7009 {
7010 8398 $self->valign_output_step_C(
7011 $seqno_string,
7012 $last_nonblank_seqno_string,
7013  
7014 $line,
7015 $leading_space_count,
7016 $level,
7017 $Kend,
7018 );
7019 }
7020 else {
7021 163 $cached_line_text = $line;
7022 163 $cached_line_text_length = $line_length;
7023 163 $cached_line_type = $open_or_close;
7024 163 $cached_line_opening_flag = $opening_flag;
7025 163 $cached_line_closing_flag = $closing_flag;
7026 163 $cached_seqno = $seqno;
7027 163 $cached_line_valid = $valid;
7028 163 $cached_line_leading_space_count = $leading_space_count;
7029 163 $cached_seqno_string = $seqno_string;
7030 163 $cached_line_Kend = $Kend;
7031 163 $cached_line_maximum_length = $maximum_line_length;
7032 }
7033  
7034 8561 $self->[_last_level_written_] = $level;
7035 8561 $self->[_last_side_comment_length_] = $side_comment_length;
7036 8561 return;
7037 } ## end sub valign_output_step_B
7038 }
7039  
7040 ################################
7041 # CODE SECTION 10: Output Step C
7042 ################################
7043  
7044 { ## closure for sub valign_output_step_C
7045  
7046 # Vertical alignment buffer used by valign_output_step_C
7047 45 my $valign_buffer_filling;
7048 45 my @valign_buffer;
7049  
7050 665 ▶ sub initialize_valign_buffer {
Subroutine initialize_valign_buffer: called CC=1 SCAR=0.0
Pod undocumented
7051 665 @valign_buffer = ();
7052 665 $valign_buffer_filling = EMPTY_STRING;
7053 665 return;
7054 }
7055  
7056 2361 ▶ sub dump_valign_buffer {
Subroutine dump_valign_buffer: called CC=3 SCAR=11.0
Pod undocumented
7057  
7058 2361 my ($self) = @_;
7059  
7060 # Send all lines in the current buffer on to step_D
7061 2361 ▶ if (@valign_buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (@valign_buffer) 2 2359 2361
7062 2 foreach (@valign_buffer) {
7063 7 $self->valign_output_step_D( @{$_} );
7064 }
7065 2 @valign_buffer = ();
7066 }
7067 2361 $valign_buffer_filling = EMPTY_STRING;
7068 2361 return;
7069 } ## end sub dump_valign_buffer
7070  
7071 3 ▶ sub reduce_valign_buffer_indentation {
Subroutine reduce_valign_buffer_indentation: called CC=7 SCAR=19.9
Pod undocumented
7072  
7073 3 my ( $self, $diff ) = @_;
7074  
7075 # Reduce the leading indentation of lines in the current
7076 # buffer by $diff spaces
7077 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
7078 2 my $max_valign_buffer = @valign_buffer;
7079 2 foreach my $i ( 0 .. $max_valign_buffer - 1 ) {
7080 my ( $line, $leading_space_count, $level, $Kend ) =
7081 7 @{ $valign_buffer[$i] };
7082 7 my $ws = substr( $line, 0, $diff );
7083 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
7084 7 $line = substr( $line, $diff );
7085 }
7086 7 ▶ if ( $leading_space_count >= $diff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count >= $diff) 7 0 7
7087 7 $leading_space_count -= $diff;
7088 7 $level =
7089 $self->level_change( $leading_space_count, $diff,
7090 $level );
7091 }
7092 7 $valign_buffer[$i] =
7093 [ $line, $leading_space_count, $level, $Kend ];
7094 }
7095 }
7096 3 return;
7097 } ## end sub reduce_valign_buffer_indentation
7098  
7099 8508 ▶ sub valign_output_step_C {
Subroutine valign_output_step_C: called CC=8 SCAR=20.8
Pod undocumented
7100  
7101 my (
7102 8508 $self,
7103 $seqno_string,
7104 $last_nonblank_seqno_string,
7105  
7106 @args_to_D,
7107 ) = @_;
7108  
7109 #-----------------------------------------------------------------------
7110 # This is Step C in writing vertically aligned lines.
7111 # Lines are either stored in a buffer or passed along to the next step.
7112 # The reason for storing lines is that we may later want to reduce their
7113 # indentation when -sot and -sct are both used.
7114 #-----------------------------------------------------------------------
7115  
7116 # Dump any saved lines if we see a line with an unbalanced opening or
7117 # closing token.
7118 8508 ▶ $self->dump_valign_buffer()
Branch: 1 branch
BranchTrueFalseTotal
if $seqno_string and $valign_buffer_filling 2 8506 8508
Condition100%3/3
$seqno_string and $valign_buffer_filling
!ll&&!rl&&r
81103962
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
7119 if ( $seqno_string && $valign_buffer_filling );
7120  
7121 # Either store or write this line
7122 8508 ▶ if ($valign_buffer_filling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valign_buffer_filling) { } 7 8501 8508
7123 7 push @valign_buffer, [@args_to_D];
7124 }
7125 else {
7126 8501 $self->valign_output_step_D(@args_to_D);
7127 }
7128  
7129 # For lines starting or ending with opening or closing tokens..
7130 8508 ▶ if ($seqno_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqno_string) 398 8110 8508
7131 398 $last_nonblank_seqno_string = $seqno_string;
7132 398 set_last_nonblank_seqno_string($seqno_string);
7133  
7134 # Start storing lines when we see a line with multiple stacked
7135 # opening tokens.
7136 # patch for RT #94354, requested by Colin Williams
7137 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
7138 && $seqno_string =~ /^\d+(\:+\d+)+$/
7139 && $args_to_D[0] !~ /^[\}\)\]\:\?]/ )
7140 {
7141  
7142 # This test is efficient but a little subtle: The first test
7143 # says that we have multiple sequence numbers and hence
7144 # multiple opening or closing tokens in this line. The second
7145 # part of the test rejects stacked closing and ternary tokens.
7146 # So if we get here then we should have stacked unbalanced
7147 # opening tokens.
7148  
7149 # Here is a complex example:
7150  
7151 # Foo($Bar[0], { # (side comment)
7152 # baz => 1,
7153 # });
7154  
7155 # The first line has sequence 6::4. It does not begin with
7156 # a closing token or ternary, so it passes the test and must be
7157 # stacked opening tokens.
7158  
7159 # The last line has sequence 4:6 but is a stack of closing
7160 # tokens, so it gets rejected.
7161  
7162 # Note that the sequence number of an opening token for a qw
7163 # quote is a negative number and will be rejected. For
7164 # example, for the following line: skip_symbols([qw(
7165 # $seqno_string='10:5:-1'. It would be okay to accept it but I
7166 # decided not to do this after testing.
7167  
7168 8 $valign_buffer_filling = $seqno_string;
7169  
7170 }
7171 }
7172 8508 return;
7173 } ## end sub valign_output_step_C
7174 }
7175  
7176 ###############################
7177 # CODE SECTION 11: Output Step D
7178 ###############################
7179  
7180 45 ▶sub add_leading_tabs {
Subroutine add_leading_tabs: called CC=9 SCAR=36.4
Pod undocumented
7181  
7182 45 my ( $self, $line, $leading_space_count, $level ) = @_;
7183  
7184 # Convert leading whitespace to use tabs if -et or -t are set
7185  
7186 # Given:
7187 # $line = the line of text to be written, without any tabs
7188 # $leading_whitespace = expected number of leading blank spaces
7189 # $level = indentation level (needed for -t)
7190  
7191 # Return:
7192 # $line = the line with possible leading tabs
7193  
7194 45 my $trimmed_line = $line;
7195 45 $trimmed_line =~ s/^ [^\S\n]+ //gxm;
7196  
7197 # Check for discrepancy in actual leading white spaces with estimate
7198 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
7199  
7200 # If $leading_space_count is zero, then this routine must not
7201 # be called because we might be in a quote of some kind
7202 0 ▶ if ( $leading_space_count <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count <= 0) 0 0 0
7203 0 DEVEL_MODE && Fault(<<EOM);
7204 should not be here with leading space count = $leading_space_count
7205 EOM
7206 0 return $line;
7207 }
7208  
7209 0 my $leading_space_count_test = length($line) - length($trimmed_line);
7210  
7211 # Skip tabbing if actual whitespace is less than expected
7212 0 ▶ if ( $leading_space_count_test < $leading_space_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_count_test < $leading_space_count) 0 0 0
7213 0 DEBUG_TABS
7214 && $self->warning(<<EOM);
7215 Error entabbing: expected count=$leading_space_count but only found $leading_space_count_test for line:
7216 '$line'
7217 EOM
7218 0 return $line;
7219 }
7220  
7221 # Use actual whitespace if it exceeds prediction. This mainly
7222 # occurs at hanging side comments.
7223 0 $leading_space_count = $leading_space_count_test;
7224 }
7225  
7226 #----------------------------------
7227 # Handle --entab-leading-whitespace
7228 #----------------------------------
7229 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
7230  
7231 45 my $space_count =
7232 $leading_space_count % $rOpts_entab_leading_whitespace;
7233 45 my $tab_count =
7234 int( $leading_space_count / $rOpts_entab_leading_whitespace );
7235 45 my $leading_string = "\t" x $tab_count . SPACE x $space_count;
7236 45 $line = $leading_string . $trimmed_line;
7237 }
7238  
7239 #-----------------------------------------------
7240 # Handle -t (one tab per level; not recommended)
7241 #-----------------------------------------------
7242 ▶ 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
7243  
7244 0 my $leading_string = ( "\t" x $level );
7245 0 my $space_count = $leading_space_count - $level * $rOpts_indent_columns;
7246  
7247 # shouldn't happen:
7248 0 ▶ if ( $space_count < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space_count < 0) { } 0 0 0
7249  
7250 # But it could be an outdented comment
7251 0 ▶ if ( $line !~ /^\s*#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*#/) 0 0 0
7252 0 DEBUG_TABS
7253 && $self->warning(
7254 "Error entabbing in valign_output_step_D: for level=$level count=$leading_space_count\n"
7255 );
7256 }
7257 0 $leading_string = ( SPACE x $leading_space_count );
7258 }
7259 else {
7260 0 $leading_string .= ( SPACE x $space_count );
7261 }
7262 0 $line = $leading_string . $trimmed_line;
7263 }
7264  
7265 # nothing to do; we should have skipped a call to this sub
7266 else {
7267 0 if (DEVEL_MODE) {
7268 Fault(
7269 "in tab sub but neither -t nor -et set: check flag 'require_tabs'\n"
7270 );
7271 }
7272 }
7273 45 return $line;
7274 } ## end sub add_leading_tabs
7275  
7276 8508 ▶sub valign_output_step_D {
Subroutine valign_output_step_D: called CC=3 SCAR=11.0
Pod undocumented
7277  
7278 8508 my ( $self, $line, $leading_space_count, $level, $Kend ) = @_;
7279  
7280 #----------------------------------------------------------------
7281 # This is Step D in writing vertically aligned lines.
7282 # It is the end of the vertical alignment pipeline.
7283 # Write one vertically aligned line of code to the output object.
7284 #----------------------------------------------------------------
7285  
7286 # Convert leading whitespace to use tabs if requested.
7287 8508 ▶ if ( $require_tabs && $leading_space_count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require_tabs and $leading_space_count > 0) 45 8463 8508
Condition100%3/3
$require_tabs and $leading_space_count > 0
!ll&&!rl&&r
8458545
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
7288 45 $line = $self->add_leading_tabs( $line, $leading_space_count, $level );
7289 }
7290  
7291 8508 my $file_writer_object = $self->[_file_writer_object_];
7292 8508 $file_writer_object->write_code_line( $line . "\n", $Kend );
7293  
7294 8508 return;
7295 } ## end sub valign_output_step_D
7296  
7297 ##########################
7298 # CODE SECTION 12: Summary
7299 ##########################
7300  
7301 665 ▶sub report_anything_unusual {
Subroutine report_anything_unusual: called CC=4 SCAR=13.9
Pod undocumented
7302 665 my $self = shift;
7303 665 my $logger_object = $self->[_logger_object_];
7304 665 ▶ return if ( !$logger_object );
Branch: 1 branch
BranchTrueFalseTotal
unless $logger_object 2 663 665
7305  
7306 663 my $outdented_line_count = $self->[_outdented_line_count_];
7307 663 ▶ if ( $outdented_line_count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outdented_line_count > 0) 23 640 663
7308 23 $logger_object->write_logfile_entry(
7309 "$outdented_line_count long lines were outdented:\n");
7310 23 my $first_outdented_line_at = $self->[_first_outdented_line_at_];
7311 23 $logger_object->write_logfile_entry(
7312 " First at output line $first_outdented_line_at\n");
7313  
7314 23 ▶ if ( $outdented_line_count > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outdented_line_count > 1) 7 16 23
7315 7 my $last_outdented_line_at = $self->[_last_outdented_line_at_];
7316 7 $logger_object->write_logfile_entry(
7317 " Last at output line $last_outdented_line_at\n");
7318 }
7319 $logger_object->write_logfile_entry(
7320 23 " use -noll to prevent outdenting, -l=n to increase line length\n"
7321 );
7322 23 $logger_object->write_logfile_entry("\n");
7323 }
7324 663 return;
7325 } ## end sub report_anything_unusual
7326  
7327 } ## end package Perl::Tidy::VerticalAligner
7328 45 1;
« Tidy/Logger.pm ↑ Summary Tidy/Diagnostics.pm »