Commit 07cc0586 authored by Russ Cox's avatar Russ Cox

fmt: fix %v of complex slice

Fixes #4525.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6929049
parent 15353d21
...@@ -476,6 +476,11 @@ var fmttests = []struct { ...@@ -476,6 +476,11 @@ var fmttests = []struct {
// Used to crash because nByte didn't allow for a sign. // Used to crash because nByte didn't allow for a sign.
{"%b", int64(-1 << 63), "-1000000000000000000000000000000000000000000000000000000000000000"}, {"%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) { func TestSprintf(t *testing.T) {
......
...@@ -428,6 +428,7 @@ func (f *fmt) fmt_fb32(v float32) { f.formatFloat(float64(v), 'b', 0, 32) } ...@@ -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) { func (f *fmt) fmt_c64(v complex64, verb rune) {
f.buf.WriteByte('(') f.buf.WriteByte('(')
r := real(v) r := real(v)
oldPlus := f.plus
for i := 0; ; i++ { for i := 0; ; i++ {
switch verb { switch verb {
case 'e': case 'e':
...@@ -447,6 +448,7 @@ func (f *fmt) fmt_c64(v complex64, verb rune) { ...@@ -447,6 +448,7 @@ func (f *fmt) fmt_c64(v complex64, verb rune) {
f.plus = true f.plus = true
r = imag(v) r = imag(v)
} }
f.plus = oldPlus
f.buf.Write(irparenBytes) f.buf.Write(irparenBytes)
} }
...@@ -454,6 +456,7 @@ func (f *fmt) fmt_c64(v complex64, verb rune) { ...@@ -454,6 +456,7 @@ func (f *fmt) fmt_c64(v complex64, verb rune) {
func (f *fmt) fmt_c128(v complex128, verb rune) { func (f *fmt) fmt_c128(v complex128, verb rune) {
f.buf.WriteByte('(') f.buf.WriteByte('(')
r := real(v) r := real(v)
oldPlus := f.plus
for i := 0; ; i++ { for i := 0; ; i++ {
switch verb { switch verb {
case 'e': case 'e':
...@@ -473,5 +476,6 @@ func (f *fmt) fmt_c128(v complex128, verb rune) { ...@@ -473,5 +476,6 @@ func (f *fmt) fmt_c128(v complex128, verb rune) {
f.plus = true f.plus = true
r = imag(v) r = imag(v)
} }
f.plus = oldPlus
f.buf.Write(irparenBytes) f.buf.Write(irparenBytes)
} }
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