Skip to content

Commit

Permalink
Merge pull request #7531 from Vexu/orphanage
Browse files Browse the repository at this point in the history
Move ArrayListSentineled to std lib orphanage
  • Loading branch information
Vexu authored Dec 24, 2020
2 parents 0fd68f4 + e79acc2 commit 83646df
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 330 deletions.
38 changes: 38 additions & 0 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
return result;
}

/// The caller owns the returned memory. ArrayList becomes empty.
pub fn toOwnedSliceSentinel(self: *Self, comptime sentinel: T) ![:sentinel]T {
try self.append(sentinel);
const result = self.toOwnedSlice();
return result[0 .. result.len - 1 :sentinel];
}

/// Insert `item` at index `n` by moving `list[n .. list.len]` to make room.
/// This operation is O(N).
pub fn insert(self: *Self, n: usize, item: T) !void {
Expand Down Expand Up @@ -389,6 +396,13 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
return result;
}

/// The caller owns the returned memory. ArrayList becomes empty.
pub fn toOwnedSliceSentinel(self: *Self, allocator: *Allocator, comptime sentinel: T) ![:sentinel]T {
try self.append(allocator, sentinel);
const result = self.toOwnedSlice(allocator);
return result[0 .. result.len - 1 :sentinel];
}

/// Insert `item` at index `n`. Moves `list[n .. list.len]`
/// to make room.
pub fn insert(self: *Self, allocator: *Allocator, n: usize, item: T) !void {
Expand Down Expand Up @@ -1121,3 +1135,27 @@ test "std.ArrayList/ArrayListUnmanaged.addManyAsArray" {
testing.expectEqualSlices(u8, list.items, "aoeuasdf");
}
}

test "std.ArrayList/ArrayListUnmanaged.toOwnedSliceSentinel" {
const a = testing.allocator;
{
var list = ArrayList(u8).init(a);
defer list.deinit();

try list.appendSlice("foobar");

const result = try list.toOwnedSliceSentinel(0);
defer a.free(result);
testing.expectEqualStrings(result, mem.spanZ(result.ptr));
}
{
var list = ArrayListUnmanaged(u8){};
defer list.deinit(a);

try list.appendSlice(a, "foobar");

const result = try list.toOwnedSliceSentinel(a, 0);
defer a.free(result);
testing.expectEqualStrings(result, mem.spanZ(result.ptr));
}
}
229 changes: 0 additions & 229 deletions lib/std/array_list_sentineled.zig

This file was deleted.

24 changes: 11 additions & 13 deletions lib/std/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const windows = os.windows;
const mem = std.mem;
const debug = std.debug;
const BufMap = std.BufMap;
const ArrayListSentineled = std.ArrayListSentineled;
const builtin = @import("builtin");
const Os = builtin.Os;
const TailQueue = std.TailQueue;
Expand Down Expand Up @@ -749,38 +748,37 @@ fn windowsCreateProcess(app_name: [*:0]u16, cmd_line: [*:0]u16, envp_ptr: ?[*]u1

/// Caller must dealloc.
fn windowsCreateCommandLine(allocator: *mem.Allocator, argv: []const []const u8) ![:0]u8 {
var buf = try ArrayListSentineled(u8, 0).initSize(allocator, 0);
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();
const buf_stream = buf.outStream();

for (argv) |arg, arg_i| {
if (arg_i != 0) try buf_stream.writeByte(' ');
if (arg_i != 0) try buf.append(' ');
if (mem.indexOfAny(u8, arg, " \t\n\"") == null) {
try buf_stream.writeAll(arg);
try buf.appendSlice(arg);
continue;
}
try buf_stream.writeByte('"');
try buf.append('"');
var backslash_count: usize = 0;
for (arg) |byte| {
switch (byte) {
'\\' => backslash_count += 1,
'"' => {
try buf_stream.writeByteNTimes('\\', backslash_count * 2 + 1);
try buf_stream.writeByte('"');
try buf.appendNTimes('\\', backslash_count * 2 + 1);
try buf.append('"');
backslash_count = 0;
},
else => {
try buf_stream.writeByteNTimes('\\', backslash_count);
try buf_stream.writeByte(byte);
try buf.appendNTimes('\\', backslash_count);
try buf.append(byte);
backslash_count = 0;
},
}
}
try buf_stream.writeByteNTimes('\\', backslash_count * 2);
try buf_stream.writeByte('"');
try buf.appendNTimes('\\', backslash_count * 2);
try buf.append('"');
}

return buf.toOwnedSlice();
return buf.toOwnedSliceSentinel(0);
}

fn windowsDestroyPipe(rd: ?windows.HANDLE, wr: ?windows.HANDLE) void {
Expand Down
1 change: 1 addition & 0 deletions lib/std/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ test "" {
_ = @import("io/buffered_writer.zig");
_ = @import("io/c_writer.zig");
_ = @import("io/counting_writer.zig");
_ = @import("io/counting_reader.zig");
_ = @import("io/fixed_buffer_stream.zig");
_ = @import("io/reader.zig");
_ = @import("io/writer.zig");
Expand Down
18 changes: 9 additions & 9 deletions lib/std/io/counting_reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ pub fn CountingReader(comptime ReaderType: anytype) type {
return struct {
child_reader: ReaderType,
bytes_read: u64 = 0,

pub const Error = ReaderType.Error;
pub const Reader = io.Reader(*@This(), Error, read);

pub fn read(self: *@This(), buf: []u8) Error!usize {
const amt = try self.child_reader.read(buf);
self.bytes_read += amt;
return amt;
}

pub fn reader(self: *@This()) Reader {
return .{ .context = self };
}
};
}

pub fn countingReader(reader: anytype) CountingReader(@TypeOf(reader)) {
return .{ .child_reader = reader, };
return .{ .child_reader = reader };
}

test "io.CountingReader" {
const bytes = "yay" ** 100;
var fbs = io.fixedBufferStream(bytes);

var counting_stream = countingReader(fbs.reader());
const stream = counting_stream.reader();

//read and discard all bytes
while(stream.readByte()) |_| {} else |err| {
while (stream.readByte()) |_| {} else |err| {
testing.expect(err == error.EndOfStream);
}

testing.expect(counting_stream.bytes_read == bytes.len);
}
}
Loading

0 comments on commit 83646df

Please sign in to comment.