1
mirror of https://github.com/rclone/rclone synced 2024-11-21 22:50:16 +01:00

drive: extract function for escaping search titles

Slight optimization opportunity. Instead of calling `strings.ReplaceAll` twice, we can loop over the search title once while minimizing unnecessary allocations.
This commit is contained in:
YenForYang 2022-06-18 07:22:13 -05:00 committed by GitHub
parent b9de37af80
commit 370ae6f5f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -806,6 +806,25 @@ func (f *Fs) getRootID(ctx context.Context) (string, error) {
return info.Id, nil
}
// Replaces `\` to `\\` and `'` to `\'`
// Escaping backslashes requirement is not documented
// See https://stackoverflow.com/a/32310092
func escapeSingleQuotesAndBackslashes(s string) string {
n := strings.Count(s, "\\") + strings.Count(s, "'")
var b strings.Builder
b.Grow(len(s) + n)
start := 0
for pos, c := range s {
if c == '\\' || c == '\'' {
b.WriteString(s[start:pos])
b.Write([]byte{'\\', byte(c)})
start = pos + 1
}
}
b.WriteString(s[start:])
return b.String()
}
// Lists the directory required calling the user function on each item found
//
// If the user fn ever returns true then it early exits with found = true
@ -860,8 +879,7 @@ func (f *Fs) list(ctx context.Context, dirIDs []string, title string, directorie
if title != "" {
searchTitle := f.opt.Enc.FromStandardName(title)
// Escaping the backslash isn't documented but seems to work
searchTitle = strings.ReplaceAll(searchTitle, `\`, `\\`)
searchTitle = strings.ReplaceAll(searchTitle, `'`, `\'`)
searchTitle = escapeSingleQuotesAndBackslashes(searchTitle)
var titleQuery bytes.Buffer
_, _ = fmt.Fprintf(&titleQuery, "(name='%s'", searchTitle)