2012-03-27 23:13:14 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package fmt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Some constants in the form of bytes, to avoid string overhead.
|
|
|
|
// Needlessly fastidious, I suppose.
|
|
|
|
var (
|
2014-09-21 17:33:12 +00:00
|
|
|
commaSpaceBytes = []byte(", ")
|
|
|
|
nilAngleBytes = []byte("<nil>")
|
|
|
|
nilParenBytes = []byte("(nil)")
|
|
|
|
nilBytes = []byte("nil")
|
|
|
|
mapBytes = []byte("map[")
|
|
|
|
percentBangBytes = []byte("%!")
|
|
|
|
missingBytes = []byte("(MISSING)")
|
|
|
|
badIndexBytes = []byte("(BADINDEX)")
|
|
|
|
panicBytes = []byte("(PANIC=")
|
|
|
|
extraBytes = []byte("%!(EXTRA ")
|
|
|
|
irparenBytes = []byte("i)")
|
|
|
|
bytesBytes = []byte("[]byte{")
|
|
|
|
badWidthBytes = []byte("%!(BADWIDTH)")
|
|
|
|
badPrecBytes = []byte("%!(BADPREC)")
|
|
|
|
noVerbBytes = []byte("%!(NOVERB)")
|
2012-03-27 23:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// State represents the printer state passed to custom formatters.
|
|
|
|
// It provides access to the io.Writer interface plus information about
|
|
|
|
// the flags and options for the operand's format specifier.
|
|
|
|
type State interface {
|
|
|
|
// Write is the function to call to emit formatted output to be printed.
|
|
|
|
Write(b []byte) (ret int, err error)
|
|
|
|
// Width returns the value of the width option and whether it has been set.
|
|
|
|
Width() (wid int, ok bool)
|
|
|
|
// Precision returns the value of the precision option and whether it has been set.
|
|
|
|
Precision() (prec int, ok bool)
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// Flag reports whether the flag c, a character, has been set.
|
2012-03-27 23:13:14 +00:00
|
|
|
Flag(c int) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formatter is the interface implemented by values with a custom formatter.
|
2014-09-21 17:33:12 +00:00
|
|
|
// The implementation of Format may call Sprint(f) or Fprint(f) etc.
|
2012-03-27 23:13:14 +00:00
|
|
|
// to generate its output.
|
|
|
|
type Formatter interface {
|
|
|
|
Format(f State, c rune)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stringer is implemented by any value that has a String method,
|
|
|
|
// which defines the ``native'' format for that value.
|
|
|
|
// The String method is used to print values passed as an operand
|
2014-09-21 17:33:12 +00:00
|
|
|
// to any format that accepts a string or to an unformatted printer
|
|
|
|
// such as Print.
|
2012-03-27 23:13:14 +00:00
|
|
|
type Stringer interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GoStringer is implemented by any value that has a GoString method,
|
|
|
|
// which defines the Go syntax for that value.
|
|
|
|
// The GoString method is used to print values passed as an operand
|
|
|
|
// to a %#v format.
|
|
|
|
type GoStringer interface {
|
|
|
|
GoString() string
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// Use simple []byte instead of bytes.Buffer to avoid large dependency.
|
|
|
|
type buffer []byte
|
|
|
|
|
|
|
|
func (b *buffer) Write(p []byte) (n int, err error) {
|
|
|
|
*b = append(*b, p...)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) WriteString(s string) (n int, err error) {
|
|
|
|
*b = append(*b, s...)
|
|
|
|
return len(s), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) WriteByte(c byte) error {
|
|
|
|
*b = append(*b, c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *buffer) WriteRune(r rune) error {
|
|
|
|
if r < utf8.RuneSelf {
|
|
|
|
*bp = append(*bp, byte(r))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := *bp
|
|
|
|
n := len(b)
|
|
|
|
for n+utf8.UTFMax > cap(b) {
|
|
|
|
b = append(b, 0)
|
|
|
|
}
|
|
|
|
w := utf8.EncodeRune(b[n:n+utf8.UTFMax], r)
|
|
|
|
*bp = b[:n+w]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
type pp struct {
|
|
|
|
n int
|
|
|
|
panicking bool
|
|
|
|
erroring bool // printing an error condition
|
2014-09-21 17:33:12 +00:00
|
|
|
buf buffer
|
|
|
|
// arg holds the current item, as an interface{}.
|
|
|
|
arg interface{}
|
2012-03-27 23:13:14 +00:00
|
|
|
// value holds the current item, as a reflect.Value, and will be
|
|
|
|
// the zero Value if the item has not been reflected.
|
2014-09-21 17:33:12 +00:00
|
|
|
value reflect.Value
|
|
|
|
// reordered records whether the format string used argument reordering.
|
|
|
|
reordered bool
|
|
|
|
// goodArgNum records whether the most recent reordering directive was valid.
|
|
|
|
goodArgNum bool
|
|
|
|
runeBuf [utf8.UTFMax]byte
|
|
|
|
fmt fmt
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
var ppFree = sync.Pool{
|
|
|
|
New: func() interface{} { return new(pp) },
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
// newPrinter allocates a new pp struct or grabs a cached one.
|
2012-03-27 23:13:14 +00:00
|
|
|
func newPrinter() *pp {
|
2015-08-28 15:33:40 +00:00
|
|
|
p := ppFree.Get().(*pp)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.panicking = false
|
|
|
|
p.erroring = false
|
|
|
|
p.fmt.init(&p.buf)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// free saves used pp structs in ppFree; avoids an allocation per invocation.
|
2012-03-27 23:13:14 +00:00
|
|
|
func (p *pp) free() {
|
|
|
|
// Don't hold on to pp structs with large buffers.
|
2014-09-21 17:33:12 +00:00
|
|
|
if cap(p.buf) > 1024 {
|
2012-03-27 23:13:14 +00:00
|
|
|
return
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
p.buf = p.buf[:0]
|
|
|
|
p.arg = nil
|
2012-03-27 23:13:14 +00:00
|
|
|
p.value = reflect.Value{}
|
2015-08-28 15:33:40 +00:00
|
|
|
ppFree.Put(p)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) Width() (wid int, ok bool) { return p.fmt.wid, p.fmt.widPresent }
|
|
|
|
|
|
|
|
func (p *pp) Precision() (prec int, ok bool) { return p.fmt.prec, p.fmt.precPresent }
|
|
|
|
|
|
|
|
func (p *pp) Flag(b int) bool {
|
|
|
|
switch b {
|
|
|
|
case '-':
|
|
|
|
return p.fmt.minus
|
|
|
|
case '+':
|
|
|
|
return p.fmt.plus
|
|
|
|
case '#':
|
|
|
|
return p.fmt.sharp
|
|
|
|
case ' ':
|
|
|
|
return p.fmt.space
|
|
|
|
case '0':
|
|
|
|
return p.fmt.zero
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) add(c rune) {
|
|
|
|
p.buf.WriteRune(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement Write so we can call Fprintf on a pp (through State), for
|
|
|
|
// recursive use in custom verbs.
|
|
|
|
func (p *pp) Write(b []byte) (ret int, err error) {
|
|
|
|
return p.buf.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// These routines end in 'f' and take a format string.
|
|
|
|
|
|
|
|
// Fprintf formats according to a format specifier and writes to w.
|
|
|
|
// It returns the number of bytes written and any write error encountered.
|
|
|
|
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
|
|
|
|
p := newPrinter()
|
|
|
|
p.doPrintf(format, a)
|
2014-09-21 17:33:12 +00:00
|
|
|
n, err = w.Write(p.buf)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.free()
|
2014-09-21 17:33:12 +00:00
|
|
|
return
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Printf formats according to a format specifier and writes to standard output.
|
|
|
|
// It returns the number of bytes written and any write error encountered.
|
|
|
|
func Printf(format string, a ...interface{}) (n int, err error) {
|
|
|
|
return Fprintf(os.Stdout, format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sprintf formats according to a format specifier and returns the resulting string.
|
|
|
|
func Sprintf(format string, a ...interface{}) string {
|
|
|
|
p := newPrinter()
|
|
|
|
p.doPrintf(format, a)
|
2014-09-21 17:33:12 +00:00
|
|
|
s := string(p.buf)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.free()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// Errorf formats according to a format specifier and returns the string
|
2012-03-27 23:13:14 +00:00
|
|
|
// as a value that satisfies error.
|
|
|
|
func Errorf(format string, a ...interface{}) error {
|
|
|
|
return errors.New(Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// These routines do not take a format string
|
|
|
|
|
|
|
|
// Fprint formats using the default formats for its operands and writes to w.
|
|
|
|
// Spaces are added between operands when neither is a string.
|
|
|
|
// It returns the number of bytes written and any write error encountered.
|
|
|
|
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
|
|
|
|
p := newPrinter()
|
|
|
|
p.doPrint(a, false, false)
|
2014-09-21 17:33:12 +00:00
|
|
|
n, err = w.Write(p.buf)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.free()
|
2014-09-21 17:33:12 +00:00
|
|
|
return
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print formats using the default formats for its operands and writes to standard output.
|
|
|
|
// Spaces are added between operands when neither is a string.
|
|
|
|
// It returns the number of bytes written and any write error encountered.
|
|
|
|
func Print(a ...interface{}) (n int, err error) {
|
|
|
|
return Fprint(os.Stdout, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sprint formats using the default formats for its operands and returns the resulting string.
|
|
|
|
// Spaces are added between operands when neither is a string.
|
|
|
|
func Sprint(a ...interface{}) string {
|
|
|
|
p := newPrinter()
|
|
|
|
p.doPrint(a, false, false)
|
2014-09-21 17:33:12 +00:00
|
|
|
s := string(p.buf)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.free()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// These routines end in 'ln', do not take a format string,
|
|
|
|
// always add spaces between operands, and add a newline
|
|
|
|
// after the last operand.
|
|
|
|
|
|
|
|
// Fprintln formats using the default formats for its operands and writes to w.
|
|
|
|
// Spaces are always added between operands and a newline is appended.
|
|
|
|
// It returns the number of bytes written and any write error encountered.
|
|
|
|
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
|
|
|
|
p := newPrinter()
|
|
|
|
p.doPrint(a, true, true)
|
2014-09-21 17:33:12 +00:00
|
|
|
n, err = w.Write(p.buf)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.free()
|
2014-09-21 17:33:12 +00:00
|
|
|
return
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Println formats using the default formats for its operands and writes to standard output.
|
|
|
|
// Spaces are always added between operands and a newline is appended.
|
|
|
|
// It returns the number of bytes written and any write error encountered.
|
|
|
|
func Println(a ...interface{}) (n int, err error) {
|
|
|
|
return Fprintln(os.Stdout, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sprintln formats using the default formats for its operands and returns the resulting string.
|
|
|
|
// Spaces are always added between operands and a newline is appended.
|
|
|
|
func Sprintln(a ...interface{}) string {
|
|
|
|
p := newPrinter()
|
|
|
|
p.doPrint(a, true, true)
|
2014-09-21 17:33:12 +00:00
|
|
|
s := string(p.buf)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.free()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// getField gets the i'th field of the struct value.
|
|
|
|
// If the field is itself is an interface, return a value for
|
2012-03-27 23:13:14 +00:00
|
|
|
// the thing inside the interface, not the interface itself.
|
|
|
|
func getField(v reflect.Value, i int) reflect.Value {
|
|
|
|
val := v.Field(i)
|
|
|
|
if val.Kind() == reflect.Interface && !val.IsNil() {
|
|
|
|
val = val.Elem()
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// parsenum converts ASCII to integer. num is 0 (and isnum is false) if no number present.
|
2012-03-27 23:13:14 +00:00
|
|
|
func parsenum(s string, start, end int) (num int, isnum bool, newi int) {
|
|
|
|
if start >= end {
|
|
|
|
return 0, false, end
|
|
|
|
}
|
|
|
|
for newi = start; newi < end && '0' <= s[newi] && s[newi] <= '9'; newi++ {
|
|
|
|
num = num*10 + int(s[newi]-'0')
|
|
|
|
isnum = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
func (p *pp) unknownType(v reflect.Value) {
|
|
|
|
if !v.IsValid() {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.buf.WriteByte('?')
|
2015-08-28 15:33:40 +00:00
|
|
|
p.buf.WriteString(v.Type().String())
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteByte('?')
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) badVerb(verb rune) {
|
|
|
|
p.erroring = true
|
|
|
|
p.add('%')
|
|
|
|
p.add('!')
|
|
|
|
p.add(verb)
|
|
|
|
p.add('(')
|
|
|
|
switch {
|
2014-09-21 17:33:12 +00:00
|
|
|
case p.arg != nil:
|
|
|
|
p.buf.WriteString(reflect.TypeOf(p.arg).String())
|
2012-03-27 23:13:14 +00:00
|
|
|
p.add('=')
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printArg(p.arg, 'v', 0)
|
2012-03-27 23:13:14 +00:00
|
|
|
case p.value.IsValid():
|
|
|
|
p.buf.WriteString(p.value.Type().String())
|
|
|
|
p.add('=')
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printValue(p.value, 'v', 0)
|
2012-03-27 23:13:14 +00:00
|
|
|
default:
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
}
|
|
|
|
p.add(')')
|
|
|
|
p.erroring = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) fmtBool(v bool, verb rune) {
|
|
|
|
switch verb {
|
|
|
|
case 't', 'v':
|
|
|
|
p.fmt.fmt_boolean(v)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmtC formats a rune for the 'c' format.
|
|
|
|
func (p *pp) fmtC(c int64) {
|
|
|
|
r := rune(c) // Check for overflow.
|
|
|
|
if int64(r) != c {
|
|
|
|
r = utf8.RuneError
|
|
|
|
}
|
|
|
|
w := utf8.EncodeRune(p.runeBuf[0:utf8.UTFMax], r)
|
|
|
|
p.fmt.pad(p.runeBuf[0:w])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) fmtInt64(v int64, verb rune) {
|
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.integer(v, 2, signed, ldigits)
|
|
|
|
case 'c':
|
|
|
|
p.fmtC(v)
|
|
|
|
case 'd', 'v':
|
|
|
|
p.fmt.integer(v, 10, signed, ldigits)
|
|
|
|
case 'o':
|
|
|
|
p.fmt.integer(v, 8, signed, ldigits)
|
|
|
|
case 'q':
|
2014-09-21 17:33:12 +00:00
|
|
|
if 0 <= v && v <= utf8.MaxRune {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.fmt_qc(v)
|
|
|
|
} else {
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
case 'x':
|
|
|
|
p.fmt.integer(v, 16, signed, ldigits)
|
|
|
|
case 'U':
|
|
|
|
p.fmtUnicode(v)
|
|
|
|
case 'X':
|
|
|
|
p.fmt.integer(v, 16, signed, udigits)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or
|
|
|
|
// not, as requested, by temporarily setting the sharp flag.
|
|
|
|
func (p *pp) fmt0x64(v uint64, leading0x bool) {
|
|
|
|
sharp := p.fmt.sharp
|
|
|
|
p.fmt.sharp = leading0x
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, ldigits)
|
|
|
|
p.fmt.sharp = sharp
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmtUnicode formats a uint64 in U+1234 form by
|
|
|
|
// temporarily turning on the unicode flag and tweaking the precision.
|
|
|
|
func (p *pp) fmtUnicode(v int64) {
|
|
|
|
precPresent := p.fmt.precPresent
|
|
|
|
sharp := p.fmt.sharp
|
|
|
|
p.fmt.sharp = false
|
|
|
|
prec := p.fmt.prec
|
|
|
|
if !precPresent {
|
|
|
|
// If prec is already set, leave it alone; otherwise 4 is minimum.
|
|
|
|
p.fmt.prec = 4
|
|
|
|
p.fmt.precPresent = true
|
|
|
|
}
|
|
|
|
p.fmt.unicode = true // turn on U+
|
|
|
|
p.fmt.uniQuote = sharp
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, udigits)
|
|
|
|
p.fmt.unicode = false
|
|
|
|
p.fmt.uniQuote = false
|
|
|
|
p.fmt.prec = prec
|
|
|
|
p.fmt.precPresent = precPresent
|
|
|
|
p.fmt.sharp = sharp
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
func (p *pp) fmtUint64(v uint64, verb rune) {
|
2012-03-27 23:13:14 +00:00
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.integer(int64(v), 2, unsigned, ldigits)
|
|
|
|
case 'c':
|
|
|
|
p.fmtC(int64(v))
|
|
|
|
case 'd':
|
|
|
|
p.fmt.integer(int64(v), 10, unsigned, ldigits)
|
|
|
|
case 'v':
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt0x64(v, true)
|
|
|
|
} else {
|
|
|
|
p.fmt.integer(int64(v), 10, unsigned, ldigits)
|
|
|
|
}
|
|
|
|
case 'o':
|
|
|
|
p.fmt.integer(int64(v), 8, unsigned, ldigits)
|
|
|
|
case 'q':
|
2014-09-21 17:33:12 +00:00
|
|
|
if 0 <= v && v <= utf8.MaxRune {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.fmt_qc(int64(v))
|
|
|
|
} else {
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
case 'x':
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, ldigits)
|
|
|
|
case 'X':
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, udigits)
|
|
|
|
case 'U':
|
|
|
|
p.fmtUnicode(int64(v))
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) fmtFloat32(v float32, verb rune) {
|
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.fmt_fb32(v)
|
|
|
|
case 'e':
|
|
|
|
p.fmt.fmt_e32(v)
|
|
|
|
case 'E':
|
|
|
|
p.fmt.fmt_E32(v)
|
2015-08-28 15:33:40 +00:00
|
|
|
case 'f', 'F':
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.fmt_f32(v)
|
|
|
|
case 'g', 'v':
|
|
|
|
p.fmt.fmt_g32(v)
|
|
|
|
case 'G':
|
|
|
|
p.fmt.fmt_G32(v)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) fmtFloat64(v float64, verb rune) {
|
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.fmt_fb64(v)
|
|
|
|
case 'e':
|
|
|
|
p.fmt.fmt_e64(v)
|
|
|
|
case 'E':
|
|
|
|
p.fmt.fmt_E64(v)
|
2015-08-28 15:33:40 +00:00
|
|
|
case 'f', 'F':
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.fmt_f64(v)
|
|
|
|
case 'g', 'v':
|
|
|
|
p.fmt.fmt_g64(v)
|
|
|
|
case 'G':
|
|
|
|
p.fmt.fmt_G64(v)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) fmtComplex64(v complex64, verb rune) {
|
|
|
|
switch verb {
|
2014-09-21 17:33:12 +00:00
|
|
|
case 'b', 'e', 'E', 'f', 'F', 'g', 'G':
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.fmt_c64(v, verb)
|
|
|
|
case 'v':
|
|
|
|
p.fmt.fmt_c64(v, 'g')
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) fmtComplex128(v complex128, verb rune) {
|
|
|
|
switch verb {
|
2014-09-21 17:33:12 +00:00
|
|
|
case 'b', 'e', 'E', 'f', 'F', 'g', 'G':
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.fmt_c128(v, verb)
|
|
|
|
case 'v':
|
|
|
|
p.fmt.fmt_c128(v, 'g')
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
func (p *pp) fmtString(v string, verb rune) {
|
2012-03-27 23:13:14 +00:00
|
|
|
switch verb {
|
|
|
|
case 'v':
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.fmt_q(v)
|
|
|
|
} else {
|
|
|
|
p.fmt.fmt_s(v)
|
|
|
|
}
|
|
|
|
case 's':
|
|
|
|
p.fmt.fmt_s(v)
|
|
|
|
case 'x':
|
|
|
|
p.fmt.fmt_sx(v, ldigits)
|
|
|
|
case 'X':
|
|
|
|
p.fmt.fmt_sx(v, udigits)
|
|
|
|
case 'q':
|
|
|
|
p.fmt.fmt_q(v)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
func (p *pp) fmtBytes(v []byte, verb rune, typ reflect.Type, depth int) {
|
2012-03-27 23:13:14 +00:00
|
|
|
if verb == 'v' || verb == 'd' {
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
|
|
|
if v == nil {
|
|
|
|
if typ == nil {
|
|
|
|
p.buf.WriteString("[]byte(nil)")
|
|
|
|
} else {
|
|
|
|
p.buf.WriteString(typ.String())
|
|
|
|
p.buf.Write(nilParenBytes)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if typ == nil {
|
|
|
|
p.buf.Write(bytesBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteString(typ.String())
|
|
|
|
p.buf.WriteByte('{')
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
} else {
|
|
|
|
p.buf.WriteByte('[')
|
|
|
|
}
|
|
|
|
for i, c := range v {
|
|
|
|
if i > 0 {
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printArg(c, 'v', depth+1)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteByte('}')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch verb {
|
|
|
|
case 's':
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmt.fmt_s(string(v))
|
2012-03-27 23:13:14 +00:00
|
|
|
case 'x':
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmt.fmt_bx(v, ldigits)
|
2012-03-27 23:13:14 +00:00
|
|
|
case 'X':
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmt.fmt_bx(v, udigits)
|
2012-03-27 23:13:14 +00:00
|
|
|
case 'q':
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmt.fmt_q(string(v))
|
2012-03-27 23:13:14 +00:00
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
func (p *pp) fmtPointer(value reflect.Value, verb rune) {
|
2014-09-21 17:33:12 +00:00
|
|
|
use0x64 := true
|
2012-03-27 23:13:14 +00:00
|
|
|
switch verb {
|
2014-09-21 17:33:12 +00:00
|
|
|
case 'p', 'v':
|
|
|
|
// ok
|
|
|
|
case 'b', 'd', 'o', 'x', 'X':
|
|
|
|
use0x64 = false
|
2012-03-27 23:13:14 +00:00
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var u uintptr
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
|
|
|
|
u = value.Pointer()
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.add('(')
|
|
|
|
p.buf.WriteString(value.Type().String())
|
|
|
|
p.add(')')
|
|
|
|
p.add('(')
|
|
|
|
if u == 0 {
|
|
|
|
p.buf.Write(nilBytes)
|
|
|
|
} else {
|
|
|
|
p.fmt0x64(uint64(u), true)
|
|
|
|
}
|
|
|
|
p.add(')')
|
|
|
|
} else if verb == 'v' && u == 0 {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
} else {
|
2014-09-21 17:33:12 +00:00
|
|
|
if use0x64 {
|
|
|
|
p.fmt0x64(uint64(u), !p.fmt.sharp)
|
|
|
|
} else {
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtUint64(uint64(u), verb)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
intBits = reflect.TypeOf(0).Bits()
|
|
|
|
uintptrBits = reflect.TypeOf(uintptr(0)).Bits()
|
|
|
|
)
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
func (p *pp) catchPanic(arg interface{}, verb rune) {
|
2012-03-27 23:13:14 +00:00
|
|
|
if err := recover(); err != nil {
|
|
|
|
// 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
|
|
|
|
// value receiver, and in either case, "<nil>" is a nice result.
|
2014-09-21 17:33:12 +00:00
|
|
|
if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Otherwise print a concise panic message. Most of the time the panic
|
|
|
|
// value will print itself nicely.
|
|
|
|
if p.panicking {
|
2014-09-21 17:33:12 +00:00
|
|
|
// Nested panics; the recursion in printArg cannot succeed.
|
2012-03-27 23:13:14 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmt.clearflags() // We are done, and for this output we want default behavior.
|
2014-09-21 17:33:12 +00:00
|
|
|
p.buf.Write(percentBangBytes)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.add(verb)
|
|
|
|
p.buf.Write(panicBytes)
|
|
|
|
p.panicking = true
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printArg(err, 'v', 0)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.panicking = false
|
|
|
|
p.buf.WriteByte(')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
// clearSpecialFlags pushes %#v back into the regular flags and returns their old state.
|
|
|
|
func (p *pp) clearSpecialFlags() (plusV, sharpV bool) {
|
|
|
|
plusV = p.fmt.plusV
|
|
|
|
if plusV {
|
|
|
|
p.fmt.plus = true
|
|
|
|
p.fmt.plusV = false
|
|
|
|
}
|
|
|
|
sharpV = p.fmt.sharpV
|
|
|
|
if sharpV {
|
|
|
|
p.fmt.sharp = true
|
|
|
|
p.fmt.sharpV = false
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// restoreSpecialFlags, whose argument should be a call to clearSpecialFlags,
|
|
|
|
// restores the setting of the plusV and sharpV flags.
|
|
|
|
func (p *pp) restoreSpecialFlags(plusV, sharpV bool) {
|
|
|
|
if plusV {
|
|
|
|
p.fmt.plus = false
|
|
|
|
p.fmt.plusV = true
|
|
|
|
}
|
|
|
|
if sharpV {
|
|
|
|
p.fmt.sharp = false
|
|
|
|
p.fmt.sharpV = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) handleMethods(verb rune, depth int) (handled bool) {
|
2012-03-27 23:13:14 +00:00
|
|
|
if p.erroring {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Is it a Formatter?
|
2014-09-21 17:33:12 +00:00
|
|
|
if formatter, ok := p.arg.(Formatter); ok {
|
2012-03-27 23:13:14 +00:00
|
|
|
handled = true
|
2015-08-28 15:33:40 +00:00
|
|
|
defer p.restoreSpecialFlags(p.clearSpecialFlags())
|
2014-09-21 17:33:12 +00:00
|
|
|
defer p.catchPanic(p.arg, verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
formatter.Format(p, verb)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// If we're doing Go syntax and the argument knows how to supply it, take care of it now.
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2014-09-21 17:33:12 +00:00
|
|
|
if stringer, ok := p.arg.(GoStringer); ok {
|
2012-03-27 23:13:14 +00:00
|
|
|
handled = true
|
2014-09-21 17:33:12 +00:00
|
|
|
defer p.catchPanic(p.arg, verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
// Print the result of GoString unadorned.
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmt.fmt_s(stringer.GoString())
|
2012-03-27 23:13:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If a string is acceptable according to the format, see if
|
|
|
|
// the value satisfies one of the string-valued interfaces.
|
|
|
|
// Println etc. set verb to %v, which is "stringable".
|
|
|
|
switch verb {
|
|
|
|
case 'v', 's', 'x', 'X', 'q':
|
|
|
|
// Is it an error or Stringer?
|
|
|
|
// The duplication in the bodies is necessary:
|
2015-08-28 15:33:40 +00:00
|
|
|
// setting handled and deferring catchPanic
|
2012-03-27 23:13:14 +00:00
|
|
|
// must happen before calling the method.
|
2014-09-21 17:33:12 +00:00
|
|
|
switch v := p.arg.(type) {
|
2012-03-27 23:13:14 +00:00
|
|
|
case error:
|
|
|
|
handled = true
|
2014-09-21 17:33:12 +00:00
|
|
|
defer p.catchPanic(p.arg, verb)
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printArg(v.Error(), verb, depth)
|
2012-03-27 23:13:14 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case Stringer:
|
|
|
|
handled = true
|
2014-09-21 17:33:12 +00:00
|
|
|
defer p.catchPanic(p.arg, verb)
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printArg(v.String(), verb, depth)
|
2012-03-27 23:13:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
return false
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
func (p *pp) printArg(arg interface{}, verb rune, depth int) (wasString bool) {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.arg = arg
|
|
|
|
p.value = reflect.Value{}
|
|
|
|
|
|
|
|
if arg == nil {
|
2012-03-27 23:13:14 +00:00
|
|
|
if verb == 'T' || verb == 'v' {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmt.pad(nilAngleBytes)
|
2012-03-27 23:13:14 +00:00
|
|
|
} else {
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special processing considerations.
|
|
|
|
// %T (the value's type) and %p (its address) are special; we always do them first.
|
|
|
|
switch verb {
|
|
|
|
case 'T':
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printArg(reflect.TypeOf(arg).String(), 's', 0)
|
2012-03-27 23:13:14 +00:00
|
|
|
return false
|
|
|
|
case 'p':
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtPointer(reflect.ValueOf(arg), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some types can be done without reflection.
|
2014-09-21 17:33:12 +00:00
|
|
|
switch f := arg.(type) {
|
2012-03-27 23:13:14 +00:00
|
|
|
case bool:
|
|
|
|
p.fmtBool(f, verb)
|
|
|
|
case float32:
|
|
|
|
p.fmtFloat32(f, verb)
|
|
|
|
case float64:
|
|
|
|
p.fmtFloat64(f, verb)
|
|
|
|
case complex64:
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmtComplex64(f, verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case complex128:
|
|
|
|
p.fmtComplex128(f, verb)
|
|
|
|
case int:
|
|
|
|
p.fmtInt64(int64(f), verb)
|
|
|
|
case int8:
|
|
|
|
p.fmtInt64(int64(f), verb)
|
|
|
|
case int16:
|
|
|
|
p.fmtInt64(int64(f), verb)
|
|
|
|
case int32:
|
|
|
|
p.fmtInt64(int64(f), verb)
|
|
|
|
case int64:
|
|
|
|
p.fmtInt64(f, verb)
|
|
|
|
case uint:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtUint64(uint64(f), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case uint8:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtUint64(uint64(f), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case uint16:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtUint64(uint64(f), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case uint32:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtUint64(uint64(f), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case uint64:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtUint64(f, verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case uintptr:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtUint64(uint64(f), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case string:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtString(f, verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
wasString = verb == 's' || verb == 'v'
|
|
|
|
case []byte:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtBytes(f, verb, nil, depth)
|
2012-03-27 23:13:14 +00:00
|
|
|
wasString = verb == 's'
|
|
|
|
default:
|
2014-09-21 17:33:12 +00:00
|
|
|
// If the type is not simple, it might have methods.
|
2015-08-28 15:33:40 +00:00
|
|
|
if handled := p.handleMethods(verb, depth); handled {
|
|
|
|
return false
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
// Need to use reflection
|
2015-08-28 15:33:40 +00:00
|
|
|
return p.printReflectValue(reflect.ValueOf(arg), verb, depth)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
p.arg = nil
|
2012-03-27 23:13:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// printValue is like printArg but starts with a reflect value, not an interface{} value.
|
2015-08-28 15:33:40 +00:00
|
|
|
func (p *pp) printValue(value reflect.Value, verb rune, depth int) (wasString bool) {
|
2012-03-27 23:13:14 +00:00
|
|
|
if !value.IsValid() {
|
|
|
|
if verb == 'T' || verb == 'v' {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
} else {
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special processing considerations.
|
|
|
|
// %T (the value's type) and %p (its address) are special; we always do them first.
|
|
|
|
switch verb {
|
|
|
|
case 'T':
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printArg(value.Type().String(), 's', 0)
|
2012-03-27 23:13:14 +00:00
|
|
|
return false
|
|
|
|
case 'p':
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtPointer(value, verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle values with special methods.
|
2014-09-21 17:33:12 +00:00
|
|
|
// Call always, even when arg == nil, because handleMethods clears p.fmt.plus for us.
|
|
|
|
p.arg = nil // Make sure it's cleared, for safety.
|
2012-03-27 23:13:14 +00:00
|
|
|
if value.CanInterface() {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.arg = value.Interface()
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
if handled := p.handleMethods(verb, depth); handled {
|
|
|
|
return false
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
return p.printReflectValue(value, verb, depth)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
var byteType = reflect.TypeOf(byte(0))
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// printReflectValue is the fallback for both printArg and printValue.
|
2012-03-27 23:13:14 +00:00
|
|
|
// It uses reflect to print the value.
|
2015-08-28 15:33:40 +00:00
|
|
|
func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) (wasString bool) {
|
2012-03-27 23:13:14 +00:00
|
|
|
oldValue := p.value
|
|
|
|
p.value = value
|
|
|
|
BigSwitch:
|
|
|
|
switch f := value; f.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
p.fmtBool(f.Bool(), verb)
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
p.fmtInt64(f.Int(), verb)
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtUint64(f.Uint(), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
if f.Type().Size() == 4 {
|
|
|
|
p.fmtFloat32(float32(f.Float()), verb)
|
|
|
|
} else {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmtFloat64(f.Float(), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
case reflect.Complex64, reflect.Complex128:
|
|
|
|
if f.Type().Size() == 8 {
|
|
|
|
p.fmtComplex64(complex64(f.Complex()), verb)
|
|
|
|
} else {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmtComplex128(f.Complex(), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
case reflect.String:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtString(f.String(), verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
case reflect.Map:
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteString(f.Type().String())
|
|
|
|
if f.IsNil() {
|
|
|
|
p.buf.WriteString("(nil)")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.buf.WriteByte('{')
|
|
|
|
} else {
|
|
|
|
p.buf.Write(mapBytes)
|
|
|
|
}
|
|
|
|
keys := f.MapKeys()
|
|
|
|
for i, key := range keys {
|
|
|
|
if i > 0 {
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printValue(key, verb, depth+1)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteByte(':')
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printValue(f.MapIndex(key), verb, depth+1)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteByte('}')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteString(value.Type().String())
|
|
|
|
}
|
|
|
|
p.add('{')
|
|
|
|
v := f
|
|
|
|
t := v.Type()
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
if i > 0 {
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.plusV || p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
if f := t.Field(i); f.Name != "" {
|
|
|
|
p.buf.WriteString(f.Name)
|
|
|
|
p.buf.WriteByte(':')
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printValue(getField(v, i), verb, depth+1)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
p.buf.WriteByte('}')
|
|
|
|
case reflect.Interface:
|
|
|
|
value := f.Elem()
|
|
|
|
if !value.IsValid() {
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteString(f.Type().String())
|
|
|
|
p.buf.Write(nilParenBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-28 15:33:40 +00:00
|
|
|
wasString = p.printValue(value, verb, depth+1)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
case reflect.Array, reflect.Slice:
|
2015-08-28 15:33:40 +00:00
|
|
|
// Byte slices are special:
|
|
|
|
// - Handle []byte (== []uint8) with fmtBytes.
|
|
|
|
// - Handle []T, where T is a named byte type, with fmtBytes only
|
|
|
|
// for the s, q, an x verbs. For other verbs, T might be a
|
|
|
|
// Stringer, so we use printValue to print each element.
|
|
|
|
if typ := f.Type(); typ.Elem().Kind() == reflect.Uint8 && (typ.Elem() == byteType || verb == 's' || verb == 'q' || verb == 'x') {
|
2014-09-21 17:33:12 +00:00
|
|
|
var bytes []byte
|
|
|
|
if f.Kind() == reflect.Slice {
|
|
|
|
bytes = f.Bytes()
|
|
|
|
} else if f.CanAddr() {
|
|
|
|
bytes = f.Slice(0, f.Len()).Bytes()
|
|
|
|
} else {
|
|
|
|
// We have an array, but we cannot Slice() a non-addressable array,
|
|
|
|
// so we build a slice by hand. This is a rare case but it would be nice
|
|
|
|
// if reflection could help a little more.
|
|
|
|
bytes = make([]byte, f.Len())
|
|
|
|
for i := range bytes {
|
|
|
|
bytes[i] = byte(f.Index(i).Uint())
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtBytes(bytes, verb, typ, depth)
|
2012-03-27 23:13:14 +00:00
|
|
|
wasString = verb == 's'
|
|
|
|
break
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteString(value.Type().String())
|
|
|
|
if f.Kind() == reflect.Slice && f.IsNil() {
|
|
|
|
p.buf.WriteString("(nil)")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.buf.WriteByte('{')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte('[')
|
|
|
|
}
|
|
|
|
for i := 0; i < f.Len(); i++ {
|
|
|
|
if i > 0 {
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printValue(f.Index(i), verb, depth+1)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
if p.fmt.sharpV {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteByte('}')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
v := f.Pointer()
|
|
|
|
// pointer to array or slice or struct? ok at top level
|
|
|
|
// but not embedded (avoid loops)
|
|
|
|
if v != 0 && depth == 0 {
|
|
|
|
switch a := f.Elem(); a.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
p.buf.WriteByte('&')
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printValue(a, verb, depth+1)
|
2012-03-27 23:13:14 +00:00
|
|
|
break BigSwitch
|
|
|
|
case reflect.Struct:
|
|
|
|
p.buf.WriteByte('&')
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printValue(a, verb, depth+1)
|
|
|
|
break BigSwitch
|
|
|
|
case reflect.Map:
|
|
|
|
p.buf.WriteByte('&')
|
|
|
|
p.printValue(a, verb, depth+1)
|
2012-03-27 23:13:14 +00:00
|
|
|
break BigSwitch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case reflect.Chan, reflect.Func, reflect.UnsafePointer:
|
2015-08-28 15:33:40 +00:00
|
|
|
p.fmtPointer(value, verb)
|
2012-03-27 23:13:14 +00:00
|
|
|
default:
|
|
|
|
p.unknownType(f)
|
|
|
|
}
|
|
|
|
p.value = oldValue
|
|
|
|
return wasString
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has type int.
|
|
|
|
func intFromArg(a []interface{}, argNum int) (num int, isInt bool, newArgNum int) {
|
|
|
|
newArgNum = argNum
|
|
|
|
if argNum < len(a) {
|
|
|
|
num, isInt = a[argNum].(int)
|
|
|
|
newArgNum = argNum + 1
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// 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 bracket.
|
|
|
|
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, found bool) {
|
|
|
|
if len(format) <= i || format[i] != '[' {
|
|
|
|
return argNum, i, false
|
|
|
|
}
|
|
|
|
p.reordered = true
|
|
|
|
index, wid, ok := parseArgNumber(format[i:])
|
|
|
|
if ok && 0 <= index && index < numArgs {
|
|
|
|
return index, i + wid, true
|
|
|
|
}
|
|
|
|
p.goodArgNum = false
|
|
|
|
return argNum, i + wid, true
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
func (p *pp) doPrintf(format string, a []interface{}) {
|
|
|
|
end := len(format)
|
2014-09-21 17:33:12 +00:00
|
|
|
argNum := 0 // we process one argument per non-trivial format
|
|
|
|
afterIndex := false // previous item in format was an index like [3].
|
|
|
|
p.reordered = false
|
2012-03-27 23:13:14 +00:00
|
|
|
for i := 0; i < end; {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.goodArgNum = true
|
2012-03-27 23:13:14 +00:00
|
|
|
lasti := i
|
|
|
|
for i < end && format[i] != '%' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i > lasti {
|
|
|
|
p.buf.WriteString(format[lasti:i])
|
|
|
|
}
|
|
|
|
if i >= end {
|
|
|
|
// done processing format string
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process one verb
|
|
|
|
i++
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
// Do we have flags?
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.clearflags()
|
|
|
|
F:
|
|
|
|
for ; i < end; i++ {
|
|
|
|
switch format[i] {
|
|
|
|
case '#':
|
|
|
|
p.fmt.sharp = true
|
|
|
|
case '0':
|
|
|
|
p.fmt.zero = true
|
|
|
|
case '+':
|
|
|
|
p.fmt.plus = true
|
|
|
|
case '-':
|
|
|
|
p.fmt.minus = true
|
|
|
|
case ' ':
|
|
|
|
p.fmt.space = true
|
|
|
|
default:
|
|
|
|
break F
|
|
|
|
}
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
// Do we have an explicit argument index?
|
|
|
|
argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
|
|
|
|
|
|
|
|
// Do we have width?
|
2012-03-27 23:13:14 +00:00
|
|
|
if i < end && format[i] == '*' {
|
2014-09-21 17:33:12 +00:00
|
|
|
i++
|
|
|
|
p.fmt.wid, p.fmt.widPresent, argNum = intFromArg(a, argNum)
|
2012-03-27 23:13:14 +00:00
|
|
|
if !p.fmt.widPresent {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.buf.Write(badWidthBytes)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
afterIndex = false
|
2012-03-27 23:13:14 +00:00
|
|
|
} else {
|
|
|
|
p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end)
|
2014-09-21 17:33:12 +00:00
|
|
|
if afterIndex && p.fmt.widPresent { // "%[3]2d"
|
|
|
|
p.goodArgNum = false
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
// Do we have precision?
|
|
|
|
if i+1 < end && format[i] == '.' {
|
|
|
|
i++
|
|
|
|
if afterIndex { // "%[3].2d"
|
|
|
|
p.goodArgNum = false
|
|
|
|
}
|
|
|
|
argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
|
|
|
|
if format[i] == '*' {
|
|
|
|
i++
|
|
|
|
p.fmt.prec, p.fmt.precPresent, argNum = intFromArg(a, argNum)
|
2012-03-27 23:13:14 +00:00
|
|
|
if !p.fmt.precPresent {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.buf.Write(badPrecBytes)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
afterIndex = false
|
2012-03-27 23:13:14 +00:00
|
|
|
} else {
|
2014-09-21 17:33:12 +00:00
|
|
|
p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i, end)
|
2012-03-27 23:13:14 +00:00
|
|
|
if !p.fmt.precPresent {
|
|
|
|
p.fmt.prec = 0
|
|
|
|
p.fmt.precPresent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
if !afterIndex {
|
|
|
|
argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
if i >= end {
|
|
|
|
p.buf.Write(noVerbBytes)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c, w := utf8.DecodeRuneInString(format[i:])
|
|
|
|
i += w
|
|
|
|
// percent is special - absorbs no operand
|
|
|
|
if c == '%' {
|
|
|
|
p.buf.WriteByte('%') // We ignore width and prec.
|
|
|
|
continue
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if !p.goodArgNum {
|
|
|
|
p.buf.Write(percentBangBytes)
|
|
|
|
p.add(c)
|
|
|
|
p.buf.Write(badIndexBytes)
|
|
|
|
continue
|
|
|
|
} else if argNum >= len(a) { // out of operands
|
|
|
|
p.buf.Write(percentBangBytes)
|
2012-03-27 23:13:14 +00:00
|
|
|
p.add(c)
|
|
|
|
p.buf.Write(missingBytes)
|
|
|
|
continue
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
arg := a[argNum]
|
|
|
|
argNum++
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
if c == 'v' {
|
|
|
|
if p.fmt.sharp {
|
|
|
|
// Go syntax. Set the flag in the fmt and clear the sharp flag.
|
|
|
|
p.fmt.sharp = false
|
|
|
|
p.fmt.sharpV = true
|
|
|
|
}
|
|
|
|
if p.fmt.plus {
|
|
|
|
// Struct-field syntax. Set the flag in the fmt and clear the plus flag.
|
|
|
|
p.fmt.plus = false
|
|
|
|
p.fmt.plusV = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.printArg(arg, c, 0)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// 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) {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.Write(extraBytes)
|
2014-09-21 17:33:12 +00:00
|
|
|
for ; argNum < len(a); argNum++ {
|
|
|
|
arg := a[argNum]
|
|
|
|
if arg != nil {
|
|
|
|
p.buf.WriteString(reflect.TypeOf(arg).String())
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.WriteByte('=')
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
p.printArg(arg, 'v', 0)
|
2014-09-21 17:33:12 +00:00
|
|
|
if argNum+1 < len(a) {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.buf.WriteByte(')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
|
|
|
|
prevString := false
|
2014-09-21 17:33:12 +00:00
|
|
|
for argNum := 0; argNum < len(a); argNum++ {
|
2012-03-27 23:13:14 +00:00
|
|
|
p.fmt.clearflags()
|
2014-09-21 17:33:12 +00:00
|
|
|
// always add spaces if we're doing Println
|
|
|
|
arg := a[argNum]
|
|
|
|
if argNum > 0 {
|
|
|
|
isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
|
2012-03-27 23:13:14 +00:00
|
|
|
if addspace || !isString && !prevString {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
prevString = p.printArg(arg, 'v', 0)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
if addnewline {
|
|
|
|
p.buf.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|