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 @@...@@ -1,6 +1,6 @@
1const std = @import("std.zig");1const std = @import("std.zig");
2const tokenizer = @import("zig/tokenizer.zig");2const tokenizer = @import("zig/tokenizer.zig");
3const fmt = @import("zig/fmt.zig");3pub const fmt = @import("zig/fmt.zig");
4const assert = std.debug.assert;4const assert = std.debug.assert;
55
6pub const ErrorBundle = @import("zig/ErrorBundle.zig");6pub const ErrorBundle = @import("zig/ErrorBundle.zig");
lib/std/zig/fmt.zig+3-3
...@@ -13,7 +13,7 @@ fn formatId(...@@ -13,7 +13,7 @@ fn formatId(
13 return writer.writeAll(bytes);13 return writer.writeAll(bytes);
14 }14 }
15 try writer.writeAll("@\"");15 try writer.writeAll("@\"");
16 try formatEscapes(bytes, "", options, writer);16 try stringEscape(bytes, "", options, writer);
17 try writer.writeByte('"');17 try writer.writeByte('"');
18}18}
1919
...@@ -47,7 +47,7 @@ test "isValidId" {...@@ -47,7 +47,7 @@ test "isValidId" {
47/// Print the string as escaped contents of a double quoted or single-quoted string.47/// Print the string as escaped contents of a double quoted or single-quoted string.
48/// Format `{}` treats contents as a double-quoted string.48/// Format `{}` treats contents as a double-quoted string.
49/// Format `{'}` treats contents as a single-quoted string.49/// Format `{'}` treats contents as a single-quoted string.
50fn formatEscapes(50pub fn stringEscape(
51 bytes: []const u8,51 bytes: []const u8,
52 comptime fmt: []const u8,52 comptime fmt: []const u8,
53 options: std.fmt.FormatOptions,53 options: std.fmt.FormatOptions,
...@@ -90,7 +90,7 @@ fn formatEscapes(...@@ -90,7 +90,7 @@ fn formatEscapes(
90/// The format specifier must be one of:90/// The format specifier must be one of:
91/// * `{}` treats contents as a double-quoted string.91/// * `{}` treats contents as a double-quoted string.
92/// * `{'}` treats contents as a single-quoted string.92/// * `{'}` 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) {
94 return .{ .data = bytes };94 return .{ .data = bytes };
95}95}
9696
src/Package.zig+17-1
...@@ -89,7 +89,23 @@ pub const Path = struct {...@@ -89,7 +89,23 @@ pub const Path = struct {
89 options: std.fmt.FormatOptions,89 options: std.fmt.FormatOptions,
90 writer: anytype,90 writer: anytype,
91 ) !void {91 ) !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 }
93 if (fmt_string.len > 0)109 if (fmt_string.len > 0)
94 std.fmt.invalidFmtError(fmt_string, self);110 std.fmt.invalidFmtError(fmt_string, self);
95 if (self.root_dir.path) |p| {111 if (self.root_dir.path) |p| {