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