Commit c6540d31 authored by Rob Pike's avatar Rob Pike

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
parent 8fdc5b60
...@@ -85,6 +85,10 @@ var fmttests = []fmtTest{ ...@@ -85,6 +85,10 @@ var fmttests = []fmtTest{
fmtTest{ "%v", &array, "&[1 2 3 4 5]" }, fmtTest{ "%v", &array, "&[1 2 3 4 5]" },
fmtTest{ "%v", &iarray, "&[1 hello 2.5 <nil>]" }, fmtTest{ "%v", &iarray, "&[1 hello 2.5 <nil>]" },
// erroneous formats
fmtTest{ "", 2, "?(extra int=2)" },
fmtTest{ "%d", "hello", "%d(string=hello)%" },
// old test/fmt_test.go // old test/fmt_test.go
fmtTest{ "%d", 1234, "1234" }, fmtTest{ "%d", 1234, "1234" },
fmtTest{ "%d", -1234, "-1234" }, fmtTest{ "%d", -1234, "-1234" },
......
...@@ -700,14 +700,20 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) { ...@@ -700,14 +700,20 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) {
default: default:
badtype: badtype:
s = "%" + string(c) + "(" + field.Type().String() + ")%"; s = "%" + string(c) + "(" + field.Type().String() + "=";
p.addstr(s);
p.printField(field);
s= ")%";
} }
p.addstr(s); p.addstr(s);
} }
if fieldnum < v.NumField() { if fieldnum < v.NumField() {
p.addstr("?(extra "); p.addstr("?(extra ");
for ; fieldnum < v.NumField(); fieldnum++ { 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() { if fieldnum + 1 < v.NumField() {
p.addstr(", "); p.addstr(", ");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment