authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-08 22:37:27+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-08 22:37:27+03:00
log453df1cc1e362005e5f302d151447dc062cce02f
treeed5b0d7e697cab98fac8389a30ec395d80d3d320
parentf2d326607505f1f16d4964a824444aaa0da2c259
parent336ddb5b7656367d074ab7d8a6899321a041452c
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4892 from Sobeston/patch-4

mem.zeroes - add sentinel terminated array support

1 files changed, 7 insertions(+), 0 deletions(-)

lib/std/mem.zig+7
...@@ -382,6 +382,9 @@ pub fn zeroes(comptime T: type) T {...@@ -382,6 +382,9 @@ pub fn zeroes(comptime T: type) T {
382 }382 }
383 },383 },
384 .Array => |info| {384 .Array => |info| {
385 if (info.sentinel) |sentinel| {
386 return [_:sentinel]info.child{zeroes(info.child)} ** info.len;
387 }
385 return [_]info.child{zeroes(info.child)} ** info.len;388 return [_]info.child{zeroes(info.child)} ** info.len;
386 },389 },
387 .Vector,390 .Vector,
...@@ -442,6 +445,7 @@ test "mem.zeroes" {...@@ -442,6 +445,7 @@ test "mem.zeroes" {
442 array: [2]u32,445 array: [2]u32,
443 optional_int: ?u8,446 optional_int: ?u8,
444 empty: void,447 empty: void,
448 sentinel: [3:0]u8,
445 };449 };
446450
447 const b = zeroes(ZigStruct);451 const b = zeroes(ZigStruct);
...@@ -466,6 +470,9 @@ test "mem.zeroes" {...@@ -466,6 +470,9 @@ test "mem.zeroes" {
466 testing.expectEqual(@as(u32, 0), e);470 testing.expectEqual(@as(u32, 0), e);
467 }471 }
468 testing.expectEqual(@as(?u8, null), b.optional_int);472 testing.expectEqual(@as(?u8, null), b.optional_int);
473 for (b.sentinel) |e| {
474 testing.expectEqual(@as(u8, 0), e);
475 }
469}476}
470477
471pub fn secureZero(comptime T: type, s: []T) void {478pub fn secureZero(comptime T: type, s: []T) void {