aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorgooncreeper <149842806+gooncreeper@users.noreply.github.com>2024-11-18 11:48:54 +0000
committerGitHub <noreply@github.com>2024-11-18 13:48:54 +0200
commit73f2671c7befa0ca70fefc8230b90510723e22b9 (patch)
treea19d583553dc36c1933512d450cfa12e2defade8 /lib/std
parent3a6a8b8aa540413d099a6f41a0d8f882f22acb45 (diff)
downloadzig-73f2671c7befa0ca70fefc8230b90510723e22b9.tar.gz
zig-73f2671c7befa0ca70fefc8230b90510723e22b9.zip
std.format: properly handle vectors of pointers
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/fmt.zig22
1 files changed, 19 insertions, 3 deletions
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
index 8deef118c0..eba72721f9 100644
--- a/lib/std/fmt.zig
+++ b/lib/std/fmt.zig
@@ -456,10 +456,10 @@ const ANY = "any";
pub fn defaultSpec(comptime T: type) [:0]const u8 {
switch (@typeInfo(T)) {
- .array => |_| return ANY,
+ .array, .vector => return ANY,
.pointer => |ptr_info| switch (ptr_info.size) {
.One => switch (@typeInfo(ptr_info.child)) {
- .array => |_| return ANY,
+ .array => return ANY,
else => {},
},
.Many, .C => return "*",
@@ -680,7 +680,7 @@ pub fn formatType(
try writer.writeAll("{ ");
var i: usize = 0;
while (i < info.len) : (i += 1) {
- try formatValue(value[i], actual_fmt, options, writer);
+ try formatType(value[i], actual_fmt, options, writer, max_depth - 1);
if (i < info.len - 1) {
try writer.writeAll(", ");
}
@@ -2608,6 +2608,22 @@ test "vector" {
try expectFmt("{ -2, -1, +0, +1 }", "{d:5}", .{vi64});
try expectFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64});
try expectFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64});
+
+ const x: [4]u64 = undefined;
+ const vp: @Vector(4, *const u64) = [_]*const u64{ &x[0], &x[1], &x[2], &x[3] };
+ const vop: @Vector(4, ?*const u64) = [_]?*const u64{ &x[0], null, null, &x[3] };
+
+ var expect_buffer: [@sizeOf(usize) * 2 * 4 + 64]u8 = undefined;
+ try expectFmt(try bufPrint(
+ &expect_buffer,
+ "{{ {}, {}, {}, {} }}",
+ .{ &x[0], &x[1], &x[2], &x[3] },
+ ), "{}", .{vp});
+ try expectFmt(try bufPrint(
+ &expect_buffer,
+ "{{ {?}, null, null, {?} }}",
+ .{ &x[0], &x[3] },
+ ), "{any}", .{vop});
}
test "enum-literal" {