This is a Kotlin port of the difflib
Python package found
here. Requires JRE >= 8.
A flexible class for comparing pairs of sequences. The basic algorithm predates, and is a little fancier than, an algorithm published in the late 1980's by Ratcliff and Obershelp under the hyperbolic name "gestalt pattern matching".
val s = SequenceMatcher("abcd".toList(), "bcde".toList())
s.ratio() // 0.75
Differ is a class for comparing sequences of lines of text, and producing human-readable differences or deltas.
val d = Differ()
val a = listOf("one\n", "two\n", "three\n")
val b = listOf("ore\n", "tree\n", "emu\n")
d.compare(a, b)
Note: This class depends on experimental Kotlin features!
For producing HTML side by side comparison with change highlights. The current implementation differs from the original
Python difflib.HtmlDiff
in that it uses SequenceMatcher
to produce side-by-side comparisons instead of Differ
.
val from = listOf("one", "two", "three")
val to = listOf("once", "too", "tree")
htmlDiff.makeFile(from, to)
repositories {
jcenter()
}
...
dependencies {
compile 'com.mhv2109:difflib:1.0.0'
}
<repositories>
<repository>
<id>jcenter<id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
...
<dependency>
<groupId>com.mhv2109</groupId>
<artifactId>difflib</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>