diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-07-10 12:04:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-10 12:04:27 +0200 |
| commit | 1a998886c863a1829d649f196093f1058cd9cf13 (patch) | |
| tree | db1b3b0a043a113cfb6544e8103cb64f8e4e4d8f /lib/std/Build/Cache/Path.zig | |
| parent | 5b4b033236a7bed19c90faf7fefbc1990911cfef (diff) | |
| parent | 10d6db5d7d1fb62ee2915a7ad2c7feb771ea3bbb (diff) | |
| download | zig-1a998886c863a1829d649f196093f1058cd9cf13.tar.gz zig-1a998886c863a1829d649f196093f1058cd9cf13.zip | |
Merge pull request #24329 from ziglang/writergate
Deprecates all existing std.io readers and writers in favor of the newly
provided std.io.Reader and std.io.Writer which are non-generic and have the
buffer above the vtable - in other words the buffer is in the interface, not
the implementation. This means that although Reader and Writer are no longer
generic, they are still transparent to optimization; all of the interface
functions have a concrete hot path operating on the buffer, and only make
vtable calls when the buffer is full.
Diffstat (limited to 'lib/std/Build/Cache/Path.zig')
| -rw-r--r-- | lib/std/Build/Cache/Path.zig | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/lib/std/Build/Cache/Path.zig b/lib/std/Build/Cache/Path.zig index 8822fb64be..a0a58067fc 100644 --- a/lib/std/Build/Cache/Path.zig +++ b/lib/std/Build/Cache/Path.zig @@ -1,3 +1,10 @@ +const Path = @This(); +const std = @import("../../std.zig"); +const assert = std.debug.assert; +const fs = std.fs; +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. @@ -133,38 +140,42 @@ pub fn makePath(p: Path, sub_path: []const u8) !void { } pub fn toString(p: Path, allocator: Allocator) Allocator.Error![]u8 { - return std.fmt.allocPrint(allocator, "{}", .{p}); + return std.fmt.allocPrint(allocator, "{f}", .{p}); } pub fn toStringZ(p: Path, allocator: Allocator) Allocator.Error![:0]u8 { - return std.fmt.allocPrintZ(allocator, "{}", .{p}); -} - -pub fn format( - self: Path, - comptime fmt_string: []const u8, - options: std.fmt.FormatOptions, - writer: anytype, -) !void { - if (fmt_string.len == 1) { - // Quote-escape the string. - const stringEscape = std.zig.stringEscape; - const f = switch (fmt_string[0]) { - 'q' => "", - '\'' => "\'", - else => @compileError("unsupported format string: " ++ fmt_string), - }; - if (self.root_dir.path) |p| { - try stringEscape(p, f, options, writer); - if (self.sub_path.len > 0) try stringEscape(fs.path.sep_str, f, options, writer); - } - if (self.sub_path.len > 0) { - try stringEscape(self.sub_path, f, options, writer); - } - return; + return std.fmt.allocPrintSentinel(allocator, "{f}", .{p}, 0); +} + +pub fn fmtEscapeString(path: Path) std.fmt.Formatter(Path, formatEscapeString) { + return .{ .data = path }; +} + +pub fn formatEscapeString(path: Path, writer: *std.io.Writer) std.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(fs.path.sep_str, writer); } - if (fmt_string.len > 0) - std.fmt.invalidFmtError(fmt_string, self); + if (path.sub_path.len > 0) { + try std.zig.stringEscape(path.sub_path, writer); + } +} + +pub fn fmtEscapeChar(path: Path) std.fmt.Formatter(Path, formatEscapeChar) { + return .{ .data = path }; +} + +pub fn formatEscapeChar(path: Path, writer: *std.io.Writer) std.io.Writer.Error!void { + if (path.root_dir.path) |p| { + try std.zig.charEscape(p, writer); + if (path.sub_path.len > 0) try std.zig.charEscape(fs.path.sep_str, writer); + } + if (path.sub_path.len > 0) { + try std.zig.charEscape(path.sub_path, writer); + } +} + +pub fn format(self: Path, writer: *std.io.Writer) std.io.Writer.Error!void { if (std.fs.path.isAbsolute(self.sub_path)) { try writer.writeAll(self.sub_path); return; @@ -223,9 +234,3 @@ pub const TableAdapter = struct { return a.eql(b); } }; - -const Path = @This(); -const std = @import("../../std.zig"); -const fs = std.fs; -const Allocator = std.mem.Allocator; -const Cache = std.Build.Cache; |
