github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/fmt/print.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fmt
     6  
     7  import (
     8  	"errors"
     9  	"io"
    10  	"os"
    11  	"reflect"
    12  	"sync"
    13  	"unicode/utf8"
    14  )
    15  
    16  // Some constants in the form of bytes, to avoid string overhead.
    17  // Needlessly fastidious, I suppose.
    18  var (
    19  	commaSpaceBytes  = []byte(", ")
    20  	nilAngleBytes    = []byte("<nil>")
    21  	nilParenBytes    = []byte("(nil)")
    22  	nilBytes         = []byte("nil")
    23  	mapBytes         = []byte("map[")
    24  	percentBangBytes = []byte("%!")
    25  	missingBytes     = []byte("(MISSING)")
    26  	badIndexBytes    = []byte("(BADINDEX)")
    27  	panicBytes       = []byte("(PANIC=")
    28  	extraBytes       = []byte("%!(EXTRA ")
    29  	irparenBytes     = []byte("i)")
    30  	bytesBytes       = []byte("[]byte{")
    31  	badWidthBytes    = []byte("%!(BADWIDTH)")
    32  	badPrecBytes     = []byte("%!(BADPREC)")
    33  	noVerbBytes      = []byte("%!(NOVERB)")
    34  )
    35  
    36  // State represents the printer state passed to custom formatters.
    37  // It provides access to the io.Writer interface plus information about
    38  // the flags and options for the operand's format specifier.
    39  type State interface {
    40  	// Write is the function to call to emit formatted output to be printed.
    41  	Write(b []byte) (ret int, err error)
    42  	// Width returns the value of the width option and whether it has been set.
    43  	Width() (wid int, ok bool)
    44  	// Precision returns the value of the precision option and whether it has been set.
    45  	Precision() (prec int, ok bool)
    46  
    47  	// Flag reports whether the flag c, a character, has been set.
    48  	Flag(c int) bool
    49  }
    50  
    51  // Formatter is the interface implemented by values with a custom formatter.
    52  // The implementation of Format may call Sprint(f) or Fprint(f) etc.
    53  // to generate its output.
    54  type Formatter interface {
    55  	Format(f State, c rune)
    56  }
    57  
    58  // Stringer is implemented by any value that has a String method,
    59  // which defines the ``native'' format for that value.
    60  // The String method is used to print values passed as an operand
    61  // to any format that accepts a string or to an unformatted printer
    62  // such as Print.
    63  type Stringer interface {
    64  	String() string
    65  }
    66  
    67  // GoStringer is implemented by any value that has a GoString method,
    68  // which defines the Go syntax for that value.
    69  // The GoString method is used to print values passed as an operand
    70  // to a %#v format.
    71  type GoStringer interface {
    72  	GoString() string
    73  }
    74  
    75  // Use simple []byte instead of bytes.Buffer to avoid large dependency.
    76  type buffer []byte
    77  
    78  func (b *buffer) Write(p []byte) (n int, err error) {
    79  	*b = append(*b, p...)
    80  	return len(p), nil
    81  }
    82  
    83  func (b *buffer) WriteString(s string) (n int, err error) {
    84  	*b = append(*b, s...)
    85  	return len(s), nil
    86  }
    87  
    88  func (b *buffer) WriteByte(c byte) error {
    89  	*b = append(*b, c)
    90  	return nil
    91  }
    92  
    93  func (bp *buffer) WriteRune(r rune) error {
    94  	if r < utf8.RuneSelf {
    95  		*bp = append(*bp, byte(r))
    96  		return nil
    97  	}
    98  
    99  	b := *bp
   100  	n := len(b)
   101  	for n+utf8.UTFMax > cap(b) {
   102  		b = append(b, 0)
   103  	}
   104  	w := utf8.EncodeRune(b[n:n+utf8.UTFMax], r)
   105  	*bp = b[:n+w]
   106  	return nil
   107  }
   108  
   109  type pp struct {
   110  	n         int
   111  	panicking bool
   112  	erroring  bool // printing an error condition
   113  	buf       buffer
   114  	// arg holds the current item, as an interface{}.
   115  	arg interface{}
   116  	// value holds the current item, as a reflect.Value, and will be
   117  	// the zero Value if the item has not been reflected.
   118  	value reflect.Value
   119  	// reordered records whether the format string used argument reordering.
   120  	reordered bool
   121  	// goodArgNum records whether the most recent reordering directive was valid.
   122  	goodArgNum bool
   123  	runeBuf    [utf8.UTFMax]byte
   124  	fmt        fmt
   125  }
   126  
   127  var ppFree = sync.Pool{
   128  	New: func() interface{} { return new(pp) },
   129  }
   130  
   131  // newPrinter allocates a new pp struct or grabs a cached one.
   132  func newPrinter() *pp {
   133  	p := ppFree.Get().(*pp)
   134  	p.panicking = false
   135  	p.erroring = false
   136  	p.fmt.init(&p.buf)
   137  	return p
   138  }
   139  
   140  // free saves used pp structs in ppFree; avoids an allocation per invocation.
   141  func (p *pp) free() {
   142  	// Don't hold on to pp structs with large buffers.
   143  	if cap(p.buf) > 1024 {
   144  		return
   145  	}
   146  	p.buf = p.buf[:0]
   147  	p.arg = nil
   148  	p.value = reflect.Value{}
   149  	ppFree.Put(p)
   150  }
   151  
   152  func (p *pp) Width() (wid int, ok bool) { return p.fmt.wid, p.fmt.widPresent }
   153  
   154  func (p *pp) Precision() (prec int, ok bool) { return p.fmt.prec, p.fmt.precPresent }
   155  
   156  func (p *pp) Flag(b int) bool {
   157  	switch b {
   158  	case '-':
   159  		return p.fmt.minus
   160  	case '+':
   161  		return p.fmt.plus
   162  	case '#':
   163  		return p.fmt.sharp
   164  	case ' ':
   165  		return p.fmt.space
   166  	case '0':
   167  		return p.fmt.zero
   168  	}
   169  	return false
   170  }
   171  
   172  func (p *pp) add(c rune) {
   173  	p.buf.WriteRune(c)
   174  }
   175  
   176  // Implement Write so we can call Fprintf on a pp (through State), for
   177  // recursive use in custom verbs.
   178  func (p *pp) Write(b []byte) (ret int, err error) {
   179  	return p.buf.Write(b)
   180  }
   181  
   182  // These routines end in 'f' and take a format string.
   183  
   184  // Fprintf formats according to a format specifier and writes to w.
   185  // It returns the number of bytes written and any write error encountered.
   186  func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
   187  	p := newPrinter()
   188  	p.doPrintf(format, a)
   189  	n, err = w.Write(p.buf)
   190  	p.free()
   191  	return
   192  }
   193  
   194  // Printf formats according to a format specifier and writes to standard output.
   195  // It returns the number of bytes written and any write error encountered.
   196  func Printf(format string, a ...interface{}) (n int, err error) {
   197  	return Fprintf(os.Stdout, format, a...)
   198  }
   199  
   200  // Sprintf formats according to a format specifier and returns the resulting string.
   201  func Sprintf(format string, a ...interface{}) string {
   202  	p := newPrinter()
   203  	p.doPrintf(format, a)
   204  	s := string(p.buf)
   205  	p.free()
   206  	return s
   207  }
   208  
   209  // Errorf formats according to a format specifier and returns the string
   210  // as a value that satisfies error.
   211  func Errorf(format string, a ...interface{}) error {
   212  	return errors.New(Sprintf(format, a...))
   213  }
   214  
   215  // These routines do not take a format string
   216  
   217  // Fprint formats using the default formats for its operands and writes to w.
   218  // Spaces are added between operands when neither is a string.
   219  // It returns the number of bytes written and any write error encountered.
   220  func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
   221  	p := newPrinter()
   222  	p.doPrint(a, false, false)
   223  	n, err = w.Write(p.buf)
   224  	p.free()
   225  	return
   226  }
   227  
   228  // Print formats using the default formats for its operands and writes to standard output.
   229  // Spaces are added between operands when neither is a string.
   230  // It returns the number of bytes written and any write error encountered.
   231  func Print(a ...interface{}) (n int, err error) {
   232  	return Fprint(os.Stdout, a...)
   233  }
   234  
   235  // Sprint formats using the default formats for its operands and returns the resulting string.
   236  // Spaces are added between operands when neither is a string.
   237  func Sprint(a ...interface{}) string {
   238  	p := newPrinter()
   239  	p.doPrint(a, false, false)
   240  	s := string(p.buf)
   241  	p.free()
   242  	return s
   243  }
   244  
   245  // These routines end in 'ln', do not take a format string,
   246  // always add spaces between operands, and add a newline
   247  // after the last operand.
   248  
   249  // Fprintln formats using the default formats for its operands and writes to w.
   250  // Spaces are always added between operands and a newline is appended.
   251  // It returns the number of bytes written and any write error encountered.
   252  func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
   253  	p := newPrinter()
   254  	p.doPrint(a, true, true)
   255  	n, err = w.Write(p.buf)
   256  	p.free()
   257  	return
   258  }
   259  
   260  // Println formats using the default formats for its operands and writes to standard output.
   261  // Spaces are always added between operands and a newline is appended.
   262  // It returns the number of bytes written and any write error encountered.
   263  func Println(a ...interface{}) (n int, err error) {
   264  	return Fprintln(os.Stdout, a...)
   265  }
   266  
   267  // Sprintln formats using the default formats for its operands and returns the resulting string.
   268  // Spaces are always added between operands and a newline is appended.
   269  func Sprintln(a ...interface{}) string {
   270  	p := newPrinter()
   271  	p.doPrint(a, true, true)
   272  	s := string(p.buf)
   273  	p.free()
   274  	return s
   275  }
   276  
   277  // getField gets the i'th field of the struct value.
   278  // If the field is itself is an interface, return a value for
   279  // the thing inside the interface, not the interface itself.
   280  func getField(v reflect.Value, i int) reflect.Value {
   281  	val := v.Field(i)
   282  	if val.Kind() == reflect.Interface && !val.IsNil() {
   283  		val = val.Elem()
   284  	}
   285  	return val
   286  }
   287  
   288  // tooLarge reports whether the magnitude of the integer is
   289  // too large to be used as a formatting width or precision.
   290  func tooLarge(x int) bool {
   291  	const max int = 1e6
   292  	return x > max || x < -max
   293  }
   294  
   295  // parsenum converts ASCII to integer.  num is 0 (and isnum is false) if no number present.
   296  func parsenum(s string, start, end int) (num int, isnum bool, newi int) {
   297  	if start >= end {
   298  		return 0, false, end
   299  	}
   300  	for newi = start; newi < end && '0' <= s[newi] && s[newi] <= '9'; newi++ {
   301  		if tooLarge(num) {
   302  			return 0, false, end // Overflow; crazy long number most likely.
   303  		}
   304  		num = num*10 + int(s[newi]-'0')
   305  		isnum = true
   306  	}
   307  	return
   308  }
   309  
   310  func (p *pp) unknownType(v reflect.Value) {
   311  	if !v.IsValid() {
   312  		p.buf.Write(nilAngleBytes)
   313  		return
   314  	}
   315  	p.buf.WriteByte('?')
   316  	p.buf.WriteString(v.Type().String())
   317  	p.buf.WriteByte('?')
   318  }
   319  
   320  func (p *pp) badVerb(verb rune) {
   321  	p.erroring = true
   322  	p.add('%')
   323  	p.add('!')
   324  	p.add(verb)
   325  	p.add('(')
   326  	switch {
   327  	case p.arg != nil:
   328  		p.buf.WriteString(reflect.TypeOf(p.arg).String())
   329  		p.add('=')
   330  		p.printArg(p.arg, 'v', 0)
   331  	case p.value.IsValid():
   332  		p.buf.WriteString(p.value.Type().String())
   333  		p.add('=')
   334  		p.printValue(p.value, 'v', 0)
   335  	default:
   336  		p.buf.Write(nilAngleBytes)
   337  	}
   338  	p.add(')')
   339  	p.erroring = false
   340  }
   341  
   342  func (p *pp) fmtBool(v bool, verb rune) {
   343  	switch verb {
   344  	case 't', 'v':
   345  		p.fmt.fmt_boolean(v)
   346  	default:
   347  		p.badVerb(verb)
   348  	}
   349  }
   350  
   351  // fmtC formats a rune for the 'c' format.
   352  func (p *pp) fmtC(c int64) {
   353  	r := rune(c) // Check for overflow.
   354  	if int64(r) != c {
   355  		r = utf8.RuneError
   356  	}
   357  	w := utf8.EncodeRune(p.runeBuf[0:utf8.UTFMax], r)
   358  	p.fmt.pad(p.runeBuf[0:w])
   359  }
   360  
   361  func (p *pp) fmtInt64(v int64, verb rune) {
   362  	switch verb {
   363  	case 'b':
   364  		p.fmt.integer(v, 2, signed, ldigits)
   365  	case 'c':
   366  		p.fmtC(v)
   367  	case 'd', 'v':
   368  		p.fmt.integer(v, 10, signed, ldigits)
   369  	case 'o':
   370  		p.fmt.integer(v, 8, signed, ldigits)
   371  	case 'q':
   372  		if 0 <= v && v <= utf8.MaxRune {
   373  			p.fmt.fmt_qc(v)
   374  		} else {
   375  			p.badVerb(verb)
   376  		}
   377  	case 'x':
   378  		p.fmt.integer(v, 16, signed, ldigits)
   379  	case 'U':
   380  		p.fmtUnicode(v)
   381  	case 'X':
   382  		p.fmt.integer(v, 16, signed, udigits)
   383  	default:
   384  		p.badVerb(verb)
   385  	}
   386  }
   387  
   388  // fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or
   389  // not, as requested, by temporarily setting the sharp flag.
   390  func (p *pp) fmt0x64(v uint64, leading0x bool) {
   391  	sharp := p.fmt.sharp
   392  	p.fmt.sharp = leading0x
   393  	p.fmt.integer(int64(v), 16, unsigned, ldigits)
   394  	p.fmt.sharp = sharp
   395  }
   396  
   397  // fmtUnicode formats a uint64 in U+1234 form by
   398  // temporarily turning on the unicode flag and tweaking the precision.
   399  func (p *pp) fmtUnicode(v int64) {
   400  	precPresent := p.fmt.precPresent
   401  	sharp := p.fmt.sharp
   402  	p.fmt.sharp = false
   403  	prec := p.fmt.prec
   404  	if !precPresent {
   405  		// If prec is already set, leave it alone; otherwise 4 is minimum.
   406  		p.fmt.prec = 4
   407  		p.fmt.precPresent = true
   408  	}
   409  	p.fmt.unicode = true // turn on U+
   410  	p.fmt.uniQuote = sharp
   411  	p.fmt.integer(int64(v), 16, unsigned, udigits)
   412  	p.fmt.unicode = false
   413  	p.fmt.uniQuote = false
   414  	p.fmt.prec = prec
   415  	p.fmt.precPresent = precPresent
   416  	p.fmt.sharp = sharp
   417  }
   418  
   419  func (p *pp) fmtUint64(v uint64, verb rune) {
   420  	switch verb {
   421  	case 'b':
   422  		p.fmt.integer(int64(v), 2, unsigned, ldigits)
   423  	case 'c':
   424  		p.fmtC(int64(v))
   425  	case 'd':
   426  		p.fmt.integer(int64(v), 10, unsigned, ldigits)
   427  	case 'v':
   428  		if p.fmt.sharpV {
   429  			p.fmt0x64(v, true)
   430  		} else {
   431  			p.fmt.integer(int64(v), 10, unsigned, ldigits)
   432  		}
   433  	case 'o':
   434  		p.fmt.integer(int64(v), 8, unsigned, ldigits)
   435  	case 'q':
   436  		if 0 <= v && v <= utf8.MaxRune {
   437  			p.fmt.fmt_qc(int64(v))
   438  		} else {
   439  			p.badVerb(verb)
   440  		}
   441  	case 'x':
   442  		p.fmt.integer(int64(v), 16, unsigned, ldigits)
   443  	case 'X':
   444  		p.fmt.integer(int64(v), 16, unsigned, udigits)
   445  	case 'U':
   446  		p.fmtUnicode(int64(v))
   447  	default:
   448  		p.badVerb(verb)
   449  	}
   450  }
   451  
   452  func (p *pp) fmtFloat32(v float32, verb rune) {
   453  	switch verb {
   454  	case 'b':
   455  		p.fmt.fmt_fb32(v)
   456  	case 'e':
   457  		p.fmt.fmt_e32(v)
   458  	case 'E':
   459  		p.fmt.fmt_E32(v)
   460  	case 'f', 'F':
   461  		p.fmt.fmt_f32(v)
   462  	case 'g', 'v':
   463  		p.fmt.fmt_g32(v)
   464  	case 'G':
   465  		p.fmt.fmt_G32(v)
   466  	default:
   467  		p.badVerb(verb)
   468  	}
   469  }
   470  
   471  func (p *pp) fmtFloat64(v float64, verb rune) {
   472  	switch verb {
   473  	case 'b':
   474  		p.fmt.fmt_fb64(v)
   475  	case 'e':
   476  		p.fmt.fmt_e64(v)
   477  	case 'E':
   478  		p.fmt.fmt_E64(v)
   479  	case 'f', 'F':
   480  		p.fmt.fmt_f64(v)
   481  	case 'g', 'v':
   482  		p.fmt.fmt_g64(v)
   483  	case 'G':
   484  		p.fmt.fmt_G64(v)
   485  	default:
   486  		p.badVerb(verb)
   487  	}
   488  }
   489  
   490  func (p *pp) fmtComplex64(v complex64, verb rune) {
   491  	switch verb {
   492  	case 'b', 'e', 'E', 'f', 'F', 'g', 'G':
   493  		p.fmt.fmt_c64(v, verb)
   494  	case 'v':
   495  		p.fmt.fmt_c64(v, 'g')
   496  	default:
   497  		p.badVerb(verb)
   498  	}
   499  }
   500  
   501  func (p *pp) fmtComplex128(v complex128, verb rune) {
   502  	switch verb {
   503  	case 'b', 'e', 'E', 'f', 'F', 'g', 'G':
   504  		p.fmt.fmt_c128(v, verb)
   505  	case 'v':
   506  		p.fmt.fmt_c128(v, 'g')
   507  	default:
   508  		p.badVerb(verb)
   509  	}
   510  }
   511  
   512  func (p *pp) fmtString(v string, verb rune) {
   513  	switch verb {
   514  	case 'v':
   515  		if p.fmt.sharpV {
   516  			p.fmt.fmt_q(v)
   517  		} else {
   518  			p.fmt.fmt_s(v)
   519  		}
   520  	case 's':
   521  		p.fmt.fmt_s(v)
   522  	case 'x':
   523  		p.fmt.fmt_sx(v, ldigits)
   524  	case 'X':
   525  		p.fmt.fmt_sx(v, udigits)
   526  	case 'q':
   527  		p.fmt.fmt_q(v)
   528  	default:
   529  		p.badVerb(verb)
   530  	}
   531  }
   532  
   533  func (p *pp) fmtBytes(v []byte, verb rune, typ reflect.Type, depth int) {
   534  	if verb == 'v' || verb == 'd' {
   535  		if p.fmt.sharpV {
   536  			if v == nil {
   537  				if typ == nil {
   538  					p.buf.WriteString("[]byte(nil)")
   539  				} else {
   540  					p.buf.WriteString(typ.String())
   541  					p.buf.Write(nilParenBytes)
   542  				}
   543  				return
   544  			}
   545  			if typ == nil {
   546  				p.buf.Write(bytesBytes)
   547  			} else {
   548  				p.buf.WriteString(typ.String())
   549  				p.buf.WriteByte('{')
   550  			}
   551  		} else {
   552  			p.buf.WriteByte('[')
   553  		}
   554  		for i, c := range v {
   555  			if i > 0 {
   556  				if p.fmt.sharpV {
   557  					p.buf.Write(commaSpaceBytes)
   558  				} else {
   559  					p.buf.WriteByte(' ')
   560  				}
   561  			}
   562  			p.printArg(c, 'v', depth+1)
   563  		}
   564  		if p.fmt.sharpV {
   565  			p.buf.WriteByte('}')
   566  		} else {
   567  			p.buf.WriteByte(']')
   568  		}
   569  		return
   570  	}
   571  	switch verb {
   572  	case 's':
   573  		p.fmt.fmt_s(string(v))
   574  	case 'x':
   575  		p.fmt.fmt_bx(v, ldigits)
   576  	case 'X':
   577  		p.fmt.fmt_bx(v, udigits)
   578  	case 'q':
   579  		p.fmt.fmt_q(string(v))
   580  	default:
   581  		p.badVerb(verb)
   582  	}
   583  }
   584  
   585  func (p *pp) fmtPointer(value reflect.Value, verb rune) {
   586  	use0x64 := true
   587  	switch verb {
   588  	case 'p', 'v':
   589  		// ok
   590  	case 'b', 'd', 'o', 'x', 'X':
   591  		use0x64 = false
   592  		// ok
   593  	default:
   594  		p.badVerb(verb)
   595  		return
   596  	}
   597  
   598  	var u uintptr
   599  	switch value.Kind() {
   600  	case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
   601  		u = value.Pointer()
   602  	default:
   603  		p.badVerb(verb)
   604  		return
   605  	}
   606  
   607  	if p.fmt.sharpV {
   608  		p.add('(')
   609  		p.buf.WriteString(value.Type().String())
   610  		p.add(')')
   611  		p.add('(')
   612  		if u == 0 {
   613  			p.buf.Write(nilBytes)
   614  		} else {
   615  			p.fmt0x64(uint64(u), true)
   616  		}
   617  		p.add(')')
   618  	} else if verb == 'v' && u == 0 {
   619  		p.buf.Write(nilAngleBytes)
   620  	} else {
   621  		if use0x64 {
   622  			p.fmt0x64(uint64(u), !p.fmt.sharp)
   623  		} else {
   624  			p.fmtUint64(uint64(u), verb)
   625  		}
   626  	}
   627  }
   628  
   629  var (
   630  	intBits     = reflect.TypeOf(0).Bits()
   631  	uintptrBits = reflect.TypeOf(uintptr(0)).Bits()
   632  )
   633  
   634  func (p *pp) catchPanic(arg interface{}, verb rune) {
   635  	if err := recover(); err != nil {
   636  		// If it's a nil pointer, just say "<nil>". The likeliest causes are a
   637  		// Stringer that fails to guard against nil or a nil pointer for a
   638  		// value receiver, and in either case, "<nil>" is a nice result.
   639  		if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() {
   640  			p.buf.Write(nilAngleBytes)
   641  			return
   642  		}
   643  		// Otherwise print a concise panic message. Most of the time the panic
   644  		// value will print itself nicely.
   645  		if p.panicking {
   646  			// Nested panics; the recursion in printArg cannot succeed.
   647  			panic(err)
   648  		}
   649  		p.fmt.clearflags() // We are done, and for this output we want default behavior.
   650  		p.buf.Write(percentBangBytes)
   651  		p.add(verb)
   652  		p.buf.Write(panicBytes)
   653  		p.panicking = true
   654  		p.printArg(err, 'v', 0)
   655  		p.panicking = false
   656  		p.buf.WriteByte(')')
   657  	}
   658  }
   659  
   660  // clearSpecialFlags pushes %#v back into the regular flags and returns their old state.
   661  func (p *pp) clearSpecialFlags() (plusV, sharpV bool) {
   662  	plusV = p.fmt.plusV
   663  	if plusV {
   664  		p.fmt.plus = true
   665  		p.fmt.plusV = false
   666  	}
   667  	sharpV = p.fmt.sharpV
   668  	if sharpV {
   669  		p.fmt.sharp = true
   670  		p.fmt.sharpV = false
   671  	}
   672  	return
   673  }
   674  
   675  // restoreSpecialFlags, whose argument should be a call to clearSpecialFlags,
   676  // restores the setting of the plusV and sharpV flags.
   677  func (p *pp) restoreSpecialFlags(plusV, sharpV bool) {
   678  	if plusV {
   679  		p.fmt.plus = false
   680  		p.fmt.plusV = true
   681  	}
   682  	if sharpV {
   683  		p.fmt.sharp = false
   684  		p.fmt.sharpV = true
   685  	}
   686  }
   687  
   688  func (p *pp) handleMethods(verb rune, depth int) (handled bool) {
   689  	if p.erroring {
   690  		return
   691  	}
   692  	// Is it a Formatter?
   693  	if formatter, ok := p.arg.(Formatter); ok {
   694  		handled = true
   695  		defer p.restoreSpecialFlags(p.clearSpecialFlags())
   696  		defer p.catchPanic(p.arg, verb)
   697  		formatter.Format(p, verb)
   698  		return
   699  	}
   700  
   701  	// If we're doing Go syntax and the argument knows how to supply it, take care of it now.
   702  	if p.fmt.sharpV {
   703  		if stringer, ok := p.arg.(GoStringer); ok {
   704  			handled = true
   705  			defer p.catchPanic(p.arg, verb)
   706  			// Print the result of GoString unadorned.
   707  			p.fmt.fmt_s(stringer.GoString())
   708  			return
   709  		}
   710  	} else {
   711  		// If a string is acceptable according to the format, see if
   712  		// the value satisfies one of the string-valued interfaces.
   713  		// Println etc. set verb to %v, which is "stringable".
   714  		switch verb {
   715  		case 'v', 's', 'x', 'X', 'q':
   716  			// Is it an error or Stringer?
   717  			// The duplication in the bodies is necessary:
   718  			// setting handled and deferring catchPanic
   719  			// must happen before calling the method.
   720  			switch v := p.arg.(type) {
   721  			case error:
   722  				handled = true
   723  				defer p.catchPanic(p.arg, verb)
   724  				p.printArg(v.Error(), verb, depth)
   725  				return
   726  
   727  			case Stringer:
   728  				handled = true
   729  				defer p.catchPanic(p.arg, verb)
   730  				p.printArg(v.String(), verb, depth)
   731  				return
   732  			}
   733  		}
   734  	}
   735  	return false
   736  }
   737  
   738  func (p *pp) printArg(arg interface{}, verb rune, depth int) (wasString bool) {
   739  	p.arg = arg
   740  	p.value = reflect.Value{}
   741  
   742  	if arg == nil {
   743  		if verb == 'T' || verb == 'v' {
   744  			p.fmt.pad(nilAngleBytes)
   745  		} else {
   746  			p.badVerb(verb)
   747  		}
   748  		return false
   749  	}
   750  
   751  	// Special processing considerations.
   752  	// %T (the value's type) and %p (its address) are special; we always do them first.
   753  	switch verb {
   754  	case 'T':
   755  		p.printArg(reflect.TypeOf(arg).String(), 's', 0)
   756  		return false
   757  	case 'p':
   758  		p.fmtPointer(reflect.ValueOf(arg), verb)
   759  		return false
   760  	}
   761  
   762  	// Some types can be done without reflection.
   763  	switch f := arg.(type) {
   764  	case bool:
   765  		p.fmtBool(f, verb)
   766  	case float32:
   767  		p.fmtFloat32(f, verb)
   768  	case float64:
   769  		p.fmtFloat64(f, verb)
   770  	case complex64:
   771  		p.fmtComplex64(f, verb)
   772  	case complex128:
   773  		p.fmtComplex128(f, verb)
   774  	case int:
   775  		p.fmtInt64(int64(f), verb)
   776  	case int8:
   777  		p.fmtInt64(int64(f), verb)
   778  	case int16:
   779  		p.fmtInt64(int64(f), verb)
   780  	case int32:
   781  		p.fmtInt64(int64(f), verb)
   782  	case int64:
   783  		p.fmtInt64(f, verb)
   784  	case uint:
   785  		p.fmtUint64(uint64(f), verb)
   786  	case uint8:
   787  		p.fmtUint64(uint64(f), verb)
   788  	case uint16:
   789  		p.fmtUint64(uint64(f), verb)
   790  	case uint32:
   791  		p.fmtUint64(uint64(f), verb)
   792  	case uint64:
   793  		p.fmtUint64(f, verb)
   794  	case uintptr:
   795  		p.fmtUint64(uint64(f), verb)
   796  	case string:
   797  		p.fmtString(f, verb)
   798  		wasString = verb == 's' || verb == 'v'
   799  	case []byte:
   800  		p.fmtBytes(f, verb, nil, depth)
   801  		wasString = verb == 's'
   802  	case reflect.Value:
   803  		return p.printReflectValue(f, verb, depth)
   804  	default:
   805  		// If the type is not simple, it might have methods.
   806  		if handled := p.handleMethods(verb, depth); handled {
   807  			return false
   808  		}
   809  		// Need to use reflection
   810  		return p.printReflectValue(reflect.ValueOf(arg), verb, depth)
   811  	}
   812  	p.arg = nil
   813  	return
   814  }
   815  
   816  // printValue is like printArg but starts with a reflect value, not an interface{} value.
   817  func (p *pp) printValue(value reflect.Value, verb rune, depth int) (wasString bool) {
   818  	if !value.IsValid() {
   819  		if verb == 'T' || verb == 'v' {
   820  			p.buf.Write(nilAngleBytes)
   821  		} else {
   822  			p.badVerb(verb)
   823  		}
   824  		return false
   825  	}
   826  
   827  	// Special processing considerations.
   828  	// %T (the value's type) and %p (its address) are special; we always do them first.
   829  	switch verb {
   830  	case 'T':
   831  		p.printArg(value.Type().String(), 's', 0)
   832  		return false
   833  	case 'p':
   834  		p.fmtPointer(value, verb)
   835  		return false
   836  	}
   837  
   838  	// Handle values with special methods.
   839  	// Call always, even when arg == nil, because handleMethods clears p.fmt.plus for us.
   840  	p.arg = nil // Make sure it's cleared, for safety.
   841  	if value.CanInterface() {
   842  		p.arg = value.Interface()
   843  	}
   844  	if handled := p.handleMethods(verb, depth); handled {
   845  		return false
   846  	}
   847  
   848  	return p.printReflectValue(value, verb, depth)
   849  }
   850  
   851  var byteType = reflect.TypeOf(byte(0))
   852  
   853  // printReflectValue is the fallback for both printArg and printValue.
   854  // It uses reflect to print the value.
   855  func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) (wasString bool) {
   856  	oldValue := p.value
   857  	p.value = value
   858  BigSwitch:
   859  	switch f := value; f.Kind() {
   860  	case reflect.Invalid:
   861  		p.buf.WriteString("<invalid reflect.Value>")
   862  	case reflect.Bool:
   863  		p.fmtBool(f.Bool(), verb)
   864  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   865  		p.fmtInt64(f.Int(), verb)
   866  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   867  		p.fmtUint64(f.Uint(), verb)
   868  	case reflect.Float32, reflect.Float64:
   869  		if f.Type().Size() == 4 {
   870  			p.fmtFloat32(float32(f.Float()), verb)
   871  		} else {
   872  			p.fmtFloat64(f.Float(), verb)
   873  		}
   874  	case reflect.Complex64, reflect.Complex128:
   875  		if f.Type().Size() == 8 {
   876  			p.fmtComplex64(complex64(f.Complex()), verb)
   877  		} else {
   878  			p.fmtComplex128(f.Complex(), verb)
   879  		}
   880  	case reflect.String:
   881  		p.fmtString(f.String(), verb)
   882  	case reflect.Map:
   883  		if p.fmt.sharpV {
   884  			p.buf.WriteString(f.Type().String())
   885  			if f.IsNil() {
   886  				p.buf.WriteString("(nil)")
   887  				break
   888  			}
   889  			p.buf.WriteByte('{')
   890  		} else {
   891  			p.buf.Write(mapBytes)
   892  		}
   893  		keys := f.MapKeys()
   894  		for i, key := range keys {
   895  			if i > 0 {
   896  				if p.fmt.sharpV {
   897  					p.buf.Write(commaSpaceBytes)
   898  				} else {
   899  					p.buf.WriteByte(' ')
   900  				}
   901  			}
   902  			p.printValue(key, verb, depth+1)
   903  			p.buf.WriteByte(':')
   904  			p.printValue(f.MapIndex(key), verb, depth+1)
   905  		}
   906  		if p.fmt.sharpV {
   907  			p.buf.WriteByte('}')
   908  		} else {
   909  			p.buf.WriteByte(']')
   910  		}
   911  	case reflect.Struct:
   912  		if p.fmt.sharpV {
   913  			p.buf.WriteString(value.Type().String())
   914  		}
   915  		p.add('{')
   916  		v := f
   917  		t := v.Type()
   918  		for i := 0; i < v.NumField(); i++ {
   919  			if i > 0 {
   920  				if p.fmt.sharpV {
   921  					p.buf.Write(commaSpaceBytes)
   922  				} else {
   923  					p.buf.WriteByte(' ')
   924  				}
   925  			}
   926  			if p.fmt.plusV || p.fmt.sharpV {
   927  				if f := t.Field(i); f.Name != "" {
   928  					p.buf.WriteString(f.Name)
   929  					p.buf.WriteByte(':')
   930  				}
   931  			}
   932  			p.printValue(getField(v, i), verb, depth+1)
   933  		}
   934  		p.buf.WriteByte('}')
   935  	case reflect.Interface:
   936  		value := f.Elem()
   937  		if !value.IsValid() {
   938  			if p.fmt.sharpV {
   939  				p.buf.WriteString(f.Type().String())
   940  				p.buf.Write(nilParenBytes)
   941  			} else {
   942  				p.buf.Write(nilAngleBytes)
   943  			}
   944  		} else {
   945  			wasString = p.printValue(value, verb, depth+1)
   946  		}
   947  	case reflect.Array, reflect.Slice:
   948  		// Byte slices are special:
   949  		// - Handle []byte (== []uint8) with fmtBytes.
   950  		// - Handle []T, where T is a named byte type, with fmtBytes only
   951  		//   for the s, q, an x verbs. For other verbs, T might be a
   952  		//   Stringer, so we use printValue to print each element.
   953  		if typ := f.Type(); typ.Elem().Kind() == reflect.Uint8 && (typ.Elem() == byteType || verb == 's' || verb == 'q' || verb == 'x') {
   954  			var bytes []byte
   955  			if f.Kind() == reflect.Slice {
   956  				bytes = f.Bytes()
   957  			} else if f.CanAddr() {
   958  				bytes = f.Slice(0, f.Len()).Bytes()
   959  			} else {
   960  				// We have an array, but we cannot Slice() a non-addressable array,
   961  				// so we build a slice by hand. This is a rare case but it would be nice
   962  				// if reflection could help a little more.
   963  				bytes = make([]byte, f.Len())
   964  				for i := range bytes {
   965  					bytes[i] = byte(f.Index(i).Uint())
   966  				}
   967  			}
   968  			p.fmtBytes(bytes, verb, typ, depth)
   969  			wasString = verb == 's'
   970  			break
   971  		}
   972  		if p.fmt.sharpV {
   973  			p.buf.WriteString(value.Type().String())
   974  			if f.Kind() == reflect.Slice && f.IsNil() {
   975  				p.buf.WriteString("(nil)")
   976  				break
   977  			}
   978  			p.buf.WriteByte('{')
   979  		} else {
   980  			p.buf.WriteByte('[')
   981  		}
   982  		for i := 0; i < f.Len(); i++ {
   983  			if i > 0 {
   984  				if p.fmt.sharpV {
   985  					p.buf.Write(commaSpaceBytes)
   986  				} else {
   987  					p.buf.WriteByte(' ')
   988  				}
   989  			}
   990  			p.printValue(f.Index(i), verb, depth+1)
   991  		}
   992  		if p.fmt.sharpV {
   993  			p.buf.WriteByte('}')
   994  		} else {
   995  			p.buf.WriteByte(']')
   996  		}
   997  	case reflect.Ptr:
   998  		v := f.Pointer()
   999  		// pointer to array or slice or struct?  ok at top level
  1000  		// but not embedded (avoid loops)
  1001  		if v != 0 && depth == 0 {
  1002  			switch a := f.Elem(); a.Kind() {
  1003  			case reflect.Array, reflect.Slice:
  1004  				p.buf.WriteByte('&')
  1005  				p.printValue(a, verb, depth+1)
  1006  				break BigSwitch
  1007  			case reflect.Struct:
  1008  				p.buf.WriteByte('&')
  1009  				p.printValue(a, verb, depth+1)
  1010  				break BigSwitch
  1011  			case reflect.Map:
  1012  				p.buf.WriteByte('&')
  1013  				p.printValue(a, verb, depth+1)
  1014  				break BigSwitch
  1015  			}
  1016  		}
  1017  		fallthrough
  1018  	case reflect.Chan, reflect.Func, reflect.UnsafePointer:
  1019  		p.fmtPointer(value, verb)
  1020  	default:
  1021  		p.unknownType(f)
  1022  	}
  1023  	p.value = oldValue
  1024  	return wasString
  1025  }
  1026  
  1027  // intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type.
  1028  func intFromArg(a []interface{}, argNum int) (num int, isInt bool, newArgNum int) {
  1029  	newArgNum = argNum
  1030  	if argNum < len(a) {
  1031  		num, isInt = a[argNum].(int) // Almost always OK.
  1032  		if !isInt {
  1033  			// Work harder.
  1034  			switch v := reflect.ValueOf(a[argNum]); v.Kind() {
  1035  			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1036  				n := v.Int()
  1037  				if int64(int(n)) == n {
  1038  					num = int(n)
  1039  					isInt = true
  1040  				}
  1041  			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1042  				n := v.Uint()
  1043  				if int64(n) >= 0 && uint64(int(n)) == n {
  1044  					num = int(n)
  1045  					isInt = true
  1046  				}
  1047  			default:
  1048  				// Already 0, false.
  1049  			}
  1050  		}
  1051  		newArgNum = argNum + 1
  1052  		if tooLarge(num) {
  1053  			num = 0
  1054  			isInt = false
  1055  		}
  1056  	}
  1057  	return
  1058  }
  1059  
  1060  // parseArgNumber returns the value of the bracketed number, minus 1
  1061  // (explicit argument numbers are one-indexed but we want zero-indexed).
  1062  // The opening bracket is known to be present at format[0].
  1063  // The returned values are the index, the number of bytes to consume
  1064  // up to the closing paren, if present, and whether the number parsed
  1065  // ok. The bytes to consume will be 1 if no closing paren is present.
  1066  func parseArgNumber(format string) (index int, wid int, ok bool) {
  1067  	// There must be at least 3 bytes: [n].
  1068  	if len(format) < 3 {
  1069  		return 0, 1, false
  1070  	}
  1071  
  1072  	// Find closing bracket.
  1073  	for i := 1; i < len(format); i++ {
  1074  		if format[i] == ']' {
  1075  			width, ok, newi := parsenum(format, 1, i)
  1076  			if !ok || newi != i {
  1077  				return 0, i + 1, false
  1078  			}
  1079  			return width - 1, i + 1, true // arg numbers are one-indexed and skip paren.
  1080  		}
  1081  	}
  1082  	return 0, 1, false
  1083  }
  1084  
  1085  // argNumber returns the next argument to evaluate, which is either the value of the passed-in
  1086  // argNum or the value of the bracketed integer that begins format[i:]. It also returns
  1087  // the new value of i, that is, the index of the next byte of the format to process.
  1088  func (p *pp) argNumber(argNum int, format string, i int, numArgs int) (newArgNum, newi int, found bool) {
  1089  	if len(format) <= i || format[i] != '[' {
  1090  		return argNum, i, false
  1091  	}
  1092  	p.reordered = true
  1093  	index, wid, ok := parseArgNumber(format[i:])
  1094  	if ok && 0 <= index && index < numArgs {
  1095  		return index, i + wid, true
  1096  	}
  1097  	p.goodArgNum = false
  1098  	return argNum, i + wid, ok
  1099  }
  1100  
  1101  func (p *pp) doPrintf(format string, a []interface{}) {
  1102  	end := len(format)
  1103  	argNum := 0         // we process one argument per non-trivial format
  1104  	afterIndex := false // previous item in format was an index like [3].
  1105  	p.reordered = false
  1106  	for i := 0; i < end; {
  1107  		p.goodArgNum = true
  1108  		lasti := i
  1109  		for i < end && format[i] != '%' {
  1110  			i++
  1111  		}
  1112  		if i > lasti {
  1113  			p.buf.WriteString(format[lasti:i])
  1114  		}
  1115  		if i >= end {
  1116  			// done processing format string
  1117  			break
  1118  		}
  1119  
  1120  		// Process one verb
  1121  		i++
  1122  
  1123  		// Do we have flags?
  1124  		p.fmt.clearflags()
  1125  	F:
  1126  		for ; i < end; i++ {
  1127  			switch format[i] {
  1128  			case '#':
  1129  				p.fmt.sharp = true
  1130  			case '0':
  1131  				p.fmt.zero = true
  1132  			case '+':
  1133  				p.fmt.plus = true
  1134  			case '-':
  1135  				p.fmt.minus = true
  1136  			case ' ':
  1137  				p.fmt.space = true
  1138  			default:
  1139  				break F
  1140  			}
  1141  		}
  1142  
  1143  		// Do we have an explicit argument index?
  1144  		argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
  1145  
  1146  		// Do we have width?
  1147  		if i < end && format[i] == '*' {
  1148  			i++
  1149  			p.fmt.wid, p.fmt.widPresent, argNum = intFromArg(a, argNum)
  1150  
  1151  			if !p.fmt.widPresent {
  1152  				p.buf.Write(badWidthBytes)
  1153  			}
  1154  
  1155  			// We have a negative width, so take its value and ensure
  1156  			// that the minus flag is set
  1157  			if p.fmt.wid < 0 {
  1158  				p.fmt.wid = -p.fmt.wid
  1159  				p.fmt.minus = true
  1160  			}
  1161  			afterIndex = false
  1162  		} else {
  1163  			p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end)
  1164  			if afterIndex && p.fmt.widPresent { // "%[3]2d"
  1165  				p.goodArgNum = false
  1166  			}
  1167  		}
  1168  
  1169  		// Do we have precision?
  1170  		if i+1 < end && format[i] == '.' {
  1171  			i++
  1172  			if afterIndex { // "%[3].2d"
  1173  				p.goodArgNum = false
  1174  			}
  1175  			argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
  1176  			if i < end && format[i] == '*' {
  1177  				i++
  1178  				p.fmt.prec, p.fmt.precPresent, argNum = intFromArg(a, argNum)
  1179  				// Negative precision arguments don't make sense
  1180  				if p.fmt.prec < 0 {
  1181  					p.fmt.prec = 0
  1182  					p.fmt.precPresent = false
  1183  				}
  1184  				if !p.fmt.precPresent {
  1185  					p.buf.Write(badPrecBytes)
  1186  				}
  1187  				afterIndex = false
  1188  			} else {
  1189  				p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i, end)
  1190  				if !p.fmt.precPresent {
  1191  					p.fmt.prec = 0
  1192  					p.fmt.precPresent = true
  1193  				}
  1194  			}
  1195  		}
  1196  
  1197  		if !afterIndex {
  1198  			argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
  1199  		}
  1200  
  1201  		if i >= end {
  1202  			p.buf.Write(noVerbBytes)
  1203  			continue
  1204  		}
  1205  		c, w := utf8.DecodeRuneInString(format[i:])
  1206  		i += w
  1207  		// percent is special - absorbs no operand
  1208  		if c == '%' {
  1209  			p.buf.WriteByte('%') // We ignore width and prec.
  1210  			continue
  1211  		}
  1212  		if !p.goodArgNum {
  1213  			p.buf.Write(percentBangBytes)
  1214  			p.add(c)
  1215  			p.buf.Write(badIndexBytes)
  1216  			continue
  1217  		} else if argNum >= len(a) { // out of operands
  1218  			p.buf.Write(percentBangBytes)
  1219  			p.add(c)
  1220  			p.buf.Write(missingBytes)
  1221  			continue
  1222  		}
  1223  		arg := a[argNum]
  1224  		argNum++
  1225  
  1226  		if c == 'v' {
  1227  			if p.fmt.sharp {
  1228  				// Go syntax. Set the flag in the fmt and clear the sharp flag.
  1229  				p.fmt.sharp = false
  1230  				p.fmt.sharpV = true
  1231  			}
  1232  			if p.fmt.plus {
  1233  				// Struct-field syntax. Set the flag in the fmt and clear the plus flag.
  1234  				p.fmt.plus = false
  1235  				p.fmt.plusV = true
  1236  			}
  1237  		}
  1238  		p.printArg(arg, c, 0)
  1239  	}
  1240  
  1241  	// Check for extra arguments unless the call accessed the arguments
  1242  	// out of order, in which case it's too expensive to detect if they've all
  1243  	// been used and arguably OK if they're not.
  1244  	if !p.reordered && argNum < len(a) {
  1245  		p.buf.Write(extraBytes)
  1246  		for ; argNum < len(a); argNum++ {
  1247  			arg := a[argNum]
  1248  			if arg != nil {
  1249  				p.buf.WriteString(reflect.TypeOf(arg).String())
  1250  				p.buf.WriteByte('=')
  1251  			}
  1252  			p.printArg(arg, 'v', 0)
  1253  			if argNum+1 < len(a) {
  1254  				p.buf.Write(commaSpaceBytes)
  1255  			}
  1256  		}
  1257  		p.buf.WriteByte(')')
  1258  	}
  1259  }
  1260  
  1261  func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
  1262  	prevString := false
  1263  	for argNum := 0; argNum < len(a); argNum++ {
  1264  		p.fmt.clearflags()
  1265  		// always add spaces if we're doing Println
  1266  		arg := a[argNum]
  1267  		if argNum > 0 {
  1268  			isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
  1269  			if addspace || !isString && !prevString {
  1270  				p.buf.WriteByte(' ')
  1271  			}
  1272  		}
  1273  		prevString = p.printArg(arg, 'v', 0)
  1274  	}
  1275  	if addnewline {
  1276  		p.buf.WriteByte('\n')
  1277  	}
  1278  }