authorgravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-05-27 00:00:19+05:00
committergravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-05-27 00:00:19+05:00
log49dd2cbd9a651dac051718e6fe8015417061f754
tree0795c6c1558e52e7e1ec51eabeeaf5d3e488e23e
parentdd62f63c04fa21427869abea86bbc5fc090b6562

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,