root_dir: Cache.Directorysub_path: []const u8 = ""The path, relative to the root dir, that this Path represents. Empty string means the root_dir is the path.
pub fn assert(ok: bool) voidInvokes detectable illegal behavior when ok is false.
In debug and safe modes, calls to this function are always generated, and the unreachable statement triggers a panic.
In fast and small modes, calls to this function are optimized away, and in fact the optimizer is able to use the assertion in its heuristics.
Inside a test block, it is best to use the testing module rather than this function, because this function may not detect a test failure in fast and small mode. Outside of a test block, this assert function is the correct function to use.
pub fn clone(p: Path, arena: Allocator) Allocator.Error!Pathpub fn cwd() Pathpub fn initCwd(sub_path: []const u8) Pathpub fn join(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Pathpub fn resolvePosix(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Pathpub fn joinString(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![]u8pub fn joinStringZ(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![: 0]u8pub fn openFile(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.OpenFileOptions) !Io.Filepub fn openDir(p: Path, io: Io, sub_path: []const u8, args: Io.Dir.OpenOptions) Io.Dir.OpenError!Io.Dirpub fn createDirPathOpen(p: Path, io: Io, sub_path: []const u8, opts: Io.Dir.CreateDirPathOpenOptions) !Io.Dirpub fn statFile(p: Path, io: Io, sub_path: []const u8) !Io.Dir.Statpub fn atomicFile(p: Path, io: Io, sub_path: []const u8, options: Io.Dir.CreateFileAtomicOptions, buf: *[Io.Dir.max_path_bytes]u8) !Io.File.Atomicpub fn access(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.AccessOptions) !voidpub fn createDirPath(p: Path, io: Io, sub_path: []const u8) !voidpub fn toString(p: Path, allocator: Allocator) Allocator.Error![]u8pub fn toStringZ(p: Path, allocator: Allocator) Allocator.Error![: 0]u8pub fn fmtEscapeString(path: Path) std.fmt.Alt(Path, formatEscapeString)pub fn formatEscapeString(path: Path, writer: *Io.Writer) Io.Writer.Error!voidpub fn fmtEscapeChar(path: Path) std.fmt.Alt(Path, formatEscapeChar)Deprecated, use double quoted escape to print paths.
pub fn formatEscapeChar(path: Path, writer: *Io.Writer) Io.Writer.Error!voidDeprecated, use double quoted escape to print paths.
pub fn format(self: Path, writer: *Io.Writer) Io.Writer.Error!voidpub fn eql(self: Path, other: Path) boolpub fn subPathOpt(self: Path) ?[]const u8pub fn subPathOrDot(self: Path) []const u8pub fn stem(p: Path) []const u8pub fn dirname(p: Path) ?Pathpub fn basename(p: Path) []const u81
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238const Path = @This();
const std = @import("../../std.zig");
const Io = std.Io;
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const Cache = std.Build.Cache;
root_dir: Cache.Directory,
/// The path, relative to the root dir, that this `Path` represents.
/// Empty string means the root_dir is the path.
sub_path: []const u8 = "",
pub fn clone(p: Path, arena: Allocator) Allocator.Error!Path {
return .{
.root_dir = try p.root_dir.clone(arena),
.sub_path = try arena.dupe(u8, p.sub_path),
};
}
pub fn cwd() Path {
return initCwd("");
}
pub fn initCwd(sub_path: []const u8) Path {
return .{ .root_dir = .cwd(), .sub_path = sub_path };
}
pub fn join(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path {
if (sub_path.len == 0) return p;
const parts: []const []const u8 =
if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
return .{
.root_dir = p.root_dir,
.sub_path = try Io.Dir.path.join(arena, parts),
};
}
pub fn resolvePosix(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path {
if (sub_path.len == 0) return p;
const new_sub_path = try Io.Dir.path.resolvePosix(arena, &.{ p.sub_path, sub_path });
return .{
.root_dir = p.root_dir,
// Use "" instead of "." to represent `root_dir` itself.
.sub_path = if (std.mem.eql(u8, new_sub_path, ".")) "" else new_sub_path,
};
}
pub fn joinString(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![]u8 {
const parts: []const []const u8 =
if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
return p.root_dir.join(gpa, parts);
}
pub fn joinStringZ(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![:0]u8 {
const parts: []const []const u8 =
if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
return p.root_dir.joinZ(gpa, parts);
}
pub fn openFile(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.OpenFileOptions) !Io.File {
var buf: [Io.Dir.max_path_bytes]u8 = undefined;
const joined_path = if (p.sub_path.len == 0) sub_path else p: {
break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
p.sub_path, sub_path,
}) catch return error.NameTooLong;
};
return p.root_dir.handle.openFile(io, joined_path, flags);
}
pub fn openDir(
p: Path,
io: Io,
sub_path: []const u8,
args: Io.Dir.OpenOptions,
) Io.Dir.OpenError!Io.Dir {
var buf: [Io.Dir.max_path_bytes]u8 = undefined;
const joined_path = if (p.sub_path.len == 0) sub_path else p: {
break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
p.sub_path, sub_path,
}) catch return error.NameTooLong;
};
return p.root_dir.handle.openDir(io, joined_path, args);
}
pub fn createDirPathOpen(p: Path, io: Io, sub_path: []const u8, opts: Io.Dir.CreateDirPathOpenOptions) !Io.Dir {
var buf: [Io.Dir.max_path_bytes]u8 = undefined;
const joined_path = if (p.sub_path.len == 0) sub_path else p: {
break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
p.sub_path, sub_path,
}) catch return error.NameTooLong;
};
return p.root_dir.handle.createDirPathOpen(io, joined_path, opts);
}
pub fn statFile(p: Path, io: Io, sub_path: []const u8) !Io.Dir.Stat {
var buf: [Io.Dir.max_path_bytes]u8 = undefined;
const joined_path = if (p.sub_path.len == 0) sub_path else p: {
break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
p.sub_path, sub_path,
}) catch return error.NameTooLong;
};
return p.root_dir.handle.statFile(io, joined_path, .{});
}
pub fn atomicFile(
p: Path,
io: Io,
sub_path: []const u8,
options: Io.Dir.CreateFileAtomicOptions,
buf: *[Io.Dir.max_path_bytes]u8,
) !Io.File.Atomic {
const joined_path = if (p.sub_path.len == 0) sub_path else p: {
break :p std.mem.print(buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
p.sub_path, sub_path,
}) catch return error.NameTooLong;
};
return p.root_dir.handle.createFileAtomic(io, joined_path, options);
}
pub fn access(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.AccessOptions) !void {
var buf: [Io.Dir.max_path_bytes]u8 = undefined;
const joined_path = if (p.sub_path.len == 0) sub_path else p: {
break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
p.sub_path, sub_path,
}) catch return error.NameTooLong;
};
return p.root_dir.handle.access(io, joined_path, flags);
}
pub fn createDirPath(p: Path, io: Io, sub_path: []const u8) !void {
var buf: [Io.Dir.max_path_bytes]u8 = undefined;
const joined_path = if (p.sub_path.len == 0) sub_path else p: {
break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
p.sub_path, sub_path,
}) catch return error.NameTooLong;
};
return p.root_dir.handle.createDirPath(io, joined_path);
}
pub fn toString(p: Path, allocator: Allocator) Allocator.Error![]u8 {
return allocator.print("{f}", .{p});
}
pub fn toStringZ(p: Path, allocator: Allocator) Allocator.Error![:0]u8 {
return allocator.printSentinel("{f}", .{p}, 0);
}
pub fn fmtEscapeString(path: Path) std.fmt.Alt(Path, formatEscapeString) {
return .{ .data = path };
}
pub fn formatEscapeString(path: Path, writer: *Io.Writer) Io.Writer.Error!void {
if (path.root_dir.path) |p| {
try std.zig.stringEscape(p, writer);
if (path.sub_path.len > 0) try std.zig.stringEscape(Io.Dir.path.sep_str, writer);
}
if (path.sub_path.len > 0) {
try std.zig.stringEscape(path.sub_path, writer);
}
}
/// Deprecated, use double quoted escape to print paths.
pub fn fmtEscapeChar(path: Path) std.fmt.Alt(Path, formatEscapeChar) {
return .{ .data = path };
}
/// Deprecated, use double quoted escape to print paths.
pub fn formatEscapeChar(path: Path, writer: *Io.Writer) Io.Writer.Error!void {
if (path.root_dir.path) |p| {
for (p) |byte| try std.zig.charEscape(byte, writer);
if (path.sub_path.len > 0) try writer.writeByte(Io.Dir.path.sep);
}
if (path.sub_path.len > 0) {
for (path.sub_path) |byte| try std.zig.charEscape(byte, writer);
}
}
pub fn format(self: Path, writer: *Io.Writer) Io.Writer.Error!void {
if (Io.Dir.path.isAbsolute(self.sub_path)) {
return writer.writeAll(self.sub_path);
} else if (self.root_dir.path) |p| {
var bufs: [3][]const u8 = .{ p, Io.Dir.path.sep_str, self.sub_path };
return writer.writeVecAll(bufs[0..if (self.sub_path.len > 0) 3 else 1]);
} else if (self.sub_path.len > 0) {
return writer.writeAll(self.sub_path);
} else {
return writer.writeByte('.');
}
}
pub fn eql(self: Path, other: Path) bool {
return self.root_dir.eql(other.root_dir) and std.mem.eql(u8, self.sub_path, other.sub_path);
}
pub fn subPathOpt(self: Path) ?[]const u8 {
return if (self.sub_path.len == 0) null else self.sub_path;
}
pub fn subPathOrDot(self: Path) []const u8 {
return if (self.sub_path.len == 0) "." else self.sub_path;
}
pub fn stem(p: Path) []const u8 {
return Io.Dir.path.stem(p.sub_path);
}
pub fn dirname(p: Path) ?Path {
return .{
.root_dir = p.root_dir,
.sub_path = Io.Dir.path.dirname(p.subPathOpt() orelse return null) orelse "",
};
}
pub fn basename(p: Path) []const u8 {
return Io.Dir.path.basename(p.sub_path);
}
/// Useful to make `Path` a key in `std.ArrayHashMap`.
pub const TableAdapter = struct {
pub const Hash = std.hash.Wyhash;
pub fn hash(self: TableAdapter, a: Cache.Path) u32 {
_ = self;
const seed = switch (@typeInfo(@TypeOf(a.root_dir.handle.handle))) {
.pointer => @intFromPtr(a.root_dir.handle.handle),
.int => @as(u32, @bitCast(a.root_dir.handle.handle)),
else => @compileError("unimplemented hash function"),
};
return @truncate(Hash.hash(seed, a.sub_path));
}
pub fn eql(self: TableAdapter, a: Cache.Path, b: Cache.Path, b_index: usize) bool {
_ = self;
_ = b_index;
return a.eql(b);
}
};