Skip to content

Commit 06b7c72

Browse files
authored
Perform an overall code cleanup (#108)
As per title, this is just a cleanup to enhance readability or remove unused/duplicated APIs.
1 parent 5f3e6b3 commit 06b7c72

26 files changed

+698
-463
lines changed

Diff for: detekt.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
style:
22
active: true
3+
ReturnCount:
4+
max: 3
35
MaxLineLength:
46
active: true
57
maxLineLength: 180
8+
ForbiddenComment:
9+
active: false
610
formatting:
711
active: true
812
MaximumLineLength:
913
active: false
1014
ParameterListWrapping:
11-
active: false
15+
active: false
16+
TrailingCommaOnDeclarationSite:
17+
active: true

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

+101-134
Original file line numberDiff line numberDiff line change
@@ -16,187 +16,154 @@
1616
*
1717
* This file has been modified by Peter Trifanov when porting from Java to Kotlin.
1818
*/
19-
/**
20-
* Implements the difference and patching engine
21-
*/
22-
23-
@file:Suppress("TooManyFunctions")
19+
@file:JvmName("DiffUtils")
2420

2521
package io.github.petertrr.diffutils
2622

2723
import io.github.petertrr.diffutils.algorithm.DiffAlgorithm
2824
import io.github.petertrr.diffutils.algorithm.DiffAlgorithmListener
25+
import io.github.petertrr.diffutils.algorithm.NoopAlgorithmListener
2926
import io.github.petertrr.diffutils.algorithm.myers.MyersDiff
3027
import io.github.petertrr.diffutils.patch.Patch
3128
import io.github.petertrr.diffutils.patch.PatchFailedException
29+
import io.github.petertrr.diffutils.text.DiffRowGenerator
30+
import kotlin.jvm.JvmName
31+
import kotlin.jvm.JvmOverloads
3232

33-
/**
34-
* Computes the difference between the original and revised list of elements with default diff
35-
* algorithm
36-
*
37-
* @param T types to be diffed
38-
* @param original The original text.
39-
* @param revised The revised text.
40-
* @param progress progress listener
41-
* @return The patch describing the difference between the original and revised sequences.
42-
*/
43-
public fun <T> diff(original: List<T>, revised: List<T>, progress: DiffAlgorithmListener?): Patch<T> {
44-
return diff(original, revised, MyersDiff(), progress)
45-
}
46-
47-
public fun <T> diff(original: List<T>, revised: List<T>): Patch<T> {
48-
return diff(original, revised, MyersDiff(), null)
49-
}
50-
51-
public fun <T> diff(original: List<T>, revised: List<T>, includeEqualParts: Boolean): Patch<T> {
52-
return diff(original, revised, MyersDiff(), null, includeEqualParts)
53-
}
33+
// Instead of asking consumers to normalize their line endings, we simply catch them all.
34+
private val lineBreak = Regex("\r\n|\r|\n")
5435

5536
/**
56-
* Computes the difference between the original and revised text.
37+
* Computes the difference between the source and target text.
38+
*
39+
* By default, uses the Myers algorithm.
40+
*
41+
* @param sourceText The original text
42+
* @param targetText The target text
43+
* @param algorithm The diff algorithm to use
44+
* @param progress The diff algorithm progress listener
45+
* @param includeEqualParts Whether to include equal data parts into the patch. `false` by default.
46+
* @return The patch describing the difference between the original and target text
5747
*/
48+
@JvmOverloads
5849
public fun diff(
5950
sourceText: String,
6051
targetText: String,
61-
progress: DiffAlgorithmListener?
62-
): Patch<String> {
63-
return diff(
64-
sourceText.split("\n"),
65-
targetText.split("\n"),
66-
progress
52+
algorithm: DiffAlgorithm<String> = MyersDiff(),
53+
progress: DiffAlgorithmListener = NoopAlgorithmListener(),
54+
includeEqualParts: Boolean = false,
55+
): Patch<String> =
56+
diff(
57+
source = sourceText.split(lineBreak),
58+
target = targetText.split(lineBreak),
59+
algorithm = algorithm,
60+
progress = progress,
61+
includeEqualParts = includeEqualParts,
6762
)
68-
}
6963

7064
/**
71-
* Computes the difference between the original and revised list of elements with default diff
72-
* algorithm
73-
*
74-
* @param source The original text.
75-
* @param target The revised text.
65+
* Computes the difference between the source and target list of elements using the Myers algorithm.
7666
*
77-
* @param equalizer the equalizer object to replace the default compare algorithm
78-
* (Object.equals). If `null` the default equalizer of the default algorithm is used..
79-
* @return The patch describing the difference between the original and revised sequences.
67+
* @param source The original elements
68+
* @param target The target elements
69+
* @param equalizer The equalizer to replace the default compare algorithm [Any.equals].
70+
* If `null`, the default equalizer of the default algorithm is used.
71+
* @return The patch describing the difference between the source and target sequences
8072
*/
8173
public fun <T> diff(
8274
source: List<T>,
8375
target: List<T>,
84-
equalizer: ((T, T) -> Boolean)?
85-
): Patch<T> {
86-
return if (equalizer != null) {
87-
diff(
88-
source,
89-
target,
90-
MyersDiff(equalizer)
91-
)
92-
} else {
93-
diff(source, target, MyersDiff())
94-
}
95-
}
96-
97-
public fun <T> diff(
98-
original: List<T>,
99-
revised: List<T>,
100-
algorithm: DiffAlgorithm<T>,
101-
progress: DiffAlgorithmListener?
102-
): Patch<T> {
103-
return diff(original, revised, algorithm, progress, false)
104-
}
76+
equalizer: ((T, T) -> Boolean),
77+
): Patch<T> =
78+
diff(
79+
source = source,
80+
target = target,
81+
algorithm = MyersDiff(equalizer),
82+
)
10583

