Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Packages/AstraCore/Sources/CodeDiff.swift10// Module : Packages11// Repository : https://git.spboucher.ai/orix-astra12// Created : 2026-11-2213// Last Modified : 2026-11-2214//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : Proprietary, all rights reserved (see license file)80// License File : LICENSE81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation8990/// A line-based diff between two pieces of code, for the "compare" view. Pure and bounded: inputs above the line limit are91/// compared coarsely instead of spending quadratic time.92public enum CodeDiff {93 public enum Kind: String, Sendable, Hashable, Codable { case same, removed, added }9495 public struct Line: Sendable, Hashable, Identifiable {96 public var id: Int97 public var kind: Kind98 public var text: String99 /// 1-based line numbers in the old and the new text; nil on the side where the line does not exist.100 public var oldNumber: Int?101 public var newNumber: Int?102 }103104 public struct Summary: Sendable, Hashable {105 public var added: Int106 public var removed: Int107 public var unchanged: Int108 public var isIdentical: Bool { added == 0 && removed == 0 }109 public var label: String {110 isIdentical ? "No differences" : "+\(added) −\(removed) · \(unchanged) unchanged"111 }112 }113114 public static let lineLimit = 2_000115116 /// Longest-common-subsequence diff over lines. Trailing whitespace is ignored when comparing, kept when displaying.117 public static func lines(old: String, new: String) -> [Line] {118 let a = split(old), b = split(new)119 guard a.count <= lineLimit, b.count <= lineLimit else { return coarse(a, b) }120 let ka = a.map(key), kb = b.map(key)121 // trim the common prefix and suffix first: most edits are local, and this keeps the table small122 var start = 0123 while start < ka.count, start < kb.count, ka[start] == kb[start] { start += 1 }124 var endA = ka.count, endB = kb.count125 while endA > start, endB > start, ka[endA - 1] == kb[endB - 1] { endA -= 1; endB -= 1 }126 let n = endA - start, m = endB - start127 var table = [[Int32]](repeating: [Int32](repeating: 0, count: m + 1), count: n + 1)128 if n > 0, m > 0 {129 for i in stride(from: n - 1, through: 0, by: -1) {130 for j in stride(from: m - 1, through: 0, by: -1) {131 table[i][j] = ka[start + i] == kb[start + j] ? table[i + 1][j + 1] + 1 : max(table[i + 1][j], table[i][j + 1])132 }133 }134 }135 var out: [Line] = []136 func push(_ kind: Kind, _ text: String, _ o: Int?, _ nw: Int?) { out.append(Line(id: out.count, kind: kind, text: text, oldNumber: o, newNumber: nw)) }137 for i in 0..<start { push(.same, b[i], i + 1, i + 1) }138 var i = 0, j = 0139 while i < n, j < m {140 if ka[start + i] == kb[start + j] { push(.same, b[start + j], start + i + 1, start + j + 1); i += 1; j += 1 }141 else if table[i + 1][j] >= table[i][j + 1] { push(.removed, a[start + i], start + i + 1, nil); i += 1 }142 else { push(.added, b[start + j], nil, start + j + 1); j += 1 }143 }144 while i < n { push(.removed, a[start + i], start + i + 1, nil); i += 1 }145 while j < m { push(.added, b[start + j], nil, start + j + 1); j += 1 }146 for k in 0..<(ka.count - endA) { push(.same, b[endB + k], endA + k + 1, endB + k + 1) }147 return out148 }149150 public static func summary(_ lines: [Line]) -> Summary {151 Summary(added: lines.filter { $0.kind == .added }.count, removed: lines.filter { $0.kind == .removed }.count,152 unchanged: lines.filter { $0.kind == .same }.count)153 }154155 private static func split(_ text: String) -> [String] {156 var parts = text.replacingOccurrences(of: "\r\n", with: "\n").split(separator: "\n", omittingEmptySubsequences: false).map(String.init)157 if parts.last == "" { parts.removeLast() }158 return parts159 }160161 private static func key(_ line: String) -> String {162 var s = Substring(line)163 while let last = s.last, last == " " || last == "\t" { s.removeLast() }164 return String(s)165 }166167 /// Fallback for very large inputs: everything old is shown as removed and everything new as added.168 private static func coarse(_ a: [String], _ b: [String]) -> [Line] {169 var out: [Line] = []170 for (i, l) in a.enumerated() { out.append(Line(id: out.count, kind: .removed, text: l, oldNumber: i + 1, newNumber: nil)) }171 for (j, l) in b.enumerated() { out.append(Line(id: out.count, kind: .added, text: l, oldNumber: nil, newNumber: j + 1)) }172 return out173 }174}175