Skip to content

Commit c3da98c

Browse files
committed
std.zon: update to new I/O API
1 parent b956b02 commit c3da98c

File tree

5 files changed

+959
-973
lines changed

5 files changed

+959
-973
lines changed

lib/std/Build/Cache/Path.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,19 @@ pub fn formatEscapeString(path: Path, writer: *std.io.Writer) std.io.Writer.Erro
161161
}
162162
}
163163

164+
/// Deprecated, use double quoted escape to print paths.
164165
pub fn fmtEscapeChar(path: Path) std.fmt.Formatter(Path, formatEscapeChar) {
165166
return .{ .data = path };
166167
}
167168

169+
/// Deprecated, use double quoted escape to print paths.
168170
pub fn formatEscapeChar(path: Path, writer: *std.io.Writer) std.io.Writer.Error!void {
169171
if (path.root_dir.path) |p| {
170-
try std.zig.charEscape(p, writer);
171-
if (path.sub_path.len > 0) try std.zig.charEscape(fs.path.sep_str, writer);
172+
for (p) |byte| try std.zig.charEscape(byte, writer);
173+
if (path.sub_path.len > 0) try writer.writeByte(fs.path.sep);
172174
}
173175
if (path.sub_path.len > 0) {
174-
try std.zig.charEscape(path.sub_path, writer);
176+
for (path.sub_path) |byte| try std.zig.charEscape(byte, writer);
175177
}
176178
}
177179

lib/std/zig.zig

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ pub fn fmtString(bytes: []const u8) std.fmt.Formatter([]const u8, stringEscape)
446446
}
447447

448448
/// Return a formatter for escaping a single quoted Zig string.
449-
pub fn fmtChar(bytes: []const u8) std.fmt.Formatter([]const u8, charEscape) {
450-
return .{ .data = bytes };
449+
pub fn fmtChar(c: u21) std.fmt.Formatter(u21, charEscape) {
450+
return .{ .data = c };
451451
}
452452

453453
test fmtString {
@@ -458,9 +458,7 @@ test fmtString {
458458
}
459459

460460
test fmtChar {
461-
try std.testing.expectFmt(
462-
\\" \\ hi \x07 \x11 " derp \'"
463-
, "\"{f}\"", .{fmtChar(" \\ hi \x07 \x11 \" derp '")});
461+
try std.testing.expectFmt("c \\u{26a1}", "{f} {f}", .{ fmtChar('c'), fmtChar('⚡') });
464462
}
465463

466464
/// Print the string as escaped contents of a double quoted string.
@@ -480,21 +478,26 @@ pub fn stringEscape(bytes: []const u8, w: *Writer) Writer.Error!void {
480478
};
481479
}
482480

483-
/// Print the string as escaped contents of a single-quoted string.
484-
pub fn charEscape(bytes: []const u8, w: *Writer) Writer.Error!void {
485-
for (bytes) |byte| switch (byte) {
481+
/// Print as escaped contents of a single-quoted string.
482+
pub fn charEscape(codepoint: u21, w: *Writer) Writer.Error!void {
483+
switch (codepoint) {
486484
'\n' => try w.writeAll("\\n"),
487485
'\r' => try w.writeAll("\\r"),
488486
'\t' => try w.writeAll("\\t"),
489487
'\\' => try w.writeAll("\\\\"),
490-
'"' => try w.writeByte('"'),
491488
'\'' => try w.writeAll("\\'"),
492-
' ', '!', '#'...'&', '('...'[', ']'...'~' => try w.writeByte(byte),
489+
'"', ' ', '!', '#'...'&', '('...'[', ']'...'~' => try w.writeByte(@intCast(codepoint)),
493490
else => {
494-
try w.writeAll("\\x");
495-
try w.printInt(byte, 16, .lower, .{ .width = 2, .fill = '0' });
491+
if (std.math.cast(u8, codepoint)) |byte| {
492+
try w.writeAll("\\x");
493+
try w.printInt(byte, 16, .lower, .{ .width = 2, .fill = '0' });
494+
} else {
495+
try w.writeAll("\\u{");
496+
try w.printInt(codepoint, 16, .lower, .{});
497+
try w.writeByte('}');
498+
}
496499
},
497-
};
500+
}
498501
}
499502

500503
pub fn isValidId(bytes: []const u8) bool {

lib/std/zig/Ast.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ pub fn renderError(tree: Ast, parse_error: Error, w: *Writer) Writer.Error!void
574574
'/' => "comment",
575575
else => unreachable,
576576
},
577-
std.zig.fmtChar(tok_slice[parse_error.extra.offset..][0..1]),
577+
std.zig.fmtChar(tok_slice[parse_error.extra.offset]),
578578
});
579579
},
580580

lib/std/zon/parse.zig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ pub const Error = union(enum) {
6464
}
6565
};
6666

