authorgravatar for goon.pri.low@gmail.comKendall Condon <goon.pri.low@gmail.com> 2024-11-18 11:48:54+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-11-18 13:48:54+02:00
log73f2671c7befa0ca70fefc8230b90510723e22b9
treea19d583553dc36c1933512d450cfa12e2defade8
parent3a6a8b8aa540413d099a6f41a0d8f882f22acb45
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.format: properly handle vectors of pointers


1 files changed, 19 insertions(+), 3 deletions(-)

lib/std/fmt.zig+19-3
......@@ -456,10 +456,10 @@ const ANY = "any";
456456
457457pub fn defaultSpec(comptime T: type) [:0]const u8 {
458458 switch (@typeInfo(T)) {
459 .array => |_| return ANY,
459 .array, .vector => return ANY,
460460 .pointer => |ptr_info| switch (ptr_info.size) {
461461 .One => switch (@typeInfo(ptr_info.child)) {
462 .array => |_| return ANY,
462 .array => return ANY,
463463 else => {},
464464 },
465465 .Many, .C => return "*",
......@@ -680,7 +680,7 @@ pub fn formatType(
680680 try writer.writeAll("{ ");
681681 var i: usize = 0;
682682 while (i < info.len) : (i += 1) {
683 try formatValue(value[i], actual_fmt, options, writer);
683 try formatType(value[i], actual_fmt, options, writer, max_depth - 1);
684684 if (i < info.len - 1) {
685685 try writer.writeAll(", ");
686686 }
......@@ -2608,6 +2608,22 @@ test "vector" {
26082608 try expectFmt("{ -2, -1, +0, +1 }", "{d:5}", .{vi64});
26092609 try expectFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64});
26102610 try expectFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64});
2611
2612 const x: [4]u64 = undefined;
2613 const vp: @Vector(4, *const u64) = [_]*const u64{ &x[0], &x[1], &x[2], &x[3] };
2614 const vop: @Vector(4, ?*const u64) = [_]?*const u64{ &x[0], null, null, &x[3] };
2615
2616 var expect_buffer: [@sizeOf(usize) * 2 * 4 + 64]u8 = undefined;
2617 try expectFmt(try bufPrint(
2618 &expect_buffer,
2619 "{{ {}, {}, {}, {} }}",
2620 .{ &x[0], &x[1], &x[2], &x[3] },
2621 ), "{}", .{vp});
2622 try expectFmt(try bufPrint(
2623 &expect_buffer,
2624 "{{ {?}, null, null, {?} }}",
2625 .{ &x[0], &x[3] },
2626 ), "{any}", .{vop});
26112627}
26122628
26132629test "enum-literal" {