Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(completion): compile regexp outside of any loops
Browse files Browse the repository at this point in the history
kangasta committed Feb 6, 2023
1 parent 4a06dae commit 7da143e
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions internal/completion/helpers.go
Original file line number Diff line number Diff line change
@@ -5,12 +5,13 @@ import (
"strings"
)

var oneOrMoreWhitespace = regexp.MustCompile(`\s+`)

// RemoveWordBreaks replaces all whitespaces in input strings with non-breaking spaces to prevent bash from splitting completion with whitespace into multiple completions.
//
// This hack allows us to use cobras built-in completion logic and can be removed once cobra supports whitespace in bash completions (See https://github.com/spf13/cobra/issues/1740).
func RemoveWordBreaks(input string) string {
re := regexp.MustCompile(`\s+`)
return re.ReplaceAllString(input, "\u00A0")
return oneOrMoreWhitespace.ReplaceAllString(input, "\u00A0")
}

// MatchStringPrefix returns a list of string in vals which have a prefix as specified in key. Quotes are removed from key and output strings are escaped according to completion rules

0 comments on commit 7da143e

Please sign in to comment.