Skip to content

feat: Refactor DiffRowGenerator, code cleanup #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import io.github.petertrr.diffutils.patch.PatchFailedException
* @return The patch describing the difference between the original and revised sequences.
*/
public fun <T> diff(original: List<T>, revised: List<T>, progress: DiffAlgorithmListener?): Patch<T> {
return diff(original, revised, MyersDiff<T>(), progress)
return diff(original, revised, MyersDiff(), progress)
}

public fun <T> diff(original: List<T>, revised: List<T>): Patch<T> {
Expand Down Expand Up @@ -168,7 +168,7 @@ private fun compressLines(lines: List<String>, delimiter: String): List<String>
* @return the revised text
* @throws PatchFailedException if can't apply patch
*/
@kotlin.Throws(PatchFailedException::class)
@Throws(PatchFailedException::class)
public fun <T> patch(original: List<T>, patch: Patch<T>): List<T> {
return patch.applyTo(original)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2
* @param orig The original sequence.
* @param rev The revised sequence.
* @return A minimum [PathNode] across the differences graph.
* @throws DifferentiationFailedException if a diff path could not be found.
* @throws IllegalStateException if a diff path could not be found.
*/
private fun buildPath(orig: List<T>, rev: List<T>, progress: DiffAlgorithmListener?): PathNode? {
// these are local constants
Expand Down Expand Up @@ -74,13 +74,13 @@ internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2
}
diagonal[kminus] = null // no longer used
var j = i - k
var node = PathNode(i, j, false, false, prev)
var node = PathNode(i, j, snake = false, bootstrap = false, prev = prev)
while (i < N && j < M && equalizer.invoke(orig[i], rev[j])) {
i++
j++
}
if (i != node.i) {
node = PathNode(i, j, true, false, node)
node = PathNode(i, j, snake = true, bootstrap = false, prev = node)
}
diagonal[kmiddle] = node
if (i >= N && j >= M) {
Expand All @@ -94,14 +94,13 @@ internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2
}

/**
* Constructs a [Patch] from a difference path.
* Constructs a patch from a difference path.
*
* @param actualPath The path.
* @param orig The original sequence.
* @param rev The revised sequence.
* @return A [Patch] script corresponding to the path.
* @throws DifferentiationFailedException if a [Patch] could not be built from the given
* path.
* @return A list of [Change]s corresponding to the path.
* @throws IllegalStateException if a patch could not be built from the given path.
*/
private fun buildRevision(actualPath: PathNode?, orig: List<T>, rev: List<T>): List<Change> {
var path: PathNode? = actualPath
Expand All @@ -114,14 +113,14 @@ internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2
val i: Int = path.i
val j: Int = path.j
path = path.prev
val ianchor: Int = path!!.i
val janchor: Int = path.j
if (ianchor == i && janchor != j) {
changes.add(Change(DeltaType.INSERT, ianchor, i, janchor, j))
} else if (ianchor != i && janchor == j) {
changes.add(Change(DeltaType.DELETE, ianchor, i, janchor, j))
val iAnchor: Int = path!!.i
val jAnchor: Int = path.j
if (iAnchor == i && jAnchor != j) {
changes.add(Change(DeltaType.INSERT, iAnchor, i, jAnchor, j))
} else if (iAnchor != i && jAnchor == j) {
changes.add(Change(DeltaType.DELETE, iAnchor, i, jAnchor, j))
} else {
changes.add(Change(DeltaType.CHANGE, ianchor, i, janchor, j))
changes.add(Change(DeltaType.CHANGE, iAnchor, i, jAnchor, j))
}
if (path.snake) {
path = path.prev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ package io.github.petertrr.diffutils.patch
/**
* Holds the information about the part of text involved in the diff process
*
* Text is represented as `Object[]` because the diff engine is capable of handling more
* than plain ascci. In fact, arrays or lists of any type that implements
* [hashCode()][java.lang.Object.hashCode] and [equals()][java.lang.Object.equals]
* correctly can be subject to differencing using this library.
* Text is represented as generic class `T` because the diff engine is capable of handling more
* than plain ASCII. In fact, arrays or lists of any type that implements
* `hashCode()` and `equals()` correctly can be subject to differencing using this library.
* @param T The type of the compared elements in the 'lines'.
) */
*/
public data class Chunk<T>(
/**
* the start position of chunk in the text
Expand All @@ -45,7 +44,7 @@ public data class Chunk<T>(
* Verifies that this chunk's saved text matches the corresponding text in the given sequence.
*
* @param target the sequence to verify against.
* @throws com.github.difflib.patch.PatchFailedException
* @throws PatchFailedException
*/
@Throws(PatchFailedException::class)
public fun verify(target: List<T>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class Patch<T>(estimatedPatchSize: Int = 10) {
}
if (includeEquals && startOriginal < original.size) {
patch.addDelta(
EqualDelta<T>(
EqualDelta(
buildChunk(startOriginal, original.size, original),
buildChunk(startRevised, revised.size, revised)
)
Expand Down
Loading