authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-06 23:59:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
log9eb21541ec9e8b64ae0d64e2851ed8d105308a35
tree29003c399bdcd078e71c758d6acf200e038aff11
parentcbb9b5d9f04509260b8cea935a0441f63f33bc32

make Package.Path support string escape formatting


3 files changed, 21 insertions(+), 5 deletions(-)

lib/std/zig.zig+1-1
......@@ -1,6 +1,6 @@
11const std = @import("std.zig");
22const tokenizer = @import("zig/tokenizer.zig");
3const fmt = @import("zig/fmt.zig");
3pub const fmt = @import("zig/fmt.zig");
44const assert = std.debug.assert;
55
66pub const ErrorBundle = @import("zig/ErrorBundle.zig");
lib/std/zig/fmt.zig+3-3
......@@ -13,7 +13,7 @@ fn formatId(
1313 return writer.writeAll(bytes);
1414 }
1515 try writer.writeAll("@\"");
16 try formatEscapes(bytes, "", options, writer);
16 try stringEscape(bytes, "", options, writer);
1717 try writer.writeByte('"');
1818}
1919
......@@ -47,7 +47,7 @@ test "isValidId" {
4747/// Print the string as escaped contents of a double quoted or single-quoted string.
4848/// Format `{}` treats contents as a double-quoted string.
4949/// Format `{'}` treats contents as a single-quoted string.
50fn formatEscapes(
50pub fn stringEscape(
5151 bytes: []const u8,
5252 comptime fmt: []const u8,
5353 options: std.fmt.FormatOptions,
......@@ -90,7 +90,7 @@ fn formatEscapes(
9090/// The format specifier must be one of:
9191/// * `{}` treats contents as a double-quoted string.
9292/// * `{'}` treats contents as a single-quoted string.
93pub fn fmtEscapes(bytes: []const u8) std.fmt.Formatter(formatEscapes) {
93pub fn fmtEscapes(bytes: []const u8) std.fmt.Formatter(stringEscape) {
9494 return .{ .data = bytes };
9595}
9696
src/Package.zig+17-1
......@@ -89,7 +89,23 @@ pub const Path = struct {
8989 options: std.fmt.FormatOptions,
9090 writer: anytype,
9191 ) !void {
92 _ = options;
92 if (fmt_string.len == 1) {
93 // Quote-escape the string.
94 const stringEscape = std.zig.fmt.stringEscape;
95 const f = switch (fmt_string[0]) {
96 'q' => "",
97 '\'' => '\'',
98 else => @compileError("unsupported format string: " ++ fmt_string),
99 };
100 if (self.root_dir.path) |p| {
101 try stringEscape(p, f, options, writer);
102 if (self.sub_path.len > 0) try writer.writeAll(fs.path.sep_str);
103 }
104 if (self.sub_path.len > 0) {
105 try stringEscape(self.sub_path, f, options, writer);
106 }
107 return;
108 }
93109 if (fmt_string.len > 0)
94110 std.fmt.invalidFmtError(fmt_string, self);
95111 if (self.root_dir.path) |p| {