Line-oriented unified diff with unlimited context

169 views
Skip to first unread message

Tim Landscheidt

unread,
Dec 12, 2014, 11:58:11 PM12/12/14
to diff-mat...@googlegroups.com
Hi,


I'd like to display changes in a text field in a mail in unified form with unlimited context, i. e. I want the "patch" to include all of the "new" text (with no "@@" lines).  Copying and pasting from the wiki I came up with:

function diff_lineMode ($text1, $text2) {
  $dmp = new diff_match_patch ();
  $a = $dmp->diff_linesToChars ($text1, $text2);
  $lineText1 = $a [0];
  $lineText2 = $a [1];
  $lineArray = $a [2];

  $diffs = $dmp->diff_main ($lineText1, $lineText2, false);

  $dmp->diff_charsToLines ($diffs, $lineArray);

  for ($i = 0; $i < count ($diffs); $i++) {
    if ($diffs [$i] [0] < 0) {
      $prefix = '-';
    } elseif ($diffs [$i] [0] > 0) {
      $prefix = '+';
    } else {
      $prefix = ' ';
    }
    $lines = explode ("\n", substr ($diffs [$i] [1], 0, -1));
    foreach ($lines as $line) {
      echo ($prefix . $line . "\n");
    }
  }
}

which works nicely, but looks rather clumsy.  Is there a better way to solve this?

TIA,
Tim
Reply all
Reply to author
Forward
0 new messages