Skip to content

Commit a15fb2d

Browse files
committed
make Package.Path support string escape formatting
1 parent 49bb45c commit a15fb2d

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

lib/std/zig.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std.zig");
22
const tokenizer = @import("zig/tokenizer.zig");
3-
const fmt = @import("zig/fmt.zig");
3+
pub const fmt = @import("zig/fmt.zig");
44
const assert = std.debug.assert;
55

66
pub const ErrorBundle = @import("zig/ErrorBundle.zig");

lib/std/zig/fmt.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
50-
fn formatEscapes(
50+
pub 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.
93-
pub fn fmtEscapes(bytes: []const u8) std.fmt.Formatter(formatEscapes) {
93+
pub fn fmtEscapes(bytes: []const u8) std.fmt.Formatter(stringEscape) {
9494
return .{ .data = bytes };
9595
}
9696

src/Package.zig

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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| {

0 commit comments

Comments
 (0)