2010-12-03 05:34:57 +01: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 (
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
2010-12-03 05:34:57 +01:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2011-10-27 01:57:58 +02:00
|
|
|
"sync"
|
2011-12-07 02:11:29 +01:00
|
|
|
"unicode/utf8"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Some constants in the form of bytes, to avoid string overhead.
|
|
|
|
// Needlessly fastidious, I suppose.
|
|
|
|
var (
|
2013-11-06 20:49:01 +01: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)")
|
2010-12-03 05:34:57 +01: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.
|
2011-12-03 03:17:34 +01:00
|
|
|
Write(b []byte) (ret int, err error)
|
2010-12-03 05:34:57 +01:00
|
|
|
// 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)
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// Flag reports whether the flag c, a character, has been set.
|
2011-09-16 17:47:21 +02:00
|
|
|
Flag(c int) bool
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Formatter is the interface implemented by values with a custom formatter.
|
2013-07-16 08:54:42 +02:00
|
|
|
// The implementation of Format may call Sprint(f) or Fprint(f) etc.
|
2010-12-03 05:34:57 +01:00
|
|
|
// to generate its output.
|
|
|
|
type Formatter interface {
|
2011-12-02 20:34:41 +01:00
|
|
|
Format(f State, c rune)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Stringer is implemented by any value that has a String method,
|
2010-12-03 05:34:57 +01:00
|
|
|
// which defines the ``native'' format for that value.
|
|
|
|
// The String method is used to print values passed as an operand
|
2013-07-16 08:54:42 +02:00
|
|
|
// to any format that accepts a string or to an unformatted printer
|
|
|
|
// such as Print.
|
2010-12-03 05:34:57 +01:00
|
|
|
type Stringer interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// GoStringer is implemented by any value that has a GoString method,
|
2010-12-03 05:34:57 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02: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
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
type pp struct {
|
2011-09-16 17:47:21 +02:00
|
|
|
n int
|
|
|
|
panicking bool
|
2012-01-12 02:31:45 +01:00
|
|
|
erroring bool // printing an error condition
|
2012-03-30 23:27:11 +02:00
|
|
|
buf buffer
|
2013-11-06 20:49:01 +01:00
|
|
|
// arg holds the current item, as an interface{}.
|
|
|
|
arg interface{}
|
2011-10-27 01:57:58 +02:00
|
|
|
// value holds the current item, as a reflect.Value, and will be
|
|
|
|
// the zero Value if the item has not been reflected.
|
2013-11-06 20:49:01 +01: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
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
var ppFree = sync.Pool{
|
|
|
|
New: func() interface{} { return new(pp) },
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// newPrinter allocates a new pp struct or grab a cached one.
|
2011-03-17 00:05:44 +01:00
|
|
|
func newPrinter() *pp {
|
2014-06-05 01:15:33 +02:00
|
|
|
p := ppFree.Get().(*pp)
|
2011-09-16 17:47:21 +02:00
|
|
|
p.panicking = false
|
2012-01-12 02:31:45 +01:00
|
|
|
p.erroring = false
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.init(&p.buf)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// free saves used pp structs in ppFree; avoids an allocation per invocation.
|
2010-12-03 05:34:57 +01:00
|
|
|
func (p *pp) free() {
|
|
|
|
// Don't hold on to pp structs with large buffers.
|
2012-03-30 23:27:11 +02:00
|
|
|
if cap(p.buf) > 1024 {
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
p.buf = p.buf[:0]
|
2013-11-06 20:49:01 +01:00
|
|
|
p.arg = nil
|
2011-10-27 01:57:58 +02:00
|
|
|
p.value = reflect.Value{}
|
2014-06-05 01:15:33 +02:00
|
|
|
ppFree.Put(p)
|
2010-12-03 05:34:57 +01: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
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) add(c rune) {
|
2011-01-21 19:19:03 +01:00
|
|
|
p.buf.WriteRune(c)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implement Write so we can call Fprintf on a pp (through State), for
|
|
|
|
// recursive use in custom verbs.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (p *pp) Write(b []byte) (ret int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := newPrinter()
|
|
|
|
p.doPrintf(format, a)
|
2013-11-06 20:49:01 +01:00
|
|
|
n, err = w.Write(p.buf)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.free()
|
2013-11-06 20:49:01 +01:00
|
|
|
return
|
2010-12-03 05:34:57 +01: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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Printf(format string, a ...interface{}) (n int, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
return Fprintf(os.Stdout, format, a...)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2012-03-30 23:27:11 +02:00
|
|
|
s := string(p.buf)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.free()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
// Errorf formats according to a format specifier and returns the string
|
2011-12-03 03:17:34 +01:00
|
|
|
// as a value that satisfies error.
|
|
|
|
func Errorf(format string, a ...interface{}) error {
|
|
|
|
return errors.New(Sprintf(format, a...))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := newPrinter()
|
|
|
|
p.doPrint(a, false, false)
|
2013-11-06 20:49:01 +01:00
|
|
|
n, err = w.Write(p.buf)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.free()
|
2013-11-06 20:49:01 +01:00
|
|
|
return
|
2010-12-03 05:34:57 +01: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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Print(a ...interface{}) (n int, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
return Fprint(os.Stdout, a...)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2012-03-30 23:27:11 +02:00
|
|
|
s := string(p.buf)
|
2010-12-03 05:34:57 +01: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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := newPrinter()
|
|
|
|
p.doPrint(a, true, true)
|
2013-11-06 20:49:01 +01:00
|
|
|
n, err = w.Write(p.buf)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.free()
|
2013-11-06 20:49:01 +01:00
|
|
|
return
|
2010-12-03 05:34:57 +01: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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Println(a ...interface{}) (n int, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
return Fprintln(os.Stdout, a...)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2012-03-30 23:27:11 +02:00
|
|
|
s := string(p.buf)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.free()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// getField gets the i'th field of the struct value.
|
|
|
|
// If the field is itself is an interface, return a value for
|
2010-12-03 05:34:57 +01:00
|
|
|
// the thing inside the interface, not the interface itself.
|
2011-05-20 02:18:15 +02:00
|
|
|
func getField(v reflect.Value, i int) reflect.Value {
|
2010-12-03 05:34:57 +01:00
|
|
|
val := v.Field(i)
|
2011-10-22 18:19:46 +02:00
|
|
|
if val.Kind() == reflect.Interface && !val.IsNil() {
|
|
|
|
val = val.Elem()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// parsenum converts ASCII to integer. num is 0 (and isnum is false) if no number present.
|
2010-12-03 05:34:57 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) unknownType(v interface{}) {
|
|
|
|
if v == nil {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.buf.WriteByte('?')
|
2011-05-20 02:18:15 +02:00
|
|
|
p.buf.WriteString(reflect.TypeOf(v).String())
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('?')
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) badVerb(verb rune) {
|
2012-01-12 02:31:45 +01:00
|
|
|
p.erroring = true
|
2010-12-03 05:34:57 +01:00
|
|
|
p.add('%')
|
|
|
|
p.add('!')
|
|
|
|
p.add(verb)
|
|
|
|
p.add('(')
|
2011-10-22 18:19:46 +02:00
|
|
|
switch {
|
2013-11-06 20:49:01 +01:00
|
|
|
case p.arg != nil:
|
|
|
|
p.buf.WriteString(reflect.TypeOf(p.arg).String())
|
2010-12-03 05:34:57 +01:00
|
|
|
p.add('=')
|
2013-11-06 20:49:01 +01:00
|
|
|
p.printArg(p.arg, 'v', false, false, 0)
|
2011-10-27 01:57:58 +02:00
|
|
|
case p.value.IsValid():
|
|
|
|
p.buf.WriteString(p.value.Type().String())
|
2011-10-22 18:19:46 +02:00
|
|
|
p.add('=')
|
2011-10-27 01:57:58 +02:00
|
|
|
p.printValue(p.value, 'v', false, false, 0)
|
2011-10-22 18:19:46 +02:00
|
|
|
default:
|
|
|
|
p.buf.Write(nilAngleBytes)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
p.add(')')
|
2012-01-12 02:31:45 +01:00
|
|
|
p.erroring = false
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtBool(v bool, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
|
|
|
case 't', 'v':
|
|
|
|
p.fmt.fmt_boolean(v)
|
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmtC formats a rune for the 'c' format.
|
|
|
|
func (p *pp) fmtC(c int64) {
|
2011-12-02 20:34:41 +01:00
|
|
|
r := rune(c) // Check for overflow.
|
|
|
|
if int64(r) != c {
|
|
|
|
r = utf8.RuneError
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
w := utf8.EncodeRune(p.runeBuf[0:utf8.UTFMax], r)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.pad(p.runeBuf[0:w])
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtInt64(v int64, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
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)
|
2011-09-16 17:47:21 +02:00
|
|
|
case 'q':
|
2012-03-30 23:27:11 +02:00
|
|
|
if 0 <= v && v <= utf8.MaxRune {
|
2011-09-16 17:47:21 +02:00
|
|
|
p.fmt.fmt_qc(v)
|
|
|
|
} else {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'x':
|
|
|
|
p.fmt.integer(v, 16, signed, ldigits)
|
2011-01-21 19:19:03 +01:00
|
|
|
case 'U':
|
|
|
|
p.fmtUnicode(v)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'X':
|
|
|
|
p.fmt.integer(v, 16, signed, udigits)
|
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// 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) {
|
2010-12-03 05:34:57 +01:00
|
|
|
sharp := p.fmt.sharp
|
2011-03-17 00:05:44 +01:00
|
|
|
p.fmt.sharp = leading0x
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.integer(int64(v), 16, unsigned, ldigits)
|
|
|
|
p.fmt.sharp = sharp
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
// 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
|
2011-09-16 17:47:21 +02:00
|
|
|
sharp := p.fmt.sharp
|
|
|
|
p.fmt.sharp = false
|
2011-01-21 19:19:03 +01:00
|
|
|
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+
|
2011-09-16 17:47:21 +02:00
|
|
|
p.fmt.uniQuote = sharp
|
2011-01-21 19:19:03 +01:00
|
|
|
p.fmt.integer(int64(v), 16, unsigned, udigits)
|
|
|
|
p.fmt.unicode = false
|
2011-09-16 17:47:21 +02:00
|
|
|
p.fmt.uniQuote = false
|
2011-01-21 19:19:03 +01:00
|
|
|
p.fmt.prec = prec
|
|
|
|
p.fmt.precPresent = precPresent
|
2011-09-16 17:47:21 +02:00
|
|
|
p.fmt.sharp = sharp
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtUint64(v uint64, verb rune, goSyntax bool) {
|
2010-12-03 05:34:57 +01: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':
|
|
|
|
if goSyntax {
|
2011-03-17 00:05:44 +01:00
|
|
|
p.fmt0x64(v, true)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
|
|
|
p.fmt.integer(int64(v), 10, unsigned, ldigits)
|
|
|
|
}
|
|
|
|
case 'o':
|
|
|
|
p.fmt.integer(int64(v), 8, unsigned, ldigits)
|
2011-09-16 17:47:21 +02:00
|
|
|
case 'q':
|
2012-03-30 23:27:11 +02:00
|
|
|
if 0 <= v && v <= utf8.MaxRune {
|
2011-09-16 17:47:21 +02:00
|
|
|
p.fmt.fmt_qc(int64(v))
|
|
|
|
} else {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'x':
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, ldigits)
|
|
|
|
case 'X':
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, udigits)
|
2011-05-20 02:18:15 +02:00
|
|
|
case 'U':
|
|
|
|
p.fmtUnicode(int64(v))
|
2010-12-03 05:34:57 +01:00
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtFloat32(v float32, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.fmt_fb32(v)
|
|
|
|
case 'e':
|
|
|
|
p.fmt.fmt_e32(v)
|
|
|
|
case 'E':
|
|
|
|
p.fmt.fmt_E32(v)
|
2014-07-19 10:53:52 +02:00
|
|
|
case 'f', 'F':
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.fmt_f32(v)
|
|
|
|
case 'g', 'v':
|
|
|
|
p.fmt.fmt_g32(v)
|
|
|
|
case 'G':
|
|
|
|
p.fmt.fmt_G32(v)
|
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtFloat64(v float64, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.fmt_fb64(v)
|
|
|
|
case 'e':
|
|
|
|
p.fmt.fmt_e64(v)
|
|
|
|
case 'E':
|
|
|
|
p.fmt.fmt_E64(v)
|
2014-07-19 10:53:52 +02:00
|
|
|
case 'f', 'F':
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.fmt_f64(v)
|
|
|
|
case 'g', 'v':
|
|
|
|
p.fmt.fmt_g64(v)
|
|
|
|
case 'G':
|
|
|
|
p.fmt.fmt_G64(v)
|
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtComplex64(v complex64, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
2013-11-06 20:49:01 +01:00
|
|
|
case 'b', 'e', 'E', 'f', 'F', 'g', 'G':
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.fmt_c64(v, verb)
|
|
|
|
case 'v':
|
|
|
|
p.fmt.fmt_c64(v, 'g')
|
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtComplex128(v complex128, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
2013-11-06 20:49:01 +01:00
|
|
|
case 'b', 'e', 'E', 'f', 'F', 'g', 'G':
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.fmt_c128(v, verb)
|
|
|
|
case 'v':
|
|
|
|
p.fmt.fmt_c128(v, 'g')
|
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtString(v string, verb rune, goSyntax bool) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
|
|
|
case 'v':
|
|
|
|
if goSyntax {
|
|
|
|
p.fmt.fmt_q(v)
|
|
|
|
} else {
|
|
|
|
p.fmt.fmt_s(v)
|
|
|
|
}
|
|
|
|
case 's':
|
|
|
|
p.fmt.fmt_s(v)
|
|
|
|
case 'x':
|
2012-01-13 06:11:45 +01:00
|
|
|
p.fmt.fmt_sx(v, ldigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'X':
|
2012-01-13 06:11:45 +01:00
|
|
|
p.fmt.fmt_sx(v, udigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'q':
|
|
|
|
p.fmt.fmt_q(v)
|
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, typ reflect.Type, depth int) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if verb == 'v' || verb == 'd' {
|
|
|
|
if goSyntax {
|
2014-07-19 10:53:52 +02:00
|
|
|
if v == nil {
|
|
|
|
if typ == nil {
|
|
|
|
p.buf.WriteString("[]byte(nil)")
|
|
|
|
} else {
|
|
|
|
p.buf.WriteString(typ.String())
|
|
|
|
p.buf.Write(nilParenBytes)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
if typ == nil {
|
|
|
|
p.buf.Write(bytesBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteString(typ.String())
|
|
|
|
p.buf.WriteByte('{')
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
|
|
|
p.buf.WriteByte('[')
|
|
|
|
}
|
|
|
|
for i, c := range v {
|
|
|
|
if i > 0 {
|
|
|
|
if goSyntax {
|
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
p.printArg(c, 'v', p.fmt.plus, goSyntax, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if goSyntax {
|
|
|
|
p.buf.WriteByte('}')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch verb {
|
|
|
|
case 's':
|
2012-10-23 06:31:11 +02:00
|
|
|
p.fmt.fmt_s(string(v))
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'x':
|
2012-10-23 06:31:11 +02:00
|
|
|
p.fmt.fmt_bx(v, ldigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'X':
|
2012-10-23 06:31:11 +02:00
|
|
|
p.fmt.fmt_bx(v, udigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'q':
|
2012-10-23 06:31:11 +02:00
|
|
|
p.fmt.fmt_q(string(v))
|
2010-12-03 05:34:57 +01:00
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
|
2012-10-23 06:31:11 +02:00
|
|
|
use0x64 := true
|
2012-03-02 17:38:43 +01:00
|
|
|
switch verb {
|
2012-10-23 06:31:11 +02:00
|
|
|
case 'p', 'v':
|
|
|
|
// ok
|
|
|
|
case 'b', 'd', 'o', 'x', 'X':
|
|
|
|
use0x64 = false
|
2012-03-02 17:38:43 +01:00
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
var u uintptr
|
2011-05-20 02:18:15 +02:00
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
|
|
|
|
u = value.Pointer()
|
2011-04-07 19:09:10 +02:00
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if goSyntax {
|
|
|
|
p.add('(')
|
2011-10-22 18:19:46 +02:00
|
|
|
p.buf.WriteString(value.Type().String())
|
2010-12-03 05:34:57 +01:00
|
|
|
p.add(')')
|
|
|
|
p.add('(')
|
|
|
|
if u == 0 {
|
|
|
|
p.buf.Write(nilBytes)
|
|
|
|
} else {
|
2011-04-07 19:09:10 +02:00
|
|
|
p.fmt0x64(uint64(u), true)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
p.add(')')
|
2012-03-02 17:38:43 +01:00
|
|
|
} else if verb == 'v' && u == 0 {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
2012-10-23 06:31:11 +02:00
|
|
|
if use0x64 {
|
|
|
|
p.fmt0x64(uint64(u), !p.fmt.sharp)
|
|
|
|
} else {
|
|
|
|
p.fmtUint64(uint64(u), verb, false)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2011-05-20 02:18:15 +02:00
|
|
|
intBits = reflect.TypeOf(0).Bits()
|
|
|
|
uintptrBits = reflect.TypeOf(uintptr(0)).Bits()
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (p *pp) catchPanic(arg interface{}, verb rune) {
|
2011-09-16 17:47:21 +02: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.
|
2013-11-06 20:49:01 +01:00
|
|
|
if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() {
|
2011-09-16 17:47:21 +02: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 {
|
2013-11-06 20:49:01 +01:00
|
|
|
// Nested panics; the recursion in printArg cannot succeed.
|
2011-09-16 17:47:21 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
p.buf.Write(percentBangBytes)
|
2011-09-16 17:47:21 +02:00
|
|
|
p.add(verb)
|
|
|
|
p.buf.Write(panicBytes)
|
|
|
|
p.panicking = true
|
2013-11-06 20:49:01 +01:00
|
|
|
p.printArg(err, 'v', false, false, 0)
|
2011-09-16 17:47:21 +02:00
|
|
|
p.panicking = false
|
|
|
|
p.buf.WriteByte(')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString, handled bool) {
|
2012-01-12 02:31:45 +01:00
|
|
|
if p.erroring {
|
|
|
|
return
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
// Is it a Formatter?
|
2013-11-06 20:49:01 +01:00
|
|
|
if formatter, ok := p.arg.(Formatter); ok {
|
2011-10-22 18:19:46 +02:00
|
|
|
handled = true
|
|
|
|
wasString = false
|
2013-11-06 20:49:01 +01:00
|
|
|
defer p.catchPanic(p.arg, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
formatter.Format(p, verb)
|
2011-10-22 18:19:46 +02:00
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
// Must not touch flags before Formatter looks at them.
|
|
|
|
if plus {
|
|
|
|
p.fmt.plus = false
|
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// If we're doing Go syntax and the argument knows how to supply it, take care of it now.
|
2010-12-03 05:34:57 +01:00
|
|
|
if goSyntax {
|
|
|
|
p.fmt.sharp = false
|
2013-11-06 20:49:01 +01:00
|
|
|
if stringer, ok := p.arg.(GoStringer); ok {
|
2011-10-22 18:19:46 +02:00
|
|
|
wasString = false
|
|
|
|
handled = true
|
2013-11-06 20:49:01 +01:00
|
|
|
defer p.catchPanic(p.arg, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
// Print the result of GoString unadorned.
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtString(stringer.GoString(), 's', false)
|
2011-10-22 18:19:46 +02:00
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2011-12-14 16:41:54 +01:00
|
|
|
// 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:
|
|
|
|
// setting wasString and handled, and deferring catchPanic,
|
|
|
|
// must happen before calling the method.
|
2013-11-06 20:49:01 +01:00
|
|
|
switch v := p.arg.(type) {
|
2011-12-14 16:41:54 +01:00
|
|
|
case error:
|
|
|
|
wasString = false
|
|
|
|
handled = true
|
2013-11-06 20:49:01 +01:00
|
|
|
defer p.catchPanic(p.arg, verb)
|
|
|
|
p.printArg(v.Error(), verb, plus, false, depth)
|
2011-12-14 16:41:54 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
case Stringer:
|
|
|
|
wasString = false
|
|
|
|
handled = true
|
2013-11-06 20:49:01 +01:00
|
|
|
defer p.catchPanic(p.arg, verb)
|
|
|
|
p.printArg(v.String(), verb, plus, false, depth)
|
2011-12-14 16:41:54 +01:00
|
|
|
return
|
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
handled = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (p *pp) printArg(arg interface{}, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
|
|
|
|
p.arg = arg
|
2012-10-03 07:27:36 +02:00
|
|
|
p.value = reflect.Value{}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if arg == nil {
|
2011-10-22 18:19:46 +02:00
|
|
|
if verb == 'T' || verb == 'v' {
|
2013-07-16 08:54:42 +02:00
|
|
|
p.fmt.pad(nilAngleBytes)
|
2011-10-22 18:19:46 +02:00
|
|
|
} else {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
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':
|
2013-11-06 20:49:01 +01:00
|
|
|
p.printArg(reflect.TypeOf(arg).String(), 's', false, false, 0)
|
2011-10-22 18:19:46 +02:00
|
|
|
return false
|
|
|
|
case 'p':
|
2013-11-06 20:49:01 +01:00
|
|
|
p.fmtPointer(reflect.ValueOf(arg), verb, goSyntax)
|
2011-10-22 18:19:46 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// Clear flags for base formatters.
|
|
|
|
// handleMethods needs them, so we must restore them later.
|
|
|
|
// We could call handleMethods here and avoid this work, but
|
|
|
|
// handleMethods is expensive enough to be worth delaying.
|
|
|
|
oldPlus := p.fmt.plus
|
|
|
|
oldSharp := p.fmt.sharp
|
|
|
|
if plus {
|
|
|
|
p.fmt.plus = false
|
|
|
|
}
|
|
|
|
if goSyntax {
|
|
|
|
p.fmt.sharp = false
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some types can be done without reflection.
|
2013-11-06 20:49:01 +01:00
|
|
|
switch f := arg.(type) {
|
2010-12-03 05:34:57 +01:00
|
|
|
case bool:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtBool(f, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case float32:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtFloat32(f, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case float64:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtFloat64(f, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case complex64:
|
2013-11-06 20:49:01 +01:00
|
|
|
p.fmtComplex64(f, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case complex128:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtComplex128(f, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtInt64(int64(f), verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int8:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtInt64(int64(f), verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int16:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtInt64(int64(f), verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int32:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtInt64(int64(f), verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int64:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtInt64(f, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint8:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint16:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint32:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint64:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtUint64(f, verb, goSyntax)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uintptr:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax)
|
2010-12-03 05:34:57 +01:00
|
|
|
case string:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtString(f, verb, goSyntax)
|
|
|
|
wasString = verb == 's' || verb == 'v'
|
2010-12-03 05:34:57 +01:00
|
|
|
case []byte:
|
2013-07-16 08:54:42 +02:00
|
|
|
p.fmtBytes(f, verb, goSyntax, nil, depth)
|
2011-11-30 00:02:54 +01:00
|
|
|
wasString = verb == 's'
|
|
|
|
default:
|
2012-10-23 06:31:11 +02:00
|
|
|
// Restore flags in case handleMethods finds a Formatter.
|
|
|
|
p.fmt.plus = oldPlus
|
|
|
|
p.fmt.sharp = oldSharp
|
|
|
|
// If the type is not simple, it might have methods.
|
2013-11-06 20:49:01 +01:00
|
|
|
if isString, handled := p.handleMethods(verb, plus, goSyntax, depth); handled {
|
|
|
|
return isString
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
2011-11-30 00:02:54 +01:00
|
|
|
// Need to use reflection
|
2013-11-06 20:49:01 +01:00
|
|
|
return p.printReflectValue(reflect.ValueOf(arg), verb, plus, goSyntax, depth)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
p.arg = nil
|
2011-11-30 00:02:54 +01:00
|
|
|
return
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// printValue is like printArg but starts with a reflect value, not an interface{} value.
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
|
2011-10-22 18:19:46 +02:00
|
|
|
if !value.IsValid() {
|
|
|
|
if verb == 'T' || verb == 'v' {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
} else {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
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':
|
2013-11-06 20:49:01 +01:00
|
|
|
p.printArg(value.Type().String(), 's', false, false, 0)
|
2011-10-22 18:19:46 +02:00
|
|
|
return false
|
|
|
|
case 'p':
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtPointer(value, verb, goSyntax)
|
2011-10-22 18:19:46 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle values with special methods.
|
2013-11-06 20:49:01 +01: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.
|
2011-10-22 18:19:46 +02:00
|
|
|
if value.CanInterface() {
|
2013-11-06 20:49:01 +01:00
|
|
|
p.arg = value.Interface()
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if isString, handled := p.handleMethods(verb, plus, goSyntax, depth); handled {
|
|
|
|
return isString
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return p.printReflectValue(value, verb, plus, goSyntax, depth)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// printReflectValue is the fallback for both printArg and printValue.
|
2011-10-22 18:19:46 +02:00
|
|
|
// It uses reflect to print the value.
|
2011-12-02 20:34:41 +01:00
|
|
|
func (p *pp) printReflectValue(value reflect.Value, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
|
2011-10-27 01:57:58 +02:00
|
|
|
oldValue := p.value
|
|
|
|
p.value = value
|
2010-12-03 05:34:57 +01:00
|
|
|
BigSwitch:
|
2011-05-20 02:18:15 +02:00
|
|
|
switch f := value; f.Kind() {
|
|
|
|
case reflect.Bool:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtBool(f.Bool(), verb)
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtInt64(f.Int(), verb)
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2013-11-06 20:49:01 +01:00
|
|
|
p.fmtUint64(f.Uint(), verb, goSyntax)
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2010-12-03 05:34:57 +01:00
|
|
|
if f.Type().Size() == 4 {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtFloat32(float32(f.Float()), verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
2013-11-06 20:49:01 +01:00
|
|
|
p.fmtFloat64(f.Float(), verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Complex64, reflect.Complex128:
|
2010-12-03 05:34:57 +01:00
|
|
|
if f.Type().Size() == 8 {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtComplex64(complex64(f.Complex()), verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
2013-11-06 20:49:01 +01:00
|
|
|
p.fmtComplex128(f.Complex(), verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.String:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtString(f.String(), verb, goSyntax)
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Map:
|
2010-12-03 05:34:57 +01:00
|
|
|
if goSyntax {
|
|
|
|
p.buf.WriteString(f.Type().String())
|
2011-12-13 00:40:51 +01:00
|
|
|
if f.IsNil() {
|
|
|
|
p.buf.WriteString("(nil)")
|
|
|
|
break
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('{')
|
|
|
|
} else {
|
|
|
|
p.buf.Write(mapBytes)
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
keys := f.MapKeys()
|
2010-12-03 05:34:57 +01:00
|
|
|
for i, key := range keys {
|
|
|
|
if i > 0 {
|
|
|
|
if goSyntax {
|
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
p.printValue(key, verb, plus, goSyntax, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte(':')
|
2011-10-22 18:19:46 +02:00
|
|
|
p.printValue(f.MapIndex(key), verb, plus, goSyntax, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if goSyntax {
|
|
|
|
p.buf.WriteByte('}')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Struct:
|
2010-12-03 05:34:57 +01:00
|
|
|
if goSyntax {
|
2011-10-22 18:19:46 +02:00
|
|
|
p.buf.WriteString(value.Type().String())
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
p.add('{')
|
|
|
|
v := f
|
2011-05-20 02:18:15 +02:00
|
|
|
t := v.Type()
|
2010-12-03 05:34:57 +01:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
if goSyntax {
|
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if plus || goSyntax {
|
|
|
|
if f := t.Field(i); f.Name != "" {
|
|
|
|
p.buf.WriteString(f.Name)
|
|
|
|
p.buf.WriteByte(':')
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
p.printValue(getField(v, i), verb, plus, goSyntax, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
p.buf.WriteByte('}')
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Interface:
|
2010-12-03 05:34:57 +01:00
|
|
|
value := f.Elem()
|
2011-05-20 02:18:15 +02:00
|
|
|
if !value.IsValid() {
|
2010-12-03 05:34:57 +01:00
|
|
|
if goSyntax {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.buf.WriteString(f.Type().String())
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.Write(nilParenBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
}
|
|
|
|
} else {
|
2011-10-27 01:57:58 +02:00
|
|
|
wasString = p.printValue(value, verb, plus, goSyntax, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Array, reflect.Slice:
|
2010-12-03 05:34:57 +01:00
|
|
|
// Byte slices are special.
|
2013-07-16 08:54:42 +02:00
|
|
|
if typ := f.Type(); typ.Elem().Kind() == reflect.Uint8 {
|
|
|
|
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())
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
p.fmtBytes(bytes, verb, goSyntax, typ, depth)
|
2011-10-27 01:57:58 +02:00
|
|
|
wasString = verb == 's'
|
|
|
|
break
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if goSyntax {
|
2011-10-22 18:19:46 +02:00
|
|
|
p.buf.WriteString(value.Type().String())
|
2011-12-13 20:16:27 +01:00
|
|
|
if f.Kind() == reflect.Slice && f.IsNil() {
|
2011-12-13 00:40:51 +01:00
|
|
|
p.buf.WriteString("(nil)")
|
|
|
|
break
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('{')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte('[')
|
|
|
|
}
|
|
|
|
for i := 0; i < f.Len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
if goSyntax {
|
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
p.printValue(f.Index(i), verb, plus, goSyntax, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if goSyntax {
|
|
|
|
p.buf.WriteByte('}')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Ptr:
|
|
|
|
v := f.Pointer()
|
2010-12-03 05:34:57 +01:00
|
|
|
// pointer to array or slice or struct? ok at top level
|
|
|
|
// but not embedded (avoid loops)
|
|
|
|
if v != 0 && depth == 0 {
|
2011-05-20 02:18:15 +02:00
|
|
|
switch a := f.Elem(); a.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('&')
|
2011-10-22 18:19:46 +02:00
|
|
|
p.printValue(a, verb, plus, goSyntax, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
break BigSwitch
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Struct:
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('&')
|
2011-10-22 18:19:46 +02:00
|
|
|
p.printValue(a, verb, plus, goSyntax, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
break BigSwitch
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
fallthrough
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Chan, reflect.Func, reflect.UnsafePointer:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.fmtPointer(value, verb, goSyntax)
|
2010-12-03 05:34:57 +01:00
|
|
|
default:
|
|
|
|
p.unknownType(f)
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
p.value = oldValue
|
|
|
|
return wasString
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01: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
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01: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
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func (p *pp) doPrintf(format string, a []interface{}) {
|
2011-01-21 19:19:03 +01:00
|
|
|
end := len(format)
|
2013-11-06 20:49:01 +01: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
|
2011-01-21 19:19:03 +01:00
|
|
|
for i := 0; i < end; {
|
2013-11-06 20:49:01 +01:00
|
|
|
p.goodArgNum = true
|
2011-01-21 19:19:03 +01:00
|
|
|
lasti := i
|
|
|
|
for i < end && format[i] != '%' {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i > lasti {
|
|
|
|
p.buf.WriteString(format[lasti:i])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
if i >= end {
|
|
|
|
// done processing format string
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process one verb
|
2010-12-03 05:34:57 +01:00
|
|
|
i++
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
// Do we have flags?
|
2010-12-03 05:34:57 +01: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
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
// Do we have an explicit argument index?
|
|
|
|
argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
|
|
|
|
|
|
|
|
// Do we have width?
|
2011-01-21 19:19:03 +01:00
|
|
|
if i < end && format[i] == '*' {
|
2013-11-06 20:49:01 +01:00
|
|
|
i++
|
|
|
|
p.fmt.wid, p.fmt.widPresent, argNum = intFromArg(a, argNum)
|
2010-12-03 05:34:57 +01:00
|
|
|
if !p.fmt.widPresent {
|
2013-01-29 21:52:43 +01:00
|
|
|
p.buf.Write(badWidthBytes)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
afterIndex = false
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
|
|
|
p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end)
|
2013-11-06 20:49:01 +01:00
|
|
|
if afterIndex && p.fmt.widPresent { // "%[3]2d"
|
|
|
|
p.goodArgNum = false
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
// Do we have precision?
|
2013-07-16 08:54:42 +02:00
|
|
|
if i+1 < end && format[i] == '.' {
|
2013-11-06 20:49:01 +01:00
|
|
|
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)
|
2010-12-03 05:34:57 +01:00
|
|
|
if !p.fmt.precPresent {
|
2013-01-29 21:52:43 +01:00
|
|
|
p.buf.Write(badPrecBytes)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
afterIndex = false
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
2013-11-06 20:49:01 +01:00
|
|
|
p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i, end)
|
2011-09-16 17:47:21 +02:00
|
|
|
if !p.fmt.precPresent {
|
|
|
|
p.fmt.prec = 0
|
|
|
|
p.fmt.precPresent = true
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
if !afterIndex {
|
|
|
|
argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
if i >= end {
|
|
|
|
p.buf.Write(noVerbBytes)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c, w := utf8.DecodeRuneInString(format[i:])
|
2010-12-03 05:34:57 +01:00
|
|
|
i += w
|
|
|
|
// percent is special - absorbs no operand
|
|
|
|
if c == '%' {
|
|
|
|
p.buf.WriteByte('%') // We ignore width and prec.
|
|
|
|
continue
|
|
|
|
}
|
2013-11-06 20:49:01 +01: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)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.add(c)
|
|
|
|
p.buf.Write(missingBytes)
|
|
|
|
continue
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
arg := a[argNum]
|
|
|
|
argNum++
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
goSyntax := c == 'v' && p.fmt.sharp
|
|
|
|
plus := c == 'v' && p.fmt.plus
|
2013-11-06 20:49:01 +01:00
|
|
|
p.printArg(arg, c, plus, goSyntax, 0)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01: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) {
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.Write(extraBytes)
|
2013-11-06 20:49:01 +01:00
|
|
|
for ; argNum < len(a); argNum++ {
|
|
|
|
arg := a[argNum]
|
|
|
|
if arg != nil {
|
|
|
|
p.buf.WriteString(reflect.TypeOf(arg).String())
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('=')
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
p.printArg(arg, 'v', false, false, 0)
|
|
|
|
if argNum+1 < len(a) {
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.buf.WriteByte(')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
|
|
|
|
prevString := false
|
2013-11-06 20:49:01 +01:00
|
|
|
for argNum := 0; argNum < len(a); argNum++ {
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.clearflags()
|
2013-01-29 21:52:43 +01:00
|
|
|
// always add spaces if we're doing Println
|
2013-11-06 20:49:01 +01:00
|
|
|
arg := a[argNum]
|
|
|
|
if argNum > 0 {
|
|
|
|
isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
|
2010-12-03 05:34:57 +01:00
|
|
|
if addspace || !isString && !prevString {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
prevString = p.printArg(arg, 'v', false, false, 0)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if addnewline {
|
|
|
|
p.buf.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|