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

fmt: fix padding when precision is set for integer formatting

Ignore the f.zero flag and use spaces for padding instead
when precision is set.

Fixes #15331

Change-Id: I3ac485df24b7bdf4fddf69e3cc17c213c737b5ff
Reviewed-on: https://go-review.googlesource.com/22131
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent d07709ed
...@@ -328,6 +328,8 @@ var fmtTests = []struct { ...@@ -328,6 +328,8 @@ var fmtTests = []struct {
{"%d", int64(-1 << 63), "-9223372036854775808"}, {"%d", int64(-1 << 63), "-9223372036854775808"},
{"%.d", 0, ""}, {"%.d", 0, ""},
{"%.0d", 0, ""}, {"%.0d", 0, ""},
{"%6.0d", 0, " "},
{"%06.0d", 0, " "},
{"% d", 12345, " 12345"}, {"% d", 12345, " 12345"},
{"%+d", 12345, "+12345"}, {"%+d", 12345, "+12345"},
{"%+d", -12345, "-12345"}, {"%+d", -12345, "-12345"},
...@@ -346,7 +348,6 @@ var fmtTests = []struct { ...@@ -346,7 +348,6 @@ var fmtTests = []struct {
{"%x", ^uint32(0), "ffffffff"}, {"%x", ^uint32(0), "ffffffff"},
{"%X", ^uint64(0), "FFFFFFFFFFFFFFFF"}, {"%X", ^uint64(0), "FFFFFFFFFFFFFFFF"},
{"%.20b", 7, "00000000000000000111"}, {"%.20b", 7, "00000000000000000111"},
{"%6.0d", 0, " "},
{"%10d", 12345, " 12345"}, {"%10d", 12345, " 12345"},
{"%10d", -12345, " -12345"}, {"%10d", -12345, " -12345"},
{"%+10d", 12345, " +12345"}, {"%+10d", 12345, " +12345"},
...@@ -354,6 +355,8 @@ var fmtTests = []struct { ...@@ -354,6 +355,8 @@ var fmtTests = []struct {
{"%010d", -12345, "-000012345"}, {"%010d", -12345, "-000012345"},
{"%20.8d", 1234, " 00001234"}, {"%20.8d", 1234, " 00001234"},
{"%20.8d", -1234, " -00001234"}, {"%20.8d", -1234, " -00001234"},
{"%020.8d", 1234, " 00001234"},
{"%020.8d", -1234, " -00001234"},
{"%-20.8d", 1234, "00001234 "}, {"%-20.8d", 1234, "00001234 "},
{"%-20.8d", -1234, "-00001234 "}, {"%-20.8d", -1234, "-00001234 "},
{"%-#20.8x", 0x1234abc, "0x01234abc "}, {"%-#20.8x", 0x1234abc, "0x01234abc "},
...@@ -892,6 +895,7 @@ var fmtTests = []struct { ...@@ -892,6 +895,7 @@ var fmtTests = []struct {
// integer formatting should not alter padding for other elements. // integer formatting should not alter padding for other elements.
{"%03.6v", []interface{}{1, 2.0, "x"}, "[000001 002 00x]"}, {"%03.6v", []interface{}{1, 2.0, "x"}, "[000001 002 00x]"},
{"%03.0v", []interface{}{0, 2.0, "x"}, "[ 002 000]"},
// Complex fmt used to leave the plus flag set for future entries in the array // Complex fmt used to leave the plus flag set for future entries in the array
// causing +2+0i and +3+0i instead of 2+0i and 3+0i. // causing +2+0i and +3+0i instead of 2+0i and 3+0i.
......
...@@ -192,14 +192,6 @@ func (f *fmt) fmt_unicode(u uint64) { ...@@ -192,14 +192,6 @@ func (f *fmt) fmt_unicode(u uint64) {
// fmt_integer formats signed and unsigned integers. // fmt_integer formats signed and unsigned integers.
func (f *fmt) fmt_integer(u uint64, base int, isSigned bool, digits string) { func (f *fmt) fmt_integer(u uint64, base int, isSigned bool, digits string) {
// Precision of 0 and value of 0 means "print nothing" but padding.
if f.precPresent && f.prec == 0 && u == 0 {
if f.widPresent {
f.writePadding(f.wid)
}
return
}
negative := isSigned && int64(u) < 0 negative := isSigned && int64(u) < 0
if negative { if negative {
u = -u u = -u
...@@ -217,11 +209,20 @@ func (f *fmt) fmt_integer(u uint64, base int, isSigned bool, digits string) { ...@@ -217,11 +209,20 @@ func (f *fmt) fmt_integer(u uint64, base int, isSigned bool, digits string) {
} }
} }
// two ways to ask for extra leading zero digits: %.3d or %03d. // Two ways to ask for extra leading zero digits: %.3d or %03d.
// apparently the first cancels the second. // If both are specified the f.zero flag is ignored and
// padding with spaces is used instead.
prec := 0 prec := 0
if f.precPresent { if f.precPresent {
prec = f.prec prec = f.prec
// Precision of 0 and value of 0 means "print nothing" but padding.
if prec == 0 && u == 0 {
oldZero := f.zero
f.zero = false
f.writePadding(f.wid)
f.zero = oldZero
return
}
} else if f.zero && f.widPresent { } else if f.zero && f.widPresent {
prec = f.wid prec = f.wid
if negative || f.plus || f.space { if negative || f.plus || f.space {
...@@ -300,13 +301,11 @@ func (f *fmt) fmt_integer(u uint64, base int, isSigned bool, digits string) { ...@@ -300,13 +301,11 @@ func (f *fmt) fmt_integer(u uint64, base int, isSigned bool, digits string) {
} }
// Left padding with zeros has already been handled like precision earlier // Left padding with zeros has already been handled like precision earlier
// or was overruled by an explicitly set precision. // or the f.zero flag is ignored due to an explicitly set precision.
if f.zero { oldZero := f.zero
f.buf.Write(buf[i:]) f.zero = false
return
}
f.pad(buf[i:]) f.pad(buf[i:])
f.zero = oldZero
} }
// truncate truncates the string to the specified precision, if present. // truncate truncates the string to the specified precision, if present.
......
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