1
0
mirror of https://github.com/golang/go synced 2024-11-19 05:04:43 -07:00

fmt: simplify handling of reporting flags to formatters

Remove rewriting of flags before calling formatters.
Change Flag method to directly take plusV and sharpV flags
into account when reporting if plus or sharp flag is set.

Change-Id: Ic3423881ad89e5a5f9fff5ab59e842062394ef6d
Reviewed-on: https://go-review.googlesource.com/20859
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Martin Möhrmann 2016-03-18 16:56:47 +01:00 committed by Rob Pike
parent 15ea61146e
commit e97789f7d9

View File

@ -152,9 +152,9 @@ func (p *pp) Flag(b int) bool {
case '-':
return p.fmt.minus
case '+':
return p.fmt.plus
return p.fmt.plus || p.fmt.plusV
case '#':
return p.fmt.sharp
return p.fmt.sharp || p.fmt.sharpV
case ' ':
return p.fmt.space
case '0':
@ -570,34 +570,6 @@ func (p *pp) catchPanic(arg interface{}, verb rune) {
}
}
// clearSpecialFlags pushes %#v back into the regular flags and returns their old state.
func (p *pp) clearSpecialFlags() (plusV, sharpV bool) {
plusV = p.fmt.plusV
if plusV {
p.fmt.plus = true
p.fmt.plusV = false
}
sharpV = p.fmt.sharpV
if sharpV {
p.fmt.sharp = true
p.fmt.sharpV = false
}
return
}
// restoreSpecialFlags, whose argument should be a call to clearSpecialFlags,
// restores the setting of the plusV and sharpV flags.
func (p *pp) restoreSpecialFlags(plusV, sharpV bool) {
if plusV {
p.fmt.plus = false
p.fmt.plusV = true
}
if sharpV {
p.fmt.sharp = false
p.fmt.sharpV = true
}
}
func (p *pp) handleMethods(verb rune, depth int) (handled bool) {
if p.erroring {
return
@ -605,7 +577,6 @@ func (p *pp) handleMethods(verb rune, depth int) (handled bool) {
// Is it a Formatter?
if formatter, ok := p.arg.(Formatter); ok {
handled = true
defer p.restoreSpecialFlags(p.clearSpecialFlags())
defer p.catchPanic(p.arg, verb)
formatter.Format(p, verb)
return