Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions lib/std/buffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,9 @@ pub const Buffer = struct {
}

pub fn allocPrint(allocator: *Allocator, comptime format: []const u8, args: var) !Buffer {
const countSize = struct {
fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
size.* += bytes.len;
}
}.countSize;
var size: usize = 0;
std.fmt.format(&size, error{}, countSize, format, args) catch |err| switch (err) {};
const size = std.fmt.count(format, args) catch |err| switch (err) {
error.Overflow => return error.OutOfMemory,
};
var self = try Buffer.initSize(allocator, size);
assert((std.fmt.bufPrint(self.list.items, format, args) catch unreachable).len == size);
return self;
Expand Down Expand Up @@ -155,7 +151,7 @@ pub const Buffer = struct {
}

pub fn print(self: *Buffer, comptime fmt: []const u8, args: var) !void {
return std.fmt.format(self, error{OutOfMemory}, Buffer.append, fmt, args);
return self.outStream().print(fmt, args);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the stream is readily available, maybe this method should be deleted.

}

pub fn outStream(self: *Buffer) std.io.OutStream(*Buffer, error{OutOfMemory}, appendWrite) {
Expand Down
12 changes: 5 additions & 7 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -436,19 +436,17 @@ pub const Version = struct {
self: Version,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
context: var,
comptime Error: type,
comptime output: fn (@TypeOf(context), []const u8) Error!void,
) Error!void {
out_stream: var,
) !void {
if (fmt.len == 0) {
if (self.patch == 0) {
if (self.minor == 0) {
return std.fmt.format(context, Error, output, "{}", .{self.major});
return std.fmt.format(out_stream, "{}", .{self.major});
} else {
return std.fmt.format(context, Error, output, "{}.{}", .{ self.major, self.minor });
return std.fmt.format(out_stream, "{}.{}", .{ self.major, self.minor });
}
} else {
return std.fmt.format(context, Error, output, "{}.{}.{}", .{ self.major, self.minor, self.patch });
return std.fmt.format(out_stream, "{}.{}.{}", .{ self.major, self.minor, self.patch });
}
} else {
@compileError("Unknown format string: '" ++ fmt ++ "'");
Expand Down
14 changes: 13 additions & 1 deletion lib/std/fifo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,19 @@ pub fn LinearFifo(
pub usingnamespace if (T == u8)
struct {
pub fn print(self: *Self, comptime format: []const u8, args: var) !void {
return std.fmt.format(self, error{OutOfMemory}, Self.write, format, args);
// TODO: maybe expose this stream as a method?
const FifoStream = struct {
const OutStream = std.io.OutStream(*Self, Error, write);
const Error = error{OutOfMemory};

fn write(fifo: *Self, bytes: []const u8) Error!usize {
try fifo.write(bytes);
return bytes.len;
}
};

var out_stream = FifoStream.OutStream{ .context = self };
try out_stream.print(format, args);
}
}
else
Expand Down
Loading