// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The legacy single-byte encodings, § 9. //! //! Twenty-eight encodings share one decoder and one encoder and differ only //! in a table of 128 entries. Bytes below 0x80 are their own code point //! without the table being consulted at all, which is why only the upper half //! is stored. const std = @import("std"); const codec = @import("../codec.zig"); const Encoding = @import("../encoding.zig").Encoding; const byte_table = @import("../byte_table.zig"); const tables = @import("../tables/single_byte.zig"); const ByteQueue = codec.ByteQueue; const Decoded = codec.Decoded; const Encoded = codec.Encoded; /// The index for an encoding, or null if it is not a single-byte one. /// /// ISO-8859-8-I shares ISO-8859-8's table: the two are one encoding with two /// names, kept apart because the name tells a consumer something about layout /// direction that the bytes do not. pub fn index(encoding: Encoding) ?*const [128]u16 { return switch (encoding) { .ibm866 => &tables.ibm866, .iso_8859_2 => &tables.iso_8859_2, .iso_8859_3 => &tables.iso_8859_3, .iso_8859_4 => &tables.iso_8859_4, .iso_8859_5 => &tables.iso_8859_5, .iso_8859_6 => &tables.iso_8859_6, .iso_8859_7 => &tables.iso_8859_7, .iso_8859_8, .iso_8859_8_i => &tables.iso_8859_8, .iso_8859_10 => &tables.iso_8859_10, .iso_8859_13 => &tables.iso_8859_13, .iso_8859_14 => &tables.iso_8859_14, .iso_8859_15 => &tables.iso_8859_15, .iso_8859_16 => &tables.iso_8859_16, .koi8_r => &tables.koi8_r, .koi8_u => &tables.koi8_u, .macintosh => &tables.macintosh, .windows_874 => &tables.windows_874, .windows_1250 => &tables.windows_1250, .windows_1251 => &tables.windows_1251, .windows_1252 => &tables.windows_1252, .windows_1253 => &tables.windows_1253, .windows_1254 => &tables.windows_1254, .windows_1255 => &tables.windows_1255, .windows_1256 => &tables.windows_1256, .windows_1257 => &tables.windows_1257, .windows_1258 => &tables.windows_1258, .x_mac_cyrillic => &tables.x_mac_cyrillic, else => null, }; } pub const Decoder = struct { index: *const [128]u16, pub fn handle(self: *Decoder, _: *ByteQueue, byte: ?u8) Decoded { const b = byte orelse return .finished; if (b < 0x80) return .{ .code_point = b }; const code_point = self.index[b - 0x80]; if (code_point == codec.unmapped) return .failure; return .{ .code_point = code_point }; } }; /// The reversed tables, built at compile time: a sort of at most 128 entries /// per encoding while the program is being built, and a binary search rather /// than a scan at run time. const reversed = struct { const ibm866 = byte_table.reverse(tables.ibm866, 0x80); const iso_8859_2 = byte_table.reverse(tables.iso_8859_2, 0x80); const iso_8859_3 = byte_table.reverse(tables.iso_8859_3, 0x80); const iso_8859_4 = byte_table.reverse(tables.iso_8859_4, 0x80); const iso_8859_5 = byte_table.reverse(tables.iso_8859_5, 0x80); const iso_8859_6 = byte_table.reverse(tables.iso_8859_6, 0x80); const iso_8859_7 = byte_table.reverse(tables.iso_8859_7, 0x80); const iso_8859_8 = byte_table.reverse(tables.iso_8859_8, 0x80); const iso_8859_10 = byte_table.reverse(tables.iso_8859_10, 0x80); const iso_8859_13 = byte_table.reverse(tables.iso_8859_13, 0x80); const iso_8859_14 = byte_table.reverse(tables.iso_8859_14, 0x80); const iso_8859_15 = byte_table.reverse(tables.iso_8859_15, 0x80); const iso_8859_16 = byte_table.reverse(tables.iso_8859_16, 0x80); const koi8_r = byte_table.reverse(tables.koi8_r, 0x80); const koi8_u = byte_table.reverse(tables.koi8_u, 0x80); const macintosh = byte_table.reverse(tables.macintosh, 0x80); const windows_874 = byte_table.reverse(tables.windows_874, 0x80); const windows_1250 = byte_table.reverse(tables.windows_1250, 0x80); const windows_1251 = byte_table.reverse(tables.windows_1251, 0x80); const windows_1252 = byte_table.reverse(tables.windows_1252, 0x80); const windows_1253 = byte_table.reverse(tables.windows_1253, 0x80); const windows_1254 = byte_table.reverse(tables.windows_1254, 0x80); const windows_1255 = byte_table.reverse(tables.windows_1255, 0x80); const windows_1256 = byte_table.reverse(tables.windows_1256, 0x80); const windows_1257 = byte_table.reverse(tables.windows_1257, 0x80); const windows_1258 = byte_table.reverse(tables.windows_1258, 0x80); const x_mac_cyrillic = byte_table.reverse(tables.x_mac_cyrillic, 0x80); }; /// The reversed index for an encoding, or null if it is not a single-byte /// one. pub fn reversedIndex(encoding: Encoding) ?*const byte_table.Reversed(128) { return switch (encoding) { .ibm866 => &reversed.ibm866, .iso_8859_2 => &reversed.iso_8859_2, .iso_8859_3 => &reversed.iso_8859_3, .iso_8859_4 => &reversed.iso_8859_4, .iso_8859_5 => &reversed.iso_8859_5, .iso_8859_6 => &reversed.iso_8859_6, .iso_8859_7 => &reversed.iso_8859_7, .iso_8859_8, .iso_8859_8_i => &reversed.iso_8859_8, .iso_8859_10 => &reversed.iso_8859_10, .iso_8859_13 => &reversed.iso_8859_13, .iso_8859_14 => &reversed.iso_8859_14, .iso_8859_15 => &reversed.iso_8859_15, .iso_8859_16 => &reversed.iso_8859_16, .koi8_r => &reversed.koi8_r, .koi8_u => &reversed.koi8_u, .macintosh => &reversed.macintosh, .windows_874 => &reversed.windows_874, .windows_1250 => &reversed.windows_1250, .windows_1251 => &reversed.windows_1251, .windows_1252 => &reversed.windows_1252, .windows_1253 => &reversed.windows_1253, .windows_1254 => &reversed.windows_1254, .windows_1255 => &reversed.windows_1255, .windows_1256 => &reversed.windows_1256, .windows_1257 => &reversed.windows_1257, .windows_1258 => &reversed.windows_1258, .x_mac_cyrillic => &reversed.x_mac_cyrillic, else => null, }; } pub const Encoder = struct { index: *const byte_table.Reversed(128), pub fn handle(self: *Encoder, code_point: ?u21) Encoded { const c = code_point orelse return .finished; if (c < 0x80) return .{ .bytes = .one(@intCast(c)) }; const byte = self.index.byte(c) orelse return .{ .failure = c }; return .{ .bytes = .one(byte) }; } }; test "the reversed index agrees with the index it came from" { for (std.enums.values(Encoding)) |encoding| { const forward = index(encoding) orelse continue; const backward = reversedIndex(encoding).?; for (forward, 0..) |code_point, offset| { if (code_point == codec.unmapped) continue; // Every single-byte index is injective, so the byte that comes // back is the byte that went in. try std.testing.expectEqual(@as(?u8, @intCast(offset + 0x80)), backward.byte(code_point)); } try std.testing.expectEqual(@as(?u8, null), backward.byte(0xffff)); } }