1
0
mirror of https://github.com/golang/go synced 2024-11-20 06:04:52 -07:00

fmt: fix %v of complex slice

Fixes #4525.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6929049
This commit is contained in:
Russ Cox 2012-12-11 11:49:41 -05:00
parent 15353d2114
commit 07cc05864c
2 changed files with 9 additions and 0 deletions

View File

@ -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) {

View 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) { 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) {
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) {
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) {
f.plus = true f.plus = true
r = imag(v) r = imag(v)
} }
f.plus = oldPlus
f.buf.Write(irparenBytes) f.buf.Write(irparenBytes)
} }