mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
refactor: lazy init global regexp variables (#40269)
Gitea binary is an all-in-one program. It doesn't make sense to compile dozens or even hundreds of regexps for git hook sub commands.
This commit is contained in:
1 parent
6bc92f464a
commit
a165fdb63c
52 files changed
+474
-282
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gitea.dev/modules/log"
|
||||
)
|
||||
@@ -20,7 +21,15 @@ const (
|
||||
|
||||
const escapeRegexpString = "_0[xX](([0-9a-fA-F][0-9a-fA-F])+)_"
|
||||
|
||||
var escapeRegex = regexp.MustCompile(escapeRegexpString)
|
||||
var globalVars = sync.OnceValue(func() (ret struct {
|
||||
escapeRegex *regexp.Regexp
|
||||
extensionReg *regexp.Regexp
|
||||
},
|
||||
) {
|
||||
ret.escapeRegex = regexp.MustCompile(escapeRegexpString)
|
||||
ret.extensionReg = regexp.MustCompile(`^(\.[-\w]+)+$`)
|
||||
return ret
|
||||
})
|
||||
|
||||
func CollectEnvConfigKeys() (keys []string) {
|
||||
for _, env := range os.Environ() {
|
||||
@@ -47,7 +56,7 @@ func ClearEnvConfigKeys() {
|
||||
func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
|
||||
inKey := false
|
||||
last := 0
|
||||
escapeStringIndices := escapeRegex.FindAllStringIndex(encoded, -1)
|
||||
escapeStringIndices := globalVars().escapeRegex.FindAllStringIndex(encoded, -1)
|
||||
for _, unescapeIdx := range escapeStringIndices {
|
||||
preceding := encoded[last:unescapeIdx[0]]
|
||||
if !inKey {
|
||||
|
||||
@@ -6,7 +6,6 @@ package setting
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/modules/util"
|
||||
@@ -222,14 +221,10 @@ func createMarkupSanitizerRule(name string, sec ConfigSection) (MarkupSanitizerR
|
||||
return rule, true
|
||||
}
|
||||
|
||||
var extensionReg = sync.OnceValue(func() *regexp.Regexp {
|
||||
return regexp.MustCompile(`^(\.[-\w]+)+$`)
|
||||
})
|
||||
|
||||
func fileExtensionsToPatterns(sectionName string, extensions []string) []string {
|
||||
patterns := make([]string, 0, len(extensions))
|
||||
for _, extension := range extensions {
|
||||
if !extensionReg().MatchString(extension) {
|
||||
if !globalVars().extensionReg.MatchString(extension) {
|
||||
log.Warn("Config section %s file extension %s is invalid. Extension ignored", sectionName, extension)
|
||||
} else {
|
||||
patterns = append(patterns, "*"+extension)
|
||||
|
||||
Reference in new issue
Block a user