From: Martin Möhrmann Date: Mon, 20 Jul 2020 05:57:06 +0000 (+0200) Subject: fmt: avoid badverb formatting for %q when used with integers X-Git-Tag: go1.16beta1~1339 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=99f179f55a66f35dc7861fa411b42ed61bd0df31;p=gostls13.git fmt: avoid badverb formatting for %q when used with integers 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 TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go index 6004061020..87fb323809 100644 --- a/src/fmt/fmt_test.go +++ b/src/fmt/fmt_test.go @@ -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"}, diff --git a/src/fmt/print.go b/src/fmt/print.go index 595869140a..778b5b0938 100644 --- a/src/fmt/print.go +++ b/src/fmt/print.go @@ -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: