authorgravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-02-07 22:18:48+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-07 16:18:48-05:00
log79ad1d96108fc25fea6a2fbdb5c9bb8b2093a6b5
tree03aee28d441bdef135727d65615dcc7210a82c6d
parent0090c2d70b89cc6b5ab02bdc02aa49c4caf71a5a

format struct pointers as "<typename>@<address>" (#752)


1 files changed, 15 insertions(+), 1 deletions(-)

std/fmt/index.zig+15-1
...@@ -228,7 +228,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons...@@ -228,7 +228,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons
228 if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) {228 if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) {
229 return output(context, (*value)[0..]);229 return output(context, (*value)[0..]);
230 } else {230 } else {
231 @compileError("Unable to format type '" ++ @typeName(T) ++ "'");231 return format(context, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
232 }232 }
233 },233 },
234 else => if (@canImplicitCast([]const u8, value)) {234 else => if (@canImplicitCast([]const u8, value)) {
...@@ -546,6 +546,12 @@ test "parse unsigned comptime" {...@@ -546,6 +546,12 @@ test "parse unsigned comptime" {
546 }546 }
547}547}
548548
549// Dummy field because of https://github.com/zig-lang/zig/issues/557.
550// At top level because of https://github.com/zig-lang/zig/issues/675.
551const Struct = struct {
552 unused: u8,
553};
554
549test "fmt.format" {555test "fmt.format" {
550 {556 {
551 var buf1: [32]u8 = undefined;557 var buf1: [32]u8 = undefined;
...@@ -577,6 +583,14 @@ test "fmt.format" {...@@ -577,6 +583,14 @@ test "fmt.format" {
577 const result = try bufPrint(buf1[0..], "u3: {}\n", value);583 const result = try bufPrint(buf1[0..], "u3: {}\n", value);
578 assert(mem.eql(u8, result, "u3: 5\n"));584 assert(mem.eql(u8, result, "u3: 5\n"));
579 }585 }
586 {
587 var buf1: [32]u8 = undefined;
588 const value = Struct {
589 .unused = 42,
590 };
591 const result = try bufPrint(buf1[0..], "pointer: {}\n", &value);
592 assert(mem.startsWith(u8, result, "pointer: Struct@"));
593 }
580594
581 // TODO get these tests passing in release modes595 // TODO get these tests passing in release modes
582 // https://github.com/zig-lang/zig/issues/564596 // https://github.com/zig-lang/zig/issues/564