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:
wxiaoguang authored and GitHub committed 2026-11-07 11:56:56 +00:00
1 parent 6bc92f464a
commit a165fdb63c
52 files changed
+474 -282

No files matched your search

+5 -2
View File
@@ -13,6 +13,7 @@ import (
"slices"
"strconv"
"strings"
"sync"
"time"
issues_model "gitea.dev/models/issues"
@@ -35,11 +36,13 @@ const (
secondsByMonth = 4 * secondsByWeek // seconds in a month
)
var reDuration = regexp.MustCompile(`(?i)^(?:(\d+([\.,]\d+)?)(?:mo))?(?:(\d+([\.,]\d+)?)(?:w))?(?:(\d+([\.,]\d+)?)(?:d))?(?:(\d+([\.,]\d+)?)(?:h))?(?:(\d+([\.,]\d+)?)(?:m))?$`)
var reDuration = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`(?i)^(?:(\d+([\.,]\d+)?)(?:mo))?(?:(\d+([\.,]\d+)?)(?:w))?(?:(\d+([\.,]\d+)?)(?:d))?(?:(\d+([\.,]\d+)?)(?:h))?(?:(\d+([\.,]\d+)?)(?:m))?$`)
})
// timeLogToAmount parses time log string and returns amount in seconds
func timeLogToAmount(str string) int64 {
matches := reDuration.FindAllStringSubmatch(str, -1)
matches := reDuration().FindAllStringSubmatch(str, -1)
if len(matches) == 0 {
return 0
}