-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
182 lines (142 loc) · 3.23 KB
/
buffer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package jq
import (
"bytes"
"nikand.dev/go/cbor"
)
type (
// Buffer stores internal state of decoded data.
// Filters manipulate data in the Buffer.
// There are BufferReader and BufferWriter types to separate methods related to reading from and writing to the buffer.
Buffer struct {
B []byte
Decoder Decoder
Encoder Encoder
Vars []Variable
Static []Off
arr []Off
Flags BufferFlags
}
// BufferReader is a thin structure on top of Buffer to logically separate reading methods.
// Buffer is indended to store correct data which is generated shortly before that.
// So most of the methods don't check data type or validity.
// It's the callers responsibility to ensure they are correct.
BufferReader struct {
*Buffer
}
BufferWriter struct {
*Buffer
}
BufferFlags int
)
const (
BufferStatic BufferFlags = 1 << iota
BufferDefault = BufferStatic
)
var (
shortToCBOR = []Tag{
-None: cbor.Simple | cbor.None,
-Null: cbor.Simple | cbor.Null,
-True: cbor.Simple | cbor.True,
-False: cbor.Simple | cbor.False,
-Zero: cbor.Int | 0,
-One: cbor.Int | 1,
-EmptyString: cbor.String | 0,
-EmptyArray: cbor.Array | 0,
}
cborToShort = []Off{
cbor.Simple | cbor.Null: Null,
cbor.Simple | cbor.True: True,
cbor.Simple | cbor.False: False,
cbor.Int | 0: Zero,
cbor.Int | 1: One,
cbor.String | 0: EmptyString,
cbor.Array | 0: EmptyArray,
}
)
func NewBuffer() *Buffer {
b := MakeBuffer()
return &b
}
func NewBufferFrom(buf []byte) *Buffer {
b := MakeBufferFrom(buf)
return &b
}
func MakeBuffer() Buffer {
return Buffer{
Encoder: MakeEncoder(),
Decoder: MakeDecoder(),
Flags: BufferDefault,
}
}
func MakeBufferFrom(buf []byte) Buffer {
return Buffer{
B: buf[:0],
Encoder: MakeEncoder(),
Decoder: MakeDecoder(),
Flags: BufferDefault,
}
}
func (b *Buffer) Reset() {
b.B = b.B[:0]
b.Vars = b.Vars[:0]
b.Static = b.Static[:0]
}
func (b *Buffer) Reader() BufferReader { return BufferReader{b} }
func (b *Buffer) Writer() BufferWriter { return BufferWriter{b} }
func (b *Buffer) Equal(loff, roff Off) (res bool) {
br := b.Reader()
// log.Printf("equal %x %x\n%s", loff, roff, DumpBuffer(b))
// defer func() { log.Printf("equal %x %x => %v", loff, roff, res) }()
if loff == roff {
return true
}
tag := br.Tag(loff)
rtag := br.Tag(roff)
if tag != rtag {
return false
}
switch tag {
case cbor.Int, cbor.Neg, cbor.Bytes, cbor.String, cbor.Simple, cbor.Labeled:
lraw := br.Raw(loff)
rraw := br.Raw(roff)
return bytes.Equal(lraw, rraw)
case cbor.Array, cbor.Map:
default:
panic(tag)
}
larr := br.ArrayMap(loff, nil)
rarr := br.ArrayMap(roff, nil)
if len(larr) != len(rarr) {
return false
}
for i := range larr {
if !b.Equal(larr[i], rarr[i]) {
return false
}
}
return true
}
func (b *Buffer) Len() int {
if b == nil {
return 0
}
return len(b.B)
}
func (b *Buffer) Buf(off Off) ([]byte, int) {
return b.B, int(off)
}
func (b *Buffer) Unwrap() []byte {
return b.B
}
func (b *Buffer) Dump() string {
return NewDumper(nil).Dump(b)
}
func (f BufferFlags) Is(x BufferFlags) bool {
return f&x == x
}
func (f *BufferFlags) Set(x BufferFlags) {
*f |= x
}
func (f *BufferFlags) Unset(x BufferFlags) {
*f &^= x
}