aboutsummaryrefslogtreecommitdiff
path: root/src/mime
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2018-03-26 06:56:39 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2018-03-26 23:05:53 +0000
commit48db2c01b42d959f2d8fa0c24d853bdb6100cf8a (patch)
treec80a885e1971c4114d33c25e905def159be7f73c /src/mime
parentf0eca373beb94763b71dadcf6504a95a3797dcbb (diff)
downloadgo-48db2c01b42d959f2d8fa0c24d853bdb6100cf8a.tar.gz
go-48db2c01b42d959f2d8fa0c24d853bdb6100cf8a.zip
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>
Diffstat (limited to 'src/mime')
-rw-r--r--src/mime/encodedword.go56
-rw-r--r--src/mime/mediatype.go7
2 files changed, 20 insertions, 43 deletions
diff --git a/src/mime/encodedword.go b/src/mime/encodedword.go
index 99eb432f54..d73c8f402c 100644
--- a/src/mime/encodedword.go
+++ b/src/mime/encodedword.go
@@ -11,7 +11,6 @@ import (
"fmt"
"io"
"strings"
- "sync"
"unicode"
"unicode/utf8"
)
@@ -51,16 +50,15 @@ func needsEncoding(s string) bool {
// encodeWord encodes a string into an encoded-word.
func (e WordEncoder) encodeWord(charset, s string) string {
- buf := getBuffer()
- defer putBuffer(buf)
+ var buf strings.Builder
- e.openWord(buf, charset)
+ e.openWord(&buf, charset)
if e == BEncoding {
- e.bEncode(buf, charset, s)
+ e.bEncode(&buf, charset, s)
} else {
- e.qEncode(buf, charset, s)
+ e.qEncode(&buf, charset, s)
}
- closeWord(buf)
+ closeWord(&buf)
return buf.String()
}
@@ -77,7 +75,7 @@ const (
var maxBase64Len = base64.StdEncoding.DecodedLen(maxContentLen)
// 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)
// If the charset is not UTF-8 or if the content is short, do not bother
// 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
// 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.
if !isUTF8(charset) {
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.
-func writeQString(buf *bytes.Buffer, s string) {
+func writeQString(buf *strings.Builder, s string) {
for i := 0; i < len(s); i++ {
switch b := s[i]; {
case b == ' ':
@@ -155,7 +153,7 @@ func writeQString(buf *bytes.Buffer, s string) {
}
// 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(charset)
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.
-func closeWord(buf *bytes.Buffer) {
+func closeWord(buf *strings.Builder) {
buf.WriteString("?=")
}
// 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)
buf.WriteByte(' ')
e.openWord(buf, charset)
@@ -224,10 +222,9 @@ func (d *WordDecoder) Decode(word string) (string, error) {
return "", err
}
- buf := getBuffer()
- defer putBuffer(buf)
+ var buf strings.Builder
- if err := d.convert(buf, charset, content); err != nil {
+ if err := d.convert(&buf, charset, content); err != nil {
return "", err
}
@@ -243,8 +240,7 @@ func (d *WordDecoder) DecodeHeader(header string) (string, error) {
return header, nil
}
- buf := getBuffer()
- defer putBuffer(buf)
+ var buf strings.Builder
buf.WriteString(header[:i])
header = header[i:]
@@ -296,7 +292,7 @@ func (d *WordDecoder) DecodeHeader(header string) (string, error) {
buf.WriteString(header[:start])
}
- if err := d.convert(buf, charset, content); err != nil {
+ if err := d.convert(&buf, charset, content); err != nil {
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 {
case strings.EqualFold("utf-8", charset):
buf.Write(content)
@@ -346,7 +342,7 @@ func (d *WordDecoder) convert(buf *bytes.Buffer, charset string, content []byte)
if err != nil {
return err
}
- if _, err = buf.ReadFrom(r); err != nil {
+ if _, err = io.Copy(buf, r); err != nil {
return err
}
}
@@ -422,21 +418,3 @@ func fromHex(b byte) (byte, error) {
}
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)
-}
diff --git a/src/mime/mediatype.go b/src/mime/mediatype.go
index 426d417da2..ea2bbac189 100644
--- a/src/mime/mediatype.go
+++ b/src/mime/mediatype.go
@@ -5,7 +5,6 @@
package mime
import (
- "bytes"
"errors"
"fmt"
"sort"
@@ -19,7 +18,7 @@ import (
// When any of the arguments result in a standard violation then
// FormatMediaType returns the empty 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 !isToken(t) {
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
// (i.e. RFC 2231 things with stars: "foo*0" or "foo*")
- var buf bytes.Buffer
+ var buf strings.Builder
for key, pieceMap := range continuation {
singlePartKey := key + "*"
if v, ok := pieceMap[singlePartKey]; ok {
@@ -265,7 +264,7 @@ func consumeValue(v string) (value, rest string) {
}
// parse a quoted-string
- buffer := new(bytes.Buffer)
+ buffer := new(strings.Builder)
for i := 1; i < len(v); i++ {
r := v[i]
if r == '"' {