github.com/meatballhat/deppy@v0.0.0-20151116212532-116c2a9aa48d/match.go (about)

     1  package main
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  // matchPattern(pattern)(name) reports whether
     9  // name matches pattern.  Pattern is a limited glob
    10  // pattern in which '...' means 'any string' and there
    11  // is no other special syntax.
    12  // Taken from $GOROOT/src/cmd/go/main.go.
    13  func matchPattern(pattern string) func(name string) bool {
    14  	re := regexp.QuoteMeta(pattern)
    15  	re = strings.Replace(re, `\.\.\.`, `.*`, -1)
    16  	// Special case: foo/... matches foo too.
    17  	if strings.HasSuffix(re, `/.*`) {
    18  		re = re[:len(re)-len(`/.*`)] + `(/.*)?`
    19  	}
    20  	reg := regexp.MustCompile(`^` + re + `$`)
    21  	return func(name string) bool {
    22  		return reg.MatchString(name)
    23  	}
    24  }