Skip to content

Commit be64f94

Browse files
committed
Fixed DiffRowGenerator, ported tests
1 parent f72f372 commit be64f94

File tree

20 files changed

+1282
-165
lines changed

20 files changed

+1282
-165
lines changed

Diff for: build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ repositories {
1515
}
1616

1717
kotlin {
18+
explicitApi()
19+
1820
jvm()
1921
js(IR) {
2022
browser()

Diff for: src/commonMain/kotlin/com/github/petertrr/diffutils/DiffUtils.kt

+14-14
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ package io.github.petertrr.diffutils
2525
import io.github.petertrr.diffutils.algorithm.DiffAlgorithm
2626
import io.github.petertrr.diffutils.algorithm.DiffAlgorithmListener
2727
import io.github.petertrr.diffutils.algorithm.myers.MyersDiff
28-
import io.github.petertrr.diffutils.patch.Chunk
2928
import io.github.petertrr.diffutils.patch.Patch
3029
import io.github.petertrr.diffutils.patch.PatchFailedException
3130

@@ -39,22 +38,22 @@ import io.github.petertrr.diffutils.patch.PatchFailedException
3938
* @param progress progress listener
4039
* @return The patch describing the difference between the original and revised sequences.
4140
*/
42-
fun <T> diff(original: List<T>, revised: List<T>, progress: DiffAlgorithmListener?): Patch<T> {
41+
public fun <T> diff(original: List<T>, revised: List<T>, progress: DiffAlgorithmListener?): Patch<T> {
4342
return diff(original, revised, MyersDiff<T>(), progress)
4443
}
4544

46-
fun <T> diff(original: List<T>, revised: List<T>): Patch<T> {
45+
public fun <T> diff(original: List<T>, revised: List<T>): Patch<T> {
4746
return diff(original, revised, MyersDiff(), null)
4847
}
4948

50-
fun <T> diff(original: List<T>, revised: List<T>, includeEqualParts: Boolean): Patch<T> {
49+
public fun <T> diff(original: List<T>, revised: List<T>, includeEqualParts: Boolean): Patch<T> {
5150
return diff(original, revised, MyersDiff(), null, includeEqualParts)
5251
}
5352

5453
/**
5554
* Computes the difference between the original and revised text.
5655
*/
57-
fun diff(sourceText: String, targetText: String,
56+
public fun diff(sourceText: String, targetText: String,
5857
progress: DiffAlgorithmListener?): Patch<String> {
5958
return diff(sourceText.split("\n"),
6059
targetText.split("\n"),
@@ -73,7 +72,7 @@ fun diff(sourceText: String, targetText: String,
7372
* (Object.equals). If `null` the default equalizer of the default algorithm is used..
7473
* @return The patch describing the difference between the original and revised sequences.
7574
*/
76-
fun <T> diff(
75+
public fun <T> diff(
7776
source: List<T>, target: List<T>,
7877
equalizer: ((T, T) -> Boolean)?
7978
): Patch<T> {
@@ -85,7 +84,7 @@ fun <T> diff(
8584
} else diff(source, target, MyersDiff())
8685
}
8786

88-
fun <T> diff(
87+
public fun <T> diff(
8988
original: List<T>, revised: List<T>,
9089
algorithm: DiffAlgorithm<T>, progress: DiffAlgorithmListener?
9190
): Patch<T> {
@@ -104,7 +103,7 @@ fun <T> diff(
104103
* @return The patch describing the difference between the original and revised sequences. Never
105104
* `null`.
106105
*/
107-
fun <T> diff(
106+
public fun <T> diff(
108107
original: List<T>, revised: List<T>,
109108
algorithm: DiffAlgorithm<T>, progress: DiffAlgorithmListener?,
110109
includeEqualParts: Boolean
@@ -122,7 +121,7 @@ fun <T> diff(
122121
* @return The patch describing the difference between the original and revised sequences. Never
123122
* `null`.
124123
*/
125-
fun <T> diff(original: List<T>, revised: List<T>, algorithm: DiffAlgorithm<T>): Patch<T> {
124+
public fun <T> diff(original: List<T>, revised: List<T>, algorithm: DiffAlgorithm<T>): Patch<T> {
126125
return diff(original, revised, algorithm, null)
127126
}
128127

@@ -135,7 +134,7 @@ fun <T> diff(original: List<T>, revised: List<T>, algorithm: DiffAlgorithm<T>):
135134
* @param revised
136135
* @return
137136
*/
138-
fun diffInline(original: String, revised: String): Patch<String> {
137+
public fun diffInline(original: String, revised: String): Patch<String> {
139138
val origList: MutableList<String> = arrayListOf()
140139
val revList: MutableList<String> = arrayListOf()
141140
for (character in original.toCharArray()) {
@@ -145,13 +144,13 @@ fun diffInline(original: String, revised: String): Patch<String> {
145144
revList.add(character.toString())
146145
}
147146
val patch: Patch<String> = diff(origList, revList)
148-
patch.getDeltas().map { delta ->
147+
patch.deltas.map { delta ->
149148
delta.withChunks(
150149
delta.source.copy(lines = compressLines(delta.source.lines, "")),
151150
delta.target.copy(lines = compressLines(delta.target.lines, ""))
152151
)
153152
}
154-
.let { patch.setDeltas(it) }
153+
.let { patch.deltas = it.toMutableList() }
155154
return patch
156155
}
157156

@@ -170,7 +169,7 @@ private fun compressLines(lines: List<String>, delimiter: String): List<String>
170169
* @throws PatchFailedException if can't apply patch
171170
*/
172171
@kotlin.Throws(PatchFailedException::class)
173-
fun <T> patch(original: List<T>, patch: Patch<T>): List<T> {
172+
public fun <T> patch(original: List<T>, patch: Patch<T>): List<T> {
174173
return patch.applyTo(original)
175174
}
176175

@@ -181,6 +180,7 @@ fun <T> patch(original: List<T>, patch: Patch<T>): List<T> {
181180
* @param patch the given patch
182181
* @return the original text
183182
*/
184-
fun <T> unpatch(revised: List<T>, patch: Patch<T>): List<T> {
183+
@Suppress("UNUSED")
184+
public fun <T> unpatch(revised: List<T>, patch: Patch<T>): List<T> {
185185
return patch.restore(revised)
186186
}

Diff for: src/commonMain/kotlin/com/github/petertrr/diffutils/algorithm/Change.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ package io.github.petertrr.diffutils.algorithm
2020

2121
import io.github.petertrr.diffutils.patch.DeltaType
2222

23-
data class Change(val deltaType: DeltaType,
24-
val startOriginal: Int,
25-
val endOriginal: Int,
26-
val startRevised: Int,
27-
val endRevised: Int
23+
public data class Change(val deltaType: DeltaType,
24+
val startOriginal: Int,
25+
val endOriginal: Int,
26+
val startRevised: Int,
27+
val endRevised: Int
2828
)

Diff for: src/commonMain/kotlin/com/github/petertrr/diffutils/algorithm/DiffAlgorithm.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package io.github.petertrr.diffutils.algorithm
2323
*
2424
* @param T type of data that is diffed.
2525
*/
26-
interface DiffAlgorithm<T> {
26+
public interface DiffAlgorithm<T> {
2727
/**
2828
* Computes the changeset to patch the source list to the target list.
2929
*
@@ -32,7 +32,7 @@ interface DiffAlgorithm<T> {
3232
* @param progress progress listener
3333
* @return
3434
*/
35-
fun computeDiff(source: List<T>, target: List<T>, progress: DiffAlgorithmListener?): List<Change>
35+
public fun computeDiff(source: List<T>, target: List<T>, progress: DiffAlgorithmListener?): List<Change>
3636

3737
/**
3838
* Simple extension to compute a changeset using arrays.
@@ -42,7 +42,7 @@ interface DiffAlgorithm<T> {
4242
* @param progress
4343
* @return
4444
*/
45-
fun computeDiff(source: Array<T>, target: Array<T>, progress: DiffAlgorithmListener?): List<Change> {
45+
public fun computeDiff(source: Array<T>, target: Array<T>, progress: DiffAlgorithmListener?): List<Change> {
4646
return computeDiff(source.toList(), target.toList(), progress)
4747
}
4848
}

Diff for: src/commonMain/kotlin/com/github/petertrr/diffutils/algorithm/DiffAlgorithmListener.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
package io.github.petertrr.diffutils.algorithm
2020

21-
interface DiffAlgorithmListener {
22-
fun diffStart()
21+
public interface DiffAlgorithmListener {
22+
public fun diffStart()
2323

2424
/**
2525
* This is a step within the diff algorithm. Due to different implementations the value
@@ -29,6 +29,7 @@ interface DiffAlgorithmListener {
2929
* @param value
3030
* @param max
3131
*/
32-
fun diffStep(value: Int, max: Int)
33-
fun diffEnd()
32+
public fun diffStep(value: Int, max: Int)
33+
34+
public fun diffEnd()
3435
}

Diff for: src/commonMain/kotlin/com/github/petertrr/diffutils/algorithm/myers/MyersDiff.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import io.github.petertrr.diffutils.patch.DeltaType
2626
/**
2727
* A clean-room implementation of Eugene Myers greedy differencing algorithm.
2828
*/
29-
class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2 -> t1 == t2 }) : DiffAlgorithm<T> {
29+
internal class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2 -> t1 == t2 }) : DiffAlgorithm<T> {
3030
/**
3131
* Return empty diff if get the error while procession the difference.
3232
*/

Diff for: src/commonMain/kotlin/com/github/petertrr/diffutils/algorithm/myers/PathNode.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package io.github.petertrr.diffutils.algorithm.myers
2121
/**
2222
* A node in a diffpath.
2323
*/
24-
class PathNode(
24+
internal class PathNode(
2525
/**
2626
* Position in the original sequence.
2727
*/

Diff for: src/commonMain/kotlin/com/github/petertrr/diffutils/patch/Chunk.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ package io.github.petertrr.diffutils.patch
2727
* correctly can be subject to differencing using this library.
2828
* @param T The type of the compared elements in the 'lines'.
2929
) */
30-
data class Chunk<T>(
30+
public data class Chunk<T>(
3131
/**
3232
* the start position of chunk in the text
3333
*/
@@ -48,7 +48,7 @@ data class Chunk<T>(
4848
* @throws com.github.difflib.patch.PatchFailedException
4949
*/
5050
@Throws(PatchFailedException::class)
51-
fun verify(target: List<T>) {
51+
public fun verify(target: List<T>) {
5252
if (position > target.size || last() > target.size) {
5353
throw PatchFailedException("Incorrect Chunk: the position of chunk > target size")
5454
}
@@ -61,14 +61,14 @@ data class Chunk<T>(
6161
}
6262
}
6363

64-
fun size() = lines.size
64+
public fun size(): Int = lines.size
6565

6666
/**
6767
* Returns the index of the last line of the chunk.
6868
*/
69-
fun last(): Int {
69+
public fun last(): Int {
7070
return position + size() - 1
7171
}
7272

73-
override fun toString() = "[position: $position, size: ${size()}, lines: $lines]"
73+
override fun toString(): String = "[position: $position, size: ${size()}, lines: $lines]"
7474
}

Diff for: src/commonMain/kotlin/com/github/petertrr/diffutils/patch/Delta.kt

+16-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ package io.github.petertrr.diffutils.patch
3333
* which is no change at all.
3434
*
3535
*/
36-
enum class DeltaType {
36+
public enum class DeltaType {
3737
/**
3838
* A change in the original.
3939
*/
@@ -52,9 +52,9 @@ enum class DeltaType {
5252
EQUAL
5353
}
5454

55-
sealed class Delta<T>(val type: DeltaType) {
56-
abstract val source: Chunk<T>
57-
abstract val target: Chunk<T>
55+
public sealed class Delta<T>(public val type: DeltaType) {
56+
public abstract val source: Chunk<T>
57+
public abstract val target: Chunk<T>
5858

5959
/**
6060
* Verify the chunk of this delta, to fit the target.
@@ -67,16 +67,16 @@ sealed class Delta<T>(val type: DeltaType) {
6767
}
6868

6969
@Throws(PatchFailedException::class)
70-
abstract fun applyTo(target: MutableList<T>)
70+
public abstract fun applyTo(target: MutableList<T>)
7171

72-
abstract fun restore(target: MutableList<T>)
72+
public abstract fun restore(target: MutableList<T>)
7373

7474
/**
7575
* Create a new delta of the actual instance with customized chunk data.
7676
*/
77-
abstract fun withChunks(original: Chunk<T>, revised: Chunk<T>): Delta<T>
77+
public abstract fun withChunks(original: Chunk<T>, revised: Chunk<T>): Delta<T>
7878
}
79-
data class ChangeDelta<T>(override val source: Chunk<T>, override val target: Chunk<T>) : Delta<T>(DeltaType.CHANGE) {
79+
public data class ChangeDelta<T>(override val source: Chunk<T>, override val target: Chunk<T>) : Delta<T>(DeltaType.CHANGE) {
8080
override fun applyTo(target: MutableList<T>) {
8181
verifyChunk(target)
8282
val position: Int = source.position
@@ -103,7 +103,7 @@ data class ChangeDelta<T>(override val source: Chunk<T>, override val target: Ch
103103
override fun withChunks(original: Chunk<T>, revised: Chunk<T>): Delta<T> = ChangeDelta(original, revised)
104104
}
105105

106-
data class DeleteDelta<T>(override val source: Chunk<T>, override val target: Chunk<T>) : Delta<T>(DeltaType.DELETE) {
106+
public data class DeleteDelta<T>(override val source: Chunk<T>, override val target: Chunk<T>) : Delta<T>(DeltaType.DELETE) {
107107
override fun applyTo(target: MutableList<T>) {
108108
verifyChunk(target)
109109
val position = source.position
@@ -120,10 +120,10 @@ data class DeleteDelta<T>(override val source: Chunk<T>, override val target: Ch
120120
}
121121
}
122122

123-
override fun withChunks(original: Chunk<T>, revised: Chunk<T>) = DeleteDelta(original, revised)
123+
override fun withChunks(original: Chunk<T>, revised: Chunk<T>): Delta<T> = DeleteDelta(original, revised)
124124
}
125125

126-
data class InsertDelta<T>(override val source: Chunk<T>, override val target: Chunk<T>) : Delta<T>(DeltaType.INSERT) {
126+
public data class InsertDelta<T>(override val source: Chunk<T>, override val target: Chunk<T>) : Delta<T>(DeltaType.INSERT) {
127127
override fun applyTo(target: MutableList<T>) {
128128
verifyChunk(target)
129129
val position = this.source.position
@@ -139,13 +139,13 @@ data class InsertDelta<T>(override val source: Chunk<T>, override val target: Ch
139139
}
140140
}
141141

142-
override fun withChunks(original: Chunk<T>, revised: Chunk<T>) = InsertDelta(original, revised)
142+
override fun withChunks(original: Chunk<T>, revised: Chunk<T>): Delta<T> = InsertDelta(original, revised)
143143
}
144144

145-
data class EqualDelta<T>(override val source: Chunk<T>, override val target: Chunk<T>) : Delta<T>(DeltaType.EQUAL) {
146-
override fun applyTo(target: MutableList<T>) = verifyChunk(target)
145+
public data class EqualDelta<T>(override val source: Chunk<T>, override val target: Chunk<T>) : Delta<T>(DeltaType.EQUAL) {
146+
override fun applyTo(target: MutableList<T>): Unit = verifyChunk(target)
147147

148-
override fun restore(target: MutableList<T>) = Unit
148+
override fun restore(target: MutableList<T>): Unit = Unit
149149

150-
override fun withChunks(original: Chunk<T>, revised: Chunk<T>) = EqualDelta(original, revised)
150+
override fun withChunks(original: Chunk<T>, revised: Chunk<T>): Delta<T> = EqualDelta(original, revised)
151151
}

0 commit comments

Comments
 (0)