Andrew Kelley 0e37ff0d59 std.fmt: breaking API changes
added adapter to AnyWriter and GenericWriter to help bridge the gap
between old and new API

make std.testing.expectFmt work at compile-time

std.fmt no longer has a dependency on std.unicode. Formatted printing
was never properly unicode-aware. Now it no longer pretends to be.

Breakage/deprecations:
* std.fs.File.reader -> std.fs.File.deprecatedReader
* std.fs.File.writer -> std.fs.File.deprecatedWriter
* std.io.GenericReader -> std.io.Reader
* std.io.GenericWriter -> std.io.Writer
* std.io.AnyReader -> std.io.Reader
* std.io.AnyWriter -> std.io.Writer
* std.fmt.format -> std.fmt.deprecatedFormat
* std.fmt.fmtSliceEscapeLower -> std.ascii.hexEscape
* std.fmt.fmtSliceEscapeUpper -> std.ascii.hexEscape
* std.fmt.fmtSliceHexLower -> {x}
* std.fmt.fmtSliceHexUpper -> {X}
* std.fmt.fmtIntSizeDec -> {B}
* std.fmt.fmtIntSizeBin -> {Bi}
* std.fmt.fmtDuration -> {D}
* std.fmt.fmtDurationSigned -> {D}
* {} -> {f} when there is a format method
* format method signature
  - anytype -> *std.io.Writer
  - inferred error set -> error{WriteFailed}
  - options -> (deleted)
* std.fmt.Formatted
  - now takes context type explicitly
  - no fmt string
2025-07-07 22:43:51 -07:00

60 lines
2.6 KiB
Zig

const std = @import("std");
/// Returns 1 on success, 0 on failure
export fn verify(argc: c_int, argv: [*]const [*:0]const u16) c_int {
const argv_slice = argv[0..@intCast(argc)];
testArgv(argv_slice) catch |err| switch (err) {
error.OutOfMemory => @panic("oom"),
error.Overflow => @panic("bytes needed to contain args would overflow usize"),
error.ArgvMismatch => return 0,
};
return 1;
}
fn testArgv(expected_args: []const [*:0]const u16) !void {
var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_state.deinit();
const allocator = arena_state.allocator();
const args = try std.process.argsAlloc(allocator);
var wtf8_buf = std.ArrayList(u8).init(allocator);
var eql = true;
if (args.len != expected_args.len) eql = false;
const min_len = @min(expected_args.len, args.len);
for (expected_args[0..min_len], args[0..min_len], 0..) |expected_arg, arg_wtf8, i| {
wtf8_buf.clearRetainingCapacity();
try std.unicode.wtf16LeToWtf8ArrayList(&wtf8_buf, std.mem.span(expected_arg));
if (!std.mem.eql(u8, wtf8_buf.items, arg_wtf8)) {
std.debug.print("{}: expected: \"{f}\"\n", .{ i, std.zig.fmtString(wtf8_buf.items) });
std.debug.print("{}: actual: \"{f}\"\n", .{ i, std.zig.fmtString(arg_wtf8) });
eql = false;
}
}
if (!eql) {
for (expected_args[min_len..], min_len..) |arg, i| {
wtf8_buf.clearRetainingCapacity();
try std.unicode.wtf16LeToWtf8ArrayList(&wtf8_buf, std.mem.span(arg));
std.debug.print("{}: expected: \"{f}\"\n", .{ i, std.zig.fmtString(wtf8_buf.items) });
}
for (args[min_len..], min_len..) |arg, i| {
std.debug.print("{}: actual: \"{f}\"\n", .{ i, std.zig.fmtString(arg) });
}
const peb = std.os.windows.peb();
const lpCmdLine: [*:0]u16 = @ptrCast(peb.ProcessParameters.CommandLine.Buffer);
wtf8_buf.clearRetainingCapacity();
try std.unicode.wtf16LeToWtf8ArrayList(&wtf8_buf, std.mem.span(lpCmdLine));
std.debug.print("command line: \"{f}\"\n", .{std.zig.fmtString(wtf8_buf.items)});
std.debug.print("expected argv:\n", .{});
std.debug.print("&.{{\n", .{});
for (expected_args) |arg| {
wtf8_buf.clearRetainingCapacity();
try std.unicode.wtf16LeToWtf8ArrayList(&wtf8_buf, std.mem.span(arg));
std.debug.print(" \"{f}\",\n", .{std.zig.fmtString(wtf8_buf.items)});
}
std.debug.print("}}\n", .{});
return error.ArgvMismatch;
}
}