From b61a6ec8a6ba7222efd3749a9c5ae30db7e4ef6b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 11 Oct 2017 10:16:13 -0400 Subject: implement command line argument parsing for windows See #302 --- std/array_list.zig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'std/array_list.zig') diff --git a/std/array_list.zig b/std/array_list.zig index e01451792e..e844095c67 100644 --- a/std/array_list.zig +++ b/std/array_list.zig @@ -14,6 +14,7 @@ pub fn ArrayList(comptime T: type) -> type{ len: usize, allocator: &Allocator, + /// Deinitialize with `deinit` or use `toOwnedSlice`. pub fn init(allocator: &Allocator) -> Self { Self { .items = []T{}, @@ -34,6 +35,25 @@ pub fn ArrayList(comptime T: type) -> type{ return l.items[0..l.len]; } + /// ArrayList takes ownership of the passed in slice. The slice must have been + /// allocated with `allocator`. + /// Deinitialize with `deinit` or use `toOwnedSlice`. + pub fn fromOwnedSlice(allocator: &Allocator, slice: []T) -> Self { + return Self { + .items = slice, + .len = slice.len, + .allocator = allocator, + }; + } + + /// The caller owns the returned memory. ArrayList becomes empty. + pub fn toOwnedSlice(self: &Self) -> []T { + const allocator = self.allocator; + const result = allocator.shrink(T, self.items, self.len); + *self = init(allocator); + return result; + } + pub fn append(l: &Self, item: &const T) -> %void { const new_item_ptr = %return l.addOne(); *new_item_ptr = *item; -- cgit v1.2.3