Skip to content

Commit dc2b266

Browse files
authored
feat!: Refactor DiffRowGenerator, code cleanup (#16)
* Get rid of builder in favor of constructor * Code cleanup, docs cleanup BREAKING CHANGE: API of DiffRowGenerator has changed, constructor should be used instead of builder
1 parent dce5b9e commit dc2b266

File tree

7 files changed

+297
-475
lines changed

7 files changed

+297
-475
lines changed

src/commonMain/kotlin/io/github/petertrr/diffutils/DiffUtils.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import io.github.petertrr.diffutils.patch.PatchFailedException
3939
* @return The patch describing the difference between the original and revised sequences.
4040
*/
4141
public fun <T> diff(original: List<T>, revised: List<T>, progress: DiffAlgorithmListener?): Patch<T> {
42-
return diff(original, revised, MyersDiff<T>(), progress)
42+
return diff(original, revised, MyersDiff(), progress)
4343
}
4444

4545
public fun <T> diff(original: List<T>, revised: List<T>): Patch<T> {
@@ -168,7 +168,7 @@ private fun compressLines(lines: List<String>, delimiter: String): List<String>
168168
* @return the revised text
169169
* @throws PatchFailedException if can't apply patch
170170
*/
171-
@kotlin.Throws(PatchFailedException::class)
171+
@Throws(PatchFailedException::class)
172172
public fun <T> patch(original: List<T>, patch: Patch<T>): List<T> {
173173
return patch.applyTo(original)
174174
}

src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiff.kt

+13-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2
4545
* @param orig The original sequence.
4646
* @param rev The revised sequence.
4747
* @return A minimum [PathNode] across the differences graph.
48-
* @throws DifferentiationFailedException if a diff path could not be found.
48+
* @throws IllegalStateException if a diff path could not be found.
4949
*/
5050
private fun buildPath(orig: List<T>, rev: List<T>, progress: DiffAlgorithmListener?): PathNode? {
5151
// these are local constants
@@ -74,13 +74,13 @@ internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2
7474
}
7575
diagonal[kminus] = null // no longer used
7676
var j = i - k
77-
var node = PathNode(i, j, false, false, prev)
77+
var node = PathNode(i, j, snake = false, bootstrap = false, prev = prev)
7878
while (i < N && j < M && equalizer.invoke(orig[i], rev[j])) {
7979
i++
8080
j++
8181
}
8282
if (i != node.i) {
83-
node = PathNode(i, j, true, false, node)
83+
node = PathNode(i, j, snake = true, bootstrap = false, prev = node)
8484
}
8585
diagonal[kmiddle] = node
8686
if (i >= N && j >= M) {
@@ -94,14 +94,13 @@ internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2
9494
}
9595

9696
/**
97-
* Constructs a [Patch] from a difference path.
97+
* Constructs a patch from a difference path.
9898
*
9999
* @param actualPath The path.
100100
* @param orig The original sequence.
101101
* @param rev The revised sequence.
102-
* @return A [Patch] script corresponding to the path.
103-
* @throws DifferentiationFailedException if a [Patch] could not be built from the given
104-
* path.
102+
* @return A list of [Change]s corresponding to the path.
103+
* @throws IllegalStateException if a patch could not be built from the given path.
105104
*/
106105
private fun buildRevision(actualPath: PathNode?, orig: List<T>, rev: List<T>): List<Change> {
107106
var path: PathNode? = actualPath
@@ -114,14 +113,14 @@ internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2
114113
val i: Int = path.i
115114
val j: Int = path.j
116115
path = path.prev
117-
val ianchor: Int = path!!.i
118-
val janchor: Int = path.j
119-
if (ianchor == i && janchor != j) {
120-
changes.add(Change(DeltaType.INSERT, ianchor, i, janchor, j))
121-
} else if (ianchor != i && janchor == j) {
122-
changes.add(Change(DeltaType.DELETE, ianchor, i, janchor, j))
116+
val iAnchor: Int = path!!.i
117+
val jAnchor: Int = path.j
118+
if (iAnchor == i && jAnchor != j) {
119+
changes.add(Change(DeltaType.INSERT, iAnchor, i, jAnchor, j))
120+
} else if (iAnchor != i && jAnchor == j) {
121+
changes.add(Change(DeltaType.DELETE, iAnchor, i, jAnchor, j))
123122
} else {
124-
changes.add(Change(DeltaType.CHANGE, ianchor, i, janchor, j))
123+
changes.add(Change(DeltaType.CHANGE, iAnchor, i, jAnchor, j))
125124
}
126125
if (path.snake) {
127126
path = path.prev

src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Chunk.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ package io.github.petertrr.diffutils.patch
2121
/**
2222
* Holds the information about the part of text involved in the diff process
2323
*
24-
* Text is represented as `Object[]` because the diff engine is capable of handling more
25-
* than plain ascci. In fact, arrays or lists of any type that implements
26-
* [hashCode()][java.lang.Object.hashCode] and [equals()][java.lang.Object.equals]
27-
* correctly can be subject to differencing using this library.
24+
* Text is represented as generic class `T` because the diff engine is capable of handling more
25+
* than plain ASCII. In fact, arrays or lists of any type that implements
26+
* `hashCode()` and `equals()` correctly can be subject to differencing using this library.
2827
* @param T The type of the compared elements in the 'lines'.
29-
) */
28+
*/
3029
public data class Chunk<T>(
3130
/**
3231
* the start position of chunk in the text
@@ -45,7 +44,7 @@ public data class Chunk<T>(
4544
* Verifies that this chunk's saved text matches the corresponding text in the given sequence.
4645
*
4746
* @param target the sequence to verify against.
48-
* @throws com.github.difflib.patch.PatchFailedException
47+
* @throws PatchFailedException
4948
*/
5049
@Throws(PatchFailedException::class)
5150
public fun verify(target: List<T>) {

src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public class Patch<T>(estimatedPatchSize: Int = 10) {
115115
}
116116
if (includeEquals && startOriginal < original.size) {
117117
patch.addDelta(
118-
EqualDelta<T>(
118+
EqualDelta(
119119
buildChunk(startOriginal, original.size, original),
120120
buildChunk(startRevised, revised.size, revised)
121121
)

0 commit comments

Comments
 (0)