mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
std.fmt: migrate bufPrintZ to bufPrintSentinel (#25260)
This commit is contained in:
parent
47c932f896
commit
37ecaae639
6 changed files with 20 additions and 8 deletions
|
|
@ -602,9 +602,19 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintErro
|
|||
return w.buffered();
|
||||
}
|
||||
|
||||
/// Deprecated in favor of `bufPrintSentinel`
|
||||
pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 {
|
||||
const result = try bufPrint(buf, fmt ++ "\x00", args);
|
||||
return result[0 .. result.len - 1 :0];
|
||||
return try bufPrintSentinel(buf, fmt, args, 0);
|
||||
}
|
||||
|
||||
pub fn bufPrintSentinel(
|
||||
buf: []u8,
|
||||
comptime fmt: []const u8,
|
||||
args: anytype,
|
||||
comptime sentinel: u8,
|
||||
) BufPrintError![:sentinel]u8 {
|
||||
const result = try bufPrint(buf, fmt ++ [_]u8{sentinel}, args);
|
||||
return result[0 .. result.len - 1 :sentinel];
|
||||
}
|
||||
|
||||
/// Count the characters needed for format.
|
||||
|
|
|
|||
|
|
@ -616,9 +616,10 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
|
|||
var path_it = mem.tokenizeScalar(u8, PATH, path.delimiter);
|
||||
while (path_it.next()) |a_path| {
|
||||
var resolved_path_buf: [max_path_bytes - 1:0]u8 = undefined;
|
||||
const resolved_path = std.fmt.bufPrintZ(&resolved_path_buf, "{s}/{s}", .{
|
||||
const resolved_path = std.fmt.bufPrintSentinel(&resolved_path_buf, "{s}/{s}", .{
|
||||
a_path,
|
||||
std.os.argv[0],
|
||||
0,
|
||||
}) catch continue;
|
||||
|
||||
var real_path_buf: [max_path_bytes]u8 = undefined;
|
||||
|
|
|
|||
|
|
@ -935,7 +935,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type {
|
|||
@setEvalBranchQuota(10_000);
|
||||
var num_buf: [128]u8 = undefined;
|
||||
tuple_fields[i] = .{
|
||||
.name = std.fmt.bufPrintZ(&num_buf, "{d}", .{i}) catch unreachable,
|
||||
.name = std.fmt.bufPrintSentinel(&num_buf, "{d}", .{i}, 0) catch unreachable,
|
||||
.type = T,
|
||||
.default_value_ptr = null,
|
||||
.is_comptime = false,
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix.
|
|||
},
|
||||
.linux, .serenity => {
|
||||
var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
|
||||
const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/fd/{d}", .{fd}) catch unreachable;
|
||||
const proc_path = std.fmt.bufPrintSentinel(procfs_buf[0..], "/proc/self/fd/{d}", .{fd}, 0) catch unreachable;
|
||||
|
||||
const target = posix.readlinkZ(proc_path, out_buffer) catch |err| {
|
||||
switch (err) {
|
||||
|
|
@ -149,7 +149,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix.
|
|||
},
|
||||
.solaris, .illumos => {
|
||||
var procfs_buf: ["/proc/self/path/-2147483648\x00".len]u8 = undefined;
|
||||
const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/path/{d}", .{fd}) catch unreachable;
|
||||
const proc_path = std.fmt.bufPrintSentinel(procfs_buf[0..], "/proc/self/path/{d}", .{fd}, 0) catch unreachable;
|
||||
|
||||
const target = posix.readlinkZ(proc_path, out_buffer) catch |err| switch (err) {
|
||||
error.UnsupportedReparsePointType => unreachable,
|
||||
|
|
|
|||
|
|
@ -497,7 +497,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr
|
|||
return error.OperationNotSupported;
|
||||
|
||||
var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
|
||||
const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/fd/{d}", .{pathfd}) catch unreachable;
|
||||
const proc_path = std.fmt.bufPrintSentinel(procfs_buf[0..], "/proc/self/fd/{d}", .{pathfd}, 0) catch unreachable;
|
||||
while (true) {
|
||||
const res = system.chmod(proc_path, mode);
|
||||
switch (errno(res)) {
|
||||
|
|
|
|||
|
|
@ -1320,10 +1320,11 @@ fn windowsMakeAsyncPipe(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *cons
|
|||
const pipe_path = blk: {
|
||||
var tmp_buf: [128]u8 = undefined;
|
||||
// Forge a random path for the pipe.
|
||||
const pipe_path = std.fmt.bufPrintZ(
|
||||
const pipe_path = std.fmt.bufPrintSentinel(
|
||||
&tmp_buf,
|
||||
"\\\\.\\pipe\\zig-childprocess-{d}-{d}",
|
||||
.{ windows.GetCurrentProcessId(), pipe_name_counter.fetchAdd(1, .monotonic) },
|
||||
0,
|
||||
) catch unreachable;
|
||||
const len = std.unicode.wtf8ToWtf16Le(&tmp_bufw, pipe_path) catch unreachable;
|
||||
tmp_bufw[len] = 0;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue