Home
Categories
Dictionary
Download
Project Details
Changes Log
License

Diff engine



The Diff class allows to apply a GNU diff to two text content.

Note that if you want to use a compoent which shows the differences side by side, you can directly use the JDiffTextPanel , the DefaultDiffEngine classes which use this class internally.

Usage examples

Basic usage

Suppose that we have two lists of lines we want to compare:
      Diff d = new Diff(leftList, rightList);
      Diff.change script = d.diff_2(false);
      GUIPrint guiPrint = new GUIPrint(leftList, rightList);
      guiPrint.printScript(script);
      
      List<Change> changes = guiPrint.getChanges();

Using Strings for the comparison

Suppose that we have two Strings, but we still want to compare them line by line:
      List<String> leftList = Diff.getLines(str1);
      List<String> rightList = Diff.getLines(str2);      
      
      Diff d = new Diff(leftList, rightList);
      Diff.change script = d.diff_2(false);
      GUIPrint guiPrint = new GUIPrint(leftList, rightList);
      guiPrint.printScript(script);
      
      List<Change> changes = guiPrint.getChanges();

Comparing two Strings characters without converting them to lines

Suppose that we have two Strings, but we still want to compare them character by character:
        Diff d = new Diff(str1, str2);
      Diff.change script = d.diff_2(false);
      GUIPrint guiPrint = new GUIPrint(str1, str2);
      guiPrint.printScript(script);
      
      List<Change> changes = guiPrint.getChanges();

Usage in the JDiffTextPanel

Main Article: JDiffTextPanel

The JDiffTextPanel class allows to apply a GNU diff to two text content and show the diff result from the two texts side by side.

Usage in the DefaultDiffEngine

Main Article: DefaultDiffEngine

The DefaultDiffEngine class allows to apply a GNU diff to get a list of differences for the left text and the right text. This class is not a Swing component, and allows to use the diff for not GUI usages (for example creating an Excel file with the content of the diff).

See also


Categories: diff

Copyright 2016-2019 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence