diff options
Diffstat (limited to 'src/pkg/fmt/print.go')
-rw-r--r-- | src/pkg/fmt/print.go | 53 |
1 files changed, 15 insertions, 38 deletions
diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go index 1ea816d6d..302661f4c 100644 --- a/src/pkg/fmt/print.go +++ b/src/pkg/fmt/print.go @@ -124,45 +124,13 @@ type pp struct { fmt fmt } -// A cache holds a set of reusable objects. -// The slice is a stack (LIFO). -// If more are needed, the cache creates them by calling new. -type cache struct { - mu sync.Mutex - saved []interface{} - new func() interface{} +var ppFree = sync.Pool{ + New: func() interface{} { return new(pp) }, } -func (c *cache) put(x interface{}) { - c.mu.Lock() - if len(c.saved) < cap(c.saved) { - c.saved = append(c.saved, x) - } - c.mu.Unlock() -} - -func (c *cache) get() interface{} { - c.mu.Lock() - n := len(c.saved) - if n == 0 { - c.mu.Unlock() - return c.new() - } - x := c.saved[n-1] - c.saved = c.saved[0 : n-1] - c.mu.Unlock() - return x -} - -func newCache(f func() interface{}) *cache { - return &cache{saved: make([]interface{}, 0, 100), new: f} -} - -var ppFree = newCache(func() interface{} { return new(pp) }) - // newPrinter allocates a new pp struct or grab a cached one. func newPrinter() *pp { - p := ppFree.get().(*pp) + p := ppFree.Get().(*pp) p.panicking = false p.erroring = false p.fmt.init(&p.buf) @@ -178,7 +146,7 @@ func (p *pp) free() { p.buf = p.buf[:0] p.arg = nil p.value = reflect.Value{} - ppFree.put(p) + ppFree.Put(p) } func (p *pp) Width() (wid int, ok bool) { return p.fmt.wid, p.fmt.widPresent } @@ -479,7 +447,7 @@ func (p *pp) fmtFloat32(v float32, verb rune) { p.fmt.fmt_e32(v) case 'E': p.fmt.fmt_E32(v) - case 'f': + case 'f', 'F': p.fmt.fmt_f32(v) case 'g', 'v': p.fmt.fmt_g32(v) @@ -498,7 +466,7 @@ func (p *pp) fmtFloat64(v float64, verb rune) { p.fmt.fmt_e64(v) case 'E': p.fmt.fmt_E64(v) - case 'f': + case 'f', 'F': p.fmt.fmt_f64(v) case 'g', 'v': p.fmt.fmt_g64(v) @@ -555,6 +523,15 @@ func (p *pp) fmtString(v string, verb rune, goSyntax bool) { func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, typ reflect.Type, depth int) { if verb == 'v' || verb == 'd' { if goSyntax { + if v == nil { + if typ == nil { + p.buf.WriteString("[]byte(nil)") + } else { + p.buf.WriteString(typ.String()) + p.buf.Write(nilParenBytes) + } + return + } if typ == nil { p.buf.Write(bytesBytes) } else { |