diff options
| author | Martin Möhrmann <martisch@uos.de> | 2016-03-12 13:53:19 +0100 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2016-03-16 05:13:10 +0000 |
| commit | b8ddcc0a03415786bb6278849530c88bfa5b97e6 (patch) | |
| tree | e8146ab3802c9d5e27f1690953f0759e4fa7924c /src/fmt | |
| parent | fee86e4aa81712596d2e6151dc85821953dde107 (diff) | |
| download | go-b8ddcc0a03415786bb6278849530c88bfa5b97e6.tar.xz | |
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: Rob Pike <r@golang.org>
Diffstat (limited to 'src/fmt')
| -rw-r--r-- | src/fmt/fmt_test.go | 6 | ||||
| -rw-r--r-- | 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 8ff53cf487..c2ca690a84 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:<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> "}, diff --git a/src/fmt/print.go b/src/fmt/print.go index e9876913b0..a077f35916 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. |
