hex/ios/HEXShared/FullKeyboard/TypingConfig/TypingConfigText.swift
Ryan Vogel dfb877d1e4 ios: typing config review: the person's keyboards, each part's evidence and place, cleaned text
The phone lane's three requests for TypingConfigReview (#2529 renders it):
- Keyboards, not one guessed context:
  - The keyboard records each letters keyboard it runs on, with the experiment on and Full
    Access, in typingConfig.keyboards (TypingConfigKeyboard: context plus KeyboardZoneGeometry),
    most recent first, at most 8, written only when the list changes.
  - TypingConfigReview(data:keyboards:) checks each key block against all of them: a block
    applies on the first keyboard it matches, a second block for that keyboard is a duplicate,
    and one that matches none is forAnotherKeyboard, with its differences from the nearest.
  - ContextCheck gains keyboardIndex, and keyboard is the matched or nearest one. Applied.blocks
    holds every applying block; Applied.aim and .zones are keyboard 0's.
  - compiled(switchedOn:) keeps every applying block, and the keyboard picks the one for its
    layout.
  - The zone compiler moved onto KeyboardZoneGeometry so recorded keyboards can be checked.
    KeyboardLayout.zoneMap wraps it, with the same results.
- Evidence and place on every Item: list (file, keep, neverCorrectTo, fixes, dictation,
  keyBlocks, aim, zones), position in it, block, the file's evidence, and the cleaned text, so
  "Words to keep, number 6" comes from the result alone.
- Cleaned text (TypingConfigText) for the note and every word, fix and dictation fix, so the
  sheet and the keyboard use the same strings.
  - Removed: controls (Cc), bidi embeddings, overrides, isolates and marks (U+202A-202E,
    U+2066-2069, U+200E, U+200F, U+061C), and invisible format characters (Cf: U+200B, U+2060,
    U+FEFF, U+00AD, ...).
  - ZWNJ and ZWJ are kept only between letters or marks of Arabic and Persian, Syriac, the Indic
    scripts, Sinhala, Myanmar and Khmer, and ZWJ between emoji. Tag characters are kept after a
    black flag.
  - Words are NFC-normalised and trimmed. A word that loses anything from inside it is skipped
    with the new Reason hiddenCharacters (after invalidValue in the order); one empty after
    cleaning is invalidValue. The note is cleaned, then cut at 280.
- The reference's "Reading" and "On the phone" sections and docs/typing-configs.md say so.

Verified on the Studio (iPhone 17 Pro, iOS 27.2):
- FullKeyboardEngineTests: 163 of 163 pass, including two new cases in
  FullKeyboardEngineTests+AdaptiveTouch.swift.
  - Four blocks on upright and sideways keyboards: English upright and sideways apply, French is
    forAnotherKeyboard with differences [language, letters], a second upright block is a
    duplicate, compiled keeps both applying blocks, the keyboard picks each on its own layout,
    and recording dedupes most-recent-first.
  - Cleaning: an override, a zero-width space, a Latin ZWJ and a fix with an isolate are
    hiddenCharacters. A Persian word keeps its ZWNJ, a Devanagari conjunct its ZWJ, and a
    decomposed café becomes NFC. The note loses its newline and override and is cut at 280.
  - The first review case also checks list, position and evidence.
- TypingLogTests: 12 of 12 pass.
2026-11-23 12:47:08 -05:00

70 lines
3.7 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
/// The strings a typing config brings in (the note, words, fixes, dictation fixes), as the review
/// shows them and the keyboard loads them. Removed:
/// - controls (general category Cc: C0, DEL, C1), the note's newlines and tabs as spaces;
/// - bidirectional formatting: the embeddings and overrides U+202A–U+202E, the isolates
/// U+2066–U+2069, and the marks U+200E, U+200F and U+061C;
/// - invisible format characters (general category Cf): U+200B ZERO WIDTH SPACE, U+2060 WORD
/// JOINER, U+FEFF, U+00AD SOFT HYPHEN, the invisible operators U+2061–U+2064, and the rest;
/// - ZERO WIDTH NON-JOINER and ZERO WIDTH JOINER (U+200C, U+200D) only where they aren't part of
/// a spelling. They are kept between two letters or marks of a script that spells with them
/// (Arabic and Persian, Syriac, the Indic scripts, Sinhala, Myanmar, Khmer), and the joiner
/// between emoji. Tag characters are kept after a black flag (subdivision flags).
/// Words are then NFC-normalised, trimmed, and runs of spaces become one.
nonisolated enum TypingConfigText {
/// `text` cleaned, and whether anything was removed from inside it (not trimming or NFC).
static func clean(_ text: String) -> (text: String, removedHidden: Bool) {
let scalars = Array(text.unicodeScalars)
var out = String.UnicodeScalarView()
var removed = false
for (i, s) in scalars.enumerated() {
if s == "\n" || s == "\t" || s == "\r" {
out.append(" ")
continue
}
if keeps(s, before: i > 0 ? scalars[i - 1] : nil, after: i + 1 < scalars.count ? scalars[i + 1] : nil, previous: scalars[..<i]) {
out.append(s)
} else {
removed = true
}
}
var cleaned = String(out).precomposedStringWithCanonicalMapping
cleaned = cleaned.split(separator: " ", omittingEmptySubsequences: true).joined(separator: " ")
return (cleaned.trimmingCharacters(in: .whitespacesAndNewlines), removed)
}
private static func keeps(_ s: Unicode.Scalar, before: Unicode.Scalar?, after: Unicode.Scalar?, previous: ArraySlice<Unicode.Scalar>) -> Bool {
switch s.properties.generalCategory {
case .control:
return false
case .format:
if s.value == 0x200C || s.value == 0x200D {
if let before, let after, joins(before), joins(after) { return true }
if s.value == 0x200D, let before, let after, before.properties.isEmoji, !before.isASCII, after.properties.isEmoji, !after.isASCII { return true }
return false
}
// Tag characters spell subdivision flags after U+1F3F4.
if (0xE0020...0xE007F).contains(s.value), let start = previous.last(where: { !(0xE0020...0xE007F).contains($0.value) }), start.value == 0x1F3F4 {
return true
}
return false
default:
return true
}
}
/// A letter or mark of a script whose spelling uses the joiners.
private static func joins(_ s: Unicode.Scalar) -> Bool {
let category = s.properties.generalCategory
guard category == .otherLetter || category == .nonspacingMark || category == .spacingMark || category == .enclosingMark else { return false }
switch s.value {
case 0x0600...0x06FF, 0x0750...0x077F, 0x08A0...0x08FF, 0xFB50...0xFDFF, 0xFE70...0xFEFF: return true // Arabic, Persian
case 0x0700...0x074F: return true // Syriac
case 0x0900...0x0DFF: return true // Devanagari through Sinhala
case 0x1000...0x109F: return true // Myanmar
case 0x1780...0x17FF: return true // Khmer
default: return false
}
}
}