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