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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! One-shot conversion, for text that is already in memory in one piece.//!//! Each of these is `Decoder` or `Encoder` with the loop written out and the//! result collected into an allocation. When the text arrives in chunks --//! off a socket, out of a file -- drive those directly instead, or a//! multi-byte character landing on a chunk boundary turns into a replacement//! character.
const std = @import("std");
const bom = @import("bom.zig");
const Allocator = std.mem.Allocator;const CodePage = @import("ebcdic.zig").CodePage;const Decoder = @import("Decoder.zig");const Encoder = @import("Encoder.zig");const Encoding = @import("encoding.zig").Encoding;const Writer = std.Io.Writer;
pub const DecodeError = Allocator.Error || error{ /// Only with `error_mode` set to `fatal`. InvalidByteSequence,};
pub const EncodeError = Allocator.Error || Encoder.NoEncoderError || EncodeEbcdicError;
/// What encoding to EBCDIC can fail with. `error.NoEncoder` is not in it:/// every EBCDIC code page has an encoder, and only three of the standard's/// encodings do not.pub const EncodeEbcdicError = Allocator.Error || error{ /// Only with `error_mode` set to `fatal`. UnmappableCodePoint,};
/// Decode `bytes` as `encoding`, returning UTF-8. Caller owns the result.////// A byte order mark is not looked for and not removed: this decodes what it/// is told to. `decodeBomAlloc` is the one that sniffs.pub fn decodeAlloc( gpa: Allocator, encoding: Encoding, bytes: []const u8, options: Decoder.Options,) DecodeError![]u8 { var allocating: Writer.Allocating = .init(gpa); errdefer allocating.deinit();
var decoder: Decoder = .init(encoding, options); try lift(decoder.decode(bytes, &allocating.writer)); try lift(decoder.finish(&allocating.writer));
return allocating.toOwnedSlice();}
/// The standard's "decode" hook: a leading byte order mark chooses the/// encoding if there is one, and `fallback` is used if there is not. The mark/// itself is not part of the result.pub fn decodeBomAlloc( gpa: Allocator, fallback: Encoding, bytes: []const u8, options: Decoder.Options,) DecodeError![]u8 { const encoding, const rest = bom.strip(bytes, fallback); return decodeAlloc(gpa, encoding, rest, options);}
/// Encode the UTF-8 in `utf8` as `encoding`. Caller owns the result.////// `error.NoEncoder` comes back for `replacement`, `UTF-16BE` and/// `UTF-16LE`, none of which the standard gives an encoder;/// `Encoding.outputEncoding` is how to avoid asking.pub fn encodeAlloc( gpa: Allocator, encoding: Encoding, utf8: []const u8, options: Encoder.Options,) EncodeError![]u8 { var allocating: Writer.Allocating = .init(gpa); errdefer allocating.deinit();
var encoder: Encoder = try .init(encoding, options); try lift(encoder.encode(utf8, &allocating.writer)); try lift(encoder.finish(&allocating.writer));
return allocating.toOwnedSlice();}
/// Decode `bytes` as an EBCDIC code page, returning UTF-8. Caller owns the/// result.////// EBCDIC is not in the Encoding Standard, so it has a function of its own/// rather than a member of `Encoding`; see `ebcdic.zig` for why that matters.pub fn decodeEbcdicAlloc( gpa: Allocator, code_page: CodePage, bytes: []const u8, options: Decoder.Options,) DecodeError![]u8 { var allocating: Writer.Allocating = .init(gpa); errdefer allocating.deinit();
var decoder: Decoder = .initEbcdic(code_page, options); try lift(decoder.decode(bytes, &allocating.writer)); try lift(decoder.finish(&allocating.writer));
return allocating.toOwnedSlice();}
/// Encode the UTF-8 in `utf8` as an EBCDIC code page. Caller owns the result.////// Every code page has an encoder, so unlike `encodeAlloc` this cannot return/// `error.NoEncoder`.pub fn encodeEbcdicAlloc( gpa: Allocator, code_page: CodePage, utf8: []const u8, options: Encoder.Options,) EncodeEbcdicError![]u8 { var allocating: Writer.Allocating = .init(gpa); errdefer allocating.deinit();
var encoder: Encoder = .initEbcdic(code_page, options); try lift(encoder.encode(utf8, &allocating.writer)); try lift(encoder.finish(&allocating.writer));
return allocating.toOwnedSlice();}
/// An `Allocating` writer fails for exactly one reason, so `WriteFailed` from/// one means the allocator said no.fn lift(result: anytype) !void { return result catch |err| switch (err) { error.WriteFailed => error.OutOfMemory, else => |other| other, };}