forked from bcicen/jstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch.go
44 lines (35 loc) · 761 Bytes
/
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
package scratch
import (
"unicode/utf8"
)
type Scratch struct {
Data []byte
fill 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] }
// 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
}