authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-01 13:21:29-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-01 13:21:39-05:00
logef3d761da545a3a72928ed0e0ba3b749a4cb74d8
treed05e7ddf19d709975f432e60f5a2321309078517
parent5b26128bacddf594dfe45958a236bfa2459f878b
signaturelock-open Commit is signed but in an unrecognized format.

breaking: std.mem.len no longer takes a type argument

also update fmt code to use std.mem.span.

8 files changed, 27 insertions(+), 29 deletions(-)

lib/std/cstr.zig+1-1
......@@ -28,7 +28,7 @@ test "cstr fns" {
2828
2929fn testCStrFnsImpl() void {
3030 testing.expect(cmp("aoeu", "aoez") == -1);
31 testing.expect(mem.len(u8, "123456789") == 9);
31 testing.expect(mem.len("123456789") == 9);
3232}
3333
3434/// Returns a mutable, null-terminated slice with the same length as `slice`.
lib/std/fmt.zig+2-4
......@@ -442,13 +442,11 @@ pub fn formatType(
442442 },
443443 .Many, .C => {
444444 if (ptr_info.sentinel) |sentinel| {
445 const slice = mem.pointerToSlice([:sentinel]const ptr_info.child, value);
446 return formatType(slice, fmt, options, context, Errors, output, max_depth);
445 return formatType(mem.span(value), fmt, options, context, Errors, output, max_depth);
447446 }
448447 if (ptr_info.child == u8) {
449448 if (fmt.len > 0 and fmt[0] == 's') {
450 const slice = mem.pointerToSlice([:0]const u8, @as([*:0]const u8, value));
451 return formatText(slice, fmt, options, context, Errors, output);
449 return formatText(mem.span(value), fmt, options, context, Errors, output);
452450 }
453451 }
454452 return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
lib/std/mem.zig+19-19
......@@ -482,27 +482,21 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
482482 return true;
483483}
484484
485/// Deprecated. Use `length` or `indexOfSentinel`.
486pub fn len(comptime T: type, ptr: [*:0]const T) usize {
487 var count: usize = 0;
488 while (ptr[count] != 0) : (count += 1) {}
489 return count;
490}
491
492485/// Deprecated. Use `span`.
493486pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {
494 return ptr[0..len(T, ptr) :0];
487 return ptr[0..len(ptr) :0];
495488}
496489
497490/// Deprecated. Use `span`.
498491pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {
499 return ptr[0..len(T, ptr) :0];
492 return ptr[0..len(ptr) :0];
500493}
501494
502495/// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and
503496/// returns a slice. If there is a sentinel on the input type, there will be a
504497/// sentinel on the output type. The constness of the output type matches
505/// the constness of the input type. `[*c]` pointers are assumed to be 0-terminated.
498/// the constness of the input type. `[*c]` pointers are assumed to be 0-terminated,
499/// and assumed to not allow null.
506500pub fn Span(comptime T: type) type {
507501 var ptr_info = @typeInfo(T).Pointer;
508502 switch (ptr_info.size) {
......@@ -515,6 +509,7 @@ pub fn Span(comptime T: type) type {
515509 },
516510 .C => {
517511 ptr_info.sentinel = 0;
512 ptr_info.is_allowzero = false;
518513 },
519514 .Many, .Slice => {},
520515 }
......@@ -541,7 +536,7 @@ test "Span" {
541536/// the constness of the input type.
542537pub fn span(ptr: var) Span(@TypeOf(ptr)) {
543538 const Result = Span(@TypeOf(ptr));
544 const l = length(ptr);
539 const l = len(ptr);
545540 if (@typeInfo(Result).Pointer.sentinel) |s| {
546541 return ptr[0..l :s];
547542 } else {
......@@ -558,7 +553,7 @@ test "span" {
558553
559554/// Takes a pointer to an array, an array, a sentinel-terminated pointer,
560555/// or a slice, and returns the length.
561pub fn length(ptr: var) usize {
556pub fn len(ptr: var) usize {
562557 return switch (@typeInfo(@TypeOf(ptr))) {
563558 .Array => |info| info.len,
564559 .Pointer => |info| switch (info.size) {
......@@ -577,16 +572,16 @@ pub fn length(ptr: var) usize {
577572 };
578573}
579574
580test "length" {
581 testing.expect(length("aoeu") == 4);
575test "len" {
576 testing.expect(len("aoeu") == 4);
582577
583578 {
584579 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
585 testing.expect(length(&array) == 5);
586 testing.expect(length(array[0..3]) == 3);
580 testing.expect(len(&array) == 5);
581 testing.expect(len(array[0..3]) == 3);
587582 array[2] = 0;
588583 const ptr = array[0..2 :0].ptr;
589 testing.expect(length(ptr) == 2);
584 testing.expect(len(ptr) == 2);
590585 }
591586}
592587
......@@ -1867,8 +1862,13 @@ fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type {
18671862 return *[length]meta.Child(meta.Child(T));
18681863}
18691864
1870///Given a pointer to an array, returns a pointer to a portion of that array, preserving constness.
1871pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) SubArrayPtrReturnType(@TypeOf(ptr), length) {
1865/// Given a pointer to an array, returns a pointer to a portion of that array, preserving constness.
1866/// TODO this will be obsoleted by https://github.com/ziglang/zig/issues/863
1867pub fn subArrayPtr(
1868 ptr: var,
1869 comptime start: usize,
1870 comptime length: usize,
1871) SubArrayPtrReturnType(@TypeOf(ptr), length) {
18721872 assert(start + length <= ptr.*.len);
18731873
18741874 const ReturnType = SubArrayPtrReturnType(@TypeOf(ptr), length);
lib/std/net.zig+1-1
......@@ -352,7 +352,7 @@ pub const Address = extern union {
352352 unreachable;
353353 }
354354
355 const path_len = std.mem.len(u8, @ptrCast([*:0]const u8, &self.un.path));
355 const path_len = std.mem.len(@ptrCast([*:0]const u8, &self.un.path));
356356 return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);
357357 },
358358 else => unreachable,
lib/std/os.zig+1-1
......@@ -1095,7 +1095,7 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.
10951095
10961096pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void {
10971097 for (envp_buf) |env| {
1098 const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break;
1098 const env_buf = if (env) |ptr| ptr[0 .. mem.len(ptr) + 1] else break;
10991099 allocator.free(env_buf);
11001100 }
11011101 allocator.free(envp_buf);
lib/std/special/c.zig+1-1
......@@ -47,7 +47,7 @@ fn strcmp(s1: [*:0]const u8, s2: [*:0]const u8) callconv(.C) c_int {
4747}
4848
4949fn strlen(s: [*:0]const u8) callconv(.C) usize {
50 return std.mem.len(u8, s);
50 return std.mem.len(s);
5151}
5252
5353fn strncmp(_l: [*:0]const u8, _r: [*:0]const u8, _n: usize) callconv(.C) c_int {
src-self-hosted/translate_c.zig+1-1
......@@ -4849,7 +4849,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {
48494849 }
48504850
48514851 const begin_c = ZigClangSourceManager_getCharacterData(c.source_manager, begin_loc);
4852 const slice = begin_c[0..mem.len(u8, begin_c)];
4852 const slice = begin_c[0..mem.len(begin_c)];
48534853
48544854 tok_list.shrink(0);
48554855 var tokenizer = std.c.Tokenizer{
test/stage1/behavior/misc.zig+1-1
......@@ -335,7 +335,7 @@ test "string concatenation" {
335335 comptime expect(@TypeOf(a) == *const [12:0]u8);
336336 comptime expect(@TypeOf(b) == *const [12:0]u8);
337337
338 const len = mem.len(u8, b);
338 const len = mem.len(b);
339339 const len_with_null = len + 1;
340340 {
341341 var i: u32 = 0;