Chore: minor changes

This commit is contained in:
SukkaW
2024-05-27 02:42:56 +08:00
parent eb0623c1a9
commit efa34399b0
7 changed files with 94 additions and 68 deletions

View File

@@ -36,32 +36,34 @@ const createNode = (parent: TrieNode | null = null): TrieNode => {
return node;
};
const hostnameToTokens = (hostname: string): string[] => {
let buf = '';
const tokens: string[] = [];
for (let i = 0, l = hostname.length; i < l; i++) {
const c = hostname[i];
if (c === '.') {
if (buf) {
tokens.push(buf, /* . */ c);
buf = '';
} else {
tokens.push(/* . */ c);
}
} else {
buf += c;
}
}
if (buf) {
tokens.push(buf);
}
return tokens;
};
export const createTrie = (from?: string[] | Set<string> | null, hostnameMode = false, smolTree = false) => {
let size = 0;
const root: TrieNode = createNode();
const suffixToTokens = hostnameMode
? (suffix: string) => {
let buf = '';
const tokens: string[] = [];
for (let i = 0, l = suffix.length; i < l; i++) {
const c = suffix[i];
if (c === '.') {
if (buf) {
tokens.push(buf, /* . */ c);
buf = '';
} else {
tokens.push(/* . */ c);
}
} else {
buf += c;
}
}
if (buf) {
tokens.push(buf);
}
return tokens;
}
? hostnameToTokens
: (suffix: string) => suffix;
/**