From 17618176c6945870d80e01ace65175b20bae8e66 Mon Sep 17 00:00:00 2001 From: Joshua Horton Date: Tue, 30 Sep 2025 08:50:54 -0500 Subject: [PATCH] change(web): assertion -> assumption, ternary if-condition rework --- .../src/main/correction/context-state.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/web/src/engine/predictive-text/worker-thread/src/main/correction/context-state.ts b/web/src/engine/predictive-text/worker-thread/src/main/correction/context-state.ts index a068a45ac5..c464bc6b1c 100644 --- a/web/src/engine/predictive-text/worker-thread/src/main/correction/context-state.ts +++ b/web/src/engine/predictive-text/worker-thread/src/main/correction/context-state.ts @@ -316,18 +316,21 @@ export class ContextState { * number of codepoints removed from its start (if sliding forward) */ export function determineContextSlideTransform(srcContext: Context, dstContext: Context): Transform { - // Assertion: the current (sliding) context window is alignable. + // Assumption: the current (sliding) context window is alignable. // See `matchBaseContextState` in ../predict-helpers.ts. + + // Assertion: If the assumption above holds and both start-of-buffer flags + // are true, the contents must then match. if(srcContext.startOfBuffer && dstContext.startOfBuffer) { return { insert: '', deleteLeft: 0, deleteRight: 0 }; } - // Assertion: the right-hand side of the left-context strings WILL match. + // Assumption: the right-hand side of the left-context strings WILL match. // The only change should be for the contents of the sliding-context window. const src = srcContext.left; const dst = dstContext.left; - // Assertion: the context will always be codepoint-aligned, as the Web engine + // Assumption: the context will always be codepoint-aligned, as the Web engine // and worker both do string ops based on codepoints, not code units. // Which way did the context window slide, if it did? This does not @@ -337,8 +340,14 @@ export function determineContextSlideTransform(srcContext: Context, dstContext: const rawDelta = dst.length - src.length; // Validation: does the part of both strings that should match actually match? - if(rawDelta > 0 ? dst.slice(rawDelta) != src : src.slice(-rawDelta) != dst) { - throw new Error("Invalid base context"); + // + // Context operations are already code-point aligned; no need to use special + // non-BMP handling here. + const smallerIsSubstringOfOther = rawDelta > 0 + ? dst.slice(rawDelta) == src + : src.slice(-rawDelta) == dst; + if(!smallerIsSubstringOfOther) { + throw new Error(`Context-slide preconditions invalidated - neither before nor after context is a substring of the other`); } return {