parser - byteparse.go
1 package parser
2
3 import "unicode/utf8"
4
5 type byteParser struct {
6 data []byte
7 pos, width int
8 }
9
10 func (p *byteParser) next() rune {
11 if p.pos == len(p.data) {
12 p.width = 0
13 return -1
14 }
15 r, s := utf8.DecodeRune(p.data[p.pos:])
16 if r == utf8.RuneError && s == 1 {
17 r = rune(p.data[p.pos])
18 }
19 p.pos += s
20 p.width = s
21 return r
22 }
23
24 func (p *byteParser) backup() {
25 if p.width > 0 {
26 p.pos -= p.width
27 p.width = 0
28 }
29 }
30
31 func (p *byteParser) get() string {
32 s := p.data[:p.pos]
33 p.data = p.data[p.pos:]
34 p.pos = 0
35 p.width = 0
36 return string(s)
37 }
38
39 func (p *byteParser) length() int {
40 return p.pos
41 }