authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-01 13:07:31-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-01 13:07:31-05:00
log4505857e30233d87f9ece403458c0988e8c68493
tree9ec07c9f1d50a20ef4c829e6435f73d35021588a
parent0b0de22fd1b0391f0cf1c7d821afc6522041c699
signature Commit is signed but in an unrecognized format.

revert changes outside std.fmt


3 files changed, 6 insertions(+), 19 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(u8, "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/mem.zig+4-17
...@@ -470,31 +470,18 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {...@@ -470,31 +470,18 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
470 return true;470 return true;
471}471}
472472
473pub fn len(comptime T: type, ptr: var) usize {473pub fn len(comptime T: type, ptr: [*:0]const T) usize {
474 const sentinel: T = comptime meta.Sentinel(@TypeOf(ptr));
475 var count: usize = 0;474 var count: usize = 0;
476 while (ptr[count] != sentinel) : (count += 1) {}475 while (ptr[count] != 0) : (count += 1) {}
477 return count;476 return count;
478}477}
479478
480/// Given a sentintel-terminated pointer-to-many, find the sentintel and return a slice.
481pub fn pointerToSlice(comptime T: type, ptr: blk: {
482 var info = @typeInfo(T).Pointer;
483 info.size = .Many;
484 break :blk @Type(std.builtin.TypeInfo{ .Pointer = info });
485}) T {
486 const sentinel = comptime meta.Sentinel(T);
487 return ptr[0..len(meta.Child(T), ptr) :sentinel];
488}
489
490/// Deprecated; use pointerToSlice instead
491pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {479pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {
492 return pointerToSlice([:0]const T, ptr);480 return ptr[0..len(T, ptr) :0];
493}481}
494482
495/// Deprecated; use pointerToSlice instead
496pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {483pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {
497 return pointerToSlice([:0]T, ptr);484 return ptr[0..len(T, ptr) :0];
498}485}
499486
500/// Returns true if all elements in a slice are equal to the scalar value provided487/// Returns true if all elements in a slice are equal to the scalar value provided
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 = b.len;338 const len = mem.len(u8, 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;