Skip to content

Commit 2b9d405

Browse files
committed
fix bugs in conversion to get all tests passing
1 parent e96f135 commit 2b9d405

File tree

6 files changed

+23
-27
lines changed

6 files changed

+23
-27
lines changed

src/commonMain/kotlin/com/github/difflib/DiffUtils.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ object DiffUtils {
6060
// @Throws(DiffException::class)
6161
fun diff(sourceText: String, targetText: String,
6262
progress: DiffAlgorithmListener): Patch<String> {
63-
return DiffUtils.diff(
64-
sourceText.split("\n".toRegex()).dropLastWhile { it.isEmpty() },
65-
targetText.split("\n".toRegex()).dropLastWhile { it.isEmpty() }, progress)
63+
return DiffUtils.diff(sourceText.lines(), targetText.lines(), progress)
6664
}
6765

6866
/**
@@ -132,7 +130,7 @@ object DiffUtils {
132130
revList.add(character.toString())
133131
}
134132
val patch = DiffUtils.diff(origList, revList)
135-
for (delta in patch.deltas) {
133+
for (delta in patch.getDeltas()) {
136134
delta.source.lines = compressLines(delta.source.lines!!, "")
137135
delta.target.lines = compressLines(delta.target.lines!!, "")
138136
}

src/commonMain/kotlin/com/github/difflib/UnifiedDiffUtils.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ object UnifiedDiffUtils {
137137
fun generateUnifiedDiff(originalFileName: String?,
138138
revisedFileName: String?, originalLines: List<String>, patch: Patch<String>,
139139
contextSize: Int): List<String> {
140-
if (!patch.deltas.isEmpty()) {
140+
if (patch.getDeltas().isNotEmpty()) {
141141
val ret = ArrayList<String>()
142142
ret.add("--- $originalFileName")
143143
ret.add("+++ $revisedFileName")
144144

145145
val patchDeltas = ArrayList(
146-
patch.deltas)
146+
patch.getDeltas())
147147

148148
// code outside the if block also works for single-delta issues.
149149
val deltas = ArrayList<AbstractDelta<String>>() // current

src/commonMain/kotlin/com/github/difflib/patch/Patch.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Patch<T> constructor(estimatedPatchSize: Int = 10) {
8282
*
8383
* @return the deltas
8484
*/
85-
fun getDeltas(): List<AbstractDelta<T>> {
85+
fun getDeltas(): MutableList<AbstractDelta<T>> {
8686
deltas.sortBy { d -> d.source.position }
8787
return deltas
8888
}

src/commonMain/kotlin/com/github/difflib/text/DiffRowGenerator.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class DiffRowGenerator private constructor(builder: Builder) {
9191
fun generateDiffRows(original: List<String>, patch: Patch<String>): List<DiffRow> {
9292
val diffRows = ArrayList<DiffRow>()
9393
var endPos = 0
94-
val deltaList = patch.deltas
94+
val deltaList = patch.getDeltas()
9595
for (delta in deltaList) {
9696
val orig = delta.source
9797
val rev = delta.target
@@ -165,7 +165,7 @@ class DiffRowGenerator private constructor(builder: Builder) {
165165
StringUtils.wrapText(newline, columnWidth))
166166
}
167167

168-
internal fun normalizeLines(list: List<String>): List<String> {
168+
fun normalizeLines(list: List<String>): List<String> {
169169
return list.map { lineNormalizer(it) }.toList()
170170
}
171171

@@ -186,7 +186,7 @@ class DiffRowGenerator private constructor(builder: Builder) {
186186
origList = inlineDiffSplitter(joinedOrig)
187187
revList = inlineDiffSplitter(joinedRev)
188188

189-
val inlineDeltas = DiffUtils.diff(origList, revList).deltas
189+
val inlineDeltas = DiffUtils.diff(origList, revList).getDeltas()
190190

191191
inlineDeltas.reverse()
192192
for (inlineDelta in inlineDeltas) {
@@ -225,8 +225,8 @@ class DiffRowGenerator private constructor(builder: Builder) {
225225
revResult.append(character)
226226
}
227227

228-
val original = origResult.toString().split("\n".toRegex()).dropLastWhile { it.isEmpty() }
229-
val revised = revResult.toString().split("\n".toRegex()).dropLastWhile { it.isEmpty() }
228+
val original = origResult.toString().lines()
229+
val revised = revResult.toString().lines()
230230
val diffRows = ArrayList<DiffRow>()
231231
for (j in 0 until max(original.size, revised.size)) {
232232
diffRows.add(buildDiffRowWithoutNormalizing(Tag.CHANGE,
@@ -432,11 +432,11 @@ class DiffRowGenerator private constructor(builder: Builder) {
432432
val results = SPLIT_PATTERN.findAll(str)
433433
var pos = 0
434434
for (result in results) {
435-
if (pos < result.range.start) {
436-
list.add(str.substring(pos, result.range.start))
435+
if (pos < result.range.first) {
436+
list.add(str.substring(pos, result.range.first))
437437
}
438-
list.add(result.groupValues.single())
439-
pos = result.range.endInclusive
438+
list.add(result.value)
439+
pos = result.range.last + 1
440440
}
441441
if (pos < str.length) {
442442
list.add(str.substring(pos))

src/commonMain/kotlin/com/github/difflib/text/StringUtils.kt

+7-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.github.difflib.text
1717

1818

19-
internal object StringUtils {
19+
object StringUtils {
2020

2121
/**
2222
* Replaces all opening an closing tags with `<` or `>`.
@@ -51,18 +51,16 @@ internal object StringUtils {
5151
return line
5252
}
5353
val length = line.length
54-
val delimiter = "<br/>".length
55-
var widthIndex = columnWidth
56-
57-
val b = StringBuilder(line)
54+
val b = StringBuilder()
5855

5956
var count = 0
60-
while (length > widthIndex) {
61-
b.append(widthIndex + delimiter * count, "<br/>")
62-
widthIndex += columnWidth
63-
count++
57+
while (count * columnWidth + columnWidth < length) {
58+
b.append(line.subSequence(columnWidth * count, columnWidth * ++count))
59+
b.append("<br/>")
6460
}
6561

62+
b.append(line.subSequence(columnWidth * count, length))
63+
6664
return b.toString()
6765
}
6866
}

src/commonMain/kotlin/com/github/difflib/unifieddiff/UnifiedDiff.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ class UnifiedDiff {
2929
private set
3030
internal val files = ArrayList<UnifiedDiffFile>()
3131

32-
internal fun addFile(file: UnifiedDiffFile) {
32+
fun addFile(file: UnifiedDiffFile) {
3333
files.add(file)
3434
}
3535

3636
fun getFiles(): List<UnifiedDiffFile> {
3737
return files
3838
}
3939

40-
internal fun setTailTxt(tailTxt: String) {
40+
fun setTailTxt(tailTxt: String) {
4141
this.tail = tailTxt
4242
}
4343

0 commit comments

Comments
 (0)