authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-24 14:09:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-24 14:09:30-07:00
log745a029b9e3f1b0dc045067cc3ce5c3883bc430d
treefec6503da0c696850a943b5b176a3223f5cef939
parent5f78220dbe17133ece22e4f6a7e5e6337bfd7800

std: move fmt.allocPrint to Allocator.print


2 files changed, 40 insertions(+), 12 deletions(-)

lib/std/fmt.zig+4-12
...@@ -622,27 +622,19 @@ pub fn count(comptime fmt: []const u8, args: anytype) usize {...@@ -622,27 +622,19 @@ pub fn count(comptime fmt: []const u8, args: anytype) usize {
622 return @intCast(dw.count + dw.writer.end);622 return @intCast(dw.count + dw.writer.end);
623}623}
624624
625/// Deprecated in favor of `Allocator.print`.
625pub fn allocPrint(gpa: Allocator, comptime fmt: []const u8, args: anytype) Allocator.Error![]u8 {626pub fn allocPrint(gpa: Allocator, comptime fmt: []const u8, args: anytype) Allocator.Error![]u8 {
626 var aw = try Writer.Allocating.initCapacity(gpa, fmt.len);627 return gpa.print(fmt, args);
627 defer aw.deinit();
628 aw.writer.print(fmt, args) catch |err| switch (err) {
629 error.WriteFailed => return error.OutOfMemory,
630 };
631 return aw.toOwnedSlice();
632}628}
633629
630/// Deprecated in favor of `Allocator.printSentinel`.
634pub fn allocPrintSentinel(631pub fn allocPrintSentinel(
635 gpa: Allocator,632 gpa: Allocator,
636 comptime fmt: []const u8,633 comptime fmt: []const u8,
637 args: anytype,634 args: anytype,
638 comptime sentinel: u8,635 comptime sentinel: u8,
639) Allocator.Error![:sentinel]u8 {636) Allocator.Error![:sentinel]u8 {
640 var aw = try Writer.Allocating.initCapacity(gpa, fmt.len);637 return gpa.printSentinel(fmt, args, sentinel);
641 defer aw.deinit();
642 aw.writer.print(fmt, args) catch |err| switch (err) {
643 error.WriteFailed => return error.OutOfMemory,
644 };
645 return aw.toOwnedSliceSentinel(sentinel);
646}638}
647639
648pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {640pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
lib/std/mem/Allocator.zig+36
...@@ -471,6 +471,42 @@ pub fn dupeSentinel(...@@ -471,6 +471,42 @@ pub fn dupeSentinel(
471 return new_buf[0..m.len :sentinel];471 return new_buf[0..m.len :sentinel];
472}472}
473473
474/// Allocates a formatted string which is returned on success.
475///
476/// Returned slice can be deallocated with `free`. If an arena-style allocator
477/// is used instead, such as `std.heap.ArenaAllocator`, then no call to `free`
478/// is necessary.
479///
480/// See `std.Io.Writer.print`.
481pub fn print(a: Allocator, comptime format: []const u8, args: anytype) Error![]u8 {
482 var aw = try std.Io.Writer.Allocating.initCapacity(a, format.len);
483 defer aw.deinit();
484 aw.writer.print(format, args) catch |err| switch (err) {
485 error.WriteFailed => return error.OutOfMemory,
486 };
487 return aw.toOwnedSlice();
488}
489
490/// Like `print` but returned slice has the provided sentinel.
491///
492/// Returned slice can be deallocated with `free`. If an arena-style allocator
493/// is used instead, such as `std.heap.ArenaAllocator`, then no call to `free`
494/// is necessary. Illegal behavior occurs if the returned slice is type-coerced
495/// to a slice without the sentinel and then passed to `free`.
496pub fn printSentinel(
497 a: Allocator,
498 comptime format: []const u8,
499 args: anytype,
500 comptime sentinel: u8,
501) Allocator.Error![:sentinel]u8 {
502 var aw = try std.Io.Writer.Allocating.initCapacity(a, format.len);
503 defer aw.deinit();
504 aw.writer.print(format, args) catch |err| switch (err) {
505 error.WriteFailed => return error.OutOfMemory,
506 };
507 return aw.toOwnedSliceSentinel(sentinel);
508}
509
474/// An allocator that always fails to allocate.510/// An allocator that always fails to allocate.
475pub const failing: Allocator = .{511pub const failing: Allocator = .{
476 .ptr = undefined,512 .ptr = undefined,