From 3f9919f0d3c1aa5894f2b704d63e308acb4c4433 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 8 Aug 2023 18:56:44 -0700 Subject: [PATCH] simplify some IO operations --- lib/Perl/Tidy/FileWriter.pm | 60 ++++++++++++++++++------------------- lib/Perl/Tidy/Formatter.pm | 2 +- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/lib/Perl/Tidy/FileWriter.pm b/lib/Perl/Tidy/FileWriter.pm index ab1cc063..d27ef98d 100644 --- a/lib/Perl/Tidy/FileWriter.pm +++ b/lib/Perl/Tidy/FileWriter.pm @@ -49,7 +49,6 @@ BEGIN { # Do not combine with other BEGIN blocks (c101). my $i = 0; use constant { - _write_line_ => $i++, _logger_object_ => $i++, _rOpts_ => $i++, _output_line_number_ => $i++, @@ -70,6 +69,7 @@ BEGIN { _K_sequence_error_msg_ => $i++, _K_last_arrival_ => $i++, _save_logfile_ => $i++, + _routput_string_ => $i++, }; } ## end BEGIN @@ -147,27 +147,21 @@ sub new { $self->[_K_sequence_error_msg_] = EMPTY_STRING; $self->[_K_last_arrival_] = -1; $self->[_save_logfile_] = defined($logger_object); + $self->[_routput_string_] = undef; - # parameter '$line_sink_object' tells where to store the line, as follows: + # '$line_sink_object' is a SCALAR ref which receives the lines. my $ref = ref($line_sink_object); if ( !$ref ) { Fault("FileWriter expects line_sink_object to be a ref\n"); } elsif ( $ref eq 'SCALAR' ) { - $self->[_write_line_] = sub { ${$line_sink_object} .= $_[0] }; - } - elsif ( $ref eq 'ARRAY' ) { - $self->[_write_line_] = sub { push @{$line_sink_object}, $_[0] }; - } - elsif ( $ref->can('write_line') ) { - $self->[_write_line_] = sub { $line_sink_object->write_line( $_[0] ) }; + $self->[_routput_string_] = $line_sink_object; } else { my $str = $ref; if ( length($str) > 63 ) { $str = substr( $str, 0, 60 ) . '...' } Fault(<[_write_line_]->("\n"); - $self->[_output_line_number_]++; + ${ $self->[_routput_string_] } .= "\n"; + $self->[_output_line_number_]++; $self->[_consecutive_blank_lines_]++; $self->[_consecutive_new_blank_lines_]++ if ($forced); @@ -307,9 +301,10 @@ sub write_code_line { $self->[_consecutive_blank_lines_] = 0; $self->[_consecutive_new_blank_lines_] = 0; $self->[_consecutive_nonblank_lines_]++; + $self->[_output_line_number_]++; + + ${ $self->[_routput_string_] } .= $str; - $self->[_write_line_]->($str); - if ( chomp $str ) { $self->[_output_line_number_]++; } if ( $self->[_save_logfile_] ) { $self->check_line_lengths($str) } #---------------------------- @@ -334,15 +329,17 @@ sub write_code_line { # caused lines to go out in the wrong order. This could happen if # either the cache or buffer that it uses are emptied in the wrong # order. - if ( !$self->[_K_sequence_error_msg_] ) { + if ( $K < $self->[_K_last_arrival_] + && !$self->[_K_sequence_error_msg_] ) + { my $K_prev = $self->[_K_last_arrival_]; - if ( $K < $K_prev ) { - chomp $str; - if ( length($str) > MAX_PRINTED_CHARS ) { - $str = substr( $str, 0, MAX_PRINTED_CHARS ) . "..."; - } - my $msg = < MAX_PRINTED_CHARS ) { + $str = substr( $str, 0, MAX_PRINTED_CHARS ) . "..."; + } + + my $msg = <warning($msg) if ( length($str) ); + # Otherwise warn if string is not empty (added for b1378) + $self->warning($msg) if ( length($str) ); - # Only issue this warning once - $self->[_K_sequence_error_msg_] = $msg; + # Only issue this warning once + $self->[_K_sequence_error_msg_] = $msg; - } } $self->[_K_last_arrival_] = $K; } @@ -372,7 +368,8 @@ sub write_line { # Write a line directly to the output, without any counting of blank or # non-blank lines. - $self->[_write_line_]->($str); + ${ $self->[_routput_string_] } .= $str; + if ( chomp $str ) { $self->[_output_line_number_]++; } if ( $self->[_save_logfile_] ) { $self->check_line_lengths($str) } @@ -385,7 +382,8 @@ sub check_line_lengths { # collect info on line lengths for logfile # This calculation of excess line length ignores any internal tabs - my $rOpts = $self->[_rOpts_]; + my $rOpts = $self->[_rOpts_]; + chomp $str; my $len_str = length($str); my $exceed = $len_str - $rOpts->{'maximum-line-length'}; if ( $str && substr( $str, 0, 1 ) eq "\t" && $str =~ /^\t+/g ) { diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 1a304616..76674751 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -14661,7 +14661,7 @@ sub process_all_lines { # Handle Format Skipping (FS) and Verbatim (VB) Lines if ( $CODE_type eq 'VB' || $CODE_type eq 'FS' ) { - $self->write_unindented_line("$input_line"); + $self->write_unindented_line($input_line); $file_writer_object->reset_consecutive_blank_lines(); next; } -- 2.39.5