Commit 5be24046 authored by Rob Pike's avatar Rob Pike

all: avoid bytes.NewBuffer(nil)

The practice encourages people to think this is the way to
create a bytes.Buffer when new(bytes.Buffer) or
just var buf bytes.Buffer work fine.
(html/token.go was missing the point altogether.)

R=golang-dev, bradfitz, r
CC=golang-dev
https://golang.org/cl/5637043
parent 9440d823
...@@ -688,7 +688,7 @@ func TestReadAfterLines(t *testing.T) { ...@@ -688,7 +688,7 @@ func TestReadAfterLines(t *testing.T) {
} }
func TestReadEmptyBuffer(t *testing.T) { func TestReadEmptyBuffer(t *testing.T) {
l, _ := NewReaderSize(bytes.NewBuffer(nil), minReadBufferSize) l, _ := NewReaderSize(new(bytes.Buffer), minReadBufferSize)
line, isPrefix, err := l.ReadLine() line, isPrefix, err := l.ReadLine()
if err != io.EOF { if err != io.EOF {
t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
......
...@@ -229,14 +229,14 @@ func testToFromWithLevel(t *testing.T, level int, input []byte, name string) err ...@@ -229,14 +229,14 @@ func testToFromWithLevel(t *testing.T, level int, input []byte, name string) err
} }
func testToFromWithLevelAndLimit(t *testing.T, level int, input []byte, name string, limit int) error { func testToFromWithLevelAndLimit(t *testing.T, level int, input []byte, name string, limit int) error {
buffer := bytes.NewBuffer(nil) var buffer bytes.Buffer
w := NewWriter(buffer, level) w := NewWriter(&buffer, level)
w.Write(input) w.Write(input)
w.Close() w.Close()
if limit > 0 && buffer.Len() > limit { if limit > 0 && buffer.Len() > limit {
t.Errorf("level: %d, len(compress(data)) = %d > limit = %d", level, buffer.Len(), limit) t.Errorf("level: %d, len(compress(data)) = %d > limit = %d", level, buffer.Len(), limit)
} }
r := NewReader(buffer) r := NewReader(&buffer)
out, err := ioutil.ReadAll(r) out, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
t.Errorf("read: %s", err) t.Errorf("read: %s", err)
......
...@@ -81,7 +81,7 @@ var lzwTests = []lzwTest{ ...@@ -81,7 +81,7 @@ var lzwTests = []lzwTest{
} }
func TestReader(t *testing.T) { func TestReader(t *testing.T) {
b := bytes.NewBuffer(nil) var b bytes.Buffer
for _, tt := range lzwTests { for _, tt := range lzwTests {
d := strings.Split(tt.desc, ";") d := strings.Split(tt.desc, ";")
var order Order var order Order
...@@ -97,7 +97,7 @@ func TestReader(t *testing.T) { ...@@ -97,7 +97,7 @@ func TestReader(t *testing.T) {
rc := NewReader(strings.NewReader(tt.compressed), order, litWidth) rc := NewReader(strings.NewReader(tt.compressed), order, litWidth)
defer rc.Close() defer rc.Close()
b.Reset() b.Reset()
n, err := io.Copy(b, rc) n, err := io.Copy(&b, rc)
if err != nil { if err != nil {
if err != tt.err { if err != tt.err {
t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err) t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
...@@ -116,7 +116,7 @@ func benchmarkDecoder(b *testing.B, n int) { ...@@ -116,7 +116,7 @@ func benchmarkDecoder(b *testing.B, n int) {
b.SetBytes(int64(n)) b.SetBytes(int64(n))
buf0, _ := ioutil.ReadFile("../testdata/e.txt") buf0, _ := ioutil.ReadFile("../testdata/e.txt")
buf0 = buf0[:10000] buf0 = buf0[:10000]
compressed := bytes.NewBuffer(nil) compressed := new(bytes.Buffer)
w := NewWriter(compressed, LSB, 8) w := NewWriter(compressed, LSB, 8)
for i := 0; i < n; i += len(buf0) { for i := 0; i < n; i += len(buf0) {
io.Copy(w, bytes.NewBuffer(buf0)) io.Copy(w, bytes.NewBuffer(buf0))
......
...@@ -124,8 +124,8 @@ func TestWriterDict(t *testing.T) { ...@@ -124,8 +124,8 @@ func TestWriterDict(t *testing.T) {
func TestWriterDictIsUsed(t *testing.T) { func TestWriterDictIsUsed(t *testing.T) {
var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
buf := bytes.NewBuffer(nil) var buf bytes.Buffer
compressor, err := NewWriterDict(buf, BestCompression, input) compressor, err := NewWriterDict(&buf, BestCompression, input)
if err != nil { if err != nil {
t.Errorf("error in NewWriterDict: %s", err) t.Errorf("error in NewWriterDict: %s", err)
return return
......
...@@ -24,7 +24,7 @@ type forkableWriter struct { ...@@ -24,7 +24,7 @@ type forkableWriter struct {
} }
func newForkableWriter() *forkableWriter { func newForkableWriter() *forkableWriter {
return &forkableWriter{bytes.NewBuffer(nil), nil, nil} return &forkableWriter{new(bytes.Buffer), nil, nil}
} }
func (f *forkableWriter) fork() (pre, post *forkableWriter) { func (f *forkableWriter) fork() (pre, post *forkableWriter) {
......
...@@ -570,8 +570,7 @@ func TestGobMapInterfaceEncode(t *testing.T) { ...@@ -570,8 +570,7 @@ func TestGobMapInterfaceEncode(t *testing.T) {
"bo": []bool{false}, "bo": []bool{false},
"st": []string{"s"}, "st": []string{"s"},
} }
buf := bytes.NewBuffer(nil) enc := NewEncoder(new(bytes.Buffer))
enc := NewEncoder(buf)
err := enc.Encode(m) err := enc.Encode(m)
if err != nil { if err != nil {
t.Errorf("encode map: %s", err) t.Errorf("encode map: %s", err)
...@@ -579,7 +578,7 @@ func TestGobMapInterfaceEncode(t *testing.T) { ...@@ -579,7 +578,7 @@ func TestGobMapInterfaceEncode(t *testing.T) {
} }
func TestSliceReusesMemory(t *testing.T) { func TestSliceReusesMemory(t *testing.T) {
buf := bytes.NewBuffer(nil) buf := new(bytes.Buffer)
// Bytes // Bytes
{ {
x := []byte("abcd") x := []byte("abcd")
......
...@@ -103,8 +103,8 @@ func DecodeString(s string) ([]byte, error) { ...@@ -103,8 +103,8 @@ func DecodeString(s string) ([]byte, error) {
// Dump returns a string that contains a hex dump of the given data. The format // Dump returns a string that contains a hex dump of the given data. The format
// of the hex dump matches the output of `hexdump -C` on the command line. // of the hex dump matches the output of `hexdump -C` on the command line.
func Dump(data []byte) string { func Dump(data []byte) string {
buf := bytes.NewBuffer(nil) var buf bytes.Buffer
dumper := Dumper(buf) dumper := Dumper(&buf)
dumper.Write(data) dumper.Write(data)
dumper.Close() dumper.Close()
return string(buf.Bytes()) return string(buf.Bytes())
......
...@@ -155,8 +155,8 @@ func TestDumper(t *testing.T) { ...@@ -155,8 +155,8 @@ func TestDumper(t *testing.T) {
} }
for stride := 1; stride < len(in); stride++ { for stride := 1; stride < len(in); stride++ {
out := bytes.NewBuffer(nil) var out bytes.Buffer
dumper := Dumper(out) dumper := Dumper(&out)
done := 0 done := 0
for done < len(in) { for done < len(in) {
todo := done + stride todo := done + stride
......
...@@ -251,7 +251,7 @@ func Encode(out io.Writer, b *Block) (err error) { ...@@ -251,7 +251,7 @@ func Encode(out io.Writer, b *Block) (err error) {
} }
func EncodeToMemory(b *Block) []byte { func EncodeToMemory(b *Block) []byte {
buf := bytes.NewBuffer(nil) var buf bytes.Buffer
Encode(buf, b) Encode(&buf, b)
return buf.Bytes() return buf.Bytes()
} }
...@@ -73,7 +73,7 @@ var lineBreakerTests = []lineBreakerTest{ ...@@ -73,7 +73,7 @@ var lineBreakerTests = []lineBreakerTest{
func TestLineBreaker(t *testing.T) { func TestLineBreaker(t *testing.T) {
for i, test := range lineBreakerTests { for i, test := range lineBreakerTests {
buf := bytes.NewBuffer(nil) buf := new(bytes.Buffer)
var breaker lineBreaker var breaker lineBreaker
breaker.out = buf breaker.out = buf
_, err := breaker.Write([]byte(test.in)) _, err := breaker.Write([]byte(test.in))
...@@ -93,7 +93,7 @@ func TestLineBreaker(t *testing.T) { ...@@ -93,7 +93,7 @@ func TestLineBreaker(t *testing.T) {
} }
for i, test := range lineBreakerTests { for i, test := range lineBreakerTests {
buf := bytes.NewBuffer(nil) buf := new(bytes.Buffer)
var breaker lineBreaker var breaker lineBreaker
breaker.out = buf breaker.out = buf
......
...@@ -233,8 +233,8 @@ func EscapeString(s string) string { ...@@ -233,8 +233,8 @@ func EscapeString(s string) string {
if strings.IndexAny(s, escapedChars) == -1 { if strings.IndexAny(s, escapedChars) == -1 {
return s return s
} }
buf := bytes.NewBuffer(nil) var buf bytes.Buffer
escape(buf, s) escape(&buf, s)
return buf.String() return buf.String()
} }
......
...@@ -159,9 +159,9 @@ func dump(n *Node) (string, error) { ...@@ -159,9 +159,9 @@ func dump(n *Node) (string, error) {
if n == nil || len(n.Child) == 0 { if n == nil || len(n.Child) == 0 {
return "", nil return "", nil
} }
b := bytes.NewBuffer(nil) var b bytes.Buffer
for _, child := range n.Child { for _, child := range n.Child {
if err := dumpLevel(b, child, 0); err != nil { if err := dumpLevel(&b, child, 0); err != nil {
return "", err return "", err
} }
} }
......
...@@ -77,8 +77,7 @@ func (t Token) tagString() string { ...@@ -77,8 +77,7 @@ func (t Token) tagString() string {
if len(t.Attr) == 0 { if len(t.Attr) == 0 {
return t.Data return t.Data
} }
buf := bytes.NewBuffer(nil) buf := bytes.NewBufferString(t.Data)
buf.WriteString(t.Data)
for _, a := range t.Attr { for _, a := range t.Attr {
buf.WriteByte(' ') buf.WriteByte(' ')
buf.WriteString(a.Key) buf.WriteString(a.Key)
......
...@@ -555,8 +555,8 @@ func TestUnescapeEscape(t *testing.T) { ...@@ -555,8 +555,8 @@ func TestUnescapeEscape(t *testing.T) {
func TestBufAPI(t *testing.T) { func TestBufAPI(t *testing.T) {
s := "0<a>1</a>2<b>3<a>4<a>5</a>6</b>7</a>8<a/>9" s := "0<a>1</a>2<b>3<a>4<a>5</a>6</b>7</a>8<a/>9"
z := NewTokenizer(bytes.NewBuffer([]byte(s))) z := NewTokenizer(bytes.NewBufferString(s))
result := bytes.NewBuffer(nil) var result bytes.Buffer
depth := 0 depth := 0
loop: loop:
for { for {
......
...@@ -238,8 +238,8 @@ func EscapeString(s string) string { ...@@ -238,8 +238,8 @@ func EscapeString(s string) string {
if strings.IndexAny(s, escapedChars) == -1 { if strings.IndexAny(s, escapedChars) == -1 {
return s return s
} }
buf := bytes.NewBuffer(nil) var buf bytes.Buffer
escape(buf, s) escape(&buf, s)
return buf.String() return buf.String()
} }
......
...@@ -54,14 +54,14 @@ func TestWriter(t *testing.T) { ...@@ -54,14 +54,14 @@ func TestWriter(t *testing.T) {
continue continue
} }
// Encode that image as JPEG. // Encode that image as JPEG.
buf := bytes.NewBuffer(nil) var buf bytes.Buffer
err = Encode(buf, m0, &Options{Quality: tc.quality}) err = Encode(&buf, m0, &Options{Quality: tc.quality})
if err != nil { if err != nil {
t.Error(tc.filename, err) t.Error(tc.filename, err)
continue continue
} }
// Decode that JPEG. // Decode that JPEG.
m1, err := Decode(buf) m1, err := Decode(&buf)
if err != nil { if err != nil {
t.Error(tc.filename, err) t.Error(tc.filename, err)
continue continue
......
...@@ -35,12 +35,12 @@ func diff(m0, m1 image.Image) error { ...@@ -35,12 +35,12 @@ func diff(m0, m1 image.Image) error {
} }
func encodeDecode(m image.Image) (image.Image, error) { func encodeDecode(m image.Image) (image.Image, error) {
b := bytes.NewBuffer(nil) var b bytes.Buffer
err := Encode(b, m) err := Encode(&b, m)
if err != nil { if err != nil {
return nil, err return nil, err
} }
m, err = Decode(b) m, err = Decode(&b)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -795,8 +795,8 @@ func TestFormatters(t *testing.T) { ...@@ -795,8 +795,8 @@ func TestFormatters(t *testing.T) {
t.Errorf("For %q, expected error, got none.", c.in) t.Errorf("For %q, expected error, got none.", c.in)
continue continue
} }
buf := bytes.NewBuffer(nil) var buf bytes.Buffer
err = tmpl.Execute(buf, data) err = tmpl.Execute(&buf, data)
if err != nil { if err != nil {
t.Error("unexpected Execute error: ", err) t.Error("unexpected Execute error: ", err)
continue continue
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment