Correctly handle ^ and $ in ignore patterns (fixes #1365)

This commit is contained in:
Jakob Borg 2015-02-19 09:10:32 +02:00
parent efd6a29909
commit 7921082ece
2 changed files with 8 additions and 0 deletions

View File

@ -56,6 +56,8 @@ func Convert(pattern string, flags int) (*regexp.Regexp, error) {
} }
pattern = strings.Replace(pattern, ".", "\\.", -1) pattern = strings.Replace(pattern, ".", "\\.", -1)
pattern = strings.Replace(pattern, "+", "\\+", -1) pattern = strings.Replace(pattern, "+", "\\+", -1)
pattern = strings.Replace(pattern, "$", "\\$", -1)
pattern = strings.Replace(pattern, "^", "\\^", -1)
pattern = strings.Replace(pattern, "**", "[:doublestar:]", -1) pattern = strings.Replace(pattern, "**", "[:doublestar:]", -1)
pattern = strings.Replace(pattern, "*", any+"*", -1) pattern = strings.Replace(pattern, "*", any+"*", -1)
pattern = strings.Replace(pattern, "[:doublestar:]", ".*", -1) pattern = strings.Replace(pattern, "[:doublestar:]", ".*", -1)

View File

@ -62,6 +62,12 @@ var testcases = []testcase{
{"**/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, true}, {"**/foo.txt", "bar/baz/foo.txt", FNM_PATHNAME, true},
{"foo.txt", "foo.TXT", FNM_CASEFOLD, true}, {"foo.txt", "foo.TXT", FNM_CASEFOLD, true},
// These characters are literals in glob, but not in regexp.
{"hey$hello", "hey$hello", 0, true},
{"hey^hello", "hey^hello", 0, true},
{"hey{hello", "hey{hello", 0, true},
{"hey}hello", "hey}hello", 0, true},
} }
func TestMatch(t *testing.T) { func TestMatch(t *testing.T) {