Something went wrong. Try again.
Converting between UTF-8 and legacy character encodings, as the WHATWG Encoding Standard and glibc's iconv define them.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Big5 as glibc 2.44 has it.//!//! The standard's Big5 is Big5-HKSCS. glibc's is Big5 itself, which is the//! standard's index with the Hong Kong additions taken out and a few cells//! changed://!//! - Lead bytes 0xA1 to 0xF9 only, so none of HKSCS's rows below 0xA1 or//! above 0xF9.//! - 0xA3C0 to 0xA3E0, where the standard puts the control pictures, are//! unassigned.//! - 0xC6A1 to 0xC8FE, where the standard has the ETEN extensions, are the//! Private Use Area: U+F6B1 onward, a cell at a time, in pointer order.//! - 0xF9FE is U+2593 DARK SHADE, where the standard has U+FFED.//! - The single byte 0x80 is U+0080, both ways.//!//! Writing, a character the table has twice is written as the first of its//! cells, except for 十 and 卅, which are written as the second: 0xA451 and//! 0xA4CA rather than 0xA2CC and 0xA2CE.
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,};
fn pointerOf(lead: u8, trail: u8) u16 { return @as(u16, lead - 0x81) * 157 + (trail - @as(u8, if (trail < 0x7f) 0x40 else 0x62));}
const private_use_first = pointerOf(0xc6, 0xa1);const private_use_last = pointerOf(0xc8, 0xfe);const private_use_start = 0xf6b1;const dark_shade = pointerOf(0xf9, 0xfe);
/// Whether a pointer is a cell of Big5 that glibc takes from the standard's/// index as it is.fn fromIndex(pointer: u16) bool { if (pointer < pointerOf(0xa1, 0x40) or pointer > pointerOf(0xf9, 0xfe)) return false; if (pointer >= pointerOf(0xa3, 0xc0) and pointer <= pointerOf(0xa3, 0xe0)) return false; if (pointer >= private_use_first and pointer <= private_use_last) return false; return pointer != dark_shade;}
fn decodeCell(pointer: u16) ?u21 { if (pointer >= private_use_first and pointer <= private_use_last) return private_use_start + (pointer - private_use_first); if (pointer == dark_shade) return 0x2593; if (!fromIndex(pointer)) return null; return index.codePointWide(&tables.decode, pointer);}
fn encodeCell(c: u21) ?u16 { if (c >= private_use_start and c <= private_use_start + (private_use_last - private_use_first)) { return private_use_first + @as(u16, @intCast(c - private_use_start)); } if (c == 0x2593) return dark_shade; const run = big5_index.run(c); const last = c == 0x5341 or c == 0x5345; var chosen: ?u16 = null; for (run) |pointer| { if (!fromIndex(pointer)) continue; chosen = pointer; if (!last) break; } return chosen;}
pub const Decoder = struct { lead: u8 = 0,
pub fn handle(self: *Decoder, queue: *ByteQueue, byte: ?u8) Decoded { const b = byte orelse { if (self.lead == 0) return .finished; self.lead = 0; return .failure; };
if (self.lead != 0) { const lead = self.lead; self.lead = 0; if ((b >= 0x40 and b <= 0x7e) or (b >= 0xa1 and b <= 0xfe)) { if (decodeCell(pointerOf(lead, b))) |c| return .{ .code_point = c }; return .failure; } if (b < 0x80) queue.restore(&.{b}); return .failure; }
if (b <= 0x80) return .{ .code_point = b }; if (b >= 0xa1 and b <= 0xf9) { self.lead = b; return .cont; } return .failure; }};
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 = encodeCell(c) orelse return .{ .failure = c }; const offset = pointer % 157; return .{ .bytes = .two( @intCast(pointer / 157 + 0x81), @intCast(offset + @as(u16, if (offset < 0x3f) 0x40 else 0x62)), ) }; }};
test "the ETEN extensions are private use, a cell at a time" { try std.testing.expectEqual(@as(?u21, 0xf6b1), decodeCell(pointerOf(0xc6, 0xa1))); try std.testing.expectEqual(@as(?u21, 0xf848), decodeCell(pointerOf(0xc8, 0xfe))); try std.testing.expectEqual(@as(?u16, pointerOf(0xc7, 0x40)), encodeCell(decodeCell(pointerOf(0xc7, 0x40)).?)); // ① is in the standard's index at 0xC6A1, and so has no bytes here. try std.testing.expectEqual(@as(?u16, null), encodeCell(0x2460));}
test "十 and 卅 are written as their second cells" { try std.testing.expectEqual(@as(?u16, pointerOf(0xa4, 0x51)), encodeCell(0x5341)); try std.testing.expectEqual(@as(?u16, pointerOf(0xa4, 0xca)), encodeCell(0x5345)); // ═ is at 0xA2A4 and 0xF9F9, and is written as the first. try std.testing.expectEqual(@as(?u16, pointerOf(0xa2, 0xa4)), encodeCell(0x2550));}