-
Notifications
You must be signed in to change notification settings - Fork 27
/
re0.go
176 lines (144 loc) · 2.79 KB
/
re0.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package browscap_go
import (
"bytes"
"fmt"
"unicode/utf8"
)
const (
TOKEN_MULTI rune = '*'
TOKEN_SINGLE rune = '?'
MODE_MULTI int = 3
MODE_SINGLE int = 2
MODE_STATIC int = 1
)
type Expression []Token
type Tokens []Token
type Token struct {
match []byte
skip int
multi bool
}
func (t *Token) String() string {
return fmt.Sprintf("%q %d %t", string(t.match), t.skip, t.multi)
}
func (t *Token) Shard() byte {
if len(t.match) > 0 {
return t.match[0]
} else {
return 0
}
}
func (t *Token) Equal(t1 Token) bool {
if !bytes.Equal(t.match, t1.match) {
return false
}
if t.skip != t1.skip {
return false
}
if t.multi != t1.multi {
return false
}
return true
}
func (t *Token) Fuzzy() bool {
return t.multi || t.skip > 0
}
func (t *Token) MatchOne(r []byte) (bool, []byte) {
if t.skip > 0 {
if len(r) < t.skip {
return false, nil
}
r = r[t.skip:]
}
n := len(t.match)
if n == 0 {
return t.multi, nil
} else if len(r) < n {
return false, nil
}
if t.multi {
ind := bytes.Index(r, t.match)
if ind == -1 {
return false, nil
}
return true, r[ind+n:]
}
if !bytes.Equal(r[:n], t.match) {
return false, nil
}
return true, r[n:]
}
type parserState struct {
lastToken Token
lastMode int
exp Expression
}
func (ps *parserState) process(r rune) {
mode := ps.modeByR(r)
if ps.lastToken.match == nil {
ps.lastToken = Token{match: make([]byte, 0, 16)}
}
modMode := false
if mode == MODE_MULTI || mode == MODE_SINGLE {
modMode = true
}
lastModMode := false
if ps.lastMode == MODE_MULTI || ps.lastMode == MODE_SINGLE {
lastModMode = true
}
// changed
if ps.lastMode > 0 && modMode && !lastModMode {
ps.exp = append(ps.exp, ps.lastToken)
ps.lastToken = Token{match: make([]byte, 0, 16)}
}
// update
switch r {
case TOKEN_SINGLE:
ps.lastToken.skip++
case TOKEN_MULTI:
ps.lastToken.multi = true
default:
ps.lastToken.match = appendRune(ps.lastToken.match, r)
}
ps.lastMode = mode
}
func (ps *parserState) modeByR(r rune) int {
if r == TOKEN_MULTI {
return MODE_MULTI
} else if r == TOKEN_SINGLE {
return MODE_SINGLE
}
return MODE_STATIC
}
func (ps *parserState) last() {
// save prev token
if ps.lastToken.match != nil {
ps.exp = append(ps.exp, ps.lastToken)
}
}
func CompileExpression(s []byte) Expression {
state := &parserState{
exp: Expression{},
lastMode: -1,
}
reader := bytes.NewReader(s)
for {
r, _, err := reader.ReadRune()
if err != nil {
break
}
state.process(r)
}
state.last()
return state.exp
}
func appendRune(b []byte, r rune) []byte {
if r < utf8.RuneSelf {
return append(b, byte(r))
}
rb := make([]byte, utf8.UTFMax)
n := utf8.EncodeRune(rb, r)
return append(b, rb[0:n]...)
}