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" {...@@ -28,7 +28,7 @@ test "cstr fns" {
2828
29fn testCStrFnsImpl() void {29fn testCStrFnsImpl() void {
30 testing.expect(cmp("aoeu", "aoez") == -1);30 testing.expect(cmp("aoeu", "aoez") == -1);
31 testing.expect(mem.len(u8, "123456789") == 9);31 testing.expect(mem.len("123456789") == 9);
32}32}
3333
34/// Returns a mutable, null-terminated slice with the same length as `slice`.34/// 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(...@@ -442,13 +442,11 @@ pub fn formatType(
442 },442 },
443 .Many, .C => {443 .Many, .C => {
444 if (ptr_info.sentinel) |sentinel| {444 if (ptr_info.sentinel) |sentinel| {
445 const slice = mem.pointerToSlice([:sentinel]const ptr_info.child, value);445 return formatType(mem.span(value), fmt, options, context, Errors, output, max_depth);
446 return formatType(slice, fmt, options, context, Errors, output, max_depth);
447 }446 }
448 if (ptr_info.child == u8) {447 if (ptr_info.child == u8) {
449 if (fmt.len > 0 and fmt[0] == 's') {448 if (fmt.len > 0 and fmt[0] == 's') {
450 const slice = mem.pointerToSlice([:0]const u8, @as([*:0]const u8, value));449 return formatText(mem.span(value), fmt, options, context, Errors, output);
451 return formatText(slice, fmt, options, context, Errors, output);
452 }450 }
453 }451 }
454 return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });452 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 {...@@ -482,27 +482,21 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
482 return true;482 return true;
483}483}
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
492/// Deprecated. Use `span`.485/// Deprecated. Use `span`.
493pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {486pub 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];
495}488}
496489
497/// Deprecated. Use `span`.490/// Deprecated. Use `span`.
498pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {491pub 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];
500}493}
501494
502/// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and495/// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and
503/// returns a slice. If there is a sentinel on the input type, there will be a496/// returns a slice. If there is a sentinel on the input type, there will be a
504/// sentinel on the output type. The constness of the output type matches497/// 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.
506pub fn Span(comptime T: type) type {500pub fn Span(comptime T: type) type {
507 var ptr_info = @typeInfo(T).Pointer;501 var ptr_info = @typeInfo(T).Pointer;
508 switch (ptr_info.size) {502 switch (ptr_info.size) {
...@@ -515,6 +509,7 @@ pub fn Span(comptime T: type) type {...@@ -515,6 +509,7 @@ pub fn Span(comptime T: type) type {
515 },509 },
516 .C => {510 .C => {
517 ptr_info.sentinel = 0;511 ptr_info.sentinel = 0;
512 ptr_info.is_allowzero = false;
518 },513 },
519 .Many, .Slice => {},514 .Many, .Slice => {},
520 }515 }
...@@ -541,7 +536,7 @@ test "Span" {...@@ -541,7 +536,7 @@ test "Span" {
541/// the constness of the input type.536/// the constness of the input type.
542pub fn span(ptr: var) Span(@TypeOf(ptr)) {537pub fn span(ptr: var) Span(@TypeOf(ptr)) {
543 const Result = Span(@TypeOf(ptr));538 const Result = Span(@TypeOf(ptr));
544 const l = length(ptr);539 const l = len(ptr);
545 if (@typeInfo(Result).Pointer.sentinel) |s| {540 if (@typeInfo(Result).Pointer.sentinel) |s| {
546 return ptr[0..l :s];541 return ptr[0..l :s];
547 } else {542 } else {
...@@ -558,7 +553,7 @@ test "span" {...@@ -558,7 +553,7 @@ test "span" {
558553
559/// Takes a pointer to an array, an array, a sentinel-terminated pointer,554/// Takes a pointer to an array, an array, a sentinel-terminated pointer,
560/// or a slice, and returns the length.555/// or a slice, and returns the length.
561pub fn length(ptr: var) usize {556pub fn len(ptr: var) usize {
562 return switch (@typeInfo(@TypeOf(ptr))) {557 return switch (@typeInfo(@TypeOf(ptr))) {
563 .Array => |info| info.len,558 .Array => |info| info.len,
564 .Pointer => |info| switch (info.size) {559 .Pointer => |info| switch (info.size) {
...@@ -577,16 +572,16 @@ pub fn length(ptr: var) usize {...@@ -577,16 +572,16 @@ pub fn length(ptr: var) usize {
577 };572 };
578}573}
579574
580test "length" {575test "len" {
581 testing.expect(length("aoeu") == 4);576 testing.expect(len("aoeu") == 4);
582577
583 {578 {
584 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };579 var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 };
585 testing.expect(length(&array) == 5);580 testing.expect(len(&array) == 5);
586 testing.expect(length(array[0..3]) == 3);581 testing.expect(len(array[0..3]) == 3);
587 array[2] = 0;582 array[2] = 0;
588 const ptr = array[0..2 :0].ptr;583 const ptr = array[0..2 :0].ptr;
589 testing.expect(length(ptr) == 2);584 testing.expect(len(ptr) == 2);
590 }585 }
591}586}
592587
...@@ -1867,8 +1862,13 @@ fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type {...@@ -1867,8 +1862,13 @@ fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type {
1867 return *[length]meta.Child(meta.Child(T));1862 return *[length]meta.Child(meta.Child(T));
1868}1863}
18691864
1870///Given a pointer to an array, returns a pointer to a portion of that array, preserving constness.1865/// 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) {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) {
1872 assert(start + length <= ptr.*.len);1872 assert(start + length <= ptr.*.len);
18731873
1874 const ReturnType = SubArrayPtrReturnType(@TypeOf(ptr), length);1874 const ReturnType = SubArrayPtrReturnType(@TypeOf(ptr), length);
lib/std/net.zig+1-1
...@@ -352,7 +352,7 @@ pub const Address = extern union {...@@ -352,7 +352,7 @@ pub const Address = extern union {
352 unreachable;352 unreachable;
353 }353 }
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));
356 return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);356 return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);
357 },357 },
358 else => unreachable,358 else => unreachable,
lib/std/os.zig+1-1
...@@ -1095,7 +1095,7 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std....@@ -1095,7 +1095,7 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.
10951095
1096pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void {1096pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void {
1097 for (envp_buf) |env| {1097 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;
1099 allocator.free(env_buf);1099 allocator.free(env_buf);
1100 }1100 }
1101 allocator.free(envp_buf);1101 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 {...@@ -47,7 +47,7 @@ fn strcmp(s1: [*:0]const u8, s2: [*:0]const u8) callconv(.C) c_int {
47}47}
4848
49fn strlen(s: [*:0]const u8) callconv(.C) usize {49fn strlen(s: [*:0]const u8) callconv(.C) usize {
50 return std.mem.len(u8, s);50 return std.mem.len(s);
51}51}
5252
53fn strncmp(_l: [*:0]const u8, _r: [*:0]const u8, _n: usize) callconv(.C) c_int {53fn 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 {...@@ -4849,7 +4849,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {
4849 }4849 }
48504850
4851 const begin_c = ZigClangSourceManager_getCharacterData(c.source_manager, begin_loc);4851 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
4854 tok_list.shrink(0);4854 tok_list.shrink(0);
4855 var tokenizer = std.c.Tokenizer{4855 var tokenizer = std.c.Tokenizer{
test/stage1/behavior/misc.zig+1-1
...@@ -335,7 +335,7 @@ test "string concatenation" {...@@ -335,7 +335,7 @@ test "string concatenation" {
335 comptime expect(@TypeOf(a) == *const [12:0]u8);335 comptime expect(@TypeOf(a) == *const [12:0]u8);
336 comptime expect(@TypeOf(b) == *const [12:0]u8);336 comptime expect(@TypeOf(b) == *const [12:0]u8);
337337
338 const len = mem.len(u8, b);338 const len = mem.len(b);
339 const len_with_null = len + 1;339 const len_with_null = len + 1;
340 {340 {
341 var i: u32 = 0;341 var i: u32 = 0;