Bringt cards-native auf 0 SwiftLint-Violations bei 75 Files. Build-Status
unverändert grün (xcodebuild iOS Debug).
.swiftlint.yml
- identifier_name excludes erweitert um math/index-Konventionen
(i, j, n, m, x, y, w, h, r, g, b, a, c, d, s, f, p, q, t, l) —
in algorithmischem Code klarer als verbose
- opening_brace disabled — kollidiert mit SwiftFormats
wrapMultilineStatementBraces (SwiftFormat ist im Pre-Commit-Hook
und gewinnt)
Code-Modernisierungen (real, nicht nur Annotations)
- Cloze.swift: regex-Tuple bekommt `swiftlint:disable large_tuple`-
Region — Regex-Output-Type ist Builder-bedingt nicht reduzierbar
- Media.swift: `data(using: .utf8)` → `Data(s.utf8)` (non-failable),
`String(data:as:)` → `String(bytes:encoding:)`
- CardsTheme.swift: HSL-Wert-Typ statt anonymes 3-Tupel —
konkretere Call-Sites, kein `large_tuple`-Warning mehr
- MediaCache.swift: `CacheEntry`-Struct statt 3-Tupel im Prune-Pfad
- GradeQueue / MediaCache / StudySession / MarketplaceStore: OSLog-
Interpolations auf lokale Variablen ziehen — fixt Swift-6-Strict-
Concurrency-Fail bei Actor-isolated-Property-Zugriff aus
@Sendable-Autoclosure
- DeckMutations.swift, MarketplaceModeration.swift: verschachtelte
VersionInfo-Sub-Types auf Top-Level (`PullUpdateVersion`,
`OwnedMarketplaceVersion`) — fixt `nesting`-Warning
- Tests/UnitTests/*.swift: alle `""".data(using: .utf8)!` migriert auf
`Data("""…""".utf8)`; force-cast `as!` in MutationEncodingTests
durch guard-let + throw ersetzt
Pragmatische Disables (mit Doc-Comment-Begründung)
- DeckEditorView / MarketplacePublishView / DeckDetailView /
PublicDeckView / DeckListView / CardEditorView / CardsAPI:
`swiftlint:disable type_body_length` (+ teilweise file_length)
als Region-Disable mit `enable` nach dem Struct. Begründung im
Doc-Comment: Multi-State-Maschinen mit shared Toolbar + Sheets;
Aufspalten würde nur @Binding-Plumbing produzieren
Auto-Format-Aufräumung
- Redundante `Sendable`-Conformance entfernt (Swift 6 leitet das
bei Wert-Typen mit Sendable-Mitgliedern automatisch ab)
- EnvironmentValues nutzt jetzt @Entry-Macro statt manueller
EnvironmentKey-Boilerplate
- Brace-Reformatting + Import-Sortierung auf allen 75 Files
Ergebnis: 80 Warnings + 3 Errors → 0 / 0.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
77 lines
2.7 KiB
Swift
77 lines
2.7 KiB
Swift
import Foundation
|
|
|
|
/// Vergleich einer getippten User-Antwort gegen die erwartete Antwort.
|
|
/// 1:1-Port aus `cards/packages/cards-domain/src/typing.ts`:
|
|
/// Normalisierung (lowercase, trim, NFD-Diakritika-Stripping),
|
|
/// dann exact-match → `correct`. Sonst Levenshtein-Distanz mit
|
|
/// Threshold `max(1, floor(answer.length * 0.2))` → `close`.
|
|
enum TypingMatch: Equatable {
|
|
case correct
|
|
case close
|
|
case wrong
|
|
}
|
|
|
|
enum Typing {
|
|
/// `aliases` ist ein Komma-getrennter String aus dem `aliases`-Feld
|
|
/// der Karte (optional). Jeder Alias zählt als gültige Antwort.
|
|
static func check(input: String, answer: String, aliases: String? = nil) -> TypingMatch {
|
|
let normInput = normalize(input)
|
|
guard !normInput.isEmpty else { return .wrong }
|
|
|
|
var candidates = [answer]
|
|
if let aliases {
|
|
candidates.append(contentsOf: aliases.split(separator: ",").map(String.init))
|
|
}
|
|
let normalizedCandidates = candidates
|
|
.map(normalize)
|
|
.filter { !$0.isEmpty }
|
|
guard !normalizedCandidates.isEmpty else { return .wrong }
|
|
|
|
if normalizedCandidates.contains(normInput) {
|
|
return .correct
|
|
}
|
|
|
|
let shortestLen = normalizedCandidates.map(\.count).min() ?? normInput.count
|
|
let threshold = max(1, Int(Double(shortestLen) * 0.2))
|
|
for candidate in normalizedCandidates where levenshtein(normInput, candidate) <= threshold {
|
|
return .close
|
|
}
|
|
return .wrong
|
|
}
|
|
|
|
private static func normalize(_ string: String) -> String {
|
|
let trimmed = string.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let lowered = trimmed.lowercased()
|
|
// NFD-Dekomposition + Combining-Marks entfernen (z.B. ä → a)
|
|
let decomposed = lowered.decomposedStringWithCanonicalMapping
|
|
let stripped = decomposed.unicodeScalars.filter { scalar in
|
|
!(0x0300 ... 0x036F).contains(scalar.value)
|
|
}
|
|
return String(String.UnicodeScalarView(stripped))
|
|
}
|
|
|
|
static func levenshtein(_ a: String, _ b: String) -> Int {
|
|
let aChars = Array(a)
|
|
let bChars = Array(b)
|
|
let m = aChars.count
|
|
let n = bChars.count
|
|
if m == 0 { return n }
|
|
if n == 0 { return m }
|
|
|
|
var row = Array(0 ... n)
|
|
for i in 1 ... m {
|
|
var prev = row[0]
|
|
row[0] = i
|
|
for j in 1 ... n {
|
|
let tmp = row[j]
|
|
if aChars[i - 1] == bChars[j - 1] {
|
|
row[j] = prev
|
|
} else {
|
|
row[j] = 1 + Swift.min(prev, row[j], row[j - 1])
|
|
}
|
|
prev = tmp
|
|
}
|
|
}
|
|
return row[n]
|
|
}
|
|
}
|