|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Darstellung von Anweisung vs. Ein-/Ausgabe im Inspector (Modus-Picker). |
| 4 | +public enum VisuStructStatementIOMode: String, CaseIterable, Hashable, Sendable { |
| 5 | + case statement |
| 6 | + case input |
| 7 | + case output |
| 8 | +} |
| 9 | + |
| 10 | +/// Textumformungen für den Zeileneditor im Inspector (ohne UI, daher testbar). |
| 11 | +public enum VisuStructLineEditorText { |
| 12 | + public static func ioMode(of text: String) -> VisuStructStatementIOMode { |
| 13 | + let t = text.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() |
| 14 | + if t.hasPrefix("input:") { return .input } |
| 15 | + if t.hasPrefix("output:") { return .output } |
| 16 | + if t.hasPrefix("print:") { return .output } |
| 17 | + return .statement |
| 18 | + } |
| 19 | + |
| 20 | + /// Entfernt `input:` / `output:` / `print:` und umgebende Leerzeichen; ohne Präfix bleibt der Text unverändert. |
| 21 | + public static func strippingIOPrefix(from s: String) -> String { |
| 22 | + let trimmed = s.trimmingCharacters(in: .whitespacesAndNewlines) |
| 23 | + let lower = trimmed.lowercased() |
| 24 | + if lower.hasPrefix("input:") { |
| 25 | + return String(trimmed.dropFirst(6)).trimmingCharacters(in: .whitespacesAndNewlines) |
| 26 | + } |
| 27 | + if lower.hasPrefix("output:") { |
| 28 | + return String(trimmed.dropFirst(7)).trimmingCharacters(in: .whitespacesAndNewlines) |
| 29 | + } |
| 30 | + if lower.hasPrefix("print:") { |
| 31 | + return String(trimmed.dropFirst(6)).trimmingCharacters(in: .whitespacesAndNewlines) |
| 32 | + } |
| 33 | + return s |
| 34 | + } |
| 35 | + |
| 36 | + /// Text nach Wechsel im Modus-Picker: Präfix tauschen, Inhalt behalten. |
| 37 | + public static func applyingIOMode(_ mode: VisuStructStatementIOMode, to text: String) -> String { |
| 38 | + let stripped = strippingIOPrefix(from: text) |
| 39 | + switch mode { |
| 40 | + case .statement: |
| 41 | + return stripped |
| 42 | + case .input: |
| 43 | + return prefixing("input:", to: stripped) |
| 44 | + case .output: |
| 45 | + return prefixing("output:", to: stripped) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// Wie `strippingIOPrefix` bzw. Schlüsselwort-Entfernung, aber Leerzeichen beim Tippen bleiben erhalten. |
| 50 | + public static func strippingPrefixForEditing(_ p: String, from s: String) -> String { |
| 51 | + if p == "input:" || p == "output:" { |
| 52 | + return strippingIOPrefixPreservingEditingWhitespace(from: s) |
| 53 | + } |
| 54 | + let stripped = strippingKeywordPrefixPreservingEditingWhitespace(p, from: s) |
| 55 | + if p == "while", stripped == s { |
| 56 | + let legacy = strippingKeywordPrefixPreservingEditingWhitespace("until", from: s) |
| 57 | + if legacy != s { return legacy } |
| 58 | + } |
| 59 | + return stripped |
| 60 | + } |
| 61 | + |
| 62 | + public static func prefixingForEditing(_ p: String, to content: String) -> String { |
| 63 | + if content.isEmpty { return "\(p) " } |
| 64 | + return "\(p) \(content)" |
| 65 | + } |
| 66 | + |
| 67 | + /// iOS/macOS ersetzen beim Tippen teils typografische Anführungszeichen; für Code immer gerade Quotes. |
| 68 | + public static func normalizingQuotes(_ s: String) -> String { |
| 69 | + s |
| 70 | + .replacingOccurrences(of: "“", with: "\"") |
| 71 | + .replacingOccurrences(of: "”", with: "\"") |
| 72 | + .replacingOccurrences(of: "„", with: "\"") |
| 73 | + .replacingOccurrences(of: "«", with: "\"") |
| 74 | + .replacingOccurrences(of: "»", with: "\"") |
| 75 | + .replacingOccurrences(of: "’", with: "'") |
| 76 | + .replacingOccurrences(of: "‘", with: "'") |
| 77 | + .replacingOccurrences(of: "‚", with: "'") |
| 78 | + .replacingOccurrences(of: "´", with: "'") |
| 79 | + } |
| 80 | + |
| 81 | + /// Genau drei Felder (Start, Bedingung, Schritt) der Zählschleife. |
| 82 | + public static func paddedForLoopParts(_ lines: [String]) -> [String] { |
| 83 | + var a = lines.isEmpty ? [""] : lines |
| 84 | + while a.count < 3 { a.append("") } |
| 85 | + if a.count > 3 { return Array(a.prefix(3)) } |
| 86 | + return a |
| 87 | + } |
| 88 | + |
| 89 | + /// Ein Feld der Zählschleife: gerade Quotes, ohne abschließende `;`. |
| 90 | + public static func normalizedForLoopPart(_ s: String) -> String { |
| 91 | + var x = normalizingQuotes(s) |
| 92 | + while true { |
| 93 | + let trimmedEnd = x.trimmingCharacters(in: .whitespacesAndNewlines) |
| 94 | + guard trimmedEnd.hasSuffix(";") else { return x } |
| 95 | + x = String(trimmedEnd.dropLast()).trimmingCharacters(in: .whitespacesAndNewlines) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static func prefixing(_ p: String, to content: String) -> String { |
| 100 | + let c = content.trimmingCharacters(in: .whitespacesAndNewlines) |
| 101 | + if c.isEmpty { return "\(p) " } |
| 102 | + return "\(p) \(c)" |
| 103 | + } |
| 104 | + |
| 105 | + private static func strippingIOPrefixPreservingEditingWhitespace(from s: String) -> String { |
| 106 | + let leadingTrimmed = String(s.drop(while: { $0.isWhitespace })) |
| 107 | + let lower = leadingTrimmed.lowercased() |
| 108 | + if lower.hasPrefix("input:") { |
| 109 | + return droppingOneLeadingWhitespace(String(leadingTrimmed.dropFirst(6))) |
| 110 | + } |
| 111 | + if lower.hasPrefix("output:") { |
| 112 | + return droppingOneLeadingWhitespace(String(leadingTrimmed.dropFirst(7))) |
| 113 | + } |
| 114 | + if lower.hasPrefix("print:") { |
| 115 | + return droppingOneLeadingWhitespace(String(leadingTrimmed.dropFirst(6))) |
| 116 | + } |
| 117 | + return s |
| 118 | + } |
| 119 | + |
| 120 | + private static func strippingKeywordPrefixPreservingEditingWhitespace(_ keyword: String, from s: String) -> String { |
| 121 | + let leadingTrimmed = String(s.drop(while: { $0.isWhitespace })) |
| 122 | + let lower = leadingTrimmed.lowercased() |
| 123 | + let key = keyword.lowercased() |
| 124 | + guard lower.hasPrefix(key) else { return s } |
| 125 | + |
| 126 | + let afterKeyword = leadingTrimmed.dropFirst(keyword.count) |
| 127 | + guard afterKeyword.isEmpty || afterKeyword.first?.isWhitespace == true || afterKeyword.first == "(" else { |
| 128 | + return s |
| 129 | + } |
| 130 | + |
| 131 | + let content = droppingOneLeadingWhitespace(String(afterKeyword)) |
| 132 | + if content.hasPrefix("("), content.hasSuffix(")"), content.count >= 2 { |
| 133 | + return String(content.dropFirst().dropLast()) |
| 134 | + } |
| 135 | + return content |
| 136 | + } |
| 137 | + |
| 138 | + private static func droppingOneLeadingWhitespace(_ s: String) -> String { |
| 139 | + guard let first = s.first, first.isWhitespace else { return s } |
| 140 | + return String(s.dropFirst()) |
| 141 | + } |
| 142 | +} |
0 commit comments