diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-07-05 10:43:14 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-07-07 22:43:53 -0700 |
| commit | 5378fdb153bc76990105e3640e7725e434e8cdee (patch) | |
| tree | 9791e08538c10c521f5634ed79e3b369fcd004df /lib/std/Build/Cache/Path.zig | |
| parent | 4ccc6f2b5777afd06f0fddbea4e0e0d0c92b007d (diff) | |
| download | zig-5378fdb153bc76990105e3640e7725e434e8cdee.tar.gz zig-5378fdb153bc76990105e3640e7725e434e8cdee.zip | |
std.fmt: fully remove format string from format methods
Introduces `std.fmt.alt` which is a helper for calling alternate format
methods besides one named "format".
Diffstat (limited to 'lib/std/Build/Cache/Path.zig')
| -rw-r--r-- | lib/std/Build/Cache/Path.zig | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/lib/std/Build/Cache/Path.zig b/lib/std/Build/Cache/Path.zig index 55af4fdbfa..a0a58067fc 100644 --- a/lib/std/Build/Cache/Path.zig +++ b/lib/std/Build/Cache/Path.zig @@ -147,25 +147,35 @@ pub fn toStringZ(p: Path, allocator: Allocator) Allocator.Error![:0]u8 { return std.fmt.allocPrintSentinel(allocator, "{f}", .{p}, 0); } -pub fn format(self: Path, writer: *std.io.Writer, comptime f: []const u8) std.io.Writer.Error!void { - if (f.len == 1) { - // Quote-escape the string. - const zigEscape = switch (f[0]) { - 'q' => std.zig.stringEscape, - '\'' => std.zig.charEscape, - else => @compileError("unsupported format string: " ++ f), - }; - if (self.root_dir.path) |p| { - try zigEscape(p, writer); - if (self.sub_path.len > 0) try zigEscape(fs.path.sep_str, writer); - } - if (self.sub_path.len > 0) { - try zigEscape(self.sub_path, writer); - } - return; +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 (path.sub_path.len > 0) { + try std.zig.stringEscape(path.sub_path, writer); } - if (f.len > 0) - std.fmt.invalidFmtError(f, self); +} + +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; |
