# 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 = <<EOM;
+Input file length has $leni chars in $nlines_i lines
+Output file length has $leno chars in $nlines_o lines
+EOM
return $msg unless ( $leni && $leno );
- my @aryi = split /^/, ${$rbufi};
- my @aryo = split /^/, ${$rbufo};
- my ( $linei, $lineo );
- my ( $counti, $counto ) = ( 0, 0 );
- my ( $last_nonblank_line, $last_nonblank_count ) = ( EMPTY_STRING, 0 );
+
my $truncate = sub {
my ( $str, $lenmax ) = @_;
if ( length($str) > $lenmax ) {
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; }
$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 .= <<EOM;
-<$counti:$linei
->$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 .= <<EOM;
Files initially match file but output file has fewer lines
EOM
}
- elsif ( $counti < $counto ) {
+ elsif ( $nlines_i < $nlines_o ) {
$msg .= <<EOM;
Files initially match file but input file has fewer lines
EOM