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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! EUC-JP, § 12.1.//!//! Three character sets in one byte stream, told apart by a prefix: bare//! pairs are JIS X 0208, a leading 0x8E means halfwidth katakana, and a//! leading 0x8F means JIS X 0212. The last of those is decode-only, here and//! everywhere else -- the standard says in as many words that JIS X 0212 is//! not widely enough supported to emit.
const std = @import("std");
const codec = @import("../codec.zig");const index = @import("../index.zig");const jis0208 = @import("../tables/jis0208.zig");const jis0212 = @import("../tables/jis0212.zig");
const ByteQueue = codec.ByteQueue;const Decoded = codec.Decoded;const Encoded = codec.Encoded;
const jis0208_index: index.Index(u16) = .{ .code_points = &jis0208.encode_code_points, .pointers = &jis0208.encode_pointers,};
pub const Decoder = struct { /// Set by a leading 0x8F, and read by the pair that follows it. is_jis0212: bool = false, 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 == 0x8e and b >= 0xa1 and b <= 0xdf) { self.leading = 0; return .{ .code_point = 0xff61 - 0xa1 + @as(u21, b) }; } if (self.leading == 0x8f and b >= 0xa1 and b <= 0xfe) { self.is_jis0212 = true; self.leading = b; return .cont; }
if (self.leading != 0) { const leading = self.leading; self.leading = 0;
var code_point: ?u21 = null; if (leading >= 0xa1 and leading <= 0xfe and b >= 0xa1 and b <= 0xfe) { const pointer = (@as(u32, leading - 0xa1) * 94) + (b - 0xa1); code_point = if (self.is_jis0212) index.codePoint(&jis0212.decode, pointer) else index.codePoint(&jis0208.decode, pointer); } self.is_jis0212 = false;
if (code_point) |c| return .{ .code_point = c }; if (b < 0x80) queue.restore(&.{b}); return .failure; }
if (b < 0x80) return .{ .code_point = b }; if (b == 0x8e or b == 0x8f or (b >= 0xa1 and b <= 0xfe)) { self.leading = b; return .cont; } return .failure; }};
pub const Encoder = struct { pub fn handle(_: *Encoder, code_point: ?u21) Encoded { var c = code_point orelse return .finished; if (c < 0x80) return .{ .bytes = .one(@intCast(c)) };
if (c == 0x00a5) return .{ .bytes = .one(0x5c) }; if (c == 0x203e) return .{ .bytes = .one(0x7e) }; if (c >= 0xff61 and c <= 0xff9f) { return .{ .bytes = .two(0x8e, @intCast(c - 0xff61 + 0xa1)) }; } if (c == 0x2212) c = 0xff0d;
const pointer = jis0208_index.pointer(c) orelse return .{ .failure = c }; return .{ .bytes = .two( @intCast(pointer / 94 + 0xa1), @intCast(pointer % 94 + 0xa1), ) }; }};