fix(encoding): use the canonical two-byte GBK euro mapping

This commit is contained in:
Menghuan1918 committed 2026-11-09 12:09:54 +08:00
1 parent 0bc07a016c
commit de29aae2a6
2 files changed
+10 -1

No files matched your search

+4 -1
View File
@@ -406,7 +406,10 @@ function getGbkEncodeMap(): Map<string, number> {
if (gbkEncodeMap !== undefined) return gbkEncodeMap
const map = new Map<string, number>()
for (let byte = 0; byte <= 0x7f; byte += 1) map.set(String.fromCharCode(byte), byte)
map.set('€', 0x80)
// GBK has a legacy single-byte euro at 0x80, but WHATWG/ICU (Node 24.21's
// ICU 78, and current Chromium) reject it as invalid under `fatal: true`.
// The canonical two-byte GBK spelling decodes everywhere.
map.set('€', 0xa2e3)
for (let lead = 0x81; lead <= 0xfe; lead += 1) {
for (let trail = 0x40; trail <= 0xfe; trail += 1) {
if (trail === 0x7f) continue
+6
View File
@@ -35,6 +35,12 @@ describe('text encoding', () => {
expect(decoded).toEqual({ content: text, encoding })
})
it('encodes the euro sign with the two-byte GBK mapping', () => {
const bytes = encodeText('€', 'gbk')
expect(bytes.toString('hex')).toBe('a2e3')
expect(decodeTextBytes(bytes)).toEqual({ content: '€', encoding: 'gbk' })
})
it('detects BOM-less UTF-16 script text, including an odd truncated read', () => {
const text = '@echo off\r\necho 中文测试\r\n'
const little = Buffer.from(text, 'utf16le')