Skip to content

Commit 6788527

Browse files
committed
refactor: port changes up to version 4.15
1 parent ea81e21 commit 6788527

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

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

+32-10
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,62 @@ public class Patch<T>(private var conflictOutput: ConflictOutput<T> = ExceptionP
3737
}
3838

3939
/**
40-
* Apply this patch to the given target.
40+
* Apply this patch to the given target list, returning a new list.
4141
*
4242
* @return The patched text
4343
* @throws PatchFailedException If the patch cannot be applied
4444
*/
4545
public fun applyTo(target: List<T>): List<T> {
4646
val result = target.toMutableList()
47+
applyToExisting(result)
48+
return result
49+
}
50+
51+
/**
52+
* Apply this patch to the given target list, directly modifying it.
53+
*
54+
* @return The patched text
55+
* @throws PatchFailedException If the patch cannot be applied
56+
*/
57+
@Suppress("MemberVisibilityCanBePrivate")
58+
public fun applyToExisting(target: MutableList<T>) {
4759
val it = deltas.listIterator(deltas.size)
4860

4961
while (it.hasPrevious()) {
5062
val delta = it.previous()
51-
val verifyChunk = delta.verifyAndApplyTo(result)
52-
conflictOutput.processConflict(verifyChunk, delta, result)
63+
val verifyChunk = delta.verifyAndApplyTo(target)
64+
conflictOutput.processConflict(verifyChunk, delta, target)
5365
}
54-
55-
return result
5666
}
5767

5868
/**
59-
* Restore the text to its original form. Opposite of the [applyTo] method.
69+
* Creates a new list, containing the restored state of the given target list.
70+
* Opposite of the [applyTo] method.
6071
*
6172
* @param target The given target
6273
* @return The restored text
6374
*/
6475
public fun restore(target: List<T>): List<T> {
6576
val result = target.toMutableList()
77+
restoreToExisting(result)
78+
return result
79+
}
80+
81+
/**
82+
* Restores all changes within the given target list.
83+
* Opposite of the [applyToExisting] method.
84+
*
85+
* @param target The given target
86+
* @return The restored text
87+
*/
88+
@Suppress("MemberVisibilityCanBePrivate")
89+
public fun restoreToExisting(target: MutableList<T>) {
6690
val it = deltas.listIterator(deltas.size)
6791

6892
while (it.hasPrevious()) {
6993
val delta = it.previous()
70-
delta.restore(result)
94+
delta.restore(target)
7195
}
72-
73-
return result
7496
}
7597

7698
/**
@@ -102,7 +124,7 @@ public class Patch<T>(private var conflictOutput: ConflictOutput<T> = ExceptionP
102124
var startRevised = 0
103125

104126
val adjustedChanges = if (includeEquals) {
105-
changes.sortedBy { it.startOriginal }
127+
changes.sortedBy(Change::startOriginal)
106128
} else {
107129
changes
108130
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
package io.github.petertrr.diffutils.text
1717

1818
// As a "global" variable to avoid re-compiling the regex each time
19-
private val defaultPattern = Regex("\\s+|[,.\\[\\](){}/\\\\*+\\-#]")
19+
@Suppress("RegExpRedundantEscape") // To be PCRE compliant!
20+
private val defaultPattern = Regex("""\s+|[,.\[\](){}\/\\*+\-#<>;:&']+""")
2021

2122
/**
2223
* Splitting lines by word to achieve word by word diff checking.

0 commit comments

Comments
 (0)