-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
pattern.go
271 lines (262 loc) · 5.13 KB
/
pattern.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package res
// Pattern is a resource pattern that may contain wildcards and tags.
//
// Pattern("example.resource.>") // Full wild card (>) matches anything that follows
// Pattern("example.item.*") // Wild card (*) matches a single part
// Pattern("example.model.$id") // Tag (starting with $) matches a single part
type Pattern string
// IsValid returns true if the pattern is valid, otherwise false.
func (p Pattern) IsValid() bool {
if len(p) == 0 {
return true
}
start := true
alone := false
emptytag := false
for i, c := range p {
if c == '.' {
if start || emptytag {
return false
}
alone = false
start = true
} else {
if alone || c < 33 || c > 126 || c == '?' {
return false
}
switch c {
case '>':
if !start || i < len(p)-1 {
return false
}
case '*':
if !start {
return false
}
alone = true
case '$':
if start {
emptytag = true
}
default:
emptytag = false
}
start = false
}
}
return !(start || emptytag)
}
// Matches tests if the resource name, s, matches the pattern.
//
// The resource name might in itself contain wild cards and tags.
//
// Behavior is undefined for an invalid pattern or an invalid resource name.
func (p Pattern) Matches(s string) bool {
pi := 0
si := 0
pl := len(p)
sl := len(s)
for pi < pl {
if si == sl {
return false
}
c := p[pi]
pi++
switch c {
case '$':
fallthrough
case '*':
for pi < pl && p[pi] != '.' {
pi++
}
if s[si] == '>' {
return false
}
for si < sl && s[si] != '.' {
si++
}
case '>':
return pi == pl
default:
if c != s[si] {
return false
}
si++
}
}
return si == sl
}
// ReplaceTags searches for tags and replaces them with
// the map value for the key matching the tag (without $).
//
// Behavior is undefined for an invalid pattern.
func (p Pattern) ReplaceTags(m map[string]string) Pattern {
// Quick exit on empty map
if len(m) == 0 {
return p
}
return p.replace(func(t string) (string, bool) {
v, ok := m[t]
return v, ok
})
}
// ReplaceTag searches for a given tag (without $) and replaces
// it with the value.
//
// Behavior is undefined for an invalid pattern.
func (p Pattern) ReplaceTag(tag string, value string) Pattern {
return p.replace(func(t string) (string, bool) {
if tag == t {
return value, true
}
return "", false
})
}
// replace replaces tags with a value.
func (p Pattern) replace(replacer func(tag string) (string, bool)) Pattern {
type rep struct {
o int // tag offset (including $)
e int // tag end
v string // replace value
}
var rs []rep
pi := 0
pl := len(p)
start := true
var o int
for pi < pl {
c := p[pi]
pi++
switch c {
case '$':
if start {
// Temporarily store tag start offset
o = pi
// Find end of tag
for pi < pl && p[pi] != '.' {
pi++
}
// Get the replacement value from the replacer callback.
if v, ok := replacer(string(p[o:pi])); ok {
rs = append(rs, rep{o: o - 1, e: pi, v: v})
}
}
case '.':
start = true
default:
start = false
}
}
// Quick exit on no replacements
if len(rs) == 0 {
return p
}
// Calculate length, nl, of resulting string
nl := pl
for _, r := range rs {
nl += len(r.v) - r.e + r.o
}
// Create our result bytes
result := make([]byte, nl)
o = 0 // Reuse as result offset
pi = 0 // Reuse as pattern index position
for _, r := range rs {
if r.o > 0 {
seg := p[pi:r.o]
copy(result[o:], seg)
o += len(seg)
}
copy(result[o:], r.v)
o += len(r.v)
pi = r.e
}
if pi < pl {
copy(result[o:], p[pi:])
}
return Pattern(result)
}
// Values extracts the tag values from a resource name, s, matching the pattern.
//
// The returned bool flag is true if s matched the pattern, otherwise false with a nil map.
//
// Behavior is undefined for an invalid pattern or an invalid resource name.
func (p Pattern) Values(s string) (map[string]string, bool) {
pi := 0
si := 0
pl := len(p)
sl := len(s)
var m map[string]string
for pi < pl {
if si == sl {
return nil, false
}
c := p[pi]
pi++
switch c {
case '$':
po := pi
for pi < pl && p[pi] != '.' {
pi++
}
so := si
for si < sl && s[si] != '.' {
si++
}
if m == nil {
m = make(map[string]string)
}
m[string(p[po:pi])] = s[so:si]
case '*':
for pi < pl && p[pi] != '.' {
pi++
}
for si < sl && s[si] != '.' {
si++
}
case '>':
if pi == pl {
return m, true
}
return nil, false
default:
for {
if c != s[si] {
return nil, false
}
si++
if c == '.' || pi == pl {
break
}
c = p[pi]
pi++
if si == sl {
return nil, false
}
}
}
}
if si != sl {
return nil, false
}
return m, true
}
// IndexWildcard returns the index of the first instance of a wild card (*, >, or $tag)
// in pattern, or -1 if no wildcard is present.
//
// Behavior is undefined for an invalid pattern.
func (p Pattern) IndexWildcard() int {
start := true
for i, c := range p {
if c == '.' {
start = true
} else {
if start && ((c == '>' && i == len(p)-1) ||
c == '*' ||
c == '$') {
return i
}
start = false
}
}
return -1
}