authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-06-01 14:44:38+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-06-01 14:44:38+03:00
log8c5c860b00d214baa5fec3830e94761aa757b0d4
treed4bd84cd65943c42a35e285e5ecdae56beba8137
parentf5eb31a55a80f86029704c2126e0f0d4504a6393
parent49dd2cbd9a651dac051718e6fe8015417061f754
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5443 from data-man/mem_len_vectors

Support vectors in mem.len

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

lib/std/mem.zig+12-7
......@@ -698,24 +698,25 @@ test "spanZ" {
698698 testing.expectEqual(@as(?[:0]u16, null), spanZ(@as(?[*:0]u16, null)));
699699}
700700
701/// Takes a pointer to an array, an array, a sentinel-terminated pointer,
701/// Takes a pointer to an array, an array, a vector, a sentinel-terminated pointer,
702702/// or a slice, and returns the length.
703703/// In the case of a sentinel-terminated array, it uses the array length.
704704/// For C pointers it assumes it is a pointer-to-many with a 0 sentinel.
705pub fn len(ptr: var) usize {
706 return switch (@typeInfo(@TypeOf(ptr))) {
705pub fn len(value: var) usize {
706 return switch (@typeInfo(@TypeOf(value))) {
707707 .Array => |info| info.len,
708 .Vector => |info| info.len,
708709 .Pointer => |info| switch (info.size) {
709710 .One => switch (@typeInfo(info.child)) {
710 .Array => ptr.len,
711 .Array => value.len,
711712 else => @compileError("invalid type given to std.mem.len"),
712713 },
713714 .Many => if (info.sentinel) |sentinel|
714 indexOfSentinel(info.child, sentinel, ptr)
715 indexOfSentinel(info.child, sentinel, value)
715716 else
716717 @compileError("length of pointer with no sentinel"),
717 .C => indexOfSentinel(info.child, 0, ptr),
718 .Slice => ptr.len,
718 .C => indexOfSentinel(info.child, 0, value),
719 .Slice => value.len,
719720 },
720721 else => @compileError("invalid type given to std.mem.len"),
721722 };
......@@ -738,6 +739,10 @@ test "len" {
738739 array[2] = 0;
739740 testing.expect(len(&array) == 5);
740741 }
742 {
743 const vector: meta.Vector(2, u32) = [2]u32{ 1, 2 };
744 testing.expect(len(vector) == 2);
745 }
741746}
742747
743748/// Takes a pointer to an array, an array, a sentinel-terminated pointer,