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" {...@@ -698,24 +698,25 @@ test "spanZ" {
698 testing.expectEqual(@as(?[:0]u16, null), spanZ(@as(?[*:0]u16, null)));698 testing.expectEqual(@as(?[:0]u16, null), spanZ(@as(?[*:0]u16, null)));
699}699}
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,
702/// or a slice, and returns the length.702/// or a slice, and returns the length.
703/// In the case of a sentinel-terminated array, it uses the array length.703/// In the case of a sentinel-terminated array, it uses the array length.
704/// For C pointers it assumes it is a pointer-to-many with a 0 sentinel.704/// For C pointers it assumes it is a pointer-to-many with a 0 sentinel.
705pub fn len(ptr: var) usize {705pub fn len(value: var) usize {
706 return switch (@typeInfo(@TypeOf(ptr))) {706 return switch (@typeInfo(@TypeOf(value))) {
707 .Array => |info| info.len,707 .Array => |info| info.len,
708 .Vector => |info| info.len,
708 .Pointer => |info| switch (info.size) {709 .Pointer => |info| switch (info.size) {
709 .One => switch (@typeInfo(info.child)) {710 .One => switch (@typeInfo(info.child)) {
710 .Array => ptr.len,711 .Array => value.len,
711 else => @compileError("invalid type given to std.mem.len"),712 else => @compileError("invalid type given to std.mem.len"),
712 },713 },
713 .Many => if (info.sentinel) |sentinel|714 .Many => if (info.sentinel) |sentinel|
714 indexOfSentinel(info.child, sentinel, ptr)715 indexOfSentinel(info.child, sentinel, value)
715 else716 else
716 @compileError("length of pointer with no sentinel"),717 @compileError("length of pointer with no sentinel"),
717 .C => indexOfSentinel(info.child, 0, ptr),718 .C => indexOfSentinel(info.child, 0, value),
718 .Slice => ptr.len,719 .Slice => value.len,
719 },720 },
720 else => @compileError("invalid type given to std.mem.len"),721 else => @compileError("invalid type given to std.mem.len"),
721 };722 };
...@@ -738,6 +739,10 @@ test "len" {...@@ -738,6 +739,10 @@ test "len" {
738 array[2] = 0;739 array[2] = 0;
739 testing.expect(len(&array) == 5);740 testing.expect(len(&array) == 5);
740 }741 }
742 {
743 const vector: meta.Vector(2, u32) = [2]u32{ 1, 2 };
744 testing.expect(len(vector) == 2);
745 }
741}746}
742747
743/// Takes a pointer to an array, an array, a sentinel-terminated pointer,748/// Takes a pointer to an array, an array, a sentinel-terminated pointer,