| ... | @@ -470,31 +470,18 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { | ... | @@ -470,31 +470,18 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { |
| 470 | return true; | 470 | return true; |
| 471 | } | 471 | } |
| 472 | | 472 | |
| 473 | pub fn len(comptime T: type, ptr: var) usize { | 473 | pub fn len(comptime T: type, ptr: [*:0]const T) usize { |
| 474 | const sentinel: T = comptime meta.Sentinel(@TypeOf(ptr)); | | |
| 475 | var count: usize = 0; | 474 | var count: usize = 0; |
| 476 | while (ptr[count] != sentinel) : (count += 1) {} | 475 | while (ptr[count] != 0) : (count += 1) {} |
| 477 | return count; | 476 | return count; |
| 478 | } | 477 | } |
| 479 | | 478 | |
| 480 | /// Given a sentintel-terminated pointer-to-many, find the sentintel and return a slice. | | |
| 481 | pub fn pointerToSlice(comptime T: type, ptr: blk: { | | |
| 482 | var info = @typeInfo(T).Pointer; | | |
| 483 | info.size = .Many; | | |
| 484 | break :blk @Type(std.builtin.TypeInfo{ .Pointer = info }); | | |
| 485 | }) T { | | |
| 486 | const sentinel = comptime meta.Sentinel(T); | | |
| 487 | return ptr[0..len(meta.Child(T), ptr) :sentinel]; | | |
| 488 | } | | |
| 489 | | | |
| 490 | /// Deprecated; use pointerToSlice instead | | |
| 491 | pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T { | 479 | pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T { |
| 492 | return pointerToSlice([:0]const T, ptr); | 480 | return ptr[0..len(T, ptr) :0]; |
| 493 | } | 481 | } |
| 494 | | 482 | |
| 495 | /// Deprecated; use pointerToSlice instead | | |
| 496 | pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T { | 483 | pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T { |
| 497 | return pointerToSlice([:0]T, ptr); | 484 | return ptr[0..len(T, ptr) :0]; |
| 498 | } | 485 | } |
| 499 | | 486 | |
| 500 | /// Returns true if all elements in a slice are equal to the scalar value provided | 487 | /// Returns true if all elements in a slice are equal to the scalar value provided |