Inkspring / app · inkspring.ai / native-mac / Sources / InkspringSchedule / NewsletterMerge.swift
| 1 | import Foundation |
| 2 | |
| 3 | public enum NewsletterMerge { |
| 4 | /// Merge changes on separate lines and preserve overlapping changes for manual resolution. |
| 5 | public static func merge(base: String, mac: String, mobile: String) -> String? { |
| 6 | if mac == base || mac == mobile { return mobile } |
| 7 | if mobile == base { return mac } |
| 8 | |
| 9 | let original = base.components(separatedBy: "\n") |
| 10 | let macLines = mac.components(separatedBy: "\n") |
| 11 | let mobileLines = mobile.components(separatedBy: "\n") |
| 12 | guard max(original.count, macLines.count, mobileLines.count) <= 4_000 else { return nil } |
| 13 | let macEdits = edits(to: macLines, from: original) |
| 14 | let mobileEdits = edits(to: mobileLines, from: original) |
| 15 | var edits = macEdits |
| 16 | var macIndex = 0 |
| 17 | for mobileEdit in mobileEdits { |
| 18 | while macIndex < macEdits.count, macEdits[macIndex].range.upperBound < mobileEdit.range.lowerBound { |
| 19 | macIndex += 1 |
| 20 | } |
| 21 | var duplicate = false |
| 22 | for macEdit in macEdits[macIndex...] { |
| 23 | if macEdit.range.lowerBound > mobileEdit.range.upperBound { break } |
| 24 | if mobileEdit == macEdit { |
| 25 | duplicate = true |
| 26 | } else if mobileEdit.overlaps(macEdit) { |
| 27 | return nil |
| 28 | } |
| 29 | } |
| 30 | if !duplicate { edits.append(mobileEdit) } |
| 31 | } |
| 32 | var result: [String] = [] |
| 33 | var cursor = 0 |
| 34 | for edit in edits.sorted(by: { |
| 35 | ($0.range.lowerBound, $0.range.upperBound) < ($1.range.lowerBound, $1.range.upperBound) |
| 36 | }) { |
| 37 | result.append(contentsOf: original[cursor..<edit.range.lowerBound]) |
| 38 | result.append(contentsOf: edit.replacement) |
| 39 | cursor = edit.range.upperBound |
| 40 | } |
| 41 | result.append(contentsOf: original[cursor...]) |
| 42 | return result.joined(separator: "\n") |
| 43 | } |
| 44 | |
| 45 | private struct Edit: Equatable { |
| 46 | var range: Range<Int> |
| 47 | var replacement: [String] |
| 48 | |
| 49 | func overlaps(_ other: Self) -> Bool { |
| 50 | if range.isEmpty && other.range.isEmpty { return range.lowerBound == other.range.lowerBound } |
| 51 | if range.isEmpty { return other.range.lowerBound < range.lowerBound && range.lowerBound < other.range.upperBound } |
| 52 | if other.range.isEmpty { return other.overlaps(self) } |
| 53 | return range.overlaps(other.range) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private static func edits(to changed: [String], from original: [String]) -> [Edit] { |
| 58 | let difference = changed.difference(from: original) |
| 59 | let removals = Set(difference.removals.compactMap { change -> Int? in |
| 60 | guard case let .remove(offset, _, _) = change else { return nil } |
| 61 | return offset |
| 62 | }) |
| 63 | let insertions = Set(difference.insertions.compactMap { change -> Int? in |
| 64 | guard case let .insert(offset, _, _) = change else { return nil } |
| 65 | return offset |
| 66 | }) |
| 67 | var result: [Edit] = [] |
| 68 | var oldIndex = 0 |
| 69 | var newIndex = 0 |
| 70 | while oldIndex < original.count || newIndex < changed.count { |
| 71 | let start = oldIndex |
| 72 | let newStart = newIndex |
| 73 | while removals.contains(oldIndex) { oldIndex += 1 } |
| 74 | while insertions.contains(newIndex) { newIndex += 1 } |
| 75 | if oldIndex != start || newIndex != newStart { |
| 76 | result.append(Edit(range: start..<oldIndex, replacement: Array(changed[newStart..<newIndex]))) |
| 77 | } |
| 78 | if oldIndex == original.count && newIndex == changed.count { break } |
| 79 | oldIndex += 1 |
| 80 | newIndex += 1 |
| 81 | } |
| 82 | return result |
| 83 | } |
| 84 | } |