| author | |
| committer | |
| log | ef3d761da545a3a72928ed0e0ba3b749a4cb74d8 |
| tree | d05e7ddf19d709975f432e60f5a2321309078517 |
| parent | 5b26128bacddf594dfe45958a236bfa2459f878b |
| signature |
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 | 28 | |
| 29 | 29 | fn testCStrFnsImpl() void { |
| 30 | 30 | testing.expect(cmp("aoeu", "aoez") == -1); |
| 31 | testing.expect(mem.len(u8, "123456789") == 9); | |
| 31 | testing.expect(mem.len("123456789") == 9); | |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 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 | 442 | }, |
| 443 | 443 | .Many, .C => { |
| 444 | 444 | 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); | |
| 447 | 446 | } |
| 448 | 447 | if (ptr_info.child == u8) { |
| 449 | 448 | 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); | |
| 452 | 450 | } |
| 453 | 451 | } |
| 454 | 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 | 482 | return true; |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | /// Deprecated. Use `length` or `indexOfSentinel`. | |
| 486 | pub 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 | 485 | /// Deprecated. Use `span`. |
| 493 | 486 | pub 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 | } |
| 496 | 489 | |
| 497 | 490 | /// Deprecated. Use `span`. |
| 498 | 491 | pub 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 | } |
| 501 | 494 | |
| 502 | 495 | /// Takes a pointer to an array, a sentinel-terminated pointer, or a slice, and |
| 503 | 496 | /// returns a slice. If there is a sentinel on the input type, there will be a |
| 504 | 497 | /// 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. | |
| 506 | 500 | pub fn Span(comptime T: type) type { |
| 507 | 501 | var ptr_info = @typeInfo(T).Pointer; |
| 508 | 502 | switch (ptr_info.size) { |
| ... | ... | @@ -515,6 +509,7 @@ pub fn Span(comptime T: type) type { |
| 515 | 509 | }, |
| 516 | 510 | .C => { |
| 517 | 511 | ptr_info.sentinel = 0; |
| 512 | ptr_info.is_allowzero = false; | |
| 518 | 513 | }, |
| 519 | 514 | .Many, .Slice => {}, |
| 520 | 515 | } |
| ... | ... | @@ -541,7 +536,7 @@ test "Span" { |
| 541 | 536 | /// the constness of the input type. |
| 542 | 537 | pub fn span(ptr: var) Span(@TypeOf(ptr)) { |
| 543 | 538 | const Result = Span(@TypeOf(ptr)); |
| 544 | const l = length(ptr); | |
| 539 | const l = len(ptr); | |
| 545 | 540 | if (@typeInfo(Result).Pointer.sentinel) |s| { |
| 546 | 541 | return ptr[0..l :s]; |
| 547 | 542 | } else { |
| ... | ... | @@ -558,7 +553,7 @@ test "span" { |
| 558 | 553 | |
| 559 | 554 | /// Takes a pointer to an array, an array, a sentinel-terminated pointer, |
| 560 | 555 | /// or a slice, and returns the length. |
| 561 | pub fn length(ptr: var) usize { | |
| 556 | pub fn len(ptr: var) usize { | |
| 562 | 557 | return switch (@typeInfo(@TypeOf(ptr))) { |
| 563 | 558 | .Array => |info| info.len, |
| 564 | 559 | .Pointer => |info| switch (info.size) { |
| ... | ... | @@ -577,16 +572,16 @@ pub fn length(ptr: var) usize { |
| 577 | 572 | }; |
| 578 | 573 | } |
| 579 | 574 | |
| 580 | test "length" { | |
| 581 | testing.expect(length("aoeu") == 4); | |
| 575 | test "len" { | |
| 576 | testing.expect(len("aoeu") == 4); | |
| 582 | 577 | |
| 583 | 578 | { |
| 584 | 579 | 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); | |
| 587 | 582 | array[2] = 0; |
| 588 | 583 | const ptr = array[0..2 :0].ptr; |
| 589 | testing.expect(length(ptr) == 2); | |
| 584 | testing.expect(len(ptr) == 2); | |
| 590 | 585 | } |
| 591 | 586 | } |
| 592 | 587 | |
| ... | ... | @@ -1867,8 +1862,13 @@ fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type { |
| 1867 | 1862 | return *[length]meta.Child(meta.Child(T)); |
| 1868 | 1863 | } |
| 1869 | 1864 | |
| 1870 | ///Given a pointer to an array, returns a pointer to a portion of that array, preserving constness. | |
| 1871 | pub 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 | |
| 1867 | pub fn subArrayPtr( | |
| 1868 | ptr: var, | |
| 1869 | comptime start: usize, | |
| 1870 | comptime length: usize, | |
| 1871 | ) SubArrayPtrReturnType(@TypeOf(ptr), length) { | |
| 1872 | 1872 | assert(start + length <= ptr.*.len); |
| 1873 | 1873 | |
| 1874 | 1874 | const ReturnType = SubArrayPtrReturnType(@TypeOf(ptr), length); |
lib/std/net.zig+1-1| ... | ... | @@ -352,7 +352,7 @@ pub const Address = extern union { |
| 352 | 352 | unreachable; |
| 353 | 353 | } |
| 354 | 354 | |
| 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 | 356 | return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len); |
| 357 | 357 | }, |
| 358 | 358 | else => unreachable, |
lib/std/os.zig+1-1| ... | ... | @@ -1095,7 +1095,7 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std. |
| 1095 | 1095 | |
| 1096 | 1096 | pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void { |
| 1097 | 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 | 1099 | allocator.free(env_buf); |
| 1100 | 1100 | } |
| 1101 | 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 | 47 | } |
| 48 | 48 | |
| 49 | 49 | fn strlen(s: [*:0]const u8) callconv(.C) usize { |
| 50 | return std.mem.len(u8, s); | |
| 50 | return std.mem.len(s); | |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | fn 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 | 4849 | } |
| 4850 | 4850 | |
| 4851 | 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)]; | |
| 4853 | 4853 | |
| 4854 | 4854 | tok_list.shrink(0); |
| 4855 | 4855 | var tokenizer = std.c.Tokenizer{ |
test/stage1/behavior/misc.zig+1-1| ... | ... | @@ -335,7 +335,7 @@ test "string concatenation" { |
| 335 | 335 | comptime expect(@TypeOf(a) == *const [12:0]u8); |
| 336 | 336 | comptime expect(@TypeOf(b) == *const [12:0]u8); |
| 337 | 337 | |
| 338 | const len = mem.len(u8, b); | |
| 338 | const len = mem.len(b); | |
| 339 | 339 | const len_with_null = len + 1; |
| 340 | 340 | { |
| 341 | 341 | var i: u32 = 0; |