authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-31 11:57:31-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-03-31 11:57:31-04:00
log28b7306a31f52f53e7936018c01c0e8d24ebf6ea
tree60cc50145b2d8ed05d1eb34ca7bcc091e20e97ca
parentd9d8c4242649b8e93dca675a0151d6f39c1d4817
parent3cf302a71d50114c44cedbe0114e513063b93302
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4880 from daurnimator/use-spanZ

Take advantage of mem.spanZ accepting null

4 files changed, 21 insertions(+), 12 deletions(-)

lib/std/debug.zig+1-1
......@@ -1254,7 +1254,7 @@ pub const DebugInfo = struct {
12541254 if (context.address >= seg_start and context.address < seg_end) {
12551255 // Android libc uses NULL instead of an empty string to mark the
12561256 // main program
1257 context.name = if (info.dlpi_name) |dlpi_name| mem.spanZ(dlpi_name) else "";
1257 context.name = mem.spanZ(info.dlpi_name) orelse "";
12581258 context.base_address = info.dlpi_addr;
12591259 // Stop the iteration
12601260 return error.Found;
lib/std/mem.zig+16-6
......@@ -559,10 +559,14 @@ test "Span" {
559559/// When there is both a sentinel and an array length or slice length, the
560560/// length value is used instead of the sentinel.
561561pub fn span(ptr: var) Span(@TypeOf(ptr)) {
562 const Result = Span(@TypeOf(ptr));
563 if (@typeInfo(@TypeOf(ptr)) == .Optional and ptr == null) {
564 return null;
562 if (@typeInfo(@TypeOf(ptr)) == .Optional) {
563 if (ptr) |non_null| {
564 return span(non_null);
565 } else {
566 return null;
567 }
565568 }
569 const Result = Span(@TypeOf(ptr));
566570 const l = len(ptr);
567571 if (@typeInfo(Result).Pointer.sentinel) |s| {
568572 return ptr[0..l :s];
......@@ -576,16 +580,21 @@ test "span" {
576580 const ptr = @as([*:3]u16, array[0..2 :3]);
577581 testing.expect(eql(u16, span(ptr), &[_]u16{ 1, 2 }));
578582 testing.expect(eql(u16, span(&array), &[_]u16{ 1, 2, 3, 4, 5 }));
583 testing.expectEqual(@as(?[:0]u16, null), span(@as(?[*:0]u16, null)));
579584}
580585
581586/// Same as `span`, except when there is both a sentinel and an array
582587/// length or slice length, scans the memory for the sentinel value
583588/// rather than using the length.
584589pub fn spanZ(ptr: var) Span(@TypeOf(ptr)) {
585 const Result = Span(@TypeOf(ptr));
586 if (@typeInfo(@TypeOf(ptr)) == .Optional and ptr == null) {
587 return null;
590 if (@typeInfo(@TypeOf(ptr)) == .Optional) {
591 if (ptr) |non_null| {
592 return spanZ(non_null);
593 } else {
594 return null;
595 }
588596 }
597 const Result = Span(@TypeOf(ptr));
589598 const l = lenZ(ptr);
590599 if (@typeInfo(Result).Pointer.sentinel) |s| {
591600 return ptr[0..l :s];
......@@ -599,6 +608,7 @@ test "spanZ" {
599608 const ptr = @as([*:3]u16, array[0..2 :3]);
600609 testing.expect(eql(u16, spanZ(ptr), &[_]u16{ 1, 2 }));
601610 testing.expect(eql(u16, spanZ(&array), &[_]u16{ 1, 2, 3, 4, 5 }));
611 testing.expectEqual(@as(?[:0]u16, null), spanZ(@as(?[*:0]u16, null)));
602612}
603613
604614/// Takes a pointer to an array, an array, a sentinel-terminated pointer,
lib/std/os.zig+1-1
......@@ -1078,7 +1078,7 @@ pub fn execvpe_expandArg0(
10781078 mem.set(?[*:0]u8, argv_buf, null);
10791079 defer {
10801080 for (argv_buf) |arg| {
1081 const arg_buf = if (arg) |ptr| mem.spanZ(ptr) else break;
1081 const arg_buf = mem.spanZ(arg) orelse break;
10821082 allocator.free(arg_buf);
10831083 }
10841084 allocator.free(argv_buf);
src-self-hosted/stage2.zig+3-4
......@@ -689,12 +689,11 @@ fn stage2CrossTarget(
689689 mcpu_oz: ?[*:0]const u8,
690690 dynamic_linker_oz: ?[*:0]const u8,
691691) !CrossTarget {
692 const zig_triple = if (zig_triple_oz) |zig_triple_z| mem.spanZ(zig_triple_z) else "native";
693 const mcpu = if (mcpu_oz) |mcpu_z| mem.spanZ(mcpu_z) else null;
694 const dynamic_linker = if (dynamic_linker_oz) |dl_z| mem.spanZ(dl_z) else null;
692 const mcpu = mem.spanZ(mcpu_oz);
693 const dynamic_linker = mem.spanZ(dynamic_linker_oz);
695694 var diags: CrossTarget.ParseOptions.Diagnostics = .{};
696695 const target: CrossTarget = CrossTarget.parse(.{
697 .arch_os_abi = zig_triple,
696 .arch_os_abi = mem.spanZ(zig_triple_oz) orelse "native",
698697 .cpu_features = mcpu,
699698 .dynamic_linker = dynamic_linker,
700699 .diagnostics = &diags,