From b8ddcc0a03415786bb6278849530c88bfa5b97e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mo=CC=88hrmann?= Date: Sat, 12 Mar 2016 13:53:19 +0100 Subject: [PATCH] fmt: cleanup %p and %T code paths Remove check for %p and %T in printValue. These verbs are not recursive and are handled already in printArg which is called on any argument before printValue. Format the type string for %T directly instead of invoking the more complex printArg with %s on the type string. Decouple the %T tests from variables declared in scan_test.go. Change-Id: Ibd51566bd4cc1a260ce6d052f36382ed05020b48 Reviewed-on: https://go-review.googlesource.com/20622 Run-TryBot: Rob Pike TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/fmt/fmt_test.go | 6 ++++-- src/fmt/print.go | 20 +++++--------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go index 8ff53cf4870..c2ca690a84b 100644 --- a/src/fmt/fmt_test.go +++ b/src/fmt/fmt_test.go @@ -723,10 +723,12 @@ var fmtTests = []struct { {"%#v", S{F(7), G(8)}, "fmt_test.S{F:, G:GoString(8)}"}, // %T + {"%T", byte(0), "uint8"}, + {"%T", reflect.ValueOf(nil), "reflect.Value"}, {"%T", (4 - 3i), "complex128"}, {"%T", renamedComplex128(4 - 3i), "fmt_test.renamedComplex128"}, - {"%T", intVal, "int"}, - {"%6T", &intVal, " *int"}, + {"%T", intVar, "int"}, + {"%6T", &intVar, " *int"}, {"%10T", nil, " "}, {"%-10T", nil, " "}, diff --git a/src/fmt/print.go b/src/fmt/print.go index e9876913b04..a077f359165 100644 --- a/src/fmt/print.go +++ b/src/fmt/print.go @@ -700,10 +700,10 @@ func (p *pp) printArg(arg interface{}, verb rune, depth int) { // %T (the value's type) and %p (its address) are special; we always do them first. switch verb { case 'T': - p.printArg(reflect.TypeOf(arg).String(), 's', 0) + p.fmt.fmt_s(reflect.TypeOf(arg).String()) return case 'p': - p.fmtPointer(reflect.ValueOf(arg), verb) + p.fmtPointer(reflect.ValueOf(arg), 'p') return } @@ -760,11 +760,12 @@ func (p *pp) printArg(arg interface{}, verb rune, depth int) { p.arg = nil } -// printValue is like printArg but starts with a reflect value, not an interface{} value. +// printValue is similar to printArg but starts with a reflect value, not an interface{} value. +// It does not handle 'p' and 'T' verbs because these should have been already handled by printArg. func (p *pp) printValue(value reflect.Value, verb rune, depth int) { if !value.IsValid() { switch verb { - case 'T', 'v': + case 'v': p.buf.WriteString(nilAngleString) default: p.badVerb(verb) @@ -772,17 +773,6 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) { return } - // Special processing considerations. - // %T (the value's type) and %p (its address) are special; we always do them first. - switch verb { - case 'T': - p.printArg(value.Type().String(), 's', 0) - return - case 'p': - p.fmtPointer(value, verb) - return - } - // Handle values with special methods. // Call always, even when arg == nil, because handleMethods clears p.fmt.plus for us. p.arg = nil // Make sure it's cleared, for safety.