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

Unexpected results with escaping #36

Open
jacobcase opened this issue Jun 7, 2019 · 1 comment
Open

Unexpected results with escaping #36

jacobcase opened this issue Jun 7, 2019 · 1 comment

Comments

@jacobcase
Copy link

jacobcase commented Jun 7, 2019

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.

@jacobcase
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant