From 5646e81b942b0d04a323218b06a62b5dd3faa637 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Tue, 9 Jun 2026 07:25:18 +0200 Subject: [PATCH] feat(parsing): Windows-1250/1251/1253/1257, KOI8-R, ISO-8859-11 (#97) --- CHANGELOG.md | 2 +- README.md | 2 +- Sources/Parsing/ByteDecoder.swift | 12 +++ Sources/Parsing/InputEncoding.swift | 6 ++ Sources/Parsing/SingleByteEncodings.swift | 31 ++++++ Sources/Parsing/SingleByteTables.swift | 116 ++++++++++++++++++++++ Tests/EncodingTests.swift | 14 +++ 7 files changed, 181 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80b73c2..3bdfce2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Single-byte legacy encodings (#97, partial): the byte decoder gains a table-based single-byte path and the full ISO-8859 family (ISO-8859-2 through 16, excluding the Thai part 11) plus Windows-1254, selected by the XML declaration's `encoding` name for both the whole-buffer and streaming decoders. The table-based encodings are vendored verbatim from the authoritative `unicode.org` mapping files; ISO-8859-5/9/15 are derived exactly; Windows-1254 composes from Windows-1252. The remaining encodings (Windows code pages, KOI8) are data drop-ins into the same path. +- Single-byte legacy encodings (#97, partial): the byte decoder gains a table-based single-byte path and a broad set of legacy encodings, selected by the XML declaration's `encoding` name for both the whole-buffer and streaming decoders: the full ISO-8859 family (parts 2 through 16), Windows-1250/1251/1253/1254/1257, and KOI8-R. The table-based encodings are vendored verbatim from the authoritative `unicode.org` mapping files; ISO-8859-5/9/15 are derived exactly; Windows-1254 composes from Windows-1252. A few Windows pages (1255/1256/1258) and KOI8-U remain as data drop-ins into the same path. ### Fixed diff --git a/README.md b/README.md index c477e4d..86a5ee0 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ flowchart TB PureXML is a working, dependency-free XML library today: parse, emit, validate, query, and stream documents on macOS, Linux, Windows, and WASM. The test suite -currently runs **756 tests in 117 suites** (`swift test`). +currently runs **757 tests in 117 suites** (`swift test`). ### Shipped (libxml2-aligned surface) diff --git a/Sources/Parsing/ByteDecoder.swift b/Sources/Parsing/ByteDecoder.swift index a7a2d4c..0028d32 100644 --- a/Sources/Parsing/ByteDecoder.swift +++ b/Sources/Parsing/ByteDecoder.swift @@ -45,6 +45,12 @@ extension PureXML.Parsing { .latin6: SingleByte.iso8859_10, .latin8: SingleByte.iso8859_14, .latin10: SingleByte.iso8859_16, + .thai: SingleByte.iso8859_11, + .windows1250: SingleByte.windows1250, + .windows1251: SingleByte.windows1251, + .windows1253: SingleByte.windows1253, + .windows1257: SingleByte.windows1257, + .koi8r: SingleByte.koi8r, .latinCyrillic: SingleByte.iso8859_5, .latin5: SingleByte.iso8859_9, .latin9: SingleByte.iso8859_15, @@ -121,6 +127,12 @@ extension PureXML.Parsing { "iso-8859-10": .latin6, "iso8859-10": .latin6, "latin6": .latin6, "latin-6": .latin6, "l6": .latin6, "iso-8859-14": .latin8, "iso8859-14": .latin8, "latin8": .latin8, "latin-8": .latin8, "l8": .latin8, "iso-8859-16": .latin10, "iso8859-16": .latin10, "latin10": .latin10, "latin-10": .latin10, "l10": .latin10, + "iso-8859-11": .thai, "iso8859-11": .thai, "tis-620": .thai, "thai": .thai, + "windows-1250": .windows1250, "cp1250": .windows1250, + "windows-1251": .windows1251, "cp1251": .windows1251, + "windows-1253": .windows1253, "cp1253": .windows1253, + "windows-1257": .windows1257, "cp1257": .windows1257, + "koi8-r": .koi8r, "koi8r": .koi8r, "iso-8859-5": .latinCyrillic, "iso8859-5": .latinCyrillic, "cyrillic": .latinCyrillic, "iso-8859-9": .latin5, "iso8859-9": .latin5, "latin5": .latin5, "latin-5": .latin5, "l5": .latin5, "iso-8859-15": .latin9, "iso8859-15": .latin9, "latin9": .latin9, "latin-9": .latin9, "l9": .latin9, diff --git a/Sources/Parsing/InputEncoding.swift b/Sources/Parsing/InputEncoding.swift index 1d593b6..2cb4804 100644 --- a/Sources/Parsing/InputEncoding.swift +++ b/Sources/Parsing/InputEncoding.swift @@ -21,6 +21,12 @@ public extension PureXML.Parsing { case latin6 // ISO-8859-10 case latin8 // ISO-8859-14 case latin10 // ISO-8859-16 + case thai // ISO-8859-11 + case windows1250 + case windows1251 + case windows1253 + case windows1257 + case koi8r case latinCyrillic // ISO-8859-5 case latin5 // ISO-8859-9 (Turkish) case latin9 // ISO-8859-15 diff --git a/Sources/Parsing/SingleByteEncodings.swift b/Sources/Parsing/SingleByteEncodings.swift index 0ec810b..df7a288 100644 --- a/Sources/Parsing/SingleByteEncodings.swift +++ b/Sources/Parsing/SingleByteEncodings.swift @@ -105,6 +105,30 @@ extension PureXML.Parsing.ByteDecoder { upperHalf(byte, latin10Upper) } + static func iso8859_11(_ byte: UInt8) -> Unicode.Scalar { + upperHalf(byte, thaiUpper) + } + + static func windows1250(_ byte: UInt8) -> Unicode.Scalar { + fullHigh(byte, windows1250High) + } + + static func windows1251(_ byte: UInt8) -> Unicode.Scalar { + fullHigh(byte, windows1251High) + } + + static func windows1253(_ byte: UInt8) -> Unicode.Scalar { + fullHigh(byte, windows1253High) + } + + static func windows1257(_ byte: UInt8) -> Unicode.Scalar { + fullHigh(byte, windows1257High) + } + + static func koi8r(_ byte: UInt8) -> Unicode.Scalar { + fullHigh(byte, koi8rHigh) + } + /// Maps a byte through a 96-entry upper-half table (`0xA0`-`0xFF`); bytes /// below `0xA0` are ASCII or C1 controls (identity). Used by the encodings /// whose upper half is vendored from the Unicode mapping files. @@ -112,6 +136,13 @@ extension PureXML.Parsing.ByteDecoder { byte >= 0xA0 ? scalar(UInt32(table[Int(byte) - 0xA0])) : Unicode.Scalar(byte) } + /// Maps a byte through a 128-entry high-half table (`0x80`-`0xFF`); bytes + /// below `0x80` are ASCII. Used by the Windows code pages and KOI8, which + /// remap the `0x80`-`0x9F` range too. + static func fullHigh(_ byte: UInt8, _ table: [UInt16]) -> Unicode.Scalar { + byte >= 0x80 ? scalar(UInt32(table[Int(byte) - 0x80])) : Unicode.Scalar(byte) + } + static func scalar(_ value: UInt32) -> Unicode.Scalar { Unicode.Scalar(value) ?? "\u{FFFD}" } diff --git a/Sources/Parsing/SingleByteTables.swift b/Sources/Parsing/SingleByteTables.swift index 2ae5d69..a5bcac5 100644 --- a/Sources/Parsing/SingleByteTables.swift +++ b/Sources/Parsing/SingleByteTables.swift @@ -159,4 +159,120 @@ extension PureXML.Parsing.ByteDecoder.SingleByte { 0x0111, 0x0144, 0x00F2, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x015B, 0x0171, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0119, 0x021B, 0x00FF, ] + + /// ISO-8859-11 (Thai) upper half, from `8859-11.TXT` (undefined bytes -> FFFD). + static let thaiUpper: [UInt16] = [ + 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, + 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, + 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17, + 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F, + 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27, + 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F, + 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, + 0x0E38, 0x0E39, 0x0E3A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x0E3F, + 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, + 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F, + 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, + 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, + ] + + /// Windows-1250 (Central European) high half (bytes 0x80-0xFF), from `CP1250.TXT`. + static let windows1250High: [UInt16] = [ + 0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021, + 0xFFFD, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179, + 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0xFFFD, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A, + 0x00A0, 0x02C7, 0x02D8, 0x0141, 0x00A4, 0x0104, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B, + 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C, + 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, + 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, + 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, + 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, + 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, + 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, + 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, + 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9, + ] + + /// Windows-1251 (Cyrillic) high half, from `CP1251.TXT`. + static let windows1251High: [UInt16] = [ + 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021, + 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F, + 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0xFFFD, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F, + 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7, + 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407, + 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7, + 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457, + 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, + 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, + 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, + 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, + 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, + 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, + 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, + 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, + ] + + /// Windows-1253 (Greek) high half, from `CP1253.TXT` (undefined bytes -> FFFD). + static let windows1253High: [UInt16] = [ + 0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, + 0xFFFD, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, + 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0xFFFD, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, + 0x00A0, 0x0385, 0x0386, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, + 0x00A8, 0x00A9, 0xFFFD, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x2015, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5, 0x00B6, 0x00B7, + 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F, + 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, + 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, + 0x03A0, 0x03A1, 0xFFFD, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, + 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF, + 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, + 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, + 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, + 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0xFFFD, + ] + + /// Windows-1257 (Baltic) high half, from `CP1257.TXT` (undefined bytes -> FFFD). + static let windows1257High: [UInt16] = [ + 0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021, + 0xFFFD, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0x00A8, 0x02C7, 0x00B8, + 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0xFFFD, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0x00AF, 0x02DB, 0xFFFD, + 0x00A0, 0xFFFD, 0x00A2, 0x00A3, 0x00A4, 0xFFFD, 0x00A6, 0x00A7, + 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, + 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6, + 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112, + 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B, + 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7, + 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF, + 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113, + 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C, + 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7, + 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x02D9, + ] + + /// KOI8-R (Cyrillic) high half, from `KOI8-R.TXT`. + static let koi8rHigh: [UInt16] = [ + 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524, + 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590, + 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248, + 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7, + 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556, + 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x255C, 0x255D, 0x255E, + 0x255F, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565, + 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x256B, 0x256C, 0x00A9, + 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, + 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, + 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, + 0x044C, 0x044B, 0x0437, 0x0448, 0x044D, 0x0449, 0x0447, 0x044A, + 0x042E, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, + 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, + 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, + 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A, + ] } diff --git a/Tests/EncodingTests.swift b/Tests/EncodingTests.swift index cfffeb3..f426038 100644 --- a/Tests/EncodingTests.swift +++ b/Tests/EncodingTests.swift @@ -126,6 +126,20 @@ struct EncodingTests { try #expect(decoded("ISO-8859-16", [0xAA]) == "\u{0218}") // Ș } + @Test("Decodes the Windows code pages, KOI8-R, and Thai from vendored tables") + func test_windowsAndKoi8() throws { + try #expect(decoded("ISO-8859-11", [0xA1]) == "\u{0E01}") // ก + try #expect(decoded("windows-1250", [0x8A]) == "\u{0160}") // Š + try #expect(decoded("windows-1250", [0xC8]) == "\u{010C}") // Č + try #expect(decoded("windows-1251", [0xC0]) == "\u{0410}") // А + try #expect(decoded("windows-1251", [0x88]) == "\u{20AC}") // € + try #expect(decoded("windows-1253", [0xC1]) == "\u{0391}") // Α + try #expect(decoded("windows-1257", [0xA8]) == "\u{00D8}") // Ø + try #expect(decoded("koi8-r", [0xE1]) == "\u{0410}") // А + try #expect(decoded("koi8-r", [0xC1]) == "\u{0430}") // а + try #expect(decoded("koi8-r", [0xA3]) == "\u{0451}") // ё + } + @Test("Decodes ISO-8859-5: the Cyrillic block") func test_iso8859_5() throws { try #expect(decoded("ISO-8859-5", [0xB0]) == "\u{0410}") // А