1
0
mirror of https://github.com/golang/go synced 2024-11-26 14:08:37 -07:00

fmt: make all errors begin with the string "%!", always.

Fixes #5730.

R=dsymonds, r, kamil.kisiel
CC=golang-dev
https://golang.org/cl/11998044
This commit is contained in:
Robert Daniel Kortschak 2013-07-31 16:11:12 +10:00 committed by Rob Pike
parent aa38aeaeaf
commit 53b61057b2
3 changed files with 37 additions and 36 deletions

View File

@ -156,8 +156,8 @@
Printf("%*s", 4.5, "hi"): %!(BADWIDTH)hi Printf("%*s", 4.5, "hi"): %!(BADWIDTH)hi
Printf("%.*s", 4.5, "hi"): %!(BADPREC)hi Printf("%.*s", 4.5, "hi"): %!(BADPREC)hi
Invalid or invalid use of argument index: %!(BADINDEX) Invalid or invalid use of argument index: %!(BADINDEX)
Printf("%*[2]d", 7): %d!(BADINDEX) Printf("%*[2]d", 7): %!d(BADINDEX)
Printf("%.[2]d", 7): %d!(BADINDEX) Printf("%.[2]d", 7): %!d(BADINDEX)
All errors begin with the string "%!" followed sometimes All errors begin with the string "%!" followed sometimes
by a single character (the verb) and end with a parenthesized by a single character (the verb) and end with a parenthesized
@ -169,9 +169,9 @@
through the fmt package. For example, if a String method through the fmt package. For example, if a String method
calls panic("bad"), the resulting formatted message will look calls panic("bad"), the resulting formatted message will look
like like
%s(PANIC=bad) %!s(PANIC=bad)
The %s just shows the print verb in use when the failure The %!s just shows the print verb in use when the failure
occurred. occurred.
Scanning Scanning

View File

