]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: don't pad strings with zeros
authorRob Pike <r@golang.org>
Sun, 14 Jan 2024 04:34:49 +0000 (15:34 +1100)
committerM Zhuo <mzh@golangcn.org>
Tue, 23 Jan 2024 08:29:53 +0000 (08:29 +0000)
It's what the documentation says, and oddly it already behaves correctly
for right padding, not left. (We never pad with zeros on the right.)

Just don't do it.

Fixes #56486

Change-Id: I2465edea93c69084e33bee0d945d5a1b85e6cd14
Reviewed-on: https://go-review.googlesource.com/c/go/+/555776
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/fmt/fmt_test.go
src/fmt/print.go

index 6a79862f285f331545becb8fe015bbbd06ae80c9..3f82fb681445b719c1f0a1918e652396ab98a5e1 100644 (file)
@@ -304,8 +304,8 @@ var fmtTests = []struct {
        {"%2s", []byte("\u263a"), " ☺"},
        {"%-5s", "abc", "abc  "},
        {"%-5s", []byte("abc"), "abc  "},
-       {"%05s", "abc", "00abc"},
-       {"%05s", []byte("abc"), "00abc"},
+       {"%05s", "abc", "  abc"},
+       {"%05s", []byte("abc"), "  abc"},
        {"%5s", "abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"},
        {"%5s", []byte("abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz"},
        {"%.5s", "abcdefghijklmnopqrstuvwxyz", "abcde"},
index cb393bd76370daa72b9af3bb8cdd2394787451bc..9596888854763af74e533abbfb187906c887d91b 100644 (file)
@@ -703,6 +703,11 @@ func (p *pp) printArg(arg any, verb rune) {
                return
        }
 
+       // Bug fix: avoid padding strings with zeros. Issue 56486.
+       if verb == 's' {
+               p.fmt.zero = false
+       }
+
        // Some types can be done without reflection.
        switch f := arg.(type) {
        case bool: