github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/fix/testdata/reflect.print.go.out (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 "bytes" 9 "io" 10 "os" 11 "reflect" 12 "utf8" 13 ) 14 15 // Some constants in the form of bytes, to avoid string overhead. 16 // Needlessly fastidious, I suppose. 17 var ( 18 commaSpaceBytes = []byte(", ") 19 nilAngleBytes = []byte("<nil>") 20 nilParenBytes = []byte("(nil)") 21 nilBytes = []byte("nil") 22 mapBytes = []byte("map[") 23 missingBytes = []byte("(MISSING)") 24 extraBytes = []byte("%!(EXTRA ") 25 irparenBytes = []byte("i)") 26 bytesBytes = []byte("[]byte{") 27 widthBytes = []byte("%!(BADWIDTH)") 28 precBytes = []byte("%!(BADPREC)") 29 noVerbBytes = []byte("%!(NOVERB)") 30 ) 31 32 // State represents the printer state passed to custom formatters. 33 // It provides access to the io.Writer interface plus information about 34 // the flags and options for the operand's format specifier. 35 type State interface { 36 // Write is the function to call to emit formatted output to be printed. 37 Write(b []byte) (ret int, err os.Error) 38 // Width returns the value of the width option and whether it has been set. 39 Width() (wid int, ok bool) 40 // Precision returns the value of the precision option and whether it has been set. 41 Precision() (prec int, ok bool) 42 43 // Flag returns whether the flag c, a character, has been set. 44 Flag(int) bool 45 } 46 47 // Formatter is the interface implemented by values with a custom formatter. 48 // The implementation of Format may call Sprintf or Fprintf(f) etc. 49 // to generate its output. 50 type Formatter interface { 51 Format(f State, c int) 52 } 53 54 // Stringer is implemented by any value that has a String method(), 55 // which defines the ``native'' format for that value. 56 // The String method is used to print values passed as an operand 57 // to a %s or %v format or to an unformatted printer such as Print. 58 type Stringer interface { 59 String() string 60 } 61 62 // GoStringer is implemented by any value that has a GoString() method, 63 // which defines the Go syntax for that value. 64 // The GoString method is used to print values passed as an operand 65 // to a %#v format. 66 type GoStringer interface { 67 GoString() string 68 } 69 70 type pp struct { 71 n int 72 buf bytes.Buffer 73 runeBuf [utf8.UTFMax]byte 74 fmt fmt 75 } 76 77 // A cache holds a set of reusable objects. 78 // The buffered channel holds the currently available objects. 79 // If more are needed, the cache creates them by calling new. 80 type cache struct { 81 saved chan interface{} 82 new func() interface{} 83 } 84 85 func (c *cache) put(x interface{}) { 86 select { 87 case c.saved <- x: 88 // saved in cache 89 default: 90 // discard 91 } 92 } 93 94 func (c *cache) get() interface{} { 95 select { 96 case x := <-c.saved: 97 return x // reused from cache 98 default: 99 return c.new() 100 } 101 panic("not reached") 102 } 103 104 func newCache(f func() interface{}) *cache { 105 return &cache{make(chan interface{}, 100), f} 106 } 107 108 var ppFree = newCache(func() interface{} { return new(pp) }) 109 110 // Allocate a new pp struct or grab a cached one. 111 func newPrinter() *pp { 112 p := ppFree.get().(*pp) 113 p.fmt.init(&p.buf) 114 return p 115 } 116 117 // Save used pp structs in ppFree; avoids an allocation per invocation. 118 func (p *pp) free() { 119 // Don't hold on to pp structs with large buffers. 120 if cap(p.buf.Bytes()) > 1024 { 121 return 122 } 123 p.buf.Reset() 124 ppFree.put(p) 125 } 126 127 func (p *pp) Width() (wid int, ok bool) { return p.fmt.wid, p.fmt.widPresent } 128 129 func (p *pp) Precision() (prec int, ok bool) { return p.fmt.prec, p.fmt.precPresent } 130 131 func (p *pp) Flag(b int) bool { 132 switch b { 133 case '-': 134 return p.fmt.minus 135 case '+': 136 return p.fmt.plus 137 case '#': 138 return p.fmt.sharp 139 case ' ': 140 return p.fmt.space 141 case '0': 142 return p.fmt.zero 143 } 144 return false 145 } 146 147 func (p *pp) add(c int) { 148 p.buf.WriteRune(c) 149 } 150 151 // Implement Write so we can call Fprintf on a pp (through State), for 152 // recursive use in custom verbs. 153 func (p *pp) Write(b []byte) (ret int, err os.Error) { 154 return p.buf.Write(b) 155 } 156 157 // These routines end in 'f' and take a format string. 158 159 // Fprintf formats according to a format specifier and writes to w. 160 // It returns the number of bytes written and any write error encountered. 161 func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error os.Error) { 162 p := newPrinter() 163 p.doPrintf(format, a) 164 n64, error := p.buf.WriteTo(w) 165 p.free() 166 return int(n64), error 167 } 168 169 // Printf formats according to a format specifier and writes to standard output. 170 // It returns the number of bytes written and any write error encountered. 171 func Printf(format string, a ...interface{}) (n int, errno os.Error) { 172 n, errno = Fprintf(os.Stdout, format, a...) 173 return n, errno 174 } 175 176 // Sprintf formats according to a format specifier and returns the resulting string. 177 func Sprintf(format string, a ...interface{}) string { 178 p := newPrinter() 179 p.doPrintf(format, a) 180 s := p.buf.String() 181 p.free() 182 return s 183 } 184 185 // Errorf formats according to a format specifier and returns the string 186 // converted to an os.ErrorString, which satisfies the os.Error interface. 187 func Errorf(format string, a ...interface{}) os.Error { 188 return os.NewError(Sprintf(format, a...)) 189 } 190 191 // These routines do not take a format string 192 193 // Fprint formats using the default formats for its operands and writes to w. 194 // Spaces are added between operands when neither is a string. 195 // It returns the number of bytes written and any write error encountered. 196 func Fprint(w io.Writer, a ...interface{}) (n int, error os.Error) { 197 p := newPrinter() 198 p.doPrint(a, false, false) 199 n64, error := p.buf.WriteTo(w) 200 p.free() 201 return int(n64), error 202 } 203 204 // Print formats using the default formats for its operands and writes to standard output. 205 // Spaces are added between operands when neither is a string. 206 // It returns the number of bytes written and any write error encountered. 207 func Print(a ...interface{}) (n int, errno os.Error) { 208 n, errno = Fprint(os.Stdout, a...) 209 return n, errno 210 } 211 212 // Sprint formats using the default formats for its operands and returns the resulting string. 213 // Spaces are added between operands when neither is a string. 214 func Sprint(a ...interface{}) string { 215 p := newPrinter() 216 p.doPrint(a, false, false) 217 s := p.buf.String() 218 p.free() 219 return s 220 } 221 222 // These routines end in 'ln', do not take a format string, 223 // always add spaces between operands, and add a newline 224 // after the last operand. 225 226 // Fprintln formats using the default formats for its operands and writes to w. 227 // Spaces are always added between operands and a newline is appended. 228 // It returns the number of bytes written and any write error encountered. 229 func Fprintln(w io.Writer, a ...interface{}) (n int, error os.Error) { 230 p := newPrinter() 231 p.doPrint(a, true, true) 232 n64, error := p.buf.WriteTo(w) 233 p.free() 234 return int(n64), error 235 } 236 237 // Println formats using the default formats for its operands and writes to standard output. 238 // Spaces are always added between operands and a newline is appended. 239 // It returns the number of bytes written and any write error encountered. 240 func Println(a ...interface{}) (n int, errno os.Error) { 241 n, errno = Fprintln(os.Stdout, a...) 242 return n, errno 243 } 244 245 // Sprintln formats using the default formats for its operands and returns the resulting string. 246 // Spaces are always added between operands and a newline is appended. 247 func Sprintln(a ...interface{}) string { 248 p := newPrinter() 249 p.doPrint(a, true, true) 250 s := p.buf.String() 251 p.free() 252 return s 253 } 254 255 // Get the i'th arg of the struct value. 256 // If the arg itself is an interface, return a value for 257 // the thing inside the interface, not the interface itself. 258 func getField(v reflect.Value, i int) reflect.Value { 259 val := v.Field(i) 260 if i := val; i.Kind() == reflect.Interface { 261 if inter := i.Interface(); inter != nil { 262 return reflect.ValueOf(inter) 263 } 264 } 265 return val 266 } 267 268 // Convert ASCII to integer. n is 0 (and got is false) if no number present. 269 func parsenum(s string, start, end int) (num int, isnum bool, newi int) { 270 if start >= end { 271 return 0, false, end 272 } 273 for newi = start; newi < end && '0' <= s[newi] && s[newi] <= '9'; newi++ { 274 num = num*10 + int(s[newi]-'0') 275 isnum = true 276 } 277 return 278 } 279 280 func (p *pp) unknownType(v interface{}) { 281 if v == nil { 282 p.buf.Write(nilAngleBytes) 283 return 284 } 285 p.buf.WriteByte('?') 286 p.buf.WriteString(reflect.TypeOf(v).String()) 287 p.buf.WriteByte('?') 288 } 289 290 func (p *pp) badVerb(verb int, val interface{}) { 291 p.add('%') 292 p.add('!') 293 p.add(verb) 294 p.add('(') 295 if val == nil { 296 p.buf.Write(nilAngleBytes) 297 } else { 298 p.buf.WriteString(reflect.TypeOf(val).String()) 299 p.add('=') 300 p.printField(val, 'v', false, false, 0) 301 } 302 p.add(')') 303 } 304 305 func (p *pp) fmtBool(v bool, verb int, value interface{}) { 306 switch verb { 307 case 't', 'v': 308 p.fmt.fmt_boolean(v) 309 default: 310 p.badVerb(verb, value) 311 } 312 } 313 314 // fmtC formats a rune for the 'c' format. 315 func (p *pp) fmtC(c int64) { 316 rune := int(c) // Check for overflow. 317 if int64(rune) != c { 318 rune = utf8.RuneError 319 } 320 w := utf8.EncodeRune(p.runeBuf[0:utf8.UTFMax], rune) 321 p.fmt.pad(p.runeBuf[0:w]) 322 } 323 324 func (p *pp) fmtInt64(v int64, verb int, value interface{}) { 325 switch verb { 326 case 'b': 327 p.fmt.integer(v, 2, signed, ldigits) 328 case 'c': 329 p.fmtC(v) 330 case 'd', 'v': 331 p.fmt.integer(v, 10, signed, ldigits) 332 case 'o': 333 p.fmt.integer(v, 8, signed, ldigits) 334 case 'x': 335 p.fmt.integer(v, 16, signed, ldigits) 336 case 'U': 337 p.fmtUnicode(v) 338 case 'X': 339 p.fmt.integer(v, 16, signed, udigits) 340 default: 341 p.badVerb(verb, value) 342 } 343 } 344 345 // fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or 346 // not, as requested, by temporarily setting the sharp flag. 347 func (p *pp) fmt0x64(v uint64, leading0x bool) { 348 sharp := p.fmt.sharp 349 p.fmt.sharp = leading0x 350 p.fmt.integer(int64(v), 16, unsigned, ldigits) 351 p.fmt.sharp = sharp 352 } 353 354 // fmtUnicode formats a uint64 in U+1234 form by 355 // temporarily turning on the unicode flag and tweaking the precision. 356 func (p *pp) fmtUnicode(v int64) { 357 precPresent := p.fmt.precPresent 358 prec := p.fmt.prec 359 if !precPresent { 360 // If prec is already set, leave it alone; otherwise 4 is minimum. 361 p.fmt.prec = 4 362 p.fmt.precPresent = true 363 } 364 p.fmt.unicode = true // turn on U+ 365 p.fmt.integer(int64(v), 16, unsigned, udigits) 366 p.fmt.unicode = false 367 p.fmt.prec = prec 368 p.fmt.precPresent = precPresent 369 } 370 371 func (p *pp) fmtUint64(v uint64, verb int, goSyntax bool, value interface{}) { 372 switch verb { 373 case 'b': 374 p.fmt.integer(int64(v), 2, unsigned, ldigits) 375 case 'c': 376 p.fmtC(int64(v)) 377 case 'd': 378 p.fmt.integer(int64(v), 10, unsigned, ldigits) 379 case 'v': 380 if goSyntax { 381 p.fmt0x64(v, true) 382 } else { 383 p.fmt.integer(int64(v), 10, unsigned, ldigits) 384 } 385 case 'o': 386 p.fmt.integer(int64(v), 8, unsigned, ldigits) 387 case 'x': 388 p.fmt.integer(int64(v), 16, unsigned, ldigits) 389 case 'X': 390 p.fmt.integer(int64(v), 16, unsigned, udigits) 391 default: 392 p.badVerb(verb, value) 393 } 394 } 395 396 func (p *pp) fmtFloat32(v float32, verb int, value interface{}) { 397 switch verb { 398 case 'b': 399 p.fmt.fmt_fb32(v) 400 case 'e': 401 p.fmt.fmt_e32(v) 402 case 'E': 403 p.fmt.fmt_E32(v) 404 case 'f': 405 p.fmt.fmt_f32(v) 406 case 'g', 'v': 407 p.fmt.fmt_g32(v) 408 case 'G': 409 p.fmt.fmt_G32(v) 410 default: 411 p.badVerb(verb, value) 412 } 413 } 414 415 func (p *pp) fmtFloat64(v float64, verb int, value interface{}) { 416 switch verb { 417 case 'b': 418 p.fmt.fmt_fb64(v) 419 case 'e': 420 p.fmt.fmt_e64(v) 421 case 'E': 422 p.fmt.fmt_E64(v) 423 case 'f': 424 p.fmt.fmt_f64(v) 425 case 'g', 'v': 426 p.fmt.fmt_g64(v) 427 case 'G': 428 p.fmt.fmt_G64(v) 429 default: 430 p.badVerb(verb, value) 431 } 432 } 433 434 func (p *pp) fmtComplex64(v complex64, verb int, value interface{}) { 435 switch verb { 436 case 'e', 'E', 'f', 'F', 'g', 'G': 437 p.fmt.fmt_c64(v, verb) 438 case 'v': 439 p.fmt.fmt_c64(v, 'g') 440 default: 441 p.badVerb(verb, value) 442 } 443 } 444 445 func (p *pp) fmtComplex128(v complex128, verb int, value interface{}) { 446 switch verb { 447 case 'e', 'E', 'f', 'F', 'g', 'G': 448 p.fmt.fmt_c128(v, verb) 449 case 'v': 450 p.fmt.fmt_c128(v, 'g') 451 default: 452 p.badVerb(verb, value) 453 } 454 } 455 456 func (p *pp) fmtString(v string, verb int, goSyntax bool, value interface{}) { 457 switch verb { 458 case 'v': 459 if goSyntax { 460 p.fmt.fmt_q(v) 461 } else { 462 p.fmt.fmt_s(v) 463 } 464 case 's': 465 p.fmt.fmt_s(v) 466 case 'x': 467 p.fmt.fmt_sx(v) 468 case 'X': 469 p.fmt.fmt_sX(v) 470 case 'q': 471 p.fmt.fmt_q(v) 472 default: 473 p.badVerb(verb, value) 474 } 475 } 476 477 func (p *pp) fmtBytes(v []byte, verb int, goSyntax bool, depth int, value interface{}) { 478 if verb == 'v' || verb == 'd' { 479 if goSyntax { 480 p.buf.Write(bytesBytes) 481 } else { 482 p.buf.WriteByte('[') 483 } 484 for i, c := range v { 485 if i > 0 { 486 if goSyntax { 487 p.buf.Write(commaSpaceBytes) 488 } else { 489 p.buf.WriteByte(' ') 490 } 491 } 492 p.printField(c, 'v', p.fmt.plus, goSyntax, depth+1) 493 } 494 if goSyntax { 495 p.buf.WriteByte('}') 496 } else { 497 p.buf.WriteByte(']') 498 } 499 return 500 } 501 s := string(v) 502 switch verb { 503 case 's': 504 p.fmt.fmt_s(s) 505 case 'x': 506 p.fmt.fmt_sx(s) 507 case 'X': 508 p.fmt.fmt_sX(s) 509 case 'q': 510 p.fmt.fmt_q(s) 511 default: 512 p.badVerb(verb, value) 513 } 514 } 515 516 func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSyntax bool) { 517 var u uintptr 518 switch value.Kind() { 519 case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer: 520 u = value.Pointer() 521 default: 522 p.badVerb(verb, field) 523 return 524 } 525 if goSyntax { 526 p.add('(') 527 p.buf.WriteString(reflect.TypeOf(field).String()) 528 p.add(')') 529 p.add('(') 530 if u == 0 { 531 p.buf.Write(nilBytes) 532 } else { 533 p.fmt0x64(uint64(u), true) 534 } 535 p.add(')') 536 } else { 537 p.fmt0x64(uint64(u), !p.fmt.sharp) 538 } 539 } 540 541 var ( 542 intBits = reflect.TypeOf(0).Bits() 543 floatBits = reflect.TypeOf(0.0).Bits() 544 complexBits = reflect.TypeOf(1i).Bits() 545 uintptrBits = reflect.TypeOf(uintptr(0)).Bits() 546 ) 547 548 func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth int) (wasString bool) { 549 if field == nil { 550 if verb == 'T' || verb == 'v' { 551 p.buf.Write(nilAngleBytes) 552 } else { 553 p.badVerb(verb, field) 554 } 555 return false 556 } 557 558 // Special processing considerations. 559 // %T (the value's type) and %p (its address) are special; we always do them first. 560 switch verb { 561 case 'T': 562 p.printField(reflect.TypeOf(field).String(), 's', false, false, 0) 563 return false 564 case 'p': 565 p.fmtPointer(field, reflect.ValueOf(field), verb, goSyntax) 566 return false 567 } 568 // Is it a Formatter? 569 if formatter, ok := field.(Formatter); ok { 570 formatter.Format(p, verb) 571 return false // this value is not a string 572 573 } 574 // Must not touch flags before Formatter looks at them. 575 if plus { 576 p.fmt.plus = false 577 } 578 // If we're doing Go syntax and the field knows how to supply it, take care of it now. 579 if goSyntax { 580 p.fmt.sharp = false 581 if stringer, ok := field.(GoStringer); ok { 582 // Print the result of GoString unadorned. 583 p.fmtString(stringer.GoString(), 's', false, field) 584 return false // this value is not a string 585 } 586 } else { 587 // Is it a Stringer? 588 if stringer, ok := field.(Stringer); ok { 589 p.printField(stringer.String(), verb, plus, false, depth) 590 return false // this value is not a string 591 } 592 } 593 594 // Some types can be done without reflection. 595 switch f := field.(type) { 596 case bool: 597 p.fmtBool(f, verb, field) 598 return false 599 case float32: 600 p.fmtFloat32(f, verb, field) 601 return false 602 case float64: 603 p.fmtFloat64(f, verb, field) 604 return false 605 case complex64: 606 p.fmtComplex64(complex64(f), verb, field) 607 return false 608 case complex128: 609 p.fmtComplex128(f, verb, field) 610 return false 611 case int: 612 p.fmtInt64(int64(f), verb, field) 613 return false 614 case int8: 615 p.fmtInt64(int64(f), verb, field) 616 return false 617 case int16: 618 p.fmtInt64(int64(f), verb, field) 619 return false 620 case int32: 621 p.fmtInt64(int64(f), verb, field) 622 return false 623 case int64: 624 p.fmtInt64(f, verb, field) 625 return false 626 case uint: 627 p.fmtUint64(uint64(f), verb, goSyntax, field) 628 return false 629 case uint8: 630 p.fmtUint64(uint64(f), verb, goSyntax, field) 631 return false 632 case uint16: 633 p.fmtUint64(uint64(f), verb, goSyntax, field) 634 return false 635 case uint32: 636 p.fmtUint64(uint64(f), verb, goSyntax, field) 637 return false 638 case uint64: 639 p.fmtUint64(f, verb, goSyntax, field) 640 return false 641 case uintptr: 642 p.fmtUint64(uint64(f), verb, goSyntax, field) 643 return false 644 case string: 645 p.fmtString(f, verb, goSyntax, field) 646 return verb == 's' || verb == 'v' 647 case []byte: 648 p.fmtBytes(f, verb, goSyntax, depth, field) 649 return verb == 's' 650 } 651 652 // Need to use reflection 653 value := reflect.ValueOf(field) 654 655 BigSwitch: 656 switch f := value; f.Kind() { 657 case reflect.Bool: 658 p.fmtBool(f.Bool(), verb, field) 659 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 660 p.fmtInt64(f.Int(), verb, field) 661 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 662 p.fmtUint64(uint64(f.Uint()), verb, goSyntax, field) 663 case reflect.Float32, reflect.Float64: 664 if f.Type().Size() == 4 { 665 p.fmtFloat32(float32(f.Float()), verb, field) 666 } else { 667 p.fmtFloat64(float64(f.Float()), verb, field) 668 } 669 case reflect.Complex64, reflect.Complex128: 670 if f.Type().Size() == 8 { 671 p.fmtComplex64(complex64(f.Complex()), verb, field) 672 } else { 673 p.fmtComplex128(complex128(f.Complex()), verb, field) 674 } 675 case reflect.String: 676 p.fmtString(f.String(), verb, goSyntax, field) 677 case reflect.Map: 678 if goSyntax { 679 p.buf.WriteString(f.Type().String()) 680 p.buf.WriteByte('{') 681 } else { 682 p.buf.Write(mapBytes) 683 } 684 keys := f.MapKeys() 685 for i, key := range keys { 686 if i > 0 { 687 if goSyntax { 688 p.buf.Write(commaSpaceBytes) 689 } else { 690 p.buf.WriteByte(' ') 691 } 692 } 693 p.printField(key.Interface(), verb, plus, goSyntax, depth+1) 694 p.buf.WriteByte(':') 695 p.printField(f.MapIndex(key).Interface(), verb, plus, goSyntax, depth+1) 696 } 697 if goSyntax { 698 p.buf.WriteByte('}') 699 } else { 700 p.buf.WriteByte(']') 701 } 702 case reflect.Struct: 703 if goSyntax { 704 p.buf.WriteString(reflect.TypeOf(field).String()) 705 } 706 p.add('{') 707 v := f 708 t := v.Type() 709 for i := 0; i < v.NumField(); i++ { 710 if i > 0 { 711 if goSyntax { 712 p.buf.Write(commaSpaceBytes) 713 } else { 714 p.buf.WriteByte(' ') 715 } 716 } 717 if plus || goSyntax { 718 if f := t.Field(i); f.Name != "" { 719 p.buf.WriteString(f.Name) 720 p.buf.WriteByte(':') 721 } 722 } 723 p.printField(getField(v, i).Interface(), verb, plus, goSyntax, depth+1) 724 } 725 p.buf.WriteByte('}') 726 case reflect.Interface: 727 value := f.Elem() 728 if !value.IsValid() { 729 if goSyntax { 730 p.buf.WriteString(reflect.TypeOf(field).String()) 731 p.buf.Write(nilParenBytes) 732 } else { 733 p.buf.Write(nilAngleBytes) 734 } 735 } else { 736 return p.printField(value.Interface(), verb, plus, goSyntax, depth+1) 737 } 738 case reflect.Array, reflect.Slice: 739 // Byte slices are special. 740 if f.Type().Elem().Kind() == reflect.Uint8 { 741 // We know it's a slice of bytes, but we also know it does not have static type 742 // []byte, or it would have been caught above. Therefore we cannot convert 743 // it directly in the (slightly) obvious way: f.Interface().([]byte); it doesn't have 744 // that type, and we can't write an expression of the right type and do a 745 // conversion because we don't have a static way to write the right type. 746 // So we build a slice by hand. This is a rare case but it would be nice 747 // if reflection could help a little more. 748 bytes := make([]byte, f.Len()) 749 for i := range bytes { 750 bytes[i] = byte(f.Index(i).Uint()) 751 } 752 p.fmtBytes(bytes, verb, goSyntax, depth, field) 753 return verb == 's' 754 } 755 if goSyntax { 756 p.buf.WriteString(reflect.TypeOf(field).String()) 757 p.buf.WriteByte('{') 758 } else { 759 p.buf.WriteByte('[') 760 } 761 for i := 0; i < f.Len(); i++ { 762 if i > 0 { 763 if goSyntax { 764 p.buf.Write(commaSpaceBytes) 765 } else { 766 p.buf.WriteByte(' ') 767 } 768 } 769 p.printField(f.Index(i).Interface(), verb, plus, goSyntax, depth+1) 770 } 771 if goSyntax { 772 p.buf.WriteByte('}') 773 } else { 774 p.buf.WriteByte(']') 775 } 776 case reflect.Ptr: 777 v := f.Pointer() 778 // pointer to array or slice or struct? ok at top level 779 // but not embedded (avoid loops) 780 if v != 0 && depth == 0 { 781 switch a := f.Elem(); a.Kind() { 782 case reflect.Array, reflect.Slice: 783 p.buf.WriteByte('&') 784 p.printField(a.Interface(), verb, plus, goSyntax, depth+1) 785 break BigSwitch 786 case reflect.Struct: 787 p.buf.WriteByte('&') 788 p.printField(a.Interface(), verb, plus, goSyntax, depth+1) 789 break BigSwitch 790 } 791 } 792 if goSyntax { 793 p.buf.WriteByte('(') 794 p.buf.WriteString(reflect.TypeOf(field).String()) 795 p.buf.WriteByte(')') 796 p.buf.WriteByte('(') 797 if v == 0 { 798 p.buf.Write(nilBytes) 799 } else { 800 p.fmt0x64(uint64(v), true) 801 } 802 p.buf.WriteByte(')') 803 break 804 } 805 if v == 0 { 806 p.buf.Write(nilAngleBytes) 807 break 808 } 809 p.fmt0x64(uint64(v), true) 810 case reflect.Chan, reflect.Func, reflect.UnsafePointer: 811 p.fmtPointer(field, value, verb, goSyntax) 812 default: 813 p.unknownType(f) 814 } 815 return false 816 } 817 818 // intFromArg gets the fieldnumth element of a. On return, isInt reports whether the argument has type int. 819 func intFromArg(a []interface{}, end, i, fieldnum int) (num int, isInt bool, newi, newfieldnum int) { 820 newi, newfieldnum = end, fieldnum 821 if i < end && fieldnum < len(a) { 822 num, isInt = a[fieldnum].(int) 823 newi, newfieldnum = i+1, fieldnum+1 824 } 825 return 826 } 827 828 func (p *pp) doPrintf(format string, a []interface{}) { 829 end := len(format) 830 fieldnum := 0 // we process one field per non-trivial format 831 for i := 0; i < end; { 832 lasti := i 833 for i < end && format[i] != '%' { 834 i++ 835 } 836 if i > lasti { 837 p.buf.WriteString(format[lasti:i]) 838 } 839 if i >= end { 840 // done processing format string 841 break 842 } 843 844 // Process one verb 845 i++ 846 // flags and widths 847 p.fmt.clearflags() 848 F: 849 for ; i < end; i++ { 850 switch format[i] { 851 case '#': 852 p.fmt.sharp = true 853 case '0': 854 p.fmt.zero = true 855 case '+': 856 p.fmt.plus = true 857 case '-': 858 p.fmt.minus = true 859 case ' ': 860 p.fmt.space = true 861 default: 862 break F 863 } 864 } 865 // do we have width? 866 if i < end && format[i] == '*' { 867 p.fmt.wid, p.fmt.widPresent, i, fieldnum = intFromArg(a, end, i, fieldnum) 868 if !p.fmt.widPresent { 869 p.buf.Write(widthBytes) 870 } 871 } else { 872 p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end) 873 } 874 // do we have precision? 875 if i < end && format[i] == '.' { 876 if format[i+1] == '*' { 877 p.fmt.prec, p.fmt.precPresent, i, fieldnum = intFromArg(a, end, i+1, fieldnum) 878 if !p.fmt.precPresent { 879 p.buf.Write(precBytes) 880 } 881 } else { 882 p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i+1, end) 883 } 884 } 885 if i >= end { 886 p.buf.Write(noVerbBytes) 887 continue 888 } 889 c, w := utf8.DecodeRuneInString(format[i:]) 890 i += w 891 // percent is special - absorbs no operand 892 if c == '%' { 893 p.buf.WriteByte('%') // We ignore width and prec. 894 continue 895 } 896 if fieldnum >= len(a) { // out of operands 897 p.buf.WriteByte('%') 898 p.add(c) 899 p.buf.Write(missingBytes) 900 continue 901 } 902 field := a[fieldnum] 903 fieldnum++ 904 905 goSyntax := c == 'v' && p.fmt.sharp 906 plus := c == 'v' && p.fmt.plus 907 p.printField(field, c, plus, goSyntax, 0) 908 } 909 910 if fieldnum < len(a) { 911 p.buf.Write(extraBytes) 912 for ; fieldnum < len(a); fieldnum++ { 913 field := a[fieldnum] 914 if field != nil { 915 p.buf.WriteString(reflect.TypeOf(field).String()) 916 p.buf.WriteByte('=') 917 } 918 p.printField(field, 'v', false, false, 0) 919 if fieldnum+1 < len(a) { 920 p.buf.Write(commaSpaceBytes) 921 } 922 } 923 p.buf.WriteByte(')') 924 } 925 } 926 927 func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) { 928 prevString := false 929 for fieldnum := 0; fieldnum < len(a); fieldnum++ { 930 p.fmt.clearflags() 931 // always add spaces if we're doing println 932 field := a[fieldnum] 933 if fieldnum > 0 { 934 isString := field != nil && reflect.TypeOf(field).Kind() == reflect.String 935 if addspace || !isString && !prevString { 936 p.buf.WriteByte(' ') 937 } 938 } 939 prevString = p.printField(field, 'v', false, false, 0) 940 } 941 if addnewline { 942 p.buf.WriteByte('\n') 943 } 944 }