update more to avoid GenericWriter

This commit is contained in:
Andrew Kelley 2025-08-27 16:05:51 -07:00
parent 4f510dba10
commit f7884961c2
10 changed files with 128 additions and 99 deletions

View File

@ -784,7 +784,7 @@ pub fn peekDelimiterInclusive(r: *Reader, delimiter: u8) DelimiterError![]u8 {
} }
/// Returns a slice of the next bytes of buffered data from the stream until /// Returns a slice of the next bytes of buffered data from the stream until
/// `delimiter` is found, advancing the seek position. /// `delimiter` is found, advancing the seek position up to the delimiter.
/// ///
/// Returned slice excludes the delimiter. End-of-stream is treated equivalent /// Returned slice excludes the delimiter. End-of-stream is treated equivalent
/// to a delimiter, unless it would result in a length 0 return value, in which /// 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 {
return result[0 .. result.len - 1]; return result[0 .. result.len - 1];
} }
/// Returns a slice of the next bytes of buffered data from the stream until
/// `delimiter` is found, advancing the seek position past the delimiter.
///
/// Returned slice excludes the delimiter. End-of-stream is treated equivalent
/// to a delimiter, unless it would result in a length 0 return value, in which
/// case `null` is returned instead.
///
/// If the delimiter is not found within a number of bytes matching the
/// capacity of this `Reader`, `error.StreamTooLong` is returned. In
/// such case, the stream state is unmodified as if this function was never
/// called.
///
/// Invalidates previously returned values from `peek`.
///
/// See also:
/// * `takeDelimiterInclusive`
/// * `takeDelimiterExclusive`
pub fn takeDelimiter(r: *Reader, delimiter: u8) error{ ReadFailed, StreamTooLong }!?[]u8 {
const result = r.peekDelimiterInclusive(delimiter) catch |err| switch (err) {
error.EndOfStream => {
const remaining = r.buffer[r.seek..r.end];
if (remaining.len == 0) return null;
r.toss(remaining.len);
return remaining;
},
else => |e| return e,
};
r.toss(result.len + 1);
return result[0 .. result.len - 1];
}
/// Returns a slice of the next bytes of buffered data from the stream until /// Returns a slice of the next bytes of buffered data from the stream until
/// `delimiter` is found, without advancing the seek position. /// `delimiter` is found, without advancing the seek position.
/// ///

View File

