mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
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
42 lines
1.2 KiB
Zig
42 lines
1.2 KiB
Zig
const std = @import("../std.zig");
|
|
const assert = std.debug.assert;
|
|
|
|
const stringify = @import("stringify.zig").stringify;
|
|
const StringifyOptions = @import("stringify.zig").StringifyOptions;
|
|
|
|
/// Returns a formatter that formats the given value using stringify.
|
|
pub fn fmt(value: anytype, options: StringifyOptions) Formatter(@TypeOf(value)) {
|
|
return Formatter(@TypeOf(value)){ .value = value, .options = options };
|
|
}
|
|
|
|
/// Formats the given value using stringify.
|
|
pub fn Formatter(comptime T: type) type {
|
|
return struct {
|
|
value: T,
|
|
options: StringifyOptions,
|
|
|
|
pub fn format(self: @This(), writer: *std.io.Writer, comptime f: []const u8) std.io.Writer.Error!void {
|
|
comptime assert(f.len == 0);
|
|
try stringify(self.value, self.options, writer);
|
|
}
|
|
};
|
|
}
|
|
|
|
test fmt {
|
|
const expectFmt = std.testing.expectFmt;
|
|
try expectFmt("123", "{}", .{fmt(@as(u32, 123), .{})});
|
|
try expectFmt(
|
|
\\{"num":927,"msg":"hello","sub":{"mybool":true}}
|
|
, "{}", .{fmt(struct {
|
|
num: u32,
|
|
msg: []const u8,
|
|
sub: struct {
|
|
mybool: bool,
|
|
},
|
|
}{
|
|
.num = 927,
|
|
.msg = "hello",
|
|
.sub = .{ .mybool = true },
|
|
}, .{})});
|
|
}
|