import Foundation /// Input and output normalisation from `docs/TRANSLATION_OUTPUT.md` §2 and §5. public enum TextNormalization { /// N1–N3: line endings to `\n`, strip BOM/NUL/C0 controls (except tab and newline), /// strip U+200B, keep ZWJ/ZWNJ, NFC, then trim outer whitespace (N2). public static func normalizeInput(_ text: String) -> String { let unified = unifyLineEndings(text) let filtered = unified.unicodeScalars.filter { scalar in switch scalar.value { case 0, 0xFEFF, 0x200B: return false default: break } if scalar.properties.generalCategory == .control { return scalar.value == 0x09 || scalar.value == 0x0A } return true } let cleaned = String(String.UnicodeScalarView(filtered)) return cleaned.precomposedStringWithCanonicalMapping.trimmingCharacters(in: .whitespacesAndNewlines) } /// T1: strip NUL, normalise line endings, trim outer whitespace. Internal text is untouched. public static func normalizeOutput(_ text: String) -> String { unifyLineEndings(text) .replacingOccurrences(of: "\0", with: "") .trimmingCharacters(in: .whitespacesAndNewlines) } /// Comparison normalisation for the double-copy rule: line endings and outer trimming only. /// Internal whitespace is deliberately preserved. public static func normalizeForComparison(_ text: String) -> String { unifyLineEndings(text).trimmingCharacters(in: .whitespacesAndNewlines) } private static func unifyLineEndings(_ text: String) -> String { text .replacingOccurrences(of: "\r\n", with: "\n") .replacingOccurrences(of: "\r", with: "\n") } }