Dmitry Savenko

Software Engineer. Get in touch:

Project: Diff

14 Jul 2011

Diff is a Fantom library to find differences between two sequences of objects of any type (e.g. strings, text files line-by-line, etc). As a pure Fantom library, it can be compiled into Java or JavaScript.

Based on the C# library by Matthias Hertel.

Examples

Find differences between simple strings:

using diff

class DiffExample
{
  static Void main(Str[] args) {
    echo("*** Strings from wiki page (http://en.wikipedia.org/wiki/Diff) ***")
    sa := "abcdfghjqz"
    sb := "abcdefgijkrxyz"
    echo("String A: $sa")
    echo("String B: $sb")
    echo(Diff.run(sa, sb))
    
    echo("\n*** List of numbers ***")
    la := [1, 2, 3, 4]
    lb := [3, 4, 1, 2]
    echo("List A: $la")
    echo("List B: $lb")
    echo(Diff.run(la, lb))
    
    echo("\n*** Example, when setting minimal flag results in shorter deltas ***")
    echo("  (setting minimal flag may decrease performance dramatically)")
    ma := "aa"
    mb := "ba"
    echo("String A: $ma")
    echo("String B: $mb")
    echo("Without minimal flag set: " + Diff.run(ma, mb))
    echo("With minimal flag set: " + Diff.run(ma, mb, true))
  }
  
}

File differences between text files:

using diff

class FileExample
{
  static Void main(Str[] args) {
    if (2 > args.size) {
      echo("Compares two files line by line")
      echo("Usage: fan FileDiff.fan <file A> <file B>")
      return
    }
    inA := File(`${args[0]}`).in
    inB := File(`${args[1]}`).in
    deltas := Diff.run(inA, inB)
    deltas.each { echo("$it.a => $it.b") }
  }
}