forked from vmihailenco/msgpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyteslice_reader.go
More file actions
46 lines (40 loc) · 932 Bytes
/
byteslice_reader.go
File metadata and controls
46 lines (40 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package msgpack
import (
"errors"
"io"
)
// byteSliceReader implements bufReader (io.Reader + io.ByteScanner) for
// zero-allocation decoding from a []byte. When Unmarshal is called with
// a complete byte slice, this avoids the overhead of bytes.NewReader +
// bufio.NewReader (2 allocations + 4KB buffer + interface dispatch per byte).
type byteSliceReader struct {
data []byte
pos int
}
func (r *byteSliceReader) reset(data []byte) {
r.data = data
r.pos = 0
}
func (r *byteSliceReader) ReadByte() (byte, error) {
if r.pos >= len(r.data) {
return 0, io.EOF
}
c := r.data[r.pos]
r.pos++
return c, nil
}
func (r *byteSliceReader) UnreadByte() error {
if r.pos <= 0 {
return errors.New("msgpack: at beginning of input")
}
r.pos--
return nil
}
func (r *byteSliceReader) Read(p []byte) (int, error) {
if r.pos >= len(r.data) {
return 0, io.EOF
}
n := copy(p, r.data[r.pos:])
r.pos += n
return n, nil
}