Skip to content

Unexpected results with escaping #36

@jacobcase

Description

@jacobcase

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions