mirror of
https://github.com/go-gitea/gitea.git
synced 2024-08-25 13:47:26 +02:00
* update github.com/blevesearch/bleve v2.0.2 -> v2.0.3 * github.com/denisenkom/go-mssqldb v0.9.0 -> v0.10.0 * github.com/editorconfig/editorconfig-core-go v2.4.1 -> v2.4.2 * github.com/go-chi/cors v1.1.1 -> v1.2.0 * github.com/go-git/go-billy v5.0.0 -> v5.1.0 * github.com/go-git/go-git v5.2.0 -> v5.3.0 * github.com/go-ldap/ldap v3.2.4 -> v3.3.0 * github.com/go-redis/redis v8.6.0 -> v8.8.2 * github.com/go-sql-driver/mysql v1.5.0 -> v1.6.0 * github.com/go-swagger/go-swagger v0.26.1 -> v0.27.0 * github.com/lib/pq v1.9.0 -> v1.10.1 * github.com/mattn/go-sqlite3 v1.14.6 -> v1.14.7 * github.com/go-testfixtures/testfixtures v3.5.0 -> v3.6.0 * github.com/issue9/identicon v1.0.1 -> v1.2.0 * github.com/klauspost/compress v1.11.8 -> v1.12.1 * github.com/mgechev/revive v1.0.3 -> v1.0.6 * github.com/microcosm-cc/bluemonday v1.0.7 -> v1.0.8 * github.com/niklasfasching/go-org v1.4.0 -> v1.5.0 * github.com/olivere/elastic v7.0.22 -> v7.0.24 * github.com/pelletier/go-toml v1.8.1 -> v1.9.0 * github.com/prometheus/client_golang v1.9.0 -> v1.10.0 * github.com/xanzy/go-gitlab v0.44.0 -> v0.48.0 * github.com/yuin/goldmark v1.3.3 -> v1.3.5 * github.com/6543/go-version v1.2.4 -> v1.3.1 * do github.com/lib/pq v1.10.0 -> v1.10.1 again ...
208 lines
4.5 KiB
Go
Vendored
208 lines
4.5 KiB
Go
Vendored
package editorconfig
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// findLeftBrackets matches the opening left bracket {.
|
|
findLeftBrackets = regexp.MustCompile(`(^|[^\\])\{`)
|
|
// findDoubleLeftBrackets matches the duplicated opening left bracket {{.
|
|
findDoubleLeftBrackets = regexp.MustCompile(`(^|[^\\])\{\{`)
|
|
// findLeftBrackets matches the closing right bracket {.
|
|
findRightBrackets = regexp.MustCompile(`(^|[^\\])\}`)
|
|
// findDoubleRightBrackets matches the duplicated opening left bracket {{.
|
|
findDoubleRightBrackets = regexp.MustCompile(`(^|[^\\])\}\}`)
|
|
// findNumericRange matches a range of number, e.g. -2..5.
|
|
findNumericRange = regexp.MustCompile(`^([+-]?\d+)\.\.([+-]?\d+)$`)
|
|
)
|
|
|
|
// FnmatchCase tests whether the name matches the given pattern case included.
|
|
func FnmatchCase(pattern, name string) (bool, error) {
|
|
p := translate(pattern)
|
|
|
|
r, err := regexp.Compile(fmt.Sprintf("^%s$", p))
|
|
if err != nil {
|
|
return false, fmt.Errorf("error compiling %q: %w", pattern, err)
|
|
}
|
|
|
|
return r.MatchString(name), nil
|
|
}
|
|
|
|
func translate(pattern string) string { // nolint: funlen,gocognit,gocyclo,cyclop
|
|
index := 0
|
|
pat := []rune(pattern)
|
|
length := len(pat)
|
|
|
|
result := strings.Builder{}
|
|
|
|
braceLevel := 0
|
|
isEscaped := false
|
|
inBrackets := false
|
|
|
|
// 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
|
|
pathSeparator := "/"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
pathSeparator = regexp.QuoteMeta("\\")
|
|
}
|
|
|
|
for index < length {
|
|
r := pat[index]
|
|
index++
|
|
|
|
switch r {
|
|
case '*':
|
|
p := index
|
|
if p < length && pat[p] == '*' {
|
|
result.WriteString(".*")
|
|
index++
|
|
} else {
|
|
result.WriteString(fmt.Sprintf("[^%s]*", pathSeparator))
|
|
}
|
|
case '/':
|
|
p := index
|
|
if p+2 < length && pat[p] == '*' && pat[p+1] == '*' && pat[p+2] == '/' {
|
|
result.WriteString(fmt.Sprintf("(?:%s|%s.*%s)", pathSeparator, pathSeparator, pathSeparator))
|
|
|
|
index += 3
|
|
} else {
|
|
result.WriteRune(r)
|
|
}
|
|
case '?':
|
|
result.WriteString(fmt.Sprintf("[^%s]", pathSeparator))
|
|
case '[':
|
|
if inBrackets { // nolint: nestif
|
|
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
|
|
|
|
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('[')
|
|
}
|
|
}
|
|
}
|
|
case ']':
|
|
if inBrackets && pat[index-2] == '\\' {
|
|
result.WriteString("\\]")
|
|
} else {
|
|
result.WriteRune(r)
|
|
inBrackets = false
|
|
}
|
|
case '{':
|
|
hasComma := false
|
|
p := index
|
|
res := strings.Builder{}
|
|
|
|
for p < length {
|
|
if pat[p] == '}' && pat[p-1] != '\\' {
|
|
break
|
|
}
|
|
|
|
res.WriteRune(pat[p])
|
|
|
|
if pat[p] == ',' && pat[p-1] != '\\' {
|
|
hasComma = true
|
|
|
|
break
|
|
}
|
|
p++
|
|
}
|
|
|
|
switch {
|
|
case !hasComma && p < length:
|
|
inner := res.String()
|
|
|
|
sub := findNumericRange.FindStringSubmatch(inner)
|
|
if len(sub) == 3 {
|
|
from, _ := strconv.Atoi(sub[1])
|
|
to, _ := strconv.Atoi(sub[2])
|
|
|
|
result.WriteString("(?:")
|
|
|
|
// XXX does not scale well
|
|
for i := from; i < to; i++ {
|
|
result.WriteString(strconv.Itoa(i))
|
|
result.WriteRune('|')
|
|
}
|
|
|
|
result.WriteString(strconv.Itoa(to))
|
|
result.WriteRune(')')
|
|
} else {
|
|
r := translate(inner)
|
|
|
|
result.WriteString(fmt.Sprintf("\\{%s\\}", r))
|
|
}
|
|
|
|
index = p + 1
|
|
case matchesBraces:
|
|
result.WriteString("(?:")
|
|
braceLevel++
|
|
default:
|
|
result.WriteString("\\{")
|
|
}
|
|
case '}':
|
|
if braceLevel > 0 {
|
|
if isEscaped {
|
|
result.WriteRune('}')
|
|
|
|
isEscaped = false
|
|
} else {
|
|
result.WriteRune(')')
|
|
|
|
braceLevel--
|
|
}
|
|
} else {
|
|
result.WriteString("\\}")
|
|
}
|
|
case ',':
|
|
if braceLevel == 0 || isEscaped {
|
|
result.WriteRune(r)
|
|
} else {
|
|
result.WriteRune('|')
|
|
}
|
|
default:
|
|
if r != '\\' || isEscaped {
|
|
result.WriteString(regexp.QuoteMeta(string(r)))
|
|
|
|
isEscaped = false
|
|
} else {
|
|
isEscaped = true
|
|
}
|
|
}
|
|
}
|
|
|
|
return result.String()
|
|
}
|