authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 19:30:09-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 19:30:09-04:00
log28a6c136e9dc9bcf3e04ab0aa38edc21918c78b9
tree8bf6e99d93360d0b9ed41bd6e134fb906d0564bb
parentdc04e97098010f590d109e6e70d4afe79cd8f01b
signaturelock-open Commit is signed but in an unrecognized format.

revert std.mem.span to prefer len over sentinel; add spanZ


1 files changed, 84 insertions(+), 15 deletions(-)

lib/std/mem.zig+84-15
......@@ -496,14 +496,14 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
496496 return true;
497497}
498498
499/// Deprecated. Use `span`.
499/// Deprecated. Use `spanZ`.
500500pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {
501 return ptr[0..len(ptr) :0];
501 return ptr[0..lenZ(ptr) :0];
502502}
503503
504/// Deprecated. Use `span`.
504/// Deprecated. Use `spanZ`.
505505pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {
506 return ptr[0..len(ptr) :0];
506 return ptr[0..lenZ(ptr) :0];
507507}
508508
509509/// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and
......@@ -548,6 +548,9 @@ test "Span" {
548548/// returns a slice. If there is a sentinel on the input type, there will be a
549549/// sentinel on the output type. The constness of the output type matches
550550/// the constness of the input type.
551///
552/// When there is both a sentinel and an array length or slice length, the
553/// length value is used instead of the sentinel.
551554pub fn span(ptr: var) Span(@TypeOf(ptr)) {
552555 const Result = Span(@TypeOf(ptr));
553556 const l = len(ptr);
......@@ -565,11 +568,74 @@ test "span" {
565568 testing.expect(eql(u16, span(&array), &[_]u16{ 1, 2, 3, 4, 5 }));
566569}
567570
571/// Same as `span`, except when there is both a sentinel and an array
572/// length or slice length, scans the memory for the sentinel value
573/// rather than using the length.
574pub fn spanZ(ptr: var) Span(@TypeOf(ptr)) {
575 const Result = Span(@TypeOf(ptr));
576 const l = lenZ(ptr);
577 if (@typeInfo(Result).Pointer.sentinel) |s| {
578 return ptr[0..l :s];
579 } else {
580 return ptr[0..l];
581 }
582}
583
584test "spanZ" {
585 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
586 const ptr = @as([*:3]u16, array[0..2 :3]);
587 testing.expect(eql(u16, spanZ(ptr), &[_]u16{ 1, 2 }));
588 testing.expect(eql(u16, spanZ(&array), &[_]u16{ 1, 2, 3, 4, 5 }));
589}
590
591/// Takes a pointer to an array, an array, a sentinel-terminated pointer,
592/// or a slice, and returns the length.
593/// In the case of a sentinel-terminated array, it uses the array length.
594/// For C pointers it assumes it is a pointer-to-many with a 0 sentinel.
595pub fn len(ptr: var) usize {
596 return switch (@typeInfo(@TypeOf(ptr))) {
597 .Array => |info| info.len,
598 .Pointer => |info| switch (info.size) {
599 .One => switch (@typeInfo(info.child)) {
600 .Array => ptr.len,
601 else => @compileError("invalid type given to std.mem.len"),
602 },
603 .Many => if (info.sentinel) |sentinel|
604 indexOfSentinel(info.child, sentinel, ptr)
605 else
606 @compileError("length of pointer with no sentinel"),
607 .C => indexOfSentinel(info.child, 0, ptr),
608 .Slice => ptr.len,
609 },
610 else => @compileError("invalid type given to std.mem.len"),
611 };
612}
613
614test "len" {
615 testing.expect(len("aoeu") == 4);
616
617 {
618 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
619 testing.expect(len(&array) == 5);
620 testing.expect(len(array[0..3]) == 3);
621 array[2] = 0;
622 const ptr = @as([*:0]u16, array[0..2 :0]);
623 testing.expect(len(ptr) == 2);
624 }
625 {
626 var array: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 };
627 testing.expect(len(&array) == 5);
628 array[2] = 0;
629 testing.expect(len(&array) == 5);
630 }
631}
632
568633/// Takes a pointer to an array, an array, a sentinel-terminated pointer,
569634/// or a slice, and returns the length.
570635/// In the case of a sentinel-terminated array, it scans the array
571636/// for a sentinel and uses that for the length, rather than using the array length.
572pub fn len(ptr: var) usize {
637/// For C pointers it assumes it is a pointer-to-many with a 0 sentinel.
638pub fn lenZ(ptr: var) usize {
573639 return switch (@typeInfo(@TypeOf(ptr))) {
574640 .Array => |info| if (info.sentinel) |sentinel|
575641 indexOfSentinel(info.child, sentinel, &ptr)
......@@ -581,35 +647,38 @@ pub fn len(ptr: var) usize {
581647 indexOfSentinel(x.child, sentinel, ptr)
582648 else
583649 ptr.len,
584 else => @compileError("invalid type given to std.mem.length"),
650 else => @compileError("invalid type given to std.mem.lenZ"),
585651 },
586652 .Many => if (info.sentinel) |sentinel|
587653 indexOfSentinel(info.child, sentinel, ptr)
588654 else
589655 @compileError("length of pointer with no sentinel"),
590656 .C => indexOfSentinel(info.child, 0, ptr),
591 .Slice => ptr.len,
657 .Slice => if (info.sentinel) |sentinel|
658 indexOfSentinel(info.child, sentinel, ptr.ptr)
659 else
660 ptr.len,
592661 },
593 else => @compileError("invalid type given to std.mem.length"),
662 else => @compileError("invalid type given to std.mem.lenZ"),
594663 };
595664}
596665
597test "len" {
598 testing.expect(len("aoeu") == 4);
666test "lenZ" {
667 testing.expect(lenZ("aoeu") == 4);
599668
600669 {
601670 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
602 testing.expect(len(&array) == 5);
603 testing.expect(len(array[0..3]) == 3);
671 testing.expect(lenZ(&array) == 5);
672 testing.expect(lenZ(array[0..3]) == 3);
604673 array[2] = 0;
605674 const ptr = @as([*:0]u16, array[0..2 :0]);
606 testing.expect(len(ptr) == 2);
675 testing.expect(lenZ(ptr) == 2);
607676 }
608677 {
609678 var array: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 };
610 testing.expect(len(&array) == 5);
679 testing.expect(lenZ(&array) == 5);
611680 array[2] = 0;
612 testing.expect(len(&array) == 2);
681 testing.expect(lenZ(&array) == 2);
613682 }
614683}
615684