Skip to content

Commit

Permalink
fmt: avoid badverb formatting for %q when used with integers
Browse files Browse the repository at this point in the history
Instead of returning a bad verb error format for runes above
utf8.Maxrune return a quoted utf8.RuneError rune (\ufffd).
This makes the behaviour consistent with the "c" verb and
aligns behaviour to not return bad verb error format when
a verb is applied to the correct argument type.

Fixes #14569

Change-Id: I679485f6bb90ebe408423ab68af16cce38816cd0
Reviewed-on: https://go-review.googlesource.com/c/go/+/248759
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
  • Loading branch information
martisch committed Aug 17, 2020
1 parent 51ac0f0 commit 99f179f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/fmt/fmt_test.go
Expand Up @@ -290,11 +290,11 @@ var fmtTests = []struct {
{"%q", '\U00000e00', `'\u0e00'`},
{"%q", '\U0010ffff', `'\U0010ffff'`},
// Runes that are not valid.
{"%q", int32(-1), "%!q(int32=-1)"},
{"%q", int32(-1), `'�'`},
{"%q", 0xDC80, `'�'`},
{"%q", rune(0x110000), "%!q(int32=1114112)"},
{"%q", int64(0xFFFFFFFFF), "%!q(int64=68719476735)"},
{"%q", uint64(0xFFFFFFFFF), "%!q(uint64=68719476735)"},
{"%q", rune(0x110000), `'�'`},
{"%q", int64(0xFFFFFFFFF), `'�'`},
{"%q", uint64(0xFFFFFFFFF), `'�'`},

// width
{"%5s", "abc", " abc"},
Expand Down
6 changes: 1 addition & 5 deletions src/fmt/print.go
Expand Up @@ -388,11 +388,7 @@ func (p *pp) fmtInteger(v uint64, isSigned bool, verb rune) {
case 'c':
p.fmt.fmtC(v)
case 'q':
if v <= utf8.MaxRune {
p.fmt.fmtQc(v)
} else {
p.badVerb(verb)
}
p.fmt.fmtQc(v)
case 'U':
p.fmt.fmtUnicode(v)
default:
Expand Down

0 comments on commit 99f179f

Please sign in to comment.