Skip to content

Commit

Permalink
replace std regex with regexp2
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Jun 27, 2023
1 parent 8633337 commit 8417128
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
10 changes: 6 additions & 4 deletions adapter/outboundgroup/common.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package outboundgroup

import (
"regexp"
"strings"
"time"

C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/constant/provider"
"github.com/Dreamacro/clash/tunnel"

"github.com/dlclark/regexp2"
)

const (
Expand All @@ -30,9 +31,9 @@ func getProvidersProxies(providers []provider.ProxyProvider, touch bool, filter
}
matchedProxies := []C.Proxy{}
if len(filter) > 0 {
var filterRegs []*regexp.Regexp
var filterRegs []*regexp2.Regexp
for _, filter := range strings.Split(filter, "`") {
filterReg, err := regexp.Compile(filter)
filterReg, err := regexp2.Compile(filter, regexp2.None)
if err != nil {
continue
}
Expand All @@ -41,7 +42,8 @@ func getProvidersProxies(providers []provider.ProxyProvider, touch bool, filter
proxiesSet := map[string]struct{}{}
for _, filterReg := range filterRegs {
for _, p := range proxies {
if name := p.Name(); filterReg.MatchString(name) {
name := p.Name()
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
if _, ok := proxiesSet[name]; !ok {
proxiesSet[name] = struct{}{}
matchedProxies = append(matchedProxies, p)
Expand Down
20 changes: 12 additions & 8 deletions adapter/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"runtime"
"strings"
"time"
Expand All @@ -14,6 +13,7 @@ import (
types "github.com/Dreamacro/clash/constant/provider"
"github.com/Dreamacro/clash/tunnel/statistic"

"github.com/dlclark/regexp2"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -110,13 +110,13 @@ func stopProxyProvider(pd *ProxySetProvider) {
}

func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, dialerProxy string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
excludeFilterReg, err := regexp.Compile(excludeFilter)
excludeFilterReg, err := regexp2.Compile(excludeFilter, regexp2.None)
if err != nil {
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
}
var filterRegs []*regexp.Regexp
var filterRegs []*regexp2.Regexp
for _, filter := range strings.Split(filter, "`") {
filterReg, err := regexp.Compile(filter)
filterReg, err := regexp2.Compile(filter, regexp2.None)
if err != nil {
return nil, fmt.Errorf("invalid filter regex: %w", err)
}
Expand Down Expand Up @@ -160,11 +160,15 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc
if !ok {
continue
}
if len(excludeFilter) > 0 && excludeFilterReg.MatchString(name) {
continue
if len(excludeFilter) > 0 {
if mat, _ := excludeFilterReg.FindStringMatch(name); mat != nil {
continue
}
}
if !filterReg.MatchString(name) {
continue
if len(filter) > 0 {
if mat, _ := filterReg.FindStringMatch(name); mat == nil {
continue
}
}
if _, ok := proxiesSet[name]; ok {
continue
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/3andne/restls-client-go v0.1.4
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da
github.com/dlclark/regexp2 v1.10.0
github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/cors v1.2.1
github.com/go-chi/render v1.0.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/ericlagergren/aegis v0.0.0-20230312195928-b4ce538b56f9 h1:/5RkVc9Rc81XmMyVqawCiDyrBHZbLAZgTTCqou4mwj8=
github.com/ericlagergren/aegis v0.0.0-20230312195928-b4ce538b56f9/go.mod h1:hkIFzoiIPZYxdFOOLyDho59b7SrDfo+w3h+yWdlg45I=
github.com/ericlagergren/polyval v0.0.0-20220411101811-e25bc10ba391 h1:8j2RH289RJplhA6WfdaPqzg1MjH2K8wX5e0uhAxrw2g=
Expand Down

0 comments on commit 8417128

Please sign in to comment.