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"
|
2019-01-18 20:04:36 +01:00
|
|
|
"internal/fmtsort"
|
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
|
|
|
)
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// Strings for use with buffer.WriteString.
|
|
|
|
// This is less overhead than using buffer.Write with byte arrays.
|
|
|
|
const (
|
|
|
|
commaSpaceString = ", "
|
|
|
|
nilAngleString = "<nil>"
|
|
|
|
nilParenString = "(nil)"
|
|
|
|
nilString = "nil"
|
|
|
|
mapString = "map["
|
|
|
|
percentBangString = "%!"
|
|
|
|
missingString = "(MISSING)"
|
|
|
|
badIndexString = "(BADINDEX)"
|
|
|
|
panicString = "(PANIC="
|
|
|
|
extraString = "%!(EXTRA "
|
|
|
|
badWidthString = "%!(BADWIDTH)"
|
|
|
|
badPrecString = "%!(BADPREC)"
|
|
|
|
noVerbString = "%!(NOVERB)"
|
|
|
|
invReflectString = "<invalid reflect.Value>"
|
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.
|
2016-07-22 20:15:38 +02:00
|
|
|
Write(b []byte) (n 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
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (b *buffer) Write(p []byte) {
|
2012-03-30 23:27:11 +02:00
|
|
|
*b = append(*b, p...)
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (b *buffer) WriteString(s string) {
|
2012-03-30 23:27:11 +02:00
|
|
|
*b = append(*b, s...)
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (b *buffer) WriteByte(c byte) {
|
2012-03-30 23:27:11 +02:00
|
|
|
*b = append(*b, c)
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (bp *buffer) WriteRune(r rune) {
|
2012-03-30 23:27:11 +02:00
|
|
|
if r < utf8.RuneSelf {
|
|
|
|
*bp = append(*bp, byte(r))
|
2016-07-22 20:15:38 +02:00
|
|
|
return
|
2012-03-30 23:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// pp is used to store a printer's state and is reused with sync.Pool to avoid allocations.
|
2010-12-03 05:34:57 +01:00
|
|
|
type pp struct {
|
2016-07-22 20:15:38 +02:00
|
|
|
buf buffer
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// arg holds the current item, as an interface{}.
|
|
|
|
arg interface{}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
// value is used instead of arg for reflect values.
|
2013-11-06 20:49:01 +01:00
|
|
|
value reflect.Value
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
// fmt is used to format basic items such as integers or strings.
|
|
|
|
fmt fmt
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// reordered records whether the format string used argument reordering.
|
|
|
|
reordered bool
|
|
|
|
// goodArgNum records whether the most recent reordering directive was valid.
|
|
|
|
goodArgNum bool
|
2016-07-22 20:15:38 +02:00
|
|
|
// panicking is set by catchPanic to avoid infinite panic, recover, panic, ... recursion.
|
|
|
|
panicking bool
|
|
|
|
// erroring is set when printing an error string to guard against calling handleMethods.
|
|
|
|
erroring bool
|
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
|
|
|
}
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
// newPrinter allocates a new pp struct or grabs 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() {
|
2019-01-18 20:04:36 +01:00
|
|
|
// Proper usage of a sync.Pool requires each entry to have approximately
|
|
|
|
// the same memory cost. To obtain this property when the stored type
|
|
|
|
// contains a variably-sized buffer, we add a hard limit on the maximum buffer
|
|
|
|
// to place back in the pool.
|
|
|
|
//
|
|
|
|
// See https://golang.org/issue/23199
|
|
|
|
if cap(p.buf) > 64<<10 {
|
|
|
|
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 '+':
|
2016-07-22 20:15:38 +02:00
|
|
|
return p.fmt.plus || p.fmt.plusV
|
2010-12-03 05:34:57 +01:00
|
|
|
case '#':
|
2016-07-22 20:15:38 +02:00
|
|
|
return p.fmt.sharp || p.fmt.sharpV
|
2010-12-03 05:34:57 +01:00
|
|
|
case ' ':
|
|
|
|
return p.fmt.space
|
|
|
|
case '0':
|
|
|
|
return p.fmt.zero
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.Write(b)
|
|
|
|
return len(b), nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
// Implement WriteString so that we can call io.WriteString
|
|
|
|
// on a pp (through state), for efficiency.
|
|
|
|
func (p *pp) WriteString(s string) (ret int, err error) {
|
|
|
|
p.buf.WriteString(s)
|
|
|
|
return len(s), nil
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// 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()
|
2016-07-22 20:15:38 +02:00
|
|
|
p.doPrint(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
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2016-07-22 20:15:38 +02:00
|
|
|
p.doPrint(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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2016-07-22 20:15:38 +02:00
|
|
|
p.doPrintln(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
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2016-07-22 20:15:38 +02:00
|
|
|
p.doPrintln(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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// tooLarge reports whether the magnitude of the integer is
|
|
|
|
// too large to be used as a formatting width or precision.
|
|
|
|
func tooLarge(x int) bool {
|
|
|
|
const max int = 1e6
|
|
|
|
return x > max || x < -max
|
|
|
|
}
|
|
|
|
|
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++ {
|
2015-10-31 01:59:47 +01:00
|
|
|
if tooLarge(num) {
|
|
|
|
return 0, false, end // Overflow; crazy long number most likely.
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
num = num*10 + int(s[newi]-'0')
|
|
|
|
isnum = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
func (p *pp) unknownType(v reflect.Value) {
|
|
|
|
if !v.IsValid() {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(nilAngleString)
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.buf.WriteByte('?')
|
2015-01-15 01:27:56 +01:00
|
|
|
p.buf.WriteString(v.Type().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
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(percentBangString)
|
|
|
|
p.buf.WriteRune(verb)
|
|
|
|
p.buf.WriteByte('(')
|
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())
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteByte('=')
|
|
|
|
p.printArg(p.arg, 'v')
|
2011-10-27 01:57:58 +02:00
|
|
|
case p.value.IsValid():
|
|
|
|
p.buf.WriteString(p.value.Type().String())
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteByte('=')
|
2015-01-15 01:27:56 +01:00
|
|
|
p.printValue(p.value, 'v', 0)
|
2011-10-22 18:19:46 +02:00
|
|
|
default:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(nilAngleString)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteByte(')')
|
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':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtBoolean(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-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
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtInteger(v, 16, unsigned, ldigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.fmt.sharp = sharp
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// fmtInteger formats a signed or unsigned integer.
|
|
|
|
func (p *pp) fmtInteger(v uint64, isSigned bool, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
|
|
|
case 'v':
|
2016-07-22 20:15:38 +02:00
|
|
|
if p.fmt.sharpV && !isSigned {
|
2011-03-17 00:05:44 +01:00
|
|
|
p.fmt0x64(v, true)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtInteger(v, 10, isSigned, ldigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'd':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtInteger(v, 10, isSigned, ldigits)
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'b':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtInteger(v, 2, isSigned, ldigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'o':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtInteger(v, 8, isSigned, ldigits)
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'x':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtInteger(v, 16, isSigned, ldigits)
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'X':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtInteger(v, 16, isSigned, udigits)
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'c':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtC(v)
|
2011-09-16 17:47:21 +02:00
|
|
|
case 'q':
|
2016-07-22 20:15:38 +02:00
|
|
|
if v <= utf8.MaxRune {
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtQc(v)
|
2011-09-16 17:47:21 +02:00
|
|
|
} else {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case 'U':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtUnicode(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// fmtFloat formats a float. The default precision for each verb
|
|
|
|
// is specified as last argument in the call to fmt_float.
|
|
|
|
func (p *pp) fmtFloat(v float64, size int, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
|
|
|
case 'v':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtFloat(v, size, 'g', -1)
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'b', 'g', 'G':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtFloat(v, size, verb, -1)
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'f', 'e', 'E':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtFloat(v, size, verb, 6)
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'F':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtFloat(v, size, 'f', 6)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// fmtComplex formats a complex number v with
|
|
|
|
// r = real(v) and j = imag(v) as (r+ji) using
|
|
|
|
// fmtFloat for r and j formatting.
|
|
|
|
func (p *pp) fmtComplex(v complex128, size int, verb rune) {
|
|
|
|
// Make sure any unsupported verbs are found before the
|
|
|
|
// calls to fmtFloat to not generate an incorrect error string.
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'v', 'b', 'g', 'G', 'f', 'F', 'e', 'E':
|
|
|
|
oldPlus := p.fmt.plus
|
|
|
|
p.buf.WriteByte('(')
|
|
|
|
p.fmtFloat(real(v), size/2, verb)
|
|
|
|
// Imaginary part always has a sign.
|
|
|
|
p.fmt.plus = true
|
|
|
|
p.fmtFloat(imag(v), size/2, verb)
|
|
|
|
p.buf.WriteString("i)")
|
|
|
|
p.fmt.plus = oldPlus
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
func (p *pp) fmtString(v string, verb rune) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch verb {
|
|
|
|
case 'v':
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtQ(v)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtS(v)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
case 's':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtS(v)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'x':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtSx(v, ldigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'X':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtSx(v, udigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'q':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtQ(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (p *pp) fmtBytes(v []byte, verb rune, typeString string) {
|
|
|
|
switch verb {
|
|
|
|
case 'v', 'd':
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(typeString)
|
2014-07-19 10:53:52 +02:00
|
|
|
if v == nil {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(nilParenString)
|
2014-07-19 10:53:52 +02:00
|
|
|
return
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteByte('{')
|
|
|
|
for i, c := range v {
|
|
|
|
if i > 0 {
|
|
|
|
p.buf.WriteString(commaSpaceString)
|
|
|
|
}
|
|
|
|
p.fmt0x64(uint64(c), true)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteByte('}')
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
|
|
|
p.buf.WriteByte('[')
|
2016-07-22 20:15:38 +02:00
|
|
|
for i, c := range v {
|
|
|
|
if i > 0 {
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtInteger(uint64(c), 10, unsigned, ldigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
|
|
|
case 's':
|
2019-01-18 20:04:36 +01:00
|
|
|
p.fmt.fmtBs(v)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'x':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtBx(v, ldigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'X':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtBx(v, udigits)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'q':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtQ(string(v))
|
2010-12-03 05:34:57 +01:00
|
|
|
default:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.printValue(reflect.ValueOf(v), verb, 0)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
func (p *pp) fmtPointer(value reflect.Value, verb rune) {
|
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
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
switch verb {
|
|
|
|
case 'v':
|
|
|
|
if p.fmt.sharpV {
|
|
|
|
p.buf.WriteByte('(')
|
|
|
|
p.buf.WriteString(value.Type().String())
|
|
|
|
p.buf.WriteString(")(")
|
|
|
|
if u == 0 {
|
|
|
|
p.buf.WriteString(nilString)
|
|
|
|
} else {
|
|
|
|
p.fmt0x64(uint64(u), true)
|
|
|
|
}
|
|
|
|
p.buf.WriteByte(')')
|
2012-10-23 06:31:11 +02:00
|
|
|
} else {
|
2016-07-22 20:15:38 +02:00
|
|
|
if u == 0 {
|
|
|
|
p.fmt.padString(nilAngleString)
|
|
|
|
} else {
|
|
|
|
p.fmt0x64(uint64(u), !p.fmt.sharp)
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
case 'p':
|
|
|
|
p.fmt0x64(uint64(u), !p.fmt.sharp)
|
|
|
|
case 'b', 'o', 'd', 'x', 'X':
|
|
|
|
p.fmtInteger(uint64(u), unsigned, verb)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
func (p *pp) catchPanic(arg interface{}, verb rune, method string) {
|
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() {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(nilAngleString)
|
2011-09-16 17:47:21 +02:00
|
|
|
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)
|
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
|
|
|
|
oldFlags := p.fmt.fmtFlags
|
|
|
|
// For this output we want default behavior.
|
|
|
|
p.fmt.clearflags()
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(percentBangString)
|
|
|
|
p.buf.WriteRune(verb)
|
|
|
|
p.buf.WriteString(panicString)
|
2019-01-18 20:04:36 +01:00
|
|
|
p.buf.WriteString(method)
|
|
|
|
p.buf.WriteString(" method: ")
|
2011-09-16 17:47:21 +02:00
|
|
|
p.panicking = true
|
2016-07-22 20:15:38 +02:00
|
|
|
p.printArg(err, 'v')
|
2011-09-16 17:47:21 +02:00
|
|
|
p.panicking = false
|
|
|
|
p.buf.WriteByte(')')
|
2017-01-14 01:05:42 +01:00
|
|
|
|
|
|
|
p.fmt.fmtFlags = oldFlags
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (p *pp) handleMethods(verb rune) (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
|
2019-01-18 20:04:36 +01:00
|
|
|
defer p.catchPanic(p.arg, verb, "Format")
|
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
|
|
|
}
|
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.
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2013-11-06 20:49:01 +01:00
|
|
|
if stringer, ok := p.arg.(GoStringer); ok {
|
2011-10-22 18:19:46 +02:00
|
|
|
handled = true
|
2019-01-18 20:04:36 +01:00
|
|
|
defer p.catchPanic(p.arg, verb, "GoString")
|
2010-12-03 05:34:57 +01:00
|
|
|
// Print the result of GoString unadorned.
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtS(stringer.GoString())
|
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:
|
2015-01-15 01:27:56 +01:00
|
|
|
// setting handled and deferring catchPanic
|
2011-12-14 16:41:54 +01:00
|
|
|
// 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:
|
|
|
|
handled = true
|
2019-01-18 20:04:36 +01:00
|
|
|
defer p.catchPanic(p.arg, verb, "Error")
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtString(v.Error(), verb)
|
2011-12-14 16:41:54 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
case Stringer:
|
|
|
|
handled = true
|
2019-01-18 20:04:36 +01:00
|
|
|
defer p.catchPanic(p.arg, verb, "String")
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtString(v.String(), verb)
|
2011-12-14 16:41:54 +01:00
|
|
|
return
|
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
return false
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (p *pp) printArg(arg interface{}, verb rune) {
|
2013-11-06 20:49:01 +01:00
|
|
|
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 {
|
2016-07-22 20:15:38 +02:00
|
|
|
switch verb {
|
|
|
|
case 'T', 'v':
|
|
|
|
p.fmt.padString(nilAngleString)
|
|
|
|
default:
|
2011-11-30 00:02:54 +01:00
|
|
|
p.badVerb(verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
return
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special processing considerations.
|
|
|
|
// %T (the value's type) and %p (its address) are special; we always do them first.
|
|
|
|
switch verb {
|
|
|
|
case 'T':
|
2018-09-24 23:46:21 +02:00
|
|
|
p.fmt.fmtS(reflect.TypeOf(arg).String())
|
2016-07-22 20:15:38 +02:00
|
|
|
return
|
2011-10-22 18:19:46 +02:00
|
|
|
case 'p':
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtPointer(reflect.ValueOf(arg), 'p')
|
|
|
|
return
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
|
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:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtFloat(float64(f), 32, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case float64:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtFloat(f, 64, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case complex64:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtComplex(complex128(f), 64, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case complex128:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtComplex(f, 128, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), signed, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int8:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), signed, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int16:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), signed, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int32:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), signed, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case int64:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), signed, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), unsigned, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint8:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), unsigned, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint16:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), unsigned, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint32:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), unsigned, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uint64:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(f, unsigned, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case uintptr:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f), unsigned, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case string:
|
2015-01-15 01:27:56 +01:00
|
|
|
p.fmtString(f, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
case []byte:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtBytes(f, verb, "[]byte")
|
2015-10-31 01:59:47 +01:00
|
|
|
case reflect.Value:
|
2017-01-14 01:05:42 +01:00
|
|
|
// Handle extractable values with special methods
|
|
|
|
// since printValue does not handle them at depth 0.
|
|
|
|
if f.IsValid() && f.CanInterface() {
|
|
|
|
p.arg = f.Interface()
|
|
|
|
if p.handleMethods(verb) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.printValue(f, verb, 0)
|
2011-11-30 00:02:54 +01:00
|
|
|
default:
|
2012-10-23 06:31:11 +02:00
|
|
|
// If the type is not simple, it might have methods.
|
2016-07-22 20:15:38 +02:00
|
|
|
if !p.handleMethods(verb) {
|
|
|
|
// Need to use reflection, since the type had no
|
|
|
|
// interface methods that could be used for formatting.
|
|
|
|
p.printValue(reflect.ValueOf(f), verb, 0)
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// printValue is similar to printArg but starts with a reflect value, not an interface{} value.
|
|
|
|
// It does not handle 'p' and 'T' verbs because these should have been already handled by printArg.
|
|
|
|
func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
|
|
|
|
// Handle values with special methods if not already handled by printArg (depth == 0).
|
|
|
|
if depth > 0 && value.IsValid() && value.CanInterface() {
|
2013-11-06 20:49:01 +01:00
|
|
|
p.arg = value.Interface()
|
2016-07-22 20:15:38 +02:00
|
|
|
if p.handleMethods(verb) {
|
|
|
|
return
|
|
|
|
}
|
2011-10-22 18:19:46 +02:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.arg = nil
|
2011-10-27 01:57:58 +02:00
|
|
|
p.value = value
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
switch f := value; value.Kind() {
|
2015-10-31 01:59:47 +01:00
|
|
|
case reflect.Invalid:
|
2016-07-22 20:15:38 +02:00
|
|
|
if depth == 0 {
|
|
|
|
p.buf.WriteString(invReflectString)
|
|
|
|
} else {
|
|
|
|
switch verb {
|
|
|
|
case 'v':
|
|
|
|
p.buf.WriteString(nilAngleString)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb)
|
|
|
|
}
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
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:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(uint64(f.Int()), signed, verb)
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtInteger(f.Uint(), unsigned, verb)
|
|
|
|
case reflect.Float32:
|
|
|
|
p.fmtFloat(f.Float(), 32, verb)
|
|
|
|
case reflect.Float64:
|
|
|
|
p.fmtFloat(f.Float(), 64, verb)
|
|
|
|
case reflect.Complex64:
|
|
|
|
p.fmtComplex(f.Complex(), 64, verb)
|
|
|
|
case reflect.Complex128:
|
|
|
|
p.fmtComplex(f.Complex(), 128, verb)
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.String:
|
2015-01-15 01:27:56 +01:00
|
|
|
p.fmtString(f.String(), verb)
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Map:
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteString(f.Type().String())
|
2011-12-13 00:40:51 +01:00
|
|
|
if f.IsNil() {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(nilParenString)
|
|
|
|
return
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('{')
|
|
|
|
} else {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(mapString)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2019-01-18 20:04:36 +01:00
|
|
|
sorted := fmtsort.Sort(f)
|
|
|
|
for i, key := range sorted.Key {
|
2010-12-03 05:34:57 +01:00
|
|
|
if i > 0 {
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(commaSpaceString)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
p.printValue(key, verb, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte(':')
|
2019-01-18 20:04:36 +01:00
|
|
|
p.printValue(sorted.Value[i], verb, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('}')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Struct:
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(f.Type().String())
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteByte('{')
|
|
|
|
for i := 0; i < f.NumField(); i++ {
|
2010-12-03 05:34:57 +01:00
|
|
|
if i > 0 {
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(commaSpaceString)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.plusV || p.fmt.sharpV {
|
2016-07-22 20:15:38 +02:00
|
|
|
if name := f.Type().Field(i).Name; name != "" {
|
|
|
|
p.buf.WriteString(name)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte(':')
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.printValue(getField(f, i), verb, 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() {
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2011-11-30 00:02:54 +01:00
|
|
|
p.buf.WriteString(f.Type().String())
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(nilParenString)
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(nilAngleString)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.printValue(value, verb, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Array, reflect.Slice:
|
2016-07-22 20:15:38 +02:00
|
|
|
switch verb {
|
|
|
|
case 's', 'q', 'x', 'X':
|
|
|
|
// Handle byte and uint8 slices and arrays special for the above verbs.
|
|
|
|
t := f.Type()
|
|
|
|
if t.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())
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtBytes(bytes, verb, t.String())
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
if p.fmt.sharpV {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(f.Type().String())
|
2011-12-13 20:16:27 +01:00
|
|
|
if f.Kind() == reflect.Slice && f.IsNil() {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(nilParenString)
|
|
|
|
return
|
2017-01-14 01:05:42 +01:00
|
|
|
}
|
|
|
|
p.buf.WriteByte('{')
|
|
|
|
for i := 0; i < f.Len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
p.buf.WriteString(commaSpaceString)
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
p.printValue(f.Index(i), verb, depth+1)
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
p.buf.WriteByte('}')
|
2010-12-03 05:34:57 +01:00
|
|
|
} else {
|
|
|
|
p.buf.WriteByte('[')
|
2016-07-22 20:15:38 +02:00
|
|
|
for i := 0; i < f.Len(); i++ {
|
|
|
|
if i > 0 {
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.printValue(f.Index(i), verb, depth+1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Ptr:
|
2018-01-09 02:23:08 +01:00
|
|
|
// pointer to array or slice or struct? ok at top level
|
2010-12-03 05:34:57 +01:00
|
|
|
// but not embedded (avoid loops)
|
2016-07-22 20:15:38 +02:00
|
|
|
if depth == 0 && f.Pointer() != 0 {
|
2011-05-20 02:18:15 +02:00
|
|
|
switch a := f.Elem(); a.Kind() {
|
2016-07-22 20:15:38 +02:00
|
|
|
case reflect.Array, reflect.Slice, reflect.Struct, reflect.Map:
|
2015-01-15 01:27:56 +01:00
|
|
|
p.buf.WriteByte('&')
|
|
|
|
p.printValue(a, verb, depth+1)
|
2016-07-22 20:15:38 +02:00
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
fallthrough
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Chan, reflect.Func, reflect.UnsafePointer:
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmtPointer(f, verb)
|
2010-12-03 05:34:57 +01:00
|
|
|
default:
|
|
|
|
p.unknownType(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
// intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type.
|
2013-11-06 20:49:01 +01:00
|
|
|
func intFromArg(a []interface{}, argNum int) (num int, isInt bool, newArgNum int) {
|
|
|
|
newArgNum = argNum
|
|
|
|
if argNum < len(a) {
|
2016-02-03 22:58:02 +01:00
|
|
|
num, isInt = a[argNum].(int) // Almost always OK.
|
|
|
|
if !isInt {
|
|
|
|
// Work harder.
|
|
|
|
switch v := reflect.ValueOf(a[argNum]); v.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
n := v.Int()
|
|
|
|
if int64(int(n)) == n {
|
|
|
|
num = int(n)
|
|
|
|
isInt = true
|
|
|
|
}
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
n := v.Uint()
|
|
|
|
if int64(n) >= 0 && uint64(int(n)) == n {
|
|
|
|
num = int(n)
|
|
|
|
isInt = true
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Already 0, false.
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
newArgNum = argNum + 1
|
2015-10-31 01:59:47 +01:00
|
|
|
if tooLarge(num) {
|
|
|
|
num = 0
|
|
|
|
isInt = false
|
|
|
|
}
|
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) {
|
2015-10-31 01:59:47 +01:00
|
|
|
// There must be at least 3 bytes: [n].
|
|
|
|
if len(format) < 3 {
|
|
|
|
return 0, 1, false
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// 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
|
2015-10-31 01:59:47 +01:00
|
|
|
return argNum, i + wid, ok
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (p *pp) badArgNum(verb rune) {
|
|
|
|
p.buf.WriteString(percentBangString)
|
|
|
|
p.buf.WriteRune(verb)
|
|
|
|
p.buf.WriteString(badIndexString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) missingArg(verb rune) {
|
|
|
|
p.buf.WriteString(percentBangString)
|
|
|
|
p.buf.WriteRune(verb)
|
|
|
|
p.buf.WriteString(missingString)
|
|
|
|
}
|
|
|
|
|
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
|
2016-07-22 20:15:38 +02:00
|
|
|
formatLoop:
|
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()
|
2016-07-22 20:15:38 +02:00
|
|
|
simpleFormat:
|
2010-12-03 05:34:57 +01:00
|
|
|
for ; i < end; i++ {
|
2016-07-22 20:15:38 +02:00
|
|
|
c := format[i]
|
|
|
|
switch c {
|
2010-12-03 05:34:57 +01:00
|
|
|
case '#':
|
|
|
|
p.fmt.sharp = true
|
|
|
|
case '0':
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmt.zero = !p.fmt.minus // Only allow zero padding to the left.
|
2010-12-03 05:34:57 +01:00
|
|
|
case '+':
|
|
|
|
p.fmt.plus = true
|
|
|
|
case '-':
|
|
|
|
p.fmt.minus = true
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmt.zero = false // Do not pad with zeros to the right.
|
2010-12-03 05:34:57 +01:00
|
|
|
case ' ':
|
|
|
|
p.fmt.space = true
|
|
|
|
default:
|
2016-07-22 20:15:38 +02:00
|
|
|
// Fast path for common case of ascii lower case simple verbs
|
|
|
|
// without precision or width or argument indices.
|
|
|
|
if 'a' <= c && c <= 'z' && argNum < len(a) {
|
|
|
|
if c == 'v' {
|
|
|
|
// Go syntax
|
|
|
|
p.fmt.sharpV = p.fmt.sharp
|
|
|
|
p.fmt.sharp = false
|
|
|
|
// Struct-field syntax
|
|
|
|
p.fmt.plusV = p.fmt.plus
|
|
|
|
p.fmt.plus = false
|
|
|
|
}
|
|
|
|
p.printArg(a[argNum], rune(c))
|
|
|
|
argNum++
|
|
|
|
i++
|
|
|
|
continue formatLoop
|
|
|
|
}
|
|
|
|
// Format is more complex than simple flags and a verb or is malformed.
|
|
|
|
break simpleFormat
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
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)
|
2015-10-31 01:59:47 +01:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if !p.fmt.widPresent {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(badWidthString)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
|
|
|
// We have a negative width, so take its value and ensure
|
|
|
|
// that the minus flag is set
|
|
|
|
if p.fmt.wid < 0 {
|
|
|
|
p.fmt.wid = -p.fmt.wid
|
|
|
|
p.fmt.minus = true
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmt.zero = false // Do not pad with zeros to the right.
|
2015-10-31 01:59:47 +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))
|
2015-10-31 01:59:47 +01:00
|
|
|
if i < end && format[i] == '*' {
|
2013-11-06 20:49:01 +01:00
|
|
|
i++
|
|
|
|
p.fmt.prec, p.fmt.precPresent, argNum = intFromArg(a, argNum)
|
2015-10-31 01:59:47 +01:00
|
|
|
// Negative precision arguments don't make sense
|
|
|
|
if p.fmt.prec < 0 {
|
|
|
|
p.fmt.prec = 0
|
|
|
|
p.fmt.precPresent = false
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
if !p.fmt.precPresent {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(badPrecString)
|
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 {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteString(noVerbString)
|
|
|
|
break
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
verb, size := rune(format[i]), 1
|
|
|
|
if verb >= utf8.RuneSelf {
|
|
|
|
verb, size = utf8.DecodeRuneInString(format[i:])
|
|
|
|
}
|
|
|
|
i += size
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case verb == '%': // Percent does not absorb operands and ignores f.wid and f.prec.
|
|
|
|
p.buf.WriteByte('%')
|
|
|
|
case !p.goodArgNum:
|
|
|
|
p.badArgNum(verb)
|
|
|
|
case argNum >= len(a): // No argument left over to print for the current verb.
|
|
|
|
p.missingArg(verb)
|
|
|
|
case verb == 'v':
|
|
|
|
// Go syntax
|
|
|
|
p.fmt.sharpV = p.fmt.sharp
|
|
|
|
p.fmt.sharp = false
|
|
|
|
// Struct-field syntax
|
|
|
|
p.fmt.plusV = p.fmt.plus
|
|
|
|
p.fmt.plus = false
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
p.printArg(a[argNum], verb)
|
|
|
|
argNum++
|
2015-01-15 01:27:56 +01:00
|
|
|
}
|
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) {
|
2016-07-22 20:15:38 +02:00
|
|
|
p.fmt.clearflags()
|
|
|
|
p.buf.WriteString(extraString)
|
|
|
|
for i, arg := range a[argNum:] {
|
|
|
|
if i > 0 {
|
|
|
|
p.buf.WriteString(commaSpaceString)
|
|
|
|
}
|
|
|
|
if arg == nil {
|
|
|
|
p.buf.WriteString(nilAngleString)
|
|
|
|
} else {
|
2013-11-06 20:49:01 +01:00
|
|
|
p.buf.WriteString(reflect.TypeOf(arg).String())
|
2010-12-03 05:34:57 +01:00
|
|
|
p.buf.WriteByte('=')
|
2016-07-22 20:15:38 +02:00
|
|
|
p.printArg(arg, 'v')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.buf.WriteByte(')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (p *pp) doPrint(a []interface{}) {
|
2010-12-03 05:34:57 +01:00
|
|
|
prevString := false
|
2016-07-22 20:15:38 +02:00
|
|
|
for argNum, arg := range a {
|
|
|
|
isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
|
|
|
|
// Add a space between two non-string arguments.
|
|
|
|
if argNum > 0 && !isString && !prevString {
|
|
|
|
p.buf.WriteByte(' ')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.printArg(arg, 'v')
|
|
|
|
prevString = isString
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// doPrintln is like doPrint but always adds a space between arguments
|
|
|
|
// and a newline after the last argument.
|
|
|
|
func (p *pp) doPrintln(a []interface{}) {
|
|
|
|
for argNum, arg := range a {
|
|
|
|
if argNum > 0 {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
p.printArg(arg, 'v')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
p.buf.WriteByte('\n')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|