Merge #97: complete single-byte legacy encodings

This commit is contained in:
Mihaela Mihaljevic 2026-06-09 07:29:26 +02:00
commit e3e42f5dc1
6 changed files with 117 additions and 4 deletions

View File

@ -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 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.
- Single-byte legacy encodings (#97): the byte decoder gains a table-based single-byte path and the full set of single-byte legacy encodings, selected by the XML declaration's `encoding` name for both the whole-buffer and streaming decoders: the complete ISO-8859 family (parts 2 through 16), Windows-1250/1251/1253/1254/1255/1256/1257/1258, and KOI8-R/U. Most are vendored verbatim from the authoritative `unicode.org` mapping files; ISO-8859-5/9/15 are derived exactly; Windows-1254 composes from Windows-1252. Multi-byte CJK encodings (#99) are tracked separately.
### Fixed

View File

@ -50,7 +50,11 @@ extension PureXML.Parsing {
.windows1251: SingleByte.windows1251,
.windows1253: SingleByte.windows1253,
.windows1257: SingleByte.windows1257,
.windows1255: SingleByte.windows1255,
.windows1256: SingleByte.windows1256,
.windows1258: SingleByte.windows1258,
.koi8r: SingleByte.koi8r,
.koi8u: SingleByte.koi8u,
.latinCyrillic: SingleByte.iso8859_5,
.latin5: SingleByte.iso8859_9,
.latin9: SingleByte.iso8859_15,
@ -132,7 +136,11 @@ extension PureXML.Parsing {
"windows-1251": .windows1251, "cp1251": .windows1251,
"windows-1253": .windows1253, "cp1253": .windows1253,
"windows-1257": .windows1257, "cp1257": .windows1257,
"windows-1255": .windows1255, "cp1255": .windows1255,
"windows-1256": .windows1256, "cp1256": .windows1256,
"windows-1258": .windows1258, "cp1258": .windows1258,
"koi8-r": .koi8r, "koi8r": .koi8r,
"koi8-u": .koi8u, "koi8u": .koi8u,
"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,

View File

@ -26,7 +26,11 @@ public extension PureXML.Parsing {
case windows1251
case windows1253
case windows1257
case windows1255
case windows1256
case windows1258
case koi8r
case koi8u
case latinCyrillic // ISO-8859-5
case latin5 // ISO-8859-9 (Turkish)
case latin9 // ISO-8859-15

View File

@ -1,9 +1,9 @@
extension PureXML.Parsing.ByteDecoder {
/// To-Unicode mappings for the single-byte legacy encodings. Each maps one
/// byte to its Unicode scalar; bytes `0x00`-`0x7F` are ASCII in every case.
/// The encodings here are the ones expressible exactly as ISO-8859-1 with a
/// few substitutions or as a contiguous block, so they carry no risk of a
/// transcription error.
/// A few encodings are expressed exactly (ISO-8859-1 with substitutions, or a
/// contiguous block); the rest are vendored verbatim from the authoritative
/// `unicode.org` mapping files (see `SingleByteTables.swift`).
enum SingleByte {
/// ISO-8859-15 (Latin-9): ISO-8859-1 with the euro sign and seven other
/// substitutions.
@ -129,6 +129,22 @@ extension PureXML.Parsing.ByteDecoder {
fullHigh(byte, koi8rHigh)
}
static func windows1255(_ byte: UInt8) -> Unicode.Scalar {
fullHigh(byte, windows1255High)
}
static func windows1256(_ byte: UInt8) -> Unicode.Scalar {
fullHigh(byte, windows1256High)
}
static func windows1258(_ byte: UInt8) -> Unicode.Scalar {
fullHigh(byte, windows1258High)
}
static func koi8u(_ byte: UInt8) -> Unicode.Scalar {
fullHigh(byte, koi8uHigh)
}
/// 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.

View File

@ -275,4 +275,84 @@ extension PureXML.Parsing.ByteDecoder.SingleByte {
0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412,
0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A,
]
/// Windows-1255 (Hebrew) high half, from `CP1255.TXT` (undefined bytes -> FFFD).
static let windows1255High: [UInt16] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
0x02DC, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AA, 0x00A5, 0x00A6, 0x00A7,
0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
0x05B8, 0x05B9, 0xFFFD, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,
0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3,
0x05F4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,
0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD,
]
/// Windows-1256 (Arabic) high half, from `CP1256.TXT`.
static let windows1256High: [UInt16] = [
0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688,
0x06AF, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
0x06A9, 0x2122, 0x0691, 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA,
0x00A0, 0x060C, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x061F,
0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,
0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7,
0x0637, 0x0638, 0x0639, 0x063A, 0x0640, 0x0641, 0x0642, 0x0643,
0x00E0, 0x0644, 0x00E2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7,
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0649, 0x064A, 0x00EE, 0x00EF,
0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7,
0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E, 0x200F, 0x06D2,
]
/// Windows-1258 (Vietnamese) high half, from `CP1258.TXT` (undefined -> FFFD).
static let windows1258High: [UInt16] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0xFFFD, 0x2039, 0x0152, 0xFFFD, 0xFFFD, 0xFFFD,
0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
0x02DC, 0x2122, 0xFFFD, 0x203A, 0x0153, 0xFFFD, 0xFFFD, 0x0178,
0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x0300, 0x00CD, 0x00CE, 0x00CF,
0x0110, 0x00D1, 0x0309, 0x00D3, 0x00D4, 0x01A0, 0x00D6, 0x00D7,
0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x01AF, 0x0303, 0x00DF,
0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0301, 0x00ED, 0x00EE, 0x00EF,
0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7,
0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0, 0x20AB, 0x00FF,
]
/// KOI8-U (Cyrillic, Ukrainian) high half, from `KOI8-U.TXT`.
static let koi8uHigh: [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, 0x0454, 0x2554, 0x0456, 0x0457,
0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x0491, 0x255D, 0x255E,
0x255F, 0x2560, 0x2561, 0x0401, 0x0404, 0x2563, 0x0406, 0x0407,
0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x0490, 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,
]
}

View File

@ -138,6 +138,11 @@ struct EncodingTests {
try #expect(decoded("koi8-r", [0xE1]) == "\u{0410}") // А
try #expect(decoded("koi8-r", [0xC1]) == "\u{0430}") // а
try #expect(decoded("koi8-r", [0xA3]) == "\u{0451}") // ё
try #expect(decoded("windows-1255", [0xE0]) == "\u{05D0}") // א
try #expect(decoded("windows-1255", [0xA4]) == "\u{20AA}") // ₪
try #expect(decoded("windows-1256", [0xC7]) == "\u{0627}") // ا
try #expect(decoded("windows-1258", [0xFE]) == "\u{20AB}") // ₫
try #expect(decoded("koi8-u", [0xA4]) == "\u{0454}") // є (KOI8-U specific)
}
@Test("Decodes ISO-8859-5: the Cyrillic block")