1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-06 00:03:32 +00:00

refactor: use strings.Cut to simplify code

Signed-off-by: vicerace <vicerace@sohu.com>
This commit is contained in:
vicerace
2025-11-25 18:26:16 +08:00
committed by dougal
parent 6858bf242e
commit 9be7f99bf8
4 changed files with 15 additions and 15 deletions

View File

@@ -403,14 +403,14 @@ func (c *Cipher) deobfuscateSegment(ciphertext string) (string, error) {
if ciphertext == "" { if ciphertext == "" {
return "", nil return "", nil
} }
pos := strings.Index(ciphertext, ".") before, after, ok := strings.Cut(ciphertext, ".")
if pos == -1 { if !ok {
return "", ErrorNotAnEncryptedFile return "", ErrorNotAnEncryptedFile
} // No . } // No .
num := ciphertext[:pos] num := before
if num == "!" { if num == "!" {
// No rotation; probably original was not valid unicode // No rotation; probably original was not valid unicode
return ciphertext[pos+1:], nil return after, nil
} }
dir, err := strconv.Atoi(num) dir, err := strconv.Atoi(num)
if err != nil { if err != nil {
@@ -425,7 +425,7 @@ func (c *Cipher) deobfuscateSegment(ciphertext string) (string, error) {
var result bytes.Buffer var result bytes.Buffer
inQuote := false inQuote := false
for _, runeValue := range ciphertext[pos+1:] { for _, runeValue := range after {
switch { switch {
case inQuote: case inQuote:
_, _ = result.WriteRune(runeValue) _, _ = result.WriteRune(runeValue)

View File

@@ -389,8 +389,8 @@ func parseHash(str string) (string, string, error) {
if str == "-" { if str == "-" {
return "", "", nil return "", "", nil
} }
if pos := strings.Index(str, ":"); pos > 0 { if before, after, ok := strings.Cut(str, ":"); ok {
name, val := str[:pos], str[pos+1:] name, val := before, after
if name != "" && val != "" { if name != "" && val != "" {
return name, val, nil return name, val, nil
} }

View File

@@ -58,10 +58,10 @@ type conn struct {
// interoperate with the rclone sftp backend // interoperate with the rclone sftp backend
func (c *conn) execCommand(ctx context.Context, out io.Writer, command string) (err error) { func (c *conn) execCommand(ctx context.Context, out io.Writer, command string) (err error) {
binary, args := command, "" binary, args := command, ""
space := strings.Index(command, " ") before, after, ok := strings.Cut(command, " ")
if space >= 0 { if ok {
binary = command[:space] binary = before
args = strings.TrimLeft(command[space+1:], " ") args = strings.TrimLeft(after, " ")
} }
args = shellUnEscape(args) args = shellUnEscape(args)
fs.Debugf(c.what, "exec command: binary = %q, args = %q", binary, args) fs.Debugf(c.what, "exec command: binary = %q, args = %q", binary, args)

View File

@@ -29,16 +29,16 @@ func (bp *BwPair) String() string {
// Set the bandwidth from a string which is either // Set the bandwidth from a string which is either
// SizeSuffix or SizeSuffix:SizeSuffix (for tx:rx bandwidth) // SizeSuffix or SizeSuffix:SizeSuffix (for tx:rx bandwidth)
func (bp *BwPair) Set(s string) (err error) { func (bp *BwPair) Set(s string) (err error) {
colon := strings.Index(s, ":") before, after, ok := strings.Cut(s, ":")
stx, srx := s, "" stx, srx := s, ""
if colon >= 0 { if ok {
stx, srx = s[:colon], s[colon+1:] stx, srx = before, after
} }
err = bp.Tx.Set(stx) err = bp.Tx.Set(stx)
if err != nil { if err != nil {
return err return err
} }
if colon < 0 { if !ok {
bp.Rx = bp.Tx bp.Rx = bp.Tx
} else { } else {
err = bp.Rx.Set(srx) err = bp.Rx.Set(srx)