Skip to content

Commit 04efa7a

Browse files
committed
refactor: rework MyersDiff.buildRevision to avoid unnecessary non-null assertion
1 parent 473c3aa commit 04efa7a

File tree

1 file changed

+9
-3
lines changed
  • src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers

1 file changed

+9
-3
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,19 @@ public class MyersDiff<T>(private val equalizer: (T, T) -> Boolean = { t1, t2 ->
122122

123123
val changes = ArrayList<Change>()
124124

125-
// TODO: this can be improved to avoid the non-null assertion on prev
126-
while (path?.prev != null && path.prev!!.j >= 0) {
125+
while (path != null) {
126+
val prevPath = path.prev
127+
128+
if (prevPath == null || prevPath.j < 0) {
129+
break
130+
}
131+
127132
check(!path.snake) { "Bad diffpath: found snake when looking for diff" }
128133

129134
val i = path.i
130135
val j = path.j
131-
path = path.prev ?: error("Expected a non-null previous path node")
136+
137+
path = prevPath
132138

133139
val iAnchor = path.i
134140
val jAnchor = path.j

0 commit comments

Comments
 (0)