mirror of
https://github.com/ziglang/zig.git
synced 2025-12-28 17:13:19 +00:00
std.fmt: migrate bufPrintZ to bufPrintSentinel (#25260)
This commit is contained in:
parent
47c932f896
commit
37ecaae639
@ -602,9 +602,19 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintErro
|
|||||||
return w.buffered();
|
return w.buffered();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deprecated in favor of `bufPrintSentinel`
|
||||||
pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 {
|
pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 {
|
||||||
const result = try bufPrint(buf, fmt ++ "\x00", args);
|
return try bufPrintSentinel(buf, fmt, args, 0);
|
||||||
return result[0 .. result.len - 1 :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.
|
/// 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);
|
var path_it = mem.tokenizeScalar(u8, PATH, path.delimiter);
|
||||||
while (path_it.next()) |a_path| {
|
while (path_it.next()) |a_path| {
|
||||||
var resolved_path_buf: [max_path_bytes - 1:0]u8 = undefined;
|
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,
|
a_path,
|
||||||
std.os.argv[0],
|
std.os.argv[0],
|
||||||
|
0,
|
||||||
}) catch continue;
|
}) catch continue;
|
||||||
|
|
||||||
var real_path_buf: [max_path_bytes]u8 = undefined;
|
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);
|
@setEvalBranchQuota(10_000);
|
||||||
var num_buf: [128]u8 = undefined;
|
var num_buf: [128]u8 = undefined;
|
||||||
tuple_fields[i] = .{
|
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,
|
.type = T,
|
||||||
.default_value_ptr = null,
|
.default_value_ptr = null,
|
||||||
.is_comptime = false,
|
.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 => {
|
.linux, .serenity => {
|
||||||
var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
|
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| {
|
const target = posix.readlinkZ(proc_path, out_buffer) catch |err| {
|
||||||
switch (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 => {
|
.solaris, .illumos => {
|
||||||
var procfs_buf: ["/proc/self/path/-2147483648\x00".len]u8 = undefined;
|
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) {
|
const target = posix.readlinkZ(proc_path, out_buffer) catch |err| switch (err) {
|
||||||
error.UnsupportedReparsePointType => unreachable,
|
error.UnsupportedReparsePointType => unreachable,
|
||||||
|
|||||||
@ -497,7 +497,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr
|
|||||||
return error.OperationNotSupported;
|
return error.OperationNotSupported;
|
||||||
|
|
||||||
var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
|
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) {
|
while (true) {
|
||||||
const res = system.chmod(proc_path, mode);
|
const res = system.chmod(proc_path, mode);
|
||||||
switch (errno(res)) {
|
switch (errno(res)) {
|
||||||
|
|||||||
@ -1320,10 +1320,11 @@ fn windowsMakeAsyncPipe(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *cons
|
|||||||
const pipe_path = blk: {
|
const pipe_path = blk: {
|
||||||
var tmp_buf: [128]u8 = undefined;
|
var tmp_buf: [128]u8 = undefined;
|
||||||
// Forge a random path for the pipe.
|
// Forge a random path for the pipe.
|
||||||
const pipe_path = std.fmt.bufPrintZ(
|
const pipe_path = std.fmt.bufPrintSentinel(
|
||||||
&tmp_buf,
|
&tmp_buf,
|
||||||
"\\\\.\\pipe\\zig-childprocess-{d}-{d}",
|
"\\\\.\\pipe\\zig-childprocess-{d}-{d}",
|
||||||
.{ windows.GetCurrentProcessId(), pipe_name_counter.fetchAdd(1, .monotonic) },
|
.{ windows.GetCurrentProcessId(), pipe_name_counter.fetchAdd(1, .monotonic) },
|
||||||
|
0,
|
||||||
) catch unreachable;
|
) catch unreachable;
|
||||||
const len = std.unicode.wtf8ToWtf16Le(&tmp_bufw, pipe_path) catch unreachable;
|
const len = std.unicode.wtf8ToWtf16Le(&tmp_bufw, pipe_path) catch unreachable;
|
||||||
tmp_bufw[len] = 0;
|
tmp_bufw[len] = 0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user