-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Pre-generate encode trie (#776)
- Loading branch information
Showing
7 changed files
with
133 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
node_modules/ | ||
coverage/ | ||
lib/ | ||
src/maps/ | ||
maps/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* eslint-disable node/no-unsupported-features/es-builtins */ | ||
|
||
import htmlMap from "../maps/entities.json"; | ||
import { writeFileSync } from "fs"; | ||
|
||
interface TrieNode { | ||
/** The value, if the node has a value. */ | ||
v?: string; | ||
/** A map with the next nodes, if there are any. */ | ||
n?: Map<number, TrieNode>; | ||
} | ||
|
||
const htmlTrie = getTrie(htmlMap); | ||
const serialized = serializeTrie(htmlTrie); | ||
|
||
writeFileSync( | ||
`${__dirname}/../src/generated/encode-html.ts`, | ||
`// Generated using scripts/write-encode-map.ts | ||
type EncodeTrieNode = | ||
| string | ||
| { v?: string; n: number | Map<number, EncodeTrieNode>; o?: string }; | ||
// prettier-ignore | ||
export default ${ | ||
// Fix the type of the first map to refer to trie nodes. | ||
serialized.replace("<number,string>", "<number,EncodeTrieNode>") | ||
}; | ||
` | ||
); | ||
|
||
console.log("Done!"); | ||
|
||
function getTrie(map: Record<string, string>): Map<number, TrieNode> { | ||
const trie = new Map<number, TrieNode>(); | ||
|
||
for (const entity of Object.keys(map)) { | ||
const decoded = map[entity]; | ||
// Resolve the key | ||
let lastMap = trie; | ||
for (let i = 0; i < decoded.length - 1; i++) { | ||
const char = decoded.charCodeAt(i); | ||
const next = lastMap.get(char) ?? {}; | ||
lastMap.set(char, next); | ||
lastMap = next.n ??= new Map(); | ||
} | ||
const val = lastMap.get(decoded.charCodeAt(decoded.length - 1)) ?? {}; | ||
val.v ??= entity; | ||
lastMap.set(decoded.charCodeAt(decoded.length - 1), val); | ||
} | ||
|
||
return trie; | ||
} | ||
|
||
function wrapValue(value: string | undefined): string { | ||
if (value == null) throw new Error("unexpected null"); | ||
|
||
return `"&${value};"`; | ||
} | ||
|
||
function serializeTrie(trie: Map<number, TrieNode>): string { | ||
const entries: [number, TrieNode][] = Array.from(trie.entries()).sort( | ||
(a, b) => a[0] - b[0] | ||
); | ||
|
||
return `new Map<number,string>([${entries | ||
.map(([key, value]) => { | ||
if (!value.n) { | ||
if (value.v == null) throw new Error("unexpected null"); | ||
return `[${key},${wrapValue(value.v)}]`; | ||
} | ||
const entries: string[] = []; | ||
if (value.v != null) { | ||
entries.push(`v:${wrapValue(value.v)}`); | ||
} | ||
/* | ||
* We encode branches as either a number with an `o` (other) value, | ||
* or as a map. | ||
* | ||
* We use a map if there are more than one character in the key. | ||
*/ | ||
if (value.n.size > 1) { | ||
entries.push(`n:${serializeTrie(value.n)}`); | ||
} else { | ||
const [cond, other] = Array.from(value.n)[0]; | ||
entries.push(`n:${cond},o:${wrapValue(other.v)}`); | ||
} | ||
return `[${key},{${entries.join(",")}}]`; | ||
}) | ||
.join(",")}])`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.