/
bytes.go
357 lines (285 loc) · 9.45 KB
/
bytes.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package core
import (
"errors"
"fmt"
"slices"
"strconv"
"sync"
"github.com/inoxlang/inox/internal/mimeconsts"
"github.com/inoxlang/inox/internal/utils"
)
var (
ErrAttemptToMutateReadonlyByteSlice = errors.New("attempt to write a readonly byte slice")
ErrAttemptToCreateMutableSpecificTypeByteSlice = errors.New("attempt to create a mutable byte slice with specific content type")
_ = []WrappedBytes{(*ByteSlice)(nil)}
_ = []BytesLike{(*ByteSlice)(nil), (*BytesConcatenation)(nil)}
)
// A WrappedBytes represents a value that wraps a byte slice ( []byte ).
type WrappedBytes interface {
Readable
//the returned bytes should NOT be modified
UnderlyingBytes() []byte
}
// A BytesLike represents an abstract byte slice, it should behave exactly like a regular ByteSlice and have the same pseudo properties.
type BytesLike interface {
MutableSequence
Iterable
GetOrBuildBytes() *ByteSlice
Mutable() bool
}
// ByteSlice implements Value, its mutability is set at creation.
type ByteSlice struct {
bytes []byte
isDataMutable bool
contentType Mimetype
constraintId ConstraintId
// exclusive access for initializing .watchers & .mutationCallbacks only.
lock sync.Mutex
watchers *ValueWatchers
mutationCallbacks *MutationCallbacks
}
func NewByteSlice(bytes []byte, mutable bool, contentType Mimetype) *ByteSlice {
//TODO: check content type
if contentType != "" && mutable {
panic(ErrAttemptToCreateMutableSpecificTypeByteSlice)
}
return &ByteSlice{bytes: bytes, isDataMutable: mutable, contentType: contentType}
}
func NewMutableByteSlice(bytes []byte, contentType Mimetype) *ByteSlice {
return NewByteSlice(bytes, true, contentType)
}
func NewImmutableByteSlice(bytes []byte, contentType Mimetype) *ByteSlice {
return NewByteSlice(bytes, false, contentType)
}
func (slice *ByteSlice) UnderlyingBytes() []byte {
return slice.bytes
}
func (slice *ByteSlice) UnsafeBytesAsString() string {
return utils.BytesAsString(slice.UnderlyingBytes())
}
func (slice *ByteSlice) GetOrBuildBytes() *ByteSlice {
return slice
}
func (slice *ByteSlice) Mutable() bool {
return slice.isDataMutable
}
// ContentType returns the content type specified at creation.
// If no content type was specified mimeconsts.APP_OCTET_STREAM_CTYPE is returned instead.
func (slice *ByteSlice) ContentType() Mimetype {
if slice.contentType == "" {
return mimeconsts.APP_OCTET_STREAM_CTYPE
}
return slice.contentType
}
func (slice *ByteSlice) slice(start, end int) Sequence {
sliceCopy := make([]byte, end-start)
copy(sliceCopy, slice.bytes[start:end])
return &ByteSlice{bytes: sliceCopy, isDataMutable: slice.isDataMutable}
}
func (slice *ByteSlice) Len() int {
return len(slice.bytes)
}
func (slice *ByteSlice) At(ctx *Context, i int) Value {
return Byte(slice.bytes[i])
}
func (slice *ByteSlice) set(ctx *Context, i int, v Value) {
if !slice.isDataMutable {
panic(ErrAttemptToMutateReadonlyByteSlice)
}
slice.bytes[i] = byte(v.(Byte))
mutation := NewSetElemAtIndexMutation(ctx, i, v.(Byte), ShallowWatching, Path("/"+strconv.Itoa(i)))
slice.mutationCallbacks.CallMicrotasks(ctx, mutation)
slice.watchers.InformAboutAsync(ctx, mutation, ShallowWatching, true)
}
func (slice *ByteSlice) SetSlice(ctx *Context, start, end int, seq Sequence) {
if !slice.isDataMutable {
panic(ErrAttemptToMutateReadonlyByteSlice)
}
if seq.Len() != end-start {
panic(errors.New(FormatIndexableShouldHaveLen(end - start)))
}
for i := start; i < end; i++ {
slice.bytes[i] = byte(seq.At(ctx, i-start).(Byte))
}
path := Path("/" + strconv.Itoa(int(start)) + ".." + strconv.Itoa(int(end-1)))
mutation := NewSetSliceAtRangeMutation(ctx, NewIntRange(int64(start), int64(end-1)), seq.(Serializable), ShallowWatching, path)
slice.mutationCallbacks.CallMicrotasks(ctx, mutation)
slice.watchers.InformAboutAsync(ctx, mutation, ShallowWatching, true)
}
func (s *ByteSlice) insertElement(ctx *Context, v Value, i Int) {
if !s.isDataMutable {
panic(ErrAttemptToMutateReadonlyByteSlice)
}
b := v.(Byte)
s.bytes = append(s.bytes, 0)
copy(s.bytes[i+1:], s.bytes[i:len(s.bytes)-1])
s.bytes[i] = byte(b)
mutation := NewInsertElemAtIndexMutation(ctx, int(i), b, ShallowWatching, Path("/"+strconv.Itoa(int(i))))
s.mutationCallbacks.CallMicrotasks(ctx, mutation)
s.watchers.InformAboutAsync(ctx, mutation, ShallowWatching, true)
}
func (s *ByteSlice) removePosition(ctx *Context, i Int) {
if !s.isDataMutable {
panic(ErrAttemptToMutateReadonlyByteSlice)
}
if int(i) > len(s.bytes) || i < 0 {
panic(ErrIndexOutOfRange)
}
if int(i) == len(s.bytes)-1 { // remove last position
s.bytes = s.bytes[:len(s.bytes)-1]
} else {
copy(s.bytes[i:], s.bytes[i+1:])
s.bytes = s.bytes[:len(s.bytes)-1]
}
mutation := NewRemovePositionMutation(ctx, int(i), ShallowWatching, Path("/"+strconv.Itoa(int(i))))
s.mutationCallbacks.CallMicrotasks(ctx, mutation)
s.watchers.InformAboutAsync(ctx, mutation, ShallowWatching, true)
}
func (s *ByteSlice) removePositionRange(ctx *Context, r IntRange) {
if !s.isDataMutable {
panic(ErrAttemptToMutateReadonlyByteSlice)
}
start := int(r.KnownStart())
end := int(r.InclusiveEnd())
if start > len(s.bytes) || start < 0 || end >= len(s.bytes) || end < 0 {
panic(ErrIndexOutOfRange)
}
if end == len(s.bytes)-1 { // remove trailing sub slice
s.bytes = s.bytes[:len(s.bytes)-r.Len()]
} else {
copy(s.bytes[start:], s.bytes[end+1:])
s.bytes = s.bytes[:len(s.bytes)-r.Len()]
}
path := Path("/" + strconv.Itoa(int(r.KnownStart())) + ".." + strconv.Itoa(int(r.InclusiveEnd())))
mutation := NewRemovePositionRangeMutation(ctx, r, ShallowWatching, path)
s.mutationCallbacks.CallMicrotasks(ctx, mutation)
s.watchers.InformAboutAsync(ctx, mutation, ShallowWatching, true)
}
func (s *ByteSlice) insertSequence(ctx *Context, seq Sequence, i Int) {
if !s.isDataMutable {
panic(ErrAttemptToMutateReadonlyByteSlice)
}
// TODO: lock sequence
seqLen := seq.Len()
if seqLen == 0 {
return
}
if cap(s.bytes)-len(s.bytes) < seqLen {
newSlice := make([]byte, len(s.bytes)+seqLen)
copy(newSlice, s.bytes)
s.bytes = newSlice
} else {
s.bytes = s.bytes[:len(s.bytes)+seqLen]
}
copy(s.bytes[int(i)+seqLen:], s.bytes[i:])
for ind := 0; ind < seqLen; ind++ {
s.bytes[int(i)+ind] = byte(seq.At(ctx, ind).(Byte))
}
path := Path("/" + strconv.Itoa(int(i)))
mutation := NewInsertSequenceAtIndexMutation(ctx, int(i), seq, ShallowWatching, path)
s.mutationCallbacks.CallMicrotasks(ctx, mutation)
s.watchers.InformAboutAsync(ctx, mutation, ShallowWatching, true)
}
func (s *ByteSlice) appendSequence(ctx *Context, seq Sequence) {
s.insertSequence(ctx, seq, Int(s.Len()))
}
// Byte implements Value.
type Byte byte
func (b Byte) Int64() int64 {
return int64(b)
}
func (b Byte) IsSigned() bool {
return false
}
// BytesConcatenation is a lazy concatenation of values that can form a byte slice, BytesConcatenation implements BytesLike.
type BytesConcatenation struct {
elements []BytesLike
totalLen int
finalBytes []byte // empty by default
}
func ConcatBytesLikes(bytesLikes ...BytesLike) (BytesLike, error) {
//TODO: concatenate small sequences together
totalLen := 0
for i, bytesLike := range bytesLikes {
if bytesLike.Mutable() {
b := slices.Clone(bytesLike.GetOrBuildBytes().bytes) // TODO: use Copy On Write
bytesLikes[i] = NewByteSlice(b, false, "")
}
totalLen += bytesLike.Len()
}
if len(bytesLikes) == 1 {
return bytesLikes[0], nil
}
return &BytesConcatenation{
elements: slices.Clone(bytesLikes),
totalLen: totalLen,
}, nil
}
func NewBytesConcatenation(bytesLikes ...BytesLike) *BytesConcatenation {
if len(bytesLikes) < 2 {
panic(errors.New("not enough elements"))
}
var totalLen int
for i, bytesLike := range bytesLikes {
if bytesLike.Mutable() {
b := slices.Clone(bytesLike.GetOrBuildBytes().bytes) // TODO: use Copy On Write
bytesLikes[i] = NewByteSlice(b, false, "")
}
totalLen += bytesLike.Len()
}
return &BytesConcatenation{
elements: slices.Clone(bytesLikes),
totalLen: totalLen,
}
}
func (c *BytesConcatenation) GetOrBuildBytes() *ByteSlice {
if c.Len() > 0 && len(c.finalBytes) == 0 {
slice := make([]byte, c.totalLen)
pos := 0
for _, elem := range c.elements {
copy(slice[pos:pos+elem.Len()], elem.GetOrBuildBytes().bytes)
pos += elem.Len()
}
c.finalBytes = slice
//get rid of elements to allow garbage collection ?
}
return &ByteSlice{
bytes: c.finalBytes,
isDataMutable: false,
}
}
func (c *BytesConcatenation) Mutable() bool {
return c.elements[0].Mutable()
}
func (c *BytesConcatenation) set(ctx *Context, i int, v Value) {
if !c.Mutable() {
panic(fmt.Errorf("attempt to write a readonly bytes concatenation"))
}
panic(fmt.Errorf("cannot mutate bytes concatenation %w", ErrNotImplementedYet))
}
func (c *BytesConcatenation) SetSlice(ctx *Context, start, end int, seq Sequence) {
if !c.Mutable() {
panic(fmt.Errorf("attempt to write a readonly bytes concatenation"))
}
panic(fmt.Errorf("cannot mutate bytes concatenation %w", ErrNotImplementedYet))
}
func (c *BytesConcatenation) Len() int {
return c.totalLen
}
func (c *BytesConcatenation) At(ctx *Context, i int) Value {
elementIndex := 0
pos := 0
for pos < i {
element := c.elements[elementIndex]
if pos+element.Len() >= i {
return element.At(ctx, i-pos)
}
elementIndex++
pos += element.Len()
}
panic(ErrIndexOutOfRange)
}
func (c *BytesConcatenation) slice(start, end int) Sequence {
//TODO: change implementation + make the function stoppable for large slices
return c.GetOrBuildBytes().slice(start, end)
}