-
Notifications
You must be signed in to change notification settings - Fork 5
/
arena.go
96 lines (80 loc) · 1.73 KB
/
arena.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
package fastrlp
import (
"encoding/binary"
"math/big"
)
// Arena is a pool of RLP values.
type Arena struct {
c cache
}
// Reset resets the values allocated in the arena.
func (a *Arena) Reset() {
a.c.reset()
}
// NewString returns a new string value.
func (a *Arena) NewString(s string) *Value {
return a.NewBytes([]byte(s))
}
// NewBigInt returns a new big.int value.
func (a *Arena) NewBigInt(b *big.Int) *Value {
if b == nil {
return valueNull
}
return a.NewBytes(b.Bytes())
}
// NewCopyBytes returns a bytes value that copies the input.
func (a *Arena) NewCopyBytes(b []byte) *Value {
v := a.c.getValue()
v.t = TypeBytes
v.b = append(v.b[:0], b...)
v.l = uint64(len(b))
return v
}
// NewBytes returns a bytes value.
func (a *Arena) NewBytes(b []byte) *Value {
return a.NewCopyBytes(b)
}
// NewUint returns a new uint value.
func (a *Arena) NewUint(i uint64) *Value {
if i == 0 {
return valueNull
}
intSize := intsize(i)
binary.BigEndian.PutUint64(a.c.buf[:], i)
v := a.c.getValue()
v.t = TypeBytes
v.b = append(v.b[:0], a.c.buf[8-intSize:]...)
v.l = intSize
return v
}
// NewArray returns a new array value.
func (a *Arena) NewArray() *Value {
v := a.c.getValue()
v.t = TypeArray
v.a = v.a[:0]
v.l = 0
return v
}
// NewBool returns a new bool value.
func (a *Arena) NewBool(b bool) *Value {
if b {
return valueTrue
}
return valueFalse
}
// NewTrue returns a true value.
func (a *Arena) NewTrue() *Value {
return valueTrue
}
// NewFalse returns a false value.
func (a *Arena) NewFalse() *Value {
return valueFalse
}
// NewNullArray returns a null array value.
func (a *Arena) NewNullArray() *Value {
return valueArrayNull
}
// NewNull returns a new null value.
func (a *Arena) NewNull() *Value {
return valueNull
}