-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
94 lines (75 loc) · 1.71 KB
/
cursor.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cursor
import (
"encoding/binary"
)
// Cursor is a byte-slice cursor.
type Cursor struct {
cursor int
buf []byte
defaultBitSize int
order binary.ByteOrder
}
// NewCursor creates new Cursor.
func NewCursor(buf []byte) *Cursor {
return &Cursor{buf: buf, order: binary.LittleEndian, defaultBitSize: 8}
}
// Sets default bit size of length word.
func (c *Cursor) LengthBitSize(i int) {
c.defaultBitSize = i
}
// Sets byte order.
func (c *Cursor) Order(order binary.ByteOrder) {
c.order = order
}
// Increase buffer size.
func (c *Cursor) Grow(length int) {
c.buf = AppendSize(c.buf, length)
}
// Sets buffer size to zero, moves cursor to zero index
func (c *Cursor) Reset() {
c.buf = c.buf[:0]
c.cursor = 0
}
// Move sets cursor position.
// Warning: user should reallocate buffer manually.
func (c *Cursor) Move(index int) {
c.cursor = index
}
// Index returns current position of cursor.
func (c *Cursor) Index() int {
return c.cursor
}
// Returns length of buffer.
func (c *Cursor) Len() int {
return len(c.buf)
}
// Returns capacity of buffer.
func (c *Cursor) Cap() int {
return cap(c.buf)
}
// Buffer returns current buffer.
func (c *Cursor) Buffer() []byte {
return c.buf
}
// SetBuffer sets current buffer.
func (c *Cursor) SetBuffer(buf []byte) {
c.buf = buf
}
func (c *Cursor) checkRange(i int) bool {
return i >= 0 && i < len(c.buf)
}
func (c *Cursor) Sub(from, to int) (*Cursor, bool) {
if from <= to && c.checkRange(from) && c.checkRange(to-1) {
return NewCursor(c.buf[from:to]), true
}
return nil, false
}
func (c *Cursor) need(length int) {
c.Grow(length)
}
func (c *Cursor) should(length int) error {
if c.cursor+length > len(c.buf) {
return ErrInvalidLength
}
return nil
}