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

wildmatch: ensure given patterns are POSIX-compliant #6

Merged
merged 3 commits into from
Feb 19, 2018
Merged
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
33 changes: 32 additions & 1 deletion wildmatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Wildmatch struct {
// If the pattern is malformed, for instance, it has an unclosed character
// group, escape sequence, or character class, NewWildmatch will panic().
func NewWildmatch(p string, opts ...opt) *Wildmatch {
w := &Wildmatch{p: p}
w := &Wildmatch{p: slashEscape(p)}

for _, opt := range opts {
opt(w)
Expand All @@ -69,6 +69,37 @@ const (
escapes = "\\[]*?"
)

// slashEscape converts paths "p" to POSIX-compliant path, independent of which
// escape character the host machine uses.
//
// slashEscape resepcts escapable sequences, and thus will not transform
// `foo\*bar` to `foo/*bar` on non-Windows operating systems.
func slashEscape(p string) string {
var pp string

for i := 0; i < len(p); {
c := p[i]

switch c {
case '\\':
if i+1 < len(p) && escapable(p[i+1]) {
pp += `\`
pp += string(p[i+1])

i += 2
} else {
pp += `/`
i += 1
}
default:
pp += string(c)
i += 1
}
}

return pp
}

// escapable returns whether the given "c" is escapable.
func escapable(c byte) bool {
return strings.IndexByte(escapes, c) > -1
Expand Down
46 changes: 31 additions & 15 deletions wildmatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,6 @@ var Cases = []*Case{
Subject: `?a?b`,
Match: true,
},
{
Pattern: `\a\b\c`,
Subject: `abc`,
Match: true,
},
{
Pattern: `''`,
Subject: `foo`,
Expand Down Expand Up @@ -561,16 +556,6 @@ var Cases = []*Case{
Subject: `-.]`,
Match: false,
},
{
Pattern: `[\1-\3]`,
Subject: `2`,
Match: true,
},
{
Pattern: `[\1-\3]`,
Subject: `3`,
Match: true,
},
{
Pattern: `-*-*-*-*-*-*-12-*-*-*-m-*-*-*`,
Subject: `-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1`,
Expand Down Expand Up @@ -619,3 +604,34 @@ func TestWildmatch(t *testing.T) {
c.Assert(t)
}
}

type SlashCase struct {
Given string
Expect string
}

func (c *SlashCase) Assert(t *testing.T) {
got := slashEscape(c.Given)

if c.Expect != got {
t.Errorf("wildmatch: expected slashEscape(\"%s\") -> %s, got: %s",
c.Given,
c.Expect,
got,
)
}
}

func TestSlashEscape(t *testing.T) {
for _, c := range []*SlashCase{
{Given: ``, Expect: ``},
{Given: `foo/bar`, Expect: `foo/bar`},
{Given: `foo\bar`, Expect: `foo/bar`},
{Given: `foo\*bar`, Expect: `foo\*bar`},
{Given: `foo\?bar`, Expect: `foo\?bar`},
{Given: `foo\[bar`, Expect: `foo\[bar`},
{Given: `foo\]bar`, Expect: `foo\]bar`},
} {
c.Assert(t)
}
}