Skip to content

Commit d3a6b06

Browse files
authored
perf: replace strings.Split with strings.SplitSeq (#2441)
1 parent e87e028 commit d3a6b06

15 files changed

Lines changed: 43 additions & 52 deletions

File tree

drivers/lenovonas_share/driver.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
stdpath "path"
89
"strings"
910
"time"
1011

@@ -32,6 +33,7 @@ func (d *LenovoNasShare) GetAddition() driver.Additional {
3233
}
3334

3435
func (d *LenovoNasShare) Init(ctx context.Context) error {
36+
d.ShareId = stdpath.Base(d.ShareId)
3537
if err := d.getStoken(); err != nil {
3638
return err
3739
}
@@ -98,9 +100,6 @@ func (d *LenovoNasShare) getStoken() error { // 获取stoken
98100
d.Host = "https://siot-share.lenovo.com.cn"
99101
}
100102

101-
parts := strings.Split(d.ShareId, "/")
102-
d.ShareId = parts[len(parts)-1]
103-
104103
query := map[string]string{
105104
"code": d.ShareId,
106105
"password": d.SharePwd,

drivers/strm/driver.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (d *Strm) Init(ctx context.Context) error {
4545
return errors.New("SaveStrmLocalPath is required")
4646
}
4747
d.pathMap = make(map[string][]string)
48-
for _, path := range strings.Split(d.Paths, "\n") {
48+
for path := range strings.SplitSeq(d.Paths, "\n") {
4949
path = strings.TrimSpace(path)
5050
if path == "" {
5151
continue
@@ -97,17 +97,17 @@ func (d *Strm) Init(ctx context.Context) error {
9797
}
9898

9999
if d.Version != 5 {
100-
types := strings.Split("mp4,mkv,flv,avi,wmv,ts,rmvb,webm,mp3,flac,aac,wav,ogg,m4a,wma,alac", ",")
101-
for _, ext := range types {
100+
types := strings.SplitSeq("mp4,mkv,flv,avi,wmv,ts,rmvb,webm,mp3,flac,aac,wav,ogg,m4a,wma,alac", ",")
101+
for ext := range types {
102102
if _, ok := d.supportSuffix[ext]; !ok {
103103
d.supportSuffix[ext] = struct{}{}
104104
supportTypes = append(supportTypes, ext)
105105
}
106106
}
107107
d.FilterFileTypes = strings.Join(supportTypes, ",")
108108

109-
types = strings.Split("ass,srt,vtt,sub,strm", ",")
110-
for _, ext := range types {
109+
types = strings.SplitSeq("ass,srt,vtt,sub,strm", ",")
110+
for ext := range types {
111111
if _, ok := d.downloadSuffix[ext]; !ok {
112112
d.downloadSuffix[ext] = struct{}{}
113113
downloadTypes = append(downloadTypes, ext)
@@ -127,7 +127,7 @@ func (d *Strm) Drop(ctx context.Context) error {
127127
d.pathMap = nil
128128
d.downloadSuffix = nil
129129
d.supportSuffix = nil
130-
for _, path := range strings.Split(d.Paths, "\n") {
130+
for path := range strings.SplitSeq(d.Paths, "\n") {
131131
RemoveStrm(utils.FixAndCleanPath(strings.TrimSpace(path)), d)
132132
}
133133
return nil

drivers/url_tree/util.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ import (
3434
*/
3535
// if there are no name, use the last segment of url as name
3636
func BuildTree(text string, headSize bool) (*Node, error) {
37-
lines := strings.Split(text, "\n")
3837
var root = &Node{Level: -1, Name: "root"}
3938
stack := []*Node{root}
40-
for _, line := range lines {
39+
for line := range strings.SplitSeq(text, "\n") {
4140
// calculate indent
4241
indent := 0
4342
for i := 0; i < len(line); i++ {
@@ -153,9 +152,7 @@ func splitPath(path string) []string {
153152
if path == "/" {
154153
return []string{"root"}
155154
}
156-
if strings.HasSuffix(path, "/") {
157-
path = path[:len(path)-1]
158-
}
155+
path = strings.TrimSuffix(path, "/")
159156
parts := strings.Split(path, "/")
160157
parts[0] = "root"
161158
return parts

drivers/webdav/odrvcookie/fetch.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ func getLoginUrl(endpoint string) (string, error) {
151151
if err != nil {
152152
return "", err
153153
}
154-
domains := strings.Split(spRoot.Host, ".")
155-
tld := domains[len(domains)-1]
154+
tld := spRoot.Host[strings.LastIndex(spRoot.Host, ".")+1:]
156155
loginUrl, ok := loginUrlsMap[tld]
157156
if !ok {
158157
return "", fmt.Errorf("tld %s is not supported", tld)

internal/archive/iso9660/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func getObj(img *iso9660.Image, path string) (*iso9660.File, error) {
2929
if path == "/" {
3030
return obj, nil
3131
}
32-
paths := strings.Split(strings.TrimPrefix(path, "/"), "/")
33-
for _, p := range paths {
32+
paths := strings.SplitSeq(strings.TrimPrefix(path, "/"), "/")
33+
for p := range paths {
3434
if !obj.IsDir() {
3535
return nil, errs.ObjectNotFound
3636
}

internal/net/request.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
stdpath "path"
910
"strconv"
10-
"strings"
1111
"sync"
1212
"time"
1313

@@ -490,30 +490,27 @@ func (d *downloader) checkTotalBytes(resp *http.Response) error {
490490
totalBytes = resp.ContentLength
491491
}
492492
} else {
493-
parts := strings.Split(contentRange, "/")
494-
495-
total := int64(-1)
496493

497494
// Checking for whether a numbered total exists
498495
// If one does not exist, we will assume the total to be -1, undefined,
499496
// and sequentially download each chunk until hitting a 416 error
500-
totalStr := parts[len(parts)-1]
497+
498+
totalStr := stdpath.Base(contentRange)
501499
if totalStr != "*" {
502-
total, err = strconv.ParseInt(totalStr, 10, 64)
503-
if err != nil {
504-
err = fmt.Errorf("failed extracting file size")
500+
if total, err := strconv.ParseInt(totalStr, 10, 64); err != nil {
501+
err = fmt.Errorf("failed extracting file size: %s", totalStr)
502+
} else {
503+
totalBytes = total
505504
}
506505
} else {
507-
err = fmt.Errorf("file size unknown")
506+
err = fmt.Errorf("file size unknown: %s", contentRange)
508507
}
509508

510-
totalBytes = total
511509
}
512510
if totalBytes != d.params.Size && err == nil {
513511
err = fmt.Errorf("expect file size=%d unmatch remote report size=%d, need refresh cache", d.params.Size, totalBytes)
514512
}
515513
if err != nil {
516-
// _ = d.interrupt()
517514
d.setErr(err)
518515
d.cancel(err)
519516
}

internal/op/fs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ func list(ctx context.Context, storage driver.Driver, path string, args model.Li
8484

8585
customCachePolicies := storage.GetStorage().CustomCachePolicies
8686
if len(customCachePolicies) > 0 {
87-
configPolicies := strings.Split(customCachePolicies, "\n")
88-
for _, configPolicy := range configPolicies {
87+
for configPolicy := range strings.SplitSeq(customCachePolicies, "\n") {
8988
pattern, ttlstr, ok := strings.Cut(strings.TrimSpace(configPolicy), ":")
9089
if !ok {
9190
log.Warnf("Malformed custom cache policy entry: %s in storage %s for path %s. Expected format: pattern:ttl", configPolicy, storage.GetStorage().MountPath, path)

pkg/gowebdav/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ func (c *Client) MkdirAll(path string, _ os.FileMode) (err error) {
300300
return nil
301301
}
302302
if status == 409 {
303-
paths := strings.Split(path, "/")
303+
paths := strings.SplitSeq(path, "/")
304304
sub := "/"
305-
for _, e := range paths {
305+
for e := range paths {
306306
if e == "" {
307307
continue
308308
}

pkg/gowebdav/digestAuth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func digestParts(resp *http.Response) map[string]string {
4545
result := map[string]string{}
4646
if len(resp.Header["Www-Authenticate"]) > 0 {
4747
wantedHeaders := []string{"nonce", "realm", "qop", "opaque", "algorithm", "entityBody"}
48-
responseHeaders := strings.Split(resp.Header["Www-Authenticate"][0], ",")
49-
for _, r := range responseHeaders {
48+
responseHeaders := strings.SplitSeq(resp.Header["Www-Authenticate"][0], ",")
49+
for r := range responseHeaders {
5050
for _, w := range wantedHeaders {
5151
if strings.Contains(r, w) {
5252
result[w] = strings.Trim(

pkg/http_range/range.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ func ParseRange(s string, size int64) ([]Range, error) { // nolint:gocognit
4343
}
4444
var ranges []Range
4545
noOverlap := false
46-
for _, ra := range strings.Split(s[len(b):], ",") {
46+
for ra := range strings.SplitSeq(s[len(b):], ",") {
4747
ra = textproto.TrimString(ra)
4848
if ra == "" {
4949
continue
5050
}
51-
i := strings.Index(ra, "-")
52-
if i < 0 {
51+
before, after, ok := strings.Cut(ra, "-")
52+
if !ok {
5353
return nil, ErrInvalid
5454
}
55-
start, end := textproto.TrimString(ra[:i]), textproto.TrimString(ra[i+1:])
55+
start, end := textproto.TrimString(before), textproto.TrimString(after)
5656
var r Range
5757
if start == "" {
5858
// If no start is specified, end specifies the

0 commit comments

Comments
 (0)