Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to compile a pattern #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions doublestar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,36 @@ func TestMatch(t *testing.T) {
}
}

func TestCompile(t *testing.T) {
for idx, tt := range matchTests {
testCompileWith(t, idx, tt)
}
}

func testCompileWith(t *testing.T, idx int, tt MatchTest) {
defer func() {
if r := recover(); r != nil {
t.Errorf("#%v. Match(%#q, %#q) panicked: %#v", idx, tt.pattern, tt.testPath, r)
}
}()

ok := false
pat, err := Compile(tt.pattern)
if err == nil {
ok = pat.Match(tt.testPath)
}
if ok != tt.shouldMatch || err != tt.expectedErr {
t.Errorf("#%v. Match(%#q, %#q) = %v, %v want %v, %v", idx, tt.pattern, tt.testPath, ok, err, tt.shouldMatch, tt.expectedErr)
}

if tt.isStandard {
stdOk, stdErr := path.Match(tt.pattern, tt.testPath)
if ok != stdOk || !compareErrors(err, stdErr) {
t.Errorf("#%v. Match(%#q, %#q) != path.Match(...). Got %v, %v want %v, %v", idx, tt.pattern, tt.testPath, ok, err, stdOk, stdErr)
}
}
}

func testMatchWith(t *testing.T, idx int, tt MatchTest) {
defer func() {
if r := recover(); r != nil {
Expand Down
33 changes: 33 additions & 0 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ func PathMatchUnvalidated(pattern, name string) bool {
return matched
}

// Pattern is a representation of a compiled glob pattern.
type Pattern struct {
// initial implementation doesn't actually "compile", just saves the pattern to use when matching
// Obviously long term we'll want to store some kind of tree or something
pattern string
}

// Compile parses a pattern and returns, if successful, a [Pattern] object
// that can be used to match against text.
func Compile(pattern string) (*Pattern, error) {
if !ValidatePattern(pattern) {
return nil, ErrBadPattern
}
return &Pattern{
pattern: pattern,
}, nil
}

// MustCompile is like Compile but panics if the expression cannot be parsed.
func MustCompile(pattern string) *Pattern {

p, err := Compile(pattern)
if err != nil {
panic(err)
}
return p
}

// Match reports whether the name matches this pattern
func (p *Pattern) Match(name string) bool {
return MatchUnvalidated(p.pattern, name)
}

func matchWithSeparator(pattern, name string, separator rune, validate bool) (matched bool, err error) {
return doMatchWithSeparator(pattern, name, separator, validate, -1, -1, -1, -1, 0, 0)
}
Expand Down
Loading