1
0
mirror of https://github.com/golang/go synced 2024-11-19 17:14:44 -07:00

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
This commit is contained in:
Rob Pike 2012-02-06 14:09:00 +11:00
parent 9440d823a5
commit 5be24046c7
18 changed files with 37 additions and 39 deletions

View File

@ -688,7 +688,7 @@ func TestReadAfterLines(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()
if err != io.EOF {
t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)

View File

@ -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 {
buffer := bytes.NewBuffer(nil)
w := NewWriter(buffer, level)
var buffer bytes.Buffer
w := NewWriter(&buffer, level)
w.Write(input)
w.Close()
if limit > 0 && 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)
if err != nil {
t.Errorf("read: %s", err)

View File

@ -81,7 +81,7 @@ var lzwTests = []lzwTest{
}
func TestReader(t *testing.T) {
b := bytes.NewBuffer(nil)
var b bytes.Buffer
for _, tt := range lzwTests {
d := strings.Split(tt.desc, ";")
var order Order
@ -97,7 +97,7 @@ func TestReader(t *testing.T) {
rc := NewReader(strings.NewReader(tt.compressed), order, litWidth)
defer rc.Close()
b.Reset()
n, err := io.Copy(b, rc)
n, err := io.Copy(&b, rc)
if err != nil {
if 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) {
b.SetBytes(int64(n))
buf0, _ := ioutil.ReadFile("../testdata/e.txt")
buf0 = buf0[:10000]
compressed := bytes.NewBuffer(nil)
compressed := new(bytes.Buffer)
w := NewWriter(compressed, LSB, 8)
for i := 0; i < n; i += len(buf0) {
io.Copy(w, bytes.NewBuffer(buf0))

View File

@ -124,8 +124,8 @@ func TestWriterDict(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.")
buf := bytes.NewBuffer(nil)
compressor, err := NewWriterDict(buf, BestCompression, input)
var buf bytes.Buffer
compressor, err := NewWriterDict(&buf, BestCompression, input)
if err != nil {
t.Errorf("error in NewWriterDict: %s", err)
return

View File

@ -24,7 +24,7 @@ type forkableWriter struct {
}
func newForkableWriter() *forkableWriter {
return &forkableWriter{bytes.NewBuffer(nil), nil, nil}
return &forkableWriter{new(bytes.Buffer), nil, nil}
}
func (f *forkableWriter) fork() (pre, post *forkableWriter) {

View File

@ -570,8 +570,7 @@ func TestGobMapInterfaceEncode(t *testing.T) {
"bo": []bool{false},
"st": []string{"s"},
}
buf := bytes.NewBuffer(nil)
enc := NewEncoder(buf)
enc := NewEncoder(new(bytes.Buffer))
err := enc.Encode(m)
if err != nil {
t.Errorf("encode map: %s", err)
@ -579,7 +578,7 @@ func TestGobMapInterfaceEncode(t *testing.T) {
}
func TestSliceReusesMemory(t *testing.T) {
buf := bytes.NewBuffer(nil)
buf := new(bytes.Buffer)
// Bytes
{
x := []byte("abcd")

View File

@ -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
// of the hex dump matches the output of `hexdump -C` on the command line.
func Dump(data []byte) string {
buf := bytes.NewBuffer(nil)
dumper := Dumper(buf)
var buf bytes.Buffer
dumper := Dumper(&buf)
dumper.Write(data)
dumper.Close()
return string(buf.Bytes())

View File

@ -155,8 +155,8 @@ func TestDumper(t *testing.T) {
}
for stride := 1; stride < len(in); stride++ {
out := bytes.NewBuffer(nil)
dumper := Dumper(out)
var out bytes.Buffer
dumper := Dumper(&out)
done := 0
for done < len(in) {
todo := done + stride

View File

@ -251,7 +251,7 @@ func Encode(out io.Writer, b *Block) (err error) {
}
func EncodeToMemory(b *Block) []byte {
buf := bytes.NewBuffer(nil)
Encode(buf, b)
var buf bytes.Buffer
Encode(&buf, b)
return buf.Bytes()
}

View File

@ -73,7 +73,7 @@ var lineBreakerTests = []lineBreakerTest{
func TestLineBreaker(t *testing.T) {
for i, test := range lineBreakerTests {
buf := bytes.NewBuffer(nil)
buf := new(bytes.Buffer)
var breaker lineBreaker
breaker.out = buf
_, err := breaker.Write([]byte(test.in))
@ -93,7 +93,7 @@ func TestLineBreaker(t *testing.T) {
}
for i, test := range lineBreakerTests {
buf := bytes.NewBuffer(nil)
buf := new(bytes.Buffer)
var breaker lineBreaker
breaker.out = buf

View File

@ -233,8 +233,8 @@ func EscapeString(s string) string {
if strings.IndexAny(s, escapedChars) == -1 {
return s
}
buf := bytes.NewBuffer(nil)
escape(buf, s)
var buf bytes.Buffer
escape(&buf, s)
return buf.String()
}

View File

@ -159,9 +159,9 @@ func dump(n *Node) (string, error) {
if n == nil || len(n.Child) == 0 {
return "", nil
}
b := bytes.NewBuffer(nil)
var b bytes.Buffer
for _, child := range n.Child {
if err := dumpLevel(b, child, 0); err != nil {
if err := dumpLevel(&b, child, 0); err != nil {
return "", err
}
}

View File

@ -77,8 +77,7 @@ func (t Token) tagString() string {
if len(t.Attr) == 0 {
return t.Data
}
buf := bytes.NewBuffer(nil)
buf.WriteString(t.Data)
buf := bytes.NewBufferString(t.Data)
for _, a := range t.Attr {
buf.WriteByte(' ')
buf.WriteString(a.Key)

View File

@ -555,8 +555,8 @@ func TestUnescapeEscape(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"
z := NewTokenizer(bytes.NewBuffer([]byte(s)))
result := bytes.NewBuffer(nil)
z := NewTokenizer(bytes.NewBufferString(s))
var result bytes.Buffer
depth := 0
loop:
for {

View File

@ -238,8 +238,8 @@ func EscapeString(s string) string {
if strings.IndexAny(s, escapedChars) == -1 {
return s
}
buf := bytes.NewBuffer(nil)
escape(buf, s)
var buf bytes.Buffer
escape(&buf, s)
return buf.String()
}

View File

@ -54,14 +54,14 @@ func TestWriter(t *testing.T) {
continue
}
// Encode that image as JPEG.
buf := bytes.NewBuffer(nil)
err = Encode(buf, m0, &Options{Quality: tc.quality})
var buf bytes.Buffer
err = Encode(&buf, m0, &Options{Quality: tc.quality})
if err != nil {
t.Error(tc.filename, err)
continue
}
// Decode that JPEG.
m1, err := Decode(buf)
m1, err := Decode(&buf)
if err != nil {
t.Error(tc.filename, err)
continue

View File

@ -35,12 +35,12 @@ func diff(m0, m1 image.Image) error {
}
func encodeDecode(m image.Image) (image.Image, error) {
b := bytes.NewBuffer(nil)
err := Encode(b, m)
var b bytes.Buffer
err := Encode(&b, m)
if err != nil {
return nil, err
}
m, err = Decode(b)
m, err = Decode(&b)
if err != nil {
return nil, err
}

View File

@ -795,8 +795,8 @@ func TestFormatters(t *testing.T) {
t.Errorf("For %q, expected error, got none.", c.in)
continue
}
buf := bytes.NewBuffer(nil)
err = tmpl.Execute(buf, data)
var buf bytes.Buffer
err = tmpl.Execute(&buf, data)
if err != nil {
t.Error("unexpected Execute error: ", err)
continue