Home
Categories
Dictionnary
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 class which uses 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();


Categories: diff

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