diff --git a/build.zig b/build.zig index 32694afb0ace..7de4f50702c0 100644 --- a/build.zig +++ b/build.zig @@ -6,7 +6,6 @@ const BufMap = std.BufMap; const warn = std.debug.warn; const mem = std.mem; const ArrayList = std.ArrayList; -const Buffer = std.Buffer; const io = std.io; const fs = std.fs; const InstallDirectoryOptions = std.build.InstallDirectoryOptions; diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index d1946be02aef..ca006d79e35b 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -248,6 +248,21 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { if (self.len == 0) return null; return self.pop(); } + + pub fn eql(self: Self, m: []const T) bool { + return mem.eql(T, self.toSliceConst(), m); + } + + pub fn startsWith(self: Self, m: []const T) bool { + if (self.len < m.len) return false; + return mem.eql(T, self.items[0..m.len], m); + } + + pub fn endsWith(self: Self, m: []const T) bool { + if (self.len < m.len) return false; + const start = self.len - m.len; + return mem.eql(T, self.items[start..self.len], m); + } }; } diff --git a/lib/std/buffer.zig b/lib/std/buffer.zig index f808af14852d..bcefe5bdfb9f 100644 --- a/lib/std/buffer.zig +++ b/lib/std/buffer.zig @@ -133,7 +133,7 @@ pub const Buffer = struct { pub fn startsWith(self: Buffer, m: []const u8) bool { if (self.len() < m.len) return false; - return mem.eql(u8, self.list.items[0..m.len], m); + return self.list.startsWith(m); } pub fn endsWith(self: Buffer, m: []const u8) bool { diff --git a/lib/std/build.zig b/lib/std/build.zig index 3ca59ae4cb8d..4bd14fe50caa 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -926,11 +926,9 @@ pub const Builder = struct { try child.spawn(); - var stdout = std.Buffer.initNull(self.allocator); - defer std.Buffer.deinit(&stdout); - var stdout_file_in_stream = child.stdout.?.inStream(); - try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size); + const stdout = try stdout_file_in_stream.stream.readAllAlloc(self.allocator, max_output_size); + errdefer self.allocator.free(stdout); const term = try child.wait(); switch (term) { @@ -939,7 +937,7 @@ pub const Builder = struct { out_code.* = @truncate(u8, code); return error.ExitCodeFailure; } - return stdout.toOwnedSlice(); + return stdout; }, .Signal, .Stopped, .Unknown => |code| { out_code.* = @truncate(u8, code); diff --git a/lib/std/build/run.zig b/lib/std/build/run.zig index eacf408ba90f..da86b15d78a7 100644 --- a/lib/std/build/run.zig +++ b/lib/std/build/run.zig @@ -9,7 +9,6 @@ const mem = std.mem; const process = std.process; const ArrayList = std.ArrayList; const BufMap = std.BufMap; -const Buffer = std.Buffer; const warn = std.debug.warn; const max_stdout_size = 1 * 1024 * 1024; // 1 MiB @@ -169,26 +168,33 @@ pub const RunStep = struct { return err; }; - var stdout = Buffer.initNull(self.builder.allocator); - var stderr = Buffer.initNull(self.builder.allocator); - // TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O). + var stdout: []const u8 = undefined; switch (self.stdout_action) { .expect_exact, .expect_matches => { var stdout_file_in_stream = child.stdout.?.inStream(); - stdout_file_in_stream.stream.readAllBuffer(&stdout, max_stdout_size) catch unreachable; + stdout = stdout_file_in_stream.stream.readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable; }, .inherit, .ignore => {}, } + defer switch (self.stdout_action) { + .expect_exact, .expect_matches => self.builder.allocator.free(stdout), + .inherit, .ignore => {}, + }; - switch (self.stdout_action) { + var stderr: []const u8 = undefined; + switch (self.stderr_action) { .expect_exact, .expect_matches => { var stderr_file_in_stream = child.stderr.?.inStream(); - stderr_file_in_stream.stream.readAllBuffer(&stderr, max_stdout_size) catch unreachable; + stderr = stderr_file_in_stream.stream.readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable; }, .inherit, .ignore => {}, } + defer switch (self.stderr_action) { + .expect_exact, .expect_matches => self.builder.allocator.free(stderr), + .inherit, .ignore => {}, + }; const term = child.wait() catch |err| { warn("Unable to spawn {}: {}\n", .{ argv[0], @errorName(err) }); @@ -216,7 +222,7 @@ pub const RunStep = struct { switch (self.stderr_action) { .inherit, .ignore => {}, .expect_exact => |expected_bytes| { - if (!mem.eql(u8, expected_bytes, stderr.toSliceConst())) { + if (!mem.eql(u8, expected_bytes, stderr)) { warn( \\ \\========= Expected this stderr: ========= @@ -224,13 +230,13 @@ pub const RunStep = struct { \\========= But found: ==================== \\{} \\ - , .{ expected_bytes, stderr.toSliceConst() }); + , .{ expected_bytes, stderr }); printCmd(cwd, argv); return error.TestFailed; } }, .expect_matches => |matches| for (matches) |match| { - if (mem.indexOf(u8, stderr.toSliceConst(), match) == null) { + if (mem.indexOf(u8, stderr, match) == null) { warn( \\ \\========= Expected to find in stderr: ========= @@ -238,7 +244,7 @@ pub const RunStep = struct { \\========= But stderr does not contain it: ===== \\{} \\ - , .{ match, stderr.toSliceConst() }); + , .{ match, stderr }); printCmd(cwd, argv); return error.TestFailed; } @@ -248,7 +254,7 @@ pub const RunStep = struct { switch (self.stdout_action) { .inherit, .ignore => {}, .expect_exact => |expected_bytes| { - if (!mem.eql(u8, expected_bytes, stdout.toSliceConst())) { + if (!mem.eql(u8, expected_bytes, stdout)) { warn( \\ \\========= Expected this stdout: ========= @@ -256,13 +262,13 @@ pub const RunStep = struct { \\========= But found: ==================== \\{} \\ - , .{ expected_bytes, stdout.toSliceConst() }); + , .{ expected_bytes, stdout }); printCmd(cwd, argv); return error.TestFailed; } }, .expect_matches => |matches| for (matches) |match| { - if (mem.indexOf(u8, stdout.toSliceConst(), match) == null) { + if (mem.indexOf(u8, stdout, match) == null) { warn( \\ \\========= Expected to find in stdout: ========= @@ -270,7 +276,7 @@ pub const RunStep = struct { \\========= But stdout does not contain it: ===== \\{} \\ - , .{ match, stdout.toSliceConst() }); + , .{ match, stdout }); printCmd(cwd, argv); return error.TestFailed; } diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index d5e914b28632..4f5fc2b49694 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -217,21 +217,19 @@ pub const ChildProcess = struct { try child.spawn(); - var stdout = Buffer.initNull(args.allocator); - var stderr = Buffer.initNull(args.allocator); - defer Buffer.deinit(&stdout); - defer Buffer.deinit(&stderr); - var stdout_file_in_stream = child.stdout.?.inStream(); var stderr_file_in_stream = child.stderr.?.inStream(); - try stdout_file_in_stream.stream.readAllBuffer(&stdout, args.max_output_bytes); - try stderr_file_in_stream.stream.readAllBuffer(&stderr, args.max_output_bytes); + // TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O). + const stdout = try stdout_file_in_stream.stream.readAllAlloc(args.allocator, args.max_output_bytes); + errdefer args.allocator.free(stdout); + const stderr = try stderr_file_in_stream.stream.readAllAlloc(args.allocator, args.max_output_bytes); + errdefer args.allocator.free(stderr); return ExecResult{ .term = try child.wait(), - .stdout = stdout.toOwnedSlice(), - .stderr = stderr.toOwnedSlice(), + .stdout = stdout, + .stderr = stderr, }; } diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index d0931016461e..c07e5b71a783 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1633,10 +1633,10 @@ test "hexToBytes" { test "formatIntValue with comptime_int" { const value: comptime_int = 123456789123456789; - var buf = try std.Buffer.init(std.testing.allocator, ""); + var buf = std.ArrayList(u8).init(std.testing.allocator); defer buf.deinit(); - try formatIntValue(value, "", FormatOptions{}, &buf, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append); - std.testing.expect(mem.eql(u8, buf.toSlice(), "123456789123456789")); + try formatIntValue(value, "", FormatOptions{}, &buf, @TypeOf(std.ArrayList(u8).appendSlice).ReturnType.ErrorSet, std.ArrayList(u8).appendSlice); + std.testing.expect(mem.eql(u8, buf.toSliceConst(), "123456789123456789")); } test "formatType max_depth" { @@ -1688,24 +1688,24 @@ test "formatType max_depth" { inst.a = &inst; inst.tu.ptr = &inst.tu; - var buf0 = try std.Buffer.init(std.testing.allocator, ""); + var buf0 = std.ArrayList(u8).init(std.testing.allocator); defer buf0.deinit(); - try formatType(inst, "", FormatOptions{}, &buf0, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 0); + try formatType(inst, "", FormatOptions{}, &buf0, @TypeOf(std.ArrayList(u8).appendSlice).ReturnType.ErrorSet, std.ArrayList(u8).appendSlice, 0); std.testing.expect(mem.eql(u8, buf0.toSlice(), "S{ ... }")); - var buf1 = try std.Buffer.init(std.testing.allocator, ""); + var buf1 = std.ArrayList(u8).init(std.testing.allocator); defer buf1.deinit(); - try formatType(inst, "", FormatOptions{}, &buf1, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 1); + try formatType(inst, "", FormatOptions{}, &buf1, @TypeOf(std.ArrayList(u8).appendSlice).ReturnType.ErrorSet, std.ArrayList(u8).appendSlice, 1); std.testing.expect(mem.eql(u8, buf1.toSlice(), "S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }")); - var buf2 = try std.Buffer.init(std.testing.allocator, ""); + var buf2 = std.ArrayList(u8).init(std.testing.allocator); defer buf2.deinit(); - try formatType(inst, "", FormatOptions{}, &buf2, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 2); + try formatType(inst, "", FormatOptions{}, &buf2, @TypeOf(std.ArrayList(u8).appendSlice).ReturnType.ErrorSet, std.ArrayList(u8).appendSlice, 2); std.testing.expect(mem.eql(u8, buf2.toSlice(), "S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }")); - var buf3 = try std.Buffer.init(std.testing.allocator, ""); + var buf3 = std.ArrayList(u8).init(std.testing.allocator); defer buf3.deinit(); - try formatType(inst, "", FormatOptions{}, &buf3, @TypeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append, 3); + try formatType(inst, "", FormatOptions{}, &buf3, @TypeOf(std.ArrayList(u8).appendSlice).ReturnType.ErrorSet, std.ArrayList(u8).appendSlice, 3); std.testing.expect(mem.eql(u8, buf3.toSlice(), "S{ .a = S{ .a = S{ .a = S{ ... }, .tu = TU{ ... }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ ... } }, .e = E.Two, .vec = (10.200,2.220) }, .tu = TU{ .ptr = TU{ .ptr = TU{ ... } } }, .e = E.Two, .vec = (10.200,2.220) }")); } diff --git a/lib/std/io/in_stream.zig b/lib/std/io/in_stream.zig index 4f3bac3c21a7..809e388ebd8a 100644 --- a/lib/std/io/in_stream.zig +++ b/lib/std/io/in_stream.zig @@ -65,22 +65,31 @@ pub fn InStream(comptime ReadError: type) type { /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and /// the contents read from the stream are lost. pub fn readAllBuffer(self: *Self, buffer: *Buffer, max_size: usize) !void { - try buffer.resize(0); + try buffer.list.ensureCapacity(1); + buffer.list.len = 0; + errdefer buffer.resize(0) catch unreachable; // make sure we leave buffer in a valid state on error + try self.readAllArrayList(&buffer.list, max_size); + try buffer.list.append(0); + } + /// Appends to the ArrayList contents by reading from the stream until end of stream is found. + /// If the ArrayList length would exceed `max_size`, `error.StreamTooLong` is returned and the contents + /// read from the stream so far are lost. + pub fn readAllArrayList(self: *Self, array_list: *std.ArrayList(u8), max_size: usize) !void { var actual_buf_len: usize = 0; while (true) { - const dest_slice = buffer.toSlice()[actual_buf_len..]; + const dest_slice = array_list.toSlice()[actual_buf_len..]; const bytes_read = try self.readFull(dest_slice); actual_buf_len += bytes_read; if (bytes_read != dest_slice.len) { - buffer.shrink(actual_buf_len); + array_list.shrink(actual_buf_len); return; } const new_buf_size = math.min(max_size, actual_buf_len + mem.page_size); if (new_buf_size == actual_buf_len) return error.StreamTooLong; - try buffer.resize(new_buf_size); + try array_list.resize(new_buf_size); } } @@ -89,11 +98,10 @@ pub fn InStream(comptime ReadError: type) type { /// Caller owns returned memory. /// If this function returns an error, the contents from the stream read so far are lost. pub fn readAllAlloc(self: *Self, allocator: *mem.Allocator, max_size: usize) ![]u8 { - var buf = Buffer.initNull(allocator); - defer buf.deinit(); - - try self.readAllBuffer(&buf, max_size); - return buf.toOwnedSlice(); + var array_list = std.ArrayList(u8).init(allocator); + defer array_list.deinit(); + try self.readAllArrayList(&array_list, max_size); + return array_list.toOwnedSlice(); } /// Replaces `buffer` contents by reading from the stream until `delimiter` is found. @@ -101,8 +109,18 @@ pub fn InStream(comptime ReadError: type) type { /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents /// read from the stream so far are lost. pub fn readUntilDelimiterBuffer(self: *Self, buffer: *Buffer, delimiter: u8, max_size: usize) !void { - try buffer.resize(0); + try buffer.list.ensureCapacity(1); + buffer.list.len = 0; + errdefer buffer.resize(0) catch unreachable; // make sure we leave buffer in a valid state on error + try self.readUntilDelimiterArrayList(&buffer.list, delimiter, max_size); + try buffer.list.append(0); + } + /// Appends to the ArrayList contents by reading from the stream until `delimiter` is found. + /// Does not include the delimiter in the result. + /// If the ArrayList length would exceed `max_size`, `error.StreamTooLong` is returned and the contents + /// read from the stream so far are lost. + pub fn readUntilDelimiterArrayList(self: *Self, array_list: *std.ArrayList(u8), delimiter: u8, max_size: usize) !void { while (true) { var byte: u8 = try self.readByte(); @@ -110,11 +128,11 @@ pub fn InStream(comptime ReadError: type) type { return; } - if (buffer.len() == max_size) { + if (array_list.len == max_size) { return error.StreamTooLong; } - try buffer.appendByte(byte); + try array_list.append(byte); } } @@ -123,11 +141,10 @@ pub fn InStream(comptime ReadError: type) type { /// Caller owns returned memory. /// If this function returns an error, the contents from the stream read so far are lost. pub fn readUntilDelimiterAlloc(self: *Self, allocator: *mem.Allocator, delimiter: u8, max_size: usize) ![]u8 { - var buf = Buffer.initNull(allocator); - defer buf.deinit(); - - try self.readUntilDelimiterBuffer(&buf, delimiter, max_size); - return buf.toOwnedSlice(); + var array_list = std.ArrayList(u8).init(allocator); + defer array_list.deinit(); + try self.readUntilDelimiterArrayList(&array_list, delimiter, max_size); + return array_list.toOwnedSlice(); } /// Reads from the stream until specified byte is found. If the buffer is not diff --git a/lib/std/process.zig b/lib/std/process.zig index 01b9947518d5..084aec4c49c5 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -3,7 +3,6 @@ const builtin = std.builtin; const os = std.os; const fs = std.fs; const BufMap = std.BufMap; -const Buffer = std.Buffer; const mem = std.mem; const math = std.math; const Allocator = mem.Allocator; @@ -266,7 +265,7 @@ pub const ArgIteratorWindows = struct { } fn internalNext(self: *ArgIteratorWindows, allocator: *Allocator) NextError![]u8 { - var buf = try Buffer.initSize(allocator, 0); + var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); var backslash_count: usize = 0; @@ -282,10 +281,10 @@ pub const ArgIteratorWindows = struct { if (quote_is_real) { self.seen_quote_count += 1; if (self.seen_quote_count == self.quote_count and self.seen_quote_count % 2 == 1) { - try buf.appendByte('"'); + try buf.append('"'); } } else { - try buf.appendByte('"'); + try buf.append('"'); } }, '\\' => { @@ -295,7 +294,7 @@ pub const ArgIteratorWindows = struct { try self.emitBackslashes(&buf, backslash_count); backslash_count = 0; if (self.seen_quote_count % 2 == 1 and self.seen_quote_count != self.quote_count) { - try buf.appendByte(byte); + try buf.append(byte); } else { return buf.toOwnedSlice(); } @@ -303,16 +302,16 @@ pub const ArgIteratorWindows = struct { else => { try self.emitBackslashes(&buf, backslash_count); backslash_count = 0; - try buf.appendByte(byte); + try buf.append(byte); }, } } } - fn emitBackslashes(self: *ArgIteratorWindows, buf: *Buffer, emit_count: usize) !void { + fn emitBackslashes(self: *ArgIteratorWindows, buf: *std.ArrayList(u8), emit_count: usize) !void { var i: usize = 0; while (i < emit_count) : (i += 1) { - try buf.appendByte('\\'); + try buf.append('\\'); } } @@ -410,7 +409,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![][]u8 { // TODO refactor to only make 1 allocation. var it = args(); - var contents = try Buffer.initSize(allocator, 0); + var contents = std.ArrayList(u8).init(allocator); defer contents.deinit(); var slice_list = std.ArrayList(usize).init(allocator); @@ -419,7 +418,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![][]u8 { while (it.next(allocator)) |arg_or_err| { const arg = try arg_or_err; defer allocator.free(arg); - try contents.append(arg); + try contents.appendSlice(arg); try slice_list.append(arg.len); } diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 264a896f4668..6377604b1efd 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -9,7 +9,6 @@ const mem = std.mem; const process = std.process; const Allocator = mem.Allocator; const ArrayList = std.ArrayList; -const Buffer = std.Buffer; const c = @import("c.zig"); const introspect = @import("introspect.zig"); diff --git a/test/tests.zig b/test/tests.zig index 9cf4e7bd9879..e3249025792c 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -566,14 +566,13 @@ pub const StackTracesContext = struct { } child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) }); - var stdout = Buffer.initNull(b.allocator); - var stderr = Buffer.initNull(b.allocator); - var stdout_file_in_stream = child.stdout.?.inStream(); var stderr_file_in_stream = child.stderr.?.inStream(); - stdout_file_in_stream.stream.readAllBuffer(&stdout, max_stdout_size) catch unreachable; - stderr_file_in_stream.stream.readAllBuffer(&stderr, max_stdout_size) catch unreachable; + const stdout = stdout_file_in_stream.stream.readAllAlloc(b.allocator, max_stdout_size) catch unreachable; + defer b.allocator.free(stdout); + const stderr = stderr_file_in_stream.stream.readAllAlloc(b.allocator, max_stdout_size) catch unreachable; + defer b.allocator.free(stderr); const term = child.wait() catch |err| { debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) }); @@ -616,11 +615,8 @@ pub const StackTracesContext = struct { const got: []const u8 = got_result: { var buf = try Buffer.initSize(b.allocator, 0); defer buf.deinit(); - const bytes = if (stderr.endsWith("\n")) - stderr.toSliceConst()[0 .. stderr.len() - 1] - else - stderr.toSliceConst()[0..stderr.len()]; - var it = mem.separate(bytes, "\n"); + if (stderr.len != 0 and stderr[stderr.len - 1] == '\n') stderr = stderr[0 .. stderr.len - 1]; + var it = mem.separate(stderr, "\n"); process_lines: while (it.next()) |line| { if (line.len == 0) continue; const delims = [_][]const u8{ ":", ":", ":", " in " };