@ -563,17 +563,17 @@ var reorderTests = []struct {
{"%d %d %d %#[1]o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015"}, {"%d %d %d %#[1]o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015"},
// Erroneous cases. // Erroneous cases.
{"%[d", SE{2, 1}, "%d!(BADINDEX)"}, {"%[d", SE{2, 1}, "%!d(BADINDEX)"},
{"%]d", SE{2, 1}, "%!](int=2)d%!(EXTRA int=1)"}, {"%]d", SE{2, 1}, "%!](int=2)d%!(EXTRA int=1)"},
{"%[]d", SE{2, 1}, "%d!(BADINDEX)"}, {"%[]d", SE{2, 1}, "%!d(BADINDEX)"},
{"%[-3]d", SE{2, 1}, "%d!(BADINDEX)"}, {"%[-3]d", SE{2, 1}, "%!d(BADINDEX)"},
{"%[99]d", SE{2, 1}, "%d!(BADINDEX)"}, {"%[99]d", SE{2, 1}, "%!d(BADINDEX)"},
{"%[3]", SE{2, 1}, "%!(NOVERB)"}, {"%[3]", SE{2, 1}, "%!(NOVERB)"},
{"%[1].2d", SE{5, 6}, "%d!(BADINDEX)"}, {"%[1].2d", SE{5, 6}, "%!d(BADINDEX)"},
{"%[1]2d", SE{2, 1}, "%d!(BADINDEX)"}, {"%[1]2d", SE{2, 1}, "%!d(BADINDEX)"},
{"%3.[2]d", SE{7}, "%d!(BADINDEX)"}, {"%3.[2]d", SE{7}, "%!d(BADINDEX)"},
{"%.[2]d", SE{7}, "%d!(BADINDEX)"}, {"%.[2]d", SE{7}, "%!d(BADINDEX)"},
{"%d %d %d %#[1]o %#o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015 %o!(MISSING)"}, {"%d %d %d %#[1]o %#o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015 %!o(MISSING)"},
} }
func TestReorder(t *testing.T) { func TestReorder(t *testing.T) {
@ -877,16 +877,16 @@ var panictests = []struct {
}{ }{
// String // String
{"%s", (*Panic)(nil), "<nil>"}, // nil pointer special case {"%s", (*Panic)(nil), "<nil>"}, // nil pointer special case
{"%s", Panic{io.ErrUnexpectedEOF}, "%s!(PANIC=unexpected EOF)"}, {"%s", Panic{io.ErrUnexpectedEOF}, "%!s(PANIC=unexpected EOF)"},
{"%s", Panic{3}, "%s!(PANIC=3)"}, {"%s", Panic{3}, "%!s(PANIC=3)"},
// GoString // GoString
{"%#v", (*Panic)(nil), "<nil>"}, // nil pointer special case {"%#v", (*Panic)(nil), "<nil>"}, // nil pointer special case
{"%#v", Panic{io.ErrUnexpectedEOF}, "%v!(PANIC=unexpected EOF)"}, {"%#v", Panic{io.ErrUnexpectedEOF}, "%!v(PANIC=unexpected EOF)"},
{"%#v", Panic{3}, "%v!(PANIC=3)"}, {"%#v", Panic{3}, "%!v(PANIC=3)"},
// Format // Format
{"%s", (*PanicF)(nil), "<nil>"}, // nil pointer special case {"%s", (*PanicF)(nil), "<nil>"}, // nil pointer special case
{"%s", PanicF{io.ErrUnexpectedEOF}, "%s!(PANIC=unexpected EOF)"}, {"%s", PanicF{io.ErrUnexpectedEOF}, "%!s(PANIC=unexpected EOF)"},
{"%s", PanicF{3}, "%s!(PANIC=3)"}, {"%s", PanicF{3}, "%!s(PANIC=3)"},
} }
func TestPanics(t *testing.T) { func TestPanics(t *testing.T) {

View File

@ -16,20 +16,21 @@ import (
// Some constants in the form of bytes, to avoid string overhead. // Some constants in the form of bytes, to avoid string overhead.
// Needlessly fastidious, I suppose. // Needlessly fastidious, I suppose.
var ( var (
commaSpaceBytes = []byte(", ") commaSpaceBytes = []byte(", ")
nilAngleBytes = []byte("<nil>") nilAngleBytes = []byte("<nil>")
nilParenBytes = []byte("(nil)") nilParenBytes = []byte("(nil)")
nilBytes = []byte("nil") nilBytes = []byte("nil")
mapBytes = []byte("map[") mapBytes = []byte("map[")
missingBytes = []byte("!(MISSING)") percentBangBytes = []byte("%!")
badIndexBytes = []byte("!(BADINDEX)") missingBytes = []byte("(MISSING)")
panicBytes = []byte("!(PANIC=") badIndexBytes = []byte("(BADINDEX)")
extraBytes = []byte("%!(EXTRA ") panicBytes = []byte("(PANIC=")
irparenBytes = []byte("i)") extraBytes = []byte("%!(EXTRA ")
bytesBytes = []byte("[]byte{") irparenBytes = []byte("i)")
badWidthBytes = []byte("%!(BADWIDTH)") bytesBytes = []byte("[]byte{")
badPrecBytes = []byte("%!(BADPREC)") badWidthBytes = []byte("%!(BADWIDTH)")
noVerbBytes = []byte("%!(NOVERB)") badPrecBytes = []byte("%!(BADPREC)")
noVerbBytes = []byte("%!(NOVERB)")
) )
// State represents the printer state passed to custom formatters. // State represents the printer state passed to custom formatters.
@ -660,7 +661,7 @@ func (p *pp) catchPanic(arg interface{}, verb rune) {
// Nested panics; the recursion in printArg cannot succeed. // Nested panics; the recursion in printArg cannot succeed.
panic(err) panic(err)
} }
p.buf.WriteByte('%') p.buf.Write(percentBangBytes)
p.add(verb) p.add(verb)
p.buf.Write(panicBytes) p.buf.Write(panicBytes)
p.panicking = true p.panicking = true
@ -1165,12 +1166,12 @@ func (p *pp) doPrintf(format string, a []interface{}) {
continue continue
} }
if !p.goodArgNum { if !p.goodArgNum {
p.buf.WriteByte('%') p.buf.Write(percentBangBytes)
p.add(c) p.add(c)
p.buf.Write(badIndexBytes) p.buf.Write(badIndexBytes)
continue continue
} else if argNum >= len(a) { // out of operands } else if argNum >= len(a) { // out of operands
p.buf.WriteByte('%') p.buf.Write(percentBangBytes)
p.add(c) p.add(c)
p.buf.Write(missingBytes) p.buf.Write(missingBytes)
continue continue