replace TrimFunc(s, IsSpace) with TrimSpace for ASCII optimization (#1663)

This commit is contained in:
Alison Winters 2021-04-05 02:46:57 -07:00 committed by GitHub
parent 03413eae2f
commit eda8dd5181
Failed to generate hash of commit
7 changed files with 13 additions and 18 deletions

View file

@ -5,7 +5,6 @@ import (
"net" "net"
"strings" "strings"
"time" "time"
"unicode"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -163,7 +162,7 @@ func ColdStart(proxy *Proxy) (*CaptivePortalHandler, error) {
} }
var ips []net.IP var ips []net.IP
for _, ip := range strings.Split(ipsStr, ",") { for _, ip := range strings.Split(ipsStr, ",") {
ipStr := strings.TrimFunc(ip, unicode.IsSpace) ipStr := strings.TrimSpace(ip)
if ip := net.ParseIP(ipStr); ip != nil { if ip := net.ParseIP(ipStr); ip != nil {
ips = append(ips, ip) ips = append(ips, ip)
} else { } else {

View file

@ -126,7 +126,7 @@ func StringTwoFields(str string) (string, string, bool) {
if pos == -1 { if pos == -1 {
return "", "", false return "", "", false
} }
a, b := strings.TrimFunc(str[:pos], unicode.IsSpace), strings.TrimFunc(str[pos+1:], unicode.IsSpace) a, b := strings.TrimSpace(str[:pos]), strings.TrimSpace(str[pos+1:])
if len(a) == 0 || len(b) == 0 { if len(a) == 0 || len(b) == 0 {
return a, b, false return a, b, false
} }
@ -156,7 +156,7 @@ func TrimAndStripInlineComments(str string) string {
str = str[:idx-1] str = str[:idx-1]
} }
} }
return strings.TrimFunc(str, unicode.IsSpace) return strings.TrimSpace(str)
} }
func ExtractHostAndPort(str string, defaultPort int) (host string, port int) { func ExtractHostAndPort(str string, defaultPort int) (host string, port int) {

View file

@ -7,7 +7,6 @@ import (
"net" "net"
"strings" "strings"
"time" "time"
"unicode"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -44,8 +43,8 @@ func (plugin *PluginAllowName) Init(proxy *Proxy) error {
parts := strings.Split(line, "@") parts := strings.Split(line, "@")
timeRangeName := "" timeRangeName := ""
if len(parts) == 2 { if len(parts) == 2 {
line = strings.TrimFunc(parts[0], unicode.IsSpace) line = strings.TrimSpace(parts[0])
timeRangeName = strings.TrimFunc(parts[1], unicode.IsSpace) timeRangeName = strings.TrimSpace(parts[1])
} else if len(parts) > 2 { } else if len(parts) > 2 {
dlog.Errorf("Syntax error in allowed names at line %d -- Unexpected @ character", 1+lineNo) dlog.Errorf("Syntax error in allowed names at line %d -- Unexpected @ character", 1+lineNo)
continue continue

View file

@ -7,7 +7,6 @@ import (
"net" "net"
"strings" "strings"
"time" "time"
"unicode"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -101,8 +100,8 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
parts := strings.Split(line, "@") parts := strings.Split(line, "@")
timeRangeName := "" timeRangeName := ""
if len(parts) == 2 { if len(parts) == 2 {
line = strings.TrimFunc(parts[0], unicode.IsSpace) line = strings.TrimSpace(parts[0])
timeRangeName = strings.TrimFunc(parts[1], unicode.IsSpace) timeRangeName = strings.TrimSpace(parts[1])
} else if len(parts) > 2 { } else if len(parts) > 2 {
dlog.Errorf("Syntax error in block rules at line %d -- Unexpected @ character", 1+lineNo) dlog.Errorf("Syntax error in block rules at line %d -- Unexpected @ character", 1+lineNo)
continue continue

View file

@ -52,8 +52,8 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
var target string var target string
parts := strings.FieldsFunc(line, unicode.IsSpace) parts := strings.FieldsFunc(line, unicode.IsSpace)
if len(parts) == 2 { if len(parts) == 2 {
line = strings.TrimFunc(parts[0], unicode.IsSpace) line = strings.TrimSpace(parts[0])
target = strings.TrimFunc(parts[1], unicode.IsSpace) target = strings.TrimSpace(parts[1])
} else if len(parts) > 2 { } else if len(parts) > 2 {
dlog.Errorf("Syntax error in cloaking rules at line %d -- Unexpected space character", 1+lineNo) dlog.Errorf("Syntax error in cloaking rules at line %d -- Unexpected space character", 1+lineNo)
continue continue

View file

@ -5,7 +5,6 @@ import (
"math/rand" "math/rand"
"net" "net"
"strings" "strings"
"unicode"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -49,7 +48,7 @@ func (plugin *PluginForward) Init(proxy *Proxy) error {
domain = strings.ToLower(domain) domain = strings.ToLower(domain)
var servers []string var servers []string
for _, server := range strings.Split(serversStr, ",") { for _, server := range strings.Split(serversStr, ",") {
server = strings.TrimFunc(server, unicode.IsSpace) server = strings.TrimSpace(server)
if net.ParseIP(server) != nil { if net.ParseIP(server) != nil {
server = fmt.Sprintf("%s:%d", server, 53) server = fmt.Sprintf("%s:%d", server, 53)
} }

View file

@ -10,7 +10,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"unicode"
"github.com/dchest/safefile" "github.com/dchest/safefile"
@ -247,12 +246,12 @@ func (source *Source) parseV2() ([]RegisteredServer, error) {
} }
parts = parts[1:] parts = parts[1:]
for _, part := range parts { for _, part := range parts {
part = strings.TrimFunc(part, unicode.IsSpace) part = strings.TrimSpace(part)
subparts := strings.Split(part, "\n") subparts := strings.Split(part, "\n")
if len(subparts) < 2 { if len(subparts) < 2 {
return registeredServers, fmt.Errorf("Invalid format for source at [%v]", source.urls) return registeredServers, fmt.Errorf("Invalid format for source at [%v]", source.urls)
} }
name := strings.TrimFunc(subparts[0], unicode.IsSpace) name := strings.TrimSpace(subparts[0])
if len(name) == 0 { if len(name) == 0 {
return registeredServers, fmt.Errorf("Invalid format for source at [%v]", source.urls) return registeredServers, fmt.Errorf("Invalid format for source at [%v]", source.urls)
} }
@ -261,7 +260,7 @@ func (source *Source) parseV2() ([]RegisteredServer, error) {
var stampStr, description string var stampStr, description string
stampStrs := make([]string, 0) stampStrs := make([]string, 0)
for _, subpart := range subparts { for _, subpart := range subparts {
subpart = strings.TrimFunc(subpart, unicode.IsSpace) subpart = strings.TrimSpace(subpart)
if strings.HasPrefix(subpart, "sdns:") && len(subpart) >= 6 { if strings.HasPrefix(subpart, "sdns:") && len(subpart) >= 6 {
stampStrs = append(stampStrs, subpart) stampStrs = append(stampStrs, subpart)
continue continue