authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-06 02:59:39 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-06 02:59:39 -07:00
log348fa46c43831dc963c123814bcad3c07874413a
tree64a3274fd2972a9eed51e94ed3e6fe31b34ff054
parent4a41782048c27d6e8e5215b947daffd6ee1ddd81
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

fmt: use nio containers


1 files changed, 9 insertions(+), 8 deletions(-)

fmt.zig+9-8
......@@ -3,6 +3,7 @@
33const std = @import("std");
44const builtin = @import("builtin");
55const extras = @import("extras");
6const nio = @import("./nio.zig");
67
78/// Renders fmt string with args, calling `writer` with slices of bytes.
89/// If `writer` returns an error, the error is returned from `format` and
......@@ -808,20 +809,20 @@ fn formatBuf(buf: []const u8, options: FormatOptions, writer: anytype) !void {
808809
809810/// Count the characters needed for format. Useful for preallocating memory
810811fn count(comptime fmt: []const u8, args: anytype) u64 {
811 var counting_writer = std.io.countingWriter(std.io.null_writer);
812 format(counting_writer.writer().any(), fmt, args) catch unreachable;
812 var counting_writer: nio.CountingWriter(nio.NullWriter) = .init(.{});
813 format(&counting_writer, fmt, args) catch unreachable;
813814 return counting_writer.bytes_written;
814815}
815816
816817/// Print a Formatter string into `buf`. Actually just a thin wrapper around `format` and `fixedBufferStream`.
817818/// Returns a slice of the bytes printed to.
818819fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) ![]u8 {
819 var fbs = std.io.fixedBufferStream(buf);
820 format(fbs.writer().any(), fmt, args) catch |err| switch (err) {
820 var fbs: nio.FixedBufferStream([]u8) = .init(buf);
821 format(&fbs, fmt, args) catch |err| switch (err) {
821822 error.NoSpaceLeft => return error.NoSpaceLeft,
822823 else => unreachable,
823824 };
824 return fbs.getWritten();
825 return fbs.written();
825826}
826827
827828const digits2_alphabet = blk: {
......@@ -920,11 +921,11 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt
920921 };
921922 return formatBuf(s, options, writer);
922923 } else if (comptime std.mem.eql(u8, fmt, "x")) {
923 var buf_stream = std.io.fixedBufferStream(&buf);
924 std.fmt.formatFloatHexadecimal(value, options, buf_stream.writer()) catch |err| switch (err) {
924 var buf_stream: nio.FixedBufferStream([]u8) = .init(&buf);
925 std.fmt.formatFloatHexadecimal(value, options, &buf_stream) catch |err| switch (err) {
925926 error.NoSpaceLeft => unreachable,
926927 };
927 return formatBuf(buf_stream.getWritten(), options, writer);
928 return formatBuf(buf_stream.written(), options, writer);
928929 } else {
929930 invalidFmtError(fmt, value);
930931 }