10684
/**
107-
* Computes the difference between the original and revised list of elements with default diff
108-
* algorithm
85+
* Computes the difference between the original and target list of elements.
10986
*
110-
* @param original The original text. Must not be `null`.
111-
* @param revised The revised text. Must not be `null`.
112-
* @param algorithm The diff algorithm. Must not be `null`.
113-
* @param progress The diff algorithm listener.
114-
* @param includeEqualParts Include equal data parts into the patch.
115-
* @return The patch describing the difference between the original and revised sequences. Never
116-
* `null`.
117-
*/
118-
public fun <T> diff(
119-
original: List<T>,
120-
revised: List<T>,
121-
algorithm: DiffAlgorithm<T>,
122-
progress: DiffAlgorithmListener?,
123-
includeEqualParts: Boolean
124-
): Patch<T> {
125-
return Patch.generate(original, revised, algorithm.computeDiff(original, revised, progress), includeEqualParts)
126-
}
127-
128-
/**
129-
* Computes the difference between the original and revised list of elements with default diff
130-
* algorithm
87+
* By default, uses the Meyers algorithm.
13188
*
132-
* @param original The original text. Must not be `null`.
133-
* @param revised The revised text. Must not be `null`.
134-
* @param algorithm The diff algorithm. Must not be `null`.
135-
* @return The patch describing the difference between the original and revised sequences. Never
136-
* `null`.
89+
* @param source The original elements
90+
* @param target The target elements
91+
* @param algorithm The diff algorithm to use
92+
* @param progress The diff algorithm progress listener
93+
* @param includeEqualParts Whether to include equal data parts into the patch. `false` by default.
94+
* @return The patch describing the difference between the original and target sequences
13795
*/
138-
public fun <T> diff(original: List<T>, revised: List<T>, algorithm: DiffAlgorithm<T>): Patch<T> {
139-
return diff(original, revised, algorithm, null)
140-
}
96+
@JvmOverloads
97+
public fun <T> diff(
98+
source: List<T>,
99+
target: List<T>,
100+
algorithm: DiffAlgorithm<T> = MyersDiff(),
101+
progress: DiffAlgorithmListener = NoopAlgorithmListener(),
102+
includeEqualParts: Boolean = false,
103+
): Patch<T> =
104+
Patch.generate(
105+
original = source,
106+
revised = target,
107+
changes = algorithm.computeDiff(source, target, progress),
108+
includeEquals = includeEqualParts,
109+
)
141110

