mirror of
https://github.com/ziglang/zig.git
synced 2026-02-17 06:49:23 +00:00
Convert Buffer to use fmtstream
This commit is contained in:
parent
2429fdd73b
commit
8dd078b99a
@ -65,15 +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.fmtstream.count(format, args);
|
||||
var self = try Buffer.initSize(allocator, size);
|
||||
assert((std.fmt.bufPrint(self.list.items, format, args) catch unreachable).len == size);
|
||||
assert((std.fmtstream.bufPrint(self.list.items, format, args) catch unreachable).len == size);
|
||||
return self;
|
||||
}
|
||||
|
||||
@ -155,7 +149,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);
|
||||
}
|
||||
|
||||
pub fn outStream(self: *Buffer) std.io.OutStream(*Buffer, error{OutOfMemory}, appendWrite) {
|
||||
|
||||
@ -1080,14 +1080,17 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]
|
||||
return buf[0..fbs.pos];
|
||||
}
|
||||
|
||||
// Count the characters needed for format. Useful for preallocating memory
|
||||
pub fn count(comptime fmt: []const u8, args: var) usize {
|
||||
var counting_stream = std.io.countingOutStream(std.io.null_out_stream);
|
||||
format(counting_stream.outStream(), fmt, args) catch |err| switch (err) {};
|
||||
return counting_stream.bytes_written;
|
||||
}
|
||||
|
||||
pub const AllocPrintError = error{OutOfMemory};
|
||||
|
||||
pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: var) AllocPrintError![]u8 {
|
||||
// Count the characters we need to preallocate
|
||||
var counting_stream = std.io.countingOutStream(std.io.null_out_stream);
|
||||
format(counting_stream.outStream(), fmt, args) catch |err| switch (err) {};
|
||||
|
||||
const buf = try allocator.alloc(u8, counting_stream.bytes_written);
|
||||
const buf = try allocator.alloc(u8, count(fmt, args));
|
||||
return bufPrint(buf, fmt, args) catch |err| switch (err) {
|
||||
error.BufferTooSmall => unreachable, // we just counted the size above
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user