From c6540d31f6c1aa1c93638db28d32ad6eb7392c9f Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Fri, 28 Aug 2009 13:02:34 -0700 Subject: [PATCH] print the value using (in effect) %v when Printf is given mismatched args for its format Printf("%s", 2) gives %s(int=2) R=rsc DELTA=12 (10 added, 0 deleted, 2 changed) OCL=34042 CL=34044 --- src/pkg/fmt/fmt_test.go | 4 ++++ src/pkg/fmt/print.go | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go index 76234e552b2..5e16c5f2765 100644 --- a/src/pkg/fmt/fmt_test.go +++ b/src/pkg/fmt/fmt_test.go @@ -85,6 +85,10 @@ var fmttests = []fmtTest{ fmtTest{ "%v", &array, "&[1 2 3 4 5]" }, fmtTest{ "%v", &iarray, "&[1 hello 2.5 ]" }, + // erroneous formats + fmtTest{ "", 2, "?(extra int=2)" }, + fmtTest{ "%d", "hello", "%d(string=hello)%" }, + // old test/fmt_test.go fmtTest{ "%d", 1234, "1234" }, fmtTest{ "%d", -1234, "-1234" }, diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go index bb1030e726b..e5177ef19c2 100644 --- a/src/pkg/fmt/print.go +++ b/src/pkg/fmt/print.go @@ -700,14 +700,20 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) { default: badtype: - s = "%" + string(c) + "(" + field.Type().String() + ")%"; + s = "%" + string(c) + "(" + field.Type().String() + "="; + p.addstr(s); + p.printField(field); + s= ")%"; } p.addstr(s); } if fieldnum < v.NumField() { p.addstr("?(extra "); for ; fieldnum < v.NumField(); fieldnum++ { - p.addstr(getField(v, fieldnum).Type().String()); + field := getField(v, fieldnum); + p.addstr(field.Type().String()); + p.addstr("="); + p.printField(field); if fieldnum + 1 < v.NumField() { p.addstr(", "); }