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 @@...@@ -3,6 +3,7 @@
3const std = @import("std");3const std = @import("std");
4const builtin = @import("builtin");4const builtin = @import("builtin");
5const extras = @import("extras");5const extras = @import("extras");
6const nio = @import("./nio.zig");
67
7/// Renders fmt string with args, calling `writer` with slices of bytes.8/// Renders fmt string with args, calling `writer` with slices of bytes.
8/// If `writer` returns an error, the error is returned from `format` and9/// 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 {...@@ -808,20 +809,20 @@ fn formatBuf(buf: []const u8, options: FormatOptions, writer: anytype) !void {
808809
809/// Count the characters needed for format. Useful for preallocating memory810/// Count the characters needed for format. Useful for preallocating memory
810fn count(comptime fmt: []const u8, args: anytype) u64 {811fn count(comptime fmt: []const u8, args: anytype) u64 {
811 var counting_writer = std.io.countingWriter(std.io.null_writer);812 var counting_writer: nio.CountingWriter(nio.NullWriter) = .init(.{});
812 format(counting_writer.writer().any(), fmt, args) catch unreachable;813 format(&counting_writer, fmt, args) catch unreachable;
813 return counting_writer.bytes_written;814 return counting_writer.bytes_written;
814}815}
815816
816/// Print a Formatter string into `buf`. Actually just a thin wrapper around `format` and `fixedBufferStream`.817/// Print a Formatter string into `buf`. Actually just a thin wrapper around `format` and `fixedBufferStream`.
817/// Returns a slice of the bytes printed to.818/// Returns a slice of the bytes printed to.
818fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) ![]u8 {819fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) ![]u8 {
819 var fbs = std.io.fixedBufferStream(buf);820 var fbs: nio.FixedBufferStream([]u8) = .init(buf);
820 format(fbs.writer().any(), fmt, args) catch |err| switch (err) {821 format(&fbs, fmt, args) catch |err| switch (err) {
821 error.NoSpaceLeft => return error.NoSpaceLeft,822 error.NoSpaceLeft => return error.NoSpaceLeft,
822 else => unreachable,823 else => unreachable,
823 };824 };
824 return fbs.getWritten();825 return fbs.written();
825}826}
826827
827const digits2_alphabet = blk: {828const digits2_alphabet = blk: {
...@@ -920,11 +921,11 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt...@@ -920,11 +921,11 @@ fn formatFloatValue(value: anytype, comptime fmt: []const u8, options: FormatOpt
920 };921 };
921 return formatBuf(s, options, writer);922 return formatBuf(s, options, writer);
922 } else if (comptime std.mem.eql(u8, fmt, "x")) {923 } else if (comptime std.mem.eql(u8, fmt, "x")) {
923 var buf_stream = std.io.fixedBufferStream(&buf);924 var buf_stream: nio.FixedBufferStream([]u8) = .init(&buf);
924 std.fmt.formatFloatHexadecimal(value, options, buf_stream.writer()) catch |err| switch (err) {925 std.fmt.formatFloatHexadecimal(value, options, &buf_stream) catch |err| switch (err) {
925 error.NoSpaceLeft => unreachable,926 error.NoSpaceLeft => unreachable,
926 };927 };
927 return formatBuf(buf_stream.getWritten(), options, writer);928 return formatBuf(buf_stream.written(), options, writer);
928 } else {929 } else {
929 invalidFmtError(fmt, value);930 invalidFmtError(fmt, value);
930 }931 }