-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatch.go
213 lines (195 loc) · 4.45 KB
/
match.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (C) 2016 Kohei YOSHIDA. All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of The BSD 3-Clause License
// that can be found in the LICENSE file.
package uritemplate
import (
"bytes"
"unicode"
"unicode/utf8"
)
type matcher struct {
prog *prog
list1 threadList
list2 threadList
matched bool
cap map[string][]int
input string
}
func (m *matcher) at(pos int) (rune, int, bool) {
if l := len(m.input); pos < l {
c := m.input[pos]
if c < utf8.RuneSelf {
return rune(c), 1, pos+1 < l
}
r, size := utf8.DecodeRuneInString(m.input[pos:])
return r, size, pos+size < l
}
return -1, 0, false
}
func (m *matcher) add(list *threadList, pc uint32, pos int, next bool, cap map[string][]int) {
if i := list.sparse[pc]; i < uint32(len(list.dense)) && list.dense[i].pc == pc {
return
}
n := len(list.dense)
list.dense = list.dense[:n+1]
list.sparse[pc] = uint32(n)
e := &list.dense[n]
e.pc = pc
e.t = nil
op := &m.prog.op[pc]
switch op.code {
default:
panic("unhandled opcode")
case opRune, opRuneClass, opEnd:
e.t = &thread{
op: &m.prog.op[pc],
cap: make(map[string][]int, len(m.cap)),
}
for k, v := range cap {
e.t.cap[k] = make([]int, len(v))
copy(e.t.cap[k], v)
}
case opLineBegin:
if pos == 0 {
m.add(list, pc+1, pos, next, cap)
}
case opLineEnd:
if !next {
m.add(list, pc+1, pos, next, cap)
}
case opCapStart, opCapEnd:
ocap := make(map[string][]int, len(m.cap))
for k, v := range cap {
ocap[k] = make([]int, len(v))
copy(ocap[k], v)
}
ocap[op.name] = append(ocap[op.name], pos)
m.add(list, pc+1, pos, next, ocap)
case opSplit:
m.add(list, pc+1, pos, next, cap)
m.add(list, op.i, pos, next, cap)
case opJmp:
m.add(list, op.i, pos, next, cap)
case opJmpIfNotDefined:
m.add(list, pc+1, pos, next, cap)
m.add(list, op.i, pos, next, cap)
case opJmpIfNotFirst:
m.add(list, pc+1, pos, next, cap)
m.add(list, op.i, pos, next, cap)
case opJmpIfNotEmpty:
m.add(list, op.i, pos, next, cap)
m.add(list, pc+1, pos, next, cap)
case opNoop:
m.add(list, pc+1, pos, next, cap)
}
}
func (m *matcher) step(clist *threadList, nlist *threadList, r rune, pos int, nextPos int, next bool) {
debug.Printf("===== %q =====", string(r))
for i := 0; i < len(clist.dense); i++ {
e := clist.dense[i]
if debug {
var buf bytes.Buffer
dumpProg(&buf, m.prog, e.pc)
debug.Printf("\n%s", buf.String())
}
if e.t == nil {
continue
}
t := e.t
op := t.op
switch op.code {
default:
panic("unhandled opcode")
case opRune:
if op.r == r {
m.add(nlist, e.pc+1, nextPos, next, t.cap)
}
case opRuneClass:
ret := false
if !ret && op.rc&runeClassU == runeClassU {
ret = ret || unicode.Is(rangeUnreserved, r)
}
if !ret && op.rc&runeClassR == runeClassR {
ret = ret || unicode.Is(rangeReserved, r)
}
if !ret && op.rc&runeClassPctE == runeClassPctE {
ret = ret || unicode.Is(unicode.ASCII_Hex_Digit, r)
}
if ret {
m.add(nlist, e.pc+1, nextPos, next, t.cap)
}
case opEnd:
m.matched = true
for k, v := range t.cap {
m.cap[k] = make([]int, len(v))
copy(m.cap[k], v)
}
clist.dense = clist.dense[:0]
}
}
clist.dense = clist.dense[:0]
}
func (m *matcher) match() bool {
pos := 0
clist, nlist := &m.list1, &m.list2
for {
if len(clist.dense) == 0 && m.matched {
break
}
r, width, next := m.at(pos)
if !m.matched {
m.add(clist, 0, pos, next, m.cap)
}
m.step(clist, nlist, r, pos, pos+width, next)
if width < 1 {
break
}
pos += width
clist, nlist = nlist, clist
}
return m.matched
}
func (tmpl *Template) Match(expansion string) Values {
tmpl.mu.Lock()
if tmpl.prog == nil {
c := compiler{}
c.init()
c.compile(tmpl)
tmpl.prog = c.prog
}
prog := tmpl.prog
tmpl.mu.Unlock()
n := len(prog.op)
m := matcher{
prog: prog,
list1: threadList{
dense: make([]threadEntry, 0, n),
sparse: make([]uint32, n),
},
list2: threadList{
dense: make([]threadEntry, 0, n),
sparse: make([]uint32, n),
},
cap: make(map[string][]int, prog.numCap),
input: expansion,
}
if !m.match() {
return nil
}
match := make(Values, len(m.cap))
for name, indices := range m.cap {
v := Value{V: make([]string, len(indices)/2)}
for i := range v.V {
v.V[i] = pctDecode(expansion[indices[2*i]:indices[2*i+1]])
}
if len(v.V) == 1 {
v.T = ValueTypeString
} else {
v.T = ValueTypeList
}
match[name] = v
}
return match
}