Skip to content

Commit b4a470a

Browse files
committed
Fix algorithmic Hangul decomposition mappings
Assisted-By: GPT-6
1 parent b596066 commit b4a470a

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎scripts/parse-decomposition-mapping.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ const parseDecompositionMapping = async (version) => {
3535
return;
3636
}
3737
const map = {};
38+
// UnicodeData represents all modern Hangul syllables as a range with empty
39+
// decomposition fields. Their canonical mappings are defined algorithmically:
40+
// https://www.unicode.org/versions/Unicode18.0.0/core-spec/chapter-3/#G56669
41+
const hangul = new Map();
42+
for (let syllable = 0xac00; syllable <= 0xd7a3; syllable++) {
43+
const index = syllable - 0xac00;
44+
const trailing = index % 28;
45+
hangul.set(
46+
syllable,
47+
trailing
48+
? [syllable - trailing, 0x11a7 + trailing]
49+
: [
50+
0x1100 + Math.floor(index / 588),
51+
0x1161 + (Math.floor(index / 28) % 21),
52+
],
53+
);
54+
}
55+
map.Canonical = hangul;
3856
const lines = source.split('\n');
3957
for (const line of lines) {
4058
const trimmed = line.trim();

‎tests/parsers.mjs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { test } from 'node:test';
2+
import parseDecompositionMapping from '../scripts/parse-decomposition-mapping.mjs';
3+
4+
test('Hangul has pairwise canonical decomposition mappings', async (t) => {
5+
for (const version of ['2.0.14', '3.2.0', '18.0.0']) {
6+
const { Canonical } = await parseDecompositionMapping(version);
7+
t.assert.deepEqual(Canonical.get(0xac00), [0x1100, 0x1161]);
8+
t.assert.deepEqual(Canonical.get(0xac01), [0xac00, 0x11a8]);
9+
t.assert.deepEqual(Canonical.get(0xd7a3), [0xd788, 0x11c2]);
10+
for (let cp = 0xac00; cp <= 0xd7a3; cp++) {
11+
t.assert.equal(Canonical.get(cp)?.length, 2);
12+
}
13+
}
14+
t.assert.equal(await parseDecompositionMapping('1.1.5'), undefined);
15+
});

‎tests/tests.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { suite, test } from 'node:test';
2+
import './parsers.mjs';
23
import resources from '../data/resources.mjs';
34

45
const oldest = resources[0].version;

0 commit comments

Comments
 (0)