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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! glibc's charsets, checked against what glibc does.//!//! The name cases are glibc's own verdicts, recorded by//! `tools/glibc_names.py`. The spot checks below are worked by hand from what//! each charset is defined to be. The broad check, every input of every//! charset against glibc itself, is `tools/crosscheck_glibc.py`.
const std = @import("std");
const charset = @import("charset");
const Glibc = charset.Glibc;const testing = std.testing;
const cases = @embedFile("glibc-name-cases.txt");
fn memberNamed(name: []const u8) ?Glibc { for (std.enums.values(Glibc)) |member| { if (std.mem.eql(u8, member.name(), name)) return member; } return null;}
test "every name is read the way glibc reads it" { var checked: usize = 0; var lines = std.mem.splitScalar(u8, cases, '\n'); while (lines.next()) |line| { if (line.len == 0 or line[0] == '#') continue; const tab = std.mem.findScalar(u8, line, '\t').?; var name_buffer: [256]u8 = undefined; const name = try std.fmt.hexToBytes(&name_buffer, line[0..tab]); const verdict = line[tab + 1 ..];
const expected: Glibc.Lookup = if (std.mem.eql(u8, verdict, "unknown")) .unknown else if (std.mem.eql(u8, verdict, "locale") or std.mem.findScalar(u8, verdict, ' ') != null) // The locale's charset, or a suffix glibc acts on. .unsupported else if (memberNamed(verdict)) |member| .{ .supported = member } else .unsupported;
const actual = Glibc.lookup(name); if (!std.meta.eql(expected, actual)) { std.debug.print("{x}: glibc says {s}, lookup says {any}\n", .{ name, verdict, actual }); return error.TestUnexpectedResult; } checked += 1; } try testing.expect(checked > 5000);}
fn expectConvert(from: Glibc, to: Glibc, input: []const u8, expected: []const u8) !void { const actual = try charset.convertGlibcAlloc(testing.allocator, from, to, input); defer testing.allocator.free(actual); try testing.expectEqualSlices(u8, expected, actual);}
test "ISO-8859-1 is Latin-1, not windows-1252" { try expectConvert(.iso_8859_1, .utf_8, "\x80\xe9", "\u{80}\u{e9}"); try testing.expectError(error.UnmappableCodePoint, charset.convertGlibcAlloc(testing.allocator, .utf_8, .iso_8859_1, "\u{20ac}"));}
test "ASCII refuses everything above 0x7F" { try expectConvert(.ansi_x3_4_1968, .utf_8, "plain", "plain"); try testing.expectError(error.InvalidByteSequence, charset.convertGlibcAlloc(testing.allocator, .ansi_x3_4_1968, .utf_8, "\x80"));}
test "the Windows code pages leave Microsoft's empty positions empty" { try expectConvert(.cp1252, .utf_8, "\x80", "\u{20ac}"); try testing.expectError(error.InvalidByteSequence, charset.convertGlibcAlloc(testing.allocator, .cp1252, .utf_8, "\x81")); try testing.expectError(error.UnmappableCodePoint, charset.convertGlibcAlloc(testing.allocator, .utf_8, .cp1252, "\u{81}"));}
test "ISO-8859-9 is Latin-5" { try expectConvert(.iso_8859_9, .utf_8, "\xd0\xdd\xde\xf0\xfd\xfe\x80", "ĞİŞğış\u{80}");}
test "KOI8-U is RFC 2319's, with box drawing at 0xAE and 0xBE" { try expectConvert(.koi8_u, .utf_8, "\xae\xbe\xa4", "╝╬є"); try expectConvert(.koi8_ru, .utf_8, "\xae\xbe", "ўЎ");}
test "the tag characters are dropped everywhere but UTF-8" { try expectConvert(.utf_8, .iso_8859_1, "a\u{e0041}b", "ab"); try expectConvert(.utf_8, .euc_kr, "\u{e007f}", ""); try expectConvert(.utf_8, .utf_8, "a\u{e0041}b", "a\u{e0041}b");}
test "EUC-KR is KS X 1001 alone, with no eight-byte syllables" { try expectConvert(.utf_8, .euc_kr, "가", "\xb0\xa1"); try testing.expectError(error.UnmappableCodePoint, charset.convertGlibcAlloc(testing.allocator, .utf_8, .euc_kr, "똠")); // The filler and three jamo are four characters here, not one syllable. try expectConvert(.euc_kr, .utf_8, "\xa4\xd4\xa4\xa1\xa4\xbf\xa4\xa1", "\u{3164}ㄱㅏㄱ"); // Code page 949 writes the same syllable as a Unified Hangul Code pair. try expectConvert(.utf_8, .uhc, "똠", "\x8c\x63"); try expectConvert(.euc_kr, .utf_8, "\xa2\xe8\x85", "\u{327e}\u{85}");}
test "Johab writes the won sign at 0x5C" { try expectConvert(.johab, .utf_8, "\x5c", "\u{20a9}"); try testing.expectError(error.UnmappableCodePoint, charset.convertGlibcAlloc(testing.allocator, .utf_8, .johab, "\\")); try expectConvert(.utf_8, .johab, "한", "\xd0\x65");}
test "code page 932 writes back its end-user defined characters" { try expectConvert(.cp932, .utf_8, "\xf0\x40", "\u{e000}"); try expectConvert(.utf_8, .cp932, "\u{e000}\u{e757}", "\xf0\x40\xf9\xfc"); try testing.expectError(error.InvalidByteSequence, charset.convertGlibcAlloc(testing.allocator, .cp932, .utf_8, "\x80"));}
test "EBCDIC converts both ways" { try expectConvert(.ibm037, .iso_8859_1, "\xc8\x85\x93\x93\x96", "Hello"); try expectConvert(.ibm875, .utf_8, "\x74", "\u{2207}");}