Home | History | Annotate | Download | only in fmt
      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 type int.
   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)
   1032 		newArgNum = argNum + 1
   1033 		if tooLarge(num) {
   1034 			num = 0
   1035 			isInt = false
   1036 		}
   1037 	}
   1038 	return
   1039 }
   1040 
   1041 // parseArgNumber returns the value of the bracketed number, minus 1
   1042 // (explicit argument numbers are one-indexed but we want zero-indexed).
   1043 // The opening bracket is known to be present at format[0].
   1044 // The returned values are the index, the number of bytes to consume
   1045 // up to the closing paren, if present, and whether the number parsed
   1046 // ok. The bytes to consume will be 1 if no closing paren is present.
   1047 func parseArgNumber(format string) (index int, wid int, ok bool) {
   1048 	// There must be at least 3 bytes: [n].
   1049 	if len(format) < 3 {
   1050 		return 0, 1, false
   1051 	}
   1052 
   1053 	// Find closing bracket.
   1054 	for i := 1; i < len(format); i++ {
   1055 		if format[i] == ']' {
   1056 			width, ok, newi := parsenum(format, 1, i)
   1057 			if !ok || newi != i {
   1058 				return 0, i + 1, false
   1059 			}
   1060 			return width - 1, i + 1, true // arg numbers are one-indexed and skip paren.
   1061 		}
   1062 	}
   1063 	return 0, 1, false
   1064 }
   1065 
   1066 // argNumber returns the next argument to evaluate, which is either the value of the passed-in
   1067 // argNum or the value of the bracketed integer that begins format[i:]. It also returns
   1068 // the new value of i, that is, the index of the next byte of the format to process.
   1069 func (p *pp) argNumber(argNum int, format string, i int, numArgs int) (newArgNum, newi int, found bool) {
   1070 	if len(format) <= i || format[i] != '[' {
   1071 		return argNum, i, false
   1072 	}
   1073 	p.reordered = true
   1074 	index, wid, ok := parseArgNumber(format[i:])
   1075 	if ok && 0 <= index && index < numArgs {
   1076 		return index, i + wid, true
   1077 	}
   1078 	p.goodArgNum = false
   1079 	return argNum, i + wid, ok
   1080 }
   1081 
   1082 func (p *pp) doPrintf(format string, a []interface{}) {
   1083 	end := len(format)
   1084 	argNum := 0         // we process one argument per non-trivial format
   1085 	afterIndex := false // previous item in format was an index like [3].
   1086 	p.reordered = false
   1087 	for i := 0; i < end; {
   1088 		p.goodArgNum = true
   1089 		lasti := i
   1090 		for i < end && format[i] != '%' {
   1091 			i++
   1092 		}
   1093 		if i > lasti {
   1094 			p.buf.WriteString(format[lasti:i])
   1095 		}
   1096 		if i >= end {
   1097 			// done processing format string
   1098 			break
   1099 		}
   1100 
   1101 		// Process one verb
   1102 		i++
   1103 
   1104 		// Do we have flags?
   1105 		p.fmt.clearflags()
   1106 	F:
   1107 		for ; i < end; i++ {
   1108 			switch format[i] {
   1109 			case '#':
   1110 				p.fmt.sharp = true
   1111 			case '0':
   1112 				p.fmt.zero = true
   1113 			case '+':
   1114 				p.fmt.plus = true
   1115 			case '-':
   1116 				p.fmt.minus = true
   1117 			case ' ':
   1118 				p.fmt.space = true
   1119 			default:
   1120 				break F
   1121 			}
   1122 		}
   1123 
   1124 		// Do we have an explicit argument index?
   1125 		argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
   1126 
   1127 		// Do we have width?
   1128 		if i < end && format[i] == '*' {
   1129 			i++
   1130 			p.fmt.wid, p.fmt.widPresent, argNum = intFromArg(a, argNum)
   1131 
   1132 			if !p.fmt.widPresent {
   1133 				p.buf.Write(badWidthBytes)
   1134 			}
   1135 
   1136 			// We have a negative width, so take its value and ensure
   1137 			// that the minus flag is set
   1138 			if p.fmt.wid < 0 {
   1139 				p.fmt.wid = -p.fmt.wid
   1140 				p.fmt.minus = true
   1141 			}
   1142 			afterIndex = false
   1143 		} else {
   1144 			p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end)
   1145 			if afterIndex && p.fmt.widPresent { // "%[3]2d"
   1146 				p.goodArgNum = false
   1147 			}
   1148 		}
   1149 
   1150 		// Do we have precision?
   1151 		if i+1 < end && format[i] == '.' {
   1152 			i++
   1153 			if afterIndex { // "%[3].2d"
   1154 				p.goodArgNum = false
   1155 			}
   1156 			argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
   1157 			if i < end && format[i] == '*' {
   1158 				i++
   1159 				p.fmt.prec, p.fmt.precPresent, argNum = intFromArg(a, argNum)
   1160 				// Negative precision arguments don't make sense
   1161 				if p.fmt.prec < 0 {
   1162 					p.fmt.prec = 0
   1163 					p.fmt.precPresent = false
   1164 				}
   1165 				if !p.fmt.precPresent {
   1166 					p.buf.Write(badPrecBytes)
   1167 				}
   1168 				afterIndex = false
   1169 			} else {
   1170 				p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i, end)
   1171 				if !p.fmt.precPresent {
   1172 					p.fmt.prec = 0
   1173 					p.fmt.precPresent = true
   1174 				}
   1175 			}
   1176 		}
   1177 
   1178 		if !afterIndex {
   1179 			argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
   1180 		}
   1181 
   1182 		if i >= end {
   1183 			p.buf.Write(noVerbBytes)
   1184 			continue
   1185 		}
   1186 		c, w := utf8.DecodeRuneInString(format[i:])
   1187 		i += w
   1188 		// percent is special - absorbs no operand
   1189 		if c == '%' {
   1190 			p.buf.WriteByte('%') // We ignore width and prec.
   1191 			continue
   1192 		}
   1193 		if !p.goodArgNum {
   1194 			p.buf.Write(percentBangBytes)
   1195 			p.add(c)
   1196 			p.buf.Write(badIndexBytes)
   1197 			continue
   1198 		} else if argNum >= len(a) { // out of operands
   1199 			p.buf.Write(percentBangBytes)
   1200 			p.add(c)
   1201 			p.buf.Write(missingBytes)
   1202 			continue
   1203 		}
   1204 		arg := a[argNum]
   1205 		argNum++
   1206 
   1207 		if c == 'v' {
   1208 			if p.fmt.sharp {
   1209 				// Go syntax. Set the flag in the fmt and clear the sharp flag.
   1210 				p.fmt.sharp = false
   1211 				p.fmt.sharpV = true
   1212 			}
   1213 			if p.fmt.plus {
   1214 				// Struct-field syntax. Set the flag in the fmt and clear the plus flag.
   1215 				p.fmt.plus = false
   1216 				p.fmt.plusV = true
   1217 			}
   1218 		}
   1219 		p.printArg(arg, c, 0)
   1220 	}
   1221 
   1222 	// Check for extra arguments unless the call accessed the arguments
   1223 	// out of order, in which case it's too expensive to detect if they've all
   1224 	// been used and arguably OK if they're not.
   1225 	if !p.reordered && argNum < len(a) {
   1226 		p.buf.Write(extraBytes)
   1227 		for ; argNum < len(a); argNum++ {
   1228 			arg := a[argNum]
   1229 			if arg != nil {
   1230 				p.buf.WriteString(reflect.TypeOf(arg).String())
   1231 				p.buf.WriteByte('=')
   1232 			}
   1233 			p.printArg(arg, 'v', 0)
   1234 			if argNum+1 < len(a) {
   1235 				p.buf.Write(commaSpaceBytes)
   1236 			}
   1237 		}
   1238 		p.buf.WriteByte(')')
   1239 	}
   1240 }
   1241 
   1242 func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
   1243 	prevString := false
   1244 	for argNum := 0; argNum < len(a); argNum++ {
   1245 		p.fmt.clearflags()
   1246 		// always add spaces if we're doing Println
   1247 		arg := a[argNum]
   1248 		if argNum > 0 {
   1249 			isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
   1250 			if addspace || !isString && !prevString {
   1251 				p.buf.WriteByte(' ')
   1252 			}
   1253 		}
   1254 		prevString = p.printArg(arg, 'v', 0)
   1255 	}
   1256 	if addnewline {
   1257 		p.buf.WriteByte('\n')
   1258 	}
   1259 }
   1260