From 542e5b8adeed34cf692479c6a343dfadd27dec93 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Fri, 20 Nov 2009 11:04:51 -0800 Subject: [PATCH] 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 --- src/pkg/fmt/fmt_test.go | 14 ++++++++++++++ src/pkg/fmt/format.go | 34 +++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go index 5d9b3abe50..3d9d9bf6fd 100644 --- a/src/pkg/fmt/fmt_test.go +++ b/src/pkg/fmt/fmt_test.go @@ -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)"}, diff --git a/src/pkg/fmt/format.go b/src/pkg/fmt/format.go index 8407fbe0b4..bf13ac3144 100644 --- a/src/pkg/fmt/format.go +++ b/src/pkg/fmt/format.go @@ -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). -- 2.50.0