]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: fix formatting of numbers with f.space and f.plus specified
authorMartin Möhrmann <martisch@uos.de>
Sat, 27 Feb 2016 11:19:49 +0000 (12:19 +0100)
committerRob Pike <r@golang.org>
Mon, 29 Feb 2016 00:17:07 +0000 (00:17 +0000)
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 <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/fmt/fmt_test.go
src/fmt/format.go

index 05187af29e31c0fd675f441d368a3c277dc9494b..3cbe93419ee0b271ee874304d379aa3eced0c103 100644 (file)
@@ -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)"},
index f7ac04722923291b89100542071d7176e53e9b85..fc8d057be4b3b1f98067fbff17803435b25742e6 100644 (file)
@@ -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,