// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Big5, § 11. //! //! The index here is Big5 together with the Hong Kong Supplementary Character //! Set and other common extensions, which is why the encoder reads it with //! everything below pointer 5024 taken out: those are the extensions, and //! emitting them literally would produce bytes a plain Big5 consumer has //! never heard of. Decoding them is fine, and the decoder does. //! //! It is also the only decoder in the standard that can return two code //! points for one pointer. Four Big5 characters are a letter with two //! diacritics and Unicode spells them with a combining mark, which an index //! -- one pointer, one code point -- cannot hold. const std = @import("std"); const codec = @import("../codec.zig"); const index = @import("../index.zig"); const tables = @import("../tables/big5.zig"); const ByteQueue = codec.ByteQueue; const Decoded = codec.Decoded; const Encoded = codec.Encoded; const big5_index: index.Index(u32) = .{ .code_points = &tables.encode_code_points, .pointers = &tables.encode_pointers, }; /// Pointers below this are the Hong Kong Supplementary Character Set /// extensions: `(0xA1 - 0x81) × 157`. const extensions_end = (0xa1 - 0x81) * 157; pub const Decoder = struct { leading: u8 = 0, pub fn handle(self: *Decoder, queue: *ByteQueue, byte: ?u8) Decoded { const b = byte orelse { if (self.leading == 0) return .finished; self.leading = 0; return .failure; }; if (self.leading != 0) { const leading = self.leading; self.leading = 0; if ((b >= 0x40 and b <= 0x7e) or (b >= 0xa1 and b <= 0xfe)) { const offset: u8 = if (b < 0x7f) 0x40 else 0x62; const pointer = (@as(u32, leading - 0x81) * 157) + (b - offset); switch (pointer) { 1133 => return .{ .code_points = .{ 0x00ca, 0x0304 } }, 1135 => return .{ .code_points = .{ 0x00ca, 0x030c } }, 1164 => return .{ .code_points = .{ 0x00ea, 0x0304 } }, 1166 => return .{ .code_points = .{ 0x00ea, 0x030c } }, else => {}, } if (index.codePointWide(&tables.decode, pointer)) |c| return .{ .code_point = c }; } if (b < 0x80) queue.restore(&.{b}); return .failure; } if (b < 0x80) return .{ .code_point = b }; if (b >= 0x81 and b <= 0xfe) { self.leading = b; return .cont; } return .failure; } }; /// The six code points for which the standard wants the *last* pointer rather /// than the first. Everything else with more than one pointer takes the /// first. fn wantsLastPointer(code_point: u21) bool { return switch (code_point) { 0x2550, 0x255e, 0x2561, 0x256a, 0x5341, 0x5345 => true, else => false, }; } /// The standard's "index Big5 pointer for codePoint". fn big5Pointer(code_point: u21) ?u16 { const run = big5_index.run(code_point); // The exclusion is by pointer, and the run is sorted by pointer, so // trimming the front of it is the whole of "excluding all entries whose // pointer is less than 5024". var start: usize = 0; while (start < run.len and run[start] < extensions_end) start += 1; const eligible = run[start..]; if (eligible.len == 0) return null; return if (wantsLastPointer(code_point)) eligible[eligible.len - 1] else eligible[0]; } pub const Encoder = struct { pub fn handle(_: *Encoder, code_point: ?u21) Encoded { const c = code_point orelse return .finished; if (c < 0x80) return .{ .bytes = .one(@intCast(c)) }; const pointer = big5Pointer(c) orelse return .{ .failure = c }; const leading: u8 = @intCast(pointer / 157 + 0x81); const trailing = pointer % 157; const offset: u8 = if (trailing < 0x3f) 0x40 else 0x62; return .{ .bytes = .two(leading, @intCast(trailing + offset)) }; } };