| 1 |
// Copyright (c) 2026, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"regexp" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"mvdan.cc/sh/v3/pattern" |
| 13 |
) |
| 14 |
|
| 15 |
// ExtendedPatternMatcher returns a [regexp.Regexp.MatchString]-like function |
| 16 |
// to support !(pattern-list) extended patterns where possible. |
| 17 |
// It can be used instead of [pattern.Regexp] for narrow use cases. |
| 18 |
func ExtendedPatternMatcher(pat string, mode pattern.Mode) (func(string) bool, error) { |
| 19 |
if mode&pattern.ExtendedOperators != 0 && mode&pattern.EntireString == 0 { |
| 20 |
// In the future we could try to support !(pattern) without matching |
| 21 |
// the entire input, ensuring we add enough test cases. |
| 22 |
panic("ExtendedOperators is only supported with EntireString") |
| 23 |
} |
| 24 |
|
| 25 |
// Extended pattern matching operators are always on outside of pathname expansion. |
| 26 |
expr, err := pattern.Regexp(pat, mode) |
| 27 |
if err != nil { |
| 28 |
// Handle !(pattern-list) negation: when Regexp returns NegExtglobError, |
| 29 |
// match the inner pattern and negate the result. |
| 30 |
negErr, ok := errors.AsType[*pattern.NegExtGlobError](err) |
| 31 |
if !ok { |
| 32 |
return nil, err |
| 33 |
} |
| 34 |
return extNegatedMatcher(pat, negErr.Groups) |
| 35 |
} |
| 36 |
rx := regexp.MustCompile(expr) |
| 37 |
return rx.MatchString, nil |
| 38 |
} |
| 39 |
|
| 40 |
// extNegatedMatcher handles !(pattern-list) extglob negation. |
| 41 |
// Only a single !(...) group with fixed-string prefix and suffix is supported. |
| 42 |
func extNegatedMatcher(pat string, groups []pattern.NegExtGlobGroup) (func(string) bool, error) { |
| 43 |
if len(groups) != 1 { |
| 44 |
return nil, fmt.Errorf("multiple extglob !(...) groups are not supported yet") |
| 45 |
} |
| 46 |
g := groups[0] |
| 47 |
prefix := pat[:g.Start] |
| 48 |
suffix := pat[g.End:] |
| 49 |
|
| 50 |
if pattern.HasMeta(prefix, 0) || pattern.HasMeta(suffix, 0) { |
| 51 |
return nil, fmt.Errorf("extglob !(...) is only supported with a fixed prefix and suffix") |
| 52 |
} |
| 53 |
|
| 54 |
// Use @(inner) to compile the pattern list, then negate the match. |
| 55 |
inner := pat[g.Start+len("!(") : g.End-len(")")] |
| 56 |
expr, err := pattern.Regexp("@("+inner+")", pattern.EntireString|pattern.ExtendedOperators) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
rx := regexp.MustCompile(expr) |
| 61 |
|
| 62 |
return func(name string) bool { |
| 63 |
if !strings.HasPrefix(name, prefix) { |
| 64 |
return false |
| 65 |
} |
| 66 |
if !strings.HasSuffix(name, suffix) { |
| 67 |
return false |
| 68 |
} |
| 69 |
end := len(name) - len(suffix) |
| 70 |
if end < len(prefix) { |
| 71 |
return false // prefix and suffix overlap in name |
| 72 |
} |
| 73 |
middle := name[len(prefix):end] |
| 74 |
|
| 75 |
return !rx.MatchString(middle) |
| 76 |
}, nil |
| 77 |
} |