Function len [src]

Takes a sentinel-terminated pointer and iterates over the memory to find the sentinel and determine the length. [*c] pointers are assumed to be non-null and 0-terminated.

Prototype

pub fn len(value: anytype) usize

Example

test len { var array: [5]u16 = [_]u16{ 1, 2, 0, 4, 5 }; const ptr = @as([*:4]u16, array[0..3 :4]); try testing.expect(len(ptr) == 3); const c_ptr = @as([*c]u16, ptr); try testing.expect(len(c_ptr) == 2); }

Source

pub fn len(value: anytype) usize { switch (@typeInfo(@TypeOf(value))) { .pointer => |info| switch (info.size) { .many => { const sentinel = info.sentinel() orelse @compileError("invalid type given to std.mem.len: " ++ @typeName(@TypeOf(value))); return indexOfSentinel(info.child, sentinel, value); }, .c => { assert(value != null); return indexOfSentinel(info.child, 0, value); }, else => @compileError("invalid type given to std.mem.len: " ++ @typeName(@TypeOf(value))), }, else => @compileError("invalid type given to std.mem.len: " ++ @typeName(@TypeOf(value))), } }