67-
fn formatMessage(self: []const u8, w: *std.io.Writer) std.io.Writer.Error!void {
67+
fn formatMessage(self: []const u8, w: *std.Io.Writer) std.Io.Writer.Error!void {
6868
// Just writes the string for now, but we're keeping this behind a formatter so we have
6969
// the option to extend it in the future to print more advanced messages (like `Error`
7070
// does) without breaking the API.
7171
try w.writeAll(self);
7272
}
7373

74-
pub fn fmtMessage(self: Note, diag: *const Diagnostics) std.fmt.Formatter([]const u8, Note.formatMessage) {
74+
pub fn fmtMessage(self: Note, diag: *const Diagnostics) std.fmt.Alt([]const u8, Note.formatMessage) {
7575
return .{ .data = switch (self) {
7676
.zoir => |note| note.msg.get(diag.zoir),
7777
.type_check => |note| note.msg,
@@ -147,14 +147,14 @@ pub const Error = union(enum) {
147147
diag: *const Diagnostics,
148148
};
149149

150-
fn formatMessage(self: FormatMessage, w: *std.io.Writer) std.io.Writer.Error!void {
150+
fn formatMessage(self: FormatMessage, w: *std.Io.Writer) std.Io.Writer.Error!void {
151151
switch (self.err) {
152152
.zoir => |err| try w.writeAll(err.msg.get(self.diag.zoir)),
153153
.type_check => |tc| try w.writeAll(tc.message),
154154
}
155155
}
156156

157-
pub fn fmtMessage(self: @This(), diag: *const Diagnostics) std.fmt.Formatter(FormatMessage, formatMessage) {
157+
pub fn fmtMessage(self: @This(), diag: *const Diagnostics) std.fmt.Alt(FormatMessage, formatMessage) {
158158
return .{ .data = .{
159159
.err = self,
160160
.diag = diag,
@@ -226,7 +226,7 @@ pub const Diagnostics = struct {
226226
return .{ .diag = self };
227227
}
228228

229-
pub fn format(self: *const @This(), w: *std.io.Writer) std.io.Writer.Error!void {
229+
pub fn format(self: *const @This(), w: *std.Io.Writer) std.Io.Writer.Error!void {
230230
var errors = self.iterateErrors();
231231
while (errors.next()) |err| {
232232
const loc = err.getLocation(self);
@@ -606,7 +606,7 @@ const Parser = struct {
606606
}
607607
}
608608

609-
fn parseSlicePointer(self: *@This(), T: type, node: Zoir.Node.Index) !T {
609+
fn parseSlicePointer(self: *@This(), T: type, node: Zoir.Node.Index) ParseExprInnerError!T {
610610
switch (node.get(self.zoir)) {
611611
.string_literal => return self.parseString(T, node),
612612
.array_literal => |nodes| return self.parseSlice(T, nodes),
@@ -1048,6 +1048,7 @@ const Parser = struct {
10481048
name: []const u8,
10491049
) error{ OutOfMemory, ParseZon } {
10501050
@branchHint(.cold);
1051+
const gpa = self.gpa;
10511052
const token = if (field) |f| b: {
10521053
var buf: [2]Ast.Node.Index = undefined;
10531054
const struct_init = self.ast.fullStructInit(&buf, node.getAstNode(self.zoir)).?;
@@ -1065,21 +1066,20 @@ const Parser = struct {
10651066
};
10661067
} else b: {
10671068
const msg = "supported: ";
1068-
var buf: std.ArrayListUnmanaged(u8) = try .initCapacity(self.gpa, 64);
1069-
defer buf.deinit(self.gpa);
1070-
const writer = buf.writer(self.gpa);
1071-
try writer.writeAll(msg);
1069+
var buf: std.ArrayListUnmanaged(u8) = try .initCapacity(gpa, 64);
1070+
defer buf.deinit(gpa);
1071+
try buf.appendSlice(gpa, msg);
10721072
inline for (info.fields, 0..) |field_info, i| {
1073-
if (i != 0) try writer.writeAll(", ");
1074-
try writer.print("'{f}'", .{std.zig.fmtIdFlags(field_info.name, .{
1073+
if (i != 0) try buf.appendSlice(gpa, ", ");
1074+
try buf.print(gpa, "'{f}'", .{std.zig.fmtIdFlags(field_info.name, .{
10751075
.allow_primitive = true,
10761076
.allow_underscore = true,
10771077
})});
10781078
}
10791079
break :b .{
10801080
.token = token,
10811081
.offset = 0,
1082-
.msg = try buf.toOwnedSlice(self.gpa),
1082+
.msg = try buf.toOwnedSlice(gpa),
10831083
.owned = true,
10841084
};
10851085
};

0 commit comments

Comments
 (0)