summaryrefslogtreecommitdiff
path: root/src/pkg
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2010-07-29 10:50:09 -0700
committerRob Pike <r@golang.org>2010-07-29 10:50:09 -0700
commit277f3623f79f0f92ecdb6acc7dfc5a957d811f6c (patch)
tree1fe6746a8fab536ad952e95c0c42092166559fc1 /src/pkg
parent091c23433e0d0a68d6900ba3a5ac56514cdffa20 (diff)
downloadgo-277f3623f79f0f92ecdb6acc7dfc5a957d811f6c.tar.gz
fmt.Print: fix bug in placement of spaces introduced when ...T went in.
Fixes issue 976. R=rsc, chris tighe, r CC=golang-dev http://codereview.appspot.com/1697057
Diffstat (limited to 'src/pkg')
-rw-r--r--src/pkg/fmt/fmt_test.go20
-rw-r--r--src/pkg/fmt/print.go10
2 files changed, 25 insertions, 5 deletions
diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go
index 7e59d4073..7a4ad7d00 100644
--- a/src/pkg/fmt/fmt_test.go
+++ b/src/pkg/fmt/fmt_test.go
@@ -526,3 +526,23 @@ func TestEmptyMap(t *testing.T) {
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)
+ }
+}
diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go
index 9ae7c39b4..d1ceb7c35 100644
--- a/src/pkg/fmt/print.go
+++ b/src/pkg/fmt/print.go
@@ -492,7 +492,7 @@ var (
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 {
switch {
default:
@@ -850,18 +850,18 @@ func (p *pp) doPrintf(format string, a []interface{}) {
}
func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
- prev_string := false
+ prevString := false
for fieldnum := 0; fieldnum < len(a); fieldnum++ {
p.fmt.clearflags()
// always add spaces if we're doing println
field := a[fieldnum]
if fieldnum > 0 {
- _, is_string := field.(*reflect.StringValue)
- if addspace || !is_string && !prev_string {
+ isString := field != nil && reflect.Typeof(field).Kind() == reflect.String
+ if addspace || !isString && !prevString {
p.buf.WriteByte(' ')
}
}
- prev_string = p.printField(field, 'v', false, false, 0)
+ prevString = p.printField(field, 'v', false, false, 0)
}
if addnewline {
p.buf.WriteByte('\n')