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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Shift_JIS, § 12.3.//!//! Also called Windows code page 932, which is what the labels `ms932` and//! `windows-31j` are getting at. It reads index jis0208 through two shifts://! the leading byte comes from two disjoint ranges, and the trailing byte//! skips 0x7F.//!//! Two things here are Windows rather than JIS. Pointers 8836 through 10715//! are the end-user defined characters, which the standard maps straight into//! the private use area without an index; and the encoder skips pointers 8272//! through 8835, which is what pushes a handful of duplicated code points//! onto their later, and more widely understood, pointer.
const std = @import("std");
const codec = @import("../codec.zig");const index = @import("../index.zig");const tables = @import("../tables/jis0208.zig");
const ByteQueue = codec.ByteQueue;const Decoded = codec.Decoded;const Encoded = codec.Encoded;
const jis0208_index: index.Index(u16) = .{ .code_points = &tables.encode_code_points, .pointers = &tables.encode_pointers,};
/// The end-user defined character range, mapped to the private use area/// starting at U+E000.const eudc_first = 8836;const eudc_last = 10715;
/// Pointers the encoder does not use. The index contains each of the code/// points behind them somewhere later as well, and the later spelling is the/// interoperable one.const excluded_first = 8272;const excluded_last = 8835;
pub const Decoder = struct { leading: u8 = 0, /// Code page 932 as glibc has it, where 0x80 is not a character. glibc: bool = false,
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 >= 0x80 and b <= 0xfc)) { const offset: u8 = if (b < 0x7f) 0x40 else 0x41; const leading_offset: u8 = if (leading < 0xa0) 0x81 else 0xc1; const pointer = (@as(u32, leading - leading_offset) * 188) + (b - offset);
if (pointer >= eudc_first and pointer <= eudc_last) { return .{ .code_point = @intCast(0xe000 - eudc_first + pointer) }; } if (index.codePoint(&tables.decode, pointer)) |c| return .{ .code_point = c }; } if (b < 0x80) queue.restore(&.{b}); return .failure; }
// 0x80 is not ASCII and is not a leading byte: Shift_JIS passes it // through as U+0080. if (b < 0x80 or (b == 0x80 and !self.glibc)) return .{ .code_point = b }; // Halfwidth katakana, which Shift_JIS puts in a single-byte range. if (b >= 0xa1 and b <= 0xdf) return .{ .code_point = 0xff61 - 0xa1 + @as(u21, b) }; if ((b >= 0x81 and b <= 0x9f) or (b >= 0xe0 and b <= 0xfc)) { self.leading = b; return .cont; } return .failure; }};
/// The standard's "index Shift_JIS pointer for codePoint".fn shiftJisPointer(code_point: u21) ?u16 { for (jis0208_index.run(code_point)) |pointer| { if (pointer < excluded_first or pointer > excluded_last) return pointer; } return null;}
/// What glibc's code page 932 writes that the standard's Shift_JIS does not:/// the Latin-1 cent, pound and not signs as their fullwidth forms, and three/// characters as the JIS X 0208 positions they were first mapped from. None/// of these decodes back to what was written.const glibc_one_way = [_]struct { code_point: u21, bytes: [2]u8 }{ .{ .code_point = 0x00a2, .bytes = .{ 0x81, 0x91 } }, .{ .code_point = 0x00a3, .bytes = .{ 0x81, 0x92 } }, .{ .code_point = 0x00ac, .bytes = .{ 0x81, 0xca } }, .{ .code_point = 0x2014, .bytes = .{ 0x81, 0x5c } }, .{ .code_point = 0x2016, .bytes = .{ 0x81, 0x61 } }, .{ .code_point = 0x301c, .bytes = .{ 0x81, 0x60 } },};
pub const Encoder = struct { /// Code page 932 as glibc has it: 0x80 is not a character, the end-user /// defined characters are written back to where they decode from, and /// `glibc_one_way` is written too. glibc: bool = false,
pub fn handle(self: *Encoder, code_point: ?u21) Encoded { var c = code_point orelse return .finished; if (self.glibc) { if (c == 0x80) return .{ .failure = c }; if (c >= 0xe000 and c <= 0xe000 + eudc_last - eudc_first) { return .{ .bytes = bytesOf(@intCast(c - 0xe000 + eudc_first)) }; } for (glibc_one_way) |entry| { if (entry.code_point == c) return .{ .bytes = .two(entry.bytes[0], entry.bytes[1]) }; } } if (c <= 0x80) return .{ .bytes = .one(@intCast(c)) };
// Shift_JIS has no yen sign or overline of its own: it reuses the // ASCII positions, which is the whole reason a Japanese path name can // arrive with a yen sign where a backslash was meant. if (c == 0x00a5) return .{ .bytes = .one(0x5c) }; if (c == 0x203e) return .{ .bytes = .one(0x7e) }; if (c >= 0xff61 and c <= 0xff9f) return .{ .bytes = .one(@intCast(c - 0xff61 + 0xa1)) }; if (c == 0x2212) c = 0xff0d;
const pointer = shiftJisPointer(c) orelse return .{ .failure = c }; return .{ .bytes = bytesOf(pointer) }; }};
fn bytesOf(pointer: u16) codec.Bytes { const leading = pointer / 188; const leading_offset: u8 = if (leading < 0x1f) 0x81 else 0xc1; const trailing = pointer % 188; const offset: u8 = if (trailing < 0x3f) 0x40 else 0x41; return .two( @intCast(leading + leading_offset), @intCast(trailing + offset), );}