2019-10-15 21:24:16 +00:00
|
|
|
package editorconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2020-11-29 20:54:08 +00:00
|
|
|
"runtime"
|
2019-10-15 21:24:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-28 23:08:33 +00:00
|
|
|
// findLeftBrackets matches the opening left bracket {.
|
2019-10-15 21:24:16 +00:00
|
|
|
findLeftBrackets = regexp.MustCompile(`(^|[^\\])\{`)
|
2021-02-28 23:08:33 +00:00
|
|
|
// findDoubleLeftBrackets matches the duplicated opening left bracket {{.
|
|
|
|
findDoubleLeftBrackets = regexp.MustCompile(`(^|[^\\])\{\{`)
|
|
|
|
// findLeftBrackets matches the closing right bracket {.
|
2019-10-15 21:24:16 +00:00
|
|
|
findRightBrackets = regexp.MustCompile(`(^|[^\\])\}`)
|
2021-02-28 23:08:33 +00:00
|
|
|
// findDoubleRightBrackets matches the duplicated opening left bracket {{.
|
|
|
|
findDoubleRightBrackets = regexp.MustCompile(`(^|[^\\])\}\}`)
|
|
|
|
// findNumericRange matches a range of number, e.g. -2..5.
|
2019-10-15 21:24:16 +00:00
|
|
|
findNumericRange = regexp.MustCompile(`^([+-]?\d+)\.\.([+-]?\d+)$`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// FnmatchCase tests whether the name matches the given pattern case included.
|
|
|
|
func FnmatchCase(pattern, name string) (bool, error) {
|
2020-10-16 05:06:27 +00:00
|
|
|
p := translate(pattern)
|
2019-10-15 21:24:16 +00:00
|
|
|
|
|
|
|
r, err := regexp.Compile(fmt.Sprintf("^%s$", p))
|
|
|
|
if err != nil {
|
2021-02-28 23:08:33 +00:00
|
|
|
return false, fmt.Errorf("error compiling %q: %w", pattern, err)
|
2019-10-15 21:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r.MatchString(name), nil
|
|
|
|
}
|
|
|
|
|
2021-04-23 00:08:53 +00:00
|
|
|
func translate(pattern string) string { // nolint: funlen,gocognit,gocyclo,cyclop
|
2019-10-15 21:24:16 +00:00
|
|
|
index := 0
|
|
|
|
pat := []rune(pattern)
|
|
|
|
length := len(pat)
|
|
|
|
|
|
|
|
result := strings.Builder{}
|
|
|
|
|
|
|
|
braceLevel := 0
|
|
|
|
isEscaped := false
|
|
|
|
inBrackets := false
|
|
|
|
|
2021-02-28 23:08:33 +00:00
|
|
|
// Double left and right is a hack to pass the core-test suite.
|
|
|
|
left := len(findLeftBrackets.FindAllString(pattern, -1))
|
|
|
|
doubleLeft := len(findDoubleLeftBrackets.FindAllString(pattern, -1))
|
|
|
|
right := len(findRightBrackets.FindAllString(pattern, -1))
|
|
|
|
doubleRight := len(findDoubleRightBrackets.FindAllString(pattern, -1))
|
|
|
|
matchesBraces := left+doubleLeft == right+doubleRight
|
2020-11-29 20:54:08 +00:00
|
|
|
pathSeparator := "/"
|
2021-02-28 23:08:33 +00:00
|
|
|
|
2020-11-29 20:54:08 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
pathSeparator = regexp.QuoteMeta("\\")
|
|
|
|
}
|
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
for index < length {
|
|
|
|
r := pat[index]
|
|
|
|
index++
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
switch r {
|
|
|
|
case '*':
|
2019-10-15 21:24:16 +00:00
|
|
|
p := index
|
|
|
|
if p < length && pat[p] == '*' {
|
|
|
|
result.WriteString(".*")
|
|
|
|
index++
|
|
|
|
} else {
|
2020-11-29 20:54:08 +00:00
|
|
|
result.WriteString(fmt.Sprintf("[^%s]*", pathSeparator))
|
2019-10-15 21:24:16 +00:00
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
case '/':
|
2019-10-15 21:24:16 +00:00
|
|
|
p := index
|
|
|
|
if p+2 < length && pat[p] == '*' && pat[p+1] == '*' && pat[p+2] == '/' {
|
2020-11-29 20:54:08 +00:00
|
|
|
result.WriteString(fmt.Sprintf("(?:%s|%s.*%s)", pathSeparator, pathSeparator, pathSeparator))
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
index += 3
|
|
|
|
} else {
|
|
|
|
result.WriteRune(r)
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
case '?':
|
2020-11-29 20:54:08 +00:00
|
|
|
result.WriteString(fmt.Sprintf("[^%s]", pathSeparator))
|
2020-10-16 05:06:27 +00:00
|
|
|
case '[':
|
2021-02-28 23:08:33 +00:00
|
|
|
if inBrackets { // nolint: nestif
|
2019-10-15 21:24:16 +00:00
|
|
|
result.WriteString("\\[")
|
|
|
|
} else {
|
|
|
|
hasSlash := false
|
|
|
|
res := strings.Builder{}
|
|
|
|
|
|
|
|
p := index
|
|
|
|
for p < length {
|
|
|
|
if pat[p] == ']' && pat[p-1] != '\\' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res.WriteRune(pat[p])
|
|
|
|
if pat[p] == '/' && pat[p-1] != '\\' {
|
|
|
|
hasSlash = true
|
2021-02-28 23:08:33 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
p++
|
|
|
|
}
|
|
|
|
if hasSlash {
|
|
|
|
result.WriteString("\\[" + res.String())
|
|
|
|
index = p + 1
|
|
|
|
} else {
|
|
|
|
inBrackets = true
|
|
|
|
if index < length && pat[index] == '!' || pat[index] == '^' {
|
|
|
|
index++
|
|
|
|
result.WriteString("[^")
|
|
|
|
} else {
|
|
|
|
result.WriteRune('[')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
case ']':
|
2019-10-15 21:24:16 +00:00
|
|
|
if inBrackets && pat[index-2] == '\\' {
|
|
|
|
result.WriteString("\\]")
|
|
|
|
} else {
|
|
|
|
result.WriteRune(r)
|
|
|
|
inBrackets = false
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
case '{':
|
2019-10-15 21:24:16 +00:00
|
|
|
hasComma := false
|
|
|
|
p := index
|
|
|
|
res := strings.Builder{}
|
|
|
|
|
|
|
|
for p < length {
|
|
|
|
if pat[p] == '}' && pat[p-1] != '\\' {
|
|
|
|
break
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
res.WriteRune(pat[p])
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
if pat[p] == ',' && pat[p-1] != '\\' {
|
|
|
|
hasComma = true
|
2021-02-28 23:08:33 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
p++
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
switch {
|
|
|
|
case !hasComma && p < length:
|
2019-10-15 21:24:16 +00:00
|
|
|
inner := res.String()
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
sub := findNumericRange.FindStringSubmatch(inner)
|
|
|
|
if len(sub) == 3 {
|
|
|
|
from, _ := strconv.Atoi(sub[1])
|
|
|
|
to, _ := strconv.Atoi(sub[2])
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
result.WriteString("(?:")
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
// XXX does not scale well
|
|
|
|
for i := from; i < to; i++ {
|
|
|
|
result.WriteString(strconv.Itoa(i))
|
|
|
|
result.WriteRune('|')
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
result.WriteString(strconv.Itoa(to))
|
|
|
|
result.WriteRune(')')
|
|
|
|
} else {
|
2020-10-16 05:06:27 +00:00
|
|
|
r := translate(inner)
|
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
result.WriteString(fmt.Sprintf("\\{%s\\}", r))
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
index = p + 1
|
2020-10-16 05:06:27 +00:00
|
|
|
case matchesBraces:
|
2019-10-15 21:24:16 +00:00
|
|
|
result.WriteString("(?:")
|
|
|
|
braceLevel++
|
2020-10-16 05:06:27 +00:00
|
|
|
default:
|
2019-10-15 21:24:16 +00:00
|
|
|
result.WriteString("\\{")
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
case '}':
|
2019-10-15 21:24:16 +00:00
|
|
|
if braceLevel > 0 {
|
|
|
|
if isEscaped {
|
|
|
|
result.WriteRune('}')
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
isEscaped = false
|
|
|
|
} else {
|
|
|
|
result.WriteRune(')')
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-10-15 21:24:16 +00:00
|
|
|
braceLevel--
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.WriteString("\\}")
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
case ',':
|
2019-10-15 21:24:16 +00:00
|
|
|
if braceLevel == 0 || isEscaped {
|
|
|
|
result.WriteRune(r)
|
|
|
|
} else {
|
|
|
|
result.WriteRune('|')
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
default:
|
|
|
|
if r != '\\' || isEscaped {
|
|
|
|
result.WriteString(regexp.QuoteMeta(string(r)))
|
|
|
|
|
|
|
|
isEscaped = false
|
|
|
|
} else {
|
|
|
|
isEscaped = true
|
|
|
|
}
|
2019-10-15 21:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
return result.String()
|
2019-10-15 21:24:16 +00:00
|
|
|
}
|