diff --git a/lib/std/cstr.zig b/lib/std/cstr.zig index 4057d4b62b..9cb16b0ed3 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("123456789") == 9); } /// Returns a mutable, null-terminated slice with the same length as `slice`. diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 97f2133d67..3729b7dc87 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -442,13 +442,11 @@ pub fn formatType( }, .Many, .C => { if (ptr_info.sentinel) |sentinel| { - const slice = mem.pointerToSlice([:sentinel]const ptr_info.child, value); - return formatType(slice, fmt, options, context, Errors, output, max_depth); + return formatType(mem.span(value), fmt, options, context, Errors, output, max_depth); } if (ptr_info.child == u8) { if (fmt.len > 0 and fmt[0] == 's') { - const slice = mem.pointerToSlice([:0]const u8, @as([*:0]const u8, value)); - return formatText(slice, fmt, options, context, Errors, output); + return formatText(mem.span(value), fmt, options, context, Errors, output); } } return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 775e999e0b..a6c4d0057e 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -482,27 +482,21 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { return true; } -/// Deprecated. Use `length` or `indexOfSentinel`. -pub fn len(comptime T: type, ptr: [*:0]const T) usize { - var count: usize = 0; - while (ptr[count] != 0) : (count += 1) {} - return count; -} - /// Deprecated. Use `span`. pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T { - return ptr[0..len(T, ptr) :0]; + return ptr[0..len(ptr) :0]; } /// Deprecated. Use `span`. pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T { - return ptr[0..len(T, ptr) :0]; + return ptr[0..len(ptr) :0]; } /// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and /// returns a slice. If there is a sentinel on the input type, there will be a /// sentinel on the output type. The constness of the output type matches -/// the constness of the input type. `[*c]` pointers are assumed to be 0-terminated. +/// the constness of the input type. `[*c]` pointers are assumed to be 0-terminated, +/// and assumed to not allow null. pub fn Span(comptime T: type) type { var ptr_info = @typeInfo(T).Pointer; switch (ptr_info.size) { @@ -515,6 +509,7 @@ pub fn Span(comptime T: type) type { }, .C => { ptr_info.sentinel = 0; + ptr_info.is_allowzero = false; }, .Many, .Slice => {}, } @@ -541,7 +536,7 @@ test "Span" { /// the constness of the input type. pub fn span(ptr: var) Span(@TypeOf(ptr)) { const Result = Span(@TypeOf(ptr)); - const l = length(ptr); + const l = len(ptr); if (@typeInfo(Result).Pointer.sentinel) |s| { return ptr[0..l :s]; } else { @@ -558,7 +553,7 @@ test "span" { /// Takes a pointer to an array, an array, a sentinel-terminated pointer, /// or a slice, and returns the length. -pub fn length(ptr: var) usize { +pub fn len(ptr: var) usize { return switch (@typeInfo(@TypeOf(ptr))) { .Array => |info| info.len, .Pointer => |info| switch (info.size) { @@ -577,16 +572,16 @@ pub fn length(ptr: var) usize { }; } -test "length" { - testing.expect(length("aoeu") == 4); +test "len" { + testing.expect(len("aoeu") == 4); { var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 }; - testing.expect(length(&array) == 5); - testing.expect(length(array[0..3]) == 3); + testing.expect(len(&array) == 5); + testing.expect(len(array[0..3]) == 3); array[2] = 0; const ptr = array[0..2 :0].ptr; - testing.expect(length(ptr) == 2); + testing.expect(len(ptr) == 2); } } @@ -1867,8 +1862,13 @@ fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type { return *[length]meta.Child(meta.Child(T)); } -///Given a pointer to an array, returns a pointer to a portion of that array, preserving constness. -pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) SubArrayPtrReturnType(@TypeOf(ptr), length) { +/// Given a pointer to an array, returns a pointer to a portion of that array, preserving constness. +/// TODO this will be obsoleted by https://github.com/ziglang/zig/issues/863 +pub fn subArrayPtr( + ptr: var, + comptime start: usize, + comptime length: usize, +) SubArrayPtrReturnType(@TypeOf(ptr), length) { assert(start + length <= ptr.*.len); const ReturnType = SubArrayPtrReturnType(@TypeOf(ptr), length); diff --git a/lib/std/net.zig b/lib/std/net.zig index b54803cd39..6d0daefdc0 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -352,7 +352,7 @@ pub const Address = extern union { unreachable; } - const path_len = std.mem.len(u8, @ptrCast([*:0]const u8, &self.un.path)); + const path_len = std.mem.len(@ptrCast([*:0]const u8, &self.un.path)); return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len); }, else => unreachable, diff --git a/lib/std/os.zig b/lib/std/os.zig index 24d78bec9a..127ada8fe5 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1095,7 +1095,7 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std. pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void { for (envp_buf) |env| { - const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break; + const env_buf = if (env) |ptr| ptr[0 .. mem.len(ptr) + 1] else break; allocator.free(env_buf); } allocator.free(envp_buf); diff --git a/lib/std/special/c.zig b/lib/std/special/c.zig index 1f11fabca0..e0c2636df1 100644 --- a/lib/std/special/c.zig +++ b/lib/std/special/c.zig @@ -47,7 +47,7 @@ fn strcmp(s1: [*:0]const u8, s2: [*:0]const u8) callconv(.C) c_int { } fn strlen(s: [*:0]const u8) callconv(.C) usize { - return std.mem.len(u8, s); + return std.mem.len(s); } fn strncmp(_l: [*:0]const u8, _r: [*:0]const u8, _n: usize) callconv(.C) c_int { diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index b33c758c38..8efc0e3746 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -4849,7 +4849,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void { } const begin_c = ZigClangSourceManager_getCharacterData(c.source_manager, begin_loc); - const slice = begin_c[0..mem.len(u8, begin_c)]; + const slice = begin_c[0..mem.len(begin_c)]; tok_list.shrink(0); var tokenizer = std.c.Tokenizer{ diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index 681f5be500..44a8334b5d 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 = mem.len(u8, b); + const len = mem.len(b); const len_with_null = len + 1; { var i: u32 = 0;