diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-02-13 16:14:10 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-02-13 16:14:10 -0500 |
| commit | de23c571334b6df172751f45e6eda02a138f7cdf (patch) | |
| tree | 808467672a272c8fb9f1593fc92f143a70495aca | |
| parent | 1675d4f82b94b4db0272aff483760cd526963a4c (diff) | |
| parent | cdba521a06353a1ae25c2e37bb44e11c6fec1035 (diff) | |
| download | zig-de23c571334b6df172751f45e6eda02a138f7cdf.tar.gz zig-de23c571334b6df172751f45e6eda02a138f7cdf.zip | |
Merge branch 'LemonBoy-revive-3904'
closes #3904
closes #4448
| -rw-r--r-- | lib/std/fmt.zig | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 0967d9f069..a0ec668769 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -74,7 +74,7 @@ fn peekIsAlign(comptime fmt: []const u8) bool { /// with `?` being the type formatted, this function will be called instead of the default implementation. /// This allows user types to be formatted in a logical manner instead of dumping all fields of the type. /// -/// A user type may be a `struct`, `union` or `enum` type. +/// A user type may be a `struct`, `vector`, `union` or `enum` type. pub fn format( context: var, comptime Errors: type, @@ -474,6 +474,18 @@ pub fn formatType( }); return formatType(@as(Slice, &value), fmt, options, context, Errors, output, max_depth); }, + .Vector => { + const len = @typeInfo(T).Vector.len; + try output(context, "{ "); + var i: usize = 0; + while (i < len) : (i += 1) { + try formatValue(value[i], fmt, options, context, Errors, output); + if (i < len - 1) { + try output(context, ", "); + } + } + try output(context, " }"); + }, .Fn => { return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) }); }, @@ -500,6 +512,7 @@ fn formatValue( switch (@typeId(T)) { .Float => return formatFloatValue(value, fmt, options, context, Errors, output), .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output), + .Bool => return output(context, if (value) "true" else "false"), else => comptime unreachable, } } @@ -1713,3 +1726,20 @@ test "positional with specifier" { test "positional/alignment/width/precision" { try testFmt("10.0", "{0d: >3.1}", .{@as(f64, 9.999)}); } + +test "vector" { + // https://github.com/ziglang/zig/issues/3317 + if (builtin.arch == .mipsel) return error.SkipZigTest; + + const vbool: @Vector(4, bool) = [_]bool{ true, false, true, false }; + const vi64: @Vector(4, i64) = [_]i64{ -2, -1, 0, 1 }; + const vu64: @Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 }; + + try testFmt("{ true, false, true, false }", "{}", .{vbool}); + try testFmt("{ -2, -1, 0, 1 }", "{}", .{vi64}); + try testFmt("{ - 2, - 1, + 0, + 1 }", "{d:5}", .{vi64}); + try testFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64}); + try testFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64}); + try testFmt("{ 1kB, 2kB, 3kB, 4kB }", "{B}", .{vu64}); + try testFmt("{ 1000B, 1.953125KiB, 2.9296875KiB, 3.90625KiB }", "{Bi}", .{vu64}); +} |
