gobwas/glob

Unexpected results with escaping

jacobcase opened this issue · 1 comments

Hello,

UPDATE: Looks like all of the multi-term patterns were not working as expected due to spacing. They all work as expected when that's fixed. All that leaves is the first pattern I put where I would expect it not to match.

I am building a single string out of a list of glob patterns I want to match to pass into glob.Compile().
An example might be glob.Compile("{"+strings.Join(globPatterns, ",")+"}", '.')

It seems though it may be impossible to have a , in one of the patterns. I wrote a simple test table to demonstrate what my assumption would be and what the actual results were in a comment above each test case. The last case I listed also surprised me, because the Compile documentation suggests that \ should escape { and }.

type T struct {
	Pattern string
	Input   string
}

func main() {
	tests := []T{
		{
			// Should not match since ',' is a separator (it does)
			Pattern: "test,pattern",
			Input:   "test,pattern",
		},
		{
			// Should match (and it does)
			Pattern: "test\\,pattern",
			Input:   "test,pattern",
		},
		{
			// Should not match (it doesn't)
			Pattern: "{ test,pattern }",
			Input:   "test,pattern",
		},
		{
			// Should match (it doesn't)
			Pattern: "{ test\\,pattern }",
			Input:   "test,pattern",
		},
		{
			// Should match (it doesn't)
			Pattern: "{ \\{test\\} }",
			Input:   "{test}",
		},
	}

	for _, t := range tests {
		g, err := glob.Compile(t.Pattern)
		if err != nil {
			panic(err)
		}

		fmt.Printf("%+v, %v\n", t, g.Match(t.Input))
	}
}

Which prints the following:

./globtest
{Pattern:test,pattern Input:test,pattern}, true
{Pattern:test\,pattern Input:test,pattern}, true
{Pattern:{ test,pattern } Input:test,pattern}, false
{Pattern:{ test\,pattern } Input:test,pattern}, false
{Pattern:{ \{test\} } Input:{test}}, false

Are my assumptions in my comments correct? If so, how would you suggest they be addressed? I would be willing to take a crack at it if you would like.

Additionally, should the Compile documentation be updated to include , in the list of special characters that need escaped? QuoteMeta also appears to not escape ,.