gunk-dev/the-valley

glob.go

sourcee2dd787b4f4fmain's published tip history
3.1 KiB
1package derive
2
3import "strings"
4
5// Match reports whether a changed path matches a class's glob.
6//
7// A glob is read segment by segment, and matched byte by byte, because a
8// path is bytes and need not be text. In a segment, `*` matches any bytes
9// but `/`, `?` matches one byte that is not `/`, and every other byte
10// matches itself. A segment that is exactly `**` matches any number of
11// segments, none included, so `a/**/b` matches `a/b` as well as `a/x/y/b`,
12// and a last `**` matches the rest of the path, whatever it is. Every
13// other segment matches exactly one segment.
14//
15// One trailing `/` is dropped, so `docs/` matches the path `docs`. `**` is special only as a whole
16// segment, so `a**b` is two `*` in one segment.
17func Match(glob, path string) bool {
18 return compile(glob).match(path)
19}
20
21// glob is a compiled glob: a sequence of tokens, each matching some bytes.
22type glob []token
23
24type token struct {
25 kind tokenKind
26 b byte // the byte a literal matches
27}
28
29type tokenKind uint8
30
31const (
32 literal tokenKind = iota // one byte, itself
33 one // `?`: one byte that is not `/`
34 within // `*`: any bytes, none of them `/`
35 rest // a last `**` segment: any bytes at all
36 segments // any other `**` segment: nothing, or any bytes ending in `/`
37)
38
39func compile(pattern string) glob {
40 segs := strings.Split(pattern, "/")
41 if segs[len(segs)-1] == "" {
42 segs = segs[:len(segs)-1]
43 }
44 var g glob
45 for i, seg := range segs {
46 last := i == len(segs)-1
47 if seg == "**" {
48 if last {
49 g = append(g, token{kind: rest})
50 } else {
51 // It takes the `/` that would follow it, so that matching
52 // no segment leaves no `/` behind.
53 g = append(g, token{kind: segments})
54 }
55 continue
56 }
57 for j := 0; j < len(seg); j++ {
58 switch seg[j] {
59 case '*':
60 g = append(g, token{kind: within})
61 case '?':
62 g = append(g, token{kind: one})
63 default:
64 g = append(g, token{kind: literal, b: seg[j]})
65 }
66 }
67 if !last {
68 g = append(g, token{kind: literal, b: '/'})
69 }
70 }
71 return g
72}
73
74// match reports whether the whole of path matches the whole of g. Each
75// pair of token and position is decided once, so a glob with many `*` and
76// `**` costs no more than the product of its length and the path's.
77func (g glob) match(path string) bool {
78 n := len(path)
79 const unknown, no, yes = 0, 1, 2
80 seen := make([]uint8, (len(g)+1)*(n+1))
81 var at func(t, p int) bool
82 at = func(t, p int) bool {
83 if t == len(g) {
84 return p == n
85 }
86 k := t*(n+1) + p
87 if seen[k] != unknown {
88 return seen[k] == yes
89 }
90 ok := false
91 switch tok := g[t]; tok.kind {
92 case literal:
93 ok = p < n && path[p] == tok.b && at(t+1, p+1)
94 case one:
95 ok = p < n && path[p] != '/' && at(t+1, p+1)
96 case within:
97 for q := p; !ok; q++ {
98 ok = at(t+1, q)
99 if q == n || path[q] == '/' {
100 break
101 }
102 }
103 case rest:
104 for q := p; q <= n && !ok; q++ {
105 ok = at(t+1, q)
106 }
107 case segments:
108 ok = at(t+1, p)
109 for q := p; q < n && !ok; q++ {
110 if path[q] == '/' {
111 ok = at(t+1, q+1)
112 }
113 }
114 }
115 seen[k] = no
116 if ok {
117 seen[k] = yes
118 }
119 return ok
120 }
121 return at(0, 0)
122}