mirror of https://github.com/golang/go.git
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: Rob Pike <r@golang.org>
This commit is contained in:
parent
d07709ed7b
commit
a85a224e8e
|
|
@ -328,6 +328,8 @@ var fmtTests = []struct {
|
|||
{"%d", int64(-1 << 63), "-9223372036854775808"},
|
||||
{"%.d", 0, ""},
|
||||
{"%.0d", 0, ""},
|
||||
{"%6.0d", 0, " "},
|
||||
{"%06.0d", 0, " "},
|
||||
{"% d", 12345, " 12345"},
|
||||
{"%+d", 12345, "+12345"},
|
||||
{"%+d", -12345, "-12345"},
|
||||
|
|
@ -346,7 +348,6 @@ var fmtTests = []struct {
|
|||
{"%x", ^uint32(0), "ffffffff"},
|
||||
{"%X", ^uint64(0), "FFFFFFFFFFFFFFFF"},
|
||||
{"%.20b", 7, "00000000000000000111"},
|
||||
{"%6.0d", 0, " "},
|
||||
{"%10d", 12345, " 12345"},
|
||||
{"%10d", -12345, " -12345"},
|
||||
{"%+10d", 12345, " +12345"},
|
||||
|
|
@ -354,6 +355,8 @@ var fmtTests = []struct {
|
|||
{"%010d", -12345, "-000012345"},
|
||||
{"%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.8x", 0x1234abc, "0x01234abc "},
|
||||
|
|
@ -892,6 +895,7 @@ var fmtTests = []struct {
|
|||
|
||||
// integer formatting should not alter padding for other elements.
|
||||
{"%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
|
||||
// causing +2+0i and +3+0i instead of 2+0i and 3+0i.
|
||||
|
|
|
|||
|
|
@ -192,14 +192,6 @@ func (f *fmt) fmt_unicode(u uint64) {
|
|||
|
||||
// fmt_integer formats signed and unsigned integers.
|
||||
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
|
||||
if negative {
|
||||
u = -u
|
||||
|
|
@ -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.
|
||||
// apparently the first cancels the second.
|
||||
// Two ways to ask for extra leading zero digits: %.3d or %03d.
|
||||
// If both are specified the f.zero flag is ignored and
|
||||
// padding with spaces is used instead.
|
||||
prec := 0
|
||||
if f.precPresent {
|
||||
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 {
|
||||
prec = f.wid
|
||||
if negative || f.plus || f.space {
|
||||
|
|
@ -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
|
||||
// or was overruled by an explicitly set precision.
|
||||
if f.zero {
|
||||
f.buf.Write(buf[i:])
|
||||
return
|
||||
}
|
||||
|
||||
// or the f.zero flag is ignored due to an explicitly set precision.
|
||||
oldZero := f.zero
|
||||
f.zero = false
|
||||
f.pad(buf[i:])
|
||||
f.zero = oldZero
|
||||
}
|
||||
|
||||
// truncate truncates the string to the specified precision, if present.
|
||||
|
|
|
|||
Loading…
Reference in New Issue