-
Notifications
You must be signed in to change notification settings - Fork 2
/
scratch.go
70 lines (55 loc) · 1.3 KB
/
scratch.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
package jsparser
import "unicode/utf8"
// based on https://github.com/bcicen/jstream
type scratch struct {
data []byte
dataRes []*JSON
fill int
fillRes int
}
// reset scratch buffer
func (s *scratch) reset() { s.fill = 0 }
// bytes returns the written contents of scratch buffer
func (s *scratch) bytes() []byte { return s.data[0:s.fill] }
// string returns the written contents of scratch buffer
func (s *scratch) string() string { return string(s.data[0:s.fill]) }
// grow scratch buffer
func (s *scratch) grow() {
ndata := make([]byte, cap(s.data)*2)
copy(ndata, s.data[:])
s.data = ndata
}
// append single byte to scratch buffer
func (s *scratch) add(c byte) {
if s.fill+1 >= cap(s.data) {
s.grow()
}
s.data[s.fill] = c
s.fill++
}
// append encoded rune to scratch buffer
func (s *scratch) addRune(r rune) int {
if s.fill+utf8.UTFMax >= cap(s.data) {
s.grow()
}
n := utf8.EncodeRune(s.data[s.fill:], r)
s.fill += n
return n
}
// grow result buffer
func (s *scratch) growRes() {
ndata := make([]*JSON, cap(s.dataRes)*2)
copy(ndata, s.dataRes[:])
s.dataRes = ndata
}
// add result
func (s *scratch) addRes(res *JSON) {
if s.fillRes+1 >= cap(s.dataRes) {
s.growRes()
}
s.dataRes[s.fillRes] = res
s.fillRes++
}
func (s *scratch) allRes() []*JSON {
return s.dataRes[0:s.fillRes]
}