From 9be7f99bf8287084a91b8a6e3fd554f23e2c1658 Mon Sep 17 00:00:00 2001 From: vicerace Date: Tue, 25 Nov 2025 18:26:16 +0800 Subject: [PATCH] refactor: use strings.Cut to simplify code Signed-off-by: vicerace --- backend/crypt/cipher.go | 10 +++++----- cmd/bisync/listing.go | 4 ++-- cmd/serve/sftp/connection.go | 8 ++++---- fs/bwtimetable.go | 8 ++++---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/backend/crypt/cipher.go b/backend/crypt/cipher.go index c9a3786ef..6f4c987f6 100644 --- a/backend/crypt/cipher.go +++ b/backend/crypt/cipher.go @@ -403,14 +403,14 @@ func (c *Cipher) deobfuscateSegment(ciphertext string) (string, error) { if ciphertext == "" { return "", nil } - pos := strings.Index(ciphertext, ".") - if pos == -1 { + before, after, ok := strings.Cut(ciphertext, ".") + if !ok { return "", ErrorNotAnEncryptedFile } // No . - num := ciphertext[:pos] + num := before if num == "!" { // No rotation; probably original was not valid unicode - return ciphertext[pos+1:], nil + return after, nil } dir, err := strconv.Atoi(num) if err != nil { @@ -425,7 +425,7 @@ func (c *Cipher) deobfuscateSegment(ciphertext string) (string, error) { var result bytes.Buffer inQuote := false - for _, runeValue := range ciphertext[pos+1:] { + for _, runeValue := range after { switch { case inQuote: _, _ = result.WriteRune(runeValue) diff --git a/cmd/bisync/listing.go b/cmd/bisync/listing.go index 1a15fe56a..181301ef4 100644 --- a/cmd/bisync/listing.go +++ b/cmd/bisync/listing.go @@ -389,8 +389,8 @@ func parseHash(str string) (string, string, error) { if str == "-" { return "", "", nil } - if pos := strings.Index(str, ":"); pos > 0 { - name, val := str[:pos], str[pos+1:] + if before, after, ok := strings.Cut(str, ":"); ok { + name, val := before, after if name != "" && val != "" { return name, val, nil } diff --git a/cmd/serve/sftp/connection.go b/cmd/serve/sftp/connection.go index 3fc4396cf..5d174c2ab 100644 --- a/cmd/serve/sftp/connection.go +++ b/cmd/serve/sftp/connection.go @@ -58,10 +58,10 @@ type conn struct { // interoperate with the rclone sftp backend func (c *conn) execCommand(ctx context.Context, out io.Writer, command string) (err error) { binary, args := command, "" - space := strings.Index(command, " ") - if space >= 0 { - binary = command[:space] - args = strings.TrimLeft(command[space+1:], " ") + before, after, ok := strings.Cut(command, " ") + if ok { + binary = before + args = strings.TrimLeft(after, " ") } args = shellUnEscape(args) fs.Debugf(c.what, "exec command: binary = %q, args = %q", binary, args) diff --git a/fs/bwtimetable.go b/fs/bwtimetable.go index f17e3ee30..0f7597d11 100644 --- a/fs/bwtimetable.go +++ b/fs/bwtimetable.go @@ -29,16 +29,16 @@ func (bp *BwPair) String() string { // Set the bandwidth from a string which is either // SizeSuffix or SizeSuffix:SizeSuffix (for tx:rx bandwidth) func (bp *BwPair) Set(s string) (err error) { - colon := strings.Index(s, ":") + before, after, ok := strings.Cut(s, ":") stx, srx := s, "" - if colon >= 0 { - stx, srx = s[:colon], s[colon+1:] + if ok { + stx, srx = before, after } err = bp.Tx.Set(stx) if err != nil { return err } - if colon < 0 { + if !ok { bp.Rx = bp.Tx } else { err = bp.Rx.Set(srx)