diff options
| author | Rob Pike <r@golang.org> | 2024-01-14 15:34:49 +1100 |
|---|---|---|
| committer | M Zhuo <mzh@golangcn.org> | 2024-01-23 08:29:53 +0000 |
| commit | 704401ffa06c60e059c9e6e4048045b4ff42530a (patch) | |
| tree | bd0f2c5c0cbefd28fe8640f1ca6515799941c9c3 | |
| parent | adead1a93f472affa97c494ef19f2f492ee6f34a (diff) | |
| download | go-704401ffa06c60e059c9e6e4048045b4ff42530a.tar.xz | |
fmt: don't pad strings with zeros
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>
| -rw-r--r-- | src/fmt/fmt_test.go | 4 | ||||
| -rw-r--r-- | src/fmt/print.go | 5 |
2 files changed, 7 insertions, 2 deletions
diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go index 6a79862f28..3f82fb6814 100644 --- a/src/fmt/fmt_test.go +++ b/src/fmt/fmt_test.go @@ -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"}, diff --git a/src/fmt/print.go b/src/fmt/print.go index cb393bd763..9596888854 100644 --- a/src/fmt/print.go +++ b/src/fmt/print.go @@ -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: |
