Replace godep with dep

This commit is contained in:
Manuel de Brito Fontes 2017-10-06 17:26:14 -03:00
parent 1e7489927c
commit bf5616c65b
14883 changed files with 3937406 additions and 361781 deletions

View file

@ -179,18 +179,25 @@ func (b *Buffer) DumpTo(w io.Writer) (written int, err error) {
}
// BuildBytes creates a single byte slice with all the contents of the buffer. Data is
// copied if it does not fit in a single chunk.
func (b *Buffer) BuildBytes() []byte {
// copied if it does not fit in a single chunk. You can optionally provide one byte
// slice as argument that it will try to reuse.
func (b *Buffer) BuildBytes(reuse ...[]byte) []byte {
if len(b.bufs) == 0 {
ret := b.Buf
b.toPool = nil
b.Buf = nil
return ret
}
ret := make([]byte, 0, b.Size())
var ret []byte
size := b.Size()
// If we got a buffer as argument and it is big enought, reuse it.
if len(reuse) == 1 && cap(reuse[0]) >= size {
ret = reuse[0][:0]
} else {
ret = make([]byte, 0, size)
}
for _, buf := range b.bufs {
ret = append(ret, buf...)
putBuf(buf)
@ -205,3 +212,59 @@ func (b *Buffer) BuildBytes() []byte {
return ret
}
type readCloser struct {
offset int
bufs [][]byte
}
func (r *readCloser) Read(p []byte) (n int, err error) {
for _, buf := range r.bufs {
// Copy as much as we can.
x := copy(p[n:], buf[r.offset:])
n += x // Increment how much we filled.
// Did we empty the whole buffer?
if r.offset+x == len(buf) {
// On to the next buffer.
r.offset = 0
r.bufs = r.bufs[1:]
// We can release this buffer.
putBuf(buf)
} else {
r.offset += x
}
if n == len(p) {
break
}
}
// No buffers left or nothing read?
if len(r.bufs) == 0 {
err = io.EOF
}
return
}
func (r *readCloser) Close() error {
// Release all remaining buffers.
for _, buf := range r.bufs {
putBuf(buf)
}
// In case Close gets called multiple times.
r.bufs = nil
return nil
}
// ReadCloser creates an io.ReadCloser with all the contents of the buffer.
func (b *Buffer) ReadCloser() io.ReadCloser {
ret := &readCloser{0, append(b.bufs, b.Buf)}
b.bufs = nil
b.toPool = nil
b.Buf = nil
return ret
}

107
vendor/github.com/mailru/easyjson/buffer/pool_test.go generated vendored Normal file
View file

@ -0,0 +1,107 @@
package buffer
import (
"bytes"
"testing"
)
func TestAppendByte(t *testing.T) {
var b Buffer
var want []byte
for i := 0; i < 1000; i++ {
b.AppendByte(1)
b.AppendByte(2)
want = append(want, 1, 2)
}
got := b.BuildBytes()
if !bytes.Equal(got, want) {
t.Errorf("BuildBytes() = %v; want %v", got, want)
}
}
func TestAppendBytes(t *testing.T) {
var b Buffer
var want []byte
for i := 0; i < 1000; i++ {
b.AppendBytes([]byte{1, 2})
want = append(want, 1, 2)
}
got := b.BuildBytes()
if !bytes.Equal(got, want) {
t.Errorf("BuildBytes() = %v; want %v", got, want)
}
}
func TestAppendString(t *testing.T) {
var b Buffer
var want []byte
s := "test"
for i := 0; i < 1000; i++ {
b.AppendBytes([]byte(s))
want = append(want, s...)
}
got := b.BuildBytes()
if !bytes.Equal(got, want) {
t.Errorf("BuildBytes() = %v; want %v", got, want)
}
}
func TestDumpTo(t *testing.T) {
var b Buffer
var want []byte
s := "test"
for i := 0; i < 1000; i++ {
b.AppendBytes([]byte(s))
want = append(want, s...)
}
out := &bytes.Buffer{}
n, err := b.DumpTo(out)
if err != nil {
t.Errorf("DumpTo() error: %v", err)
}
got := out.Bytes()
if !bytes.Equal(got, want) {
t.Errorf("DumpTo(): got %v; want %v", got, want)
}
if n != len(want) {
t.Errorf("DumpTo() = %v; want %v", n, len(want))
}
}
func TestReadCloser(t *testing.T) {
var b Buffer
var want []byte
s := "test"
for i := 0; i < 1000; i++ {
b.AppendBytes([]byte(s))
want = append(want, s...)
}
out := &bytes.Buffer{}
rc := b.ReadCloser()
n, err := out.ReadFrom(rc)
if err != nil {
t.Errorf("ReadCloser() error: %v", err)
}
rc.Close() // Will always return nil
got := out.Bytes()
if !bytes.Equal(got, want) {
t.Errorf("DumpTo(): got %v; want %v", got, want)
}
if n != int64(len(want)) {
t.Errorf("DumpTo() = %v; want %v", n, len(want))
}
}