Commit 7472ce0e authored by Rob Pike's avatar Rob Pike

fmt.Printf: introduce notation for random access to arguments.

This text is added to doc.go:

        Explicit argument indexes:

        In Printf, Sprintf, and Fprintf, the default behavior is for each
        formatting verb to format successive arguments passed in the call.
        However, the notation [n] immediately before the verb indicates that the
        nth one-indexed argument is to be formatted instead. The same notation
        before a '*' for a width or precision selects the argument index holding
        the value. After processing a bracketed expression [n], arguments n+1,
        n+2, etc. will be processed unless otherwise directed.

        For example,
                fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
        will yield "22, 11", while
                fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
        equivalent to
                fmt.Sprintf("%6.2f", 12.0),
        will yield " 12.00". Because an explicit index affects subsequent verbs,
        this notation can be used to print the same values multiple times
        by resetting the index for the first argument to be repeated:
                fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
        will yield "16 17 0x10 0x11".

The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.

While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.

R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
parent ffe8a3c5
...@@ -8,4 +8,5 @@ Please keep the descriptions to a single line, starting with the ...@@ -8,4 +8,5 @@ Please keep the descriptions to a single line, starting with the
package or cmd/xxx directory name, and ending in a CL number. package or cmd/xxx directory name, and ending in a CL number.
Please keep the list sorted (as in sort.Strings of the lines). Please keep the list sorted (as in sort.Strings of the lines).
fmt: indexed access to arguments in Printf etc. (CL 9680043).
io: Copy prioritizes WriterTo over ReaderFrom (CL 9462044). io: Copy prioritizes WriterTo over ReaderFrom (CL 9462044).
...@@ -118,6 +118,28 @@ ...@@ -118,6 +118,28 @@
convert the value before recurring: convert the value before recurring:
func (x X) String() string { return Sprintf("<%s>", string(x)) } func (x X) String() string { return Sprintf("<%s>", string(x)) }
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
Format errors: Format errors:
If an invalid argument is given for a verb, such as providing If an invalid argument is given for a verb, such as providing
...@@ -133,6 +155,8 @@ ...@@ -133,6 +155,8 @@
Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC) Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC)
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 out-of-range argument index: %!(BADARGNUM)
Printf("%*[2]d", 7): %d(BADARGNUM)
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
......
...@@ -110,7 +110,7 @@ var bslice = barray[:] ...@@ -110,7 +110,7 @@ var bslice = barray[:]
var b byte var b byte
var fmttests = []struct { var fmtTests = []struct {
fmt string fmt string
val interface{} val interface{}
out string out string
...@@ -503,7 +503,7 @@ var fmttests = []struct { ...@@ -503,7 +503,7 @@ var fmttests = []struct {
} }
func TestSprintf(t *testing.T) { func TestSprintf(t *testing.T) {
for _, tt := range fmttests { for _, tt := range fmtTests {
s := Sprintf(tt.fmt, tt.val) s := Sprintf(tt.fmt, tt.val)
if i := strings.Index(tt.out, "PTR"); i >= 0 { if i := strings.Index(tt.out, "PTR"); i >= 0 {
pattern := "PTR" pattern := "PTR"
...@@ -539,6 +539,42 @@ func TestSprintf(t *testing.T) { ...@@ -539,6 +539,42 @@ func TestSprintf(t *testing.T) {
} }
} }
type SE []interface{} // slice of empty; notational compactness.
var reorderTests = []struct {
fmt string
val SE
out string
}{
{"%[1]d", SE{1}, "1"},
{"%[2]d", SE{2, 1}, "1"},
{"%[2]d %[1]d", SE{1, 2}, "2 1"},
{"%[2]*[1]d", SE{2, 5}, " 2"},
{"%6.2f", SE{12.0}, " 12.00"},
{"%[3]*[2].*[1]f", SE{12.0, 2, 6}, " 12.00"},
{"%[1]*[2].*[3]f", SE{6, 2, 12.0}, " 12.00"},
// An actual use! Print the same arguments twice.
{"%d %d %d %#[1]o %#o %#o", SE{11, 12, 13}, "11 12 13 013 014 015"},
// Erroneous cases.
{"%[]d", SE{2, 1}, "%d(BADARGNUM)"},
{"%[-3]d", SE{2, 1}, "%d(BADARGNUM)"},
{"%[x]d", SE{2, 1}, "%d(BADARGNUM)"},
{"%[23]d", SE{2, 1}, "%d(BADARGNUM)"},
{"%[3]", SE{2, 1}, "%!(NOVERB)"},
{"%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) {
for _, tt := range reorderTests {
s := Sprintf(tt.fmt, tt.val...)
if s != tt.out {
t.Errorf("Sprintf(%q, %v) = <%s> want <%s>", tt.fmt, tt.val, s, tt.out)
} else {
}
}
}
func BenchmarkSprintfEmpty(b *testing.B) { func BenchmarkSprintfEmpty(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Sprintf("") Sprintf("")
......
...@@ -22,6 +22,7 @@ var ( ...@@ -22,6 +22,7 @@ var (
nilBytes = []byte("nil") nilBytes = []byte("nil")
mapBytes = []byte("map[") mapBytes = []byte("map[")
missingBytes = []byte("(MISSING)") missingBytes = []byte("(MISSING)")
badArgNum = []byte("(BADARGNUM)")
panicBytes = []byte("(PANIC=") panicBytes = []byte("(PANIC=")
extraBytes = []byte("%!(EXTRA ") extraBytes = []byte("%!(EXTRA ")
irparenBytes = []byte("i)") irparenBytes = []byte("i)")
...@@ -109,13 +110,17 @@ type pp struct { ...@@ -109,13 +110,17 @@ type pp struct {
panicking bool panicking bool
erroring bool // printing an error condition erroring bool // printing an error condition
buf buffer buf buffer
// field holds the current item, as an interface{}. // arg holds the current item, as an interface{}.
field interface{} arg interface{}
// value holds the current item, as a reflect.Value, and will be // value holds the current item, as a reflect.Value, and will be
// the zero Value if the item has not been reflected. // the zero Value if the item has not been reflected.
value reflect.Value value reflect.Value
runeBuf [utf8.UTFMax]byte // reordered records whether the format string used argument reordering.
fmt fmt reordered bool
// goodArgNum records whether the last reordering directive was valid.
goodArgNum bool
runeBuf [utf8.UTFMax]byte
fmt fmt
} }
// A cache holds a set of reusable objects. // A cache holds a set of reusable objects.
...@@ -170,7 +175,7 @@ func (p *pp) free() { ...@@ -170,7 +175,7 @@ func (p *pp) free() {
return return
} }
p.buf = p.buf[:0] p.buf = p.buf[:0]
p.field = nil p.arg = nil
p.value = reflect.Value{} p.value = reflect.Value{}
ppFree.put(p) ppFree.put(p)
} }
...@@ -212,9 +217,9 @@ func (p *pp) Write(b []byte) (ret int, err error) { ...@@ -212,9 +217,9 @@ func (p *pp) Write(b []byte) (ret int, err error) {
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
p := newPrinter() p := newPrinter()
p.doPrintf(format, a) p.doPrintf(format, a)
n64, err := w.Write(p.buf) n, err = w.Write(p.buf)
p.free() p.free()
return int(n64), err return
} }
// Printf formats according to a format specifier and writes to standard output. // Printf formats according to a format specifier and writes to standard output.
...@@ -246,9 +251,9 @@ func Errorf(format string, a ...interface{}) error { ...@@ -246,9 +251,9 @@ func Errorf(format string, a ...interface{}) error {
func Fprint(w io.Writer, a ...interface{}) (n int, err error) { func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
p := newPrinter() p := newPrinter()
p.doPrint(a, false, false) p.doPrint(a, false, false)
n64, err := w.Write(p.buf) n, err = w.Write(p.buf)
p.free() p.free()
return int(n64), err return
} }
// Print formats using the default formats for its operands and writes to standard output. // Print formats using the default formats for its operands and writes to standard output.
...@@ -278,9 +283,9 @@ func Sprint(a ...interface{}) string { ...@@ -278,9 +283,9 @@ func Sprint(a ...interface{}) string {
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) { func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
p := newPrinter() p := newPrinter()
p.doPrint(a, true, true) p.doPrint(a, true, true)
n64, err := w.Write(p.buf) n, err = w.Write(p.buf)
p.free() p.free()
return int(n64), err return
} }
// Println formats using the default formats for its operands and writes to standard output. // Println formats using the default formats for its operands and writes to standard output.
...@@ -300,8 +305,8 @@ func Sprintln(a ...interface{}) string { ...@@ -300,8 +305,8 @@ func Sprintln(a ...interface{}) string {
return s return s
} }
// getField gets the i'th arg of the struct value. // getField gets the i'th field of the struct value.
// If the arg itself is an interface, return a value for // If the field is itself is an interface, return a value for
// the thing inside the interface, not the interface itself. // the thing inside the interface, not the interface itself.
func getField(v reflect.Value, i int) reflect.Value { func getField(v reflect.Value, i int) reflect.Value {
val := v.Field(i) val := v.Field(i)
...@@ -340,10 +345,10 @@ func (p *pp) badVerb(verb rune) { ...@@ -340,10 +345,10 @@ func (p *pp) badVerb(verb rune) {
p.add(verb) p.add(verb)
p.add('(') p.add('(')
switch { switch {
case p.field != nil: case p.arg != nil:
p.buf.WriteString(reflect.TypeOf(p.field).String()) p.buf.WriteString(reflect.TypeOf(p.arg).String())
p.add('=') p.add('=')
p.printField(p.field, 'v', false, false, 0) p.printArg(p.arg, 'v', false, false, 0)
case p.value.IsValid(): case p.value.IsValid():
p.buf.WriteString(p.value.Type().String()) p.buf.WriteString(p.value.Type().String())
p.add('=') p.add('=')
...@@ -566,7 +571,7 @@ func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, typ reflect.Type, dept ...@@ -566,7 +571,7 @@ func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, typ reflect.Type, dept
p.buf.WriteByte(' ') p.buf.WriteByte(' ')
} }
} }
p.printField(c, 'v', p.fmt.plus, goSyntax, depth+1) p.printArg(c, 'v', p.fmt.plus, goSyntax, depth+1)
} }
if goSyntax { if goSyntax {
p.buf.WriteByte('}') p.buf.WriteByte('}')
...@@ -640,26 +645,26 @@ var ( ...@@ -640,26 +645,26 @@ var (
uintptrBits = reflect.TypeOf(uintptr(0)).Bits() uintptrBits = reflect.TypeOf(uintptr(0)).Bits()
) )
func (p *pp) catchPanic(field interface{}, verb rune) { func (p *pp) catchPanic(arg interface{}, verb rune) {
if err := recover(); err != nil { if err := recover(); err != nil {
// If it's a nil pointer, just say "<nil>". The likeliest causes are a // If it's a nil pointer, just say "<nil>". The likeliest causes are a
// Stringer that fails to guard against nil or a nil pointer for a // Stringer that fails to guard against nil or a nil pointer for a
// value receiver, and in either case, "<nil>" is a nice result. // value receiver, and in either case, "<nil>" is a nice result.
if v := reflect.ValueOf(field); v.Kind() == reflect.Ptr && v.IsNil() { if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() {
p.buf.Write(nilAngleBytes) p.buf.Write(nilAngleBytes)
return return
} }
// Otherwise print a concise panic message. Most of the time the panic // Otherwise print a concise panic message. Most of the time the panic
// value will print itself nicely. // value will print itself nicely.
if p.panicking { if p.panicking {
// Nested panics; the recursion in printField cannot succeed. // Nested panics; the recursion in printArg cannot succeed.
panic(err) panic(err)
} }
p.buf.WriteByte('%') p.buf.WriteByte('%')
p.add(verb) p.add(verb)
p.buf.Write(panicBytes) p.buf.Write(panicBytes)
p.panicking = true p.panicking = true
p.printField(err, 'v', false, false, 0) p.printArg(err, 'v', false, false, 0)
p.panicking = false p.panicking = false
p.buf.WriteByte(')') p.buf.WriteByte(')')
} }
...@@ -670,10 +675,10 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString ...@@ -670,10 +675,10 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString
return return
} }
// Is it a Formatter? // Is it a Formatter?
if formatter, ok := p.field.(Formatter); ok { if formatter, ok := p.arg.(Formatter); ok {
handled = true handled = true
wasString = false wasString = false
defer p.catchPanic(p.field, verb) defer p.catchPanic(p.arg, verb)
formatter.Format(p, verb) formatter.Format(p, verb)
return return
} }
...@@ -682,13 +687,13 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString ...@@ -682,13 +687,13 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString
p.fmt.plus = false p.fmt.plus = false
} }
// If we're doing Go syntax and the field knows how to supply it, take care of it now. // If we're doing Go syntax and the argument knows how to supply it, take care of it now.
if goSyntax { if goSyntax {
p.fmt.sharp = false p.fmt.sharp = false
if stringer, ok := p.field.(GoStringer); ok { if stringer, ok := p.arg.(GoStringer); ok {
wasString = false wasString = false
handled = true handled = true
defer p.catchPanic(p.field, verb) defer p.catchPanic(p.arg, verb)
// Print the result of GoString unadorned. // Print the result of GoString unadorned.
p.fmtString(stringer.GoString(), 's', false) p.fmtString(stringer.GoString(), 's', false)
return return
...@@ -703,19 +708,19 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString ...@@ -703,19 +708,19 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString
// The duplication in the bodies is necessary: // The duplication in the bodies is necessary:
// setting wasString and handled, and deferring catchPanic, // setting wasString and handled, and deferring catchPanic,
// must happen before calling the method. // must happen before calling the method.
switch v := p.field.(type) { switch v := p.arg.(type) {
case error: case error:
wasString = false wasString = false
handled = true handled = true
defer p.catchPanic(p.field, verb) defer p.catchPanic(p.arg, verb)
p.printField(v.Error(), verb, plus, false, depth) p.printArg(v.Error(), verb, plus, false, depth)
return return
case Stringer: case Stringer:
wasString = false wasString = false
handled = true handled = true
defer p.catchPanic(p.field, verb) defer p.catchPanic(p.arg, verb)
p.printField(v.String(), verb, plus, false, depth) p.printArg(v.String(), verb, plus, false, depth)
return return
} }
} }
...@@ -724,11 +729,11 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString ...@@ -724,11 +729,11 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString
return return
} }
func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth int) (wasString bool) { func (p *pp) printArg(arg interface{}, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
p.field = field p.arg = arg
p.value = reflect.Value{} p.value = reflect.Value{}
if field == nil { if arg == nil {
if verb == 'T' || verb == 'v' { if verb == 'T' || verb == 'v' {
p.fmt.pad(nilAngleBytes) p.fmt.pad(nilAngleBytes)
} else { } else {
...@@ -741,10 +746,10 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth ...@@ -741,10 +746,10 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth
// %T (the value's type) and %p (its address) are special; we always do them first. // %T (the value's type) and %p (its address) are special; we always do them first.
switch verb { switch verb {
case 'T': case 'T':
p.printField(reflect.TypeOf(field).String(), 's', false, false, 0) p.printArg(reflect.TypeOf(arg).String(), 's', false, false, 0)
return false return false
case 'p': case 'p':
p.fmtPointer(reflect.ValueOf(field), verb, goSyntax) p.fmtPointer(reflect.ValueOf(arg), verb, goSyntax)
return false return false
} }
...@@ -762,7 +767,7 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth ...@@ -762,7 +767,7 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth
} }
// Some types can be done without reflection. // Some types can be done without reflection.
switch f := field.(type) { switch f := arg.(type) {
case bool: case bool:
p.fmtBool(f, verb) p.fmtBool(f, verb)
case float32: case float32:
...@@ -810,13 +815,13 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth ...@@ -810,13 +815,13 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth
return wasString return wasString
} }
// Need to use reflection // Need to use reflection
return p.printReflectValue(reflect.ValueOf(field), verb, plus, goSyntax, depth) return p.printReflectValue(reflect.ValueOf(arg), verb, plus, goSyntax, depth)
} }
p.field = nil p.arg = nil
return return
} }
// printValue is like printField but starts with a reflect value, not an interface{} value. // printValue is like printArg but starts with a reflect value, not an interface{} value.
func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, depth int) (wasString bool) { func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
if !value.IsValid() { if !value.IsValid() {
if verb == 'T' || verb == 'v' { if verb == 'T' || verb == 'v' {
...@@ -831,7 +836,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, dep ...@@ -831,7 +836,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, dep
// %T (the value's type) and %p (its address) are special; we always do them first. // %T (the value's type) and %p (its address) are special; we always do them first.
switch verb { switch verb {
case 'T': case 'T':
p.printField(value.Type().String(), 's', false, false, 0) p.printArg(value.Type().String(), 's', false, false, 0)
return false return false
case 'p': case 'p':
p.fmtPointer(value, verb, goSyntax) p.fmtPointer(value, verb, goSyntax)
...@@ -839,10 +844,10 @@ func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, dep ...@@ -839,10 +844,10 @@ func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, dep
} }
// Handle values with special methods. // Handle values with special methods.
// Call always, even when field == nil, because handleMethods clears p.fmt.plus for us. // Call always, even when arg == nil, because handleMethods clears p.fmt.plus for us.
p.field = nil // Make sure it's cleared, for safety. p.arg = nil // Make sure it's cleared, for safety.
if value.CanInterface() { if value.CanInterface() {
p.field = value.Interface() p.arg = value.Interface()
} }
if wasString, handled := p.handleMethods(verb, plus, goSyntax, depth); handled { if wasString, handled := p.handleMethods(verb, plus, goSyntax, depth); handled {
return wasString return wasString
...@@ -851,7 +856,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, dep ...@@ -851,7 +856,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, dep
return p.printReflectValue(value, verb, plus, goSyntax, depth) return p.printReflectValue(value, verb, plus, goSyntax, depth)
} }
// printReflectValue is the fallback for both printField and printValue. // printReflectValue is the fallback for both printArg and printValue.
// It uses reflect to print the value. // It uses reflect to print the value.
func (p *pp) printReflectValue(value reflect.Value, verb rune, plus, goSyntax bool, depth int) (wasString bool) { func (p *pp) printReflectValue(value reflect.Value, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
oldValue := p.value oldValue := p.value
...@@ -1015,19 +1020,57 @@ BigSwitch: ...@@ -1015,19 +1020,57 @@ BigSwitch:
return wasString return wasString
} }
// intFromArg gets the fieldnumth element of a. On return, isInt reports whether the argument has type int. // intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has type int.
func intFromArg(a []interface{}, end, i, fieldnum int) (num int, isInt bool, newi, newfieldnum int) { func intFromArg(a []interface{}, end, i, argNum int) (num int, isInt bool, newi, newArgNum int) {
newi, newfieldnum = end, fieldnum newi, newArgNum = end, argNum
if i < end && fieldnum < len(a) { if i < end && argNum < len(a) {
num, isInt = a[fieldnum].(int) num, isInt = a[argNum].(int)
newi, newfieldnum = i+1, fieldnum+1 newi, newArgNum = i+1, argNum+1
} }
return return
} }
// parseArgNumber returns the value of the bracketed number, minus 1
// (explicit argument numbers are one-indexed but we want zero-indexed).
// The opening bracket is known to be present at format[0].
// The returned values are the index, the number of bytes to consume
// up to the closing paren, if present, and whether the number parsed
// ok. The bytes to consume will be 1 if no closing paren is present.
func parseArgNumber(format string) (index int, wid int, ok bool) {
// Find closing parenthesis
for i := 1; i < len(format); i++ {
if format[i] == ']' {
width, ok, newi := parsenum(format, 1, i)
if !ok || newi != i {
return 0, i + 1, false
}
return width - 1, i + 1, true // arg numbers are one-indexed and skip paren.
}
}
return 0, 1, false
}
// argNumber returns the next argument to evaluate, which is either the value of the passed-in
// argNum or the value of the bracketed integer that begins format[i:]. It also returns
// the new value of i, that is, the index of the next byte of the format to process.
func (p *pp) argNumber(argNum int, format string, i int, numArgs int) (newArgNum, newi int) {
p.goodArgNum = true
if len(format) <= i || format[i] != '[' {
return argNum, i
}
p.reordered = true
index, wid, ok := parseArgNumber(format[i:])
if ok && 0 <= index && index < numArgs {
return index, i + wid
}
p.goodArgNum = false
return argNum, i + wid
}
func (p *pp) doPrintf(format string, a []interface{}) { func (p *pp) doPrintf(format string, a []interface{}) {
end := len(format) end := len(format)
fieldnum := 0 // we process one field per non-trivial format argNum := 0 // we process one argument per non-trivial format
p.reordered = false
for i := 0; i < end; { for i := 0; i < end; {
lasti := i lasti := i
for i < end && format[i] != '%' { for i < end && format[i] != '%' {
...@@ -1043,7 +1086,8 @@ func (p *pp) doPrintf(format string, a []interface{}) { ...@@ -1043,7 +1086,8 @@ func (p *pp) doPrintf(format string, a []interface{}) {
// Process one verb // Process one verb
i++ i++
// flags and widths
// Do we have flags?
p.fmt.clearflags() p.fmt.clearflags()
F: F:
for ; i < end; i++ { for ; i < end; i++ {
...@@ -1062,22 +1106,29 @@ func (p *pp) doPrintf(format string, a []interface{}) { ...@@ -1062,22 +1106,29 @@ func (p *pp) doPrintf(format string, a []interface{}) {
break F break F
} }
} }
// do we have width?
// Do we have an explicit argument index?
argNum, i = p.argNumber(argNum, format, i, len(a))
// Do we have width?
if i < end && format[i] == '*' { if i < end && format[i] == '*' {
p.fmt.wid, p.fmt.widPresent, i, fieldnum = intFromArg(a, end, i, fieldnum) p.fmt.wid, p.fmt.widPresent, i, argNum = intFromArg(a, end, i, argNum)
if !p.fmt.widPresent { if !p.fmt.widPresent {
p.buf.Write(badWidthBytes) p.buf.Write(badWidthBytes)
} }
argNum, i = p.argNumber(argNum, format, i, len(a)) // We consumed []; another can follow here.
} else { } else {
p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end) p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end)
} }
// do we have precision?
// Do we have precision?
if i+1 < end && format[i] == '.' { if i+1 < end && format[i] == '.' {
if format[i+1] == '*' { if format[i+1] == '*' {
p.fmt.prec, p.fmt.precPresent, i, fieldnum = intFromArg(a, end, i+1, fieldnum) p.fmt.prec, p.fmt.precPresent, i, argNum = intFromArg(a, end, i+1, argNum)
if !p.fmt.precPresent { if !p.fmt.precPresent {
p.buf.Write(badPrecBytes) p.buf.Write(badPrecBytes)
} }
argNum, i = p.argNumber(argNum, format, i, len(a)) // We consumed []; another can follow here.
} else { } else {
p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i+1, end) p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i+1, end)
if !p.fmt.precPresent { if !p.fmt.precPresent {
...@@ -1097,30 +1148,38 @@ func (p *pp) doPrintf(format string, a []interface{}) { ...@@ -1097,30 +1148,38 @@ func (p *pp) doPrintf(format string, a []interface{}) {
p.buf.WriteByte('%') // We ignore width and prec. p.buf.WriteByte('%') // We ignore width and prec.
continue continue
} }
if fieldnum >= len(a) { // out of operands if !p.goodArgNum {
p.buf.WriteByte('%')
p.add(c)
p.buf.Write(badArgNum)
continue
} else if argNum >= len(a) { // out of operands
p.buf.WriteByte('%') p.buf.WriteByte('%')
p.add(c) p.add(c)
p.buf.Write(missingBytes) p.buf.Write(missingBytes)
continue continue
} }
field := a[fieldnum] arg := a[argNum]
fieldnum++ argNum++
goSyntax := c == 'v' && p.fmt.sharp goSyntax := c == 'v' && p.fmt.sharp
plus := c == 'v' && p.fmt.plus plus := c == 'v' && p.fmt.plus
p.printField(field, c, plus, goSyntax, 0) p.printArg(arg, c, plus, goSyntax, 0)
} }
if fieldnum < len(a) { // Check for extra arguments unless the call accessed the arguments
// out of order, in which case it's too expensive to detect if they've all
// been used and arguably OK if they're not.
if !p.reordered && argNum < len(a) {
p.buf.Write(extraBytes) p.buf.Write(extraBytes)
for ; fieldnum < len(a); fieldnum++ { for ; argNum < len(a); argNum++ {
field := a[fieldnum] arg := a[argNum]
if field != nil { if arg != nil {
p.buf.WriteString(reflect.TypeOf(field).String()) p.buf.WriteString(reflect.TypeOf(arg).String())
p.buf.WriteByte('=') p.buf.WriteByte('=')
} }
p.printField(field, 'v', false, false, 0) p.printArg(arg, 'v', false, false, 0)
if fieldnum+1 < len(a) { if argNum+1 < len(a) {
p.buf.Write(commaSpaceBytes) p.buf.Write(commaSpaceBytes)
} }
} }
...@@ -1130,17 +1189,17 @@ func (p *pp) doPrintf(format string, a []interface{}) { ...@@ -1130,17 +1189,17 @@ func (p *pp) doPrintf(format string, a []interface{}) {
func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) { func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
prevString := false prevString := false
for fieldnum := 0; fieldnum < len(a); fieldnum++ { for argNum := 0; argNum < len(a); argNum++ {
p.fmt.clearflags() p.fmt.clearflags()
// always add spaces if we're doing Println // always add spaces if we're doing Println
field := a[fieldnum] arg := a[argNum]
if fieldnum > 0 { if argNum > 0 {
isString := field != nil && reflect.TypeOf(field).Kind() == reflect.String isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
if addspace || !isString && !prevString { if addspace || !isString && !prevString {
p.buf.WriteByte(' ') p.buf.WriteByte(' ')
} }
} }
prevString = p.printField(field, 'v', false, false, 0) prevString = p.printArg(arg, 'v', false, false, 0)
} }
if addnewline { if addnewline {
p.buf.WriteByte('\n') p.buf.WriteByte('\n')
......
...@@ -168,12 +168,12 @@ type ss struct { ...@@ -168,12 +168,12 @@ type ss struct {
// ssave holds the parts of ss that need to be // ssave holds the parts of ss that need to be
// saved and restored on recursive scans. // saved and restored on recursive scans.
type ssave struct { type ssave struct {
validSave bool // is or was a part of an actual ss. validSave bool // is or was a part of an actual ss.
nlIsEnd bool // whether newline terminates scan nlIsEnd bool // whether newline terminates scan
nlIsSpace bool // whether newline counts as white space nlIsSpace bool // whether newline counts as white space
fieldLimit int // max value of ss.count for this field; fieldLimit <= limit argLimit int // max value of ss.count for this arg; argLimit <= limit
limit int // max value of ss.count. limit int // max value of ss.count.
maxWid int // width of this field. maxWid int // width of this arg.
} }
// The Read method is only in ScanState so that ScanState // The Read method is only in ScanState so that ScanState
...@@ -192,7 +192,7 @@ func (s *ss) ReadRune() (r rune, size int, err error) { ...@@ -192,7 +192,7 @@ func (s *ss) ReadRune() (r rune, size int, err error) {
s.peekRune = -1 s.peekRune = -1
return return
} }
if s.atEOF || s.nlIsEnd && s.prevRune == '\n' || s.count >= s.fieldLimit { if s.atEOF || s.nlIsEnd && s.prevRune == '\n' || s.count >= s.argLimit {
err = io.EOF err = io.EOF
return return
} }
...@@ -389,7 +389,7 @@ func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) { ...@@ -389,7 +389,7 @@ func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
s, ok := r.(*ss) s, ok := r.(*ss)
if ok { if ok {
old = s.ssave old = s.ssave
s.limit = s.fieldLimit s.limit = s.argLimit
s.nlIsEnd = nlIsEnd || s.nlIsEnd s.nlIsEnd = nlIsEnd || s.nlIsEnd
s.nlIsSpace = nlIsSpace s.nlIsSpace = nlIsSpace
return return
...@@ -407,7 +407,7 @@ func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) { ...@@ -407,7 +407,7 @@ func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
s.peekRune = -1 s.peekRune = -1
s.atEOF = false s.atEOF = false
s.limit = hugeWid s.limit = hugeWid
s.fieldLimit = hugeWid s.argLimit = hugeWid
s.maxWid = hugeWid s.maxWid = hugeWid
s.validSave = true s.validSave = true
s.count = 0 s.count = 0
...@@ -477,8 +477,8 @@ func (s *ss) token(skipSpace bool, f func(rune) bool) []byte { ...@@ -477,8 +477,8 @@ func (s *ss) token(skipSpace bool, f func(rune) bool) []byte {
} }
// typeError indicates that the type of the operand did not match the format // typeError indicates that the type of the operand did not match the format
func (s *ss) typeError(field interface{}, expected string) { func (s *ss) typeError(arg interface{}, expected string) {
s.errorString("expected field of type pointer to " + expected + "; found " + reflect.TypeOf(field).String()) s.errorString("expected argument of type pointer to " + expected + "; found " + reflect.TypeOf(arg).String())
} }
var complexError = errors.New("syntax error scanning complex number") var complexError = errors.New("syntax error scanning complex number")
...@@ -927,11 +927,11 @@ const floatVerbs = "beEfFgGv" ...@@ -927,11 +927,11 @@ const floatVerbs = "beEfFgGv"
const hugeWid = 1 << 30 const hugeWid = 1 << 30
// scanOne scans a single value, deriving the scanner from the type of the argument. // scanOne scans a single value, deriving the scanner from the type of the argument.
func (s *ss) scanOne(verb rune, field interface{}) { func (s *ss) scanOne(verb rune, arg interface{}) {
s.buf = s.buf[:0] s.buf = s.buf[:0]
var err error var err error
// If the parameter has its own Scan method, use that. // If the parameter has its own Scan method, use that.
if v, ok := field.(Scanner); ok { if v, ok := arg.(Scanner); ok {
err = v.Scan(s, verb) err = v.Scan(s, verb)
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
...@@ -942,7 +942,7 @@ func (s *ss) scanOne(verb rune, field interface{}) { ...@@ -942,7 +942,7 @@ func (s *ss) scanOne(verb rune, field interface{}) {
return return
} }
switch v := field.(type) { switch v := arg.(type) {
case *bool: case *bool:
*v = s.scanBool(verb) *v = s.scanBool(verb)
case *complex64: case *complex64:
...@@ -1046,8 +1046,8 @@ func errorHandler(errp *error) { ...@@ -1046,8 +1046,8 @@ func errorHandler(errp *error) {
// doScan does the real work for scanning without a format string. // doScan does the real work for scanning without a format string.
func (s *ss) doScan(a []interface{}) (numProcessed int, err error) { func (s *ss) doScan(a []interface{}) (numProcessed int, err error) {
defer errorHandler(&err) defer errorHandler(&err)
for _, field := range a { for _, arg := range a {
s.scanOne('v', field) s.scanOne('v', arg)
numProcessed++ numProcessed++
} }
// Check for newline if required. // Check for newline if required.
...@@ -1144,9 +1144,9 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err erro ...@@ -1144,9 +1144,9 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err erro
if !widPresent { if !widPresent {
s.maxWid = hugeWid s.maxWid = hugeWid
} }
s.fieldLimit = s.limit s.argLimit = s.limit
if f := s.count + s.maxWid; f < s.fieldLimit { if f := s.count + s.maxWid; f < s.argLimit {
s.fieldLimit = f s.argLimit = f
} }
c, w := utf8.DecodeRuneInString(format[i:]) c, w := utf8.DecodeRuneInString(format[i:])
...@@ -1156,11 +1156,11 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err erro ...@@ -1156,11 +1156,11 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err erro
s.errorString("too few operands for format %" + format[i-w:]) s.errorString("too few operands for format %" + format[i-w:])
break break
} }
field := a[numProcessed] arg := a[numProcessed]
s.scanOne(c, field) s.scanOne(c, arg)
numProcessed++ numProcessed++
s.fieldLimit = s.limit s.argLimit = s.limit
} }
if numProcessed < len(a) { if numProcessed < len(a) {
s.errorString("too many operands") s.errorString("too many operands")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment