TableDiffFormatter: Don't repeatedly call array_shift()
authorKevin Israel <pleasestand@live.com>
Fri, 9 Oct 2015 19:48:50 +0000 (15:48 -0400)
committerKevin Israel <pleasestand@live.com>
Wed, 9 Mar 2016 09:09:07 +0000 (04:09 -0500)
This also fixes a bug that could cause deleted lines to appear as if
they were replaced with blank lines rather than removed entirely.

Change-Id: I99dc2862b130c02aed311f93236eb6b2dc50a0fb

includes/diff/TableDiffFormatter.php

index f1826ed..bcae746 100644 (file)
@@ -204,16 +204,13 @@ class TableDiffFormatter extends DiffFormatter {
                # Notice that WordLevelDiff returns HTML-escaped output.
                # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
 
-               $line = array_shift( $del );
-               while ( $line ) {
-                       $aline = array_shift( $add );
-                       $this->writeOutput( '<tr>' . $this->deletedLine( $line ) .
-                               $this->addedLine( $aline ) . "</tr>\n" );
-                       $line = array_shift( $del );
-               }
-               foreach ( $add as $line ) { # If any leftovers
-                       $this->writeOutput( '<tr>' . $this->emptyLine() .
-                               $this->addedLine( $line ) . "</tr>\n" );
+               $ndel = count( $del );
+               $nadd = count( $add );
+               $n = max( $ndel, $nadd );
+               for ( $i = 0; $i < $n; $i++ ) {
+                       $delLine = $i < $ndel ? $this->deletedLine( $del[$i] ) : $this->emptyLine();
+                       $addLine = $i < $nadd ? $this->addedLine( $add[$i] ) : $this->emptyLine();
+                       $this->writeOutput( "<tr>{$delLine}{$addLine}</tr>\n" );
                }
        }