diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt index 71c8c34..5d83ac9 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt @@ -64,28 +64,29 @@ public class MyersDiffWithLinearSpace( ++i ++j } else { + // Remember size can be less than 0 here + val size = data.script.size - 1 + // TODO: compress these commands if (end1 - start1 > end2 - start2) { - if (data.script.isEmpty() || - data.script[data.script.size - 1].endOriginal != i || - data.script[data.script.size - 1].deltaType != DeltaType.DELETE + if (size < 1 || + data.script[size].endOriginal != i || + data.script[size].deltaType != DeltaType.DELETE ) { data.script.add(Change(DeltaType.DELETE, i, i + 1, j, j)) } else { - data.script[data.script.size - 1] = - data.script[data.script.size - 1].copy(endOriginal = i + 1) + data.script[size] = data.script[size].copy(endOriginal = i + 1) } ++i } else { - if (data.script.isEmpty() || - data.script[data.script.size - 1].endRevised != j || - data.script[data.script.size - 1].deltaType != DeltaType.INSERT + if (size < 1 || + data.script[size].endRevised != j || + data.script[size].deltaType != DeltaType.INSERT ) { data.script.add(Change(DeltaType.INSERT, i, i, j, j + 1)) } else { - data.script[data.script.size - 1] = - data.script[data.script.size - 1].copy(endRevised = j + 1) + data.script[size] = data.script[size].copy(endRevised = j + 1) } ++j