]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: avoid badverb formatting for %q when used with integers
authorMartin Möhrmann <moehrmann@google.com>
Mon, 20 Jul 2020 05:57:06 +0000 (07:57 +0200)
committerMartin Möhrmann <moehrmann@google.com>
Mon, 17 Aug 2020 04:55:28 +0000 (04:55 +0000)
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>
src/fmt/fmt_test.go
src/fmt/print.go

index 6004061020577cbf21bbc6c89648432f099ec70c..87fb32380946ee6aee54b98fe7c424b32468fa12 100644 (file)
@@ -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"},
index 595869140a1f9e619745274048182aca104cd0b7..778b5b0938f9babaa30aa19a3ed92b23a52dafaf 100644 (file)
@@ -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: