2008-10-24 17:33:29 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package fmt
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:27:16 -07:00
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"utf8"
|
2008-10-24 17:33:29 -06:00
|
|
|
)
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
// Some constants in the form of bytes, to avoid string overhead.
|
|
|
|
// Needlessly fastidious, I suppose.
|
|
|
|
var (
|
2010-02-25 17:43:43 -07:00
|
|
|
commaSpaceBytes = []byte(", ")
|
|
|
|
nilAngleBytes = []byte("<nil>")
|
|
|
|
nilParenBytes = []byte("(nil)")
|
|
|
|
nilBytes = []byte("nil")
|
|
|
|
mapBytes = []byte("map[")
|
|
|
|
missingBytes = []byte("missing")
|
|
|
|
extraBytes = []byte("?(extra ")
|
2010-03-05 21:16:04 -07:00
|
|
|
irparenBytes = []byte("i)")
|
2010-06-14 18:16:35 -06:00
|
|
|
bytesBytes = []byte("[]byte{")
|
2009-12-06 13:03:52 -07:00
|
|
|
)
|
|
|
|
|
2009-06-23 16:20:30 -06:00
|
|
|
// State represents the printer state passed to custom formatters.
|
2009-05-08 12:22:57 -06:00
|
|
|
// It provides access to the io.Writer interface plus information about
|
2009-03-06 04:35:38 -07:00
|
|
|
// the flags and options for the operand's format specifier.
|
2009-06-23 16:20:30 -06:00
|
|
|
type State interface {
|
2009-03-06 04:35:38 -07:00
|
|
|
// Write is the function to call to emit formatted output to be printed.
|
2009-12-15 16:27:16 -07:00
|
|
|
Write(b []byte) (ret int, err os.Error)
|
2009-03-06 04:35:38 -07:00
|
|
|
// Width returns the value of the width option and whether it has been set.
|
2009-12-15 16:27:16 -07:00
|
|
|
Width() (wid int, ok bool)
|
2009-03-06 04:35:38 -07:00
|
|
|
// Precision returns the value of the precision option and whether it has been set.
|
2009-12-15 16:27:16 -07:00
|
|
|
Precision() (prec int, ok bool)
|
2008-12-11 17:53:33 -07:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Flag returns whether the flag c, a character, has been set.
|
2009-12-15 16:27:16 -07:00
|
|
|
Flag(int) bool
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
|
2009-08-31 17:38:30 -06:00
|
|
|
// Formatter is the interface implemented by values with a custom formatter.
|
2009-03-06 04:35:38 -07:00
|
|
|
// The implementation of Format may call Sprintf or Fprintf(f) etc.
|
|
|
|
// to generate its output.
|
2009-06-23 16:20:30 -06:00
|
|
|
type Formatter interface {
|
2009-12-15 16:27:16 -07:00
|
|
|
Format(f State, c int)
|
2008-11-06 12:38:44 -07:00
|
|
|
}
|
|
|
|
|
2009-08-31 17:38:30 -06:00
|
|
|
// Stringer is implemented by any value that has a String method(),
|
|
|
|
// which defines the ``native'' format for that value.
|
|
|
|
// The String method is used to print values passed as an operand
|
|
|
|
// to a %s or %v format or to an unformatted printer such as Print.
|
2009-05-08 12:22:57 -06:00
|
|
|
type Stringer interface {
|
2009-12-15 16:27:16 -07:00
|
|
|
String() string
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
|
2009-08-31 17:38:30 -06:00
|
|
|
// GoStringer is implemented by any value that has a GoString() method,
|
|
|
|
// which defines the Go syntax for that value.
|
|
|
|
// The GoString method is used to print values passed as an operand
|
|
|
|
// to a %#v format.
|
|
|
|
type GoStringer interface {
|
2009-12-15 16:27:16 -07:00
|
|
|
GoString() string
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
type pp struct {
|
2009-12-15 16:27:16 -07:00
|
|
|
n int
|
|
|
|
buf bytes.Buffer
|
|
|
|
runeBuf [utf8.UTFMax]byte
|
|
|
|
fmt fmt
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
// A leaky bucket of reusable pp structures.
|
|
|
|
var ppFree = make(chan *pp, 100)
|
|
|
|
|
2009-12-06 16:01:07 -07:00
|
|
|
// Allocate a new pp struct. Probably can grab the previous one from ppFree.
|
2009-01-16 14:29:43 -07:00
|
|
|
func newPrinter() *pp {
|
2009-12-15 16:27:16 -07:00
|
|
|
p, ok := <-ppFree
|
2009-12-06 13:03:52 -07:00
|
|
|
if !ok {
|
|
|
|
p = new(pp)
|
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
p.fmt.init(&p.buf)
|
|
|
|
return p
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-12-06 16:01:07 -07:00
|
|
|
// Save used pp structs in ppFree; avoids an allocation per invocation.
|
|
|
|
func (p *pp) free() {
|
|
|
|
// Don't hold on to pp structs with large buffers.
|
|
|
|
if cap(p.buf.Bytes()) > 1024 {
|
|
|
|
return
|
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.Reset()
|
|
|
|
_ = ppFree <- p
|
2009-12-06 16:01:07 -07:00
|
|
|
}
|
2009-12-06 13:03:52 -07:00
|
|
|
|
2009-12-15 16:27:16 -07:00
|
|
|
func (p *pp) Width() (wid int, ok bool) { return p.fmt.wid, p.fmt.widPresent }
|
2008-11-06 11:40:57 -07:00
|
|
|
|
2009-12-15 16:27:16 -07:00
|
|
|
func (p *pp) Precision() (prec int, ok bool) { return p.fmt.prec, p.fmt.precPresent }
|
2008-12-11 17:53:33 -07:00
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) Flag(b int) bool {
|
2008-12-11 17:53:33 -07:00
|
|
|
switch b {
|
|
|
|
case '-':
|
2009-11-09 13:07:39 -07:00
|
|
|
return p.fmt.minus
|
2008-12-11 17:53:33 -07:00
|
|
|
case '+':
|
2009-11-09 13:07:39 -07:00
|
|
|
return p.fmt.plus
|
2008-12-11 17:53:33 -07:00
|
|
|
case '#':
|
2009-11-09 13:07:39 -07:00
|
|
|
return p.fmt.sharp
|
2008-12-11 17:53:33 -07:00
|
|
|
case ' ':
|
2009-11-09 13:07:39 -07:00
|
|
|
return p.fmt.space
|
2008-12-11 17:53:33 -07:00
|
|
|
case '0':
|
2009-11-09 13:07:39 -07:00
|
|
|
return p.fmt.zero
|
2008-12-11 17:53:33 -07:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
return false
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) add(c int) {
|
2009-12-06 16:01:07 -07:00
|
|
|
if c < utf8.RuneSelf {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte(byte(c))
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2010-05-27 15:51:47 -06:00
|
|
|
w := utf8.EncodeRune(c, p.runeBuf[0:])
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.Write(p.runeBuf[0:w])
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-06 13:03:52 -07:00
|
|
|
// Implement Write so we can call Fprintf on a pp (through State), for
|
2008-11-04 14:57:21 -07:00
|
|
|
// recursive use in custom verbs.
|
2009-04-17 01:08:24 -06:00
|
|
|
func (p *pp) Write(b []byte) (ret int, err os.Error) {
|
2009-12-06 13:03:52 -07:00
|
|
|
return p.buf.Write(b)
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These routines end in 'f' and take a format string.
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fprintf formats according to a format specifier and writes to w.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error os.Error) {
|
2009-12-15 16:27:16 -07:00
|
|
|
p := newPrinter()
|
2010-06-14 18:16:35 -06:00
|
|
|
p.doPrintf(format, a)
|
2009-12-15 16:27:16 -07:00
|
|
|
n64, error := p.buf.WriteTo(w)
|
|
|
|
p.free()
|
|
|
|
return int(n64), error
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Printf formats according to a format specifier and writes to standard output.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Printf(format string, a ...interface{}) (n int, errno os.Error) {
|
|
|
|
n, errno = Fprintf(os.Stdout, format, a)
|
2009-12-15 16:27:16 -07:00
|
|
|
return n, errno
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Sprintf formats according to a format specifier and returns the resulting string.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Sprintf(format string, a ...interface{}) string {
|
2009-12-15 16:27:16 -07:00
|
|
|
p := newPrinter()
|
2010-06-14 18:16:35 -06:00
|
|
|
p.doPrintf(format, a)
|
2009-12-15 16:27:16 -07:00
|
|
|
s := p.buf.String()
|
|
|
|
p.free()
|
|
|
|
return s
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// These routines do not take a format string
|
2008-10-24 17:33:29 -06:00
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fprint formats using the default formats for its operands and writes to w.
|
|
|
|
// Spaces are added between operands when neither is a string.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Fprint(w io.Writer, a ...interface{}) (n int, error os.Error) {
|
2009-12-15 16:27:16 -07:00
|
|
|
p := newPrinter()
|
2010-06-14 18:16:35 -06:00
|
|
|
p.doPrint(a, false, false)
|
2009-12-15 16:27:16 -07:00
|
|
|
n64, error := p.buf.WriteTo(w)
|
|
|
|
p.free()
|
|
|
|
return int(n64), error
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Print formats using the default formats for its operands and writes to standard output.
|
|
|
|
// Spaces are added between operands when neither is a string.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Print(a ...interface{}) (n int, errno os.Error) {
|
|
|
|
n, errno = Fprint(os.Stdout, a)
|
2009-12-15 16:27:16 -07:00
|
|
|
return n, errno
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Sprint formats using the default formats for its operands and returns the resulting string.
|
|
|
|
// Spaces are added between operands when neither is a string.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Sprint(a ...interface{}) string {
|
2009-12-15 16:27:16 -07:00
|
|
|
p := newPrinter()
|
2010-06-14 18:16:35 -06:00
|
|
|
p.doPrint(a, false, false)
|
2009-12-15 16:27:16 -07:00
|
|
|
s := p.buf.String()
|
|
|
|
p.free()
|
|
|
|
return s
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These routines end in 'ln', do not take a format string,
|
|
|
|
// always add spaces between operands, and add a newline
|
|
|
|
// after the last operand.
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Fprintln formats using the default formats for its operands and writes to w.
|
|
|
|
// Spaces are always added between operands and a newline is appended.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Fprintln(w io.Writer, a ...interface{}) (n int, error os.Error) {
|
2009-12-15 16:27:16 -07:00
|
|
|
p := newPrinter()
|
2010-06-14 18:16:35 -06:00
|
|
|
p.doPrint(a, true, true)
|
2009-12-15 16:27:16 -07:00
|
|
|
n64, error := p.buf.WriteTo(w)
|
|
|
|
p.free()
|
|
|
|
return int(n64), error
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Println formats using the default formats for its operands and writes to standard output.
|
|
|
|
// Spaces are always added between operands and a newline is appended.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Println(a ...interface{}) (n int, errno os.Error) {
|
|
|
|
n, errno = Fprintln(os.Stdout, a)
|
2009-12-15 16:27:16 -07:00
|
|
|
return n, errno
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:35:38 -07:00
|
|
|
// Sprintln formats using the default formats for its operands and returns the resulting string.
|
|
|
|
// Spaces are always added between operands and a newline is appended.
|
2010-02-01 16:53:37 -07:00
|
|
|
func Sprintln(a ...interface{}) string {
|
2009-12-15 16:27:16 -07:00
|
|
|
p := newPrinter()
|
2010-06-14 18:16:35 -06:00
|
|
|
p.doPrint(a, true, true)
|
2009-12-15 16:27:16 -07:00
|
|
|
s := p.buf.String()
|
|
|
|
p.free()
|
|
|
|
return s
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2008-11-24 15:51:33 -07:00
|
|
|
|
|
|
|
// Get the i'th arg of the struct value.
|
|
|
|
// If the arg itself is an interface, return a value for
|
|
|
|
// the thing inside the interface, not the interface itself.
|
2009-07-07 12:03:31 -06:00
|
|
|
func getField(v *reflect.StructValue, i int) reflect.Value {
|
2009-12-15 16:27:16 -07:00
|
|
|
val := v.Field(i)
|
2009-07-07 12:03:31 -06:00
|
|
|
if i, ok := val.(*reflect.InterfaceValue); ok {
|
|
|
|
if inter := i.Interface(); inter != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return reflect.NewValue(inter)
|
2009-07-07 12:03:31 -06:00
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
return val
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
// Convert ASCII to integer. n is 0 (and got is false) if no number present.
|
|
|
|
func parsenum(s string, start, end int) (num int, isnum bool, newi int) {
|
|
|
|
if start >= end {
|
|
|
|
return 0, false, end
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
for newi = start; newi < end && '0' <= s[newi] && s[newi] <= '9'; newi++ {
|
|
|
|
num = num*10 + int(s[newi]-'0')
|
|
|
|
isnum = true
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
return
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
// Reflection values like reflect.FuncValue implement this method. We use it for %p.
|
|
|
|
type uintptrGetter interface {
|
|
|
|
Get() uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) unknownType(v interface{}) {
|
|
|
|
if v == nil {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
return
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
p.buf.WriteByte('?')
|
|
|
|
p.buf.WriteString(reflect.Typeof(v).String())
|
|
|
|
p.buf.WriteByte('?')
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) badVerb(verb int, val interface{}) {
|
|
|
|
p.add('%')
|
|
|
|
p.add(verb)
|
|
|
|
p.add('(')
|
|
|
|
if val == nil {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteString(reflect.Typeof(val).String())
|
|
|
|
p.add('=')
|
|
|
|
p.printField(val, 'v', false, false, 0)
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
p.add(')')
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
func (p *pp) fmtBool(v bool, verb int, value interface{}) {
|
|
|
|
switch verb {
|
|
|
|
case 't', 'v':
|
|
|
|
p.fmt.fmt_boolean(v)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
2010-02-04 17:23:25 -07:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// fmtC formats a rune for the 'c' format.
|
|
|
|
func (p *pp) fmtC(c int64) {
|
|
|
|
rune := int(c) // Check for overflow.
|
|
|
|
if int64(rune) != c {
|
|
|
|
rune = utf8.RuneError
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
w := utf8.EncodeRune(rune, p.runeBuf[0:utf8.UTFMax])
|
|
|
|
p.fmt.pad(p.runeBuf[0:w])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pp) fmtInt64(v int64, verb int, value interface{}) {
|
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.integer(v, 2, signed, ldigits)
|
|
|
|
case 'c':
|
|
|
|
p.fmtC(v)
|
|
|
|
case 'd', 'v':
|
|
|
|
p.fmt.integer(v, 10, signed, ldigits)
|
|
|
|
case 'o':
|
|
|
|
p.fmt.integer(v, 8, signed, ldigits)
|
|
|
|
case 'x':
|
|
|
|
p.fmt.integer(v, 16, signed, ldigits)
|
|
|
|
case 'X':
|
|
|
|
p.fmt.integer(v, 16, signed, udigits)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
2009-07-07 12:03:31 -06:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// fmt_sharpHex64 formats a uint64 in hexadecimal and prefixes it with 0x by
|
|
|
|
// temporarily turning on the sharp flag.
|
|
|
|
func (p *pp) fmt0x64(v uint64) {
|
|
|
|
sharp := p.fmt.sharp
|
|
|
|
p.fmt.sharp = true // turn on 0x
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, ldigits)
|
|
|
|
p.fmt.sharp = sharp
|
|
|
|
}
|
|
|
|
|
2010-06-28 15:11:38 -06:00
|
|
|
func (p *pp) fmtUint64(v uint64, verb int, goSyntax bool, value interface{}) {
|
2010-06-14 18:16:35 -06:00
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.integer(int64(v), 2, unsigned, ldigits)
|
|
|
|
case 'c':
|
|
|
|
p.fmtC(int64(v))
|
|
|
|
case 'd':
|
|
|
|
p.fmt.integer(int64(v), 10, unsigned, ldigits)
|
|
|
|
case 'v':
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmt0x64(v)
|
|
|
|
} else {
|
|
|
|
p.fmt.integer(int64(v), 10, unsigned, ldigits)
|
|
|
|
}
|
|
|
|
case 'o':
|
|
|
|
p.fmt.integer(int64(v), 8, unsigned, ldigits)
|
|
|
|
case 'x':
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, ldigits)
|
|
|
|
case 'X':
|
|
|
|
p.fmt.integer(int64(v), 16, unsigned, udigits)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
func (p *pp) fmtFloat32(v float32, verb int, value interface{}) {
|
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.fmt_fb32(v)
|
|
|
|
case 'e':
|
|
|
|
p.fmt.fmt_e32(v)
|
|
|
|
case 'E':
|
|
|
|
p.fmt.fmt_E32(v)
|
|
|
|
case 'f':
|
|
|
|
p.fmt.fmt_f32(v)
|
|
|
|
case 'g', 'v':
|
|
|
|
p.fmt.fmt_g32(v)
|
|
|
|
case 'G':
|
|
|
|
p.fmt.fmt_G32(v)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
func (p *pp) fmtFloat64(v float64, verb int, value interface{}) {
|
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
p.fmt.fmt_fb64(v)
|
|
|
|
case 'e':
|
|
|
|
p.fmt.fmt_e64(v)
|
|
|
|
case 'E':
|
|
|
|
p.fmt.fmt_E64(v)
|
|
|
|
case 'f':
|
|
|
|
p.fmt.fmt_f64(v)
|
|
|
|
case 'g', 'v':
|
|
|
|
p.fmt.fmt_g64(v)
|
|
|
|
case 'G':
|
|
|
|
p.fmt.fmt_G64(v)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
func (p *pp) fmtComplex64(v complex64, verb int, value interface{}) {
|
|
|
|
switch verb {
|
|
|
|
case 'e', 'E', 'f', 'F', 'g', 'G':
|
|
|
|
p.fmt.fmt_c64(v, verb)
|
|
|
|
case 'v':
|
|
|
|
p.fmt.fmt_c64(v, 'g')
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
2010-03-05 21:16:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
func (p *pp) fmtComplex128(v complex128, verb int, value interface{}) {
|
|
|
|
switch verb {
|
|
|
|
case 'e', 'E', 'f', 'F', 'g', 'G':
|
|
|
|
p.fmt.fmt_c128(v, verb)
|
|
|
|
case 'v':
|
|
|
|
p.fmt.fmt_c128(v, 'g')
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
2010-03-05 21:16:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-28 15:11:38 -06:00
|
|
|
func (p *pp) fmtString(v string, verb int, goSyntax bool, value interface{}) {
|
2010-06-14 18:16:35 -06:00
|
|
|
switch verb {
|
|
|
|
case 'v':
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmt.fmt_q(v)
|
|
|
|
} else {
|
|
|
|
p.fmt.fmt_s(v)
|
|
|
|
}
|
|
|
|
case 's':
|
|
|
|
p.fmt.fmt_s(v)
|
|
|
|
case 'x':
|
|
|
|
p.fmt.fmt_sx(v)
|
|
|
|
case 'X':
|
|
|
|
p.fmt.fmt_sX(v)
|
|
|
|
case 'q':
|
|
|
|
p.fmt.fmt_q(v)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-28 15:11:38 -06:00
|
|
|
func (p *pp) fmtBytes(v []byte, verb int, goSyntax bool, depth int, value interface{}) {
|
2010-06-14 18:16:35 -06:00
|
|
|
if verb == 'v' {
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.buf.Write(bytesBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte('[')
|
|
|
|
}
|
|
|
|
for i, c := range v {
|
|
|
|
if i > 0 {
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(c, 'v', p.fmt.plus, goSyntax, depth+1)
|
2010-06-14 18:16:35 -06:00
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.buf.WriteByte('}')
|
|
|
|
} else {
|
|
|
|
p.buf.WriteByte(']')
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s := string(v)
|
|
|
|
switch verb {
|
|
|
|
case 's':
|
|
|
|
p.fmt.fmt_s(s)
|
|
|
|
case 'x':
|
|
|
|
p.fmt.fmt_sx(s)
|
|
|
|
case 'X':
|
|
|
|
p.fmt.fmt_sX(s)
|
|
|
|
case 'q':
|
|
|
|
p.fmt.fmt_q(s)
|
|
|
|
default:
|
|
|
|
p.badVerb(verb, value)
|
|
|
|
}
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
func (p *pp) fmtUintptrGetter(field interface{}, value reflect.Value, verb int, sharp bool) bool {
|
|
|
|
v, ok := value.(uintptrGetter)
|
|
|
|
if !ok {
|
|
|
|
return false
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
u := v.Get()
|
|
|
|
if sharp {
|
|
|
|
p.add('(')
|
|
|
|
p.buf.WriteString(reflect.Typeof(field).String())
|
|
|
|
p.add(')')
|
|
|
|
p.add('(')
|
|
|
|
if u == 0 {
|
|
|
|
p.buf.Write(nilBytes)
|
|
|
|
} else {
|
|
|
|
p.fmt0x64(uint64(v.Get()))
|
|
|
|
}
|
|
|
|
p.add(')')
|
|
|
|
} else {
|
|
|
|
p.fmt0x64(uint64(u))
|
|
|
|
}
|
|
|
|
return true
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
|
|
|
|
2010-06-20 13:16:25 -06:00
|
|
|
var (
|
2010-06-21 14:19:29 -06:00
|
|
|
intBits = reflect.Typeof(0).Bits()
|
|
|
|
floatBits = reflect.Typeof(0.0).Bits()
|
|
|
|
complexBits = reflect.Typeof(1i).Bits()
|
|
|
|
uintptrBits = reflect.Typeof(uintptr(0)).Bits()
|
2010-06-20 13:16:25 -06:00
|
|
|
)
|
|
|
|
|
2010-07-29 11:50:09 -06:00
|
|
|
func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth int) (wasString bool) {
|
2010-06-14 18:16:35 -06:00
|
|
|
if field != nil {
|
2009-08-31 17:38:30 -06:00
|
|
|
switch {
|
|
|
|
default:
|
2010-02-01 16:53:37 -07:00
|
|
|
if stringer, ok := field.(Stringer); ok {
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(stringer.String(), verb, plus, goSyntax, depth)
|
2009-12-15 16:27:16 -07:00
|
|
|
return false // this value is not a string
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
case goSyntax:
|
2010-02-01 16:53:37 -07:00
|
|
|
if stringer, ok := field.(GoStringer); ok {
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(stringer.GoString(), verb, plus, goSyntax, depth)
|
2009-12-15 16:27:16 -07:00
|
|
|
return false // this value is not a string
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2008-12-11 13:59:49 -07:00
|
|
|
}
|
2009-11-05 10:40:28 -07:00
|
|
|
}
|
2010-02-01 16:53:37 -07:00
|
|
|
|
|
|
|
// Some types can be done without reflection.
|
2009-07-07 12:03:31 -06:00
|
|
|
switch f := field.(type) {
|
2010-02-01 16:53:37 -07:00
|
|
|
case bool:
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtBool(f, verb, field)
|
2010-02-01 16:53:37 -07:00
|
|
|
return false
|
|
|
|
case float:
|
|
|
|
if floatBits == 32 {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtFloat32(float32(f), verb, field)
|
2010-02-01 16:53:37 -07:00
|
|
|
} else {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtFloat64(float64(f), verb, field)
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
|
|
|
return false
|
2010-06-14 18:16:35 -06:00
|
|
|
case float32:
|
|
|
|
p.fmtFloat32(f, verb, field)
|
2010-03-06 12:26:33 -07:00
|
|
|
return false
|
2010-06-14 18:16:35 -06:00
|
|
|
case float64:
|
|
|
|
p.fmtFloat64(f, verb, field)
|
2010-03-06 12:26:33 -07:00
|
|
|
return false
|
|
|
|
case complex:
|
|
|
|
if complexBits == 64 {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtComplex64(complex64(f), verb, field)
|
2010-03-06 12:26:33 -07:00
|
|
|
} else {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtComplex128(complex128(f), verb, field)
|
2010-03-06 12:26:33 -07:00
|
|
|
}
|
|
|
|
return false
|
2010-06-14 18:16:35 -06:00
|
|
|
case complex64:
|
|
|
|
p.fmtComplex64(complex64(f), verb, field)
|
|
|
|
return false
|
|
|
|
case complex128:
|
|
|
|
p.fmtComplex128(f, verb, field)
|
|
|
|
return false
|
|
|
|
case int:
|
|
|
|
p.fmtInt64(int64(f), verb, field)
|
|
|
|
return false
|
|
|
|
case int8:
|
|
|
|
p.fmtInt64(int64(f), verb, field)
|
|
|
|
return false
|
|
|
|
case int16:
|
|
|
|
p.fmtInt64(int64(f), verb, field)
|
|
|
|
return false
|
|
|
|
case int32:
|
|
|
|
p.fmtInt64(int64(f), verb, field)
|
|
|
|
return false
|
|
|
|
case int64:
|
|
|
|
p.fmtInt64(f, verb, field)
|
|
|
|
return false
|
|
|
|
case uint:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax, field)
|
2010-06-14 18:16:35 -06:00
|
|
|
return false
|
|
|
|
case uint8:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax, field)
|
2010-06-14 18:16:35 -06:00
|
|
|
return false
|
|
|
|
case uint16:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax, field)
|
2010-06-14 18:16:35 -06:00
|
|
|
return false
|
|
|
|
case uint32:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax, field)
|
2010-06-14 18:16:35 -06:00
|
|
|
return false
|
|
|
|
case uint64:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtUint64(f, verb, goSyntax, field)
|
2010-06-14 18:16:35 -06:00
|
|
|
return false
|
|
|
|
case uintptr:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtUint64(uint64(f), verb, goSyntax, field)
|
2010-02-01 16:53:37 -07:00
|
|
|
return false
|
|
|
|
case string:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtString(f, verb, goSyntax, field)
|
2010-06-14 18:16:35 -06:00
|
|
|
return verb == 's' || verb == 'v'
|
|
|
|
case []byte:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtBytes(f, verb, goSyntax, depth, field)
|
2010-06-14 18:16:35 -06:00
|
|
|
return verb == 's'
|
|
|
|
}
|
|
|
|
|
|
|
|
if field == nil {
|
|
|
|
if verb == 'v' {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
2010-02-01 16:53:37 -07:00
|
|
|
} else {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.badVerb(verb, field)
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
return false
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
value := reflect.NewValue(field)
|
2010-02-01 16:53:37 -07:00
|
|
|
// Need to use reflection
|
2010-06-14 18:16:35 -06:00
|
|
|
// Special case for reflection values that know how to print with %p.
|
2010-06-28 15:11:38 -06:00
|
|
|
if verb == 'p' && p.fmtUintptrGetter(field, value, verb, goSyntax) { // TODO: is this goSyntax right?
|
2010-06-14 18:16:35 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2010-02-01 16:53:37 -07:00
|
|
|
BigSwitch:
|
2010-06-14 18:16:35 -06:00
|
|
|
switch f := value.(type) {
|
2009-07-07 12:03:31 -06:00
|
|
|
case *reflect.BoolValue:
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtBool(f.Get(), verb, field)
|
|
|
|
case *reflect.IntValue:
|
|
|
|
p.fmtInt64(f.Get(), verb, field)
|
|
|
|
case *reflect.UintValue:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtUint64(uint64(f.Get()), verb, goSyntax, field)
|
2009-07-07 12:03:31 -06:00
|
|
|
case *reflect.FloatValue:
|
2010-06-20 13:16:25 -06:00
|
|
|
if f.Type().Size() == 4 {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtFloat32(float32(f.Get()), verb, field)
|
2008-11-17 13:34:03 -07:00
|
|
|
} else {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtFloat64(float64(f.Get()), verb, field)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
case *reflect.ComplexValue:
|
2010-06-20 13:16:25 -06:00
|
|
|
if f.Type().Size() == 8 {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtComplex64(complex64(f.Get()), verb, field)
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmtComplex128(complex128(f.Get()), verb, field)
|
2008-12-22 12:04:17 -07:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
case *reflect.StringValue:
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmtString(f.Get(), verb, goSyntax, field)
|
2009-07-09 18:30:07 -06:00
|
|
|
case *reflect.MapValue:
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-02-01 16:53:37 -07:00
|
|
|
p.buf.WriteString(f.Type().String())
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte('{')
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.Write(mapBytes)
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
keys := f.Keys()
|
2009-07-09 18:30:07 -06:00
|
|
|
for i, key := range keys {
|
|
|
|
if i > 0 {
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte(' ')
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2009-07-09 18:30:07 -06:00
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(key.Interface(), verb, plus, goSyntax, depth+1)
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte(':')
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(f.Elem(key).Interface(), verb, plus, goSyntax, depth+1)
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte('}')
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte(']')
|
2009-07-09 18:30:07 -06:00
|
|
|
}
|
2009-07-07 12:03:31 -06:00
|
|
|
case *reflect.StructValue:
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-02-01 16:53:37 -07:00
|
|
|
p.buf.WriteString(reflect.Typeof(field).String())
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
p.add('{')
|
|
|
|
v := f
|
|
|
|
t := v.Type().(*reflect.StructType)
|
2009-10-06 16:38:57 -06:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
2008-12-11 17:53:33 -07:00
|
|
|
if i > 0 {
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte(' ')
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2008-12-11 17:53:33 -07:00
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
if plus || goSyntax {
|
2009-07-07 12:03:31 -06:00
|
|
|
if f := t.Field(i); f.Name != "" {
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteString(f.Name)
|
|
|
|
p.buf.WriteByte(':')
|
2008-12-11 17:53:33 -07:00
|
|
|
}
|
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(getField(v, i).Interface(), verb, plus, goSyntax, depth+1)
|
2008-12-11 17:53:33 -07:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte('}')
|
2009-07-07 12:03:31 -06:00
|
|
|
case *reflect.InterfaceValue:
|
2009-12-15 16:27:16 -07:00
|
|
|
value := f.Elem()
|
2009-04-06 22:28:04 -06:00
|
|
|
if value == nil {
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-02-01 16:53:37 -07:00
|
|
|
p.buf.WriteString(reflect.Typeof(field).String())
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.Write(nilParenBytes)
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.Write(nilAngleBytes)
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2008-12-11 13:59:49 -07:00
|
|
|
} else {
|
2010-06-28 15:11:38 -06:00
|
|
|
return p.printField(value.Interface(), verb, plus, goSyntax, depth+1)
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
|
|
|
case reflect.ArrayOrSliceValue:
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2010-02-01 16:53:37 -07:00
|
|
|
p.buf.WriteString(reflect.Typeof(field).String())
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte('{')
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte('[')
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
|
|
|
for i := 0; i < f.Len(); i++ {
|
|
|
|
if i > 0 {
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte(' ')
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(f.Elem(i).Interface(), verb, plus, goSyntax, depth+1)
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte('}')
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte(']')
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
|
|
|
case *reflect.PtrValue:
|
2009-12-15 16:27:16 -07:00
|
|
|
v := f.Get()
|
2009-08-31 17:38:30 -06:00
|
|
|
// pointer to array or slice or struct? ok at top level
|
|
|
|
// but not embedded (avoid loops)
|
|
|
|
if v != 0 && depth == 0 {
|
|
|
|
switch a := f.Elem().(type) {
|
|
|
|
case reflect.ArrayOrSliceValue:
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte('&')
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(a.Interface(), verb, plus, goSyntax, depth+1)
|
2009-12-15 16:27:16 -07:00
|
|
|
break BigSwitch
|
2009-08-31 17:38:30 -06:00
|
|
|
case *reflect.StructValue:
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte('&')
|
2010-06-28 15:11:38 -06:00
|
|
|
p.printField(a.Interface(), verb, plus, goSyntax, depth+1)
|
2009-12-15 16:27:16 -07:00
|
|
|
break BigSwitch
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
|
|
|
}
|
2010-06-28 15:11:38 -06:00
|
|
|
if goSyntax {
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte('(')
|
2010-02-01 16:53:37 -07:00
|
|
|
p.buf.WriteString(reflect.Typeof(field).String())
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte(')')
|
|
|
|
p.buf.WriteByte('(')
|
2009-08-31 17:38:30 -06:00
|
|
|
if v == 0 {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.Write(nilBytes)
|
2009-08-31 17:38:30 -06:00
|
|
|
} else {
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmt0x64(uint64(v))
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte(')')
|
|
|
|
break
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
|
|
|
if v == 0 {
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
break
|
2009-08-31 17:38:30 -06:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
p.fmt0x64(uint64(v))
|
2009-08-31 17:38:30 -06:00
|
|
|
case uintptrGetter:
|
2010-06-28 15:11:38 -06:00
|
|
|
if p.fmtUintptrGetter(field, value, verb, goSyntax) {
|
2009-12-15 16:27:16 -07:00
|
|
|
break
|
2009-07-07 12:03:31 -06:00
|
|
|
}
|
2010-02-01 16:53:37 -07:00
|
|
|
p.unknownType(f)
|
2010-06-14 18:16:35 -06:00
|
|
|
default:
|
|
|
|
p.unknownType(f)
|
2008-11-13 16:20:52 -07:00
|
|
|
}
|
2010-02-01 16:53:37 -07:00
|
|
|
return false
|
2008-11-13 16:20:52 -07:00
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
func (p *pp) doPrintf(format string, a []interface{}) {
|
2009-12-15 16:27:16 -07:00
|
|
|
end := len(format) - 1
|
|
|
|
fieldnum := 0 // we process one field per non-trivial format
|
2009-10-06 16:38:57 -06:00
|
|
|
for i := 0; i <= end; {
|
2009-12-15 16:27:16 -07:00
|
|
|
c, w := utf8.DecodeRuneInString(format[i:])
|
2008-10-24 17:33:29 -06:00
|
|
|
if c != '%' || i == end {
|
2009-12-06 13:03:52 -07:00
|
|
|
if w == 1 {
|
|
|
|
p.buf.WriteByte(byte(c))
|
|
|
|
} else {
|
|
|
|
p.buf.WriteString(format[i : i+w])
|
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
i += w
|
|
|
|
continue
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
i++
|
2008-12-11 17:53:33 -07:00
|
|
|
// flags and widths
|
2009-12-15 16:27:16 -07:00
|
|
|
p.fmt.clearflags()
|
2010-05-21 21:25:08 -06:00
|
|
|
F:
|
|
|
|
for ; i < end; i++ {
|
2008-11-24 15:51:33 -07:00
|
|
|
switch format[i] {
|
|
|
|
case '#':
|
2009-11-09 13:07:39 -07:00
|
|
|
p.fmt.sharp = true
|
2008-11-24 15:51:33 -07:00
|
|
|
case '0':
|
2009-11-09 13:07:39 -07:00
|
|
|
p.fmt.zero = true
|
2008-11-24 15:51:33 -07:00
|
|
|
case '+':
|
2009-11-09 13:07:39 -07:00
|
|
|
p.fmt.plus = true
|
2008-11-24 15:51:33 -07:00
|
|
|
case '-':
|
2009-11-09 13:07:39 -07:00
|
|
|
p.fmt.minus = true
|
2008-11-24 15:51:33 -07:00
|
|
|
case ' ':
|
2009-11-09 13:07:39 -07:00
|
|
|
p.fmt.space = true
|
2008-11-24 15:51:33 -07:00
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
break F
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// do we have 20 (width)?
|
2009-12-15 16:27:16 -07:00
|
|
|
p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end)
|
2008-11-24 15:51:33 -07:00
|
|
|
// do we have .20 (precision)?
|
2008-10-24 17:33:29 -06:00
|
|
|
if i < end && format[i] == '.' {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i+1, end)
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
c, w = utf8.DecodeRuneInString(format[i:])
|
|
|
|
i += w
|
2008-10-24 17:33:29 -06:00
|
|
|
// percent is special - absorbs no operand
|
|
|
|
if c == '%' {
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte('%') // TODO: should we bother with width & prec?
|
|
|
|
continue
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2010-02-01 16:53:37 -07:00
|
|
|
if fieldnum >= len(a) { // out of operands
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte('%')
|
|
|
|
p.add(c)
|
|
|
|
p.buf.Write(missingBytes)
|
|
|
|
continue
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2010-02-01 16:53:37 -07:00
|
|
|
field := a[fieldnum]
|
2009-12-15 16:27:16 -07:00
|
|
|
fieldnum++
|
2009-08-31 17:38:30 -06:00
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
// %T is special; we always do it here.
|
|
|
|
if c == 'T' {
|
|
|
|
// the value's type
|
2010-04-28 14:07:19 -06:00
|
|
|
if field == nil {
|
|
|
|
p.buf.Write(nilAngleBytes)
|
|
|
|
break
|
|
|
|
}
|
2010-06-14 18:42:31 -06:00
|
|
|
p.printField(reflect.Typeof(field).String(), 's', false, false, 0)
|
2010-06-14 18:16:35 -06:00
|
|
|
continue
|
|
|
|
}
|
2008-11-13 16:20:52 -07:00
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
// Try Formatter (except for %T).
|
|
|
|
if field != nil {
|
|
|
|
if formatter, ok := field.(Formatter); ok {
|
|
|
|
formatter.Format(p, c)
|
|
|
|
continue
|
2010-02-04 17:23:25 -07:00
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
|
2010-06-28 15:11:38 -06:00
|
|
|
goSyntax := c == 'v' && p.fmt.sharp
|
|
|
|
if goSyntax {
|
|
|
|
p.fmt.sharp = false
|
|
|
|
}
|
|
|
|
plus := c == 'v' && p.fmt.plus
|
|
|
|
if plus {
|
|
|
|
p.fmt.plus = false
|
|
|
|
}
|
|
|
|
p.printField(field, c, plus, goSyntax, 0)
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
|
2010-02-01 16:53:37 -07:00
|
|
|
if fieldnum < len(a) {
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.Write(extraBytes)
|
2010-02-01 16:53:37 -07:00
|
|
|
for ; fieldnum < len(a); fieldnum++ {
|
|
|
|
field := a[fieldnum]
|
2010-05-13 13:49:48 -06:00
|
|
|
if field != nil {
|
|
|
|
p.buf.WriteString(reflect.Typeof(field).String())
|
|
|
|
p.buf.WriteByte('=')
|
|
|
|
}
|
2010-06-14 18:16:35 -06:00
|
|
|
p.printField(field, 'v', false, false, 0)
|
2010-02-01 16:53:37 -07:00
|
|
|
if fieldnum+1 < len(a) {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.Write(commaSpaceBytes)
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:27:16 -07:00
|
|
|
p.buf.WriteByte(')')
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2010-06-14 18:16:35 -06:00
|
|
|
func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
|
2010-07-29 11:50:09 -06:00
|
|
|
prevString := false
|
2010-02-01 16:53:37 -07:00
|
|
|
for fieldnum := 0; fieldnum < len(a); fieldnum++ {
|
2010-06-28 15:11:38 -06:00
|
|
|
p.fmt.clearflags()
|
2008-10-24 17:33:29 -06:00
|
|
|
// always add spaces if we're doing println
|
2010-02-01 16:53:37 -07:00
|
|
|
field := a[fieldnum]
|
printf as we know and love it.
Plus print[ln] with the ability to print struct values.
Note for language mavens: if a "..." function passes its argument
to another "..." function, the argument is not wrapped again. This
allows printf to call fprintf without extra manipulation. It's good
but needs to go in the spec.
This code works:
///
package main
import fmt "fmt"
import os "os"
type T struct { s string; a, b int }
func main() {
P := fmt.Printer();
P.printf("%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println("hi there", 7, 123.456);
P.fprintf(os.Stdout, "%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println(T{"x", 7, 234}, "end of struct", 8, 9);
}
R=rsc
DELTA=28 (7 added, 3 deleted, 18 changed)
OCL=18321
CL=18324
2008-11-02 13:33:02 -07:00
|
|
|
if fieldnum > 0 {
|
2010-07-29 11:50:09 -06:00
|
|
|
isString := field != nil && reflect.Typeof(field).Kind() == reflect.String
|
|
|
|
if addspace || !isString && !prevString {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte(' ')
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
}
|
2010-07-29 11:50:09 -06:00
|
|
|
prevString = p.printField(field, 'v', false, false, 0)
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
printf as we know and love it.
Plus print[ln] with the ability to print struct values.
Note for language mavens: if a "..." function passes its argument
to another "..." function, the argument is not wrapped again. This
allows printf to call fprintf without extra manipulation. It's good
but needs to go in the spec.
This code works:
///
package main
import fmt "fmt"
import os "os"
type T struct { s string; a, b int }
func main() {
P := fmt.Printer();
P.printf("%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println("hi there", 7, 123.456);
P.fprintf(os.Stdout, "%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println(T{"x", 7, 234}, "end of struct", 8, 9);
}
R=rsc
DELTA=28 (7 added, 3 deleted, 18 changed)
OCL=18321
CL=18324
2008-11-02 13:33:02 -07:00
|
|
|
if addnewline {
|
2009-12-06 13:03:52 -07:00
|
|
|
p.buf.WriteByte('\n')
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
}
|