@@ -336,7 +336,14 @@ export class DictionaryDatabase {
336336 const indexNames = isSuffix ? [ 'expressionReverse' , 'readingReverse' ] : [ 'expression' , 'reading' ] ;
337337 const query = isSuffix ? this . _createBoundQuery2 ( anchor ) : this . _createBoundQuery1 ( anchor ) ;
338338 // Reverse-index scan needs the pattern in reversed key space; guard hits with the forward matcher.
339- const scanPattern = isSuffix ? this . _reverseMaskedSubPattern ( pattern , anchor ) : pattern ;
339+ let scanPattern = pattern ;
340+ if ( isSuffix ) {
341+ let lastMask = - 1 ;
342+ for ( let i = 0 ; i < pattern . isMask . length ; ++ i ) {
343+ if ( pattern . isMask [ i ] ) { lastMask = i ; }
344+ }
345+ scanPattern = this . _reverseMaskedSubPattern ( pattern , lastMask + [ ...anchor ] . length + 1 ) ;
346+ }
340347 /** @type {?(key: string) => boolean } */
341348 const recordGuard = isSuffix ? ( key ) => keyMatcher ( stringReverse ( key ) ) : null ;
342349
@@ -366,19 +373,13 @@ export class DictionaryDatabase {
366373 }
367374
368375 /**
369- * Reverses the masks-plus-anchor slice (positions `0 .. lastMask + anchorLength`) of a forward pattern
370- * into reverse-index key space for a suffix scan.
376+ * Reverses the masks-plus-anchor slice of a forward pattern into reverse-index key space for a suffix scan.
371377 * @param {import('dictionary-database').MaskedPattern } pattern Forward masked pattern.
372- * @param {string } anchor
378+ * @param {number } subLength Length of the prefix to reverse.
373379 * @returns {import('dictionary-database').MaskedPattern }
374380 */
375- _reverseMaskedSubPattern ( pattern , anchor ) {
381+ _reverseMaskedSubPattern ( pattern , subLength ) {
376382 const { chars, isMask} = pattern ;
377- let lastMask = - 1 ;
378- for ( let i = 0 ; i < isMask . length ; ++ i ) {
379- if ( isMask [ i ] ) { lastMask = i ; }
380- }
381- const subLength = lastMask + 1 + [ ...anchor ] . length ;
382383 return {
383384 chars : chars . slice ( 0 , subLength ) . reverse ( ) ,
384385 isMask : isMask . slice ( 0 , subLength ) . reverse ( ) ,
@@ -398,6 +399,10 @@ export class DictionaryDatabase {
398399 const isSuffix = ( matchType === 'suffix' ) ;
399400 const indexNames = isSuffix ? [ 'expressionReverse' , 'readingReverse' ] : [ 'expression' , 'reading' ] ;
400401 const query = isSuffix ? this . _createBoundQuery2 ( anchor ) : this . _createBoundQuery1 ( anchor ) ;
402+ /** @type {(key: IDBValidKey) => string } */
403+ const keyNormalizer = isSuffix ?
404+ ( key ) => stringReverse ( /** @type {string } */ ( key ) ) :
405+ ( key ) => /** @type {string } */ ( key ) ;
401406
402407 const transaction = this . _db . transaction ( [ 'terms' ] , 'readonly' ) ;
403408 const objectStore = transaction . objectStore ( 'terms' ) ;
@@ -409,7 +414,7 @@ export class DictionaryDatabase {
409414 for ( let j = 0 ; j < indexNames . length ; ++ j ) {
410415 const indexIndex = j ;
411416 /** @type {(key: IDBValidKey) => boolean } */
412- const keyPredicate = ( key ) => keyMatcher ( isSuffix ? stringReverse ( /** @type { string } */ ( key ) ) : /** @type { string } */ ( key ) ) ;
417+ const keyPredicate = ( key ) => keyMatcher ( keyNormalizer ( key ) ) ;
413418 /** @type {(primaryKeys: IDBValidKey[]) => void } */
414419 const onKeys = ( primaryKeys ) => {
415420 for ( const primaryKey of primaryKeys ) {
0 commit comments