@ -228,13 +228,13 @@ fn setTypePointer(writer: anytype) !void {
fn setSegmentOffset(segment_id: u8, offset: u64, writer: anytype) !void { fn setSegmentOffset(segment_id: u8, offset: u64, writer: anytype) !void {
log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset }); log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset });
try writer.writeByte(macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @as(u4, @truncate(segment_id))); try writer.writeByte(macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @as(u4, @truncate(segment_id)));
try std.leb.writeUleb128(writer, offset); try writer.writeUleb128(offset);
} }
fn rebaseAddAddr(addr: u64, writer: anytype) !void { fn rebaseAddAddr(addr: u64, writer: anytype) !void {
log.debug(">>> rebase with add: {x}", .{addr}); log.debug(">>> rebase with add: {x}", .{addr});
try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB); try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB);
try std.leb.writeUleb128(writer, addr); try writer.writeUleb128(addr);
} }
fn rebaseTimes(count: usize, writer: anytype) !void { fn rebaseTimes(count: usize, writer: anytype) !void {
@ -243,15 +243,15 @@ fn rebaseTimes(count: usize, writer: anytype) !void {
try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | @as(u4, @truncate(count))); try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | @as(u4, @truncate(count)));
} else { } else {
try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES); try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
try std.leb.writeUleb128(writer, count); try writer.writeUleb128(count);
} }
} }
fn rebaseTimesSkip(count: usize, skip: u64, writer: anytype) !void { fn rebaseTimesSkip(count: usize, skip: u64, writer: anytype) !void {
log.debug(">>> rebase with count: {d} and skip: {x}", .{ count, skip }); log.debug(">>> rebase with count: {d} and skip: {x}", .{ count, skip });
try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB); try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);
try std.leb.writeUleb128(writer, count); try writer.writeUleb128(count);
try std.leb.writeUleb128(writer, skip); try writer.writeUleb128(skip);
} }
fn addAddr(addr: u64, writer: anytype) !void { fn addAddr(addr: u64, writer: anytype) !void {
@ -264,7 +264,7 @@ fn addAddr(addr: u64, writer: anytype) !void {
} }
} }
try writer.writeByte(macho.REBASE_OPCODE_ADD_ADDR_ULEB); try writer.writeByte(macho.REBASE_OPCODE_ADD_ADDR_ULEB);
try std.leb.writeUleb128(writer, addr); try writer.writeUleb128(addr);
} }
fn done(writer: anytype) !void { fn done(writer: anytype) !void {
@ -651,7 +651,6 @@ test "rebase - composite" {
const std = @import("std"); const std = @import("std");
const assert = std.debug.assert; const assert = std.debug.assert;
const leb = std.leb;
const log = std.log.scoped(.link_dyld_info); const log = std.log.scoped(.link_dyld_info);
const macho = std.macho; const macho = std.macho;
const mem = std.mem; const mem = std.mem;

View File

@ -262,13 +262,13 @@ fn writeNode(self: *Trie, node_index: Node.Index, writer: *std.Io.Writer) !void
// TODO Implement for special flags. // TODO Implement for special flags.
assert(export_flags & macho.EXPORT_SYMBOL_FLAGS_REEXPORT == 0 and assert(export_flags & macho.EXPORT_SYMBOL_FLAGS_REEXPORT == 0 and
export_flags & macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER == 0); export_flags & macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER == 0);
try leb.writeUleb128(&info_stream, export_flags); try info_stream.writeUleb128(export_flags);
try leb.writeUleb128(&info_stream, vmaddr_offset); try info_stream.writeUleb128(vmaddr_offset);
// Encode the size of the terminal node info. // Encode the size of the terminal node info.
var size_buf: [@sizeOf(u64)]u8 = undefined; var size_buf: [@sizeOf(u64)]u8 = undefined;
var size_stream: std.Io.Writer = .fixed(&size_buf); var size_stream: std.Io.Writer = .fixed(&size_buf);
try leb.writeUleb128(&size_stream, info_stream.end); try size_stream.writeUleb128(info_stream.end);
// Now, write them to the output stream. // Now, write them to the output stream.
try writer.writeAll(size_buf[0..size_stream.end]); 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
// Write edge label and offset to next node in trie. // Write edge label and offset to next node in trie.
try writer.writeAll(edge.label); try writer.writeAll(edge.label);
try writer.writeByte(0); try writer.writeByte(0);
try leb.writeUleb128(writer, slice.items(.trie_offset)[edge.node]); try writer.writeUleb128(slice.items(.trie_offset)[edge.node]);
} }
} }
@ -419,7 +419,6 @@ test "ordering bug" {
} }
const assert = std.debug.assert; const assert = std.debug.assert;
const leb = std.leb;
const log = std.log.scoped(.macho); const log = std.log.scoped(.macho);
const macho = std.macho; const macho = std.macho;
const mem = std.mem; const mem = std.mem;

View File

