Skip to content

Commit 04cbd65

Browse files
committed
Split statuses and mappings, condense statuses
1 parent 029b465 commit 04cbd65

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function unpackMappingTable() {
2121

2222
rangesTable = [];
2323
let current = 0;
24-
const [rangesRaw, mappingRaw] = tablesRaw;
24+
const [rangesRaw, statusesRaw, mappingRaw] = tablesRaw;
2525
while (rangesRaw.length > 0) {
2626
if (rangesRaw[0] < 0) {
2727
const repeats = -rangesRaw.shift(); // Treat as this many repeats of [1, 0]
@@ -36,11 +36,20 @@ function unpackMappingTable() {
3636
}
3737

3838
mappingTable = [];
39-
while (mappingRaw.length > 0) {
40-
const status = mappingRaw[0];
41-
const rowSize = status === STATUS_MAPPING.mapped || status === STATUS_MAPPING.deviation ? 2 : 1;
42-
mappingTable.push(mappingRaw.splice(0, rowSize));
39+
for (const status of statusesRaw) {
40+
if (status < 0) {
41+
// Threat this as many repeats of STATUS_MAPPING.mapped
42+
for (let i = 0; i < -status; i++) {
43+
mappingTable.push([STATUS_MAPPING.mapped, mappingRaw.shift()]);
44+
}
45+
} else if (status === STATUS_MAPPING.mapped || status === STATUS_MAPPING.deviation) {
46+
mappingTable.push([status, mappingRaw.shift()]);
47+
} else {
48+
mappingTable.push([status]);
49+
}
4350
}
51+
52+
statusesRaw.length = 0; // Destroy for mem
4453
}
4554

4655
function findStatus(val) {

scripts/generateMappingTable.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ async function main() {
1818
const body = await response.text();
1919

2020
const ranges = [];
21-
const lines = [];
21+
const statuses = [];
22+
const mappings = [];
2223

2324
body.split("\n").forEach(l => {
2425
l = l.split("#")[0]; // Remove comments
@@ -36,15 +37,16 @@ async function main() {
3637
cells[0] = [start, end - start];
3738
ranges.push(cells.shift());
3839

39-
cells[0] = STATUS_MAPPING[cells[0]];
40-
if (cells[0] !== STATUS_MAPPING.mapped && cells[0] !== STATUS_MAPPING.deviation) {
41-
lines.push(cells[0]);
40+
const status = STATUS_MAPPING[cells.shift()];
41+
statuses.push(status);
42+
43+
if (status !== STATUS_MAPPING.mapped && status !== STATUS_MAPPING.deviation) {
4244
return;
4345
}
4446

45-
if (cells[1] !== undefined) {
47+
if (cells[0] !== undefined) {
4648
// Parse replacement to int[] array
47-
let replacement = cells[1].split(" ");
49+
let replacement = cells[0].split(" ");
4850
if (replacement[0] === "") { // Empty array
4951
replacement = [];
5052
}
@@ -53,12 +55,10 @@ async function main() {
5355
return parseInt(r, 16);
5456
});
5557

56-
cells[1] = String.fromCodePoint(...replacement);
58+
mappings.push(String.fromCodePoint(...replacement));
5759
} else {
5860
throw new Error("Unexpected");
5961
}
60-
61-
lines.push(cells.flat());
6262
});
6363

6464
// We could drop valid chars, but those are only ~1000 ranges and
@@ -71,7 +71,7 @@ async function main() {
7171
last += range[0];
7272
}
7373

74-
// Condense repeats of N consecutive [1, 0] in ranges to -N
74+
// Condense repeats of N consecutive [1, 0] in ranges to -N, flatten the rest
7575
const rangesCondensed = [];
7676
let repeats = 0;
7777
for (const row of ranges) {
@@ -85,13 +85,35 @@ async function main() {
8585
repeats = 0;
8686
}
8787

88-
rangesCondensed.push(row);
88+
rangesCondensed.push(...row);
8989
}
9090

9191
if (repeats > 0) {
9292
rangesCondensed.push(-repeats);
93+
repeats = 0;
94+
}
95+
96+
// Condense repeats of N consecutive STATUS_MAPPING.mapped to -N
97+
const statusesCondensed = [];
98+
for (const status of statuses) {
99+
if (status === STATUS_MAPPING.mapped) {
100+
repeats++;
101+
continue;
102+
}
103+
104+
if (repeats > 0) {
105+
statusesCondensed.push(-repeats);
106+
repeats = 0;
107+
}
108+
109+
statusesCondensed.push(status);
110+
}
111+
112+
if (repeats > 0) {
113+
statusesCondensed.push(-repeats);
114+
repeats = 0;
93115
}
94116

95-
const tablesRaw = [rangesCondensed.flat(), lines.flat()];
117+
const tablesRaw = [rangesCondensed, statusesCondensed, mappings];
96118
fs.writeFileSync(path.resolve(__dirname, "../lib/mappingTable.json"), JSON.stringify(tablesRaw));
97119
}

0 commit comments

Comments
 (0)