Commit 542e5b8a authored by Rob Pike's avatar Rob Pike

add unimplemented %+ and % (space) flags to floating-point print.

fix %E: was same as %e.
add tests.

Fixes #278.

R=rsc
CC=golang-dev
https://golang.org/cl/157111
parent 652f5567
......@@ -101,8 +101,22 @@ var fmttests = []fmtTest{
fmtTest{"%010.3d", -1, " -001"},
fmtTest{"%+d", 12345, "+12345"},
fmtTest{"%+d", -12345, "-12345"},
fmtTest{"%+d", 0, "+0"},
fmtTest{"% d", 0, " 0"},
fmtTest{"% d", 12345, " 12345"},
// floats
fmtTest{"%+.3e", 0.0, "+0.000e+00"},
fmtTest{"%+.3e", 1.0, "+1.000e+00"},
fmtTest{"%+.3f", -1.0, "-1.000"},
fmtTest{"% .3E", -1.0, "-1.000E+00"},
fmtTest{"% .3e", 1.0, " 1.000e+00"},
fmtTest{"%+.3g", 0.0, "+0"},
fmtTest{"%+.3g", 1.0, "+1"},
fmtTest{"%+.3g", -1.0, "-1"},
fmtTest{"% .3g", -1.0, "-1"},
fmtTest{"% .3g", 1.0, " 1"},
// erroneous formats
fmtTest{"", 2, "?(extra int=2)"},
fmtTest{"%d", "hello", "%d(string=hello)"},
......
......@@ -422,33 +422,45 @@ func fmtString(f *Fmt, s string) *Fmt {
return f;
}
// Add a plus sign or space to the string if missing and required.
func (f *Fmt) plusSpace(s string) *Fmt {
if s[0] != '-' {
if f.plus {
s = "+" + s
} else if f.space {
s = " " + s
}
}
return fmtString(f, s);
}
// Fmt_e64 formats a float64 in the form -1.23e+12.
func (f *Fmt) Fmt_e64(v float64) *Fmt {
return fmtString(f, strconv.Ftoa64(v, 'e', doPrec(f, 6)))
return f.plusSpace(strconv.Ftoa64(v, 'e', doPrec(f, 6)))
}
// Fmt_E64 formats a float64 in the form -1.23E+12.
func (f *Fmt) Fmt_E64(v float64) *Fmt {
return fmtString(f, strconv.Ftoa64(v, 'E', doPrec(f, 6)))
return f.plusSpace(strconv.Ftoa64(v, 'E', doPrec(f, 6)))
}
// Fmt_f64 formats a float64 in the form -1.23.
func (f *Fmt) Fmt_f64(v float64) *Fmt {
return fmtString(f, strconv.Ftoa64(v, 'f', doPrec(f, 6)))
return f.plusSpace(strconv.Ftoa64(v, 'f', doPrec(f, 6)))
}
// Fmt_g64 formats a float64 in the 'f' or 'e' form according to size.
func (f *Fmt) Fmt_g64(v float64) *Fmt {
return fmtString(f, strconv.Ftoa64(v, 'g', doPrec(f, -1)))
return f.plusSpace(strconv.Ftoa64(v, 'g', doPrec(f, -1)))
}
// Fmt_g64 formats a float64 in the 'f' or 'E' form according to size.
func (f *Fmt) Fmt_G64(v float64) *Fmt {
return fmtString(f, strconv.Ftoa64(v, 'G', doPrec(f, -1)))
return f.plusSpace(strconv.Ftoa64(v, 'G', doPrec(f, -1)))
}
// Fmt_fb64 formats a float64 in the form -123p3 (exponent is power of 2).
func (f *Fmt) Fmt_fb64(v float64) *Fmt { return fmtString(f, strconv.Ftoa64(v, 'b', 0)) }
func (f *Fmt) Fmt_fb64(v float64) *Fmt { return f.plusSpace(strconv.Ftoa64(v, 'b', 0)) }
// float32
// cannot defer to float64 versions
......@@ -456,27 +468,27 @@ func (f *Fmt) Fmt_fb64(v float64) *Fmt { return fmtString(f, strconv.Ftoa64(v, '
// Fmt_e32 formats a float32 in the form -1.23e+12.
func (f *Fmt) Fmt_e32(v float32) *Fmt {
return fmtString(f, strconv.Ftoa32(v, 'e', doPrec(f, 6)))
return f.plusSpace(strconv.Ftoa32(v, 'e', doPrec(f, 6)))
}
// Fmt_E32 formats a float32 in the form -1.23E+12.
func (f *Fmt) Fmt_E32(v float32) *Fmt {
return fmtString(f, strconv.Ftoa32(v, 'e', doPrec(f, 6)))
return f.plusSpace(strconv.Ftoa32(v, 'E', doPrec(f, 6)))
}
// Fmt_f32 formats a float32 in the form -1.23.
func (f *Fmt) Fmt_f32(v float32) *Fmt {
return fmtString(f, strconv.Ftoa32(v, 'f', doPrec(f, 6)))
return f.plusSpace(strconv.Ftoa32(v, 'f', doPrec(f, 6)))
}
// Fmt_g32 formats a float32 in the 'f' or 'e' form according to size.
func (f *Fmt) Fmt_g32(v float32) *Fmt {
return fmtString(f, strconv.Ftoa32(v, 'g', doPrec(f, -1)))
return f.plusSpace(strconv.Ftoa32(v, 'g', doPrec(f, -1)))
}
// Fmt_G32 formats a float32 in the 'f' or 'E' form according to size.
func (f *Fmt) Fmt_G32(v float32) *Fmt {
return fmtString(f, strconv.Ftoa32(v, 'G', doPrec(f, -1)))
return f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f, -1)))
}
// Fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
......
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