Skip to content

Commit

Permalink
Walk brace sections in directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Oct 12, 2020
1 parent ec22af5 commit 61bc16a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
23 changes: 14 additions & 9 deletions zglob.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ var (
)

type zenv struct {
dre *regexp.Regexp
fre *regexp.Regexp
pattern string
root string
dre *regexp.Regexp
fre *regexp.Regexp
braceDir bool
pattern string
root string
}

func New(pattern string) (*zenv, error) {
Expand Down Expand Up @@ -71,6 +72,7 @@ func New(pattern string) (*zenv, error) {
cc := []rune(globmask)
dirmask := ""
filemask := ""
braceDir := false
for i := 0; i < len(cc); i++ {
if cc[i] == '*' {
if i < len(cc)-2 && cc[i+1] == '*' && cc[i+2] == '/' {
Expand All @@ -89,6 +91,7 @@ func New(pattern string) (*zenv, error) {
if cc[j] == ',' {
pattern += "|"
} else if cc[j] == '}' {
braceDir = true
i = j
break
} else {
Expand Down Expand Up @@ -130,10 +133,11 @@ func New(pattern string) (*zenv, error) {
filemask = "(?i:" + filemask + ")"
}
return &zenv{
dre: regexp.MustCompile("^" + dirmask),
fre: regexp.MustCompile("^" + filemask + "$"),
pattern: pattern,
root: filepath.Clean(root),
dre: regexp.MustCompile("^" + dirmask),
fre: regexp.MustCompile("^" + filemask + "$"),
braceDir: braceDir,
pattern: pattern,
root: filepath.Clean(root),
}, nil
}

Expand Down Expand Up @@ -186,7 +190,8 @@ func glob(pattern string, followSymlinks bool) ([]string, error) {
mu.Unlock()
return nil
}
if !zenv.dre.MatchString(path + "/") {
// TODO braceDir matches a useless directory.
if !zenv.braceDir && !zenv.dre.MatchString(path+"/") {
return filepath.SkipDir
}
}
Expand Down
1 change: 1 addition & 0 deletions zglob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var testGlobs = []testZGlob{
{`**/bar/**/*.txt`, []string{`foo/bar/baz.txt`, `foo/bar/baz/noo.txt`}, nil},
{`**/bar/**/*.{jpg,png}`, []string{`zzz/bar/baz/joo.png`, `zzz/bar/baz/zoo.jpg`}, nil},
{`zzz/bar/baz/zoo.{jpg,png}`, []string{`zzz/bar/baz/zoo.jpg`}, nil},
{`zzz/bar/{baz,z}/zoo.jpg`, []string{`zzz/bar/baz/zoo.jpg`}, nil},
}

func setup(t *testing.T) string {
Expand Down

0 comments on commit 61bc16a

Please sign in to comment.