Skip to content

Commit 704401f

Browse files
robpikemengzhuo
authored andcommittedJan 23, 2024
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>
1 parent adead1a commit 704401f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed
 

Diff for: ‎src/fmt/fmt_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ var fmtTests = []struct {
304304
{"%2s", []byte("\u263a"), " ☺"},
305305
{"%-5s", "abc", "abc "},
306306
{"%-5s", []byte("abc"), "abc "},
307-
{"%05s", "abc", "00abc"},
308-
{"%05s", []byte("abc"), "00abc"},
307+
{"%05s", "abc", " abc"},
308+
{"%05s", []byte("abc"), " abc"},
309309
{"%5s", "abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"},
310310
{"%5s", []byte("abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz"},
311311
{"%.5s", "abcdefghijklmnopqrstuvwxyz", "abcde"},

Diff for: ‎src/fmt/print.go

+5
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,11 @@ func (p *pp) printArg(arg any, verb rune) {
703703
return
704704
}
705705

706+
// Bug fix: avoid padding strings with zeros. Issue 56486.
707+
if verb == 's' {
708+
p.fmt.zero = false
709+
}
710+
706711
// Some types can be done without reflection.
707712
switch f := arg.(type) {
708713
case bool:

0 commit comments

Comments
 (0)
Please sign in to comment.