Also handle ()| in glob patterns (fixes #1365)

This commit is contained in:
Jakob Borg 2015-02-20 10:11:01 +01:00
parent 8b66472949
commit 1e4b2133f6
2 changed files with 13 additions and 4 deletions

View File

@ -54,17 +54,22 @@ func Convert(pattern string, flags int) (*regexp.Regexp, error) {
pattern = strings.Replace(pattern, "\\?", "[:escapedques:]", -1) pattern = strings.Replace(pattern, "\\?", "[:escapedques:]", -1)
pattern = strings.Replace(pattern, "\\.", "[:escapeddot:]", -1) pattern = strings.Replace(pattern, "\\.", "[:escapeddot:]", -1)
} }
pattern = strings.Replace(pattern, ".", "\\.", -1)
pattern = strings.Replace(pattern, "+", "\\+", -1) // Characters that are special in regexps but not in glob, must be
pattern = strings.Replace(pattern, "$", "\\$", -1) // escaped.
pattern = strings.Replace(pattern, "^", "\\^", -1) for _, char := range []string{".", "+", "$", "^", "(", ")", "|"} {
pattern = strings.Replace(pattern, char, "\\"+char, -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)
pattern = strings.Replace(pattern, "?", any, -1) pattern = strings.Replace(pattern, "?", any, -1)
pattern = strings.Replace(pattern, "[:escapedstar:]", "\\*", -1) pattern = strings.Replace(pattern, "[:escapedstar:]", "\\*", -1)
pattern = strings.Replace(pattern, "[:escapedques:]", "\\?", -1) pattern = strings.Replace(pattern, "[:escapedques:]", "\\?", -1)
pattern = strings.Replace(pattern, "[:escapeddot:]", "\\.", -1) pattern = strings.Replace(pattern, "[:escapeddot:]", "\\.", -1)
pattern = "^" + pattern + "$" pattern = "^" + pattern + "$"
if flags&FNM_CASEFOLD != 0 { if flags&FNM_CASEFOLD != 0 {
pattern = "(?i)" + pattern pattern = "(?i)" + pattern

View File

@ -68,6 +68,10 @@ var testcases = []testcase{
{"hey^hello", "hey^hello", 0, true}, {"hey^hello", "hey^hello", 0, true},
{"hey{hello", "hey{hello", 0, true}, {"hey{hello", "hey{hello", 0, true},
{"hey}hello", "hey}hello", 0, true}, {"hey}hello", "hey}hello", 0, true},
{"hey(hello", "hey(hello", 0, true},
{"hey)hello", "hey)hello", 0, true},
{"hey|hello", "hey|hello", 0, true},
{"hey|hello", "hey|other", 0, false},
} }
func TestMatch(t *testing.T) { func TestMatch(t *testing.T) {