hex/ios/HEXShared/HexWords/WordReplacementsSettings.swift
Ryan Vogel f21a71a31d iOS: read, merge and write Hex words files (.hexwords), with the shared cases
Adds ios/HEXShared/HexWords/, the iPhone's implementation of
docs/specs/hex-words.md:

- HexWordsCheck: the spec's check pass, one non-recursive walk over the
  bytes before JSONSerialization: more than 8 levels (tooDeep), a repeated
  key compared byte for byte after decoding escapes (duplicateKey), and
  everything strict RFC 8259 forbids that JSONSerialization lets through,
  escaped lone surrogates included (notJSON).
- HexWordsReader: size, BOM, UTF-8 (checked by a lossless round trip),
  empty, check pass, then JSONSerialization. Objects stay NSDictionary,
  because bridging keys that differ only in normalization into one Swift
  dictionary would trap. Booleans are told from numbers by CFBoolean, so
  "learned": 1 is badField and "version": true is badVersion. Lengths
  count unicodeScalars, letters are general category L or N, whitespace
  is the spec's exact list, and the key lowercases scalar by scalar then
  maps final sigma to sigma.
- HexWordsMerge: the phone column of the merge table.
- HexWordsWriter: the phone's export, by the reader's own rules and order.
- WordReplacementsSettings and HexWordsImport (Add and Undo) in the App
  Group.

HexWordsSharedCasesTests loads tests/fixtures/hex-words in place (a folder
resource in project.yml) and runs every read case, the phone's merge and
write cases (the desktop's are the desktop's), Undo after each merge, and
a small JSON Schema check of what the writer writes.

Checked on anomastudio (iOS 27.2 simulator, simlanes): read.json 54/54,
merge.json 11/11 phone cases, write.json 2/2 phone cases.
2026-11-24 18:11:43 -05:00

80 lines
3.5 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
/// Word replacements (Settings › Dictation › Custom vocabulary): heard words and what to write
/// instead, in the shared App Group so in-keyboard dictation applies them too. They apply only
/// while Custom vocabulary is on, before the learned dictation fixes, and win over a learned fix
/// with the same heard words.
nonisolated struct WordReplacementsSettings {
static let maxCount = 1000
/// Heard phrases match up to 8 words, the words file's limit.
static let maxWords = HexWordsReader.Limit.words
private enum Key {
static let entries = "wordReplacements.entries"
}
private let defaults: UserDefaults?
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
self.defaults = defaults
}
/// The list in the order it was added, cleaned and within the words file's limits.
var entries: [WordReplacement] {
get {
let stored = defaults?.array(forKey: Key.entries) as? [[String: String]] ?? []
return Self.sanitize(stored.compactMap { item in
guard let heard = item["heard"], let write = item["write"] else { return nil }
return WordReplacement(heard: heard, write: write)
})
}
nonmutating set {
defaults?.set(Self.sanitize(newValue).map { ["heard": $0.heard, "write": $0.write] }, forKey: Key.entries)
}
}
/// Empty while Custom vocabulary is off, so dictation stays exactly as it is.
var activeCorrections: [DictationCorrection] {
guard CustomVocabularySettings(defaults: defaults).isEnabled else { return [] }
return entries.map { DictationCorrection(heard: $0.heard, meant: $0.write) }
}
enum Problem: Equatable {
case noLetters, hiddenCharacters, tooLong, tooManyWords, emptyWrite, duplicate, full
}
/// Why `heard` → `write` can't join `existing`, by the words file's rules; empty or blank
/// outputs aren't allowed on the iPhone yet.
static func problem(heard: String, write: String, existing: [WordReplacement]) -> Problem? {
let heard = HexWordsText.cleanLine(heard)
let write = HexWordsText.cleanWrite(write)
if existing.count >= maxCount { return .full }
if HexWordsText.isBlank(write) { return .emptyWrite }
switch HexWordsReader.replacementProblem(heard: heard, write: write) {
case .hiddenCharacters: return .hiddenCharacters
case .noLetters: return .noLetters
case .tooLong: return .tooLong
case .tooManyWords: return .tooManyWords
default: break
}
let key = HexWordsText.key(heard)
if existing.contains(where: { HexWordsText.key($0.heard) == key }) { return .duplicate }
return nil
}
/// Cleans each entry and drops any the editor would refuse.
static func sanitize(_ entries: [WordReplacement]) -> [WordReplacement] {
var result: [WordReplacement] = []
var keys: Set<String> = []
for entry in entries {
guard result.count < maxCount else { break }
let heard = HexWordsText.cleanLine(entry.heard)
let write = HexWordsText.cleanWrite(entry.write)
guard !HexWordsText.isBlank(write),
HexWordsReader.replacementProblem(heard: heard, write: write) == nil,
keys.insert(HexWordsText.key(heard)).inserted else { continue }
result.append(WordReplacement(heard: heard, write: write))
}
return result
}
}