Commit cba81d80 authored by Rob Pike's avatar Rob Pike

fix bug in complex printing: imaginary didn't have same format as real.

add tests.

R=rsc, ken2, ken3
CC=golang-dev
https://golang.org/cl/261041
parent 788b4175
...@@ -119,6 +119,28 @@ var fmttests = []fmtTest{ ...@@ -119,6 +119,28 @@ var fmttests = []fmtTest{
fmtTest{"% .3g", -1.0, "-1"}, fmtTest{"% .3g", -1.0, "-1"},
fmtTest{"% .3g", 1.0, " 1"}, fmtTest{"% .3g", 1.0, " 1"},
/* TODO: Enable when complex support is in all compilers
// complex values
fmtTest{"%+.3e", 0i, "(+0.000e+00+0.000e+00i)"},
fmtTest{"%+.3f", 0i, "(+0.000+0.000i)"},
fmtTest{"%+.3g", 0i, "(+0+0i)"},
fmtTest{"%+.3e", 1 + 2i, "(+1.000e+00+2.000e+00i)"},
fmtTest{"%+.3f", 1 + 2i, "(+1.000+2.000i)"},
fmtTest{"%+.3g", 1 + 2i, "(+1+2i)"},
fmtTest{"%.3e", 0i, "(0.000e+00+0.000e+00i)"},
fmtTest{"%.3f", 0i, "(0.000+0.000i)"},
fmtTest{"%.3g", 0i, "(0+0i)"},
fmtTest{"%.3e", 1 + 2i, "(1.000e+00+2.000e+00i)"},
fmtTest{"%.3f", 1 + 2i, "(1.000+2.000i)"},
fmtTest{"%.3g", 1 + 2i, "(1+2i)"},
fmtTest{"%.3e", -1 - 2i, "(-1.000e+00-2.000e+00i)"},
fmtTest{"%.3f", -1 - 2i, "(-1.000-2.000i)"},
fmtTest{"%.3g", -1 - 2i, "(-1-2i)"},
fmtTest{"% .3E", -1 - 2i, "(-1.000E+00-2.000E+00i)"},
fmtTest{"%+.3g", complex64(1 + 2i), "(+1+2i)"},
fmtTest{"%+.3g", complex128(1 + 2i), "(+1+2i)"},
*/
// erroneous formats // erroneous formats
fmtTest{"", 2, "?(extra int=2)"}, fmtTest{"", 2, "?(extra int=2)"},
fmtTest{"%d", "hello", "%d(string=hello)"}, fmtTest{"%d", "hello", "%d(string=hello)"},
...@@ -210,6 +232,13 @@ var fmttests = []fmtTest{ ...@@ -210,6 +232,13 @@ var fmttests = []fmtTest{
fmtTest{"%v", &array, "&[1 2 3 4 5]"}, fmtTest{"%v", &array, "&[1 2 3 4 5]"},
fmtTest{"%v", &iarray, "&[1 hello 2.5 <nil>]"}, fmtTest{"%v", &iarray, "&[1 hello 2.5 <nil>]"},
/* TODO: Enable when complex support is in all compilers
// complexes with %v
fmtTest{"%v", 1 + 2i, "(1+2i)"},
fmtTest{"%v", complex64(1 + 2i), "(1+2i)"},
fmtTest{"%v", complex128(1 + 2i), "(1+2i)"},
*/
// structs // structs
fmtTest{"%v", A{1, 2, "a", []int{1, 2}}, `{1 2 a [1 2]}`}, fmtTest{"%v", A{1, 2, "a", []int{1, 2}}, `{1 2 a [1 2]}`},
fmtTest{"%+v", A{1, 2, "a", []int{1, 2}}, `{i:1 j:2 s:a x:[1 2]}`}, fmtTest{"%+v", A{1, 2, "a", []int{1, 2}}, `{i:1 j:2 s:a x:[1 2]}`},
......
...@@ -50,6 +50,7 @@ type fmt struct { ...@@ -50,6 +50,7 @@ type fmt struct {
sharp bool sharp bool
space bool space bool
zero bool zero bool
preserveFlags bool // don't clear flags after this print; used to carry over in complex prints
} }
func (f *fmt) clearflags() { func (f *fmt) clearflags() {
...@@ -119,7 +120,9 @@ func (f *fmt) pad(b []byte) { ...@@ -119,7 +120,9 @@ func (f *fmt) pad(b []byte) {
if right > 0 { if right > 0 {
f.writePadding(right, padding) f.writePadding(right, padding)
} }
if !f.preserveFlags {
f.clearflags() f.clearflags()
}
} }
// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus). // append s to buf, padded on left (w > 0) or right (w < 0 or f.minus).
...@@ -137,7 +140,9 @@ func (f *fmt) padString(s string) { ...@@ -137,7 +140,9 @@ func (f *fmt) padString(s string) {
if right > 0 { if right > 0 {
f.writePadding(right, padding) f.writePadding(right, padding)
} }
if !f.preserveFlags {
f.clearflags() f.clearflags()
}
} }
func putint(buf []byte, base, val uint64, digits string) int { func putint(buf []byte, base, val uint64, digits string) int {
...@@ -425,6 +430,7 @@ func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.Ftoa32(v, 'b', 0)) } ...@@ -425,6 +430,7 @@ func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.Ftoa32(v, 'b', 0)) }
func (f *fmt) fmt_c64(v complex64, fmt_x byte) { func (f *fmt) fmt_c64(v complex64, fmt_x byte) {
f.buf.WriteByte('(') f.buf.WriteByte('(')
r := real(v) r := real(v)
f.preserveFlags = true
for i := 0; ; i++ { for i := 0; ; i++ {
switch fmt_x { switch fmt_x {
case 'e': case 'e':
...@@ -438,6 +444,7 @@ func (f *fmt) fmt_c64(v complex64, fmt_x byte) { ...@@ -438,6 +444,7 @@ func (f *fmt) fmt_c64(v complex64, fmt_x byte) {
case 'G': case 'G':
f.fmt_G32(r) f.fmt_G32(r)
} }
f.preserveFlags = false
if i != 0 { if i != 0 {
break break
} }
...@@ -452,6 +459,7 @@ func (f *fmt) fmt_c64(v complex64, fmt_x byte) { ...@@ -452,6 +459,7 @@ func (f *fmt) fmt_c64(v complex64, fmt_x byte) {
func (f *fmt) fmt_c128(v complex128, fmt_x byte) { func (f *fmt) fmt_c128(v complex128, fmt_x byte) {
f.buf.WriteByte('(') f.buf.WriteByte('(')
r := real(v) r := real(v)
f.preserveFlags = true
for i := 0; ; i++ { for i := 0; ; i++ {
switch fmt_x { switch fmt_x {
case 'e': case 'e':
...@@ -465,6 +473,7 @@ func (f *fmt) fmt_c128(v complex128, fmt_x byte) { ...@@ -465,6 +473,7 @@ func (f *fmt) fmt_c128(v complex128, fmt_x byte) {
case 'G': case 'G':
f.fmt_G64(r) f.fmt_G64(r)
} }
f.preserveFlags = false
if i != 0 { if i != 0 {
break break
} }
......
...@@ -927,7 +927,6 @@ func (p *pp) doprintf(format string, a []interface{}) { ...@@ -927,7 +927,6 @@ func (p *pp) doprintf(format string, a []interface{}) {
p.fmt.fmt_c64(v, 'e') p.fmt.fmt_c64(v, 'e')
} else if v, ok := getComplex128(field); ok { } else if v, ok := getComplex128(field); ok {
p.fmt.fmt_c128(v, 'e') p.fmt.fmt_c128(v, 'e')
} else { } else {
goto badtype goto badtype
} }
......
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