// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! EUC-KR, § 13.1. //! //! Despite the name this is Windows code page 949 -- KS X 1001 together with //! the Unified Hangul Code -- which covers the Hangul Syllables block of //! Unicode in its entirety. It is the simplest of the multi-byte encodings //! here: one leading byte, one trailing byte, no modes, no duplicates in the //! index and so no choice to make when encoding. const std = @import("std"); const codec = @import("../codec.zig"); const index = @import("../index.zig"); const tables = @import("../tables/euc_kr.zig"); const ByteQueue = codec.ByteQueue; const Decoded = codec.Decoded; const Encoded = codec.Encoded; const euc_kr_index: index.Index(u16) = .{ .code_points = &tables.encode_code_points, .pointers = &tables.encode_pointers, }; 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 >= 0x41 and b <= 0xfe) { const pointer = (@as(u32, leading - 0x81) * 190) + (b - 0x41); if (index.codePoint(&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; } }; 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 = euc_kr_index.pointer(c) orelse return .{ .failure = c }; return .{ .bytes = .two( @intCast(pointer / 190 + 0x81), @intCast(pointer % 190 + 0x41), ) }; } };