Skip to content

Commit

Permalink
dev: refactor the finder to reduce redundant checking. (#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jun 18, 2021
1 parent 548ac34 commit 7513bc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/cspell-trie-lib/src/lib/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Validate findWord', () => {

// non-normalized words
['café', { matchCase: false, compoundMode: 'none' }, frFound('café')],
['Café', { matchCase: false, compoundMode: 'none' }, frNotFound()],
['cafe', { matchCase: false, compoundMode: 'none' }, frFound('cafe', { caseMatched: false })],
['Code', { matchCase: false, compoundMode: 'none' }, frNotFound()],

Expand Down
41 changes: 29 additions & 12 deletions packages/cspell-trie-lib/src/lib/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ function _findWord(root: TrieNode, word: string, options: FindOptions): FindFull
function _findWordNode(root: TrieNode, word: string, options: FindOptions): FindFullNodeResult {
const compoundMode = knownCompoundModes.get(options.compoundMode) || _defaultFindOptions.compoundMode;
const compoundPrefix = options.compoundMode === 'compound' ? options.compoundFix : '';
const possiblePrefix = options.matchCase ? '' : options.caseInsensitivePrefix;
const ignoreCasePrefix = options.matchCase ? '' : options.caseInsensitivePrefix;

function __findCompound(): FindFullNodeResult {
const f = findCompoundWord(root, word, compoundPrefix, possiblePrefix);
const f = findCompoundWord(root, word, compoundPrefix, ignoreCasePrefix);
let forbidden = false;
if (f.found !== false && f.compoundUsed) {
// If case was ignored when searching for the word, the check the forbidden
// If case was ignored when searching for the word, then check the forbidden
// in the ignore case forbidden list.
const r = !f.caseMatched ? walk(root, options.caseInsensitivePrefix) : root;
forbidden = isForbiddenWord(r, word, options.forbidPrefix);
Expand Down Expand Up @@ -148,13 +148,15 @@ export function findCompoundNode(
root: TrieNode | undefined,
word: string,
compoundCharacter: string,
possiblePrefix: string
ignoreCasePrefix: string
): FindFullNodeResult {
// Approach - do a depth first search for the matching word.
const stack: FindCompoundChain[] = [{ n: root, compoundPrefix: possiblePrefix, cr: undefined, caseMatched: true }];
const compoundPrefix = compoundCharacter || possiblePrefix;
const possibleCompoundPrefix = possiblePrefix && compoundCharacter ? possiblePrefix + compoundCharacter : '';
const w = word;
const stack: FindCompoundChain[] = [
{ n: root, compoundPrefix: ignoreCasePrefix, cr: undefined, caseMatched: true },
];
const compoundPrefix = compoundCharacter || ignoreCasePrefix;
const possibleCompoundPrefix = ignoreCasePrefix && compoundCharacter ? ignoreCasePrefix + compoundCharacter : '';
const w = word.normalize();

function determineRoot(s: FindCompoundChain): FindCompoundChain {
const prefix = s.compoundPrefix;
Expand All @@ -163,7 +165,7 @@ export function findCompoundNode(
for (i = 0; i < prefix.length && r; ++i) {
r = r.c?.get(prefix[i]);
}
const caseMatched = s.caseMatched && prefix !== possiblePrefix;
const caseMatched = s.caseMatched && prefix !== ignoreCasePrefix;
return {
n: s.n,
compoundPrefix: prefix === compoundPrefix ? possibleCompoundPrefix : '',
Expand Down Expand Up @@ -196,7 +198,17 @@ export function findCompoundNode(
}
if (i >= 0 && stack[i].compoundPrefix) {
compoundUsed = i > 0;
stack[i] = determineRoot(stack[i]);
const r = determineRoot(stack[i]);
stack[i] = r;
if (!r.cr) {
break;
}
if (!i && !r.caseMatched) {
if (w !== w.toLowerCase()) {
// It is not going to be found.
break;
}
}
} else {
break;
}
Expand All @@ -216,9 +228,14 @@ function findCompoundWord(
root: TrieNode | undefined,
word: string,
compoundCharacter: string,
possiblePrefix: string
ignoreCasePrefix: string
): FindFullNodeResult {
const { found, compoundUsed, node, caseMatched } = findCompoundNode(root, word, compoundCharacter, possiblePrefix);
const { found, compoundUsed, node, caseMatched } = findCompoundNode(
root,
word,
compoundCharacter,
ignoreCasePrefix
);
// Was it a word?
if (!node || !node.f) {
return { found: false, compoundUsed, node, forbidden: false, caseMatched };
Expand Down

0 comments on commit 7513bc7

Please sign in to comment.