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
This commit is contained in:
Andrew Kelley
2025-06-27 20:05:22 -07:00
parent 0b3f0124dc
commit 0e37ff0d59
161 changed files with 4385 additions and 5847 deletions

View File

@@ -501,7 +501,7 @@ pub fn Serializer(Writer: type) type {
try self.int(val);
},
.float, .comptime_float => try self.float(val),
.bool, .null => try std.fmt.format(self.writer, "{}", .{val}),
.bool, .null => try std.fmt.deprecatedFormat(self.writer, "{}", .{val}),
.enum_literal => try self.ident(@tagName(val)),
.@"enum" => try self.ident(@tagName(val)),
.pointer => |pointer| {
@@ -615,7 +615,8 @@ pub fn Serializer(Writer: type) type {
/// Serialize an integer.
pub fn int(self: *Self, val: anytype) Writer.Error!void {
try std.fmt.formatInt(val, 10, .lower, .{}, self.writer);
//try self.writer.printIntOptions(val, 10, .lower, .{});
try std.fmt.deprecatedFormat(self.writer, "{d}", .{val});
}
/// Serialize a float.
@@ -630,12 +631,12 @@ pub fn Serializer(Writer: type) type {
} else if (std.math.isNegativeZero(val)) {
return self.writer.writeAll("-0.0");
} else {
try std.fmt.format(self.writer, "{d}", .{val});
try std.fmt.deprecatedFormat(self.writer, "{d}", .{val});
},
.comptime_float => if (val == 0) {
return self.writer.writeAll("0");
} else {
try std.fmt.format(self.writer, "{d}", .{val});
try std.fmt.deprecatedFormat(self.writer, "{d}", .{val});
},
else => comptime unreachable,
}
@@ -645,7 +646,7 @@ pub fn Serializer(Writer: type) type {
///
/// Escapes the identifier if necessary.
pub fn ident(self: *Self, name: []const u8) Writer.Error!void {
try self.writer.print(".{p_}", .{std.zig.fmtId(name)});
try self.writer.print(".{f}", .{std.zig.fmtIdPU(name)});
}
/// Serialize `val` as a Unicode codepoint.
@@ -658,7 +659,7 @@ pub fn Serializer(Writer: type) type {
var buf: [8]u8 = undefined;
const len = std.unicode.utf8Encode(val, &buf) catch return error.InvalidCodepoint;
const str = buf[0..len];
try std.fmt.format(self.writer, "'{'}'", .{std.zig.fmtEscapes(str)});
try std.fmt.deprecatedFormat(self.writer, "'{f}'", .{std.zig.fmtChar(str)});
}
/// Like `value`, but always serializes `val` as a tuple.
@@ -716,7 +717,7 @@ pub fn Serializer(Writer: type) type {
/// Like `value`, but always serializes `val` as a string.
pub fn string(self: *Self, val: []const u8) Writer.Error!void {
try std.fmt.format(self.writer, "\"{}\"", .{std.zig.fmtEscapes(val)});
try std.fmt.deprecatedFormat(self.writer, "\"{f}\"", .{std.zig.fmtString(val)});
}
/// Options for formatting multiline strings.