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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! JIS X 0208 and JIS X 0212 as glibc 2.44 has them, for its ISO-2022-JP and//! EUC-JP.//!//! JIS X 0208 is the standard's index without what it took from code page//! 932 -- row 13's NEC symbols and rows 89 to 92's IBM kanji -- and with six//! cells read as JIS X 0208 itself has them rather than as Microsoft does://! 0x2141 is U+301C WAVE DASH, 0x2142 U+2016, 0x215D U+2212 MINUS SIGN, and//! 0x2171, 0x2172 and 0x224C the plain U+00A2, U+00A3 and U+00AC. The//! fullwidth forms the standard has in those cells cannot be written.//!//! JIS X 0212 is the standard's index exactly, and unlike the standard's//! EUC-JP, glibc writes it as well as reading it.//!//! Both take a cell as its row and column bytes in their seven-bit form,//! 0x21 to 0x7E; EUC-JP's eight-bit bytes have the top bit taken off first.
const std = @import("std");
const index = @import("../index.zig");const jis0208 = @import("../tables/jis0208.zig");const jis0212 = @import("../tables/jis0212.zig");
const jis0208_index: index.Index(u16) = .{ .code_points = &jis0208.encode_code_points, .pointers = &jis0208.encode_pointers,};
const jis0212_index: index.Index(u16) = .{ .code_points = &jis0212.encode_code_points, .pointers = &jis0212.encode_pointers,};
/// The cells where glibc and the standard's index disagree, with glibc's/// reading of each: JIS X 0208's own characters where the index has code/// page 932's.const exceptions = [_]struct { pointer: u16, code_point: u21 }{ .{ .pointer = 32, .code_point = 0x301c }, .{ .pointer = 33, .code_point = 0x2016 }, .{ .pointer = 60, .code_point = 0x2212 }, .{ .pointer = 80, .code_point = 0x00a2 }, .{ .pointer = 81, .code_point = 0x00a3 }, .{ .pointer = 137, .code_point = 0x00ac },};
/// Whether a pointer is in a row of JIS X 0208 itself, rather than one code/// page 932 added: row 13, and everything from row 89 on.fn inJis0208(pointer: usize) bool { const row = pointer / 94 + 1; return row != 13 and row <= 84;}
/// A cell, as the two bytes that name it.pub const Cell = struct { row: u8, column: u8 };
fn pointerOf(row: u8, column: u8) usize { return @as(usize, row - 0x21) * 94 + (column - 0x21);}
fn cellOf(pointer: u16) Cell { return .{ .row = @intCast(pointer / 94 + 0x21), .column = @intCast(pointer % 94 + 0x21) };}
/// The character at a JIS X 0208 cell. Both bytes must be 0x21 to 0x7E.pub fn decode0208(row: u8, column: u8) ?u21 { const pointer = pointerOf(row, column); for (exceptions) |e| if (e.pointer == pointer) return e.code_point; if (!inJis0208(pointer)) return null; return index.codePoint(&jis0208.decode, pointer);}
/// The JIS X 0208 cell for a character, if it has one.pub fn encode0208(c: u21) ?Cell { for (exceptions) |e| if (e.code_point == c) return cellOf(e.pointer); const pointer = jis0208_index.pointer(c) orelse return null; if (!inJis0208(pointer)) return null; for (exceptions) |e| if (e.pointer == pointer) return null; return cellOf(pointer);}
/// The character at a JIS X 0212 cell. Both bytes must be 0x21 to 0x7E.pub fn decode0212(row: u8, column: u8) ?u21 { return index.codePoint(&jis0212.decode, pointerOf(row, column));}
/// The JIS X 0212 cell for a character, if it has one.pub fn encode0212(c: u21) ?Cell { return cellOf(jis0212_index.pointer(c) orelse return null);}
test "the six cells JIS X 0208 reads differently from code page 932" { try std.testing.expectEqual(@as(?u21, 0x301c), decode0208(0x21, 0x41)); try std.testing.expectEqual(@as(?u21, 0x00ac), decode0208(0x22, 0x4c)); try std.testing.expectEqual(@as(?Cell, .{ .row = 0x21, .column = 0x41 }), encode0208(0x301c)); try std.testing.expectEqual(@as(?Cell, null), encode0208(0xff5e)); try std.testing.expectEqual(@as(?Cell, null), encode0208(0xffe2));}
test "rows 13 and 89 to 92 are not JIS X 0208" { // 0x2D21 is CIRCLED DIGIT ONE in code page 932, 0x7921 an IBM kanji. try std.testing.expectEqual(@as(?u21, null), decode0208(0x2d, 0x21)); try std.testing.expectEqual(@as(?u21, null), decode0208(0x79, 0x21)); try std.testing.expectEqual(@as(?Cell, null), encode0208(0x2460)); // U+4E9C is 0x3021, the first kanji. try std.testing.expectEqual(@as(?u21, 0x4e9c), decode0208(0x30, 0x21));}
test "JIS X 0212 both ways" { // U+4E02 is 0x3021 in JIS X 0212, its first kanji; U+00A6 is 0x2243. try std.testing.expectEqual(@as(?u21, 0x4e02), decode0212(0x30, 0x21)); try std.testing.expectEqual(@as(?Cell, .{ .row = 0x22, .column = 0x43 }), encode0212(0x00a6));}