]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: fix %v of complex slice
authorRuss Cox <rsc@golang.org>
Tue, 11 Dec 2012 16:49:41 +0000 (11:49 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 11 Dec 2012 16:49:41 +0000 (11:49 -0500)
Fixes #4525.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6929049

src/pkg/fmt/fmt_test.go
src/pkg/fmt/format.go

index 84fc380307f7ba457560f9db7a1dcb63e0dff02e..867cd981ff4739dff39258b4caa0eccad0d5cb8f 100644 (file)
@@ -476,6 +476,11 @@ var fmttests = []struct {
 
        // Used to crash because nByte didn't allow for a sign.
        {"%b", int64(-1 << 63), "-1000000000000000000000000000000000000000000000000000000000000000"},
+
+       // Complex fmt used to leave the plus flag set for future entries in the array
+       // causing +2+0i and +3+0i instead of 2+0i and 3+0i.
+       {"%v", []complex64{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"},
+       {"%v", []complex128{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"},
 }
 
 func TestSprintf(t *testing.T) {
index ce801162d6e288fc11add52523fbeec6863a9824..d1167ebbf946d5c20a2245752c406305401196af 100644 (file)
@@ -428,6 +428,7 @@ func (f *fmt) fmt_fb32(v float32) { f.formatFloat(float64(v), 'b', 0, 32) }
 func (f *fmt) fmt_c64(v complex64, verb rune) {
        f.buf.WriteByte('(')
        r := real(v)
+       oldPlus := f.plus
        for i := 0; ; i++ {
                switch verb {
                case 'e':
@@ -447,6 +448,7 @@ func (f *fmt) fmt_c64(v complex64, verb rune) {
                f.plus = true
                r = imag(v)
        }
+       f.plus = oldPlus
        f.buf.Write(irparenBytes)
 }
 
@@ -454,6 +456,7 @@ func (f *fmt) fmt_c64(v complex64, verb rune) {
 func (f *fmt) fmt_c128(v complex128, verb rune) {
        f.buf.WriteByte('(')
        r := real(v)
+       oldPlus := f.plus
        for i := 0; ; i++ {
                switch verb {
                case 'e':
@@ -473,5 +476,6 @@ func (f *fmt) fmt_c128(v complex128, verb rune) {
                f.plus = true
                r = imag(v)
        }
+       f.plus = oldPlus
        f.buf.Write(irparenBytes)
 }