all: use strings.Builder instead of bytes.Buffer where appropriate

I grepped for "bytes.Buffer" and "buf.String" and mostly ignored test
files. I skipped a few on purpose and probably missed a few others,
but otherwise I think this should be most of them.

Updates #18990

Change-Id: I5a6ae4296b87b416d8da02d7bfaf981d8cc14774
Reviewed-on: https://go-review.googlesource.com/102479
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Brad Fitzpatrick 2018-03-26 06:56:39 +00:00
parent f0eca373be
commit 48db2c01b4
16 changed files with 56 additions and 86 deletions

View File

@ -5,7 +5,6 @@
package tar package tar
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"path" "path"
@ -176,7 +175,7 @@ func (tw *Writer) writePAXHeader(hdr *Header, paxHdrs map[string]string) error {
sort.Strings(keys) sort.Strings(keys)
// Write each record to a buffer. // Write each record to a buffer.
var buf bytes.Buffer var buf strings.Builder
for _, k := range keys { for _, k := range keys {
rec, err := formatPAXRecord(k, paxHdrs[k]) rec, err := formatPAXRecord(k, paxHdrs[k])
if err != nil { if err != nil {

View File

@ -15,6 +15,7 @@ import (
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
// htmlOutput reads the profile data from profile and generates an HTML // htmlOutput reads the profile data from profile and generates an HTML
@ -41,7 +42,7 @@ func htmlOutput(profile, outfile string) error {
if err != nil { if err != nil {
return fmt.Errorf("can't read %q: %v", fn, err) return fmt.Errorf("can't read %q: %v", fn, err)
} }
var buf bytes.Buffer var buf strings.Builder
err = htmlGen(&buf, src, profile.Boundaries(src)) err = htmlGen(&buf, src, profile.Boundaries(src))
if err != nil { if err != nil {
return err return err

View File

@ -22,7 +22,6 @@
package expvar package expvar
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@ -32,6 +31,7 @@ import (
"runtime" "runtime"
"sort" "sort"
"strconv" "strconv"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
) )
@ -111,7 +111,7 @@ type KeyValue struct {
} }
func (v *Map) String() string { func (v *Map) String() string {
var b bytes.Buffer var b strings.Builder
fmt.Fprintf(&b, "{") fmt.Fprintf(&b, "{")
first := true first := true
v.Do(func(kv KeyValue) { v.Do(func(kv KeyValue) {

View File

@ -7,9 +7,9 @@
package types package types
import ( import (
"bytes"
"fmt" "fmt"
"sort" "sort"
"strings"
) )
// A MethodSet is an ordered set of concrete or abstract (interface) methods; // A MethodSet is an ordered set of concrete or abstract (interface) methods;
@ -24,7 +24,7 @@ func (s *MethodSet) String() string {
return "MethodSet {}" return "MethodSet {}"
} }
var buf bytes.Buffer var buf strings.Builder
fmt.Fprintln(&buf, "MethodSet {") fmt.Fprintln(&buf, "MethodSet {")
for _, f := range s.list { for _, f := range s.list {
fmt.Fprintf(&buf, "\t%s\n", f) fmt.Fprintf(&buf, "\t%s\n", f)

View File

@ -11,7 +11,6 @@ import (
"fmt" "fmt"
"io" "io"
"strings" "strings"
"sync"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
) )
@ -51,16 +50,15 @@ func needsEncoding(s string) bool {
// encodeWord encodes a string into an encoded-word. // encodeWord encodes a string into an encoded-word.
func (e WordEncoder) encodeWord(charset, s string) string { func (e WordEncoder) encodeWord(charset, s string) string {
buf := getBuffer() var buf strings.Builder
defer putBuffer(buf)
e.openWord(buf, charset) e.openWord(&buf, charset)
if e == BEncoding { if e == BEncoding {
e.bEncode(buf, charset, s) e.bEncode(&buf, charset, s)
} else { } else {
e.qEncode(buf, charset, s) e.qEncode(&buf, charset, s)
} }
closeWord(buf) closeWord(&buf)
return buf.String() return buf.String()
} }
@ -77,7 +75,7 @@ const (
var maxBase64Len = base64.StdEncoding.DecodedLen(maxContentLen) var maxBase64Len = base64.StdEncoding.DecodedLen(maxContentLen)
// bEncode encodes s using base64 encoding and writes it to buf. // bEncode encodes s using base64 encoding and writes it to buf.
func (e WordEncoder) bEncode(buf *bytes.Buffer, charset, s string) { func (e WordEncoder) bEncode(buf *strings.Builder, charset, s string) {
w := base64.NewEncoder(base64.StdEncoding, buf) w := base64.NewEncoder(base64.StdEncoding, buf)
// If the charset is not UTF-8 or if the content is short, do not bother // If the charset is not UTF-8 or if the content is short, do not bother
// splitting the encoded-word. // splitting the encoded-word.
@ -109,7 +107,7 @@ func (e WordEncoder) bEncode(buf *bytes.Buffer, charset, s string) {
// qEncode encodes s using Q encoding and writes it to buf. It splits the // qEncode encodes s using Q encoding and writes it to buf. It splits the
// encoded-words when necessary. // encoded-words when necessary.
func (e WordEncoder) qEncode(buf *bytes.Buffer, charset, s string) { func (e WordEncoder) qEncode(buf *strings.Builder, charset, s string) {
// We only split encoded-words when the charset is UTF-8. // We only split encoded-words when the charset is UTF-8.
if !isUTF8(charset) { if !isUTF8(charset) {
writeQString(buf, s) writeQString(buf, s)
@ -139,7 +137,7 @@ func (e WordEncoder) qEncode(buf *bytes.Buffer, charset, s string) {
} }
// writeQString encodes s using Q encoding and writes it to buf. // writeQString encodes s using Q encoding and writes it to buf.
func writeQString(buf *bytes.Buffer, s string) { func writeQString(buf *strings.Builder, s string) {
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
switch b := s[i]; { switch b := s[i]; {
case b == ' ': case b == ' ':
@ -155,7 +153,7 @@ func writeQString(buf *bytes.Buffer, s string) {
} }
// openWord writes the beginning of an encoded-word into buf. // openWord writes the beginning of an encoded-word into buf.
func (e WordEncoder) openWord(buf *bytes.Buffer, charset string) { func (e WordEncoder) openWord(buf *strings.Builder, charset string) {
buf.WriteString("=?") buf.WriteString("=?")
buf.WriteString(charset) buf.WriteString(charset)
buf.WriteByte('?') buf.WriteByte('?')
@ -164,12 +162,12 @@ func (e WordEncoder) openWord(buf *bytes.Buffer, charset string) {
} }
// closeWord writes the end of an encoded-word into buf. // closeWord writes the end of an encoded-word into buf.
func closeWord(buf *bytes.Buffer) { func closeWord(buf *strings.Builder) {
buf.WriteString("?=") buf.WriteString("?=")
} }
// splitWord closes the current encoded-word and opens a new one. // splitWord closes the current encoded-word and opens a new one.
func (e WordEncoder) splitWord(buf *bytes.Buffer, charset string) { func (e WordEncoder) splitWord(buf *strings.Builder, charset string) {
closeWord(buf) closeWord(buf)
buf.WriteByte(' ') buf.WriteByte(' ')
e.openWord(buf, charset) e.openWord(buf, charset)
@ -224,10 +222,9 @@ func (d *WordDecoder) Decode(word string) (string, error) {
return "", err return "", err
} }
buf := getBuffer() var buf strings.Builder
defer putBuffer(buf)
if err := d.convert(buf, charset, content); err != nil { if err := d.convert(&buf, charset, content); err != nil {
return "", err return "", err
} }
@ -243,8 +240,7 @@ func (d *WordDecoder) DecodeHeader(header string) (string, error) {
return header, nil return header, nil
} }
buf := getBuffer() var buf strings.Builder
defer putBuffer(buf)
buf.WriteString(header[:i]) buf.WriteString(header[:i])
header = header[i:] header = header[i:]
@ -296,7 +292,7 @@ func (d *WordDecoder) DecodeHeader(header string) (string, error) {
buf.WriteString(header[:start]) buf.WriteString(header[:start])
} }
if err := d.convert(buf, charset, content); err != nil { if err := d.convert(&buf, charset, content); err != nil {
return "", err return "", err
} }
@ -322,7 +318,7 @@ func decode(encoding byte, text string) ([]byte, error) {
} }
} }
func (d *WordDecoder) convert(buf *bytes.Buffer, charset string, content []byte) error { func (d *WordDecoder) convert(buf *strings.Builder, charset string, content []byte) error {
switch { switch {
case strings.EqualFold("utf-8", charset): case strings.EqualFold("utf-8", charset):
buf.Write(content) buf.Write(content)
@ -346,7 +342,7 @@ func (d *WordDecoder) convert(buf *bytes.Buffer, charset string, content []byte)
if err != nil { if err != nil {
return err return err
} }
if _, err = buf.ReadFrom(r); err != nil { if _, err = io.Copy(buf, r); err != nil {
return err return err
} }
} }
@ -422,21 +418,3 @@ func fromHex(b byte) (byte, error) {
} }
return 0, fmt.Errorf("mime: invalid hex byte %#02x", b) return 0, fmt.Errorf("mime: invalid hex byte %#02x", b)
} }
var bufPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
func getBuffer() *bytes.Buffer {
return bufPool.Get().(*bytes.Buffer)
}
func putBuffer(buf *bytes.Buffer) {
if buf.Len() > 1024 {
return
}
buf.Reset()
bufPool.Put(buf)
}

View File

@ -5,7 +5,6 @@
package mime package mime
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"sort" "sort"
@ -19,7 +18,7 @@ import (
// When any of the arguments result in a standard violation then // When any of the arguments result in a standard violation then
// FormatMediaType returns the empty string. // FormatMediaType returns the empty string.
func FormatMediaType(t string, param map[string]string) string { func FormatMediaType(t string, param map[string]string) string {
var b bytes.Buffer var b strings.Builder
if slash := strings.Index(t, "/"); slash == -1 { if slash := strings.Index(t, "/"); slash == -1 {
if !isToken(t) { if !isToken(t) {
return "" return ""
@ -167,7 +166,7 @@ func ParseMediaType(v string) (mediatype string, params map[string]string, err e
// Stitch together any continuations or things with stars // Stitch together any continuations or things with stars
// (i.e. RFC 2231 things with stars: "foo*0" or "foo*") // (i.e. RFC 2231 things with stars: "foo*0" or "foo*")
var buf bytes.Buffer var buf strings.Builder
for key, pieceMap := range continuation { for key, pieceMap := range continuation {
singlePartKey := key + "*" singlePartKey := key + "*"
if v, ok := pieceMap[singlePartKey]; ok { if v, ok := pieceMap[singlePartKey]; ok {
@ -265,7 +264,7 @@ func consumeValue(v string) (value, rest string) {
} }
// parse a quoted-string // parse a quoted-string
buffer := new(bytes.Buffer) buffer := new(strings.Builder)
for i := 1; i < len(v); i++ { for i := 1; i < len(v); i++ {
r := v[i] r := v[i]
if r == '"' { if r == '"' {

View File

@ -5,7 +5,6 @@
package http package http
import ( import (
"bytes"
"log" "log"
"net" "net"
"strconv" "strconv"
@ -143,7 +142,7 @@ func (c *Cookie) String() string {
if c == nil || !isCookieNameValid(c.Name) { if c == nil || !isCookieNameValid(c.Name) {
return "" return ""
} }
var b bytes.Buffer var b strings.Builder
b.WriteString(sanitizeCookieName(c.Name)) b.WriteString(sanitizeCookieName(c.Name))
b.WriteRune('=') b.WriteRune('=')
b.WriteString(sanitizeCookieValue(c.Value)) b.WriteString(sanitizeCookieValue(c.Value))
@ -168,17 +167,14 @@ func (c *Cookie) String() string {
log.Printf("net/http: invalid Cookie.Domain %q; dropping domain attribute", c.Domain) log.Printf("net/http: invalid Cookie.Domain %q; dropping domain attribute", c.Domain)
} }
} }
var buf [len(TimeFormat)]byte
if validCookieExpires(c.Expires) { if validCookieExpires(c.Expires) {
b.WriteString("; Expires=") b.WriteString("; Expires=")
b2 := b.Bytes() b.Write(c.Expires.UTC().AppendFormat(buf[:0], TimeFormat))
b.Reset()
b.Write(c.Expires.UTC().AppendFormat(b2, TimeFormat))
} }
if c.MaxAge > 0 { if c.MaxAge > 0 {
b.WriteString("; Max-Age=") b.WriteString("; Max-Age=")
b2 := b.Bytes() b.Write(strconv.AppendInt(buf[:0], int64(c.MaxAge), 10))
b.Reset()
b.Write(strconv.AppendInt(b2, int64(c.MaxAge), 10))
} else if c.MaxAge < 0 { } else if c.MaxAge < 0 {
b.WriteString("; Max-Age=0") b.WriteString("; Max-Age=0")
} }

View File

@ -7,7 +7,6 @@
package httptest package httptest
import ( import (
"bytes"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"flag" "flag"
@ -17,6 +16,7 @@ import (
"net/http" "net/http"
"net/http/internal" "net/http/internal"
"os" "os"
"strings"
"sync" "sync"
"time" "time"
) )
@ -224,7 +224,7 @@ func (s *Server) Close() {
func (s *Server) logCloseHangDebugInfo() { func (s *Server) logCloseHangDebugInfo() {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
var buf bytes.Buffer var buf strings.Builder
buf.WriteString("httptest.Server blocked in Close after 5 seconds, waiting for connections:\n") buf.WriteString("httptest.Server blocked in Close after 5 seconds, waiting for connections:\n")
for c, st := range s.conns { for c, st := range s.conns {
fmt.Fprintf(&buf, " %T %p %v in state %v\n", c, c, c.RemoteAddr(), st) fmt.Fprintf(&buf, " %T %p %v in state %v\n", c, c, c.RemoteAddr(), st)

View File

@ -19,7 +19,6 @@ package mail
import ( import (
"bufio" "bufio"
"bytes"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -735,7 +734,7 @@ func isQtext(r rune) bool {
// quoteString renders a string as an RFC 5322 quoted-string. // quoteString renders a string as an RFC 5322 quoted-string.
func quoteString(s string) string { func quoteString(s string) string {
var buf bytes.Buffer var buf strings.Builder
buf.WriteByte('"') buf.WriteByte('"')
for _, r := range s { for _, r := range s {
if isQtext(r) || isWSP(r) { if isQtext(r) || isWSP(r) {

View File

@ -11,7 +11,6 @@ package url
// contain references to issue numbers with details. // contain references to issue numbers with details.
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"sort" "sort"
@ -737,7 +736,7 @@ func validOptionalPort(port string) bool {
// - if u.RawQuery is empty, ?query is omitted. // - if u.RawQuery is empty, ?query is omitted.
// - if u.Fragment is empty, #fragment is omitted. // - if u.Fragment is empty, #fragment is omitted.
func (u *URL) String() string { func (u *URL) String() string {
var buf bytes.Buffer var buf strings.Builder
if u.Scheme != "" { if u.Scheme != "" {
buf.WriteString(u.Scheme) buf.WriteString(u.Scheme)
buf.WriteByte(':') buf.WriteByte(':')
@ -878,7 +877,7 @@ func (v Values) Encode() string {
if v == nil { if v == nil {
return "" return ""
} }
var buf bytes.Buffer var buf strings.Builder
keys := make([]string, 0, len(v)) keys := make([]string, 0, len(v))
for k := range v { for k := range v {
keys = append(keys, k) keys = append(keys, k)
@ -886,12 +885,13 @@ func (v Values) Encode() string {
sort.Strings(keys) sort.Strings(keys)
for _, k := range keys { for _, k := range keys {
vs := v[k] vs := v[k]
prefix := QueryEscape(k) + "=" keyEscaped := QueryEscape(k)
for _, v := range vs { for _, v := range vs {
if buf.Len() > 0 { if buf.Len() > 0 {
buf.WriteByte('&') buf.WriteByte('&')
} }
buf.WriteString(prefix) buf.WriteString(keyEscaped)
buf.WriteByte('=')
buf.WriteString(QueryEscape(v)) buf.WriteString(QueryEscape(v))
} }
} }

View File

@ -5,9 +5,9 @@
package regexp package regexp
import ( import (
"bytes"
"regexp/syntax" "regexp/syntax"
"sort" "sort"
"strings"
"unicode" "unicode"
) )
@ -54,7 +54,7 @@ func onePassPrefix(p *syntax.Prog) (prefix string, complete bool, pc uint32) {
} }
// Have prefix; gather characters. // Have prefix; gather characters.
var buf bytes.Buffer var buf strings.Builder
for iop(i) == syntax.InstRune && len(i.Rune) == 1 && syntax.Flags(i.Arg)&syntax.FoldCase == 0 { for iop(i) == syntax.InstRune && len(i.Rune) == 1 && syntax.Flags(i.Arg)&syntax.FoldCase == 0 {
buf.WriteRune(i.Rune[0]) buf.WriteRune(i.Rune[0])
pc, i = i.Out, &p.Inst[i.Out] pc, i = i.Out, &p.Inst[i.Out]

View File

@ -5,8 +5,8 @@
package syntax package syntax
import ( import (
"bytes"
"fmt" "fmt"
"strings"
"testing" "testing"
"unicode" "unicode"
) )
@ -282,7 +282,7 @@ func testParseDump(t *testing.T, tests []parseTest, flags Flags) {
// dump prints a string representation of the regexp showing // dump prints a string representation of the regexp showing
// the structure explicitly. // the structure explicitly.
func dump(re *Regexp) string { func dump(re *Regexp) string {
var b bytes.Buffer var b strings.Builder
dumpRegexp(&b, re) dumpRegexp(&b, re)
return b.String() return b.String()
} }
@ -312,7 +312,7 @@ var opNames = []string{
// dumpRegexp writes an encoding of the syntax tree for the regexp re to b. // dumpRegexp writes an encoding of the syntax tree for the regexp re to b.
// It is used during testing to distinguish between parses that might print // It is used during testing to distinguish between parses that might print
// the same using re's String method. // the same using re's String method.
func dumpRegexp(b *bytes.Buffer, re *Regexp) { func dumpRegexp(b *strings.Builder, re *Regexp) {
if int(re.Op) >= len(opNames) || opNames[re.Op] == "" { if int(re.Op) >= len(opNames) || opNames[re.Op] == "" {
fmt.Fprintf(b, "op%d", re.Op) fmt.Fprintf(b, "op%d", re.Op)
} else { } else {

View File

@ -5,8 +5,8 @@
package syntax package syntax
import ( import (
"bytes"
"strconv" "strconv"
"strings"
"unicode" "unicode"
) )
@ -117,7 +117,7 @@ type Inst struct {
} }
func (p *Prog) String() string { func (p *Prog) String() string {
var b bytes.Buffer var b strings.Builder
dumpProg(&b, p) dumpProg(&b, p)
return b.String() return b.String()
} }
@ -153,7 +153,7 @@ func (p *Prog) Prefix() (prefix string, complete bool) {
} }
// Have prefix; gather characters. // Have prefix; gather characters.
var buf bytes.Buffer var buf strings.Builder
for i.op() == InstRune && len(i.Rune) == 1 && Flags(i.Arg)&FoldCase == 0 { for i.op() == InstRune && len(i.Rune) == 1 && Flags(i.Arg)&FoldCase == 0 {
buf.WriteRune(i.Rune[0]) buf.WriteRune(i.Rune[0])
i = p.skipNop(i.Out) i = p.skipNop(i.Out)
@ -267,18 +267,18 @@ func (i *Inst) MatchEmptyWidth(before rune, after rune) bool {
} }
func (i *Inst) String() string { func (i *Inst) String() string {
var b bytes.Buffer var b strings.Builder
dumpInst(&b, i) dumpInst(&b, i)
return b.String() return b.String()
} }
func bw(b *bytes.Buffer, args ...string) { func bw(b *strings.Builder, args ...string) {
for _, s := range args { for _, s := range args {
b.WriteString(s) b.WriteString(s)
} }
} }
func dumpProg(b *bytes.Buffer, p *Prog) { func dumpProg(b *strings.Builder, p *Prog) {
for j := range p.Inst { for j := range p.Inst {
i := &p.Inst[j] i := &p.Inst[j]
pc := strconv.Itoa(j) pc := strconv.Itoa(j)
@ -298,7 +298,7 @@ func u32(i uint32) string {
return strconv.FormatUint(uint64(i), 10) return strconv.FormatUint(uint64(i), 10)
} }
func dumpInst(b *bytes.Buffer, i *Inst) { func dumpInst(b *strings.Builder, i *Inst) {
switch i.Op { switch i.Op {
case InstAlt: case InstAlt:
bw(b, "alt -> ", u32(i.Out), ", ", u32(i.Arg)) bw(b, "alt -> ", u32(i.Out), ", ", u32(i.Arg))

View File

@ -8,7 +8,6 @@ package syntax
// In this package, re is always a *Regexp and r is always a rune. // In this package, re is always a *Regexp and r is always a rune.
import ( import (
"bytes"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
@ -114,7 +113,7 @@ func (x *Regexp) Equal(y *Regexp) bool {
} }
// writeRegexp writes the Perl syntax for the regular expression re to b. // writeRegexp writes the Perl syntax for the regular expression re to b.
func writeRegexp(b *bytes.Buffer, re *Regexp) { func writeRegexp(b *strings.Builder, re *Regexp) {
switch re.Op { switch re.Op {
default: default:
b.WriteString("<invalid op" + strconv.Itoa(int(re.Op)) + ">") b.WriteString("<invalid op" + strconv.Itoa(int(re.Op)) + ">")
@ -245,14 +244,14 @@ func writeRegexp(b *bytes.Buffer, re *Regexp) {
} }
func (re *Regexp) String() string { func (re *Regexp) String() string {
var b bytes.Buffer var b strings.Builder
writeRegexp(&b, re) writeRegexp(&b, re)
return b.String() return b.String()
} }
const meta = `\.+*?()|[]{}^$` const meta = `\.+*?()|[]{}^$`
func escape(b *bytes.Buffer, r rune, force bool) { func escape(b *strings.Builder, r rune, force bool) {
if unicode.IsPrint(r) { if unicode.IsPrint(r) {
if strings.ContainsRune(meta, r) || force { if strings.ContainsRune(meta, r) || force {
b.WriteRune('\\') b.WriteRune('\\')

View File

@ -5,7 +5,6 @@
package testing package testing
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -72,7 +71,7 @@ func runExample(eg InternalExample) (ok bool) {
os.Stdout = w os.Stdout = w
outC := make(chan string) outC := make(chan string)
go func() { go func() {
var buf bytes.Buffer var buf strings.Builder
_, err := io.Copy(&buf, r) _, err := io.Copy(&buf, r)
r.Close() r.Close()
if err != nil { if err != nil {

View File

@ -376,7 +376,7 @@ func (c *common) decorate(s string) string {
file = "???" file = "???"
line = 1 line = 1
} }
buf := new(bytes.Buffer) buf := new(strings.Builder)
// Every line is indented at least one tab. // Every line is indented at least one tab.
buf.WriteByte('\t') buf.WriteByte('\t')
fmt.Fprintf(buf, "%s:%d: ", file, line) fmt.Fprintf(buf, "%s:%d: ", file, line)