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