Skip to content

Commit

Permalink
fmt: don't pad strings with zeros
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
robpike authored and mengzhuo committed Jan 23, 2024
1 parent adead1a commit 704401f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/fmt/fmt_test.go
Expand Up @@ -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"},
Expand Down
5 changes: 5 additions & 0 deletions src/fmt/print.go
Expand Up @@ -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:
Expand Down

0 comments on commit 704401f

Please sign in to comment.