mirror of
https://github.com/gogs/gogs.git
synced 2025-05-29 10:42:30 +00:00
all: use lazyregexp (#5911)
* Use lazyregexp * all: fix imports and usages
This commit is contained in:
parent
8b7fa6627f
commit
b28fb90851
@ -63,4 +63,4 @@ func main() {
|
||||
|
||||
os.Exit(code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ package db
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
@ -21,6 +20,7 @@ import (
|
||||
api "github.com/gogs/go-gogs-client"
|
||||
|
||||
"gogs.io/gogs/internal/db/errors"
|
||||
"gogs.io/gogs/internal/lazyregexp"
|
||||
"gogs.io/gogs/internal/setting"
|
||||
"gogs.io/gogs/internal/tool"
|
||||
)
|
||||
@ -58,9 +58,9 @@ var (
|
||||
IssueCloseKeywords = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"}
|
||||
IssueReopenKeywords = []string{"reopen", "reopens", "reopened"}
|
||||
|
||||
IssueCloseKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(IssueCloseKeywords))
|
||||
IssueReopenKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(IssueReopenKeywords))
|
||||
IssueReferenceKeywordsPat = regexp.MustCompile(`(?i)(?:)(^| )\S+`)
|
||||
IssueCloseKeywordsPat = lazyregexp.New(assembleKeywordsPattern(IssueCloseKeywords))
|
||||
IssueReopenKeywordsPat = lazyregexp.New(assembleKeywordsPattern(IssueReopenKeywords))
|
||||
IssueReferenceKeywordsPat = lazyregexp.New(`(?i)(?:)(^| )\S+`)
|
||||
)
|
||||
|
||||
func assembleKeywordsPattern(words []string) string {
|
||||
|
@ -7,7 +7,6 @@ package db
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@ -15,10 +14,11 @@ import (
|
||||
|
||||
api "github.com/gogs/go-gogs-client"
|
||||
|
||||
"gogs.io/gogs/internal/lazyregexp"
|
||||
"gogs.io/gogs/internal/tool"
|
||||
)
|
||||
|
||||
var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})")
|
||||
var labelColorPattern = lazyregexp.New("#([a-fA-F0-9]{6})")
|
||||
|
||||
// GetLabelTemplateFile loads the label template file by given name,
|
||||
// then parses and returns a list of name-color pairs.
|
||||
|
@ -7,17 +7,18 @@ package form
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/unknwon/com"
|
||||
"github.com/go-macaron/binding"
|
||||
"github.com/unknwon/com"
|
||||
"gopkg.in/macaron.v1"
|
||||
|
||||
"gogs.io/gogs/internal/lazyregexp"
|
||||
)
|
||||
|
||||
const ERR_ALPHA_DASH_DOT_SLASH = "AlphaDashDotSlashError"
|
||||
|
||||
var AlphaDashDotSlashPattern = regexp.MustCompile("[^\\d\\w-_\\./]")
|
||||
var AlphaDashDotSlashPattern = lazyregexp.New("[^\\d\\w-_\\./]")
|
||||
|
||||
func init() {
|
||||
binding.SetNameMapper(com.ToSnakeCase)
|
||||
|
29
internal/lazyregexp/NOTICE
Normal file
29
internal/lazyregexp/NOTICE
Normal file
@ -0,0 +1,29 @@
|
||||
Code extended from https://raw.githubusercontent.com/golang/go/go1.13.3/src/internal/lazyregexp/lazyre.go
|
||||
|
||||
Copyright (c) 2019 The Go Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
114
internal/lazyregexp/lazyre.go
Normal file
114
internal/lazyregexp/lazyre.go
Normal file
@ -0,0 +1,114 @@
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package lazyregexp is a thin wrapper over regexp, allowing the use of global
|
||||
// regexp variables without forcing them to be compiled at init.
|
||||
package lazyregexp
|
||||
|
||||
import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Regexp is a wrapper around regexp.Regexp, where the underlying regexp will be
|
||||
// compiled the first time it is needed.
|
||||
type Regexp struct {
|
||||
str string
|
||||
once sync.Once
|
||||
rx *regexp.Regexp
|
||||
}
|
||||
|
||||
func (r *Regexp) Regexp() *regexp.Regexp {
|
||||
r.once.Do(r.build)
|
||||
return r.rx
|
||||
}
|
||||
|
||||
func (r *Regexp) build() {
|
||||
r.rx = regexp.MustCompile(r.str)
|
||||
r.str = ""
|
||||
}
|
||||
|
||||
func (r *Regexp) Find(b []byte) []byte {
|
||||
return r.Regexp().Find(b)
|
||||
}
|
||||
|
||||
func (r *Regexp) FindSubmatch(s []byte) [][]byte {
|
||||
return r.Regexp().FindSubmatch(s)
|
||||
}
|
||||
|
||||
func (r *Regexp) FindStringSubmatch(s string) []string {
|
||||
return r.Regexp().FindStringSubmatch(s)
|
||||
}
|
||||
|
||||
func (r *Regexp) FindStringSubmatchIndex(s string) []int {
|
||||
return r.Regexp().FindStringSubmatchIndex(s)
|
||||
}
|
||||
|
||||
func (r *Regexp) ReplaceAllString(src, repl string) string {
|
||||
return r.Regexp().ReplaceAllString(src, repl)
|
||||
}
|
||||
|
||||
func (r *Regexp) FindString(s string) string {
|
||||
return r.Regexp().FindString(s)
|
||||
}
|
||||
|
||||
func (r *Regexp) FindAll(b []byte, n int) [][]byte {
|
||||
return r.Regexp().FindAll(b, n)
|
||||
}
|
||||
|
||||
func (r *Regexp) FindAllString(s string, n int) []string {
|
||||
return r.Regexp().FindAllString(s, n)
|
||||
}
|
||||
|
||||
func (r *Regexp) MatchString(s string) bool {
|
||||
return r.Regexp().MatchString(s)
|
||||
}
|
||||
|
||||
func (r *Regexp) SubexpNames() []string {
|
||||
return r.Regexp().SubexpNames()
|
||||
}
|
||||
|
||||
func (r *Regexp) FindAllStringSubmatch(s string, n int) [][]string {
|
||||
return r.Regexp().FindAllStringSubmatch(s, n)
|
||||
}
|
||||
|
||||
func (r *Regexp) Split(s string, n int) []string {
|
||||
return r.Regexp().Split(s, n)
|
||||
}
|
||||
|
||||
func (r *Regexp) ReplaceAllLiteralString(src, repl string) string {
|
||||
return r.Regexp().ReplaceAllLiteralString(src, repl)
|
||||
}
|
||||
|
||||
func (r *Regexp) FindAllIndex(b []byte, n int) [][]int {
|
||||
return r.Regexp().FindAllIndex(b, n)
|
||||
}
|
||||
|
||||
func (r *Regexp) Match(b []byte) bool {
|
||||
return r.Regexp().Match(b)
|
||||
}
|
||||
|
||||
func (r *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string {
|
||||
return r.Regexp().ReplaceAllStringFunc(src, repl)
|
||||
}
|
||||
|
||||
func (r *Regexp) ReplaceAll(src, repl []byte) []byte {
|
||||
return r.Regexp().ReplaceAll(src, repl)
|
||||
}
|
||||
|
||||
var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test")
|
||||
|
||||
// New creates a new lazy regexp, delaying the compiling work until it is first
|
||||
// needed. If the code is being run as part of tests, the regexp compiling will
|
||||
// happen immediately.
|
||||
func New(str string) *Regexp {
|
||||
lr := &Regexp{str: str}
|
||||
if inTest {
|
||||
// In tests, always compile the regexps early.
|
||||
lr.Regexp()
|
||||
}
|
||||
return lr
|
||||
}
|
@ -9,11 +9,11 @@ import (
|
||||
"fmt"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/russross/blackfriday"
|
||||
|
||||
"gogs.io/gogs/internal/lazyregexp"
|
||||
"gogs.io/gogs/internal/setting"
|
||||
"gogs.io/gogs/internal/tool"
|
||||
)
|
||||
@ -35,7 +35,7 @@ type MarkdownRenderer struct {
|
||||
urlPrefix string
|
||||
}
|
||||
|
||||
var validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://|^mailto:`)
|
||||
var validLinksPattern = lazyregexp.New(`^[a-z][\w-]+://|^mailto:`)
|
||||
|
||||
// isLink reports whether link fits valid format.
|
||||
func isLink(link []byte) bool {
|
||||
|
@ -8,12 +8,12 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/unknwon/com"
|
||||
"golang.org/x/net/html"
|
||||
|
||||
"gogs.io/gogs/internal/lazyregexp"
|
||||
"gogs.io/gogs/internal/setting"
|
||||
"gogs.io/gogs/internal/tool"
|
||||
)
|
||||
@ -35,26 +35,26 @@ const (
|
||||
|
||||
var (
|
||||
// MentionPattern matches string that mentions someone, e.g. @Unknwon
|
||||
MentionPattern = regexp.MustCompile(`(\s|^|\W)@[0-9a-zA-Z-_\.]+`)
|
||||
MentionPattern = lazyregexp.New(`(\s|^|\W)@[0-9a-zA-Z-_\.]+`)
|
||||
|
||||
// CommitPattern matches link to certain commit with or without trailing hash,
|
||||
// e.g. https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2
|
||||
CommitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`)
|
||||
CommitPattern = lazyregexp.New(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`)
|
||||
|
||||
// IssueFullPattern matches link to an issue with or without trailing hash,
|
||||
// e.g. https://try.gogs.io/gogs/gogs/issues/4#issue-685
|
||||
IssueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`)
|
||||
IssueFullPattern = lazyregexp.New(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`)
|
||||
// IssueNumericPattern matches string that references to a numeric issue, e.g. #1287
|
||||
IssueNumericPattern = regexp.MustCompile(`( |^|\(|\[)#[0-9]+\b`)
|
||||
IssueNumericPattern = lazyregexp.New(`( |^|\(|\[)#[0-9]+\b`)
|
||||
// IssueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
|
||||
IssueAlphanumericPattern = regexp.MustCompile(`( |^|\(|\[)[A-Z]{1,10}-[1-9][0-9]*\b`)
|
||||
IssueAlphanumericPattern = lazyregexp.New(`( |^|\(|\[)[A-Z]{1,10}-[1-9][0-9]*\b`)
|
||||
// CrossReferenceIssueNumericPattern matches string that references a numeric issue in a difference repository
|
||||
// e.g. gogs/gogs#12345
|
||||
CrossReferenceIssueNumericPattern = regexp.MustCompile(`( |^)[0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+\b`)
|
||||
CrossReferenceIssueNumericPattern = lazyregexp.New(`( |^)[0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+\b`)
|
||||
|
||||
// Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
|
||||
// FIXME: this pattern matches pure numbers as well, right now we do a hack to check in RenderSha1CurrentPattern by converting string to a number.
|
||||
Sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{7,40}\b`)
|
||||
Sha1CurrentPattern = lazyregexp.New(`\b[0-9a-f]{7,40}\b`)
|
||||
)
|
||||
|
||||
// FindAllMentions matches mention patterns in given content
|
||||
|
@ -5,11 +5,11 @@
|
||||
package markup
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"sync"
|
||||
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
|
||||
"gogs.io/gogs/internal/lazyregexp"
|
||||
"gogs.io/gogs/internal/setting"
|
||||
)
|
||||
|
||||
@ -30,10 +30,10 @@ var sanitizer = &Sanitizer{
|
||||
func NewSanitizer() {
|
||||
sanitizer.init.Do(func() {
|
||||
// We only want to allow HighlightJS specific classes for code blocks
|
||||
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^language-\w+$`)).OnElements("code")
|
||||
sanitizer.policy.AllowAttrs("class").Matching(lazyregexp.New(`^language-\w+$`).Regexp()).OnElements("code")
|
||||
|
||||
// Checkboxes
|
||||
sanitizer.policy.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
|
||||
sanitizer.policy.AllowAttrs("type").Matching(lazyregexp.New(`^checkbox$`).Regexp()).OnElements("input")
|
||||
sanitizer.policy.AllowAttrs("checked", "disabled").OnElements("input")
|
||||
|
||||
// Data URLs
|
||||
|
@ -12,7 +12,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -23,6 +22,7 @@ import (
|
||||
"gogs.io/gogs/internal/context"
|
||||
"gogs.io/gogs/internal/db"
|
||||
"gogs.io/gogs/internal/db/errors"
|
||||
"gogs.io/gogs/internal/lazyregexp"
|
||||
"gogs.io/gogs/internal/setting"
|
||||
"gogs.io/gogs/internal/tool"
|
||||
)
|
||||
@ -346,21 +346,21 @@ func getIdxFile(h serviceHandler) {
|
||||
}
|
||||
|
||||
var routes = []struct {
|
||||
reg *regexp.Regexp
|
||||
re *lazyregexp.Regexp
|
||||
method string
|
||||
handler func(serviceHandler)
|
||||
}{
|
||||
{regexp.MustCompile("(.*?)/git-upload-pack$"), "POST", serviceUploadPack},
|
||||
{regexp.MustCompile("(.*?)/git-receive-pack$"), "POST", serviceReceivePack},
|
||||
{regexp.MustCompile("(.*?)/info/refs$"), "GET", getInfoRefs},
|
||||
{regexp.MustCompile("(.*?)/HEAD$"), "GET", getTextFile},
|
||||
{regexp.MustCompile("(.*?)/objects/info/alternates$"), "GET", getTextFile},
|
||||
{regexp.MustCompile("(.*?)/objects/info/http-alternates$"), "GET", getTextFile},
|
||||
{regexp.MustCompile("(.*?)/objects/info/packs$"), "GET", getInfoPacks},
|
||||
{regexp.MustCompile("(.*?)/objects/info/[^/]*$"), "GET", getTextFile},
|
||||
{regexp.MustCompile("(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$"), "GET", getLooseObject},
|
||||
{regexp.MustCompile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.pack$"), "GET", getPackFile},
|
||||
{regexp.MustCompile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.idx$"), "GET", getIdxFile},
|
||||
{lazyregexp.New("(.*?)/git-upload-pack$"), "POST", serviceUploadPack},
|
||||
{lazyregexp.New("(.*?)/git-receive-pack$"), "POST", serviceReceivePack},
|
||||
{lazyregexp.New("(.*?)/info/refs$"), "GET", getInfoRefs},
|
||||
{lazyregexp.New("(.*?)/HEAD$"), "GET", getTextFile},
|
||||
{lazyregexp.New("(.*?)/objects/info/alternates$"), "GET", getTextFile},
|
||||
{lazyregexp.New("(.*?)/objects/info/http-alternates$"), "GET", getTextFile},
|
||||
{lazyregexp.New("(.*?)/objects/info/packs$"), "GET", getInfoPacks},
|
||||
{lazyregexp.New("(.*?)/objects/info/[^/]*$"), "GET", getTextFile},
|
||||
{lazyregexp.New("(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$"), "GET", getLooseObject},
|
||||
{lazyregexp.New("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.pack$"), "GET", getPackFile},
|
||||
{lazyregexp.New("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.idx$"), "GET", getIdxFile},
|
||||
}
|
||||
|
||||
func getGitRepoPath(dir string) (string, error) {
|
||||
@ -379,7 +379,7 @@ func getGitRepoPath(dir string) (string, error) {
|
||||
func HTTP(c *HTTPContext) {
|
||||
for _, route := range routes {
|
||||
reqPath := strings.ToLower(c.Req.URL.Path)
|
||||
m := route.reg.FindStringSubmatch(reqPath)
|
||||
m := route.re.FindStringSubmatch(reqPath)
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user