Commit 75cc05fa authored by Martin Möhrmann's avatar Martin Möhrmann Committed by Rob Pike

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 <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent f5ab890c
......@@ -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)"},
......
......@@ -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,
......
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