public DiffEngine::diff($from_lines, $to_lines)
File
- core/lib/Drupal/Component/Diff/Engine/DiffEngine.php, line 36
Class
- DiffEngine
- Class used internally by Diff to actually compute the diffs.
Namespace
Drupal\Component\Diff\Engine
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | public function diff( $from_lines , $to_lines ) { $n_from = sizeof( $from_lines ); $n_to = sizeof( $to_lines ); $this ->xchanged = $this ->ychanged = array (); $this ->xv = $this ->yv = array (); $this ->xind = $this ->yind = array (); unset( $this ->seq); unset( $this ->in_seq); unset( $this ->lcs); // Skip leading common lines. for ( $skip = 0; $skip < $n_from && $skip < $n_to ; $skip ++) { if ( $from_lines [ $skip ] !== $to_lines [ $skip ]) { break ; } $this ->xchanged[ $skip ] = $this ->ychanged[ $skip ] = FALSE; } // Skip trailing common lines. $xi = $n_from ; $yi = $n_to ; for ( $endskip = 0; -- $xi > $skip && -- $yi > $skip ; $endskip ++) { if ( $from_lines [ $xi ] !== $to_lines [ $yi ]) { break ; } $this ->xchanged[ $xi ] = $this ->ychanged[ $yi ] = FALSE; } // Ignore lines which do not exist in both files. for ( $xi = $skip ; $xi < $n_from - $endskip ; $xi ++) { $xhash [ $this ->_line_hash( $from_lines [ $xi ])] = 1; } for ( $yi = $skip ; $yi < $n_to - $endskip ; $yi ++) { $line = $to_lines [ $yi ]; if ( $this ->ychanged[ $yi ] = empty ( $xhash [ $this ->_line_hash( $line )])) { continue ; } $yhash [ $this ->_line_hash( $line )] = 1; $this ->yv[] = $line ; $this ->yind[] = $yi ; } for ( $xi = $skip ; $xi < $n_from - $endskip ; $xi ++) { $line = $from_lines [ $xi ]; if ( $this ->xchanged[ $xi ] = empty ( $yhash [ $this ->_line_hash( $line )])) { continue ; } $this ->xv[] = $line ; $this ->xind[] = $xi ; } // Find the LCS. $this ->_compareseq(0, sizeof( $this ->xv), 0, sizeof( $this ->yv)); // Merge edits when possible $this ->_shift_boundaries( $from_lines , $this ->xchanged, $this ->ychanged); $this ->_shift_boundaries( $to_lines , $this ->ychanged, $this ->xchanged); // Compute the edit operations. $edits = array (); $xi = $yi = 0; while ( $xi < $n_from || $yi < $n_to ) { $this ::USE_ASSERTS && assert( $yi < $n_to || $this ->xchanged[ $xi ]); $this ::USE_ASSERTS && assert( $xi < $n_from || $this ->ychanged[ $yi ]); // Skip matching "snake". $copy = array (); while ( $xi < $n_from && $yi < $n_to && ! $this ->xchanged[ $xi ] && ! $this ->ychanged[ $yi ]) { $copy [] = $from_lines [ $xi ++]; ++ $yi ; } if ( $copy ) { $edits [] = new DiffOpCopy( $copy ); } // Find deletes & adds. $delete = array (); while ( $xi < $n_from && $this ->xchanged[ $xi ]) { $delete [] = $from_lines [ $xi ++]; } $add = array (); while ( $yi < $n_to && $this ->ychanged[ $yi ]) { $add [] = $to_lines [ $yi ++]; } if ( $delete && $add ) { $edits [] = new DiffOpChange( $delete , $add ); } elseif ( $delete ) { $edits [] = new DiffOpDelete( $delete ); } elseif ( $add ) { $edits [] = new DiffOpAdd( $add ); } } return $edits ; } |
Please login to continue.