revert changes outside std.fmt

This commit is contained in:
Andrew Kelley 2020-03-01 13:07:31 -05:00
parent 0b0de22fd1
commit 4505857e30
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
3 changed files with 6 additions and 19 deletions

View File

@ -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(u8, "123456789") == 9);
}
/// Returns a mutable, null-terminated slice with the same length as `slice`.

View File

@ -470,31 +470,18 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
return true;
}
pub fn len(comptime T: type, ptr: var) usize {
const sentinel: T = comptime meta.Sentinel(@TypeOf(ptr));
pub fn len(comptime T: type, ptr: [*:0]const T) usize {
var count: usize = 0;
while (ptr[count] != sentinel) : (count += 1) {}
while (ptr[count] != 0) : (count += 1) {}
return count;
}
/// Given a sentintel-terminated pointer-to-many, find the sentintel and return a slice.
pub fn pointerToSlice(comptime T: type, ptr: blk: {
var info = @typeInfo(T).Pointer;
info.size = .Many;
break :blk @Type(std.builtin.TypeInfo{ .Pointer = info });
}) T {
const sentinel = comptime meta.Sentinel(T);
return ptr[0..len(meta.Child(T), ptr) :sentinel];
}
/// Deprecated; use pointerToSlice instead
pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {
return pointerToSlice([:0]const T, ptr);
return ptr[0..len(T, ptr) :0];
}
/// Deprecated; use pointerToSlice instead
pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {
return pointerToSlice([:0]T, ptr);
return ptr[0..len(T, ptr) :0];
}
/// Returns true if all elements in a slice are equal to the scalar value provided

View File

@ -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 = b.len;
const len = mem.len(u8, b);
const len_with_null = len + 1;
{
var i: u32 = 0;