From: Steve Hancock Date: Fri, 21 Jul 2023 16:55:03 +0000 (-0700) Subject: simplify some i/o logic; eliminate one call to LineSink X-Git-Tag: 20230701.02~11 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=0aaf24dcac31a79bde7308b294baf1f15cd29a4c;p=perltidy.git simplify some i/o logic; eliminate one call to LineSink --- diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index de21c88d..a3286648 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -2184,6 +2184,7 @@ sub process_filter_layer { Warn(<new( - output_file => $output_file, - line_separator => $self->[_line_separator_], - is_encoded_data => $is_encoded_data, - ); - #---------------------------------------------------------------------- # Apply any postfilter. The postfilter is a code reference that will be # applied to the source after tidying. @@ -2375,31 +2366,44 @@ EOM } } - my @buf_post_lines = + # Handle --line-range-tidy line recombination + my @output_lines = ( @buf_lines_pre, ( split /^/, $buf_post ), @buf_lines_post ); - # Copy the filtered buffer to the final destination - if ( !$remove_terminal_newline ) { - foreach my $line (@buf_post_lines) { - $sink_object_post->write_line($line); + # Handle --preserve-line-endings or -output-line-endings flags. + # The native line separator has been used in all intermediate + # iterations and filter operations until here so that string + # operations work ok. + if ($change_line_separator) { + my $line_separator = $self->[_line_separator_]; + foreach my $line (@output_lines) { + chomp $line; + $line .= $line_separator; } } - else { - # Copy the filtered buffer but remove the newline char from the - # final line - my $line; - foreach my $next_line (@buf_post_lines) { - $sink_object_post->write_line($line) if ($line); - $line = $next_line; - } - if ($line) { - $sink_object_post->set_line_separator(undef); - chomp $line; - $sink_object_post->write_line($line); - } + # Handle a '--noadd-terminal-newline' flag + if ($remove_terminal_newline) { + chomp $output_lines[-1]; + } + + #----------------------------------------- + # Copy the filtered buffer to $output_file + #----------------------------------------- + + # Most named files are written here. For output to non-files, and + # files in -b mode, this may not be the ultimate destination. + my ( $fh, $fh_name ) = + Perl::Tidy::streamhandle( $output_file, 'w', $is_encoded_data ); + unless ($fh) { Die("Cannot write to output stream\n"); } + + foreach my $line (@output_lines) { + $fh->print($line); + } + + if ( $output_file ne '-' && !ref $output_file ) { + $fh->close(); } - $sink_object_post->close_output_file(); } #--------------------------------------------------------