authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-27 16:05:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-28 18:30:57-07:00
logf7884961c230367cefd3dfbf730ac5277297dc63
treee9f49ef21141371c1643a28ac7aa7347afc43cba
parent4f510dba10872cf7b71d1a48b4dc5879444f89d7

update more to avoid GenericWriter


10 files changed, 128 insertions(+), 99 deletions(-)

lib/std/Io/Reader.zig+32-1
...@@ -784,7 +784,7 @@ pub fn peekDelimiterInclusive(r: *Reader, delimiter: u8) DelimiterError![]u8 {...@@ -784,7 +784,7 @@ pub fn peekDelimiterInclusive(r: *Reader, delimiter: u8) DelimiterError![]u8 {
784}784}
785785
786/// Returns a slice of the next bytes of buffered data from the stream until786/// Returns a slice of the next bytes of buffered data from the stream until
787/// `delimiter` is found, advancing the seek position.787/// `delimiter` is found, advancing the seek position up to the delimiter.
788///788///
789/// Returned slice excludes the delimiter. End-of-stream is treated equivalent789/// Returned slice excludes the delimiter. End-of-stream is treated equivalent
790/// to a delimiter, unless it would result in a length 0 return value, in which790/// to a delimiter, unless it would result in a length 0 return value, in which
...@@ -814,6 +814,37 @@ pub fn takeDelimiterExclusive(r: *Reader, delimiter: u8) DelimiterError![]u8 {...@@ -814,6 +814,37 @@ pub fn takeDelimiterExclusive(r: *Reader, delimiter: u8) DelimiterError![]u8 {
814 return result[0 .. result.len - 1];814 return result[0 .. result.len - 1];
815}815}
816816
817/// Returns a slice of the next bytes of buffered data from the stream until
818/// `delimiter` is found, advancing the seek position past the delimiter.
819///
820/// Returned slice excludes the delimiter. End-of-stream is treated equivalent
821/// to a delimiter, unless it would result in a length 0 return value, in which
822/// case `null` is returned instead.
823///
824/// If the delimiter is not found within a number of bytes matching the
825/// capacity of this `Reader`, `error.StreamTooLong` is returned. In
826/// such case, the stream state is unmodified as if this function was never
827/// called.
828///
829/// Invalidates previously returned values from `peek`.
830///
831/// See also:
832/// * `takeDelimiterInclusive`
833/// * `takeDelimiterExclusive`
834pub fn takeDelimiter(r: *Reader, delimiter: u8) error{ ReadFailed, StreamTooLong }!?[]u8 {
835 const result = r.peekDelimiterInclusive(delimiter) catch |err| switch (err) {
836 error.EndOfStream => {
837 const remaining = r.buffer[r.seek..r.end];
838 if (remaining.len == 0) return null;
839 r.toss(remaining.len);
840 return remaining;
841 },
842 else => |e| return e,
843 };
844 r.toss(result.len + 1);
845 return result[0 .. result.len - 1];
846}
847
817/// Returns a slice of the next bytes of buffered data from the stream until848/// Returns a slice of the next bytes of buffered data from the stream until
818/// `delimiter` is found, without advancing the seek position.849/// `delimiter` is found, without advancing the seek position.
819///850///
src/link/MachO/dyld_info/Rebase.zig+6-7
...@@ -228,13 +228,13 @@ fn setTypePointer(writer: anytype) !void {...@@ -228,13 +228,13 @@ fn setTypePointer(writer: anytype) !void {
228fn setSegmentOffset(segment_id: u8, offset: u64, writer: anytype) !void {228fn setSegmentOffset(segment_id: u8, offset: u64, writer: anytype) !void {
229 log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset });229 log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset });
230 try writer.writeByte(macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @as(u4, @truncate(segment_id)));230 try writer.writeByte(macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @as(u4, @truncate(segment_id)));
231 try std.leb.writeUleb128(writer, offset);231 try writer.writeUleb128(offset);
232}232}
233233
234fn rebaseAddAddr(addr: u64, writer: anytype) !void {234fn rebaseAddAddr(addr: u64, writer: anytype) !void {
235 log.debug(">>> rebase with add: {x}", .{addr});235 log.debug(">>> rebase with add: {x}", .{addr});
236 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB);236 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB);
237 try std.leb.writeUleb128(writer, addr);237 try writer.writeUleb128(addr);
238}238}
239239
240fn rebaseTimes(count: usize, writer: anytype) !void {240fn rebaseTimes(count: usize, writer: anytype) !void {
...@@ -243,15 +243,15 @@ fn rebaseTimes(count: usize, writer: anytype) !void {...@@ -243,15 +243,15 @@ fn rebaseTimes(count: usize, writer: anytype) !void {
243 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | @as(u4, @truncate(count)));243 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | @as(u4, @truncate(count)));
244 } else {244 } else {
245 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES);245 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
246 try std.leb.writeUleb128(writer, count);246 try writer.writeUleb128(count);
247 }247 }
248}248}
249249
250fn rebaseTimesSkip(count: usize, skip: u64, writer: anytype) !void {250fn rebaseTimesSkip(count: usize, skip: u64, writer: anytype) !void {
251 log.debug(">>> rebase with count: {d} and skip: {x}", .{ count, skip });251 log.debug(">>> rebase with count: {d} and skip: {x}", .{ count, skip });
252 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);252 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);
253 try std.leb.writeUleb128(writer, count);253 try writer.writeUleb128(count);
254 try std.leb.writeUleb128(writer, skip);254 try writer.writeUleb128(skip);
255}255}
256256
257fn addAddr(addr: u64, writer: anytype) !void {257fn addAddr(addr: u64, writer: anytype) !void {
...@@ -264,7 +264,7 @@ fn addAddr(addr: u64, writer: anytype) !void {...@@ -264,7 +264,7 @@ fn addAddr(addr: u64, writer: anytype) !void {
264 }264 }
265 }265 }
266 try writer.writeByte(macho.REBASE_OPCODE_ADD_ADDR_ULEB);266 try writer.writeByte(macho.REBASE_OPCODE_ADD_ADDR_ULEB);
267 try std.leb.writeUleb128(writer, addr);267 try writer.writeUleb128(addr);
268}268}
269269
270fn done(writer: anytype) !void {270fn done(writer: anytype) !void {
...@@ -651,7 +651,6 @@ test "rebase - composite" {...@@ -651,7 +651,6 @@ test "rebase - composite" {
651651
652const std = @import("std");652const std = @import("std");
653const assert = std.debug.assert;653const assert = std.debug.assert;
654const leb = std.leb;
655const log = std.log.scoped(.link_dyld_info);654const log = std.log.scoped(.link_dyld_info);
656const macho = std.macho;655const macho = std.macho;
657const mem = std.mem;656const mem = std.mem;
src/link/MachO/dyld_info/Trie.zig+4-5
...@@ -262,13 +262,13 @@ fn writeNode(self: *Trie, node_index: Node.Index, writer: *std.Io.Writer) !void...@@ -262,13 +262,13 @@ fn writeNode(self: *Trie, node_index: Node.Index, writer: *std.Io.Writer) !void
262 // TODO Implement for special flags.262 // TODO Implement for special flags.
263 assert(export_flags & macho.EXPORT_SYMBOL_FLAGS_REEXPORT == 0 and263 assert(export_flags & macho.EXPORT_SYMBOL_FLAGS_REEXPORT == 0 and
264 export_flags & macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER == 0);264 export_flags & macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER == 0);
265 try leb.writeUleb128(&info_stream, export_flags);265 try info_stream.writeUleb128(export_flags);
266 try leb.writeUleb128(&info_stream, vmaddr_offset);266 try info_stream.writeUleb128(vmaddr_offset);
267267
268 // Encode the size of the terminal node info.268 // Encode the size of the terminal node info.
269 var size_buf: [@sizeOf(u64)]u8 = undefined;269 var size_buf: [@sizeOf(u64)]u8 = undefined;
270 var size_stream: std.Io.Writer = .fixed(&size_buf);270 var size_stream: std.Io.Writer = .fixed(&size_buf);
271 try leb.writeUleb128(&size_stream, info_stream.end);271 try size_stream.writeUleb128(info_stream.end);
272272
273 // Now, write them to the output stream.273 // Now, write them to the output stream.
274 try writer.writeAll(size_buf[0..size_stream.end]);274 try writer.writeAll(size_buf[0..size_stream.end]);
...@@ -285,7 +285,7 @@ fn writeNode(self: *Trie, node_index: Node.Index, writer: *std.Io.Writer) !void...@@ -285,7 +285,7 @@ fn writeNode(self: *Trie, node_index: Node.Index, writer: *std.Io.Writer) !void
285 // Write edge label and offset to next node in trie.285 // Write edge label and offset to next node in trie.
286 try writer.writeAll(edge.label);286 try writer.writeAll(edge.label);
287 try writer.writeByte(0);287 try writer.writeByte(0);
288 try leb.writeUleb128(writer, slice.items(.trie_offset)[edge.node]);288 try writer.writeUleb128(slice.items(.trie_offset)[edge.node]);
289 }289 }
290}290}
291291
...@@ -419,7 +419,6 @@ test "ordering bug" {...@@ -419,7 +419,6 @@ test "ordering bug" {
419}419}
420420
421const assert = std.debug.assert;421const assert = std.debug.assert;
422const leb = std.leb;
423const log = std.log.scoped(.macho);422const log = std.log.scoped(.macho);
424const macho = std.macho;423const macho = std.macho;
425const mem = std.mem;424const mem = std.mem;
src/link/MachO/dyld_info/bind.zig+6-7
...@@ -605,7 +605,7 @@ pub const LazyBind = struct {...@@ -605,7 +605,7 @@ pub const LazyBind = struct {
605fn setSegmentOffset(segment_id: u8, offset: u64, writer: *std.Io.Writer) !void {605fn setSegmentOffset(segment_id: u8, offset: u64, writer: *std.Io.Writer) !void {
606 log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset });606 log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset });
607 try writer.writeByte(macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @as(u4, @truncate(segment_id)));607 try writer.writeByte(macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @as(u4, @truncate(segment_id)));
608 try std.leb.writeUleb128(writer, offset);608 try writer.writeUleb128(offset);
609}609}
610610
611fn setSymbol(name: []const u8, flags: u8, writer: *std.Io.Writer) !void {611fn setSymbol(name: []const u8, flags: u8, writer: *std.Io.Writer) !void {
...@@ -639,7 +639,7 @@ fn setDylibOrdinal(ordinal: i16, writer: *std.Io.Writer) !void {...@@ -639,7 +639,7 @@ fn setDylibOrdinal(ordinal: i16, writer: *std.Io.Writer) !void {
639 try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | @as(u4, @truncate(cast)));639 try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | @as(u4, @truncate(cast)));
640 } else {640 } else {
641 try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);641 try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
642 try std.leb.writeUleb128(writer, cast);642 try writer.writeUleb128(cast);
643 }643 }
644 }644 }
645}645}
...@@ -667,20 +667,20 @@ fn doBindAddAddr(addr: u64, writer: *std.Io.Writer) !void {...@@ -667,20 +667,20 @@ fn doBindAddAddr(addr: u64, writer: *std.Io.Writer) !void {
667 }667 }
668 }668 }
669 try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB);669 try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB);
670 try std.leb.writeUleb128(writer, addr);670 try writer.writeUleb128(addr);
671}671}
672672
673fn doBindTimesSkip(count: usize, skip: u64, writer: *std.Io.Writer) !void {673fn doBindTimesSkip(count: usize, skip: u64, writer: *std.Io.Writer) !void {
674 log.debug(">>> bind with count: {d} and skip: {x}", .{ count, skip });674 log.debug(">>> bind with count: {d} and skip: {x}", .{ count, skip });
675 try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB);675 try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB);
676 try std.leb.writeUleb128(writer, count);676 try writer.writeUleb128(count);
677 try std.leb.writeUleb128(writer, skip);677 try writer.writeUleb128(skip);
678}678}
679679
680fn addAddr(addr: u64, writer: *std.Io.Writer) !void {680fn addAddr(addr: u64, writer: *std.Io.Writer) !void {
681 log.debug(">>> add: {x}", .{addr});681 log.debug(">>> add: {x}", .{addr});
682 try writer.writeByte(macho.BIND_OPCODE_ADD_ADDR_ULEB);682 try writer.writeByte(macho.BIND_OPCODE_ADD_ADDR_ULEB);
683 try std.leb.writeUleb128(writer, addr);683 try writer.writeUleb128(addr);
684}684}
685685
686fn done(writer: *std.Io.Writer) !void {686fn done(writer: *std.Io.Writer) !void {
...@@ -689,7 +689,6 @@ fn done(writer: *std.Io.Writer) !void {...@@ -689,7 +689,6 @@ fn done(writer: *std.Io.Writer) !void {
689}689}
690690
691const assert = std.debug.assert;691const assert = std.debug.assert;
692const leb = std.leb;
693const log = std.log.scoped(.link_dyld_info);692const log = std.log.scoped(.link_dyld_info);
694const macho = std.macho;693const macho = std.macho;
695const mem = std.mem;694const mem = std.mem;
test/standalone/run_output_paths/create_file.zig+2-1
...@@ -10,7 +10,8 @@ pub fn main() !void {...@@ -10,7 +10,8 @@ pub fn main() !void {
10 dir_name, .{});10 dir_name, .{});
11 const file_name = args.next().?;11 const file_name = args.next().?;
12 const file = try dir.createFile(file_name, .{});12 const file = try dir.createFile(file_name, .{});
13 try file.deprecatedWriter().print(13 var file_writer = file.writer(&.{});
14 try file_writer.interface.print(
14 \\{s}15 \\{s}
15 \\{s}16 \\{s}
16 \\Hello, world!17 \\Hello, world!
tools/doctest.zig+45-45
...@@ -7,6 +7,7 @@ const process = std.process;...@@ -7,6 +7,7 @@ const process = std.process;
7const Allocator = std.mem.Allocator;7const Allocator = std.mem.Allocator;
8const testing = std.testing;8const testing = std.testing;
9const getExternalExecutor = std.zig.system.getExternalExecutor;9const getExternalExecutor = std.zig.system.getExternalExecutor;
10const Writer = std.Io.Writer;
1011
11const max_doc_file_size = 10 * 1024 * 1024;12const max_doc_file_size = 10 * 1024 * 1024;
1213
...@@ -108,7 +109,7 @@ pub fn main() !void {...@@ -108,7 +109,7 @@ pub fn main() !void {
108109
109fn printOutput(110fn printOutput(
110 arena: Allocator,111 arena: Allocator,
111 out: anytype,112 out: *Writer,
112 code: Code,113 code: Code,
113 /// Relative to this process' cwd.114 /// Relative to this process' cwd.
114 tmp_dir_path: []const u8,115 tmp_dir_path: []const u8,
...@@ -610,7 +611,7 @@ fn dumpArgs(args: []const []const u8) void {...@@ -610,7 +611,7 @@ fn dumpArgs(args: []const []const u8) void {
610 std.debug.print("\n", .{});611 std.debug.print("\n", .{});
611}612}
612613
613fn printSourceBlock(arena: Allocator, out: anytype, source_bytes: []const u8, name: []const u8) !void {614fn printSourceBlock(arena: Allocator, out: *Writer, source_bytes: []const u8, name: []const u8) !void {
614 try out.print("<figure><figcaption class=\"{s}-cap\"><cite class=\"file\">{s}</cite></figcaption><pre>", .{615 try out.print("<figure><figcaption class=\"{s}-cap\"><cite class=\"file\">{s}</cite></figcaption><pre>", .{
615 "zig", name,616 "zig", name,
616 });617 });
...@@ -618,7 +619,7 @@ fn printSourceBlock(arena: Allocator, out: anytype, source_bytes: []const u8, na...@@ -618,7 +619,7 @@ fn printSourceBlock(arena: Allocator, out: anytype, source_bytes: []const u8, na
618 try out.writeAll("</pre></figure>");619 try out.writeAll("</pre></figure>");
619}620}
620621
621fn tokenizeAndPrint(arena: Allocator, out: anytype, raw_src: []const u8) !void {622fn tokenizeAndPrint(arena: Allocator, out: *Writer, raw_src: []const u8) !void {
622 const src_non_terminated = mem.trim(u8, raw_src, " \r\n");623 const src_non_terminated = mem.trim(u8, raw_src, " \r\n");
623 const src = try arena.dupeZ(u8, src_non_terminated);624 const src = try arena.dupeZ(u8, src_non_terminated);
624625
...@@ -846,7 +847,7 @@ fn tokenizeAndPrint(arena: Allocator, out: anytype, raw_src: []const u8) !void {...@@ -846,7 +847,7 @@ fn tokenizeAndPrint(arena: Allocator, out: anytype, raw_src: []const u8) !void {
846 try out.writeAll("</code>");847 try out.writeAll("</code>");
847}848}
848849
849fn writeEscapedLines(out: anytype, text: []const u8) !void {850fn writeEscapedLines(out: *Writer, text: []const u8) !void {
850 return writeEscaped(out, text);851 return writeEscaped(out, text);
851}852}
852853
...@@ -983,7 +984,7 @@ fn escapeHtml(allocator: Allocator, input: []const u8) ![]u8 {...@@ -983,7 +984,7 @@ fn escapeHtml(allocator: Allocator, input: []const u8) ![]u8 {
983 return try buf.toOwnedSlice();984 return try buf.toOwnedSlice();
984}985}
985986
986fn writeEscaped(out: anytype, input: []const u8) !void {987fn writeEscaped(out: *Writer, input: []const u8) !void {
987 for (input) |c| {988 for (input) |c| {
988 try switch (c) {989 try switch (c) {
989 '&' => out.writeAll("&amp;"),990 '&' => out.writeAll("&amp;"),
...@@ -1014,7 +1015,6 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {...@@ -1014,7 +1015,6 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
1014 var buf = std.array_list.Managed(u8).init(allocator);1015 var buf = std.array_list.Managed(u8).init(allocator);
1015 defer buf.deinit();1016 defer buf.deinit();
10161017
1017 var out = buf.writer();
1018 var sgr_param_start_index: usize = undefined;1018 var sgr_param_start_index: usize = undefined;
1019 var sgr_num: u8 = undefined;1019 var sgr_num: u8 = undefined;
1020 var sgr_color: u8 = undefined;1020 var sgr_color: u8 = undefined;
...@@ -1037,10 +1037,10 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {...@@ -1037,10 +1037,10 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
1037 .start => switch (c) {1037 .start => switch (c) {
1038 '\x1b' => state = .escape,1038 '\x1b' => state = .escape,
1039 '\n' => {1039 '\n' => {
1040 try out.writeByte(c);1040 try buf.append(c);
1041 last_new_line = buf.items.len;1041 last_new_line = buf.items.len;
1042 },1042 },
1043 else => try out.writeByte(c),1043 else => try buf.append(c),
1044 },1044 },
1045 .escape => switch (c) {1045 .escape => switch (c) {
1046 '[' => state = .lbracket,1046 '[' => state = .lbracket,
...@@ -1101,16 +1101,16 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {...@@ -1101,16 +1101,16 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
1101 'm' => {1101 'm' => {
1102 state = .start;1102 state = .start;
1103 while (open_span_count != 0) : (open_span_count -= 1) {1103 while (open_span_count != 0) : (open_span_count -= 1) {
1104 try out.writeAll("</span>");1104 try buf.appendSlice("</span>");
1105 }1105 }
1106 if (sgr_num == 0) {1106 if (sgr_num == 0) {
1107 if (sgr_color != 0) return error.UnsupportedColor;1107 if (sgr_color != 0) return error.UnsupportedColor;
1108 continue;1108 continue;
1109 }1109 }
1110 if (sgr_color != 0) {1110 if (sgr_color != 0) {
1111 try out.print("<span class=\"sgr-{d}_{d}m\">", .{ sgr_color, sgr_num });1111 try buf.print("<span class=\"sgr-{d}_{d}m\">", .{ sgr_color, sgr_num });
1112 } else {1112 } else {
1113 try out.print("<span class=\"sgr-{d}m\">", .{sgr_num});1113 try buf.print("<span class=\"sgr-{d}m\">", .{sgr_num});
1114 }1114 }
1115 open_span_count += 1;1115 open_span_count += 1;
1116 },1116 },
...@@ -1156,7 +1156,7 @@ fn run(...@@ -1156,7 +1156,7 @@ fn run(
1156 return result;1156 return result;
1157}1157}
11581158
1159fn printShell(out: anytype, shell_content: []const u8, escape: bool) !void {1159fn printShell(out: *Writer, shell_content: []const u8, escape: bool) !void {
1160 const trimmed_shell_content = mem.trim(u8, shell_content, " \r\n");1160 const trimmed_shell_content = mem.trim(u8, shell_content, " \r\n");
1161 try out.writeAll("<figure><figcaption class=\"shell-cap\">Shell</figcaption><pre><samp>");1161 try out.writeAll("<figure><figcaption class=\"shell-cap\">Shell</figcaption><pre><samp>");
1162 var cmd_cont: bool = false;1162 var cmd_cont: bool = false;
...@@ -1401,11 +1401,11 @@ test "printShell" {...@@ -1401,11 +1401,11 @@ test "printShell" {
1401 \\</samp></pre></figure>1401 \\</samp></pre></figure>
1402 ;1402 ;
14031403
1404 var buffer = std.array_list.Managed(u8).init(test_allocator);1404 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1405 defer buffer.deinit();1405 defer buffer.deinit();
14061406
1407 try printShell(buffer.writer(), shell_out, false);1407 try printShell(&buffer.writer, shell_out, false);
1408 try testing.expectEqualSlices(u8, expected, buffer.items);1408 try testing.expectEqualSlices(u8, expected, buffer.written());
1409 }1409 }
1410 {1410 {
1411 const shell_out =1411 const shell_out =
...@@ -1418,11 +1418,11 @@ test "printShell" {...@@ -1418,11 +1418,11 @@ test "printShell" {
1418 \\</samp></pre></figure>1418 \\</samp></pre></figure>
1419 ;1419 ;
14201420
1421 var buffer = std.array_list.Managed(u8).init(test_allocator);1421 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1422 defer buffer.deinit();1422 defer buffer.deinit();
14231423
1424 try printShell(buffer.writer(), shell_out, false);1424 try printShell(&buffer.writer, shell_out, false);
1425 try testing.expectEqualSlices(u8, expected, buffer.items);1425 try testing.expectEqualSlices(u8, expected, buffer.written());
1426 }1426 }
1427 {1427 {
1428 const shell_out = "$ zig build test.zig\r\nbuild output\r\n";1428 const shell_out = "$ zig build test.zig\r\nbuild output\r\n";
...@@ -1432,11 +1432,11 @@ test "printShell" {...@@ -1432,11 +1432,11 @@ test "printShell" {
1432 \\</samp></pre></figure>1432 \\</samp></pre></figure>
1433 ;1433 ;
14341434
1435 var buffer = std.array_list.Managed(u8).init(test_allocator);1435 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1436 defer buffer.deinit();1436 defer buffer.deinit();
14371437
1438 try printShell(buffer.writer(), shell_out, false);1438 try printShell(&buffer.writer, shell_out, false);
1439 try testing.expectEqualSlices(u8, expected, buffer.items);1439 try testing.expectEqualSlices(u8, expected, buffer.written());
1440 }1440 }
1441 {1441 {
1442 const shell_out =1442 const shell_out =
...@@ -1451,11 +1451,11 @@ test "printShell" {...@@ -1451,11 +1451,11 @@ test "printShell" {
1451 \\</samp></pre></figure>1451 \\</samp></pre></figure>
1452 ;1452 ;
14531453
1454 var buffer = std.array_list.Managed(u8).init(test_allocator);1454 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1455 defer buffer.deinit();1455 defer buffer.deinit();
14561456
1457 try printShell(buffer.writer(), shell_out, false);1457 try printShell(&buffer.writer, shell_out, false);
1458 try testing.expectEqualSlices(u8, expected, buffer.items);1458 try testing.expectEqualSlices(u8, expected, buffer.written());
1459 }1459 }
1460 {1460 {
1461 const shell_out =1461 const shell_out =
...@@ -1472,11 +1472,11 @@ test "printShell" {...@@ -1472,11 +1472,11 @@ test "printShell" {
1472 \\</samp></pre></figure>1472 \\</samp></pre></figure>
1473 ;1473 ;
14741474
1475 var buffer = std.array_list.Managed(u8).init(test_allocator);1475 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1476 defer buffer.deinit();1476 defer buffer.deinit();
14771477
1478 try printShell(buffer.writer(), shell_out, false);1478 try printShell(&buffer.writer, shell_out, false);
1479 try testing.expectEqualSlices(u8, expected, buffer.items);1479 try testing.expectEqualSlices(u8, expected, buffer.written());
1480 }1480 }
1481 {1481 {
1482 const shell_out =1482 const shell_out =
...@@ -1491,11 +1491,11 @@ test "printShell" {...@@ -1491,11 +1491,11 @@ test "printShell" {
1491 \\</samp></pre></figure>1491 \\</samp></pre></figure>
1492 ;1492 ;
14931493
1494 var buffer = std.array_list.Managed(u8).init(test_allocator);1494 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1495 defer buffer.deinit();1495 defer buffer.deinit();
14961496
1497 try printShell(buffer.writer(), shell_out, false);1497 try printShell(&buffer.writer, shell_out, false);
1498 try testing.expectEqualSlices(u8, expected, buffer.items);1498 try testing.expectEqualSlices(u8, expected, buffer.written());
1499 }1499 }
1500 {1500 {
1501 const shell_out =1501 const shell_out =
...@@ -1514,11 +1514,11 @@ test "printShell" {...@@ -1514,11 +1514,11 @@ test "printShell" {
1514 \\</samp></pre></figure>1514 \\</samp></pre></figure>
1515 ;1515 ;
15161516
1517 var buffer = std.array_list.Managed(u8).init(test_allocator);1517 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1518 defer buffer.deinit();1518 defer buffer.deinit();
15191519
1520 try printShell(buffer.writer(), shell_out, false);1520 try printShell(&buffer.writer, shell_out, false);
1521 try testing.expectEqualSlices(u8, expected, buffer.items);1521 try testing.expectEqualSlices(u8, expected, buffer.written());
1522 }1522 }
1523 {1523 {
1524 // intentional space after "--build-option1 \"1524 // intentional space after "--build-option1 \"
...@@ -1536,11 +1536,11 @@ test "printShell" {...@@ -1536,11 +1536,11 @@ test "printShell" {
1536 \\</samp></pre></figure>1536 \\</samp></pre></figure>
1537 ;1537 ;
15381538
1539 var buffer = std.array_list.Managed(u8).init(test_allocator);1539 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1540 defer buffer.deinit();1540 defer buffer.deinit();
15411541
1542 try printShell(buffer.writer(), shell_out, false);1542 try printShell(&buffer.writer, shell_out, false);
1543 try testing.expectEqualSlices(u8, expected, buffer.items);1543 try testing.expectEqualSlices(u8, expected, buffer.written());
1544 }1544 }
1545 {1545 {
1546 const shell_out =1546 const shell_out =
...@@ -1553,11 +1553,11 @@ test "printShell" {...@@ -1553,11 +1553,11 @@ test "printShell" {
1553 \\</samp></pre></figure>1553 \\</samp></pre></figure>
1554 ;1554 ;
15551555
1556 var buffer = std.array_list.Managed(u8).init(test_allocator);1556 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1557 defer buffer.deinit();1557 defer buffer.deinit();
15581558
1559 try printShell(buffer.writer(), shell_out, false);1559 try printShell(&buffer.writer, shell_out, false);
1560 try testing.expectEqualSlices(u8, expected, buffer.items);1560 try testing.expectEqualSlices(u8, expected, buffer.written());
1561 }1561 }
1562 {1562 {
1563 const shell_out =1563 const shell_out =
...@@ -1572,11 +1572,11 @@ test "printShell" {...@@ -1572,11 +1572,11 @@ test "printShell" {
1572 \\</samp></pre></figure>1572 \\</samp></pre></figure>
1573 ;1573 ;
15741574
1575 var buffer = std.array_list.Managed(u8).init(test_allocator);1575 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1576 defer buffer.deinit();1576 defer buffer.deinit();
15771577
1578 try printShell(buffer.writer(), shell_out, false);1578 try printShell(&buffer.writer, shell_out, false);
1579 try testing.expectEqualSlices(u8, expected, buffer.items);1579 try testing.expectEqualSlices(u8, expected, buffer.written());
1580 }1580 }
1581 {1581 {
1582 const shell_out =1582 const shell_out =
...@@ -1587,10 +1587,10 @@ test "printShell" {...@@ -1587,10 +1587,10 @@ test "printShell" {
1587 \\</samp></pre></figure>1587 \\</samp></pre></figure>
1588 ;1588 ;
15891589
1590 var buffer = std.array_list.Managed(u8).init(test_allocator);1590 var buffer: std.Io.Writer.Allocating = .init(test_allocator);
1591 defer buffer.deinit();1591 defer buffer.deinit();
15921592
1593 try printShell(buffer.writer(), shell_out, false);1593 try printShell(&buffer.writer, shell_out, false);
1594 try testing.expectEqualSlices(u8, expected, buffer.items);1594 try testing.expectEqualSlices(u8, expected, buffer.written());
1595 }1595 }
1596}1596}
tools/gen_outline_atomics.zig+1-1
...@@ -49,7 +49,7 @@ pub fn main() !void {...@@ -49,7 +49,7 @@ pub fn main() !void {
49 @tagName(op), n.toBytes(), @tagName(order),49 @tagName(op), n.toBytes(), @tagName(order),
50 });50 });
51 try writeFunction(arena, w, name, op, n, order);51 try writeFunction(arena, w, name, op, n, order);
52 try footer.writer().print(" @export(&{s}, .{{ .name = \"{s}\", .linkage = common.linkage, .visibility = common.visibility }});\n", .{52 try footer.print(" @export(&{s}, .{{ .name = \"{s}\", .linkage = common.linkage, .visibility = common.visibility }});\n", .{
53 name, name,53 name, name,
54 });54 });
55 }55 }
tools/gen_spirv_spec.zig+8-11
...@@ -82,13 +82,11 @@ pub fn main() !void {...@@ -82,13 +82,11 @@ pub fn main() !void {
8282
83 try readExtRegistry(&exts, std.fs.cwd(), args[2]);83 try readExtRegistry(&exts, std.fs.cwd(), args[2]);
8484
85 const output_buf = try allocator.alloc(u8, 1024 * 1024);85 var allocating: std.Io.Writer.Allocating = .init(allocator);
86 var fbs = std.io.fixedBufferStream(output_buf);86 defer allocating.deinit();
87 var adapter = fbs.writer().adaptToNewApi(&.{});87 try render(&allocating.writer, core_spec, exts.items);
88 const w = &adapter.new_interface;88 try allocating.writer.writeByte(0);
89 try render(w, core_spec, exts.items);89 const output = allocating.written()[0 .. allocating.written().len - 1 :0];
90 var output: [:0]u8 = @ptrCast(fbs.getWritten());
91 output[output.len] = 0;
9290
93 var tree = try std.zig.Ast.parse(allocator, output, .zig);91 var tree = try std.zig.Ast.parse(allocator, output, .zig);
94 var color: std.zig.Color = .on;92 var color: std.zig.Color = .on;
...@@ -429,10 +427,9 @@ fn renderClass(writer: *std.io.Writer, instructions: []const Instruction) !void...@@ -429,10 +427,9 @@ fn renderClass(writer: *std.io.Writer, instructions: []const Instruction) !void
429const Formatter = struct {427const Formatter = struct {
430 data: []const u8,428 data: []const u8,
431429
432 fn format(f: Formatter, writer: *std.io.Writer) std.io.Writer.Error!void {430 fn format(f: Formatter, writer: *std.Io.Writer) std.io.Writer.Error!void {
433 var id_buf: [128]u8 = undefined;431 var id_buf: [128]u8 = undefined;
434 var fbs = std.io.fixedBufferStream(&id_buf);432 var fw: std.Io.Writer = .fixed(&id_buf);
435 const fw = fbs.writer();
436 for (f.data, 0..) |c, i| {433 for (f.data, 0..) |c, i| {
437 switch (c) {434 switch (c) {
438 '-', '_', '.', '~', ' ' => fw.writeByte('_') catch return error.WriteFailed,435 '-', '_', '.', '~', ' ' => fw.writeByte('_') catch return error.WriteFailed,
...@@ -452,7 +449,7 @@ const Formatter = struct {...@@ -452,7 +449,7 @@ const Formatter = struct {
452 }449 }
453450
454 // make sure that this won't clobber with zig keywords451 // make sure that this won't clobber with zig keywords
455 try writer.print("{f}", .{std.zig.fmtId(fbs.getWritten())});452 try writer.print("{f}", .{std.zig.fmtId(fw.buffered())});
456 }453 }
457};454};
458455
tools/migrate_langref.zig+22-18
...@@ -382,46 +382,50 @@ fn walk(arena: Allocator, tokenizer: *Tokenizer, out_dir: std.fs.Dir, w: anytype...@@ -382,46 +382,50 @@ fn walk(arena: Allocator, tokenizer: *Tokenizer, out_dir: std.fs.Dir, w: anytype
382 fatal("unable to create file '{s}': {s}", .{ name, @errorName(err) });382 fatal("unable to create file '{s}': {s}", .{ name, @errorName(err) });
383 };383 };
384 defer file.close();384 defer file.close();
385 var file_buffer: [1024]u8 = undefined;
386 var file_writer = file.writer(&file_buffer);
387 const code = &file_writer.interface;
385388
386 const source = tokenizer.buffer[source_token.start..source_token.end];389 const source = tokenizer.buffer[source_token.start..source_token.end];
387 try file.writeAll(std.mem.trim(u8, source[1..], " \t\r\n"));390 try code.writeAll(std.mem.trim(u8, source[1..], " \t\r\n"));
388 try file.writeAll("\n\n");391 try code.writeAll("\n\n");
389392
390 if (just_check_syntax) {393 if (just_check_syntax) {
391 try file.deprecatedWriter().print("// syntax\n", .{});394 try code.print("// syntax\n", .{});
392 } else switch (code_kind_id) {395 } else switch (code_kind_id) {
393 .@"test" => try file.deprecatedWriter().print("// test\n", .{}),396 .@"test" => try code.print("// test\n", .{}),
394 .lib => try file.deprecatedWriter().print("// lib\n", .{}),397 .lib => try code.print("// lib\n", .{}),
395 .test_error => |s| try file.deprecatedWriter().print("// test_error={s}\n", .{s}),398 .test_error => |s| try code.print("// test_error={s}\n", .{s}),
396 .test_safety => |s| try file.deprecatedWriter().print("// test_safety={s}\n", .{s}),399 .test_safety => |s| try code.print("// test_safety={s}\n", .{s}),
397 .exe => |s| try file.deprecatedWriter().print("// exe={s}\n", .{@tagName(s)}),400 .exe => |s| try code.print("// exe={s}\n", .{@tagName(s)}),
398 .obj => |opt| if (opt) |s| {401 .obj => |opt| if (opt) |s| {
399 try file.deprecatedWriter().print("// obj={s}\n", .{s});402 try code.print("// obj={s}\n", .{s});
400 } else {403 } else {
401 try file.deprecatedWriter().print("// obj\n", .{});404 try code.print("// obj\n", .{});
402 },405 },
403 }406 }
404407
405 if (mode != .Debug)408 if (mode != .Debug)
406 try file.deprecatedWriter().print("// optimize={s}\n", .{@tagName(mode)});409 try code.print("// optimize={s}\n", .{@tagName(mode)});
407410
408 for (link_objects.items) |link_object| {411 for (link_objects.items) |link_object| {
409 try file.deprecatedWriter().print("// link_object={s}\n", .{link_object});412 try code.print("// link_object={s}\n", .{link_object});
410 }413 }
411414
412 if (target_str) |s|415 if (target_str) |s|
413 try file.deprecatedWriter().print("// target={s}\n", .{s});416 try code.print("// target={s}\n", .{s});
414417
415 if (link_libc) try file.deprecatedWriter().print("// link_libc\n", .{});418 if (link_libc) try code.print("// link_libc\n", .{});
416 if (disable_cache) try file.deprecatedWriter().print("// disable_cache\n", .{});419 if (disable_cache) try code.print("// disable_cache\n", .{});
417 if (verbose_cimport) try file.deprecatedWriter().print("// verbose_cimport\n", .{});420 if (verbose_cimport) try code.print("// verbose_cimport\n", .{});
418421
419 if (link_mode) |m|422 if (link_mode) |m|
420 try file.deprecatedWriter().print("// link_mode={s}\n", .{@tagName(m)});423 try code.print("// link_mode={s}\n", .{@tagName(m)});
421424
422 for (additional_options.items) |o| {425 for (additional_options.items) |o| {
423 try file.deprecatedWriter().print("// additional_option={s}\n", .{o});426 try code.print("// additional_option={s}\n", .{o});
424 }427 }
428 try code.flush();
425 try w.print("{{#code|{s}#}}\n", .{basename});429 try w.print("{{#code|{s}#}}\n", .{basename});
426 } else {430 } else {
427 const close_bracket = while (true) {431 const close_bracket = while (true) {
tools/update_crc_catalog.zig+2-3
...@@ -88,10 +88,9 @@ pub fn main() anyerror!void {...@@ -88,10 +88,9 @@ pub fn main() anyerror!void {
88 \\88 \\
89 );89 );
9090
91 var stream = std.io.fixedBufferStream(catalog_txt);91 var reader: std.Io.Reader = .fixed(catalog_txt);
92 const reader = stream.reader();
9392
94 while (try reader.readUntilDelimiterOrEofAlloc(arena, '\n', std.math.maxInt(usize))) |line| {93 while (try reader.takeDelimiter('\n')) |line| {
95 if (line.len == 0 or line[0] == '#')94 if (line.len == 0 or line[0] == '#')
96 continue;95 continue;
9796