]> Cypherpunks repositories - gostls13.git/commitdiff
add unimplemented %+ and % (space) flags to floating-point print.
authorRob Pike <r@golang.org>
Fri, 20 Nov 2009 19:04:51 +0000 (11:04 -0800)
committerRob Pike <r@golang.org>
Fri, 20 Nov 2009 19:04:51 +0000 (11:04 -0800)
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
src/pkg/fmt/format.go

index 5d9b3abe50fcb8be4490d489069a281431b7cec1..3d9d9bf6fd4530dfb108f5784bd75aab7e587212 100644 (file)
@@ -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)"},
index 8407fbe0b4704ddf5b85bf881d5039196693635d..bf13ac3144eba917f7debf124620e31123cd2170 100644 (file)
@@ -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).