forked from goplus/bpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_array.go
276 lines (185 loc) · 5.14 KB
/
base_array.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package bpl
import (
"bufio"
"io"
"reflect"
"unsafe"
"qiniupkg.com/x/bufiox.v7"
)
// -----------------------------------------------------------------------------
func matchCharArray(n int, in *bufio.Reader, ctx *Context) (v interface{}, err error) {
if n == 0 {
return "", nil
}
b := make([]byte, n)
_, err = io.ReadFull(in, b)
if err != nil {
return
}
return string(b), nil
}
func matchByteArray(n int, in *bufio.Reader, ctx *Context) (v interface{}, err error) {
if n == 0 {
return []byte(nil), nil
}
b := make([]byte, n)
_, err = io.ReadFull(in, b)
if err != nil {
return
}
return b, nil
}
func matchBaseArray(R BaseType, n int, in *bufio.Reader, ctx *Context) (v interface{}, err error) {
if n == 0 {
return
}
t := baseTypes[R]
v = t.newn(n)
data := (*reflect.SliceHeader)(unsafe.Pointer(reflect.ValueOf(v).UnsafeAddr())).Data
b := (*[1 << 30]byte)(unsafe.Pointer(data))
_, err = io.ReadFull(in, b[:n*t.sizeOf])
return
}
// -----------------------------------------------------------------------------
type baseArray struct {
r BaseType
n int
}
func (p *baseArray) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
n := p.n
return matchBaseArray(p.r, n, in, ctx)
}
func (p *baseArray) RetType() reflect.Type {
return reflect.SliceOf(p.r.RetType())
}
func (p *baseArray) SizeOf() int {
return p.r.SizeOf() * p.n
}
// BaseArray returns a matching unit that matches R n times.
//
func BaseArray(r BaseType, n int) Ruler {
return &baseArray{r: r, n: n}
}
// -----------------------------------------------------------------------------
type baseDynarray struct {
r BaseType
n func(ctx *Context) int
}
func (p *baseDynarray) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
n := p.n(ctx)
return matchBaseArray(p.r, n, in, ctx)
}
func (p *baseDynarray) RetType() reflect.Type {
return reflect.SliceOf(p.r.RetType())
}
func (p *baseDynarray) SizeOf() int {
return -1
}
// BaseDynarray returns a matching unit that matches R n(ctx) times.
//
func BaseDynarray(r BaseType, n func(ctx *Context) int) Ruler {
return &baseDynarray{r: r, n: n}
}
// -----------------------------------------------------------------------------
type byteArray0 int
func (p byteArray0) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
v, err = bufiox.ReadAll(in)
return
}
func (p byteArray0) RetType() reflect.Type {
return tyByteSlice
}
func (p byteArray0) SizeOf() int {
return -1
}
// ByteArray0 is a matching unit that matches `*byte`.
//
var ByteArray0 Ruler = byteArray0(0)
// -----------------------------------------------------------------------------
type byteArray1 int
func (p byteArray1) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
ret, err := bufiox.ReadAll(in)
if err != nil {
return
}
if len(ret) == 0 {
panic("match +byte failed: EOF encountered")
}
return ret, nil
}
func (p byteArray1) RetType() reflect.Type {
return tyByteSlice
}
func (p byteArray1) SizeOf() int {
return -1
}
// ByteArray1 is a matching unit that matches `+byte`.
//
var ByteArray1 Ruler = byteArray1(0)
// -----------------------------------------------------------------------------
type byteArray int
func (p byteArray) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
return matchByteArray(int(p), in, ctx)
}
func (p byteArray) RetType() reflect.Type {
return tyByteSlice
}
func (p byteArray) SizeOf() int {
return int(p)
}
// ByteArray returns a matching unit that matches `[n]byte`.
//
func ByteArray(n int) Ruler {
return byteArray(n)
}
// -----------------------------------------------------------------------------
type charArray int
func (p charArray) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
return matchCharArray(int(p), in, ctx)
}
func (p charArray) RetType() reflect.Type {
return tyString
}
func (p charArray) SizeOf() int {
return int(p)
}
// CharArray returns a matching unit that matches `[n]char`.
//
func CharArray(n int) Ruler {
return charArray(n)
}
// -----------------------------------------------------------------------------
type byteDynarray func(ctx *Context) int
func (p byteDynarray) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
n := p(ctx)
return matchByteArray(n, in, ctx)
}
func (p byteDynarray) RetType() reflect.Type {
return tyByteSlice
}
func (p byteDynarray) SizeOf() int {
return -1
}
// ByteDynarray returns a matching unit that matches `[n(ctx)]byte`.
//
func ByteDynarray(n func(ctx *Context) int) Ruler {
return byteDynarray(n)
}
// -----------------------------------------------------------------------------
type charDynarray func(ctx *Context) int
func (p charDynarray) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
n := p(ctx)
return matchCharArray(n, in, ctx)
}
func (p charDynarray) RetType() reflect.Type {
return tyString
}
func (p charDynarray) SizeOf() int {
return -1
}
// CharDynarray returns a matching unit that matches `[n(ctx)]char`.
//
func CharDynarray(n func(ctx *Context) int) Ruler {
return charDynarray(n)
}
// -----------------------------------------------------------------------------