forked from cedar-policy/cedar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.go
More file actions
66 lines (62 loc) · 1.66 KB
/
match.go
File metadata and controls
66 lines (62 loc) · 1.66 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
package cedar
import "github.com/cedar-policy/cedar-go/x/exp/parser"
// ported from Go's stdlib and reduced to our scope.
// https://golang.org/src/path/filepath/match.go?s=1226:1284#L34
// Match reports whether name matches the shell file name pattern.
// The pattern syntax is:
//
// pattern:
// { term }
// term:
// '*' matches any sequence of non-Separator characters
// c matches character c (c != '*')
func match(p parser.Pattern, name string) (matched bool) {
Pattern:
for i, comp := range p.Comps {
lastChunk := i == len(p.Comps)-1
if comp.Star && comp.Chunk == "" {
return true
}
// Look for Match at current position.
t, ok := matchChunk(comp.Chunk, name)
// if we're the last chunk, make sure we've exhausted the name
// otherwise we'll give a false result even if we could still Match
// using the star
if ok && (len(t) == 0 || !lastChunk) {
name = t
continue
}
if comp.Star {
// Look for Match skipping i+1 bytes.
for i := 0; i < len(name); i++ {
t, ok := matchChunk(comp.Chunk, name[i+1:])
if ok {
// if we're the last chunk, make sure we exhausted the name
if lastChunk && len(t) > 0 {
continue
}
name = t
continue Pattern
}
}
}
return false
}
return len(name) == 0
}
// matchChunk checks whether chunk matches the beginning of s.
// If so, it returns the remainder of s (after the Match).
// Chunk is all single-character operators: literals, char classes, and ?.
func matchChunk(chunk, s string) (rest string, ok bool) {
for len(chunk) > 0 {
if len(s) == 0 {
return
}
if chunk[0] != s[0] {
return
}
s = s[1:]
chunk = chunk[1:]
}
return s, true
}