Skip to content

Commit 37ecaae

Browse files
authored
std.fmt: migrate bufPrintZ to bufPrintSentinel (#25260)
1 parent 47c932f commit 37ecaae

File tree

6 files changed

+20
-8
lines changed

6 files changed

+20
-8
lines changed

lib/std/fmt.zig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,19 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintErro
602602
return w.buffered();
603603
}
604604

605+
/// Deprecated in favor of `bufPrintSentinel`
605606
pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 {
606-
const result = try bufPrint(buf, fmt ++ "\x00", args);
607-
return result[0 .. result.len - 1 :0];
607+
return try bufPrintSentinel(buf, fmt, args, 0);
608+
}
609+
610+
pub 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];
608618
}
609619

610620
/// Count the characters needed for format.

lib/std/fs.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,10 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
616616
var path_it = mem.tokenizeScalar(u8, PATH, path.delimiter);
617617
while (path_it.next()) |a_path| {
618618
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}", .{
620620
a_path,
621621
std.os.argv[0],
622+
0,
622623
}) catch continue;
623624

624625
var real_path_buf: [max_path_bytes]u8 = undefined;

lib/std/meta.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type {
935935
@setEvalBranchQuota(10_000);
936936
var num_buf: [128]u8 = undefined;
937937
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,
939939
.type = T,
940940
.default_value_ptr = null,
941941
.is_comptime = false,

lib/std/os.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix.
132132
},
133133
.linux, .serenity => {
134134
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

137137
const target = posix.readlinkZ(proc_path, out_buffer) catch |err| {
138138
switch (err) {
@@ -149,7 +149,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix.
149149
},
150150
.solaris, .illumos => {
151151
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

154154
const target = posix.readlinkZ(proc_path, out_buffer) catch |err| switch (err) {
155155
error.UnsupportedReparsePointType => unreachable,

lib/std/posix.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr
497497
return error.OperationNotSupported;
498498

499499
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;
501501
while (true) {
502502
const res = system.chmod(proc_path, mode);
503503
switch (errno(res)) {

lib/std/process/Child.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,10 +1320,11 @@ fn windowsMakeAsyncPipe(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *cons
13201320
const pipe_path = blk: {
13211321
var tmp_buf: [128]u8 = undefined;
13221322
// Forge a random path for the pipe.
1323-
const pipe_path = std.fmt.bufPrintZ(
1323+
const pipe_path = std.fmt.bufPrintSentinel(
13241324
&tmp_buf,
13251325
"\\\\.\\pipe\\zig-childprocess-{d}-{d}",
13261326
.{ windows.GetCurrentProcessId(), pipe_name_counter.fetchAdd(1, .monotonic) },
1327+
0,
13271328
) catch unreachable;
13281329
const len = std.unicode.wtf8ToWtf16Le(&tmp_bufw, pipe_path) catch unreachable;
13291330
tmp_bufw[len] = 0;

0 commit comments

Comments
 (0)