Repository Public
1import Foundation2import SheetCore34/// Helpers shared by the conditional-format, validation and table code.5enum AuditRuleSupport {6 /// Parses formula text with or without a leading "=".7 static func parse(_ text: String) -> Expr? {8 let t = text.trimmingCharacters(in: .whitespaces)9 guard !t.isEmpty else { return nil }10 return try? FormulaParser.parse(t)11 }1213 /// Shifts the relative parts of every reference, as copying the formula by (dRow, dCol) would.14 static func shifted(_ e: Expr, dRow: Int, dCol: Int, sheetName: String) -> Expr {15 if dRow == 0 && dCol == 0 { return e }16 return RefTransform.shift(dRow: dRow, dCol: dCol).apply(to: e, formulaSheet: sheetName)17 }1819 /// True when the expression contains only literals and operators: its value cannot depend on the cell.20 static func isConstant(_ e: Expr) -> Bool {21 var constant = true22 e.walk { node in23 switch node {24 case .cell, .range, .name, .structured, .call, .invoke, .spill, .implicit: constant = false25 default: break26 }27 }28 return constant29 }3031 /// True when every reference is fully absolute and no function is called: the value is the same for every cell.32 static func isPositionIndependent(_ e: Expr) -> Bool {33 var independent = true34 e.walk { node in35 switch node {36 case .cell(_, let a): if !(a.absRow && a.absCol) { independent = false }37 case .range(let r):38 let rowsFixed = r.kind == .columns || (r.a.absRow && r.b.absRow)39 let colsFixed = r.kind == .rows || (r.a.absCol && r.b.absCol)40 if !(rowsFixed && colsFixed) { independent = false }41 case .call, .invoke, .structured, .spill, .implicit: independent = false42 default: break43 }44 }45 return independent46 }4748 /// Spreadsheet truth of a rule formula result: TRUE or a non-zero number. Text, blanks and errors are false.49 static func truthy(_ v: Value) -> Bool {50 switch v.scalar {51 case .bool(let b): return b52 case .number(let n): return n != 053 default: return false54 }55 }5657 /// PERCENTILE.INC on ascending data: rank h = p·(n − 1), linear interpolation between neighbours.58 static func percentileInc(_ sorted: [Double], _ p: Double) -> Double? {59 guard !sorted.isEmpty, p >= 0, p <= 1 else { return nil }60 let h = p * Double(sorted.count - 1)61 let lo = Int(h.rounded(.down)), hi = min(lo + 1, sorted.count - 1)62 return sorted[lo] + (h - Double(lo)) * (sorted[hi] - sorted[lo])63 }6465 /// Identity of a value for duplicate detection: numbers by value, text case-insensitively,66 /// booleans by value. Blanks and errors have no key.67 static func key(_ v: Value) -> String? {68 switch v {69 case .number(let n): return "n" + String((n == 0 ? 0 : n).bitPattern, radix: 16)70 case .text(let s): return "t" + s.lowercased()71 case .bool(let b): return b ? "b1" : "b0"72 default: return nil73 }74 }7576 /// Text shown for a value under the General format ("" for blanks, the code for errors).77 static func display(_ v: Value) -> String { v.scalar.description }7879 /// Applies a comparison operator named as in OOXML ("between", "greaterThan"…) using the80 /// spreadsheet ordering (`compareValues`). Returns nil for an unknown operator or a missing operand.81 static func compare(_ op: String, _ v: Value, _ a: Value?, _ b: Value?) -> Bool? {82 guard let a else { return nil }83 switch op {84 case "equal": return compareValues(v, a) == 085 case "notEqual": return compareValues(v, a) != 086 case "greaterThan": return compareValues(v, a) > 087 case "lessThan": return compareValues(v, a) < 088 case "greaterThanOrEqual": return compareValues(v, a) >= 089 case "lessThanOrEqual": return compareValues(v, a) <= 090 case "between", "notBetween":91 guard let b else { return nil }92 let (lo, hi) = compareValues(a, b) <= 0 ? (a, b) : (b, a)93 let inside = compareValues(v, lo) >= 0 && compareValues(v, hi) <= 094 return op == "between" ? inside : !inside95 default: return nil96 }97 }9899 /// Numeric version of `compare` for validation.100 static func compare(_ op: String, _ v: Double, _ a: Double, _ b: Double?) -> Bool? {101 switch op {102 case "equal": return v == a103 case "notEqual": return v != a104 case "greaterThan": return v > a105 case "lessThan": return v < a106 case "greaterThanOrEqual": return v >= a107 case "lessThanOrEqual": return v <= a108 case "between", "notBetween":109 guard let b else { return nil }110 let inside = v >= min(a, b) && v <= max(a, b)111 return op == "between" ? inside : !inside112 default: return nil113 }114 }115116 /// Linear interpolation between two colours, channel by channel (alpha included), rounded to the nearest integer.117 static func mix(_ a: RGBA, _ b: RGBA, _ f: Double) -> RGBA {118 func ch(_ x: UInt8, _ y: UInt8) -> UInt8 {119 UInt8(max(0, min(255, (Double(x) + f * (Double(y) - Double(x))).rounded())))120 }121 return RGBA(r: ch(a.r, b.r), g: ch(a.g, b.g), b: ch(a.b, b.b), a: ch(a.a, b.a))122 }123124 /// Spreadsheet sort rank of a value: numbers, then text, then booleans, then errors; blanks last.125 static func sortRank(_ v: Value) -> Int {126 switch v {127 case .number: return 0128 case .text: return 1129 case .bool: return 2130 case .error: return 3131 default: return 4132 }133 }134}135