diff options
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/array_list.zig | 28 | ||||
| -rw-r--r-- | lib/std/child_process.zig | 3 | ||||
| -rw-r--r-- | lib/std/io.zig | 1 | ||||
| -rw-r--r-- | lib/std/io/counting_reader.zig | 18 | ||||
| -rw-r--r-- | lib/std/json.zig | 14 | ||||
| -rw-r--r-- | lib/std/os/linux/io_uring.zig | 7 |
6 files changed, 47 insertions, 24 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 102dfe9fbc..4a8e0059e9 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -94,7 +94,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { /// 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.list.toOwnedSlice(); + const result = self.toOwnedSlice(); return result[0 .. result.len - 1 :sentinel]; } @@ -399,7 +399,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ /// 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.list.toOwnedSlice(allocator); + const result = self.toOwnedSlice(allocator); return result[0 .. result.len - 1 :sentinel]; } @@ -1135,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)); + } +} diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index 2d7f547a9e..6f75b01c87 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -748,9 +748,8 @@ 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 ArrayList(u8).init(allocator); + var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); - const buf_wi = buf.outStream(); for (argv) |arg, arg_i| { if (arg_i != 0) try buf.append(' '); diff --git a/lib/std/io.zig b/lib/std/io.zig index 103c443dd6..2bff6422cc 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -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"); diff --git a/lib/std/io/counting_reader.zig b/lib/std/io/counting_reader.zig index 09a624952f..1a1c357c85 100644 --- a/lib/std/io/counting_reader.zig +++ b/lib/std/io/counting_reader.zig @@ -12,16 +12,16 @@ 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 }; } @@ -29,20 +29,20 @@ pub fn CountingReader(comptime ReaderType: anytype) type { } 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); -}
\ No newline at end of file +} diff --git a/lib/std/json.zig b/lib/std/json.zig index ac1047d1a9..9e83aae0c4 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1553,7 +1553,7 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options: const source_slice = stringToken.slice(tokens.slice, tokens.i - 1); switch (stringToken.escapes) { .None => mem.copy(u8, &r, source_slice), - .Some => try unescapeString(&r, source_slice), + .Some => try unescapeValidString(&r, source_slice), } return r; }, @@ -1600,7 +1600,7 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options: .Some => |some_escapes| { const output = try allocator.alloc(u8, stringToken.decodedLength()); errdefer allocator.free(output); - try unescapeString(output, source_slice); + try unescapeValidString(output, source_slice); return output; }, } @@ -2084,7 +2084,7 @@ pub const Parser = struct { .Some => |some_escapes| { const output = try allocator.alloc(u8, s.decodedLength()); errdefer allocator.free(output); - try unescapeString(output, slice); + try unescapeValidString(output, slice); return Value{ .String = output }; }, } @@ -2098,10 +2098,10 @@ pub const Parser = struct { } }; -// Unescape a JSON string -// Only to be used on strings already validated by the parser -// (note the unreachable statements and lack of bounds checking) -pub fn unescapeString(output: []u8, input: []const u8) !void { +/// Unescape a JSON string +/// Only to be used on strings already validated by the parser +/// (note the unreachable statements and lack of bounds checking) +pub fn unescapeValidString(output: []u8, input: []const u8) !void { var inIndex: usize = 0; var outIndex: usize = 0; diff --git a/lib/std/os/linux/io_uring.zig b/lib/std/os/linux/io_uring.zig index 0fdc7651d2..a19685d33b 100644 --- a/lib/std/os/linux/io_uring.zig +++ b/lib/std/os/linux/io_uring.zig @@ -1378,11 +1378,10 @@ test "timeout_remove" { // Timeout remove operations set the fd to -1, which results in EBADF before EINVAL. // We use IORING_FEAT_RW_CUR_POS as a safety check here to make sure we are at least pre-5.6. // We don't want to skip this test for newer kernels. - if ( - cqe_timeout.user_data == 0x99999999 and + if (cqe_timeout.user_data == 0x99999999 and cqe_timeout.res == -linux.EBADF and - (ring.features & linux.IORING_FEAT_RW_CUR_POS) == 0 - ) { + (ring.features & linux.IORING_FEAT_RW_CUR_POS) == 0) + { return error.SkipZigTest; } testing.expectEqual(linux.io_uring_cqe{ |
