Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatKnowledge/Sources/DelimitedText.swift3// Description: RFC 4180 parser (quoted fields, escaped quotes, embedded newlines) with delimiter sniffing, and the "header: value" rendering used for retrieval.4import Foundation56public enum DelimitedText {7 /// Picks the delimiter whose count is the most consistent over the first records (outside quotes).8 public static func sniffDelimiter(_ text: String, candidates: [Character] = [",", ";", "\t", "|"]) -> Character {9 var best: Character = ","; var bestScore = 010 for c in candidates {11 var counts: [Int] = []; var current = 0; var quoted = false; var lines = 012 for ch in text {13 if ch == "\"" { quoted.toggle() }14 else if !quoted && ch == c { current += 1 }15 else if !quoted && ch.isNewline { counts.append(current); current = 0; lines += 1; if lines >= 25 { break } }16 }17 if lines < 25, current > 0 { counts.append(current) }18 let nonEmpty = counts.filter { $0 > 0 }19 guard let minimum = nonEmpty.min(), !nonEmpty.isEmpty else { continue }20 let consistent = nonEmpty.filter { $0 == minimum }.count21 // consistency first, then how many columns it yields22 let score = consistent * 1000 / max(1, counts.count) * 100 + min(99, minimum)23 if score > bestScore { bestScore = score; best = c }24 }25 return best26 }2728 /// Returns the records and whether a quoted field was left open at the end of the input.29 public static func parse(_ text: String, delimiter: Character) -> (rows: [[String]], unterminatedQuote: Bool) {30 var rows: [[String]] = []; var row: [String] = []; var field = ""; var quoted = false; var fieldWasQuoted = false31 var it = text.makeIterator(); var pending: Character? = nil32 func next() -> Character? { if let p = pending { pending = nil; return p }; return it.next() }33 func endRow() {34 row.append(field); field = ""; fieldWasQuoted = false35 if !(row.count == 1 && row[0].isEmpty) { rows.append(row) }36 row = []37 }38 while let ch = next() {39 if quoted {40 if ch == "\"" {41 if let peek = it.next() { if peek == "\"" { field.append("\"") } else { quoted = false; pending = peek } } else { quoted = false }42 } else { field.append(ch) }43 } else if ch == "\"" && field.isEmpty && !fieldWasQuoted { quoted = true; fieldWasQuoted = true }44 else if ch == delimiter { row.append(field); field = ""; fieldWasQuoted = false }45 else if ch.isNewline { endRow() } // "\r\n" is one Character46 else { field.append(ch) }47 }48 let open = quoted49 if !field.isEmpty || !row.isEmpty { endRow() }50 return (rows, open)51 }5253 /// One paragraph per record: "Row n" then "header: value" lines (empty values skipped).54 public static func render(rows: [[String]]) -> String {55 guard let header = rows.first else { return "" }56 if rows.count == 1 { return header.joined(separator: " | ") }57 let names = header.enumerated().map { $1.trimmingCharacters(in: .whitespaces).isEmpty ? "column \($0 + 1)" : $1.trimmingCharacters(in: .whitespaces) }58 var out: [String] = []; out.reserveCapacity(rows.count)59 for (n, record) in rows.dropFirst().enumerated() {60 var lines = ["Row \(n + 1)"]61 for (i, value) in record.enumerated() {62 let v = value.trimmingCharacters(in: .whitespacesAndNewlines)63 if v.isEmpty { continue }64 let flat = v.split(whereSeparator: \.isNewline).map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }.joined(separator: " / ")65 lines.append("\(i < names.count ? names[i] : "column \(i + 1)"): \(flat)")66 }67 if lines.count > 1 { out.append(lines.joined(separator: "\n")) }68 }69 return out.joined(separator: "\n\n")70 }71}72