From c90f36e3dd179d2de96f4f5fe38d8dc9a9de6dfe Mon Sep 17 00:00:00 2001 From: Emile Date: Fri, 25 Oct 2024 15:55:50 +0200 Subject: vendor --- vendor/go.mau.fi/util/glob/util.go | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 vendor/go.mau.fi/util/glob/util.go (limited to 'vendor/go.mau.fi/util/glob/util.go') diff --git a/vendor/go.mau.fi/util/glob/util.go b/vendor/go.mau.fi/util/glob/util.go new file mode 100644 index 0000000..37d9b2d --- /dev/null +++ b/vendor/go.mau.fi/util/glob/util.go @@ -0,0 +1,42 @@ +package glob + +import ( + "regexp" + "strings" +) + +var redundantStarRegex = regexp.MustCompile(`\*{2,}`) +var maybeRedundantQuestionRegex = regexp.MustCompile(`[*?]{2,}`) +var wildcardRegex = regexp.MustCompile(`[*?]+`) + +func SplitPattern(pattern string) []string { + indexes := wildcardRegex.FindAllStringIndex(pattern, -1) + if len(indexes) == 0 { + return []string{pattern} + } + parts := make([]string, 0, len(indexes)+1) + start := 0 + for _, part := range indexes { + end := part[0] + if end > start { + parts = append(parts, pattern[start:end]) + } + parts = append(parts, pattern[part[0]:part[1]]) + start = part[1] + } + if start < len(pattern) { + parts = append(parts, pattern[start:]) + } + return parts +} + +func Simplify(pattern string) string { + pattern = redundantStarRegex.ReplaceAllString(pattern, "*") + pattern = maybeRedundantQuestionRegex.ReplaceAllStringFunc(pattern, func(s string) string { + if !strings.ContainsRune(s, '*') { + return s + } + return strings.Repeat("?", strings.Count(s, "?")) + "*" + }) + return pattern +} -- cgit 1.4.1