| ... | ... | @@ -74,7 +74,7 @@ fn peekIsAlign(comptime fmt: []const u8) bool { |
| 74 | 74 | /// with `?` being the type formatted, this function will be called instead of the default implementation. |
| 75 | 75 | /// This allows user types to be formatted in a logical manner instead of dumping all fields of the type. |
| 76 | 76 | /// |
| 77 | | /// A user type may be a `struct`, `union` or `enum` type. |
| 77 | /// A user type may be a `struct`, `vector`, `union` or `enum` type. |
| 78 | 78 | pub fn format( |
| 79 | 79 | context: var, |
| 80 | 80 | comptime Errors: type, |
| ... | ... | @@ -474,6 +474,18 @@ pub fn formatType( |
| 474 | 474 | }); |
| 475 | 475 | return formatType(@as(Slice, &value), fmt, options, context, Errors, output, max_depth); |
| 476 | 476 | }, |
| 477 | .Vector => { |
| 478 | const len = @typeInfo(T).Vector.len; |
| 479 | try output(context, "{ "); |
| 480 | var i: usize = 0; |
| 481 | while (i < len) : (i += 1) { |
| 482 | try formatValue(value[i], fmt, options, context, Errors, output); |
| 483 | if (i < len - 1) { |
| 484 | try output(context, ", "); |
| 485 | } |
| 486 | } |
| 487 | try output(context, " }"); |
| 488 | }, |
| 477 | 489 | .Fn => { |
| 478 | 490 | return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) }); |
| 479 | 491 | }, |
| ... | ... | @@ -500,6 +512,7 @@ fn formatValue( |
| 500 | 512 | switch (@typeId(T)) { |
| 501 | 513 | .Float => return formatFloatValue(value, fmt, options, context, Errors, output), |
| 502 | 514 | .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output), |
| 515 | .Bool => return output(context, if (value) "true" else "false"), |
| 503 | 516 | else => comptime unreachable, |
| 504 | 517 | } |
| 505 | 518 | } |
| ... | ... | @@ -1713,3 +1726,20 @@ test "positional with specifier" { |
| 1713 | 1726 | test "positional/alignment/width/precision" { |
| 1714 | 1727 | try testFmt("10.0", "{0d: >3.1}", .{@as(f64, 9.999)}); |
| 1715 | 1728 | } |
| 1729 | |
| 1730 | test "vector" { |
| 1731 | // https://github.com/ziglang/zig/issues/3317 |
| 1732 | if (builtin.arch == .mipsel) return error.SkipZigTest; |
| 1733 | |
| 1734 | const vbool: @Vector(4, bool) = [_]bool{ true, false, true, false }; |
| 1735 | const vi64: @Vector(4, i64) = [_]i64{ -2, -1, 0, 1 }; |
| 1736 | const vu64: @Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 }; |
| 1737 | |
| 1738 | try testFmt("{ true, false, true, false }", "{}", .{vbool}); |
| 1739 | try testFmt("{ -2, -1, 0, 1 }", "{}", .{vi64}); |
| 1740 | try testFmt("{ - 2, - 1, + 0, + 1 }", "{d:5}", .{vi64}); |
| 1741 | try testFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64}); |
| 1742 | try testFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64}); |
| 1743 | try testFmt("{ 1kB, 2kB, 3kB, 4kB }", "{B}", .{vu64}); |
| 1744 | try testFmt("{ 1000B, 1.953125KiB, 2.9296875KiB, 3.90625KiB }", "{Bi}", .{vu64}); |
| 1745 | } |