Commit 33c0ef2d authored by Martin Möhrmann's avatar Martin Möhrmann Committed by Dave Cheney

fmt: make identification of string arguments consistent

Use only reflect.TypeOf to detect if argument is a string.

The wasString return is only needed in doPrint with the 'v' verb.
This type of string detection is handled correctly by reflect.TypeOf
which is used already in doPrint for identifying a string argument.

Remove now obsolete wasString computations and return values.

Change-Id: Iea2de7ac0f5c536a53eec63f7e679d628f5af8dc
Reviewed-on: https://go-review.googlesource.com/19976
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent 8ad027c0
...@@ -726,17 +726,18 @@ func (p *pp) handleMethods(verb rune, depth int) (handled bool) { ...@@ -726,17 +726,18 @@ func (p *pp) handleMethods(verb rune, depth int) (handled bool) {
return false return false
} }
func (p *pp) printArg(arg interface{}, verb rune, depth int) (wasString bool) { func (p *pp) printArg(arg interface{}, verb rune, depth int) {
p.arg = arg p.arg = arg
p.value = reflect.Value{} p.value = reflect.Value{}
if arg == nil { if arg == nil {
if verb == 'T' || verb == 'v' { switch verb {
case 'T', 'v':
p.fmt.padString(nilAngleString) p.fmt.padString(nilAngleString)
} else { default:
p.badVerb(verb) p.badVerb(verb)
} }
return false return
} }
// Special processing considerations. // Special processing considerations.
...@@ -744,10 +745,10 @@ func (p *pp) printArg(arg interface{}, verb rune, depth int) (wasString bool) { ...@@ -744,10 +745,10 @@ func (p *pp) printArg(arg interface{}, verb rune, depth int) (wasString bool) {
switch verb { switch verb {
case 'T': case 'T':
p.printArg(reflect.TypeOf(arg).String(), 's', 0) p.printArg(reflect.TypeOf(arg).String(), 's', 0)
return false return
case 'p': case 'p':
p.fmtPointer(reflect.ValueOf(arg), verb) p.fmtPointer(reflect.ValueOf(arg), verb)
return false return
} }
// Some types can be done without reflection. // Some types can be done without reflection.
...@@ -786,33 +787,33 @@ func (p *pp) printArg(arg interface{}, verb rune, depth int) (wasString bool) { ...@@ -786,33 +787,33 @@ func (p *pp) printArg(arg interface{}, verb rune, depth int) (wasString bool) {
p.fmtUint64(uint64(f), verb) p.fmtUint64(uint64(f), verb)
case string: case string:
p.fmtString(f, verb) p.fmtString(f, verb)
wasString = verb == 's' || verb == 'v'
case []byte: case []byte:
p.fmtBytes(f, verb, nil, depth) p.fmtBytes(f, verb, nil, depth)
wasString = verb == 's'
case reflect.Value: case reflect.Value:
return p.printReflectValue(f, verb, depth) p.printReflectValue(f, verb, depth)
return
default: default:
// If the type is not simple, it might have methods. // If the type is not simple, it might have methods.
if handled := p.handleMethods(verb, depth); handled { if p.handleMethods(verb, depth) {
return false return
} }
// Need to use reflection // Need to use reflection
return p.printReflectValue(reflect.ValueOf(arg), verb, depth) p.printReflectValue(reflect.ValueOf(arg), verb, depth)
return
} }
p.arg = nil p.arg = nil
return
} }
// printValue is like printArg but starts with a reflect value, not an interface{} value. // printValue is like printArg but starts with a reflect value, not an interface{} value.
func (p *pp) printValue(value reflect.Value, verb rune, depth int) (wasString bool) { func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
if !value.IsValid() { if !value.IsValid() {
if verb == 'T' || verb == 'v' { switch verb {
case 'T', 'v':
p.buf.WriteString(nilAngleString) p.buf.WriteString(nilAngleString)
} else { default:
p.badVerb(verb) p.badVerb(verb)
} }
return false return
} }
// Special processing considerations. // Special processing considerations.
...@@ -820,10 +821,10 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) (wasString bo ...@@ -820,10 +821,10 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) (wasString bo
switch verb { switch verb {
case 'T': case 'T':
p.printArg(value.Type().String(), 's', 0) p.printArg(value.Type().String(), 's', 0)
return false return
case 'p': case 'p':
p.fmtPointer(value, verb) p.fmtPointer(value, verb)
return false return
} }
// Handle values with special methods. // Handle values with special methods.
...@@ -832,18 +833,18 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) (wasString bo ...@@ -832,18 +833,18 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) (wasString bo
if value.CanInterface() { if value.CanInterface() {
p.arg = value.Interface() p.arg = value.Interface()
} }
if handled := p.handleMethods(verb, depth); handled { if p.handleMethods(verb, depth) {
return false return
} }
return p.printReflectValue(value, verb, depth) p.printReflectValue(value, verb, depth)
} }
var byteType = reflect.TypeOf(byte(0)) var byteType = reflect.TypeOf(byte(0))
// printReflectValue is the fallback for both printArg and printValue. // printReflectValue is the fallback for both printArg and printValue.
// It uses reflect to print the value. // It uses reflect to print the value.
func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) (wasString bool) { func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) {
oldValue := p.value oldValue := p.value
p.value = value p.value = value
BigSwitch: BigSwitch:
...@@ -933,7 +934,7 @@ BigSwitch: ...@@ -933,7 +934,7 @@ BigSwitch:
p.buf.WriteString(nilAngleString) p.buf.WriteString(nilAngleString)
} }
} else { } else {
wasString = p.printValue(value, verb, depth+1) p.printValue(value, verb, depth+1)
} }
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
// Byte slices are special: // Byte slices are special:
...@@ -957,7 +958,6 @@ BigSwitch: ...@@ -957,7 +958,6 @@ BigSwitch:
} }
} }
p.fmtBytes(bytes, verb, typ, depth) p.fmtBytes(bytes, verb, typ, depth)
wasString = verb == 's'
break break
} }
if p.fmt.sharpV { if p.fmt.sharpV {
...@@ -1012,7 +1012,6 @@ BigSwitch: ...@@ -1012,7 +1012,6 @@ BigSwitch:
p.unknownType(f) p.unknownType(f)
} }
p.value = oldValue p.value = oldValue
return wasString
} }
// intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type. // intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type.
...@@ -1257,17 +1256,16 @@ func (p *pp) doPrintf(format string, a []interface{}) { ...@@ -1257,17 +1256,16 @@ func (p *pp) doPrintf(format string, a []interface{}) {
func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) { func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
prevString := false prevString := false
for argNum := 0; argNum < len(a); argNum++ { for argNum, arg := range a {
p.fmt.clearflags() p.fmt.clearflags()
// always add spaces if we're doing Println isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
arg := a[argNum] // Add a space between two non-string arguments or if
if argNum > 0 { // explicitly asked for by addspace.
isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String if argNum > 0 && (addspace || (!isString && !prevString)) {
if addspace || !isString && !prevString { p.buf.WriteByte(' ')
p.buf.WriteByte(' ')
}
} }
prevString = p.printArg(arg, 'v', 0) p.printArg(arg, 'v', 0)
prevString = isString
} }
if addnewline { if addnewline {
p.buf.WriteByte('\n') p.buf.WriteByte('\n')
......
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