Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | 861x 481x 4391x 7x 3x 96x 96x 96x 96x 96x 3143x 3143x 3143x 3143x 2837x 4385x 9x 9x 3767x 96x 3143x 96x 96x 96x 96x 485x 485x 485x 9248x 9248x 92x 485x 485x 25x 25x 25x 421x 144x 144x 11x 25x 25x 25x 25x 25x 25x 14x 14x 11x 11x 25x 494x 494x 494x 15x 9x 86x 86x 86x 86x 9x 3x 499x 499x 499x 499x 3x 407x 367x 367x 367x 8x 1x 7x 3x 96x 96x 96x 96x 2x 2x 2x 494x 494x 485x 485x 485x 485x 393x 393x 393x 393x 91x 91x 91x 302x 302x 302x 92x 30x 30x 62x 37x 24x 24x 24x 13x 13x 13x 13x 13x 13x 25x 25x 25x 9x 9x 9x 1x 1x 1x 1x 9x 9x 9x 2x 2x 2x 9x 9x 9x 9x 88x 77x 77x 11x 11x 11x 11x 11x 11x | import type {TranslitScheme} from './types';
/**
* One tick of input through the matcher.
*
* Bindings translate this into an edit on the document/input:
*
* 1. Delete `replaceTail` UTF-16 code units at the end of the previously emitted run.
* 2. Insert `emit` at the caret.
*
* `replaceTail` is non-zero when the matcher rewrites a digraph that it
* eager-committed on the previous keystroke (e.g. `s` → `с` is replaced with
* `ш` when the user types the `h` of `sh`), and when a word boundary triggers
* a `finalForms` swap on the previously-committed glyph.
*/
export interface MatchStep {
replaceTail: number;
emit: string;
/** True when the matcher reset its state. */
reset: boolean;
}
interface NormalizedRule {
in: string;
out: string;
caseFold: boolean;
}
const isAsciiUpper = (c: number): boolean => c >= 65 && c <= 90;
const isAsciiLower = (c: number): boolean => c >= 97 && c <= 122;
const isAsciiDigit = (c: number): boolean => c >= 48 && c <= 57;
const upperFirst = (s: string): string => (s ? s.charAt(0).toUpperCase() + s.slice(1) : s);
/**
* Pre-compiled scheme: rules sorted longest-first, prefix set built up-front.
* Compile once per scheme; share across matchers.
*/
export class CompiledScheme {
public readonly rules: NormalizedRule[];
public readonly prefixes: Set<string>;
public readonly finalForms: Readonly<Record<string, string>>;
public readonly digitsAreLetters: boolean;
constructor(public readonly scheme: TranslitScheme) {
const norm: NormalizedRule[] = [];
const prefixes = new Set<string>();
let digitsInRules = false;
for (const r of scheme.rules) {
const caseFold = r.caseFold !== false;
const inKey = caseFold ? r.in.toLowerCase() : r.in;
norm.push({in: inKey, out: r.out, caseFold});
if (!digitsInRules) {
for (let i = 0; i < r.in.length; i++) {
if (isAsciiDigit(r.in.charCodeAt(i))) {
digitsInRules = true;
break;
}
}
}
}
norm.sort((a, b) => b.in.length - a.in.length);
for (const r of norm) {
for (let i = 1; i < r.in.length; i++) prefixes.add(r.in.slice(0, i));
}
this.rules = norm;
this.prefixes = prefixes;
this.finalForms = scheme.finalForms ?? {};
this.digitsAreLetters = digitsInRules;
}
public exact(s: string): NormalizedRule | null {
Iif (!s) return null;
const lc = s.toLowerCase();
for (const r of this.rules) {
const key = r.caseFold ? lc : s;
if (r.in === key) return r;
}
return null;
}
public isPrefix(s: string): boolean {
Iif (!s) return false;
return this.prefixes.has(s.toLowerCase());
}
/** Longest rule whose `in` is a prefix of `s`. */
public longestPrefix(s: string): NormalizedRule | null {
Iif (!s) return null;
const lc = s.toLowerCase();
for (const r of this.rules) {
if (r.in.length > s.length) continue;
const head = r.caseFold ? lc.slice(0, r.in.length) : s.slice(0, r.in.length);
if (head === r.in) return r;
}
return null;
}
/** Greedy-eat: emit each char/digraph using the longest rule that fits. */
public greedyEmit(s: string): string {
let out = '';
let i = 0;
while (i < s.length) {
const rest = s.slice(i);
const r = this.longestPrefix(rest);
if (r) {
out += applyCase(r, rest.slice(0, r.in.length));
i += r.in.length;
} else {
out += s.charAt(i);
i += 1;
}
}
return out;
}
/** Whether `ch` is a buffer-eligible "letter" for this scheme. */
public isLetter(ch: string): boolean {
Iif (!ch) return false;
const c = ch.charCodeAt(0);
if (c === 39 || isAsciiUpper(c) || isAsciiLower(c)) return true;
if (this.digitsAreLetters && isAsciiDigit(c)) return true;
return false;
}
/** Final-form replacement for the last code point of an emitted run. */
public applyFinalForm(emit: string): {tail: number; replacement: string} | null {
Iif (!emit) return null;
const last = lastCodePoint(emit);
const replacement = this.finalForms[last.glyph];
if (!replacement) return null;
return {tail: last.units, replacement};
}
}
const lastCodePoint = (s: string): {glyph: string; units: number} => {
const lastIdx = s.length - 1;
const code = s.charCodeAt(lastIdx);
Iif (code >= 0xdc00 && code <= 0xdfff && lastIdx > 0) {
const high = s.charCodeAt(lastIdx - 1);
if (high >= 0xd800 && high <= 0xdbff) {
return {glyph: s.slice(lastIdx - 1), units: 2};
}
}
return {glyph: s.charAt(lastIdx), units: 1};
};
const applyCase = (rule: NormalizedRule, input: string): string => {
if (!rule.caseFold) return rule.out;
Iif (!input) return rule.out;
const firstUp = isAsciiUpper(input.charCodeAt(0));
if (!firstUp) return rule.out;
if (input.length > 1 && isAsciiUpper(input.charCodeAt(input.length - 1))) {
return rule.out.toUpperCase();
}
return upperFirst(rule.out);
};
/**
* Stateful matcher. One per editor / input. Holds the partial-digraph buffer,
* the rewriteable-tail length from the last eager commit, and the previously
* emitted glyph.
*/
export class Matcher {
/** Latin chars currently held back in case they extend a digraph. */
public buffer = '';
/** UTF-16 length of the just-emitted run that can still be rewritten by an
* extension. Non-zero only after an eager commit. */
public lastEmitLen = 0;
/** Last codepoint emitted since the most recent boundary. */
public prevGlyph = '';
constructor(public readonly scheme: CompiledScheme) {}
public reset(): void {
this.buffer = '';
this.lastEmitLen = 0;
this.prevGlyph = '';
}
/** Feed a single character. Multi-char strings go through `flushAndPassthrough`. */
public feed(ch: string): MatchStep {
Iif (ch.length !== 1) return this.flushAndPassthrough(ch);
if (!this.scheme.isLetter(ch)) return this.feedBoundary(ch);
const candidate = this.buffer + ch;
const exact = this.scheme.exact(candidate);
const isPrefix = this.scheme.isPrefix(candidate);
if (exact) {
const out = applyCase(exact, candidate);
const replaceTail = this.lastEmitLen;
this.prevGlyph = lastCodePoint(out).glyph;
if (isPrefix) {
this.buffer = candidate;
this.lastEmitLen = out.length;
return {replaceTail, emit: out, reset: false};
}
this.buffer = '';
this.lastEmitLen = 0;
return {replaceTail, emit: out, reset: true};
}
if (isPrefix) {
this.buffer = candidate;
return {replaceTail: 0, emit: '', reset: false};
}
// Neither exact nor prefix.
if (this.buffer.length > 0) {
if (this.lastEmitLen > 0) {
this.buffer = '';
this.lastEmitLen = 0;
return this.feed(ch);
}
const flushed = this.scheme.greedyEmit(this.buffer);
this.buffer = '';
this.lastEmitLen = 0;
if (flushed) this.prevGlyph = lastCodePoint(flushed).glyph;
const rec = this.feed(ch);
return {replaceTail: rec.replaceTail, emit: flushed + rec.emit, reset: rec.reset};
}
this.lastEmitLen = 0;
this.prevGlyph = ch;
return {replaceTail: 0, emit: ch, reset: true};
}
private feedBoundary(ch: string): MatchStep {
let emit = '';
let replaceTail = 0;
if (this.buffer.length > 0) {
replaceTail = this.lastEmitLen;
emit = this.scheme.greedyEmit(this.buffer);
this.buffer = '';
Eif (emit) this.prevGlyph = lastCodePoint(emit).glyph;
}
// Apply final-form to the prevGlyph.
const prev = this.prevGlyph;
const finalForm = prev ? this.scheme.finalForms[prev] : undefined;
if (finalForm) {
Iif (emit) {
const last = lastCodePoint(emit);
emit = emit.slice(0, emit.length - last.units) + finalForm;
} else {
replaceTail += prev.length;
emit = finalForm;
}
}
this.lastEmitLen = 0;
this.prevGlyph = '';
emit += ch;
return {replaceTail, emit, reset: true};
}
public flushBuffer(): MatchStep {
if (!this.buffer) {
this.lastEmitLen = 0;
return {replaceTail: 0, emit: '', reset: true};
}
const replaceTail = this.lastEmitLen;
const emit = this.scheme.greedyEmit(this.buffer);
this.buffer = '';
this.lastEmitLen = 0;
Eif (emit) this.prevGlyph = lastCodePoint(emit).glyph;
return {replaceTail, emit, reset: true};
}
public flushAndPassthrough(s: string): MatchStep {
const flushed = this.flushBuffer();
this.prevGlyph = '';
return {replaceTail: flushed.replaceTail, emit: flushed.emit + s, reset: true};
}
}
|