mirror of
https://github.com/ddworken/hishtory.git
synced 2025-08-09 23:17:55 +02:00
* 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:
@ -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])
|
||||
}
|
||||
|
Reference in New Issue
Block a user