|
| 1 | +package gazelle |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/bmatcuk/doublestar/v4" |
| 10 | +) |
| 11 | + |
| 12 | +type GlobExpr func(string) bool |
| 13 | + |
| 14 | +// Expressions that are not even globs |
| 15 | +var nonGlobRe = regexp.MustCompile(`^[\w./@-]+$`) |
| 16 | + |
| 17 | +// Doublestar globs that can be simplified to only a prefix and suffix |
| 18 | +var prePostGlobRe = regexp.MustCompile(`^([\w./@-]*)\*\*(?:/\*?)?([\w./@-]+)$`) |
| 19 | + |
| 20 | +// Globs with a prefix or postfix that can be checked before invoking the regex |
| 21 | +var preGlobRe = regexp.MustCompile(`^([\w./@-]+).*$`) |
| 22 | +var postGlobRe = regexp.MustCompile(`^.*?([\w./@-]+)$`) |
| 23 | + |
| 24 | +var parsedExpCache sync.Map |
| 25 | + |
| 26 | +func ParseGlobExpression(exp string) (GlobExpr, error) { |
| 27 | + loaded, ok := parsedExpCache.Load(exp) |
| 28 | + if ok { |
| 29 | + return loaded.(GlobExpr), nil |
| 30 | + } |
| 31 | + |
| 32 | + if !doublestar.ValidatePattern(exp) { |
| 33 | + return nil, fmt.Errorf("invalid glob pattern: %s", exp) |
| 34 | + } |
| 35 | + |
| 36 | + expr := parseGlobExpression(exp) |
| 37 | + loaded, _ = parsedExpCache.LoadOrStore(exp, expr) |
| 38 | + return loaded.(GlobExpr), nil |
| 39 | +} |
| 40 | + |
| 41 | +func parseGlobExpression(exp string) GlobExpr { |
| 42 | + if nonGlobRe.MatchString(exp) { |
| 43 | + return func(p string) bool { |
| 44 | + return p == exp |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if extGlob := prePostGlobRe.FindStringSubmatch(exp); len(extGlob) > 0 { |
| 49 | + // Globs that can be expressed as pre + ** + ext |
| 50 | + pre, ext := extGlob[1], extGlob[2] |
| 51 | + minLen := len(pre) + len(ext) |
| 52 | + return func(p string) bool { |
| 53 | + return len(p) >= minLen && strings.HasPrefix(p, pre) && strings.HasSuffix(p, ext) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if preGlob := preGlobRe.FindStringSubmatch(exp); len(preGlob) > 0 { |
| 58 | + pre := preGlob[1] |
| 59 | + return func(p string) bool { |
| 60 | + if !strings.HasPrefix(p, pre) { |
| 61 | + return false |
| 62 | + } |
| 63 | + return doublestar.MatchUnvalidated(exp, p) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if postGlob := postGlobRe.FindStringSubmatch(exp); len(postGlob) > 0 { |
| 68 | + post := postGlob[1] |
| 69 | + return func(p string) bool { |
| 70 | + if !strings.HasSuffix(p, post) { |
| 71 | + return false |
| 72 | + } |
| 73 | + return doublestar.MatchUnvalidated(exp, p) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return func(p string) bool { |
| 78 | + return doublestar.MatchUnvalidated(exp, p) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func ParseGlobExpressions(exps []string) (GlobExpr, error) { |
| 83 | + if len(exps) == 1 { |
| 84 | + return ParseGlobExpression(exps[0]) |
| 85 | + } |
| 86 | + |
| 87 | + key := strings.Join(exps, ",") |
| 88 | + loaded, ok := parsedExpCache.Load(key) |
| 89 | + if ok { |
| 90 | + return loaded.(GlobExpr), nil |
| 91 | + } |
| 92 | + |
| 93 | + expr, err := parseGlobExpressions(exps) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + loaded, _ = parsedExpCache.LoadOrStore(key, expr) |
| 99 | + return loaded.(GlobExpr), nil |
| 100 | +} |
| 101 | + |
| 102 | +func parseGlobExpressions(exps []string) (GlobExpr, error) { |
| 103 | + exacts := make(map[string]struct{}) |
| 104 | + prePosts := make(map[string][]string) |
| 105 | + preGlobs := make(map[string][]string) |
| 106 | + postGlobs := make(map[string][]string) |
| 107 | + globs := make([]string, 0) |
| 108 | + |
| 109 | + for _, exp := range exps { |
| 110 | + if !doublestar.ValidatePattern(exp) { |
| 111 | + return nil, fmt.Errorf("invalid glob pattern: %s", exp) |
| 112 | + } |
| 113 | + |
| 114 | + if nonGlobRe.MatchString(exp) { |
| 115 | + exacts[exp] = struct{}{} |
| 116 | + } else if extGlob := prePostGlobRe.FindStringSubmatch(exp); len(extGlob) > 0 { |
| 117 | + // Globs that can be expressed as pre + ** + ext |
| 118 | + pre, ext := extGlob[1], extGlob[2] |
| 119 | + prePosts[pre] = append(prePosts[pre], ext) |
| 120 | + } else if preGlob := preGlobRe.FindStringSubmatch(exp); len(preGlob) > 0 { |
| 121 | + pre := preGlob[1] |
| 122 | + preGlobs[pre] = append(preGlobs[pre], exp) |
| 123 | + } else if postGlob := postGlobRe.FindStringSubmatch(exp); len(postGlob) > 0 { |
| 124 | + post := postGlob[1] |
| 125 | + postGlobs[post] = append(postGlobs[post], exp) |
| 126 | + } else { |
| 127 | + globs = append(globs, exp) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + exprFuncs := make([]GlobExpr, 0, 5) |
| 132 | + |
| 133 | + if len(exacts) > 0 { |
| 134 | + exprFuncs = append(exprFuncs, func(p string) bool { |
| 135 | + _, e := exacts[p] |
| 136 | + return e |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + if len(prePosts) > 0 { |
| 141 | + exprFuncs = append(exprFuncs, func(p string) bool { |
| 142 | + for pre, exts := range prePosts { |
| 143 | + if strings.HasPrefix(p, pre) { |
| 144 | + for _, ext := range exts { |
| 145 | + if len(p) >= len(pre)+len(ext) && strings.HasSuffix(p, ext) { |
| 146 | + return true |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + return false |
| 152 | + }) |
| 153 | + } |
| 154 | + |
| 155 | + if len(preGlobs) > 0 { |
| 156 | + exprFuncs = append(exprFuncs, func(p string) bool { |
| 157 | + for pre, globs := range preGlobs { |
| 158 | + if strings.HasPrefix(p, pre) { |
| 159 | + for _, glob := range globs { |
| 160 | + if doublestar.MatchUnvalidated(glob, p) { |
| 161 | + return true |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + return false |
| 167 | + }) |
| 168 | + } |
| 169 | + |
| 170 | + if len(postGlobs) > 0 { |
| 171 | + exprFuncs = append(exprFuncs, func(p string) bool { |
| 172 | + for post, globs := range postGlobs { |
| 173 | + if strings.HasSuffix(p, post) { |
| 174 | + for _, glob := range globs { |
| 175 | + if doublestar.MatchUnvalidated(glob, p) { |
| 176 | + return true |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + return false |
| 182 | + }) |
| 183 | + } |
| 184 | + |
| 185 | + if len(globs) > 0 { |
| 186 | + exprFuncs = append(exprFuncs, func(p string) bool { |
| 187 | + for _, glob := range globs { |
| 188 | + if doublestar.MatchUnvalidated(glob, p) { |
| 189 | + return true |
| 190 | + } |
| 191 | + } |
| 192 | + return false |
| 193 | + }) |
| 194 | + } |
| 195 | + |
| 196 | + return func(p string) bool { |
| 197 | + for _, expr := range exprFuncs { |
| 198 | + if expr(p) { |
| 199 | + return true |
| 200 | + } |
| 201 | + } |
| 202 | + return false |
| 203 | + }, nil |
| 204 | +} |
0 commit comments