authorgravatar for mrjbq7@gmail.comJohn Benediktsson <mrjbq7@gmail.com> 2025-09-18 22:02:22-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-09-19 05:02:22+00:00
log37ecaae6390d238dce21ef4b97e6994dd229c2c3
tree71fd2226964dfa43a17fde62c4d896bfdd5a2c82
parent47c932f8960e361282269202dc8ad7e63e3a06eb
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.fmt: migrate bufPrintZ to bufPrintSentinel (#25260)


6 files changed, 20 insertions(+), 8 deletions(-)

lib/std/fmt.zig+12-2
...@@ -602,9 +602,19 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintErro...@@ -602,9 +602,19 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintErro
602 return w.buffered();602 return w.buffered();
603}603}
604604
605/// Deprecated in favor of `bufPrintSentinel`
605pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 {606pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 {
606 const result = try bufPrint(buf, fmt ++ "\x00", args);607 return try bufPrintSentinel(buf, fmt, args, 0);
607 return result[0 .. result.len - 1 :0];608}
609
610pub fn bufPrintSentinel(
611 buf: []u8,
612 comptime fmt: []const u8,
613 args: anytype,
614 comptime sentinel: u8,
615) BufPrintError![:sentinel]u8 {
616 const result = try bufPrint(buf, fmt ++ [_]u8{sentinel}, args);
617 return result[0 .. result.len - 1 :sentinel];
608}618}
609619
610/// Count the characters needed for format.620/// Count the characters needed for format.
lib/std/fs.zig+2-1
...@@ -616,9 +616,10 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {...@@ -616,9 +616,10 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
616 var path_it = mem.tokenizeScalar(u8, PATH, path.delimiter);616 var path_it = mem.tokenizeScalar(u8, PATH, path.delimiter);
617 while (path_it.next()) |a_path| {617 while (path_it.next()) |a_path| {
618 var resolved_path_buf: [max_path_bytes - 1:0]u8 = undefined;618 var resolved_path_buf: [max_path_bytes - 1:0]u8 = undefined;
619 const resolved_path = std.fmt.bufPrintZ(&resolved_path_buf, "{s}/{s}", .{619 const resolved_path = std.fmt.bufPrintSentinel(&resolved_path_buf, "{s}/{s}", .{
620 a_path,620 a_path,
621 std.os.argv[0],621 std.os.argv[0],
622 0,
622 }) catch continue;623 }) catch continue;
623624
624 var real_path_buf: [max_path_bytes]u8 = undefined;625 var real_path_buf: [max_path_bytes]u8 = undefined;
lib/std/meta.zig+1-1
...@@ -935,7 +935,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type {...@@ -935,7 +935,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type {
935 @setEvalBranchQuota(10_000);935 @setEvalBranchQuota(10_000);
936 var num_buf: [128]u8 = undefined;936 var num_buf: [128]u8 = undefined;
937 tuple_fields[i] = .{937 tuple_fields[i] = .{
938 .name = std.fmt.bufPrintZ(&num_buf, "{d}", .{i}) catch unreachable,938 .name = std.fmt.bufPrintSentinel(&num_buf, "{d}", .{i}, 0) catch unreachable,
939 .type = T,939 .type = T,
940 .default_value_ptr = null,940 .default_value_ptr = null,
941 .is_comptime = false,941 .is_comptime = false,
lib/std/os.zig+2-2
...@@ -132,7 +132,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix....@@ -132,7 +132,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix.
132 },132 },
133 .linux, .serenity => {133 .linux, .serenity => {
134 var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;134 var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
135 const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/fd/{d}", .{fd}) catch unreachable;135 const proc_path = std.fmt.bufPrintSentinel(procfs_buf[0..], "/proc/self/fd/{d}", .{fd}, 0) catch unreachable;
136136
137 const target = posix.readlinkZ(proc_path, out_buffer) catch |err| {137 const target = posix.readlinkZ(proc_path, out_buffer) catch |err| {
138 switch (err) {138 switch (err) {
...@@ -149,7 +149,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix....@@ -149,7 +149,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix.
149 },149 },
150 .solaris, .illumos => {150 .solaris, .illumos => {
151 var procfs_buf: ["/proc/self/path/-2147483648\x00".len]u8 = undefined;151 var procfs_buf: ["/proc/self/path/-2147483648\x00".len]u8 = undefined;
152 const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/path/{d}", .{fd}) catch unreachable;152 const proc_path = std.fmt.bufPrintSentinel(procfs_buf[0..], "/proc/self/path/{d}", .{fd}, 0) catch unreachable;
153153
154 const target = posix.readlinkZ(proc_path, out_buffer) catch |err| switch (err) {154 const target = posix.readlinkZ(proc_path, out_buffer) catch |err| switch (err) {
155 error.UnsupportedReparsePointType => unreachable,155 error.UnsupportedReparsePointType => unreachable,
lib/std/posix.zig+1-1
...@@ -497,7 +497,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr...@@ -497,7 +497,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr
497 return error.OperationNotSupported;497 return error.OperationNotSupported;
498498
499 var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;499 var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
500 const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/fd/{d}", .{pathfd}) catch unreachable;500 const proc_path = std.fmt.bufPrintSentinel(procfs_buf[0..], "/proc/self/fd/{d}", .{pathfd}, 0) catch unreachable;
501 while (true) {501 while (true) {
502 const res = system.chmod(proc_path, mode);502 const res = system.chmod(proc_path, mode);
503 switch (errno(res)) {503 switch (errno(res)) {
lib/std/process/Child.zig+2-1
...@@ -1320,10 +1320,11 @@ fn windowsMakeAsyncPipe(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *cons...@@ -1320,10 +1320,11 @@ fn windowsMakeAsyncPipe(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *cons
1320 const pipe_path = blk: {1320 const pipe_path = blk: {
1321 var tmp_buf: [128]u8 = undefined;1321 var tmp_buf: [128]u8 = undefined;
1322 // Forge a random path for the pipe.1322 // Forge a random path for the pipe.
1323 const pipe_path = std.fmt.bufPrintZ(1323 const pipe_path = std.fmt.bufPrintSentinel(
1324 &tmp_buf,1324 &tmp_buf,
1325 "\\\\.\\pipe\\zig-childprocess-{d}-{d}",1325 "\\\\.\\pipe\\zig-childprocess-{d}-{d}",
1326 .{ windows.GetCurrentProcessId(), pipe_name_counter.fetchAdd(1, .monotonic) },1326 .{ windows.GetCurrentProcessId(), pipe_name_counter.fetchAdd(1, .monotonic) },
1327 0,
1327 ) catch unreachable;1328 ) catch unreachable;
1328 const len = std.unicode.wtf8ToWtf16Le(&tmp_bufw, pipe_path) catch unreachable;1329 const len = std.unicode.wtf8ToWtf16Le(&tmp_bufw, pipe_path) catch unreachable;
1329 tmp_bufw[len] = 0;1330 tmp_bufw[len] = 0;