142111
/**
143-
* Computes the difference between the given texts inline. This one uses the "trick" to make out
144-
* of texts lists of characters, like DiffRowGenerator does and merges those changes at the end
145-
* together again.
112+
* Computes the difference between the given texts inline.
146113
*
147-
* @param original
148-
* @param revised
149-
* @return
114+
* This one uses the "trick" to make out of texts lists of characters,
115+
* like [DiffRowGenerator] does and merges those changes at the end together again.
150116
*/
151117
public fun diffInline(original: String, revised: String): Patch<String> {
152-
val origList: MutableList<String> = arrayListOf()
153-
val revList: MutableList<String> = arrayListOf()
154-
for (character in original.toCharArray()) {
118+
val origChars = original.toCharArray()
119+
val origList = ArrayList<String>(origChars.size)
120+
121+
val revChars = revised.toCharArray()
122+
val revList = ArrayList<String>(revChars.size)
123+
124+
for (character in origChars) {
155125
origList.add(character.toString())
156126
}
157-
for (character in revised.toCharArray()) {
127+
128+
for (character in revChars) {
158129
revList.add(character.toString())
159130
}
160-
val patch: Patch<String> = diff(origList, revList)
161-
patch.deltas.map { delta ->
162-
delta.withChunks(
163-
delta.source.copy(lines = compressLines(delta.source.lines, "")),
164-
delta.target.copy(lines = compressLines(delta.target.lines, ""))
131+
132+
val patch = diff(origList, revList)
133+
patch.deltas = patch.deltas.mapTo(ArrayList(patch.deltas.size)) {
134+
it.withChunks(
135+
it.source.copy(lines = compressLines(it.source.lines, "")),
136+
it.target.copy(lines = compressLines(it.target.lines, "")),
165137
)
166138
}
167-
.let { patch.deltas = it.toMutableList() }
168-
return patch
169-
}
170139

171-
private fun compressLines(lines: List<String>, delimiter: String): List<String> {
172-
return if (lines.isEmpty()) {
173-
emptyList()
174-
} else {
175-
listOf(lines.joinToString(delimiter))
176-
}
140+
return patch
177141
}
178142

179143
/**
180-
* Patch the original text with given patch
144+
* Patch the original text with the given patch.
181145
*
182-
* @param original the original text
183-
* @param patch the given patch
184-
* @return the revised text
185-
* @throws PatchFailedException if can't apply patch
146+
* @param original The original text
147+
* @param patch The patch to apply
148+
* @return The revised text
149+
* @throws PatchFailedException If the patch cannot be applied
186150
*/
187-
@Throws(PatchFailedException::class)
188-
public fun <T> patch(original: List<T>, patch: Patch<T>): List<T> {
189-
return patch.applyTo(original)
190-
}
151+
public fun <T> patch(original: List<T>, patch: Patch<T>): List<T> =
152+
patch.applyTo(original)
191153

192154
/**
193155
* Unpatch the revised text for a given patch
194156
*
195-
* @param revised the revised text
196-
* @param patch the given patch
197-
* @return the original text
157+
* @param revised The revised text
158+
* @param patch The given patch
159+
* @return The original text
198160
*/
199-
@Suppress("UNUSED")
200-
public fun <T> unpatch(revised: List<T>, patch: Patch<T>): List<T> {
201-
return patch.restore(revised)
202-
}
161+
public fun <T> unpatch(revised: List<T>, patch: Patch<T>): List<T> =
162+
patch.restore(revised)
163+
164+
private fun compressLines(lines: List<String>, delimiter: String): List<String> =
165+
if (lines.isEmpty()) {
166+
emptyList()
167+
} else {
168+
listOf(lines.joinToString(delimiter))
169+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ public data class Change(
2525
val startOriginal: Int,
2626
val endOriginal: Int,
2727
val startRevised: Int,
28-
val endRevised: Int
28+
val endRevised: Int,
2929
)

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

+8-21
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,17 @@
1919
package io.github.petertrr.diffutils.algorithm
2020

2121
/**
22-
* Interface of a diff algorithm.
22+
* Describes a diff algorithm.
2323
*
24-
* @param T type of data that is diffed.
24+
* @param T The type of data that should be diffed
2525
*/
2626
public interface DiffAlgorithm<T> {
2727
/**
28-
* Computes the changeset to patch the source list to the target list.
29-
*
30-
* @param source source data
31-
* @param target target data
32-
* @param progress progress listener
33-
* @return
28+
* Computes the changeset to patch the [source] list to the [target] list.
3429
*/
35-
public fun computeDiff(source: List<T>, target: List<T>, progress: DiffAlgorithmListener?): List<Change>
36-
37-
/**
38-
* Simple extension to compute a changeset using arrays.
39-
*
40-
* @param source
41-
* @param target
42-
* @param progress
43-
* @return
44-
*/
45-
public fun computeDiff(source: Array<T>, target: Array<T>, progress: DiffAlgorithmListener?): List<Change> {
46-
return computeDiff(source.toList(), target.toList(), progress)
47-
}
30+
public fun computeDiff(
31+
source: List<T>,
32+
target: List<T>,
33+
progress: DiffAlgorithmListener = NoopAlgorithmListener(),
34+
): List<Change>
4835
}

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@
1919
package io.github.petertrr.diffutils.algorithm
2020

2121
public interface DiffAlgorithmListener {
22+
/**
23+
* Notifies computing a diff has started.
24+
*/
2225
public fun diffStart()
2326

2427
/**
25-
* This is a step within the diff algorithm. Due to different implementations the value
26-
* is not strict incrementing to the max and is not guarantee to reach the max. It could
27-
* stop before.
28+
* Notifies of a step within the diff algorithm.
2829
*
29-
* @param value
30-
* @param max
30+
* Due to different implementations the value is not strict incrementing
31+
* to the max and is not guarantee to reach the max. It could stop before.
3132
*/
3233
public fun diffStep(value: Int, max: Int)
3334

35+
/**
36+
* Notifies computing a diff has ended.
37+
*/
3438
public fun diffEnd()
3539
}

0 commit comments

Comments
 (0)