]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: allow padding and minus flags at the same time
authorMitar <mitar.git@tnode.com>
Fri, 1 Mar 2024 23:43:04 +0000 (23:43 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 4 Mar 2024 17:31:55 +0000 (17:31 +0000)
Existing implementation did not allow setting both padding and minus flags at the same time because standard formatting does not allow that. But custom Formatter interface implementations might have use of it. This change moves the check from the place flags are parsed to where they are used in standard formatting.

Fixes #61784

Change-Id: If5909d45dc929ddf911453e1056a4661abe76e52
GitHub-Last-Rev: d99ec55d3bbd9b2a8f14c8ade2fb25d6e0c174c3
GitHub-Pull-Request: golang/go#61836
Reviewed-on: https://go-review.googlesource.com/c/go/+/516975
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Martin Möhrmann <martin@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>

src/fmt/fmt_test.go
src/fmt/format.go
src/fmt/print.go

index 6a79862f285f331545becb8fe015bbbd06ae80c9..0f4a35dd6fe54095fdf91b889a5dcbec74475231 100644 (file)
@@ -1501,6 +1501,7 @@ var flagtests = []struct {
        {"%-+1.2a", "[%+-1.2a]"},
        {"%-+1.2abc", "[%+-1.2a]bc"},
        {"%-1.2abc", "[%-1.2a]bc"},
+       {"%-0abc", "[%-0a]bc"},
 }
 
 func TestFlagParser(t *testing.T) {
@@ -1827,6 +1828,7 @@ var formatterFlagTests = []struct {
        {"%-+1.2a", flagPrinter{}, "[%+-1.2a]"},
        {"%-+1.2abc", flagPrinter{}, "[%+-1.2a]bc"},
        {"%-1.2abc", flagPrinter{}, "[%-1.2a]bc"},
+       {"%-0abc", flagPrinter{}, "[%-0a]bc"},
 
        // composite values with the 'a' verb
        {"%a", [1]flagPrinter{}, "[[%a]]"},
@@ -1841,6 +1843,7 @@ var formatterFlagTests = []struct {
        {"%-+1.2a", [1]flagPrinter{}, "[[%+-1.2a]]"},
        {"%-+1.2abc", [1]flagPrinter{}, "[[%+-1.2a]]bc"},
        {"%-1.2abc", [1]flagPrinter{}, "[[%-1.2a]]bc"},
+       {"%-0abc", [1]flagPrinter{}, "[[%-0a]]bc"},
 
        // simple values with the 'v' verb
        {"%v", flagPrinter{}, "[%v]"},
@@ -1855,6 +1858,7 @@ var formatterFlagTests = []struct {
        {"%-+1.2v", flagPrinter{}, "[%+-1.2v]"},
        {"%-+1.2vbc", flagPrinter{}, "[%+-1.2v]bc"},
        {"%-1.2vbc", flagPrinter{}, "[%-1.2v]bc"},
+       {"%-0vbc", flagPrinter{}, "[%-0v]bc"},
 
        // composite values with the 'v' verb.
        {"%v", [1]flagPrinter{}, "[[%v]]"},
@@ -1869,6 +1873,7 @@ var formatterFlagTests = []struct {
        {"%-+1.2v", [1]flagPrinter{}, "[[%+-1.2v]]"},
        {"%-+1.2vbc", [1]flagPrinter{}, "[[%+-1.2v]]bc"},
        {"%-1.2vbc", [1]flagPrinter{}, "[[%-1.2v]]bc"},
+       {"%-0vbc", [1]flagPrinter{}, "[[%-0v]]bc"},
 }
 
 func TestFormatterFlags(t *testing.T) {
index b497ad0f1b4742a9c3c5c4a293e04540a6e4f92f..90e18cd696375f3288833a32de5f2983de46093a 100644 (file)
@@ -77,7 +77,8 @@ func (f *fmt) writePadding(n int) {
        }
        // Decide which byte the padding should be filled with.
        padByte := byte(' ')
-       if f.zero {
+       // Zero padding is allowed only to the left.
+       if f.zero && !f.minus {
                padByte = byte('0')
        }
        // Fill padding with padByte.
@@ -225,7 +226,7 @@ func (f *fmt) fmtInteger(u uint64, base int, isSigned bool, verb rune, digits st
                        f.zero = oldZero
                        return
                }
-       } else if f.zero && f.widPresent {
+       } else if f.zero && !f.minus && f.widPresent { // Zero padding is allowed only to the left.
                prec = f.wid
                if negative || f.plus || f.space {
                        prec-- // leave room for sign
@@ -582,7 +583,8 @@ func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int) {
        if f.plus || num[0] != '+' {
                // If we're zero padding to the left we want the sign before the leading zeros.
                // Achieve this by writing the sign out and then padding the unsigned number.
-               if f.zero && f.widPresent && f.wid > len(num) {
+               // Zero padding is allowed only to the left.
+               if f.zero && !f.minus && f.widPresent && f.wid > len(num) {
                        f.buf.writeByte(num[0])
                        f.writePadding(f.wid - len(num))
                        f.buf.write(num[1:])
index cb393bd76370daa72b9af3bb8cdd2394787451bc..274e635923c7537a511eeb3f20fd83f4f127c055 100644 (file)
@@ -1048,12 +1048,11 @@ formatLoop:
                        case '#':
                                p.fmt.sharp = true
                        case '0':
-                               p.fmt.zero = !p.fmt.minus // Only allow zero padding to the left.
+                               p.fmt.zero = true
                        case '+':
                                p.fmt.plus = true
                        case '-':
                                p.fmt.minus = true
-                               p.fmt.zero = false // Do not pad with zeros to the right.
                        case ' ':
                                p.fmt.space = true
                        default: