-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyteparse.go
More file actions
93 lines (73 loc) · 1.29 KB
/
Copy pathbyteparse.go
File metadata and controls
93 lines (73 loc) · 1.29 KB
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
package parser
import "unicode/utf8"
type byteParser struct {
data []byte
pos, width int
}
func (p *byteParser) next() rune {
if p.pos == len(p.data) {
p.width = 0
return -1
}
r, s := utf8.DecodeRune(p.data[p.pos:])
if r == utf8.RuneError && s == 1 {
r = rune(p.data[p.pos])
}
p.pos += s
p.width = s
return r
}
func (p *byteParser) backup() {
if p.width > 0 {
p.pos -= p.width
p.width = 0
}
}
func (p *byteParser) get() string {
s := p.data[:p.pos]
p.data = p.data[p.pos:]
p.pos = 0
p.width = 0
return string(s)
}
func (p *byteParser) length() int {
return p.pos
}
func (p *byteParser) reset() {
p.pos = 0
p.width = 0
}
func (p *byteParser) sub() tokeniser {
return &sub{
tokeniser: p,
tState: len(p.data),
start: p.pos,
}
}
func (p *byteParser) slice(state, start int) (string, int) {
if len(p.data) != state || start > p.pos {
return "", -1
}
return string(p.data[start:p.pos]), p.pos
}
type byteState struct {
b *byteParser
stateID int
pos, width int
}
func (p *byteParser) state() State {
return &byteState{
b: p,
stateID: len(p.data),
pos: p.pos,
width: p.width,
}
}
func (b *byteState) Reset() bool {
if len(b.b.data) != b.stateID {
return false
}
b.b.pos = b.pos
b.b.width = b.width
return true
}