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

path/filepath: Match incorrectly reports a pattern as malformed #13396

Closed
0xmohit opened this issue Nov 25, 2015 · 3 comments
Closed

path/filepath: Match incorrectly reports a pattern as malformed #13396

0xmohit opened this issue Nov 25, 2015 · 3 comments

Comments

@0xmohit
Copy link
Contributor

0xmohit commented Nov 25, 2015

$ go version
go version go1.5.1 linux/amd64

Match doesn't accept a hyphen inside a character class except when it is a part of a range, e.g. [0-5] or when it is escaped [\-]. A hyphen at the beginning or the end of the character class should ideally work without escaping.

$ cd $(mktemp -d)
$ touch foo.bar
$ touch foo-bar
$ touch foobar
$ ls -l
total 0
-rw-rw-r-- 1 mohit mohit 0 Nov 25 10:05 foobar
-rw-rw-r-- 1 mohit mohit 0 Nov 25 10:05 foo-bar
-rw-rw-r-- 1 mohit mohit 0 Nov 25 10:05 foo.bar

Test program:

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    m, e := filepath.Glob(`foo*[.-]*`)
    if e != nil {
        panic(e)
    }
    for _, v := range m {
        fmt.Printf("%v\n", v)
    }
}

Executing it results in:

panic: syntax error in pattern

goroutine 1 [running]:
main.main()
    /tmp/tmp.ijbcwjChbR/z.go:11 +0xa1
exit status 2

The same glob pattern yields results in the shell:

$ ls -l -- foo[.-]*
-rw-rw-r-- 1 mohit mohit 0 Nov 25 10:05 foo-bar
-rw-rw-r-- 1 mohit mohit 0 Nov 25 10:05 foo.bar

Similar globs appear to work in other languages too:

>>> from glob import glob
>>> glob("foo[.-]*")
['foo.bar', 'foo-bar']
>>> 
import std.path;

void main()
{
    assert(globMatch("foo-bar", `foo[-]*`) == true);
    assert(globMatch("foo.bar-baz", `foo[-.]*`) == true);
}
@gopherbot
Copy link
Contributor

CL https://golang.org/cl/17213 mentions this issue.

@ianlancetaylor
Copy link
Member

rsc commented on the CL: "Thanks but no. While it's true that some regexp packages do accept [-] or [x-] as equivalent to [-] or [x-], it's not universal. Go is not adhering to some external spec here. We defined a specific grammar (see the doc comment on filepath.Match) and that grammar does not admit [-] nor [x-]."

@mattn
Copy link
Member

mattn commented Nov 26, 2015

However, on windows, we can't escape - as \- because go disable escape of [] on windows. So we can't glob the files with this pattern.

@golang golang locked and limited conversation to collaborators Nov 27, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants