From: Martin Möhrmann Date: Sat, 27 Feb 2016 11:19:49 +0000 (+0100) Subject: fmt: fix formatting of numbers with f.space and f.plus specified X-Git-Tag: go1.7beta1~1649 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=75cc05fa557b26336bc8e3f0a6c9f03b904a85eb;p=gostls13.git fmt: fix formatting of numbers with f.space and f.plus specified Do not replace the sign in front of a number with a space if both f.space and f.plus are both specified for number formatting. This was already the case for integers but not for floats and complex numbers. Updates: #14543. Change-Id: I07ddeb505003db84a8a7d2c743dc19fc427a00bd Reviewed-on: https://go-review.googlesource.com/19974 Run-TryBot: Rob Pike TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go index 05187af29e..3cbe93419e 100644 --- a/src/fmt/fmt_test.go +++ b/src/fmt/fmt_test.go @@ -278,6 +278,9 @@ var fmtTests = []struct { {"%b", 1.0, "4503599627370496p-52"}, // complex values + {"%.f", 0i, "(0+0i)"}, + {"%+.f", 0i, "(+0+0i)"}, + {"% +.f", 0i, "(+0+0i)"}, {"%+.3e", 0i, "(+0.000e+00+0.000e+00i)"}, {"%+.3f", 0i, "(+0.000+0.000i)"}, {"%+.3g", 0i, "(+0+0i)"}, @@ -384,9 +387,13 @@ var fmtTests = []struct { {"%g", 1.23456789e-3, "0.00123456789"}, {"%g", 1.23456789e20, "1.23456789e+20"}, {"%20e", math.Inf(1), " +Inf"}, + {"% 20f", math.Inf(1), " Inf"}, + {"%+20f", math.Inf(1), " +Inf"}, + {"% +20f", math.Inf(1), " +Inf"}, {"%-20f", math.Inf(-1), "-Inf "}, {"%20g", math.NaN(), " NaN"}, {"%+20f", math.NaN(), " +NaN"}, + {"% +20f", math.NaN(), " +NaN"}, {"% -20f", math.NaN(), " NaN "}, {"%+-20f", math.NaN(), "+NaN "}, @@ -608,14 +615,16 @@ var fmtTests = []struct { "[%7.2f]", "[% 7.2f]", "[%+7.2f]", + "[% +7.2f]", "[%07.2f]", "[% 07.2f]", "[%+07.2f]", + "[% +07.2f]" }; int main(void) { int i; - for(i = 0; i < 9; i++) { + for(i = 0; i < 11; i++) { printf("%s: ", format[i]); printf(format[i], 1.0); printf(" "); @@ -631,9 +640,12 @@ var fmtTests = []struct { [%7.2f]: [ 1.00] [ -1.00] [% 7.2f]: [ 1.00] [ -1.00] [%+7.2f]: [ +1.00] [ -1.00] + [% +7.2f]: [ +1.00] [ -1.00] [%07.2f]: [0001.00] [-001.00] [% 07.2f]: [ 001.00] [-001.00] [%+07.2f]: [+001.00] [-001.00] + [% +07.2f]: [+001.00] [-001.00] + */ {"%.2f", 1.0, "1.00"}, {"%.2f", -1.0, "-1.00"}, @@ -647,12 +659,16 @@ var fmtTests = []struct { {"% 7.2f", -1.0, " -1.00"}, {"%+7.2f", 1.0, " +1.00"}, {"%+7.2f", -1.0, " -1.00"}, + {"% +7.2f", 1.0, " +1.00"}, + {"% +7.2f", -1.0, " -1.00"}, {"%07.2f", 1.0, "0001.00"}, {"%07.2f", -1.0, "-001.00"}, {"% 07.2f", 1.0, " 001.00"}, {"% 07.2f", -1.0, "-001.00"}, {"%+07.2f", 1.0, "+001.00"}, {"%+07.2f", -1.0, "-001.00"}, + {"% +07.2f", 1.0, "+001.00"}, + {"% +07.2f", -1.0, "-001.00"}, // Complex numbers: exhaustively tested in TestComplexFormatting. {"%7.2f", 1 + 2i, "( 1.00 +2.00i)"}, diff --git a/src/fmt/format.go b/src/fmt/format.go index f7ac047229..fc8d057be4 100644 --- a/src/fmt/format.go +++ b/src/fmt/format.go @@ -390,8 +390,9 @@ func (f *fmt) formatFloat(v float64, verb byte, prec, n int) { } else { num[0] = '+' } - // f.space says to replace a leading + with a space. - if f.space && num[0] == '+' { + // f.space means to add a leading space instead of a "+" sign unless + // the sign is explicitly asked for by f.plus. + if f.space && num[0] == '+' && !f.plus { num[0] = ' ' } // Special handling for infinities and NaN,