Skip to content

Commit eda8dd5

Browse files
authored
replace TrimFunc(s, IsSpace) with TrimSpace for ASCII optimization (#1663)
1 parent 03413ea commit eda8dd5

File tree

7 files changed

+13
-18
lines changed

7 files changed

+13
-18
lines changed

dnscrypt-proxy/coldstart.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net"
66
"strings"
77
"time"
8-
"unicode"
98

109
"github.com/jedisct1/dlog"
1110
"github.com/miekg/dns"
@@ -163,7 +162,7 @@ func ColdStart(proxy *Proxy) (*CaptivePortalHandler, error) {
163162
}
164163
var ips []net.IP
165164
for _, ip := range strings.Split(ipsStr, ",") {
166-
ipStr := strings.TrimFunc(ip, unicode.IsSpace)
165+
ipStr := strings.TrimSpace(ip)
167166
if ip := net.ParseIP(ipStr); ip != nil {
168167
ips = append(ips, ip)
169168
} else {

dnscrypt-proxy/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func StringTwoFields(str string) (string, string, bool) {
126126
if pos == -1 {
127127
return "", "", false
128128
}
129-
a, b := strings.TrimFunc(str[:pos], unicode.IsSpace), strings.TrimFunc(str[pos+1:], unicode.IsSpace)
129+
a, b := strings.TrimSpace(str[:pos]), strings.TrimSpace(str[pos+1:])
130130
if len(a) == 0 || len(b) == 0 {
131131
return a, b, false
132132
}
@@ -156,7 +156,7 @@ func TrimAndStripInlineComments(str string) string {
156156
str = str[:idx-1]
157157
}
158158
}
159-
return strings.TrimFunc(str, unicode.IsSpace)
159+
return strings.TrimSpace(str)
160160
}
161161

162162
func ExtractHostAndPort(str string, defaultPort int) (host string, port int) {

dnscrypt-proxy/plugin_allow_name.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net"
88
"strings"
99
"time"
10-
"unicode"
1110

1211
"github.com/jedisct1/dlog"
1312
"github.com/miekg/dns"
@@ -44,8 +43,8 @@ func (plugin *PluginAllowName) Init(proxy *Proxy) error {
4443
parts := strings.Split(line, "@")
4544
timeRangeName := ""
4645
if len(parts) == 2 {
47-
line = strings.TrimFunc(parts[0], unicode.IsSpace)
48-
timeRangeName = strings.TrimFunc(parts[1], unicode.IsSpace)
46+
line = strings.TrimSpace(parts[0])
47+
timeRangeName = strings.TrimSpace(parts[1])
4948
} else if len(parts) > 2 {
5049
dlog.Errorf("Syntax error in allowed names at line %d -- Unexpected @ character", 1+lineNo)
5150
continue

dnscrypt-proxy/plugin_block_name.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net"
88
"strings"
99
"time"
10-
"unicode"
1110

1211
"github.com/jedisct1/dlog"
1312
"github.com/miekg/dns"
@@ -101,8 +100,8 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
101100
parts := strings.Split(line, "@")
102101
timeRangeName := ""
103102
if len(parts) == 2 {
104-
line = strings.TrimFunc(parts[0], unicode.IsSpace)
105-
timeRangeName = strings.TrimFunc(parts[1], unicode.IsSpace)
103+
line = strings.TrimSpace(parts[0])
104+
timeRangeName = strings.TrimSpace(parts[1])
106105
} else if len(parts) > 2 {
107106
dlog.Errorf("Syntax error in block rules at line %d -- Unexpected @ character", 1+lineNo)
108107
continue

dnscrypt-proxy/plugin_cloak.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
5252
var target string
5353
parts := strings.FieldsFunc(line, unicode.IsSpace)
5454
if len(parts) == 2 {
55-
line = strings.TrimFunc(parts[0], unicode.IsSpace)
56-
target = strings.TrimFunc(parts[1], unicode.IsSpace)
55+
line = strings.TrimSpace(parts[0])
56+
target = strings.TrimSpace(parts[1])
5757
} else if len(parts) > 2 {
5858
dlog.Errorf("Syntax error in cloaking rules at line %d -- Unexpected space character", 1+lineNo)
5959
continue

dnscrypt-proxy/plugin_forward.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"math/rand"
66
"net"
77
"strings"
8-
"unicode"
98

109
"github.com/jedisct1/dlog"
1110
"github.com/miekg/dns"
@@ -49,7 +48,7 @@ func (plugin *PluginForward) Init(proxy *Proxy) error {
4948
domain = strings.ToLower(domain)
5049
var servers []string
5150
for _, server := range strings.Split(serversStr, ",") {
52-
server = strings.TrimFunc(server, unicode.IsSpace)
51+
server = strings.TrimSpace(server)
5352
if net.ParseIP(server) != nil {
5453
server = fmt.Sprintf("%s:%d", server, 53)
5554
}

dnscrypt-proxy/sources.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"path/filepath"
1111
"strings"
1212
"time"
13-
"unicode"
1413

1514
"github.com/dchest/safefile"
1615

@@ -247,12 +246,12 @@ func (source *Source) parseV2() ([]RegisteredServer, error) {
247246
}
248247
parts = parts[1:]
249248
for _, part := range parts {
250-
part = strings.TrimFunc(part, unicode.IsSpace)
249+
part = strings.TrimSpace(part)
251250
subparts := strings.Split(part, "\n")
252251
if len(subparts) < 2 {
253252
return registeredServers, fmt.Errorf("Invalid format for source at [%v]", source.urls)
254253
}
255-
name := strings.TrimFunc(subparts[0], unicode.IsSpace)
254+
name := strings.TrimSpace(subparts[0])
256255
if len(name) == 0 {
257256
return registeredServers, fmt.Errorf("Invalid format for source at [%v]", source.urls)
258257
}
@@ -261,7 +260,7 @@ func (source *Source) parseV2() ([]RegisteredServer, error) {
261260
var stampStr, description string
262261
stampStrs := make([]string, 0)
263262
for _, subpart := range subparts {
264-
subpart = strings.TrimFunc(subpart, unicode.IsSpace)
263+
subpart = strings.TrimSpace(subpart)
265264
if strings.HasPrefix(subpart, "sdns:") && len(subpart) >= 6 {
266265
stampStrs = append(stampStrs, subpart)
267266
continue

0 commit comments

Comments
 (0)