From 0b3aa5633fcc70f0eb517c655ce40a85bb68b52f Mon Sep 17 00:00:00 2001 From: ccmdi Date: Fri, 14 Aug 2026 11:42:17 -0400 Subject: [PATCH] skip subalphabet encoding when no subalphabet fits the segment --- compress.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/compress.js b/compress.js index 2de462d..7056be5 100644 --- a/compress.js +++ b/compress.js @@ -245,9 +245,9 @@ export function compress (input, alphabet) { queryParamIndex ++; } // Look for smallest subalphabet that fits this path segment - let subalphabetIndex = subalphabets.length - 1; - let subalphabet = subalphabets[subalphabetIndex]; - for (let i = 0; i < subalphabets.length - 1; i ++) { + let subalphabetIndex = -1; + let subalphabet = null; + for (let i = 0; i < subalphabets.length; i ++) { if (!Array.from(segment.value).some(c => !subalphabets[i].includes(c))) { subalphabet = subalphabets[i]; subalphabetIndex = i; @@ -283,6 +283,13 @@ export function compress (input, alphabet) { // Encode segment variant as 0 // (We're adding +1 here to introduce 0 as a special value indicating Huffman) huffmanNumber *= BigInt(subalphabets.length + 1); + // If no subalphabet fits this segment, Huffman is the only option. + // Encoding a character missing from the subalphabet would produce the + // value 0, which the decoder treats as the end of the segment. + if (!subalphabet) { + number = huffmanNumber; + continue; + } // Compute number after encoding with chosen subalphabet const subalphabetLength = BigInt(subalphabet.length + 1); let subalphabetNumber = firstIteration ? number : number * subalphabetLength;