From 92a423739d91bd0677940fc1a86b593649cafaa6 Mon Sep 17 00:00:00 2001 From: Sebastian <15335529+Sobeston@users.noreply.github.com> Date: Wed, 1 Apr 2020 09:11:05 +0100 Subject: [PATCH 1/2] mem.zeroes - add sentinel terminated array support --- lib/std/mem.zig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index f54eb03d65..cedb1bc27d 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -341,6 +341,9 @@ pub fn zeroes(comptime T: type) T { } }, .Array => |info| { + if (info.sentinel) |sentinel| { + return [_:info.sentinel]info.child{zeroes(info.child)} ** info.len; + } return [_]info.child{zeroes(info.child)} ** info.len; }, .Vector, From 336ddb5b7656367d074ab7d8a6899321a041452c Mon Sep 17 00:00:00 2001 From: Vexu Date: Fri, 8 May 2020 19:02:15 +0300 Subject: [PATCH 2/2] std: add test for mem.zeroes on sentinel terminated arrays --- lib/std/mem.zig | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index cedb1bc27d..63db3d58b2 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -341,8 +341,8 @@ pub fn zeroes(comptime T: type) T { } }, .Array => |info| { - if (info.sentinel) |sentinel| { - return [_:info.sentinel]info.child{zeroes(info.child)} ** info.len; + if (info.sentinel) |sentinel| { + return [_:sentinel]info.child{zeroes(info.child)} ** info.len; } return [_]info.child{zeroes(info.child)} ** info.len; }, @@ -407,6 +407,7 @@ test "mem.zeroes" { array: [2]u32, optional_int: ?u8, empty: void, + sentinel: [3:0]u8, }; const b = zeroes(ZigStruct); @@ -431,6 +432,9 @@ test "mem.zeroes" { testing.expectEqual(@as(u32, 0), e); } testing.expectEqual(@as(?u8, null), b.optional_int); + for (b.sentinel) |e| { + testing.expectEqual(@as(u8, 0), e); + } } pub fn secureZero(comptime T: type, s: []T) void { @@ -504,7 +508,7 @@ pub const toSlice = @compileError("deprecated; use std.mem.spanZ"); /// the constness of the input type. `[*c]` pointers are assumed to be 0-terminated, /// and assumed to not allow null. pub fn Span(comptime T: type) type { - switch(@typeInfo(T)) { + switch (@typeInfo(T)) { .Optional => |optional_info| { return ?Span(optional_info.child); },