Commit b8ddcc0a authored by Martin Möhrmann's avatar Martin Möhrmann Committed by Rob Pike

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 <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRob Pike <r@golang.org>
parent fee86e4a
......@@ -723,10 +723,12 @@ var fmtTests = []struct {
{"%#v", S{F(7), G(8)}, "fmt_test.S{F:<v=F(7)>, 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, " <nil>"},
{"%-10T", nil, "<nil> "},
......
......@@ -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.
......
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