Add support for quoted searchs for exact matches, for #135 (#145)

* Add support for quoted searchs for exact matches, for #135

* Add support for quoting search queries

* Fix spliteEscaped so that it works with escaping dashes and colons in search queries
This commit is contained in:
David Dworken
2023-12-12 22:20:49 -08:00
committed by GitHub
parent 1be8e2cb47
commit 1b3fa944bd
8 changed files with 149 additions and 11 deletions

View File

@ -978,19 +978,29 @@ func tokenize(query string) []string {
return splitEscaped(query, ' ', -1)
}
// TODO: Maybe add support for searching for the backslash character itself?
func splitEscaped(query string, separator rune, maxSplit int) []string {
var token []rune
var tokens []string
splits := 1
runeQuery := []rune(query)
isInQuotedString := false
for i := 0; i < len(runeQuery); i++ {
if (maxSplit < 0 || splits < maxSplit) && runeQuery[i] == separator {
if (maxSplit < 0 || splits < maxSplit) && runeQuery[i] == separator && !isInQuotedString {
tokens = append(tokens, string(token))
token = token[:0]
splits++
} else if runeQuery[i] == '\\' && i+1 < len(runeQuery) {
token = append(token, runeQuery[i], runeQuery[i+1])
if runeQuery[i+1] == '-' || runeQuery[i+1] == ':' || runeQuery[i+1] == '\\' {
// Note that we need to keep the backslash before the dash to support searches like `ls \-Slah`.
// And we need it before the colon so that we can search for things like `foo\:bar`
// And we need it before the backslash so that we can search for literal backslashes.
token = append(token, runeQuery[i])
}
i++
token = append(token, runeQuery[i])
} else if runeQuery[i] == '"' {
isInQuotedString = !isInQuotedString
} else {
token = append(token, runeQuery[i])
}