Skip to content

Commit ddb32f6

Browse files
committed
refactor: introduce the DiffEqualizer interface
This replaces the untyped lambda.
1 parent 66512ed commit ddb32f6

File tree

6 files changed

+76
-15
lines changed

6 files changed

+76
-15
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package io.github.petertrr.diffutils
2222

2323
import io.github.petertrr.diffutils.algorithm.DiffAlgorithm
2424
import io.github.petertrr.diffutils.algorithm.DiffAlgorithmListener
25+
import io.github.petertrr.diffutils.algorithm.DiffEqualizer
2526
import io.github.petertrr.diffutils.algorithm.NoopAlgorithmListener
2627
import io.github.petertrr.diffutils.algorithm.myers.MyersDiff
2728
import io.github.petertrr.diffutils.patch.Patch
@@ -73,7 +74,7 @@ public fun diff(
7374
public fun <T> diff(
7475
source: List<T>,
7576
target: List<T>,
76-
equalizer: ((T, T) -> Boolean),
77+
equalizer: DiffEqualizer<T>,
7778
): Patch<T> =
7879
diff(
7980
source = source,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2024 Peter Trifanov.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.petertrr.diffutils.algorithm
17+
18+
public fun interface DiffEqualizer<T> {
19+
public fun test(one: T, two: T): Boolean
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 Peter Trifanov.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.petertrr.diffutils.algorithm
17+
18+
public class EqualsDiffEqualizer<T> : DiffEqualizer<T> {
19+
override fun test(one: T, two: T): Boolean =
20+
one == two
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2024 Peter Trifanov.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.petertrr.diffutils.algorithm
17+
18+
public class IgnoreWsStringDiffEqualizer : DiffEqualizer<String> {
19+
private companion object {
20+
private val ws = Regex("\\s+")
21+
}
22+
23+
override fun test(one: String, two: String): Boolean =
24+
ws.replace(one.trim(), " ") == ws.replace(two.trim(), " ")
25+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ package io.github.petertrr.diffutils.algorithm.myers
2121
import io.github.petertrr.diffutils.algorithm.Change
2222
import io.github.petertrr.diffutils.algorithm.DiffAlgorithm
2323
import io.github.petertrr.diffutils.algorithm.DiffAlgorithmListener
24+
import io.github.petertrr.diffutils.algorithm.DiffEqualizer
25+
import io.github.petertrr.diffutils.algorithm.EqualsDiffEqualizer
2426
import io.github.petertrr.diffutils.patch.DeltaType
2527

2628
/**
2729
* A clean-room implementation of Eugene Myers greedy differencing algorithm.
2830
*/
29-
public class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2 -> t1 == t2 }) : DiffAlgorithm<T> {
31+
public class MyersDiff<T>(private val equalizer: DiffEqualizer<T> = EqualsDiffEqualizer()) : DiffAlgorithm<T> {
3032
/**
3133
* Returns an empty diff if we get an error while procession the difference.
3234
*/
@@ -82,7 +84,7 @@ public class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2 ->
8284
var j = i - k
8385
var node = PathNode(i, j, snake = false, bootstrap = false, prev = prev)
8486

85-
while (i < origSize && j < revSize && equalizer.invoke(orig[i], rev[j])) {
87+
while (i < origSize && j < revSize && equalizer.test(orig[i], rev[j])) {
8688
i++
8789
j++
8890
}

src/commonMain/kotlin/io/github/petertrr/diffutils/text/DiffRowGenerator.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package io.github.petertrr.diffutils.text
2020

21+
import io.github.petertrr.diffutils.algorithm.DiffEqualizer
22+
import io.github.petertrr.diffutils.algorithm.EqualsDiffEqualizer
23+
import io.github.petertrr.diffutils.algorithm.IgnoreWsStringDiffEqualizer
2124
import io.github.petertrr.diffutils.diff
2225
import io.github.petertrr.diffutils.patch.ChangeDelta
2326
import io.github.petertrr.diffutils.patch.Chunk
@@ -51,7 +54,7 @@ public class DiffRowGenerator(
5154
/**
5255
* Provide an equalizer for diff processing.
5356
*/
54-
private var equalizer: ((String, String) -> Boolean) = if (ignoreWhiteSpaces) IGNORE_WHITESPACE_EQUALIZER else DEFAULT_EQUALIZER,
57+
private var equalizer: DiffEqualizer<String> = if (ignoreWhiteSpaces) IgnoreWsStringDiffEqualizer() else EqualsDiffEqualizer(),
5558

5659
/**
5760
* Per default each character is separately processed. Setting this parameter to `true`
@@ -410,12 +413,6 @@ public class DiffRowGenerator(
410413
}
411414

412415
public companion object {
413-
internal val DEFAULT_EQUALIZER: (Any?, Any?) -> Boolean = { o1: Any?, o2: Any? -> o1 == o2 }
414-
internal val IGNORE_WHITESPACE_EQUALIZER: (String, String) -> Boolean = { original: String, revised: String ->
415-
adjustWhitespace(
416-
original
417-
) == adjustWhitespace(revised)
418-
}
419416
internal val LINE_NORMALIZER_FOR_HTML: (String) -> String = { normalize(it) }
420417

421418
/**
@@ -439,11 +436,6 @@ public class DiffRowGenerator(
439436
SPLIT_BY_WORD_PATTERN
440437
)
441438
}
442-
internal val WHITESPACE_PATTERN = Regex("\\s+")
443-
444-
private fun adjustWhitespace(raw: String): String {
445-
return WHITESPACE_PATTERN.replace(raw.trim(), " ")
446-
}
447439

448440
internal fun splitStringPreserveDelimiter(str: String?, splitPattern: Regex): List<String> {
449441
val list: MutableList<String> = mutableListOf()

0 commit comments

Comments
 (0)