From 4505857e30233d87f9ece403458c0988e8c68493 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 1 Mar 2020 13:07:31 -0500 Subject: [PATCH] revert changes outside std.fmt --- lib/std/cstr.zig | 2 +- lib/std/mem.zig | 21 ++++----------------- test/stage1/behavior/misc.zig | 2 +- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/lib/std/cstr.zig b/lib/std/cstr.zig index 6b2407cb7d..4057d4b62b 100644 --- a/lib/std/cstr.zig +++ b/lib/std/cstr.zig @@ -28,7 +28,7 @@ test "cstr fns" { fn testCStrFnsImpl() void { testing.expect(cmp("aoeu", "aoez") == -1); - testing.expect(mem.len(u8, "123456789".*) == 9); + testing.expect(mem.len(u8, "123456789") == 9); } /// Returns a mutable, null-terminated slice with the same length as `slice`. diff --git a/lib/std/mem.zig b/lib/std/mem.zig index e6ef7d6752..a4b48bbc1c 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -470,31 +470,18 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { return true; } -pub fn len(comptime T: type, ptr: var) usize { - const sentinel: T = comptime meta.Sentinel(@TypeOf(ptr)); +pub fn len(comptime T: type, ptr: [*:0]const T) usize { var count: usize = 0; - while (ptr[count] != sentinel) : (count += 1) {} + while (ptr[count] != 0) : (count += 1) {} return count; } -/// Given a sentintel-terminated pointer-to-many, find the sentintel and return a slice. -pub fn pointerToSlice(comptime T: type, ptr: blk: { - var info = @typeInfo(T).Pointer; - info.size = .Many; - break :blk @Type(std.builtin.TypeInfo{ .Pointer = info }); -}) T { - const sentinel = comptime meta.Sentinel(T); - return ptr[0..len(meta.Child(T), ptr) :sentinel]; -} - -/// Deprecated; use pointerToSlice instead pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T { - return pointerToSlice([:0]const T, ptr); + return ptr[0..len(T, ptr) :0]; } -/// Deprecated; use pointerToSlice instead pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T { - return pointerToSlice([:0]T, ptr); + return ptr[0..len(T, ptr) :0]; } /// Returns true if all elements in a slice are equal to the scalar value provided diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index e2513c3f4a..681f5be500 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -335,7 +335,7 @@ test "string concatenation" { comptime expect(@TypeOf(a) == *const [12:0]u8); comptime expect(@TypeOf(b) == *const [12:0]u8); - const len = b.len; + const len = mem.len(u8, b); const len_with_null = len + 1; { var i: u32 = 0;