1
0
mirror of https://github.com/golang/go synced 2024-11-20 08:44:39 -07:00

fmt.Print: fix bug in placement of spaces introduced when ...T went in.

Fixes #976.

R=rsc, chris tighe, r
CC=golang-dev
https://golang.org/cl/1697057
This commit is contained in:
Rob Pike 2010-07-29 10:50:09 -07:00
parent 45e12c7cd3
commit 2c0bbf99b9
2 changed files with 25 additions and 5 deletions

View File

@ -526,3 +526,23 @@ func TestEmptyMap(t *testing.T) {
t.Errorf("empty map printed as %q not %q", s, emptyMapStr) t.Errorf("empty map printed as %q not %q", s, emptyMapStr)
} }
} }
// Check that Sprint (and hence Print, Fprint) puts spaces in the right places,
// that is, between arg pairs in which neither is a string.
func TestBlank(t *testing.T) {
got := Sprint("<", 1, ">:", 1, 2, 3, "!")
expect := "<1>:1 2 3!"
if got != expect {
t.Errorf("got %q expected %q", got, expect)
}
}
// Check that Sprintln (and hence Println, Fprintln) puts spaces in the right places,
// that is, between all arg pairs.
func TestBlankln(t *testing.T) {
got := Sprintln("<", 1, ">:", 1, 2, 3, "!")
expect := "< 1 >: 1 2 3 !\n"
if got != expect {
t.Errorf("got %q expected %q", got, expect)
}
}

View File

@ -492,7 +492,7 @@ var (
uintptrBits = reflect.Typeof(uintptr(0)).Bits() uintptrBits = reflect.Typeof(uintptr(0)).Bits()
) )
func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth int) (was_string bool) { func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth int) (wasString bool) {
if field != nil { if field != nil {
switch { switch {
default: default:
@ -850,18 +850,18 @@ 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) {
prev_string := false prevString := false
for fieldnum := 0; fieldnum < len(a); fieldnum++ { for fieldnum := 0; fieldnum < len(a); fieldnum++ {
p.fmt.clearflags() p.fmt.clearflags()
// always add spaces if we're doing println // always add spaces if we're doing println
field := a[fieldnum] field := a[fieldnum]
if fieldnum > 0 { if fieldnum > 0 {
_, is_string := field.(*reflect.StringValue) isString := field != nil && reflect.Typeof(field).Kind() == reflect.String
if addspace || !is_string && !prev_string { if addspace || !isString && !prevString {
p.buf.WriteByte(' ') p.buf.WriteByte(' ')
} }
} }
prev_string = p.printField(field, 'v', false, false, 0) prevString = p.printField(field, 'v', false, false, 0)
} }
if addnewline { if addnewline {
p.buf.WriteByte('\n') p.buf.WriteByte('\n')