优化代码

This commit is contained in:
GoEdgeLab
2022-04-04 18:25:54 +08:00
parent 4aeb3cc7b0
commit 2df6e5a51b
4 changed files with 31 additions and 13 deletions

View File

@@ -3,9 +3,11 @@
package re
import (
"github.com/iwind/TeaGo/types"
"regexp"
"regexp/syntax"
"strings"
"sync/atomic"
)
var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x)
@@ -13,6 +15,8 @@ var prefixReg2 = regexp.MustCompile(`^\(\?([\w\s]*:)`) // (?x: ...
var braceZeroReg = regexp.MustCompile(`^{\s*0*\s*}`) // {0}
var braceZeroReg2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x}
var lastId uint64
type Regexp struct {
exp string
rawRegexp *regexp.Regexp
@@ -21,6 +25,9 @@ type Regexp struct {
isCaseInsensitive bool
keywords []string
keywordsMap RuneMap
id uint64
idString string
}
func MustCompile(exp string) *Regexp {
@@ -50,6 +57,9 @@ func NewRegexp(rawRegexp *regexp.Regexp) *Regexp {
}
func (this *Regexp) init() {
this.id = atomic.AddUint64(&lastId, 1)
this.idString = "re:" + types.String(this.id)
if len(this.exp) == 0 {
return
}
@@ -202,6 +212,10 @@ func (this *Regexp) ParseKeywords(exp string) (keywords []string) {
return
}
func (this *Regexp) IdString() string {
return this.idString
}
func (this *Regexp) parseKeyword(subExp string) (result []rune) {
if len(subExp) == 0 {
return nil