change(web): assertion -> assumption, ternary if-condition rework

This commit is contained in:
Joshua Horton 2025-09-30 08:50:54 -05:00
parent ec76f0dd5f
commit 17618176c6

View file

@ -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 {