@ -605,7 +605,7 @@ pub const LazyBind = struct {
fn setSegmentOffset(segment_id: u8, offset: u64, writer: *std.Io.Writer) !void { fn setSegmentOffset(segment_id: u8, offset: u64, writer: *std.Io.Writer) !void {
log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset }); log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset });
try writer.writeByte(macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @as(u4, @truncate(segment_id))); try writer.writeByte(macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @as(u4, @truncate(segment_id)));
try std.leb.writeUleb128(writer, offset); try writer.writeUleb128(offset);
} }
fn setSymbol(name: []const u8, flags: u8, writer: *std.Io.Writer) !void { fn setSymbol(name: []const u8, flags: u8, writer: *std.Io.Writer) !void {
@ -639,7 +639,7 @@ fn setDylibOrdinal(ordinal: i16, writer: *std.Io.Writer) !void {
try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | @as(u4, @truncate(cast))); try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | @as(u4, @truncate(cast)));
} else { } else {
try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB); try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
try std.leb.writeUleb128(writer, cast); try writer.writeUleb128(cast);
} }
} }
} }
@ -667,20 +667,20 @@ fn doBindAddAddr(addr: u64, writer: *std.Io.Writer) !void {
} }
} }
try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB); try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB);
try std.leb.writeUleb128(writer, addr); try writer.writeUleb128(addr);
} }
fn doBindTimesSkip(count: usize, skip: u64, writer: *std.Io.Writer) !void { fn doBindTimesSkip(count: usize, skip: u64, writer: *std.Io.Writer) !void {
log.debug(">>> bind with count: {d} and skip: {x}", .{ count, skip }); log.debug(">>> bind with count: {d} and skip: {x}", .{ count, skip });
try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB); try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB);
try std.leb.writeUleb128(writer, count); try writer.writeUleb128(count);
try std.leb.writeUleb128(writer, skip); try writer.writeUleb128(skip);
} }
fn addAddr(addr: u64, writer: *std.Io.Writer) !void { fn addAddr(addr: u64, writer: *std.Io.Writer) !void {
log.debug(">>> add: {x}", .{addr}); log.debug(">>> add: {x}", .{addr});
try writer.writeByte(macho.BIND_OPCODE_ADD_ADDR_ULEB); try writer.writeByte(macho.BIND_OPCODE_ADD_ADDR_ULEB);
try std.leb.writeUleb128(writer, addr); try writer.writeUleb128(addr);
} }
fn done(writer: *std.Io.Writer) !void { fn done(writer: *std.Io.Writer) !void {
@ -689,7 +689,6 @@ fn done(writer: *std.Io.Writer) !void {
} }
const assert = std.debug.assert; const assert = std.debug.assert;
const leb = std.leb;
const log = std.log.scoped(.link_dyld_info); const log = std.log.scoped(.link_dyld_info);
const macho = std.macho; const macho = std.macho;
const mem = std.mem; const mem = std.mem;

View File

@ -10,7 +10,8 @@ pub fn main() !void {
dir_name, .{}); dir_name, .{});
const file_name = args.next().?; const file_name = args.next().?;
const file = try dir.createFile(file_name, .{}); const file = try dir.createFile(file_name, .{});
try file.deprecatedWriter().print( var file_writer = file.writer(&.{});
try file_writer.interface.print(
\\{s} \\{s}
\\{s} \\{s}
\\Hello, world! \\Hello, world!

View File

@ -7,6 +7,7 @@ const process = std.process;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const testing = std.testing; const testing = std.testing;
const getExternalExecutor = std.zig.system.getExternalExecutor; const getExternalExecutor = std.zig.system.getExternalExecutor;
const Writer = std.Io.Writer;
const max_doc_file_size = 10 * 1024 * 1024; const max_doc_file_size = 10 * 1024 * 1024;
@ -108,7 +109,7 @@ pub fn main() !void {
fn printOutput( fn printOutput(
arena: Allocator, arena: Allocator,
out: anytype, out: *Writer,
code: Code, code: Code,
/// Relative to this process' cwd. /// Relative to this process' cwd.
tmp_dir_path: []const u8, tmp_dir_path: []const u8,
@ -610,7 +611,7 @@ fn dumpArgs(args: []const []const u8) void {
std.debug.print("\n", .{}); std.debug.print("\n", .{});
} }
fn printSourceBlock(arena: Allocator, out: anytype, source_bytes: []const u8, name: []const u8) !void { fn printSourceBlock(arena: Allocator, out: *Writer, source_bytes: []const u8, name: []const u8) !void {
try out.print("<figure><figcaption class=\"{s}-cap\"><cite class=\"file\">{s}</cite></figcaption><pre>", .{ try out.print("<figure><figcaption class=\"{s}-cap\"><cite class=\"file\">{s}</cite></figcaption><pre>", .{
"zig", name, "zig", name,
}); });
@ -618,7 +619,7 @@ fn printSourceBlock(arena: Allocator, out: anytype, source_bytes: []const u8, na
try out.writeAll("</pre></figure>"); try out.writeAll("</pre></figure>");
} }
fn tokenizeAndPrint(arena: Allocator, out: anytype, raw_src: []const u8) !void { fn tokenizeAndPrint(arena: Allocator, out: *Writer, raw_src: []const u8) !void {
const src_non_terminated = mem.trim(u8, raw_src, " \r\n"); const src_non_terminated = mem.trim(u8, raw_src, " \r\n");
const src = try arena.dupeZ(u8, src_non_terminated); const src = try arena.dupeZ(u8, src_non_terminated);
@ -846,7 +847,7 @@ fn tokenizeAndPrint(arena: Allocator, out: anytype, raw_src: []const u8) !void {
try out.writeAll("</code>"); try out.writeAll("</code>");
} }
fn writeEscapedLines(out: anytype, text: []const u8) !void { fn writeEscapedLines(out: *Writer, text: []const u8) !void {
return writeEscaped(out, text); return writeEscaped(out, text);
} }
@ -983,7 +984,7 @@ fn escapeHtml(allocator: Allocator, input: []const u8) ![]u8 {
return try buf.toOwnedSlice(); return try buf.toOwnedSlice();
} }
fn writeEscaped(out: anytype, input: []const u8) !void { fn writeEscaped(out: *Writer, input: []const u8) !void {
for (input) |c| { for (input) |c| {
try switch (c) { try switch (c) {
'&' => out.writeAll("&amp;"), '&' => out.writeAll("&amp;"),
@ -1014,7 +1015,6 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
var buf = std.array_list.Managed(u8).init(allocator); var buf = std.array_list.Managed(u8).init(allocator);
defer buf.deinit(); defer buf.deinit();
var out = buf.writer();
var sgr_param_start_index: usize = undefined; var sgr_param_start_index: usize = undefined;
var sgr_num: u8 = undefined; var sgr_num: u8 = undefined;
var sgr_color: u8 = undefined; var sgr_color: u8 = undefined;
@ -1037,10 +1037,10 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
.start => switch (c) { .start => switch (c) {
'\x1b' => state = .escape, '\x1b' => state = .escape,
'\n' => { '\n' => {
try out.writeByte(c); try buf.append(c);
last_new_line = buf.items.len; last_new_line = buf.items.len;
}, },
else => try out.writeByte(c), else => try buf.append(c),
}, },
.escape => switch (c) { .escape => switch (c) {
'[' => state = .lbracket, '[' => state = .lbracket,
@ -1101,16 +1101,16 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
'm' => { 'm' => {
state = .start; state = .start;
while (open_span_count != 0) : (open_span_count -= 1) { while (open_span_count != 0) : (open_span_count -= 1) {
try out.writeAll("</span>"); try buf.appendSlice("</span>");
} }
if (sgr_num == 0) { if (sgr_num == 0) {
if (sgr_color != 0) return error.UnsupportedColor; if (sgr_color != 0) return error.UnsupportedColor;
continue; continue;
} }
if (sgr_color != 0) { if (sgr_color != 0) {
try out.print("<span class=\"sgr-{d}_{d}m\">", .{ sgr_color, sgr_num }); try buf.print("<span class=\"sgr-{d}_{d}m\">", .{ sgr_color, sgr_num });
} else { } else {
try out.print("<span class=\"sgr-{d}m\">", .{sgr_num}); try buf.print("<span class=\"sgr-{d}m\">", .{sgr_num});
} }
open_span_count += 1; open_span_count += 1;
}, },
@ -1156,7 +1156,7 @@ fn run(
return result; return result;
} }
fn printShell(out: anytype, shell_content: []const u8, escape: bool) !void { fn printShell(out: *Writer, shell_content: []const u8, escape: bool) !void {
const trimmed_shell_content = mem.trim(u8, shell_content, " \r\n"); const trimmed_shell_content = mem.trim(u8, shell_content, " \r\n");
try out.writeAll("<figure><figcaption class=\"shell-cap\">Shell</figcaption><pre><samp>"); try out.writeAll("<figure><figcaption class=\"shell-cap\">Shell</figcaption><pre><samp>");
var cmd_cont: bool = false; var cmd_cont: bool = false;
@ -1401,11 +1401,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = const shell_out =
@ -1418,11 +1418,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = "$ zig build test.zig\r\nbuild output\r\n"; const shell_out = "$ zig build test.zig\r\nbuild output\r\n";
@ -1432,11 +1432,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = const shell_out =
@ -1451,11 +1451,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = const shell_out =
@ -1472,11 +1472,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = const shell_out =
@ -1491,11 +1491,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = const shell_out =
@ -1514,11 +1514,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
// intentional space after "--build-option1 \" // intentional space after "--build-option1 \"
@ -1536,11 +1536,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = const shell_out =
@ -1553,11 +1553,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = const shell_out =
@ -1572,11 +1572,11 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
{ {
const shell_out = const shell_out =
@ -1587,10 +1587,10 @@ test "printShell" {
\\</samp></pre></figure> \\</samp></pre></figure>
; ;
var buffer = std.array_list.Managed(u8).init(test_allocator); var buffer: std.Io.Writer.Allocating = .init(test_allocator);
defer buffer.deinit(); defer buffer.deinit();
try printShell(buffer.writer(), shell_out, false); try printShell(&buffer.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items); try testing.expectEqualSlices(u8, expected, buffer.written());
} }
} }

View File

@ -49,7 +49,7 @@ pub fn main() !void {
@tagName(op), n.toBytes(), @tagName(order), @tagName(op), n.toBytes(), @tagName(order),
}); });
try writeFunction(arena, w, name, op, n, order); try writeFunction(arena, w, name, op, n, order);
try footer.writer().print(" @export(&{s}, .{{ .name = \"{s}\", .linkage = common.linkage, .visibility = common.visibility }});\n", .{ try footer.print(" @export(&{s}, .{{ .name = \"{s}\", .linkage = common.linkage, .visibility = common.visibility }});\n", .{
name, name, name, name,
}); });
} }

View File

@ -82,13 +82,11 @@ pub fn main() !void {
try readExtRegistry(&exts, std.fs.cwd(), args[2]); try readExtRegistry(&exts, std.fs.cwd(), args[2]);
const output_buf = try allocator.alloc(u8, 1024 * 1024); var allocating: std.Io.Writer.Allocating = .init(allocator);
var fbs = std.io.fixedBufferStream(output_buf); defer allocating.deinit();
var adapter = fbs.writer().adaptToNewApi(&.{}); try render(&allocating.writer, core_spec, exts.items);
const w = &adapter.new_interface; try allocating.writer.writeByte(0);
try render(w, core_spec, exts.items); const output = allocating.written()[0 .. allocating.written().len - 1 :0];
var output: [:0]u8 = @ptrCast(fbs.getWritten());
output[output.len] = 0;
var tree = try std.zig.Ast.parse(allocator, output, .zig); var tree = try std.zig.Ast.parse(allocator, output, .zig);
var color: std.zig.Color = .on; var color: std.zig.Color = .on;
@ -429,10 +427,9 @@ fn renderClass(writer: *std.io.Writer, instructions: []const Instruction) !void
const Formatter = struct { const Formatter = struct {
data: []const u8, data: []const u8,
fn format(f: Formatter, writer: *std.io.Writer) std.io.Writer.Error!void { fn format(f: Formatter, writer: *std.Io.Writer) std.io.Writer.Error!void {
var id_buf: [128]u8 = undefined; var id_buf: [128]u8 = undefined;
var fbs = std.io.fixedBufferStream(&id_buf); var fw: std.Io.Writer = .fixed(&id_buf);
const fw = fbs.writer();
for (f.data, 0..) |c, i| { for (f.data, 0..) |c, i| {
switch (c) { switch (c) {
'-', '_', '.', '~', ' ' => fw.writeByte('_') catch return error.WriteFailed, '-', '_', '.', '~', ' ' => fw.writeByte('_') catch return error.WriteFailed,
@ -452,7 +449,7 @@ const Formatter = struct {
} }
// make sure that this won't clobber with zig keywords // make sure that this won't clobber with zig keywords
try writer.print("{f}", .{std.zig.fmtId(fbs.getWritten())}); try writer.print("{f}", .{std.zig.fmtId(fw.buffered())});
} }
}; };

View File

@ -382,46 +382,50 @@ fn walk(arena: Allocator, tokenizer: *Tokenizer, out_dir: std.fs.Dir, w: anytype
fatal("unable to create file '{s}': {s}", .{ name, @errorName(err) }); fatal("unable to create file '{s}': {s}", .{ name, @errorName(err) });
}; };
defer file.close(); defer file.close();
var file_buffer: [1024]u8 = undefined;
var file_writer = file.writer(&file_buffer);
const code = &file_writer.interface;
const source = tokenizer.buffer[source_token.start..source_token.end]; const source = tokenizer.buffer[source_token.start..source_token.end];
try file.writeAll(std.mem.trim(u8, source[1..], " \t\r\n")); try code.writeAll(std.mem.trim(u8, source[1..], " \t\r\n"));
try file.writeAll("\n\n"); try code.writeAll("\n\n");
if (just_check_syntax) { if (just_check_syntax) {
try file.deprecatedWriter().print("// syntax\n", .{}); try code.print("// syntax\n", .{});
} else switch (code_kind_id) { } else switch (code_kind_id) {
.@"test" => try file.deprecatedWriter().print("// test\n", .{}), .@"test" => try code.print("// test\n", .{}),
.lib => try file.deprecatedWriter().print("// lib\n", .{}), .lib => try code.print("// lib\n", .{}),
.test_error => |s| try file.deprecatedWriter().print("// test_error={s}\n", .{s}), .test_error => |s| try code.print("// test_error={s}\n", .{s}),
.test_safety => |s| try file.deprecatedWriter().print("// test_safety={s}\n", .{s}), .test_safety => |s| try code.print("// test_safety={s}\n", .{s}),
.exe => |s| try file.deprecatedWriter().print("// exe={s}\n", .{@tagName(s)}), .exe => |s| try code.print("// exe={s}\n", .{@tagName(s)}),
.obj => |opt| if (opt) |s| { .obj => |opt| if (opt) |s| {
try file.deprecatedWriter().print("// obj={s}\n", .{s}); try code.print("// obj={s}\n", .{s});
} else { } else {
try file.deprecatedWriter().print("// obj\n", .{}); try code.print("// obj\n", .{});
}, },
} }
if (mode != .Debug) if (mode != .Debug)
try file.deprecatedWriter().print("// optimize={s}\n", .{@tagName(mode)}); try code.print("// optimize={s}\n", .{@tagName(mode)});
for (link_objects.items) |link_object| { for (link_objects.items) |link_object| {
try file.deprecatedWriter().print("// link_object={s}\n", .{link_object}); try code.print("// link_object={s}\n", .{link_object});
} }
if (target_str) |s| if (target_str) |s|
try file.deprecatedWriter().print("// target={s}\n", .{s}); try code.print("// target={s}\n", .{s});
if (link_libc) try file.deprecatedWriter().print("// link_libc\n", .{}); if (link_libc) try code.print("// link_libc\n", .{});
if (disable_cache) try file.deprecatedWriter().print("// disable_cache\n", .{}); if (disable_cache) try code.print("// disable_cache\n", .{});
if (verbose_cimport) try file.deprecatedWriter().print("// verbose_cimport\n", .{}); if (verbose_cimport) try code.print("// verbose_cimport\n", .{});
if (link_mode) |m| if (link_mode) |m|
try file.deprecatedWriter().print("// link_mode={s}\n", .{@tagName(m)}); try code.print("// link_mode={s}\n", .{@tagName(m)});
for (additional_options.items) |o| { for (additional_options.items) |o| {
try file.deprecatedWriter().print("// additional_option={s}\n", .{o}); try code.print("// additional_option={s}\n", .{o});
} }
try code.flush();
try w.print("{{#code|{s}#}}\n", .{basename}); try w.print("{{#code|{s}#}}\n", .{basename});
} else { } else {
const close_bracket = while (true) { const close_bracket = while (true) {

View File

@ -88,10 +88,9 @@ pub fn main() anyerror!void {
\\ \\
); );
var stream = std.io.fixedBufferStream(catalog_txt); var reader: std.Io.Reader = .fixed(catalog_txt);
const reader = stream.reader();
while (try reader.readUntilDelimiterOrEofAlloc(arena, '\n', std.math.maxInt(usize))) |line| { while (try reader.takeDelimiter('\n')) |line| {
if (line.len == 0 or line[0] == '#') if (line.len == 0 or line[0] == '#')
continue; continue;