| ... | ... | @@ -471,6 +471,42 @@ pub fn dupeSentinel( |
| 471 | 471 | return new_buf[0..m.len :sentinel]; |
| 472 | 472 | } |
| 473 | 473 | |
| 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`. |
| 481 | pub 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`. |
| 496 | pub 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 | 510 | /// An allocator that always fails to allocate. |
| 475 | 511 | pub const failing: Allocator = .{ |
| 476 | 512 | .ptr = undefined, |