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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Converting between UTF-8 and the legacy character encodings, as the WHATWG//! Encoding Standard defines them.//!//! Forty encodings: windows-1252 and the rest of the single-byte ones,//! Shift_JIS, EUC-JP, ISO-2022-JP, GBK, gb18030, Big5, EUC-KR, UTF-16 in both//! byte orders, and UTF-8 itself. That is the list browsers implement and,//! deliberately, no more -- the standard is a closed set, and the reason is//! that a label a server and a client disagree about is an attack rather than//! a feature.//!//! Four EBCDIC code pages come with it, as a type of their own rather than as//! members of `Encoding`, so that closed set stays closed: see `ebcdic.zig`.//! So do two Korean encodings the standard leaves out, Wansung and Johab, for//! the same reason: see `korean.zig`. And so do charsets as glibc's iconv//! defines them, matched byte for byte, for a program that has to convert the//! way glibc does: see `glibc.zig`.//!//! Decoding never fails by default: a byte sequence that means nothing//! becomes U+FFFD, which is what makes a document with one bad byte in it//! still a document. Ask for `.fatal` when the input is a checked format//! rather than prose.//!//! ```zig//! const charset = @import("charset");//!//! const utf8 = try charset.decodeAlloc(gpa, .shift_jis, bytes, .{});//! defer gpa.free(utf8);//!//! const encoding = charset.Encoding.fromLabel("Shift-JIS").?;//! ```//!//! For text arriving in pieces, `Decoder` and `Encoder` keep the state that a//! character split across two chunks needs.//!//! The mapping tables under `src/tables/` are generated from the index files//! the standard publishes, which are committed in `data/`; `zig build//! gen-tables` remakes them.
const convert = @import("convert.zig");
pub const Decoder = @import("Decoder.zig");pub const Encoder = @import("Encoder.zig");pub const Encoding = @import("encoding.zig").Encoding;
/// EBCDIC, which the Encoding Standard does not define and which is/// deliberately not an `Encoding`: the standard's set is closed, and/// `Encoding.fromLabel` refusing everything outside it is a property worth/// keeping. Reached by naming a code page in code, never by a label off a/// header.pub const ebcdic = @import("ebcdic.zig");pub const Ebcdic = ebcdic.CodePage;
/// Korean encodings the Encoding Standard does not define -- Wansung, which/// is KS X 1001 without code page 949's additions, and Johab -- kept out of/// `Encoding` for the same reason EBCDIC is.pub const korean = @import("korean.zig");pub const Korean = korean.Korean;
/// Charsets as glibc 2.44's iconv defines them, for a program that has to/// convert the way something built on glibc does. Not in the Encoding/// Standard, and reached by glibc's names through `Glibc.lookup`, never by/// `Encoding.fromLabel`. See `glibc.zig`.pub const glibc = @import("glibc.zig");pub const Glibc = glibc.Glibc;
/// Byte order marks, which are not part of any encoding here but decide/// which one to use.pub const bom = @import("bom.zig");
pub const DecodeError = convert.DecodeError;pub const EncodeError = convert.EncodeError;pub const EncodeEbcdicError = convert.EncodeEbcdicError;pub const EncodeKoreanError = convert.EncodeKoreanError;pub const EncodeGlibcError = convert.EncodeGlibcError;pub const ConvertGlibcError = convert.ConvertGlibcError;
pub const decodeAlloc = convert.decodeAlloc;pub const decodeBomAlloc = convert.decodeBomAlloc;pub const encodeAlloc = convert.encodeAlloc;pub const decodeEbcdicAlloc = convert.decodeEbcdicAlloc;pub const encodeEbcdicAlloc = convert.encodeEbcdicAlloc;pub const decodeKoreanAlloc = convert.decodeKoreanAlloc;pub const encodeKoreanAlloc = convert.encodeKoreanAlloc;pub const decodeGlibcAlloc = convert.decodeGlibcAlloc;pub const encodeGlibcAlloc = convert.encodeGlibcAlloc;pub const convertGlibcAlloc = convert.convertGlibcAlloc;
test { @import("std").testing.refAllDecls(@This()); _ = @import("byte_table.zig"); _ = @import("codec.zig"); _ = @import("codecs.zig"); _ = @import("convert.zig"); _ = @import("index.zig");}