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