aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-02-20 18:36:04 -0500
committerAndrew Kelley <andrew@ziglang.org>2020-02-20 18:36:04 -0500
commit903127f36cf995d251a474438d3ecc6c6e0e30d2 (patch)
tree10b45461abfb8933b2ac6c8121026490b22d1c6d /lib/std
parent0f016b368de2ac93a298ab771646931329062fb5 (diff)
parente381a42de9c0f0c5439a926b0ac99026a0373f49 (diff)
downloadzig-903127f36cf995d251a474438d3ecc6c6e0e30d2.tar.gz
zig-903127f36cf995d251a474438d3ecc6c6e0e30d2.zip
Merge remote-tracking branch 'origin/master' into sub-architecture-annihilation
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/array_list.zig25
-rw-r--r--lib/std/fs.zig1
-rw-r--r--lib/std/io.zig67
3 files changed, 26 insertions, 67 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index f212ae8659..d1946be02a 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -188,6 +188,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
self.len += items.len;
}
+ /// Append a value to the list `n` times. Allocates more memory
+ /// as necessary.
+ pub fn appendNTimes(self: *Self, value: T, n: usize) !void {
+ const old_len = self.len;
+ try self.resize(self.len + n);
+ mem.set(T, self.items[old_len..self.len], value);
+ }
+
/// Adjust the list's length to `new_len`. Doesn't initialize
/// added items if any.
pub fn resize(self: *Self, new_len: usize) !void {
@@ -311,6 +319,23 @@ test "std.ArrayList.basic" {
testing.expect(list.pop() == 33);
}
+test "std.ArrayList.appendNTimes" {
+ var list = ArrayList(i32).init(testing.allocator);
+ defer list.deinit();
+
+ try list.appendNTimes(2, 10);
+ testing.expectEqual(@as(usize, 10), list.len);
+ for (list.toSlice()) |element| {
+ testing.expectEqual(@as(i32, 2), element);
+ }
+}
+
+test "std.ArrayList.appendNTimes with failing allocator" {
+ var list = ArrayList(i32).init(testing.failing_allocator);
+ defer list.deinit();
+ testing.expectError(error.OutOfMemory, list.appendNTimes(2, 10));
+}
+
test "std.ArrayList.orderedRemove" {
var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
index a00ba6a63f..c3d418eb8c 100644
--- a/lib/std/fs.zig
+++ b/lib/std/fs.zig
@@ -864,6 +864,7 @@ pub const Dir = struct {
.OBJECT_NAME_INVALID => unreachable,
.OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
.OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
+ .NO_MEDIA_IN_DEVICE => return error.NoDevice,
.INVALID_PARAMETER => unreachable,
.SHARING_VIOLATION => return error.SharingViolation,
.ACCESS_DENIED => return error.AccessDenied,
diff --git a/lib/std/io.zig b/lib/std/io.zig
index 22ba0b9bf4..b6e1b12534 100644
--- a/lib/std/io.zig
+++ b/lib/std/io.zig
@@ -790,73 +790,6 @@ pub const BufferedAtomicFile = struct {
}
};
-pub fn readLine(buf: *std.Buffer) ![]u8 {
- var stdin_stream = getStdIn().inStream();
- return readLineFrom(&stdin_stream.stream, buf);
-}
-
-/// Reads all characters until the next newline into buf, and returns
-/// a slice of the characters read (excluding the newline character(s)).
-pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 {
- const start = buf.len();
- while (true) {
- const byte = try stream.readByte();
- switch (byte) {
- '\r' => {
- // trash the following \n
- _ = try stream.readByte();
- return buf.toSlice()[start..];
- },
- '\n' => return buf.toSlice()[start..],
- else => try buf.appendByte(byte),
- }
- }
-}
-
-test "io.readLineFrom" {
- var buf = try std.Buffer.initSize(testing.allocator, 0);
- defer buf.deinit();
- var mem_stream = SliceInStream.init(
- \\Line 1
- \\Line 22
- \\Line 333
- );
- const stream = &mem_stream.stream;
-
- testing.expectEqualSlices(u8, "Line 1", try readLineFrom(stream, &buf));
- testing.expectEqualSlices(u8, "Line 22", try readLineFrom(stream, &buf));
- testing.expectError(error.EndOfStream, readLineFrom(stream, &buf));
- testing.expectEqualSlices(u8, "Line 1Line 22Line 333", buf.toSlice());
-}
-
-pub fn readLineSlice(slice: []u8) ![]u8 {
- var stdin_stream = getStdIn().inStream();
- return readLineSliceFrom(&stdin_stream.stream, slice);
-}
-
-/// Reads all characters until the next newline into slice, and returns
-/// a slice of the characters read (excluding the newline character(s)).
-pub fn readLineSliceFrom(stream: var, slice: []u8) ![]u8 {
- // We cannot use Buffer.fromOwnedSlice, as it wants to append a null byte
- // after taking ownership, which would always require an allocation.
- var buf = std.Buffer{ .list = std.ArrayList(u8).fromOwnedSlice(testing.failing_allocator, slice) };
- try buf.resize(0);
- return try readLineFrom(stream, &buf);
-}
-
-test "io.readLineSliceFrom" {
- var buf: [7]u8 = undefined;
- var mem_stream = SliceInStream.init(
- \\Line 1
- \\Line 22
- \\Line 333
- );
- const stream = &mem_stream.stream;
-
- testing.expectEqualSlices(u8, "Line 1", try readLineSliceFrom(stream, buf[0..]));
- testing.expectError(error.OutOfMemory, readLineSliceFrom(stream, buf[0..]));
-}
-
pub const Packing = enum {
/// Pack data to byte alignment
Byte,