import Foundation public enum LineDiff { /// Largest LCS matrix built in one go (4M cells); bigger regions are split on unique anchor lines. public static let maxCells = 4_000_000 /// "" is zero lines, and one trailing "\n" is dropped. public static func lines(_ text: String) -> [String] { if text.isEmpty { return [] } return droppingTrailingNewline(text).components(separatedBy: "\n") } /// Drops one final "\n". `hasSuffix("\n")` misses it after a "\r", where "\r\n" is one Character. static func droppingTrailingNewline(_ text: String) -> String { text.utf8.last == 10 ? String(decoding: text.utf8.dropLast(), as: UTF8.self) : text } /// Line-based LCS diff of two texts into rows, both sides and aligned split rows. /// /// On a tie the DP takes the deletion before the addition; flipping that reorders /// removed and added lines in every view. public static func build(original: String, updated: String, status: GitStatus? = nil) -> Diff { let a = lines(original) let b = lines(updated) let (ia, ib) = intern(a, b) var ops: [Op] = [] ops.reserveCapacity(max(a.count, b.count)) region(ia, ib, 0, ia.count, 0, ib.count, &ops) var rows: [DiffRow] = [] rows.reserveCapacity(ops.count) var removedLines = Set(), addedLines = Set() for op in ops { switch op { case let .same(i, j): rows.append(DiffRow(sign: .same, oldNo: i + 1, newNo: j + 1, text: a[i])) case let .del(i): rows.append(DiffRow(sign: .removed, oldNo: i + 1, newNo: nil, text: a[i])) removedLines.insert(i) case let .add(j): rows.append(DiffRow(sign: .added, oldNo: nil, newNo: j + 1, text: b[j])) addedLines.insert(j) } } let left = a.enumerated().map { SideLine(no: $0.offset + 1, text: $0.element, mark: removedLines.contains($0.offset) ? .removed : nil) } let right = b.enumerated().map { SideLine(no: $0.offset + 1, text: $0.element, mark: addedLines.contains($0.offset) ? .added : nil) } return Diff( rows: rows, left: left, right: right, split: splitRows(ops, a, b), added: addedLines.count, removed: removedLines.count, isDeletedFile: status == .deleted, isAddedFile: status == .added, original: original, updated: updated ) } private enum Op { case same(Int, Int) case del(Int) case add(Int) } /// Maps every distinct line to an integer so the DP compares ints, not strings. private static func intern(_ a: [String], _ b: [String]) -> ([Int], [Int]) { var ids: [String: Int] = [:] func id(_ line: String) -> Int { if let known = ids[line] { return known } let next = ids.count ids[line] = next return next } return (a.map(id), b.map(id)) } /// Removed and added runs between two unchanged lines pair up side by side. private static func splitRows(_ ops: [Op], _ a: [String], _ b: [String]) -> [SplitRow] { var split: [SplitRow] = [] var removed: [SideLine] = [], added: [SideLine] = [] func flush() { for k in 0.. a0 && b1 > b0 && a[a1 - 1] == b[b1 - 1] { a1 -= 1 b1 -= 1 tail.append(.same(a1, b1)) } let n = a1 - a0, m = b1 - b0 func replaceAll() { for i in a0..= dp[i * width + j + 1] { ops.append(.del(a0 + i)) i += 1 } else { ops.append(.add(b0 + j)) j += 1 } } while i < n { ops.append(.del(a0 + i)); i += 1 } while j < m { ops.append(.add(b0 + j)); j += 1 } } /// Lines that appear exactly once on each side, in an order both sides share. private static func anchors(_ a: [Int], _ b: [Int], _ a0: Int, _ a1: Int, _ b0: Int, _ b1: Int) -> [(Int, Int)] { var countA: [Int: Int] = [:], countB: [Int: Int] = [:], atB: [Int: Int] = [:] for i in a0.. [(Int, Int)] { var tails: [Int] = [] var prev = [Int](repeating: -1, count: pairs.count) for k in pairs.indices { let v = pairs[k].1 var lo = 0, hi = tails.count while lo < hi { let mid = (lo + hi) / 2 if pairs[tails[mid]].1 < v { lo = mid + 1 } else { hi = mid } } prev[k] = lo > 0 ? tails[lo - 1] : -1 if lo == tails.count { tails.append(k) } else { tails[lo] = k } } var out: [(Int, Int)] = [] var k = tails.last ?? -1 while k >= 0 { out.append(pairs[k]) k = prev[k] } return out.reversed() } }