From f6e6dea5399a6b3aea85aaf1ddb00440c5b3973f Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 9 Oct 2024 08:30:57 -0700 Subject: [PATCH] simplify sub compare_string_buffers --- lib/Perl/Tidy.pm | 80 ++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index daec260c..94251a56 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -3226,17 +3226,24 @@ sub compare_string_buffers { # Compare input and output string buffers and return a brief text # description of the first difference. - - my $leni = defined($rbufi) ? length( ${$rbufi} ) : 0; - my $leno = defined($rbufo) ? length( ${$rbufo} ) : 0; - my $msg = - "Input file length is $leni chars\nOutput file length is $leno chars\n"; + my ( @aryi, @aryo ); + my ( $leni, $leno ) = ( 0, 0 ); + if ( defined($rbufi) ) { + $leni = length( ${$rbufi} ); + @aryi = split /^/, ${$rbufi}; + } + if ( defined($rbufo) ) { + $leno = length( ${$rbufo} ); + @aryo = split /^/, ${$rbufo}; + } + my $nlines_i = @aryi; + my $nlines_o = @aryo; + my $msg = < $lenmax ) { @@ -3245,28 +3252,31 @@ sub compare_string_buffers { return $str; }; ## end $truncate = sub + my $last_nonblank_line = EMPTY_STRING; + my $last_nonblank_count = 0; + # loop over lines until we find a difference - while ( @aryi || @aryo ) { - if ($linei) { - $last_nonblank_line = $linei; - $last_nonblank_count = $counti; + my $count = 0; + while ( @aryi && @aryo ) { + $count++; + my $linei = shift @aryi; + my $lineo = shift @aryo; + chomp $linei; + chomp $lineo; + if ( $linei eq $lineo ) { + if ( length($linei) ) { + $last_nonblank_line = $linei; + $last_nonblank_count = $count; + } + next; } - $linei = shift @aryi; - $lineo = shift @aryo; - - # compare chomp'ed lines - if ( defined($linei) ) { $counti++; chomp $linei } - if ( defined($lineo) ) { $counto++; chomp $lineo } - - # all done if one or both are out of lines - last unless ( defined($linei) && defined($lineo) ); - - next if ( $linei eq $lineo ); - # lines differ ... + #--------------------------- + # lines differ ... finish up + #--------------------------- my ( $line_diff, $pos1 ) = line_diff( $linei, $lineo ); my $ch1 = $pos1 + 1; - my $reason = "Files first differ at character $ch1 of line $counti"; + my $reason = "Files first differ at character $ch1 of line $count"; my ( $leading_ws_i, $leading_ws_o ) = ( EMPTY_STRING, EMPTY_STRING ); if ( $linei =~ /^(\s+)/ ) { $leading_ws_i = $1; } @@ -3304,22 +3314,24 @@ sub compare_string_buffers { $last_nonblank_count:$last_nonblank_line EOM } - $line_diff = SPACE x ( 2 + length($counto) ) . $line_diff; + $line_diff = SPACE x ( 2 + length($count) ) . $line_diff; $msg .= <$counto:$lineo +<$count:$linei +>$count:$lineo $line_diff EOM return $msg; - } ## end while ( @aryi || @aryo ) + } ## end while ( @aryi && @aryo ) - # no line differences found, but one file may have fewer lines - if ( $counti > $counto ) { + #------------------------------------------------------ + # no differences found, see if one file has fewer lines + #------------------------------------------------------ + if ( $nlines_i > $nlines_o ) { $msg .= <